Highlight current selection
This commit is contained in:
parent
fb2aa6be2e
commit
93f1aed506
1 changed files with 31 additions and 10 deletions
|
@ -11,6 +11,8 @@ from gi.repository import Gdk, Gtk # NOQA: E402
|
||||||
|
|
||||||
|
|
||||||
class MainWindow(Gtk.ApplicationWindow):
|
class MainWindow(Gtk.ApplicationWindow):
|
||||||
|
file_info_label: Gtk.Label
|
||||||
|
|
||||||
def __init__(self, *args: Any, **kwargs: Any):
|
def __init__(self, *args: Any, **kwargs: Any):
|
||||||
super().__init__(*args, **kwargs)
|
super().__init__(*args, **kwargs)
|
||||||
|
|
||||||
|
@ -30,8 +32,8 @@ class MainWindow(Gtk.ApplicationWindow):
|
||||||
left_box = Gtk.Box(orientation=Gtk.Orientation.VERTICAL)
|
left_box = Gtk.Box(orientation=Gtk.Orientation.VERTICAL)
|
||||||
left_box.set_valign(Gtk.Align.CENTER)
|
left_box.set_valign(Gtk.Align.CENTER)
|
||||||
left_box.set_halign(Gtk.Align.FILL)
|
left_box.set_halign(Gtk.Align.FILL)
|
||||||
left_label = Gtk.Label(label="Left Side")
|
self.file_info_label = Gtk.Label(label="")
|
||||||
left_box.append(left_label)
|
left_box.append(self.file_info_label)
|
||||||
|
|
||||||
# Right two-thirds (2/3 width)
|
# Right two-thirds (2/3 width)
|
||||||
right_box = Gtk.Box(orientation=Gtk.Orientation.VERTICAL)
|
right_box = Gtk.Box(orientation=Gtk.Orientation.VERTICAL)
|
||||||
|
@ -46,7 +48,9 @@ class MainWindow(Gtk.ApplicationWindow):
|
||||||
# Create list store and view
|
# Create list store and view
|
||||||
list_store = Gtk.StringList()
|
list_store = Gtk.StringList()
|
||||||
list_view = Gtk.ListView()
|
list_view = Gtk.ListView()
|
||||||
list_view.set_model(Gtk.SingleSelection.new(list_store))
|
selection_model = Gtk.SingleSelection.new(list_store)
|
||||||
|
selection_model.connect("selection-changed", self._on_selection_changed)
|
||||||
|
list_view.set_model(selection_model)
|
||||||
list_view.set_vexpand(True)
|
list_view.set_vexpand(True)
|
||||||
|
|
||||||
# Factory for list items
|
# Factory for list items
|
||||||
|
@ -65,39 +69,56 @@ class MainWindow(Gtk.ApplicationWindow):
|
||||||
|
|
||||||
self.set_child(main_box)
|
self.set_child(main_box)
|
||||||
|
|
||||||
def _setup_list_item(self, factory: Gtk.SignalListItemFactory, list_item: Gtk.ListItem) -> None:
|
def _setup_list_item(self, factory: Gtk.SignalListItemFactory, list_item: Gtk.ListItem):
|
||||||
label = Gtk.Label()
|
label = Gtk.Label()
|
||||||
label.set_halign(Gtk.Align.START)
|
label.set_halign(Gtk.Align.START)
|
||||||
list_item.set_child(label)
|
list_item.set_child(label)
|
||||||
|
|
||||||
def _bind_list_item(self, factory: Gtk.SignalListItemFactory, list_item: Gtk.ListItem) -> None:
|
def _bind_list_item(self, factory: Gtk.SignalListItemFactory, list_item: Gtk.ListItem):
|
||||||
label = cast(Gtk.Label, list_item.get_child())
|
label = cast(Gtk.Label, list_item.get_child())
|
||||||
item = cast(Gtk.StringObject, list_item.get_item())
|
item = cast(Gtk.StringObject, list_item.get_item())
|
||||||
label.set_text(item.get_string())
|
label.set_text(item.get_string())
|
||||||
|
|
||||||
|
def _on_selection_changed(
|
||||||
|
self,
|
||||||
|
selection_model: Gtk.SingleSelection,
|
||||||
|
position: int,
|
||||||
|
n_items: int,
|
||||||
|
):
|
||||||
|
if selection_model.get_selected() == Gtk.INVALID_LIST_POSITION:
|
||||||
|
self.file_info_label.set_text("")
|
||||||
|
else:
|
||||||
|
selected_item = selection_model.get_selected_item()
|
||||||
|
if selected_item:
|
||||||
|
string_obj = cast(Gtk.StringObject, selected_item)
|
||||||
|
self.file_info_label.set_text(string_obj.get_string())
|
||||||
|
|
||||||
def _populate_file_list(self, list_store: Gtk.StringList) -> None:
|
def _populate_file_list(self, list_store: Gtk.StringList) -> None:
|
||||||
# TODO: Implement proper version sort (strverscmp equivalent)
|
# TODO: Implement proper version sort (strverscmp equivalent)
|
||||||
|
|
||||||
directories: list[str] = [".."]
|
directories: list[str] = ["../"]
|
||||||
files: list[str] = []
|
files: list[str] = []
|
||||||
|
|
||||||
with os.scandir(".") as it:
|
with os.scandir(".") as it:
|
||||||
for entry in it:
|
for entry in it:
|
||||||
if entry.name == ".." or not entry.name.startswith("."):
|
if entry.name == ".." or not entry.name.startswith("."):
|
||||||
if entry.is_dir():
|
if entry.is_dir():
|
||||||
directories.append(entry.name)
|
directories.append(entry.name + "/")
|
||||||
else:
|
else:
|
||||||
files.append(entry.name)
|
files.append(entry.name)
|
||||||
|
|
||||||
directories.sort(key=lambda x: x.lower())
|
directories.sort(key=lambda x: x.lower())
|
||||||
files.sort(key=lambda x: x.lower())
|
files.sort(key=lambda x: x.lower())
|
||||||
|
|
||||||
for name in directories:
|
while list_store.get_n_items():
|
||||||
list_store.append(f"{name}/")
|
list_store.remove(0)
|
||||||
|
|
||||||
for name in files:
|
for name in directories + files:
|
||||||
list_store.append(name)
|
list_store.append(name)
|
||||||
|
|
||||||
|
all = directories + files
|
||||||
|
self.file_info_label.set_text(all[0] if all else "")
|
||||||
|
|
||||||
|
|
||||||
class App(Gtk.Application):
|
class App(Gtk.Application):
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
|
|
Loading…
Reference in a new issue