implement recently closed history tab

This commit is contained in:
yggverse 2025-07-26 12:04:42 +03:00
parent 9a6c8be37f
commit 6d419f9234

View file

@ -3,7 +3,7 @@
use super::Profile; use super::Profile;
use crate::app::browser::window::action::{Action as WindowAction, Position}; use crate::app::browser::window::action::{Action as WindowAction, Position};
use adw::{ use adw::{
ActionRow, PreferencesGroup, PreferencesPage, ActionRow, ExpanderRow, PreferencesDialog, PreferencesGroup, PreferencesPage,
prelude::{ prelude::{
ActionRowExt, AdwDialogExt, ExpanderRowExt, PreferencesDialogExt, PreferencesGroupExt, ActionRowExt, AdwDialogExt, ExpanderRowExt, PreferencesDialogExt, PreferencesGroupExt,
PreferencesPageExt, PreferencesPageExt,
@ -32,47 +32,89 @@ pub trait History {
fn history(window_action: &Rc<WindowAction>, profile: &Rc<Profile>) -> Self; fn history(window_action: &Rc<WindowAction>, profile: &Rc<Profile>) -> Self;
} }
impl History for adw::PreferencesDialog { impl History for PreferencesDialog {
fn history(window_action: &Rc<WindowAction>, profile: &Rc<Profile>) -> Self { fn history(window_action: &Rc<WindowAction>, profile: &Rc<Profile>) -> Self {
let mut visited: IndexMap<GString, Vec<Record>> = IndexMap::new(); let d = adw::PreferencesDialog::builder()
// @TODO recently closed .search_enabled(true)
.title("History")
.build();
for history in profile.history.recently_opened(None) { d.add(&page(
match Uri::parse(&history.request, UriFlags::NONE) { window_action,
Ok(uri) => visited &d,
index(
profile
.history
.recently_opened(None)
.into_iter()
.map(|i| (i.request, i.title, i.opened.time, i.opened.count))
.collect(),
),
"document-open-recent-symbolic",
"Last visit",
));
d.add(&page(
window_action,
&d,
index(
profile
.history
.recently_closed(None)
.into_iter()
.map(|i| (i.request, i.title, i.opened.time, i.opened.count))
.collect(),
),
"document-revert-symbolic",
"Recent close",
));
d
}
}
/// Common index map for all history types
/// * @TODO make Profile member public to replace the tuple?
fn index(
index: Vec<(GString, Option<GString>, DateTime, usize)>,
) -> IndexMap<GString, Vec<Record>> {
let mut i: IndexMap<GString, Vec<Record>> = IndexMap::new();
for (request, title, time, count) in index {
match Uri::parse(&request, UriFlags::NONE) {
Ok(uri) => i
.entry(match uri.host() { .entry(match uri.host() {
Some(host) => host, Some(host) => host,
None => uri.to_str(), None => uri.to_str(),
}) })
.or_default() .or_default()
.push(Record { .push(Record {
event: Event { event: Event { time, count },
time: history.opened.time, request,
count: history.opened.count, title,
},
request: history.request,
title: history.title,
}), }),
Err(_) => continue, // @TODO Err(_) => continue, // @TODO
} }
} }
i
}
let d = adw::PreferencesDialog::builder() /// Common page UI for all widget tabs
.search_enabled(true) fn page(
.title("History") window_action: &Rc<WindowAction>,
.build(); dialog: &PreferencesDialog,
index: IndexMap<GString, Vec<Record>>,
d.add(&{ icon_name: &str,
title: &str,
) -> PreferencesPage {
let p = PreferencesPage::builder() let p = PreferencesPage::builder()
.icon_name("document-open-recent-symbolic") .icon_name(icon_name)
.title("Recently visited") .title(title)
.build(); .build();
for (group, records) in visited { for (group, records) in index {
p.add(&{ p.add(&{
let g = PreferencesGroup::new(); let g = PreferencesGroup::new();
g.add(&{ g.add(&{
let e = adw::ExpanderRow::builder() let e = ExpanderRow::builder()
.enable_expansion(true) .enable_expansion(true)
.expanded(false) .expanded(false)
.subtitle( .subtitle(
@ -122,7 +164,7 @@ impl History for adw::PreferencesDialog {
.build(); .build();
b.connect_clicked({ b.connect_clicked({
let a = window_action.clone(); let a = window_action.clone();
let d = d.clone(); let d = dialog.clone();
move |_| { move |_| {
a.append.activate_stateful_once( a.append.activate_stateful_once(
Position::After, Position::After,
@ -146,9 +188,6 @@ impl History for adw::PreferencesDialog {
}); });
} }
p p
});
d
}
} }
/// Prevents GTK warnings (`use_markup` has no effect @TODO) /// Prevents GTK warnings (`use_markup` has no effect @TODO)