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..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) {
@@ -90,6 +94,13 @@ KeyAction key_action(unsigned int keyval, unsigned int modifiers, const KeyConte
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 7a7b53f..baecdd5 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
@@ -26,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
@@ -51,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 5f71a6c..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);
@@ -482,6 +495,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;
}
@@ -489,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
@@ -557,6 +580,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;
@@ -669,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 34edb37..81015e7 100644
--- a/src/ui/MainWindow.hpp
+++ b/src/ui/MainWindow.hpp
@@ -41,6 +41,7 @@
#include
#include