From d2bd81e2f9faa68093934f790292038d68440871 Mon Sep 17 00:00:00 2001 From: Hakim Date: Tue, 11 Aug 2026 15:24:17 +0200 Subject: [PATCH 1/2] feat(ui): return to the top of the list on a new copy or paste MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Copying something new, or pasting a clip back from the list, snaps the history list back to the top with the newest clip highlighted; summoning the window lands there too. The cue is the newest created_at rising across a rebuild, which only add() does — pin and remove leave the cursor put. --- src/ui/MainWindow.cpp | 29 +++++++++++++++++++++++++++++ src/ui/MainWindow.hpp | 6 ++++++ 2 files changed, 35 insertions(+) diff --git a/src/ui/MainWindow.cpp b/src/ui/MainWindow.cpp index 7fd9963..b3a7ce1 100644 --- a/src/ui/MainWindow.cpp +++ b/src/ui/MainWindow.cpp @@ -18,6 +18,7 @@ #include #include +#include #include #include #include @@ -277,6 +278,16 @@ void MainWindow::rebuild_cards() { const std::vector entries = history_.get().entries(); card_count_ = entries.size(); + // A fresh add() — a new copy, or pasting a clip back — stamps the newest entry + // with clock.now(), lifting the max created_at; pin and remove never do. That + // lift is the cue to jump back to the top so the cursor lands on the new clip. + std::chrono::system_clock::time_point newest{}; + for (const core::ClipboardEntry& entry : entries) { + newest = std::max(newest, entry.created_at); + } + const bool new_item_arrived = newest > last_seen_newest_; + last_seen_newest_ = std::max(last_seen_newest_, newest); + // Pinning recreates the very card the cursor sits on, taking the selection with // it. Remember what it held so the cursor can be put back below, instead of // apply_filter snapping it to the top of the list. @@ -338,6 +349,12 @@ void MainWindow::rebuild_cards() { list_->invalidate_sort(); apply_filter(); + + // After apply_filter has settled the filtered selection: a just-added clip pulls + // the cursor to the top, overriding the restored selection above. + if (new_item_arrived) { + reset_to_top(); + } } void MainWindow::apply_filter() { @@ -503,6 +520,15 @@ bool MainWindow::pin_selection() { return true; } +void MainWindow::reset_to_top() { + if (Gtk::ListBoxRow* const first = visible_row_from(list_->get_first_child(), true); + first != nullptr) { + list_->select_row(*first); + } + // The list is what the viewport scrolls, so 0 is the very top. + scrolled_->get_vadjustment()->set_value(0.0); +} + void MainWindow::reveal(Gtk::ListBoxRow& row) { double row_x = 0.0; double row_y = 0.0; @@ -598,6 +624,9 @@ void MainWindow::toggle() { void MainWindow::present() { gtk_window_present(GTK_WINDOW(window_)); + // A summon lands at the top of the list, newest clip highlighted, rather than + // wherever the cursor and scroll last sat. + reset_to_top(); // Start on the search field (type to filter); never leave a header button // showing the focus ring. if (gtk_widget_get_visible(GTK_WIDGET(search_->gobj())) != FALSE) { diff --git a/src/ui/MainWindow.hpp b/src/ui/MainWindow.hpp index b90fbd0..9755049 100644 --- a/src/ui/MainWindow.hpp +++ b/src/ui/MainWindow.hpp @@ -36,6 +36,7 @@ #include +#include #include #include #include @@ -84,6 +85,8 @@ class MainWindow { bool activate_selection(); // Pin / unpin the highlighted clip. False when nothing is selected. bool pin_selection(); + // Highlight the newest (top) match and scroll the list back up to it. + void reset_to_top(); // 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 @@ -110,6 +113,9 @@ class MainWindow { bool refresh_pending_ = false; std::size_t card_count_ = 0; std::string search_text_; + // 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 snap the list back to the top. + std::chrono::system_clock::time_point last_seen_newest_{}; AdwApplicationWindow* window_ = nullptr; Gtk::Stack* stack_ = nullptr; Gtk::ScrolledWindow* scrolled_ = nullptr; // holds list_; its vadjustment scrolls it From 321f287a19c254be4c96545ce1f7793057c9fc1a Mon Sep 17 00:00:00 2001 From: Walkercito Date: Tue, 11 Aug 2026 16:21:02 -0400 Subject: [PATCH 2/2] fix(ui): put the cursor on the new clip, not the top of the list MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Pinned clips sort above the newest, so snapping the list to the top left the cursor on a pin instead of on the clip that just arrived. Follow the clip itself: remember its content, select its card and scroll it into view, and defer that scroll to the frame that lays the row out, since a card appended moments ago — or every card in a window only just mapped — has no position yet. A copy usually happens with the window down, where nothing can be shown and stray tick callbacks would pile up. Hold the arrival and honour it on the next summon instead; a summon with nothing waiting still goes home to the top. Seed the newest-seen stamp at startup so the history already on disk does not count as one big arrival. Closes #10 --- src/ui/MainWindow.cpp | 95 +++++++++++++++++++++++++++++-------------- src/ui/MainWindow.hpp | 20 +++++++-- 2 files changed, 82 insertions(+), 33 deletions(-) diff --git a/src/ui/MainWindow.cpp b/src/ui/MainWindow.cpp index b3a7ce1..5f71a6c 100644 --- a/src/ui/MainWindow.cpp +++ b/src/ui/MainWindow.cpp @@ -8,6 +8,8 @@ #include "ui/Theme.hpp" #include "ui/widgets/ClipCard.hpp" +#include + #include #include #include @@ -39,6 +41,8 @@ namespace { constexpr int kPlaceholderIconSize = 48; constexpr const char* kPageList = "list"; constexpr const char* kPageEmpty = "empty"; +// The list is what the viewport scrolls, so its adjustment reads 0 at the very top. +constexpr double kScrollTop = 0.0; // Return freed heap pages to the OS; glibc otherwise keeps them in its arenas, so // RSS never falls back after a peak (e.g. an image card's decode buffers). A free @@ -95,6 +99,12 @@ void trim_heap() { return focus != nullptr && gtk_widget_get_ancestor(focus, GTK_TYPE_POPOVER) != nullptr; } +// True once the layout has given `widget` a place in its parent; until then it has +// no size, and asking where it sits answers nothing. +[[nodiscard]] bool is_laid_out(const Gtk::Widget& widget) { + return widget.get_height() > 0; +} + // The first row at or after `start`, scanning in display order or against it, that // the search filter left visible — or nullptr once the scan runs off the list. [[nodiscard]] Gtk::ListBoxRow* visible_row_from(Gtk::Widget* start, bool forward) { @@ -280,13 +290,14 @@ void MainWindow::rebuild_cards() { // A fresh add() — a new copy, or pasting a clip back — stamps the newest entry // with clock.now(), lifting the max created_at; pin and remove never do. That - // lift is the cue to jump back to the top so the cursor lands on the new clip. - std::chrono::system_clock::time_point newest{}; - for (const core::ClipboardEntry& entry : entries) { - newest = std::max(newest, entry.created_at); + // lift is what marks a clip as newly arrived. Its key, or "" when nothing + // arrived: no entry can key on empty, since add() rejects blanks. + const auto newest = std::ranges::max_element(entries, {}, &core::ClipboardEntry::created_at); + std::string arrived_content; + if (newest != entries.end() && newest->created_at > last_seen_newest_) { + last_seen_newest_ = newest->created_at; + arrived_content = newest->content; } - const bool new_item_arrived = newest > last_seen_newest_; - last_seen_newest_ = std::max(last_seen_newest_, newest); // Pinning recreates the very card the cursor sits on, taking the selection with // it. Remember what it held so the cursor can be put back below, instead of @@ -350,19 +361,45 @@ void MainWindow::rebuild_cards() { list_->invalidate_sort(); apply_filter(); - // After apply_filter has settled the filtered selection: a just-added clip pulls - // the cursor to the top, overriding the restored selection above. - if (new_item_arrived) { - reset_to_top(); + // After apply_filter has settled the filtered selection, so the arrival wins over + // the selection restored above: it takes the cursor now if the window is up to + // see it, and waits for the next summon otherwise. + if (!arrived_content.empty() && (!showing() || !cursor_to(arrived_content))) { + pending_arrival_ = arrived_content; } } +bool MainWindow::cursor_to(const std::string& content) { + const auto card = cards_.find(content); + if (card == cards_.end() || !card->second->get_visible()) { + return false; + } + list_->select_row(*card->second); + // Scrolling to it has to wait for the frame that lays the row out: a card + // appended moments ago — and every card in a window only just presented — is not + // placed yet, so reveal() here would find no position and do nothing. A tick + // callback retries once per frame, so the wait costs frames rather than a + // spinning idle. + list_->add_tick_callback([this, content](const Glib::RefPtr&) { + // Re-find it: a rebuild in between may have recreated or dropped the card. + const auto row = cards_.find(content); + if (row == cards_.end() || !row->second->get_visible()) { + return false; // gone, or the filter hid it: nothing left to scroll to + } + if (!is_laid_out(*row->second)) { + return true; // ask again next frame + } + reveal(*row->second); + return false; + }); + return true; +} + void MainWindow::apply_filter() { // The search bar is only useful once there is something to search. search_->set_visible(card_count_ > 0); const Gtk::ListBoxRow* selected = list_->get_selected_row(); - ClipCard* first_shown = nullptr; bool selection_shown = false; std::size_t visible = 0; for (Gtk::Widget* child = list_->get_first_child(); child != nullptr; @@ -377,23 +414,13 @@ void MainWindow::apply_filter() { continue; } ++visible; - if (first_shown == nullptr) { - first_shown = card; - } selection_shown = selection_shown || card == selected; } // Always leave a row highlighted for Enter to act on. When the filter — or a - // rebuild that dropped the card — took the selection away, fall back to the - // first match and scroll back up to it: hidden rows get no allocation, so that - // row sits at the very top of the list. + // rebuild that dropped the card — took the selection away, the cursor goes home. if (!selection_shown) { - if (first_shown != nullptr) { - list_->select_row(*first_shown); - scrolled_->get_vadjustment()->set_value(0.0); - } else { - list_->unselect_all(); - } + reset_to_top(); } if (visible > 0) { @@ -524,9 +551,10 @@ void MainWindow::reset_to_top() { if (Gtk::ListBoxRow* const first = visible_row_from(list_->get_first_child(), true); first != nullptr) { list_->select_row(*first); + } else { + list_->unselect_all(); // the filter hid every card: nothing to put it on } - // The list is what the viewport scrolls, so 0 is the very top. - scrolled_->get_vadjustment()->set_value(0.0); + scrolled_->get_vadjustment()->set_value(kScrollTop); } void MainWindow::reveal(Gtk::ListBoxRow& row) { @@ -543,7 +571,7 @@ void MainWindow::copy(const core::ClipboardEntry& entry) { // ClipCard defers its click to an idle, so a fast double click can queue two // copies of the same clip — which with auto-paste on injects Ctrl+V twice into // the target app. The first one hid the window, so that is the signal to stop. - if (gtk_widget_get_visible(GTK_WIDGET(window_)) == FALSE) { + if (!showing()) { return; } // Reconstruct the clipboard payload for the entry's kind. Image bytes are @@ -614,8 +642,12 @@ GtkWidget* MainWindow::native() const { return GTK_WIDGET(window_); } +bool MainWindow::showing() const { + return gtk_widget_get_visible(GTK_WIDGET(window_)) != FALSE; +} + void MainWindow::toggle() { - if (gtk_widget_get_visible(GTK_WIDGET(window_)) != FALSE) { + if (showing()) { hide(); return; } @@ -624,9 +656,12 @@ void MainWindow::toggle() { void MainWindow::present() { gtk_window_present(GTK_WINDOW(window_)); - // A summon lands at the top of the list, newest clip highlighted, rather than - // wherever the cursor and scroll last sat. - reset_to_top(); + // A summon lands on the clip copied since the list was last looked at, and at the + // top of the list when nothing new came in — never on wherever the cursor and + // scroll last sat. Consumed either way: an arrival is only special until shown. + if (!cursor_to(std::exchange(pending_arrival_, {}))) { + reset_to_top(); + } // Start on the search field (type to filter); never leave a header button // showing the focus ring. if (gtk_widget_get_visible(GTK_WIDGET(search_->gobj())) != FALSE) { diff --git a/src/ui/MainWindow.hpp b/src/ui/MainWindow.hpp index 9755049..34edb37 100644 --- a/src/ui/MainWindow.hpp +++ b/src/ui/MainWindow.hpp @@ -68,6 +68,8 @@ class MainWindow { [[nodiscard]] GtkWidget* native() const; private: + // True while the window is on screen, as opposed to hidden in the background. + [[nodiscard]] bool showing() const; void build_ui(GtkApplication* application); // Create or drop the panel icon to match the show_panel_icon setting. void refresh_tray(); @@ -85,8 +87,14 @@ class MainWindow { bool activate_selection(); // Pin / unpin the highlighted clip. False when nothing is selected. bool pin_selection(); - // Highlight the newest (top) match and scroll the list back up to it. + // Send the cursor home: highlight the first match and scroll the list back up to + // it, or leave nothing highlighted when the filter hid every card. void reset_to_top(); + // Put the cursor on `content`'s card and scroll it into view — pinned clips sort + // above the newest, so that is rarely the top. False when no such card is shown + // (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); // 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 @@ -114,8 +122,14 @@ class MainWindow { std::size_t card_count_ = 0; std::string search_text_; // 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 snap the list back to the top. - std::chrono::system_clock::time_point last_seen_newest_{}; + // 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 + // rather than as one big arrival: only clips copied from now on move the cursor. + std::chrono::system_clock::time_point last_seen_newest_{std::chrono::system_clock::now()}; + // The arrived clip the user has not been shown yet — the usual case, since a copy + // happens in another app with this window down. Empty once a summon has honoured + // it, or when nothing is waiting. + std::string pending_arrival_; AdwApplicationWindow* window_ = nullptr; Gtk::Stack* stack_ = nullptr; Gtk::ScrolledWindow* scrolled_ = nullptr; // holds list_; its vadjustment scrolls it