feat: add random sorting (#1029)

* feat: add random sorting

* fix: improve randomness of random sort
This commit is contained in:
TheBobBobs
2025-08-23 20:42:36 +00:00
committed by GitHub
parent 89cf2b22e4
commit 660a87bb94
3 changed files with 10 additions and 1 deletions

View File

@@ -1,4 +1,5 @@
import enum
import random
from dataclasses import dataclass, replace
from pathlib import Path
@@ -69,6 +70,7 @@ class SortingModeEnum(enum.Enum):
DATE_ADDED = "file.date_added"
FILE_NAME = "generic.filename"
PATH = "file.path"
RANDOM = "sorting.mode.random"
@dataclass
@@ -78,6 +80,7 @@ class BrowsingState:
page_index: int = 0
sorting_mode: SortingModeEnum = SortingModeEnum.DATE_ADDED
ascending: bool = True
random_seed: float = 0
query: str | None = None
@@ -133,7 +136,10 @@ class BrowsingState:
return replace(self, page_index=index)
def with_sorting_mode(self, mode: SortingModeEnum) -> "BrowsingState":
return replace(self, sorting_mode=mode)
seed = self.random_seed
if mode == SortingModeEnum.RANDOM:
seed = random.random()
return replace(self, sorting_mode=mode, random_seed=seed)
def with_sorting_direction(self, ascending: bool) -> "BrowsingState":
return replace(self, ascending=ascending)

View File

@@ -921,6 +921,8 @@ class Library:
sort_on = func.lower(Entry.filename)
case SortingModeEnum.PATH:
sort_on = func.lower(Entry.path)
case SortingModeEnum.RANDOM:
sort_on = func.sin(Entry.id * search.random_seed)
statement = statement.order_by(asc(sort_on) if search.ascending else desc(sort_on))
if page_size is not None:

View File

@@ -264,6 +264,7 @@
"settings.title": "Settings",
"sorting.direction.ascending": "Ascending",
"sorting.direction.descending": "Descending",
"sorting.mode.random": "Random",
"splash.opening_library": "Opening Library \"{library_path}\"...",
"status.deleted_file_plural": "Deleted {count} files!",
"status.deleted_file_singular": "Deleted 1 file!",