From 81949d1fe43ed74813e6b21ffb62f4c143ef9aaf Mon Sep 17 00:00:00 2001 From: Hakim Date: Tue, 11 Aug 2026 15:22:08 +0200 Subject: [PATCH 1/2] feat(ui): remove the highlighted clip with the Delete key Delete (and KP_Delete) removes the highlighted clip, handing the cursor to the next match first so it can be held down the list. During an active search Delete still edits the query text (forward-delete), so filtering is unaffected. The pure KeyAction policy gains RemoveSelected and is unit-tested. --- README.md | 1 + src/ui/KeyAction.cpp | 8 ++++++++ src/ui/KeyAction.hpp | 1 + src/ui/MainWindow.cpp | 25 +++++++++++++++++++++++++ src/ui/MainWindow.hpp | 3 +++ tests/ui/KeyActionTest.cpp | 18 ++++++++++++++++++ 6 files changed, 56 insertions(+) diff --git a/README.md b/README.md index f3fb734..2897dff 100644 --- a/README.md +++ b/README.md @@ -127,6 +127,7 @@ To start hidden — for autostart entries — pass `--background`. | Super+C | Toggle the CopyClip window (default; configurable) | | Click | Copy the clip (and auto-paste if enabled) | | Ctrl+Click | Pin / unpin a clip | +| Delete | Remove the highlighted clip | | Type | Fuzzy-search the history | | Esc | Hide the window | diff --git a/src/ui/KeyAction.cpp b/src/ui/KeyAction.cpp index cede044..023b8b6 100644 --- a/src/ui/KeyAction.cpp +++ b/src/ui/KeyAction.cpp @@ -87,6 +87,14 @@ KeyAction key_action(unsigned int keyval, unsigned int modifiers, const KeyConte case GDK_KEY_Down: case GDK_KEY_KP_Down: return KeyAction::SelectNext; + case GDK_KEY_Delete: + case GDK_KEY_KP_Delete: + // Delete removes the highlighted clip — but only outside an active search, + // where the entry needs Delete to forward-delete the query text. + if (!context.search_active) { + return KeyAction::RemoveSelected; + } + break; default: break; } diff --git a/src/ui/KeyAction.hpp b/src/ui/KeyAction.hpp index 7a7b53f..145d3a6 100644 --- a/src/ui/KeyAction.hpp +++ b/src/ui/KeyAction.hpp @@ -19,6 +19,7 @@ enum class KeyAction : std::uint8_t { SelectNext, // move the highlight to the next match Paste, // paste the highlighted clip TogglePin, // pin / unpin the highlighted clip + RemoveSelected, // delete the highlighted clip from history }; // The window state the policy depends on. Grouped in a struct so the call site diff --git a/src/ui/MainWindow.cpp b/src/ui/MainWindow.cpp index 5f71a6c..b8a3f74 100644 --- a/src/ui/MainWindow.cpp +++ b/src/ui/MainWindow.cpp @@ -482,6 +482,9 @@ bool MainWindow::on_key_pressed(guint keyval, guint /*keycode*/, Gdk::ModifierTy // still points at a visible match after a just-typed query. sync_search_from_entry(); return pin_selection(); + case KeyAction::RemoveSelected: + // Only reached with an empty search (see key_action), so no filter to sync. + return remove_selection(); case KeyAction::None: return false; } @@ -557,6 +560,28 @@ void MainWindow::reset_to_top() { scrolled_->get_vadjustment()->set_value(kScrollTop); } +bool MainWindow::remove_selection() { + ClipCard* const card = selected_card(); + if (card == nullptr) { + return false; + } + const std::string content = card->entry().content; + // Hand the cursor to the neighbour that will outlive the removal — next match, + // else previous — so rebuild_cards restores the selection there instead of + // letting apply_filter snap it to the top. Delete can then be held down the list. + Gtk::ListBoxRow* neighbour = visible_row_from(card->get_next_sibling(), true); + if (neighbour == nullptr) { + neighbour = visible_row_from(card->get_prev_sibling(), false); + } + if (neighbour != nullptr) { + list_->select_row(*neighbour); + } + // remove() drops the entry whatever its pin state — Delete is an explicit user + // act, unlike the Clear button which spares pinned clips (clear_unpinned). + history_.get().remove(content); + return true; +} + void MainWindow::reveal(Gtk::ListBoxRow& row) { double row_x = 0.0; double row_y = 0.0; diff --git a/src/ui/MainWindow.hpp b/src/ui/MainWindow.hpp index 34edb37..efd3b6d 100644 --- a/src/ui/MainWindow.hpp +++ b/src/ui/MainWindow.hpp @@ -95,6 +95,9 @@ class MainWindow { // (gone, or hidden by the filter), leaving the cursor untouched for the caller to // decide what that means. bool cursor_to(const std::string& content); + // Delete the highlighted clip, moving the cursor to the next match first so the + // key can be pressed repeatedly down the list. False when nothing is selected. + bool remove_selection(); // The selected ClipCard, or nullptr when the list has no keyboard cursor. [[nodiscard]] ClipCard* selected_card() const; // Drop the filter immediately (entry + cached query + list). GtkSearchEntry's diff --git a/tests/ui/KeyActionTest.cpp b/tests/ui/KeyActionTest.cpp index bb21693..7880853 100644 --- a/tests/ui/KeyActionTest.cpp +++ b/tests/ui/KeyActionTest.cpp @@ -106,6 +106,24 @@ TEST(KeyActionTest, ModifiedArrowsFallThrough) { EXPECT_EQ(act(GDK_KEY_KP_Down, GDK_CONTROL_MASK | GDK_SHIFT_MASK), KeyAction::None); } +// Delete removes the highlighted clip when the user is not filtering. +TEST(KeyActionTest, DeleteRemovesTheSelectionWhenNotSearching) { + EXPECT_EQ(act(GDK_KEY_Delete), KeyAction::RemoveSelected); + EXPECT_EQ(act(GDK_KEY_KP_Delete), KeyAction::RemoveSelected); +} + +// Mid-search, Delete belongs to the entry (forward-delete a character), or the +// filter can't be edited with it. +TEST(KeyActionTest, DeleteEditsTextWhileSearching) { + EXPECT_EQ(act(GDK_KEY_Delete, 0, kSearching), KeyAction::None); + EXPECT_EQ(act(GDK_KEY_KP_Delete, 0, kSearching), KeyAction::None); +} + +// Modified Delete falls through so the entry keeps Ctrl+Delete (word delete). +TEST(KeyActionTest, ModifiedDeleteFallsThrough) { + EXPECT_EQ(act(GDK_KEY_Delete, GDK_CONTROL_MASK), KeyAction::None); +} + // Typing must reach the search entry untouched, or the filter stops working. TEST(KeyActionTest, OtherKeysFallThrough) { EXPECT_EQ(act(GDK_KEY_a, 0, kSearching), KeyAction::None); From b03727454414639343fba16aaa4f197853f33043 Mon Sep 17 00:00:00 2001 From: Walkercito Date: Wed, 12 Aug 2026 13:44:09 -0400 Subject: [PATCH 2/2] fix(ui): keep a held Delete from destroying clips mid-run key_action() re-read search_active on every press, so a run of Delete presses that began as text editing turned destructive the moment the query emptied: type a filter, select all, Delete, and one overshoot press removed a clip. Held down, autorepeat cascaded through several. Removal has no undo and no confirmation. Latch the run's role on its opening press and clear it on release, so the first press decides for the whole run. present() resets the latch, since a Delete held across a hide never delivers its release. Also cover the modifier, keypad, and rebinding edges of the key, plus HistoryService::remove on pinned, subscribed, and absent entries. --- src/ui/KeyAction.cpp | 19 ++++++---- src/ui/KeyAction.hpp | 8 ++++ src/ui/MainWindow.cpp | 27 +++++++++++++- src/ui/MainWindow.hpp | 10 +++++ tests/core/HistoryServiceTest.cpp | 35 ++++++++++++++++++ tests/ui/KeyActionTest.cpp | 61 +++++++++++++++++++++++++++++++ 6 files changed, 150 insertions(+), 10 deletions(-) diff --git a/src/ui/KeyAction.cpp b/src/ui/KeyAction.cpp index 023b8b6..37d56fb 100644 --- a/src/ui/KeyAction.cpp +++ b/src/ui/KeyAction.cpp @@ -52,6 +52,10 @@ BoundAccelerator bound_accelerator(std::string_view accelerator) { return BoundAccelerator{.keyval = keyval, .modifiers = static_cast(mods) & mask}; } +bool is_remove_key(unsigned int keyval) { + return keyval == GDK_KEY_Delete || keyval == GDK_KEY_KP_Delete; +} + KeyAction key_action(unsigned int keyval, unsigned int modifiers, const KeyContext& context, const WindowShortcuts& shortcuts) { switch (keyval) { @@ -87,17 +91,16 @@ KeyAction key_action(unsigned int keyval, unsigned int modifiers, const KeyConte case GDK_KEY_Down: case GDK_KEY_KP_Down: return KeyAction::SelectNext; - case GDK_KEY_Delete: - case GDK_KEY_KP_Delete: - // Delete removes the highlighted clip — but only outside an active search, - // where the entry needs Delete to forward-delete the query text. - if (!context.search_active) { - return KeyAction::RemoveSelected; - } - break; default: break; } + // Delete removes the highlighted clip — but only outside an active search, + // where the entry needs Delete to forward-delete the query text, and never + // for a press continuing a run that began as such an edit. Removal is + // irreversible, so the run's opening press decides its role for all of it. + if (is_remove_key(keyval) && !context.search_active && !context.delete_disarmed) { + return KeyAction::RemoveSelected; + } } return KeyAction::None; // typing, Tab, modified arrows, everything else } diff --git a/src/ui/KeyAction.hpp b/src/ui/KeyAction.hpp index 145d3a6..baecdd5 100644 --- a/src/ui/KeyAction.hpp +++ b/src/ui/KeyAction.hpp @@ -27,6 +27,10 @@ enum class KeyAction : std::uint8_t { struct KeyContext { bool search_active = false; // the search entry holds text bool button_focused = false; // a button has focus, so it owns Enter + // This Delete press continues a run that began while the query was non-empty. + // Such a run keeps editing text once the query empties, so holding Delete to + // clear the filter never rolls into destroying clips mid-press. + bool delete_disarmed = false; }; // A parsed GNOME accelerator (keyval + modifier mask). Produced by @@ -52,6 +56,10 @@ struct WindowShortcuts { BoundAccelerator pin; }; +// True for the keys that remove the highlighted clip. Exposed so the window can +// track a held Delete across press and release without duplicating the keyval list. +[[nodiscard]] bool is_remove_key(unsigned int keyval); + // Map a key event to the action it asks for. `modifiers` must already be masked // to gtk_accelerator_get_default_mod_mask() so it lines up with BoundAccelerator. [[nodiscard]] KeyAction key_action(unsigned int keyval, unsigned int modifiers, diff --git a/src/ui/MainWindow.cpp b/src/ui/MainWindow.cpp index b8a3f74..f1ef3d9 100644 --- a/src/ui/MainWindow.cpp +++ b/src/ui/MainWindow.cpp @@ -160,6 +160,10 @@ void MainWindow::build_ui(GtkApplication* application) { key_controller->set_propagation_phase(Gtk::PropagationPhase::CAPTURE); key_controller->signal_key_pressed().connect(sigc::mem_fun(*this, &MainWindow::on_key_pressed), false); + // Releases close a held-Delete run (see delete_run_disarmed_); nothing else + // needs them, and they carry no propagation decision. + key_controller->signal_key_released().connect( + sigc::mem_fun(*this, &MainWindow::on_key_released), false); // add_controller takes ownership of a ref, so gobj_copy() mints it — unlike the // plain gobj() handoff used for child widgets below, which would double-unref here. gtk_widget_add_controller(GTK_WIDGET(window_), @@ -450,8 +454,17 @@ bool MainWindow::on_key_pressed(guint keyval, guint /*keycode*/, Gdk::ModifierTy // Caveat: an in-progress input-method preedit is not detected, so Enter and // the arrows preempt CJK candidate selection. Filter through Gtk::IMContext if // that ever matters. - const KeyContext context{.search_active = !search_->get_text().empty(), - .button_focused = button_focused(GTK_WINDOW(window_))}; + const bool searching = !search_->get_text().empty(); + // Latch the role of a run of Delete presses on its opening press. Auto-repeat + // re-enters here with the same key still down, and emptying the query flips + // `searching` mid-run — without the latch that turns a text edit into an + // unrecoverable removal, and then into a cascade of them. + if (is_remove_key(keyval) && !delete_run_disarmed_.has_value()) { + delete_run_disarmed_ = searching; + } + const KeyContext context{.search_active = searching, + .button_focused = button_focused(GTK_WINDOW(window_)), + .delete_disarmed = delete_run_disarmed_.value_or(false)}; // Same GNOME accelerator strings Settings stores; mask matches BoundAccelerator. const auto mod_mask = static_cast(gtk_accelerator_get_default_mod_mask()); const auto modifiers = static_cast(state & mod_mask); @@ -492,6 +505,13 @@ bool MainWindow::on_key_pressed(guint keyval, guint /*keycode*/, Gdk::ModifierTy return false; } +void MainWindow::on_key_released(guint keyval, guint /*keycode*/, Gdk::ModifierType /*state*/) { + // The run is over: the next Delete press re-reads the query and decides afresh. + if (is_remove_key(keyval)) { + delete_run_disarmed_.reset(); + } +} + void MainWindow::move_selection(bool forward) { // Ctrl+click reaches GtkListBox as a toggle and leaves the list unselected, so // the cursor can be missing even with matches on screen. Start the scan at the @@ -694,6 +714,9 @@ void MainWindow::present() { } else { gtk_window_set_focus(GTK_WINDOW(window_), nullptr); } + // A Delete held while the window went away never delivers its release here, so + // open with no run in flight rather than a key disarmed for the rest of the run. + delete_run_disarmed_.reset(); } void MainWindow::refresh_tray() { diff --git a/src/ui/MainWindow.hpp b/src/ui/MainWindow.hpp index efd3b6d..81015e7 100644 --- a/src/ui/MainWindow.hpp +++ b/src/ui/MainWindow.hpp @@ -41,6 +41,7 @@ #include #include #include +#include #include namespace copyclip::ui { @@ -80,6 +81,8 @@ class MainWindow { // Returns true when the key was consumed; everything else falls through to // the focused widget. bool on_key_pressed(guint keyval, guint keycode, Gdk::ModifierType state); + // Close the run of Delete presses tracked by delete_run_disarmed_. + void on_key_released(guint keyval, guint keycode, Gdk::ModifierType state); // Move the highlighted row to the next/previous match, and scroll it into view. void move_selection(bool forward); // Paste the highlighted clip. False when there is nothing highlighted, so the @@ -124,6 +127,13 @@ class MainWindow { bool refresh_pending_ = false; std::size_t card_count_ = 0; std::string search_text_; + // Whether the in-flight run of Delete presses began on a non-empty query — set + // by its first press, cleared on release, empty while no Delete is held. The + // run keeps that role throughout: clearing the query with a held Delete would + // otherwise turn destructive the instant the entry empties, and a removal has + // no undo. Also reset on present(), since a Delete held across a hide never + // delivers its release here and would leave the key disarmed for good. + std::optional delete_run_disarmed_; // The newest created_at seen so far. When a rebuild's max exceeds it, a clip was // just added (a copy or a paste) — the cue to put the cursor on it. Starts at // startup time so the history already on disk, all of it older, counts as history diff --git a/tests/core/HistoryServiceTest.cpp b/tests/core/HistoryServiceTest.cpp index 58cf844..3d13378 100644 --- a/tests/core/HistoryServiceTest.cpp +++ b/tests/core/HistoryServiceTest.cpp @@ -144,6 +144,41 @@ TEST(HistoryServiceTest, ReAddsDuplicateAfterRemoval) { EXPECT_EQ(contents_in_order(harness.service.entries()), (std::vector{"x"})); } +// remove() takes the entry whatever its pin state, unlike clear_unpinned. The UI's +// Delete key rides this, so a pinned clip is one keystroke from gone. +TEST(HistoryServiceTest, RemoveDropsAPinnedEntry) { + ServiceHarness harness; + harness.service.add("keep"); + EXPECT_TRUE(harness.service.toggle_pin("keep")); + harness.service.remove("keep"); + EXPECT_TRUE(harness.service.entries().empty()); +} + +// The window's card rebuild hangs off this notification; without it the deleted +// card would stay on screen. +TEST(HistoryServiceTest, RemoveNotifiesSubscribers) { + ServiceHarness harness; + harness.service.add("x"); + std::vector calls; + const auto sub = harness.service.subscribe([&calls] { calls.push_back(1); }); + harness.service.remove("x"); + EXPECT_TRUE(harness.service.entries().empty()); + EXPECT_EQ(calls.size(), 1U); +} + +// Removing absent content leaves the history alone — but still notifies. Holding +// Delete can re-issue a removal for a card the deferred rebuild has not dropped +// yet, so this path must stay harmless. +TEST(HistoryServiceTest, RemoveOfMissingContentIsHarmless) { + ServiceHarness harness; + harness.service.add("x"); + std::vector calls; + const auto sub = harness.service.subscribe([&calls] { calls.push_back(1); }); + harness.service.remove("nope"); + EXPECT_EQ(contents_in_order(harness.service.entries()), (std::vector{"x"})); + EXPECT_EQ(calls.size(), 1U); // notifies unconditionally, even for a no-op +} + TEST(HistoryServiceTest, CapEvictsOldestUnpinned) { ServiceHarness harness{2}; for (const auto* text : {"a", "b", "c"}) { diff --git a/tests/ui/KeyActionTest.cpp b/tests/ui/KeyActionTest.cpp index 7880853..d59b9f7 100644 --- a/tests/ui/KeyActionTest.cpp +++ b/tests/ui/KeyActionTest.cpp @@ -9,6 +9,7 @@ namespace { using copyclip::ui::bound_accelerator; using copyclip::ui::BoundAccelerator; +using copyclip::ui::is_remove_key; using copyclip::ui::key_action; using copyclip::ui::KeyAction; using copyclip::ui::KeyContext; @@ -124,6 +125,66 @@ TEST(KeyActionTest, ModifiedDeleteFallsThrough) { EXPECT_EQ(act(GDK_KEY_Delete, GDK_CONTROL_MASK), KeyAction::None); } +// Only the bare key removes: Shift+Delete is the desktop-wide "cut", and the rest +// belong to the entry. A removal is unrecoverable, so it must not ride a chord. +TEST(KeyActionTest, DeleteWithAnyOtherModifierFallsThrough) { + EXPECT_EQ(act(GDK_KEY_Delete, GDK_SHIFT_MASK), KeyAction::None); + EXPECT_EQ(act(GDK_KEY_Delete, GDK_ALT_MASK), KeyAction::None); + EXPECT_EQ(act(GDK_KEY_Delete, GDK_SUPER_MASK), KeyAction::None); + EXPECT_EQ(act(GDK_KEY_KP_Delete, GDK_SHIFT_MASK), KeyAction::None); +} + +// With NumLock on, the keypad's delete key reports KP_Decimal — a "." the user is +// typing. Only the NumLock-off KP_Delete may remove a clip. +TEST(KeyActionTest, KeypadDecimalIsNotDelete) { + EXPECT_EQ(act(GDK_KEY_KP_Decimal), KeyAction::None); + EXPECT_EQ(act(GDK_KEY_KP_Decimal, 0, kSearching), KeyAction::None); +} + +// Delete acts on the list selection, not the focused widget — like pin, unlike +// Enter, which a focused button owns. +TEST(KeyActionTest, DeleteRemovesWithAButtonFocused) { + EXPECT_EQ(act(GDK_KEY_Delete, 0, kOnButton), KeyAction::RemoveSelected); +} + +// The hazard the disarm flag exists for: holding Delete to clear the query empties +// the entry mid-press, flipping search_active to false. Without the latch the same +// physical press would roll straight into removing clips, which has no undo. +TEST(KeyActionTest, DisarmedDeleteKeepsEditingTextOnceTheQueryEmpties) { + constexpr KeyContext disarmed{ + .search_active = false, .button_focused = false, .delete_disarmed = true}; + EXPECT_EQ(act(GDK_KEY_Delete, 0, disarmed), KeyAction::None); + EXPECT_EQ(act(GDK_KEY_KP_Delete, 0, disarmed), KeyAction::None); +} + +// The flag is scoped to the Delete run and must not disarm anything else. +TEST(KeyActionTest, DisarmingDeleteLeavesTheOtherKeysAlone) { + constexpr KeyContext disarmed{ + .search_active = false, .button_focused = false, .delete_disarmed = true}; + EXPECT_EQ(act(GDK_KEY_Down, 0, disarmed), KeyAction::SelectNext); + EXPECT_EQ(act(GDK_KEY_Return, 0, disarmed), KeyAction::Paste); + EXPECT_EQ(act(GDK_KEY_Escape, 0, disarmed), KeyAction::Dismiss); +} + +// MainWindow tracks a held Delete through this predicate, so it must cover exactly +// the keys the policy removes on — no more (KP_Decimal is a typed "."). +TEST(KeyActionTest, IsRemoveKeyCoversTheDeleteFamilyOnly) { + EXPECT_TRUE(is_remove_key(GDK_KEY_Delete)); + EXPECT_TRUE(is_remove_key(GDK_KEY_KP_Delete)); + EXPECT_FALSE(is_remove_key(GDK_KEY_KP_Decimal)); + EXPECT_FALSE(is_remove_key(GDK_KEY_BackSpace)); +} + +// Configured shortcuts are matched before the fixed keys, so rebinding pin (or +// paste) onto Delete silently costs the user the removal shortcut. +TEST(KeyActionTest, RebindingOntoDeleteWinsOverRemoval) { + const WindowShortcuts pin_on_delete{ + .paste = BoundAccelerator{.keyval = GDK_KEY_Return, .modifiers = 0}, + .pin = BoundAccelerator{.keyval = GDK_KEY_Delete, .modifiers = 0}, + }; + EXPECT_EQ(act(GDK_KEY_Delete, 0, kFresh, pin_on_delete), KeyAction::TogglePin); +} + // Typing must reach the search entry untouched, or the filter stops working. TEST(KeyActionTest, OtherKeysFallThrough) { EXPECT_EQ(act(GDK_KEY_a, 0, kSearching), KeyAction::None);