Implement chdir

This commit is contained in:
Jan Hamal Dvořák 2025-03-08 21:20:29 +01:00
parent 981beffcce
commit 789f86de25

View file

@ -16,6 +16,7 @@ class MainWindow(Gtk.ApplicationWindow):
file_info_label: Gtk.Label
stack: Gtk.Stack
list_view: Gtk.ListView
list_store: Gtk.StringList
def __init__(self, *args: Any, **kwargs: Any):
super().__init__(*args, **kwargs)
@ -69,9 +70,9 @@ class MainWindow(Gtk.ApplicationWindow):
main_box.append(grid)
# Create list store and view
list_store = Gtk.StringList()
self.list_store = Gtk.StringList()
self.list_view = Gtk.ListView()
selection_model = Gtk.SingleSelection.new(list_store)
selection_model = Gtk.SingleSelection.new(self.list_store)
selection_model.connect("selection-changed", self._on_selection_changed)
self.list_view.set_model(selection_model)
self.list_view.set_vexpand(True)
@ -81,6 +82,12 @@ class MainWindow(Gtk.ApplicationWindow):
if selected_item:
string_obj = cast(Gtk.StringObject, selected_item)
string = string_obj.get_string()
if string.endswith("/"):
os.chdir(string)
self._populate_file_list()
return
print("activated", string)
self.stack.set_visible_child_name("overlay")
@ -94,7 +101,7 @@ class MainWindow(Gtk.ApplicationWindow):
self.list_view.set_factory(factory)
# Populate the list store
self._populate_file_list(list_store)
self._populate_file_list()
# Add list view to a scrolled window
scrolled = Gtk.ScrolledWindow()
@ -141,7 +148,7 @@ class MainWindow(Gtk.ApplicationWindow):
return False
def _populate_file_list(self, list_store: Gtk.StringList) -> None:
def _populate_file_list(self) -> None:
# TODO: Implement proper version sort (strverscmp equivalent)
directories: list[str] = ["../"]
@ -158,11 +165,11 @@ class MainWindow(Gtk.ApplicationWindow):
directories.sort(key=lambda x: x.lower())
files.sort(key=lambda x: x.lower())
while list_store.get_n_items():
list_store.remove(0)
while self.list_store.get_n_items():
self.list_store.remove(0)
for name in directories + files:
list_store.append(name)
self.list_store.append(name)
all = directories + files
self.file_info_label.set_text(all[0] if all else "")