mirror of
https://github.com/gustavosett/Windows-11-Clipboard-History-For-Linux
synced 2026-04-25 17:15:35 +02:00
feat: move pasted item to top (#171)
* feat: move pasted item to top * chore: avoid moving item if its correct position --------- Co-authored-by: gustavosett <gustavosett@debian.myguest.virtualbox.org>
This commit is contained in:
@@ -532,6 +532,38 @@ impl ClipboardManager {
|
||||
Some(item_clone)
|
||||
}
|
||||
|
||||
/// Move an item to the top of the history (respecting pinned items)
|
||||
/// If the item is pinned, it moves to the top of pinned items
|
||||
/// If not pinned, it moves to the first non-pinned position
|
||||
pub fn move_item_to_top(&mut self, id: &str) -> bool {
|
||||
// Find the item's current position
|
||||
let current_pos = match self.history.iter().position(|i| i.id == id) {
|
||||
Some(pos) => pos,
|
||||
None => return false, // Item not found
|
||||
};
|
||||
// Determine where we *would* insert based on pinned status, without mutating yet
|
||||
let item_pinned = self.history[current_pos].pinned;
|
||||
let insert_pos = if item_pinned {
|
||||
// Move to top of pinned items (position 0)
|
||||
0
|
||||
} else {
|
||||
// Move to first non-pinned position (right after all pinned items)
|
||||
self.history
|
||||
.iter()
|
||||
.position(|i| !i.pinned)
|
||||
.unwrap_or(self.history.len())
|
||||
};
|
||||
// If the item is already at the correct position, avoid unnecessary mutation and I/O
|
||||
if insert_pos == current_pos {
|
||||
return true;
|
||||
}
|
||||
// Now actually move the item
|
||||
let item = self.history.remove(current_pos);
|
||||
self.history.insert(insert_pos, item);
|
||||
self.save_history();
|
||||
true
|
||||
}
|
||||
|
||||
pub fn cleanup_old_items(&mut self, interval_minutes: u64) -> bool {
|
||||
if interval_minutes == 0 {
|
||||
return false;
|
||||
@@ -624,6 +656,9 @@ impl ClipboardManager {
|
||||
// 3. Simulate User Input
|
||||
self.simulate_paste_action()?;
|
||||
|
||||
// 4. Move item to top of history so it's easily accessible for repeated use
|
||||
self.move_item_to_top(&item.id);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
||||
@@ -166,6 +166,11 @@ async fn paste_item(app: AppHandle, state: State<'_, AppState>, id: String) -> R
|
||||
// 3. Perform Paste
|
||||
let mut manager = state.clipboard_manager.lock();
|
||||
manager.paste_item(&item).map_err(|e| e.to_string())?;
|
||||
|
||||
// 4. Notify frontend of history change (item moved to top)
|
||||
let history = manager.get_history();
|
||||
drop(manager); // Release lock before emitting
|
||||
let _ = app.emit("history-sync", &history);
|
||||
}
|
||||
None => {
|
||||
eprintln!(
|
||||
|
||||
Reference in New Issue
Block a user