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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
//! Browser history dialog
use super::Profile;
use crate::app::browser::window::action::{Action as WindowAction, Position};
use adw::{
ActionRow, ExpanderRow, PreferencesDialog, PreferencesGroup, PreferencesPage,
prelude::{
ActionRowExt, AdwDialogExt, ExpanderRowExt, PreferencesDialogExt, PreferencesGroupExt,
PreferencesPageExt,
},
};
use gtk::{
Align, Button,
glib::{DateTime, GString, Uri, UriFlags},
prelude::ButtonExt,
};
use indexmap::IndexMap;
use std::rc::Rc;
pub struct Event {
pub time: DateTime,
pub count: usize,
}
struct Record {
event: Event,
request: GString,
title: Option<GString>,
}
pub trait History {
fn history(window_action: &Rc<WindowAction>, profile: &Rc<Profile>) -> Self;
}
impl History for PreferencesDialog {
fn history(window_action: &Rc<WindowAction>, profile: &Rc<Profile>) -> Self {
let d = adw::PreferencesDialog::builder()
.search_enabled(true)
.title("History")
.build();
d.add(&page(
window_action,
&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() {
Some(host) => host,
None => uri.to_str(),
})
.or_default()
.push(Record {
event: Event { time, count },
request,
title,
}),
Err(_) => continue, // @TODO
}
}
i
}
/// Common page UI for all widget tabs
fn page(
window_action: &Rc<WindowAction>,
dialog: &PreferencesDialog,
index: IndexMap<GString, Vec<Record>>,
icon_name: &str,
title: &str,
) -> PreferencesPage {
let p = PreferencesPage::builder()
.icon_name(icon_name)
.title(title)
.build();
for (group, records) in index {
p.add(&{
let g = PreferencesGroup::new();
g.add(&{
let e = ExpanderRow::builder()
.enable_expansion(true)
.expanded(false)
.subtitle(
records
.iter()
.max_by_key(|r| r.event.time.to_unix())
.unwrap()
.event
.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) => escape(&title),
None => format!(
"{} ({})",
record.event.time.format_iso8601().unwrap(),
record.event.count
),
})
.subtitle(escape(&record.request))
.subtitle_selectable(true)
.build();
a.add_prefix(
&Button::builder()
.css_classes(["circular", "caption-heading"])
.label(record.event.count.to_string())
.tooltip_text("Visit count")
.valign(Align::Center)
.build(),
);
a.add_suffix(&{
let b = Button::builder()
.css_classes(["accent", "circular", "flat"])
.icon_name("mail-forward-symbolic")
.tooltip_text("Open in the new tab")
.valign(Align::Center)
.build();
b.connect_clicked({
let a = window_action.clone();
let d = dialog.clone();
move |_| {
a.append.activate_stateful_once(
Position::After,
Some(record.request.to_string()),
false,
true,
true,
true,
);
d.close();
}
});
b
});
a
})
}
e
});
g
});
}
p
}
/// Prevents GTK warnings (`use_markup` has no effect @TODO)
fn escape(value: &str) -> String {
value.replace("&", "&").replace("&", "&")
}