1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
//! Browser bookmarks dialog
use super::Profile;
use crate::app::browser::window::action::{Action as WindowAction, Position};
use adw::{
ActionRow, PreferencesGroup, PreferencesPage,
prelude::{
ActionRowExt, AdwDialogExt, ExpanderRowExt, PreferencesDialogExt, PreferencesGroupExt,
PreferencesPageExt,
},
};
use gtk::{
glib::{DateTime, GString, Uri, UriFlags},
prelude::ButtonExt,
};
use indexmap::IndexMap;
use std::rc::Rc;
struct Record {
time: DateTime,
request: String,
title: Option<String>,
}
pub trait Bookmarks {
fn bookmarks(window_action: &Rc<WindowAction>, profile: &Rc<Profile>) -> Self;
}
impl Bookmarks for adw::PreferencesDialog {
fn bookmarks(window_action: &Rc<WindowAction>, profile: &Rc<Profile>) -> Self {
let mut index: IndexMap<GString, Vec<Record>> = IndexMap::new();
for bookmark in profile.bookmark.recent(None) {
match Uri::parse(&bookmark.request, UriFlags::NONE) {
Ok(uri) => index
.entry(match uri.host() {
Some(host) => host,
None => uri.to_str(),
})
.or_default()
.push(Record {
request: bookmark.request,
time: bookmark.time,
title: bookmark.title,
}),
Err(_) => continue, // @TODO
}
}
let d = adw::PreferencesDialog::builder()
.search_enabled(true)
.title("Bookmarks")
.build();
d.add(&{
let p = PreferencesPage::builder()
.icon_name("document-open-recent-symbolic")
//.title("All")
.build();
for (group, records) in index {
p.add(&{
let g = PreferencesGroup::new();
g.add(&{
let e = adw::ExpanderRow::builder()
.enable_expansion(true)
.expanded(false)
.subtitle(
records
.iter()
.max_by_key(|r| r.time.to_unix())
.unwrap()
.time
.format_iso8601()
.unwrap(),
)
.title(escape(&group))
.build();
for record in records {
e.add_row(&{
let a = ActionRow::builder()
.activatable(false)
.title_selectable(true)
.title(match record.title {
Some(title) => title,
None => record.time.format_iso8601().unwrap().to_string(),
})
.subtitle_selectable(true)
.subtitle(escape(&record.request))
.build();
a.add_suffix(&{
let b = gtk::Button::builder()
.css_classes(["accent", "circular", "flat"])
.icon_name("mail-forward-symbolic")
.tooltip_text("Open in the new tab")
.valign(gtk::Align::Center)
.build();
b.connect_clicked({
let a = window_action.clone();
let d = d.clone();
move |_| {
a.append.activate_stateful_once(
Position::After,
Some(record.request.clone()),
false,
true,
true,
true,
);
d.close();
}
});
b
});
a
})
}
e
});
g
});
}
p
});
d
}
}
/// Prevents GTK warnings (`use_markup` has no effect @TODO)
fn escape(value: &str) -> String {
value.replace("&", "&").replace("&", "&")
}