From 73c35d25d80a8bb15e39876e52765c766d2ea09c Mon Sep 17 00:00:00 2001 From: yggverse Date: Tue, 11 Mar 2025 11:09:49 +0200 Subject: [PATCH] implement suggestion titles, search in history, show bookmark as icon indicator --- src/app/browser/window/tab/item/client.rs | 4 ++- src/app/browser/window/tab/item/page.rs | 10 +++++- .../page/navigation/request/suggestion.rs | 32 +++++++++++++++---- .../navigation/request/suggestion/item.rs | 5 +-- .../navigation/request/suggestion/item/imp.rs | 12 ++++--- src/profile/history.rs | 9 ++++-- src/profile/history/item.rs | 4 ++- src/profile/history/memory.rs | 14 ++++++++ 8 files changed, 71 insertions(+), 19 deletions(-) diff --git a/src/app/browser/window/tab/item/client.rs b/src/app/browser/window/tab/item/client.rs index 684ec128..f6ea16dc 100644 --- a/src/app/browser/window/tab/item/client.rs +++ b/src/app/browser/window/tab/item/client.rs @@ -205,5 +205,7 @@ fn snap_history(page: &Page) { page.item_action .history .add(page.navigation.request(), true); - page.profile.history.open(page.navigation.request()) + page.profile + .history + .open(page.navigation.request(), Some(page.title())) } diff --git a/src/app/browser/window/tab/item/page.rs b/src/app/browser/window/tab/item/page.rs index 18c78338..ea0157b9 100644 --- a/src/app/browser/window/tab/item/page.rs +++ b/src/app/browser/window/tab/item/page.rs @@ -123,7 +123,9 @@ impl Page { // Restore child components self.navigation.restore(transaction, &record.id)?; // Make initial page history snap - self.profile.history.open(self.navigation.request()); + self.profile + .history + .open(self.navigation.request(), Some(self.title())); } Ok(()) } @@ -166,6 +168,12 @@ impl Page { self.navigation.set_progress_fraction(progress_fraction); self.tab_page.set_loading(progress_fraction > 0.0) } + + // Getters + + pub fn title(&self) -> gtk::glib::GString { + self.tab_page.title() + } } // Tools diff --git a/src/app/browser/window/tab/item/page/navigation/request/suggestion.rs b/src/app/browser/window/tab/item/page/navigation/request/suggestion.rs index f3e9bd53..3fe130f7 100644 --- a/src/app/browser/window/tab/item/page/navigation/request/suggestion.rs +++ b/src/app/browser/window/tab/item/page/navigation/request/suggestion.rs @@ -80,6 +80,15 @@ impl Suggestion { let r = l.child().and_downcast::().unwrap(); r.set_title(&i.title()); r.set_subtitle(&i.subtitle()); + if i.has_bookmark() { + r.add_suffix( + >k::Image::builder() + .icon_name("starred-symbolic") + .margin_end(4) + .pixel_size(11) + .build(), + ); + } }); f }) @@ -134,22 +143,31 @@ impl Suggestion { // Actions pub fn update(&self, limit: Option) { - use gtk::prelude::EditableExt; + use gtk::{glib::GString, prelude::EditableExt}; use itertools::Itertools; if self.request.text_length() > 0 { self.list_store.remove_all(); let query = self.request.text(); - let items = self.profile.bookmark.contains_request(&query, limit); + let items = self.profile.history.contains_request(&query, limit); if !items.is_empty() { for item in items .into_iter() - .sorted_by(|a, b| Ord::cmp(&b.request, &a.request)) + .sorted_by(|a, b| Ord::cmp(&b.opened.len(), &a.opened.len())) { + let subtitle = + GString::from(item.request.replace(&*query, &format!("{query}"))); + + let title = match item.title { + Some(title) => title.replace(&*query, &format!("{query}")).into(), + None => subtitle.clone(), + }; + self.list_store.append(&Item::build( - item.request.replace(&*query, &format!("{query}")), - item.request.clone(), - item.request.clone(), - )); // @TODO + title, + subtitle, + self.profile.bookmark.is_match_request(&item.request), + item.request, + )); } self.popover.popup(); return; diff --git a/src/app/browser/window/tab/item/page/navigation/request/suggestion/item.rs b/src/app/browser/window/tab/item/page/navigation/request/suggestion/item.rs index 76682748..a2861593 100644 --- a/src/app/browser/window/tab/item/page/navigation/request/suggestion/item.rs +++ b/src/app/browser/window/tab/item/page/navigation/request/suggestion/item.rs @@ -1,6 +1,6 @@ mod imp; -use gtk::glib::{self, Object}; +use gtk::glib::{self, GString, Object}; glib::wrapper! { pub struct Item(ObjectSubclass); @@ -9,11 +9,12 @@ glib::wrapper! { impl Item { // Constructors - pub fn build(title: String, subtitle: String, request: String) -> Self { + pub fn build(title: GString, subtitle: GString, has_bookmark: bool, request: GString) -> Self { Object::builder() .property("title", title) .property("subtitle", subtitle) .property("request", request) + .property("has-bookmark", has_bookmark) .build() } } diff --git a/src/app/browser/window/tab/item/page/navigation/request/suggestion/item/imp.rs b/src/app/browser/window/tab/item/page/navigation/request/suggestion/item/imp.rs index 8ce38354..8dd2a6a3 100644 --- a/src/app/browser/window/tab/item/page/navigation/request/suggestion/item/imp.rs +++ b/src/app/browser/window/tab/item/page/navigation/request/suggestion/item/imp.rs @@ -1,19 +1,21 @@ use gtk::{ gio::subclass::prelude::{DerivedObjectProperties, ObjectImpl, ObjectImplExt, ObjectSubclass}, - glib::{self, Object, Properties}, + glib::{self, GString, Object, Properties}, prelude::ObjectExt, }; -use std::cell::RefCell; +use std::cell::{Cell, RefCell}; #[derive(Properties, Default)] #[properties(wrapper_type = super::Item)] pub struct Item { #[property(get, set)] - title: RefCell, + title: RefCell, #[property(get, set)] - subtitle: RefCell, + subtitle: RefCell, #[property(get, set)] - request: RefCell, + request: RefCell, + #[property(get, set)] + has_bookmark: Cell, } #[glib::object_subclass] diff --git a/src/profile/history.rs b/src/profile/history.rs index e4dbe840..c4d034cb 100644 --- a/src/profile/history.rs +++ b/src/profile/history.rs @@ -27,10 +27,10 @@ impl History { // Actions /// Create new history record - pub fn open(&self, request: GString) { + pub fn open(&self, request: GString, title: Option) { let mut memory = self.memory.borrow_mut(); if !memory.open(&request) { - memory.add(Item::create(0, request)) // @TODO + memory.add(Item::create(0, request, title)) // @TODO } } @@ -50,4 +50,9 @@ impl History { pub fn recently_closed(&self, limit: Option) -> Vec { self.memory.borrow().recently_closed(limit) } + + /// Get unordered Items vector contains `request` + pub fn contains_request(&self, request: &str, limit: Option) -> Vec { + self.memory.borrow().contains_request(request, limit) + } } diff --git a/src/profile/history/item.rs b/src/profile/history/item.rs index 66cdbe4a..27955c96 100644 --- a/src/profile/history/item.rs +++ b/src/profile/history/item.rs @@ -4,6 +4,7 @@ use gtk::glib::{DateTime, GString}; pub struct Item { pub id: i64, pub request: GString, + pub title: Option, pub opened: Vec, pub closed: Vec, } @@ -11,10 +12,11 @@ pub struct Item { impl Item { // Constructors - pub fn create(id: i64, request: GString) -> Self { + pub fn create(id: i64, request: GString, title: Option) -> Self { Self { id, request, + title, opened: vec![now()], closed: vec![], } diff --git a/src/profile/history/memory.rs b/src/profile/history/memory.rs index 44b5b4f0..f1de7813 100644 --- a/src/profile/history/memory.rs +++ b/src/profile/history/memory.rs @@ -76,6 +76,20 @@ impl Memory { } recent } + + /// Get unordered Items vector contains `request` + pub fn contains_request(&self, request: &str, limit: Option) -> Vec { + let mut items: Vec = Vec::new(); + for (i, item) in self.0.iter().enumerate() { + if limit.is_some_and(|l| i > l) { + break; + } + if item.request.contains(request) { + items.push(item.clone()) + } + } + items + } } impl Default for Memory {