Compare commits

..

No commits in common. "b0375c863ea7f61171cb1007be074760eeb25a00" and "4ed4aa17d2eaa3e99a1f94273fe03739050411cf" have entirely different histories.

2 changed files with 41 additions and 59 deletions

View file

@ -34,7 +34,7 @@ class MainWindow(Gtk.ApplicationWindow):
super().__init__(*args, **kwargs) super().__init__(*args, **kwargs)
# Directory history stack # Directory history stack
self.directory_history: list[Path] = [] self.directory_history: list[str] = []
# For overlay text timeout # For overlay text timeout
self.overlay_hide_time = 0.0 self.overlay_hide_time = 0.0
@ -151,7 +151,10 @@ class MainWindow(Gtk.ApplicationWindow):
file_item = cast(FileItem, selected_item) file_item = cast(FileItem, selected_item)
if file_item.file_type == FileType.DIRECTORY: if file_item.file_type == FileType.DIRECTORY:
self._navigate_to(file_item.full_path) # Save current directory before changing
self.directory_history.append(os.getcwd())
os.chdir(file_item.full_path)
self._populate_file_list()
return return
position = file_item.load_attribute("position", 0) position = file_item.load_attribute("position", 0)
@ -263,46 +266,6 @@ class MainWindow(Gtk.ApplicationWindow):
item.connect("notify::attrs-changed", update_icon) item.connect("notify::attrs-changed", update_icon)
update_icon() update_icon()
def _navigate_to(self, path: Path):
# Check if we have history and if target is where we came from
if self.directory_history:
print(path, "vs", self.directory_history[-1])
if path == self.directory_history[-1]:
print("navigate back")
self._navigate_back()
return
print("navigate to", path)
# Regular directory navigation
self.directory_history.append(Path(os.getcwd()))
os.chdir(path)
self._populate_file_list()
def _navigate_back(self):
if not self.directory_history:
return
prev_dir = self.directory_history.pop()
current_dir = Path(os.getcwd())
os.chdir(prev_dir)
self._populate_file_list()
# Find and select the directory we came from
for i in range(self.list_store.get_n_items()):
item = self.list_store.get_item(i)
if not item:
continue
file_item = cast(FileItem, item)
if current_dir == file_item.full_path:
self.list_view.scroll_to(
i, Gtk.ListScrollFlags.SELECT | Gtk.ListScrollFlags.FOCUS, None
)
break
return True
def _on_selection_changed( def _on_selection_changed(
self, self,
selection_model: Gtk.SingleSelection, selection_model: Gtk.SingleSelection,
@ -315,7 +278,7 @@ class MainWindow(Gtk.ApplicationWindow):
selected_item = selection_model.get_selected_item() selected_item = selection_model.get_selected_item()
if selected_item: if selected_item:
file_item = cast(FileItem, selected_item) file_item = cast(FileItem, selected_item)
self.file_info_label.set_text(file_item.name) self.file_info_label.set_text(file_item.full_path)
def _toggle_play_pause(self) -> None: def _toggle_play_pause(self) -> None:
"""Toggle between play and pause states""" """Toggle between play and pause states"""
@ -412,7 +375,20 @@ class MainWindow(Gtk.ApplicationWindow):
return True return True
elif keyval == Gdk.keyval_from_name("BackSpace"): elif keyval == Gdk.keyval_from_name("BackSpace"):
self._navigate_back() if self.directory_history:
prev_dir = self.directory_history.pop()
current_dir_name = Path(os.getcwd()).name
os.chdir(prev_dir)
self._populate_file_list()
# Find and select the directory we came from
for i in range(self.list_store.get_n_items()):
item = self.list_store.get_item(i)
if item and cast(FileItem, item).name == current_dir_name:
self.list_view.scroll_to(
i, Gtk.ListScrollFlags.SELECT | Gtk.ListScrollFlags.FOCUS, None
)
break
return True return True
return False return False
@ -540,15 +516,20 @@ class MainWindow(Gtk.ApplicationWindow):
items: list[FileItem] = [] items: list[FileItem] = []
# Add parent directory # Add parent directory
items.append(FileItem("..", FileType.DIRECTORY, Path("..").resolve())) items.append(FileItem("..", FileType.DIRECTORY, "../"))
with os.scandir(".") as it: with os.scandir(".") as it:
for entry in it: for entry in it:
if not entry.name.startswith("."): if entry.name != ".." and not entry.name.startswith("."):
try: try:
items.append(FileItem.from_path(entry.name)) if entry.is_dir():
items.append(FileItem(entry.name, FileType.DIRECTORY, entry.name + "/"))
else:
file_item = FileItem.from_path(entry.name)
items.append(file_item)
except ValueError: except ValueError:
continue # Skip unsupported file types # Skip unsupported file types
continue
# Sort directories first, then files, both alphabetically # Sort directories first, then files, both alphabetically
items.sort(key=lambda x: (x.file_type != FileType.DIRECTORY, x.name.lower())) items.sort(key=lambda x: (x.file_type != FileType.DIRECTORY, x.name.lower()))
@ -560,7 +541,7 @@ class MainWindow(Gtk.ApplicationWindow):
self.list_store.append(item) self.list_store.append(item)
if items: if items:
self.file_info_label.set_text(items[0].name) self.file_info_label.set_text(items[0].full_path)
class App(Gtk.Application): class App(Gtk.Application):

View file

@ -2,7 +2,6 @@ from __future__ import annotations
import os import os
from enum import Enum, auto from enum import Enum, auto
from pathlib import Path
from typing import overload from typing import overload
import gi import gi
@ -18,13 +17,13 @@ class FileType(Enum):
class FileItem(GObject.Object): class FileItem(GObject.Object):
file_type: FileType file_type: FileType
full_path: Path full_path: str
__gtype_name__ = "FileItem" __gtype_name__ = "FileItem"
attrs_changed = GObject.Property(type=int, default=0) attrs_changed = GObject.Property(type=int, default=0)
def __init__(self, name: str, file_type: FileType, full_path: Path): def __init__(self, name: str, file_type: FileType, full_path: str):
super().__init__() super().__init__()
self.attrs_changed = 0 self.attrs_changed = 0
self.name = name self.name = name
@ -32,16 +31,18 @@ class FileItem(GObject.Object):
self.full_path = full_path self.full_path = full_path
@staticmethod @staticmethod
def from_path(path: str | Path) -> FileItem: def from_path(path: str) -> FileItem:
full_path = Path(path).resolve() name = os.path.basename(path)
if path.endswith("/"):
return FileItem(name, FileType.DIRECTORY, path)
if full_path.is_dir(): parts = name.split(".")
return FileItem(full_path.name, FileType.DIRECTORY, full_path) suffix = parts[-1].lower() if len(parts) >= 2 else ""
if full_path.suffix in (".mkv", ".mp4", ".avi"): if suffix in ("mkv", "mp4", "avi"):
return FileItem(full_path.name, FileType.VIDEO, full_path) return FileItem(name, FileType.VIDEO, path)
raise ValueError(f"Unsupported file type: {full_path}") raise ValueError(f"Unsupported file type: {path}")
@overload @overload
def load_attribute(self, name: str, dfl: str) -> str: ... def load_attribute(self, name: str, dfl: str) -> str: ...