GEN_PARAMS, GEN_PARAMS_STR) # noqa: E402
IMG_DIR_DEFAULT = '.'
+SORT_DEFAULT = 'guidance,n_steps,model,scheduler,prompt,seed,height,width'
UPPER_DIR = '..'
CACHE_PATH = 'cache.json'
def init_sort_orderer():
self.sort_order = [p.lower() for p in GEN_PARAMS]
- self.sort_order += ['last_mod_time', 'name']
+ new_sort_order = []
+ do_reverse = '-' in self.app.suggested_sort_order
+ for pattern in self.app.suggested_sort_order:
+ for name in [n for n in self.sort_order
+ if n.startswith(pattern)]:
+ self.sort_order.remove(name)
+ new_sort_order += [name]
+ self.sort_order = new_sort_order + self.sort_order
+ if do_reverse:
+ self.sort_order.reverse()
self.sort_store = Gio.ListStore(item_type=SortLabelItem)
self.sort_selection = Gtk.SingleSelection.new(self.sort_store)
factory = Gtk.SignalListItemFactory()
if self.include_dirs:
if isinstance(a, DirItem) and isinstance(b, DirItem):
cmp_upper_dir = f' {UPPER_DIR}'
- if cmp_upper_dir in (a.name, b.name):
- return cmp_upper_dir == b.name
+ if a.name == cmp_upper_dir:
+ return +1
+ if b.name == cmp_upper_dir:
+ return -1
elif isinstance(a, DirItem):
- return False
+ return +1
elif isinstance(b, DirItem):
- return True
+ return +1
# apply self.sort_order within DirItems and FileItems (separately)
+ result = 0
for key in self.sort_order:
a_cmp = None
b_cmp = None
a_cmp = getattr(a, key)
if hasattr(b, key):
b_cmp = getattr(b, key)
- if a_cmp is None and b_cmp is None:
- return True
- if a_cmp is None:
- return False
- if b_cmp is None:
- return True
- return a_cmp > b_cmp
+ if a_cmp is None and b_cmp is not None:
+ result = -1
+ elif a_cmp is not None and b_cmp is None:
+ result = +1
+ elif a_cmp > b_cmp:
+ result = +1
+ elif a_cmp < b_cmp:
+ result = -1
+ return result
def init_gallery_slot(file_item):
slot = Gtk.Box()
class Application(Gtk.Application):
"""Image browser application class."""
img_dir_absolute: str
+ suggested_sort_order: list[str]
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
"""Parse arguments, start window, and keep it open."""
parser = ArgumentParser()
parser.add_argument('directory', default=IMG_DIR_DEFAULT, nargs='?')
+ parser.add_argument('-s', '--sort-order', default=SORT_DEFAULT)
opts = parser.parse_args()
self.img_dir_absolute = abspath(opts.directory)
+ self.suggested_sort_order = opts.sort_order.split(',')
win = MainWindow(self)
win.present()
self.hold()