diff --git a/mpvremote/app.py b/mpvremote/app.py index d2cd2f2..912fc5e 100644 --- a/mpvremote/app.py +++ b/mpvremote/app.py @@ -1,6 +1,5 @@ import curses import threading -import time from pathlib import Path from mpvremote import config from mpvremote.mpv_client import MpvClient @@ -38,8 +37,8 @@ class App: self._video_cache = () self._video_cache_version = 0 self._video_cache_lock = threading.Lock() + self._initial_scan_complete = threading.Event() self._rescan_stop = threading.Event() - self._scan_videos() self._rescan_thread = threading.Thread(target=self._rescan_loop, daemon=True) self._rescan_thread.start() @@ -77,6 +76,8 @@ class App: self._commit_video_cache(videos) except Exception: pass + finally: + self._initial_scan_complete.set() def _commit_video_cache(self, videos): """Publish an immutable, pre-normalized snapshot of scanned videos.""" @@ -87,14 +88,11 @@ class App: self._video_cache_version += 1 def _rescan_loop(self): - """Background thread that rescans every 2 minutes.""" + """Scan immediately, then rescan every 2 minutes in the background.""" while not self._rescan_stop.is_set(): - for _ in range(120): - if self._rescan_stop.is_set(): - break - time.sleep(1) - if not self._rescan_stop.is_set(): - self._scan_videos() + self._scan_videos() + if self._rescan_stop.wait(120): + break def get_videos(self): """Return cached video list. Thread-safe.""" @@ -106,12 +104,21 @@ class App: with self._video_cache_lock: return self._video_cache, self._video_cache_version + def video_scan_complete(self): + """Return whether at least one media-directory scan has finished.""" + return self._initial_scan_complete.is_set() + def refresh_videos(self): """Rescan the configured media directory immediately.""" self._scan_videos() def run(self): while self.running: + queue_active = self.current_view is self.views['queue'] + self.stdscr.timeout(100 if queue_active else -1) + update = getattr(self.current_view, "update", None) + if update is not None: + update() self.draw() key = self.stdscr.getch() if key == -1: diff --git a/mpvremote/views.py b/mpvremote/views.py index cf51cb2..12f4abc 100644 --- a/mpvremote/views.py +++ b/mpvremote/views.py @@ -1,4 +1,5 @@ import curses +import os from mpvremote import config @@ -218,6 +219,10 @@ class QueueView: def open(self): self._apply_filter() + def update(self): + """Refresh results when the background scan publishes a new cache.""" + self._apply_filter() + def _apply_filter(self): videos, cache_version = self.app.get_videos_with_version() search_changed = self.search != self._last_search @@ -291,6 +296,18 @@ class QueueView: except curses.error: pass + if not self.filtered and self.app.height > 3: + if not self.app.video_scan_complete(): + message = "Scanning media directory..." + elif self.search: + message = "No matching videos" + else: + message = "No videos found" + try: + self.app.stdscr.addstr(2, 0, message[:self.app.width - 1]) + except curses.error: + pass + self.app.draw_help_bar(" [Type]Filter [Up/Down]Navigate [Enter]Queue [Esc]Back") def handle_key(self, key): @@ -395,13 +412,23 @@ class SettingsView: self.app.switch_view('main') def _save(self): + previous_dir = os.path.normcase( + os.path.abspath(os.path.expanduser(config.MEDIA_DIR)) + ) + requested_dir = os.path.normcase( + os.path.abspath(os.path.expanduser(self.path.strip())) + ) try: config.set_media_dir(self.path) except (OSError, ValueError) as exc: self.error = f"Could not save directory: {exc}" return self.app.refresh_videos() - self.app.add_message(f">> Media directory set to {config.MEDIA_DIR}") + if requested_dir == previous_dir: + message = f">> Media directory refreshed: {config.MEDIA_DIR}" + else: + message = f">> Media directory set to {config.MEDIA_DIR}" + self.app.add_message(message) self._close() def handle_key(self, key):