lazy-player/lazy_player/__init__.py

106 lines
3.1 KiB
Python
Raw Normal View History

2025-03-08 19:39:55 +01:00
from __future__ import annotations
2025-03-08 19:55:49 +01:00
import os
from typing import Any, cast
2025-03-08 19:39:55 +01:00
import gi
gi.require_version("Gtk", "4.0")
from gi.repository import Gtk # NOQA: E402
class MainWindow(Gtk.ApplicationWindow):
def __init__(self, *args: Any, **kwargs: Any):
super().__init__(*args, **kwargs)
# Make window fullscreen and borderless
self.set_decorated(False)
self.fullscreen()
# Main horizontal box to split the screen
main_box = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL)
main_box.set_homogeneous(True) # Make both halves equal size
2025-03-08 19:55:49 +01:00
# Left half - File list
2025-03-08 19:39:55 +01:00
left_box = Gtk.Box(orientation=Gtk.Orientation.VERTICAL)
2025-03-08 19:55:49 +01:00
# 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)
2025-03-08 19:39:55 +01:00
# Right half
right_box = Gtk.Box(orientation=Gtk.Orientation.VERTICAL)
right_box.set_valign(Gtk.Align.CENTER)
right_box.set_halign(Gtk.Align.CENTER)
right_label = Gtk.Label(label="Right Side")
right_box.append(right_label)
# Add both halves to main box
main_box.append(left_box)
main_box.append(right_box)
self.set_child(main_box)
2025-03-08 19:55:49 +01:00
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)
2025-03-08 19:39:55 +01:00
class App(Gtk.Application):
def __init__(self):
super().__init__()
def do_activate(self):
win = MainWindow(application=self)
win.present()
def main():
app = App()
app.run(None)