List files

This commit is contained in:
Jan Hamal Dvořák 2025-03-08 19:55:49 +01:00
parent 246f132eec
commit bb7a45751e

View file

@ -1,6 +1,7 @@
from __future__ import annotations
from typing import Any
import os
from typing import Any, cast
import gi
@ -20,12 +21,28 @@ class MainWindow(Gtk.ApplicationWindow):
main_box = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL)
main_box.set_homogeneous(True) # Make both halves equal size
# Left half
# Left half - File list
left_box = Gtk.Box(orientation=Gtk.Orientation.VERTICAL)
left_box.set_valign(Gtk.Align.CENTER)
left_box.set_halign(Gtk.Align.CENTER)
left_label = Gtk.Label(label="Left Side")
left_box.append(left_label)
# Create list store and view
list_store = Gtk.StringList()
list_view = Gtk.ListView()
list_view.set_model(Gtk.SingleSelection.new(list_store))
list_view.set_vexpand(True)
# Factory for list items
factory = Gtk.SignalListItemFactory()
factory.connect("setup", self._setup_list_item)
factory.connect("bind", self._bind_list_item)
list_view.set_factory(factory)
# Populate the list store
self._populate_file_list(list_store)
# Add list view to a scrolled window
scrolled = Gtk.ScrolledWindow()
scrolled.set_child(list_view)
left_box.append(scrolled)
# Right half
right_box = Gtk.Box(orientation=Gtk.Orientation.VERTICAL)
@ -40,6 +57,39 @@ class MainWindow(Gtk.ApplicationWindow):
self.set_child(main_box)
def _setup_list_item(self, factory: Gtk.SignalListItemFactory, list_item: Gtk.ListItem) -> None:
label = Gtk.Label()
label.set_halign(Gtk.Align.START)
list_item.set_child(label)
def _bind_list_item(self, factory: Gtk.SignalListItemFactory, list_item: Gtk.ListItem) -> None:
label = cast(Gtk.Label, list_item.get_child())
item = cast(Gtk.StringObject, list_item.get_item())
label.set_text(item.get_string())
def _populate_file_list(self, list_store: Gtk.StringList) -> None:
# TODO: Implement proper version sort (strverscmp equivalent)
directories: list[str] = []
files: list[str] = []
with os.scandir(".") as it:
for entry in it:
if entry.name == ".." or not entry.name.startswith("."):
if entry.is_dir():
directories.append(entry.name)
else:
files.append(entry.name)
directories.sort()
files.sort()
for name in directories:
list_store.append(f"{name}/")
for name in files:
list_store.append(name)
class App(Gtk.Application):
def __init__(self):