Skip to content

Commit b323a6f

Browse files
committed
feat: add file watcher for clipboard history updates
- Introduced a `_start_history_watcher` method in `DataManager` to monitor changes in the history file and trigger updates. - Added a callback `_on_history_updated` in `ClipboardHistoryController` to handle history updates and refresh the UI. - Updated `DataManager` to accept an `update_callback` parameter for notifying changes. - Added new constants (`COMPACT_MODE`, `ENTER_TO_PASTE`, `PASTE_TOOL_CMD`, `X11_PASTE_TOOL_CMD`) in `constants.py` for additional configuration options.
1 parent bc03a5e commit b323a6f

File tree

4 files changed

+51
-2
lines changed

4 files changed

+51
-2
lines changed

.github/workflows/release.yml

+3
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ on:
55
tags:
66
- "v*.*.*"
77

8+
permissions:
9+
contents: write
10+
811
jobs:
912
release:
1013
runs-on: ubuntu-latest

clipse_gui/app.py

+12-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ def __init__(self, application_window: Gtk.ApplicationWindow):
6060
self._vadjustment_handler_id = None
6161

6262
# --- Initialize Managers ---
63-
self.data_manager = DataManager(HISTORY_FILE_PATH)
63+
self.data_manager = DataManager(HISTORY_FILE_PATH, self._on_history_updated)
6464
self.image_handler = ImageHandler(IMAGE_CACHE_MAX_SIZE)
6565

6666
# --- Build UI Content ---
@@ -153,15 +153,26 @@ def _apply_css(self):
153153
else:
154154
log.debug("CSS provider already exists.")
155155

156+
def _on_history_updated(self, loaded_items):
157+
"""Callback function called when the file is updated."""
158+
self.items = loaded_items
159+
self.update_filtered_items()
160+
self._focus_first_item()
161+
156162
def _load_initial_data(self):
157163
"""Loads history in background thread."""
158164
loaded_items = self.data_manager.load_history()
165+
159166
GLib.idle_add(self._finish_initial_load, loaded_items)
160167

168+
self.data_manager._start_history_watcher(self._on_history_updated)
169+
161170
def _finish_initial_load(self, loaded_items):
162171
"""Updates UI after initial data load."""
163172
self.items = loaded_items
173+
164174
self.update_filtered_items()
175+
165176
if not self.items:
166177
self.status_label.set_text("No history items found. Press ? for help.")
167178
else:

clipse_gui/constants.py

+6
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,12 @@
3131
SAVE_DEBOUNCE_MS = 500
3232
SEARCH_DEBOUNCE_MS = 250
3333

34+
COMPACT_MODE = True
35+
ENTER_TO_PASTE = True
36+
PASTE_TOOL_CMD = "wl-paste --no-newline"
37+
X11_PASTE_TOOL_CMD = "xclip -o -selection clipboard"
38+
39+
3440
# --- CSS Styling ---
3541
APP_CSS = """
3642
.pinned-row {

clipse_gui/data_manager.py

+30-1
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,10 @@
1515
class DataManager:
1616
"""Handles loading and saving clipboard history data."""
1717

18-
def __init__(self, file_path):
18+
def __init__(self, file_path, update_callback=None):
1919
self.file_path = file_path
2020
self._save_lock = threading.Lock() # Lock for saving operation
21+
self.update_callback = update_callback
2122

2223
def load_history(self):
2324
"""Loads history from the JSON file."""
@@ -83,3 +84,31 @@ def _save_thread_target(self, items_to_save, callback_on_error):
8384
log.error(f"Error saving history to {self.file_path}: {e}")
8485
if callback_on_error:
8586
GLib.idle_add(callback_on_error, f"Error saving: {e}")
87+
88+
def _start_history_watcher(self, callback, interval_ms=300):
89+
"""Starts a periodic file watcher to sync clipboard history."""
90+
self._last_file_content = None
91+
92+
def check_for_changes():
93+
try:
94+
if not os.path.exists(self.file_path):
95+
return True
96+
97+
with open(self.file_path, "r", encoding="utf-8") as f:
98+
current_content = f.read()
99+
100+
if self._last_file_content is None:
101+
self._last_file_content = current_content
102+
return True
103+
104+
if current_content != self._last_file_content:
105+
self._last_file_content = current_content
106+
log.info("History file updated. Reloading...")
107+
loaded_items = self.load_history()
108+
GLib.idle_add(callback, loaded_items)
109+
except Exception as e:
110+
log.error(f"Error while watching history file: {e}")
111+
112+
return True
113+
114+
GLib.timeout_add(interval_ms, check_for_changes)

0 commit comments

Comments
 (0)