Branchwell

Clearwake / app · clearwake.ai / crates / clearwake-app / src / ui.rs

Raw 29.9 KB on 85fd7484362aeeaf8dbc19b2d6ddb6a3999a141b
1//! Drawing: the address bar, the page, and the home screen.
2
3use crate::{Clearwake, Page, Picture};
4use clearwake_core::html::{Block, Form, Inline};
5use egui::{Color32, CornerRadius, FontFamily, FontId, Key, RichText, Stroke, TextStyle, Vec2};
6
7const COLUMN: f32 = 740.0;
8
9pub struct Palette {
10 pub bg: Color32,
11 pub bar: Color32,
12 pub ink: Color32,
13 pub soft: Color32,
14 pub line: Color32,
15 pub accent: Color32,
16 pub link: Color32,
17 pub field: Color32,
18}
19
20pub fn palette(dark: bool) -> Palette {
21 if dark {
22 Palette {
23 bg: Color32::from_rgb(0x12, 0x16, 0x18),
24 bar: Color32::from_rgb(0x19, 0x1e, 0x21),
25 ink: Color32::from_rgb(0xe8, 0xec, 0xea),
26 soft: Color32::from_rgb(0x9a, 0xa6, 0xa3),
27 line: Color32::from_rgb(0x2a, 0x32, 0x35),
28 accent: Color32::from_rgb(0x6f, 0xc7, 0xb8),
29 link: Color32::from_rgb(0x8c, 0xc8, 0xf0),
30 field: Color32::from_rgb(0x22, 0x29, 0x2c),
31 }
32 } else {
33 Palette {
34 bg: Color32::from_rgb(0xfa, 0xf9, 0xf6),
35 bar: Color32::from_rgb(0xf1, 0xf0, 0xeb),
36 ink: Color32::from_rgb(0x1c, 0x22, 0x21),
37 soft: Color32::from_rgb(0x66, 0x70, 0x6d),
38 line: Color32::from_rgb(0xdd, 0xdb, 0xd3),
39 accent: Color32::from_rgb(0x1f, 0x7a, 0x6c),
40 link: Color32::from_rgb(0x1a, 0x5f, 0xa8),
41 field: Color32::from_rgb(0xff, 0xff, 0xff),
42 }
43 }
44}
45
46fn is_dark(ctx: &egui::Context) -> bool {
47 ctx.global_style().visuals.dark_mode
48}
49
50pub fn install_style(ctx: &egui::Context, dark: Option<bool>) {
51 let theme = match dark {
52 Some(true) => egui::ThemePreference::Dark,
53 Some(false) => egui::ThemePreference::Light,
54 None => egui::ThemePreference::System,
55 };
56 ctx.set_theme(theme);
57 for d in [true, false] {
58 let p = palette(d);
59 let style_theme = if d { egui::Theme::Dark } else { egui::Theme::Light };
60 ctx.style_mut_of(style_theme, |s| {
61 s.visuals.panel_fill = p.bg;
62 s.visuals.window_fill = p.bar;
63 s.visuals.extreme_bg_color = p.field;
64 s.visuals.hyperlink_color = p.link;
65 s.visuals.selection.bg_fill = p.accent.gamma_multiply(0.35);
66 s.visuals.widgets.noninteractive.fg_stroke = Stroke::new(1.0, p.ink);
67 s.visuals.widgets.inactive.corner_radius = CornerRadius::same(18);
68 s.visuals.widgets.hovered.corner_radius = CornerRadius::same(18);
69 s.visuals.widgets.active.corner_radius = CornerRadius::same(18);
70 s.spacing.button_padding = Vec2::new(12.0, 6.0);
71 s.text_styles.insert(TextStyle::Body, FontId::new(17.0, FontFamily::Proportional));
72 s.text_styles.insert(TextStyle::Button, FontId::new(15.0, FontFamily::Proportional));
73 s.text_styles.insert(TextStyle::Monospace, FontId::new(14.5, FontFamily::Monospace));
74 s.text_styles.insert(TextStyle::Small, FontId::new(12.5, FontFamily::Proportional));
75 s.text_styles.insert(TextStyle::Heading, FontId::new(30.0, FontFamily::Proportional));
76 });
77 }
78}
79
80pub fn shortcuts(app: &mut Clearwake, ctx: &egui::Context) {
81 let (back, fwd, reload, focus, mark, home) = ctx.input_mut(|i| {
82 let cmd = egui::Modifiers::COMMAND;
83 (
84 i.consume_key(egui::Modifiers::ALT, Key::ArrowLeft) || i.consume_key(cmd, Key::OpenBracket) || i.consume_key(egui::Modifiers::NONE, Key::BrowserBack),
85 i.consume_key(egui::Modifiers::ALT, Key::ArrowRight) || i.consume_key(cmd, Key::CloseBracket),
86 i.consume_key(cmd, Key::R) || i.consume_key(egui::Modifiers::NONE, Key::F5),
87 i.consume_key(cmd, Key::L),
88 i.consume_key(cmd, Key::D),
89 i.consume_key(cmd | egui::Modifiers::SHIFT, Key::H),
90 )
91 });
92 if back {
93 app.back();
94 }
95 if fwd {
96 app.forward();
97 }
98 if reload {
99 app.reload();
100 }
101 if focus {
102 app.address_focus = true;
103 }
104 if mark {
105 toggle_bookmark(app);
106 }
107 if home {
108 app.open(String::from("about:home"), true);
109 }
110}
111
112fn toggle_bookmark(app: &mut Clearwake) {
113 if let Some(u) = app.current_url() {
114 let t = app.current_title();
115 app.bookmarks.toggle(&u, &t);
116 }
117}
118
119#[derive(Clone, Copy)]
120enum Icon {
121 Back,
122 Forward,
123 Reload,
124 Stop,
125 Home,
126 Star(bool),
127 Menu,
128}
129
130/// A borderless toolbar button with an icon drawn in code (no icon font).
131fn pill(ui: &mut egui::Ui, icon: Icon, enabled: bool, tip: &str) -> bool {
132 let (rect, resp) = ui.allocate_exact_size(Vec2::new(36.0, 32.0), if enabled { egui::Sense::click() } else { egui::Sense::hover() });
133 let p = palette(is_dark(ui.ctx()));
134 let color = if !enabled { p.soft.gamma_multiply(0.45) } else if resp.hovered() { p.accent } else { p.ink };
135 if enabled && resp.hovered() {
136 ui.painter().rect_filled(rect.shrink(1.0), 16.0, p.line.gamma_multiply(0.6));
137 }
138 draw_icon(ui.painter(), rect.center(), icon, color);
139 let resp = resp.on_hover_text(tip);
140 enabled && resp.clicked()
141}
142
143fn draw_icon(painter: &egui::Painter, c: egui::Pos2, icon: Icon, color: Color32) {
144 use egui::pos2;
145 let st = Stroke::new(1.8, color);
146 match icon {
147 Icon::Back | Icon::Forward => {
148 let d = if matches!(icon, Icon::Back) { -1.0 } else { 1.0 };
149 painter.line_segment([pos2(c.x - 7.0 * d, c.y), pos2(c.x + 7.0 * d, c.y)], st);
150 painter.line_segment([pos2(c.x + 7.0 * d, c.y), pos2(c.x + 2.0 * d, c.y - 5.0)], st);
151 painter.line_segment([pos2(c.x + 7.0 * d, c.y), pos2(c.x + 2.0 * d, c.y + 5.0)], st);
152 }
153 Icon::Reload => {
154 let r = 7.0;
155 let pts: Vec<egui::Pos2> = (0..=20)
156 .map(|i| {
157 let a = -1.2 + i as f32 / 20.0 * 5.0;
158 pos2(c.x + r * a.cos(), c.y + r * a.sin())
159 })
160 .collect();
161 let end = *pts.last().unwrap();
162 painter.add(egui::Shape::line(pts, st));
163 let a: f32 = -1.2 + 5.0;
164 let tip = pos2(end.x + 4.0 * (a + 1.2).cos(), end.y + 4.0 * (a + 1.2).sin());
165 painter.line_segment([end, pos2(end.x + 4.5, end.y - 1.0)], st);
166 let _ = tip;
167 painter.line_segment([end, pos2(end.x + 0.5, end.y + 4.5)], st);
168 }
169 Icon::Stop => {
170 painter.line_segment([pos2(c.x - 6.0, c.y - 6.0), pos2(c.x + 6.0, c.y + 6.0)], st);
171 painter.line_segment([pos2(c.x + 6.0, c.y - 6.0), pos2(c.x - 6.0, c.y + 6.0)], st);
172 }
173 Icon::Home => {
174 let pts = vec![
175 pos2(c.x - 7.0, c.y - 0.5),
176 pos2(c.x, c.y - 7.5),
177 pos2(c.x + 7.0, c.y - 0.5),
178 ];
179 painter.add(egui::Shape::line(pts, st));
180 let body = vec![pos2(c.x - 5.0, c.y - 2.0), pos2(c.x - 5.0, c.y + 7.0), pos2(c.x + 5.0, c.y + 7.0), pos2(c.x + 5.0, c.y - 2.0)];
181 painter.add(egui::Shape::line(body, st));
182 }
183 Icon::Star(filled) => {
184 let pts: Vec<egui::Pos2> = (0..10)
185 .map(|i| {
186 let r = if i % 2 == 0 { 8.0 } else { 3.6 };
187 let a = -std::f32::consts::FRAC_PI_2 + i as f32 * std::f32::consts::PI / 5.0;
188 pos2(c.x + r * a.cos(), c.y + 0.6 + r * a.sin())
189 })
190 .collect();
191 if filled {
192 painter.add(egui::Shape::convex_polygon(vec![c], color, Stroke::NONE));
193 for i in 0..10 {
194 painter.add(egui::Shape::convex_polygon(vec![pos2(c.x, c.y + 0.6), pts[i], pts[(i + 1) % 10]], color, Stroke::NONE));
195 }
196 } else {
197 painter.add(egui::Shape::closed_line(pts, st));
198 }
199 }
200 Icon::Menu => {
201 for dy in [-5.0, 0.0, 5.0] {
202 painter.line_segment([pos2(c.x - 7.0, c.y + dy), pos2(c.x + 7.0, c.y + dy)], st);
203 }
204 }
205 }
206}
207
208/// A small shield, drawn before the status line.
209fn shield(ui: &mut egui::Ui, color: Color32) {
210 use egui::pos2;
211 let (rect, _) = ui.allocate_exact_size(Vec2::new(14.0, 14.0), egui::Sense::hover());
212 let c = rect.center();
213 let pts = vec![
214 pos2(c.x, c.y - 6.0),
215 pos2(c.x + 5.0, c.y - 4.0),
216 pos2(c.x + 4.5, c.y + 1.5),
217 pos2(c.x, c.y + 6.0),
218 pos2(c.x - 4.5, c.y + 1.5),
219 pos2(c.x - 5.0, c.y - 4.0),
220 ];
221 ui.painter().add(egui::Shape::convex_polygon(pts, color, Stroke::NONE));
222}
223
224pub fn toolbar(app: &mut Clearwake, ui: &mut egui::Ui) {
225 let p = palette(is_dark(ui.ctx()));
226 egui::Panel::top("toolbar")
227 .frame(egui::Frame::new().fill(p.bar).inner_margin(egui::Margin::symmetric(10, 8)).stroke(Stroke::new(1.0, p.line)))
228 .show(ui, |ui| {
229 ui.horizontal(|ui| {
230 ui.spacing_mut().item_spacing.x = 4.0;
231 if pill(ui, Icon::Back, app.history.can_back(), "Back") {
232 app.back();
233 }
234 if pill(ui, Icon::Forward, app.history.can_forward(), "Forward") {
235 app.forward();
236 }
237 let loading = matches!(app.page, Page::Loading { .. });
238 if loading {
239 if pill(ui, Icon::Stop, true, "Stop") {
240 app.generation += 1;
241 let url = app.current_url().unwrap_or_default();
242 app.page = Page::Error { url, message: String::from("Stopped.") };
243 }
244 } else if pill(ui, Icon::Reload, !matches!(app.page, Page::Home), "Reload") {
245 app.reload();
246 }
247 if pill(ui, Icon::Home, true, "Home") {
248 app.open(String::from("about:home"), true);
249 }
250
251 let right = 110.0;
252 let w = (ui.available_width() - right).max(120.0);
253 let edit = egui::TextEdit::singleline(&mut app.address)
254 .hint_text("Search or type a web address")
255 .desired_width(w)
256 .margin(egui::Margin::symmetric(14, 7))
257 .font(FontId::new(15.5, FontFamily::Proportional));
258 let resp = ui.add(edit);
259 if app.address_focus {
260 resp.request_focus();
261 app.address_focus = false;
262 }
263 if resp.lost_focus() && ui.input(|i| i.key_pressed(Key::Enter)) {
264 let typed = app.address.clone();
265 app.open_input(&typed);
266 }
267
268 let saved = app.current_url().is_some_and(|u| app.bookmarks.contains(&u));
269 if pill(ui, Icon::Star(saved), app.current_url().is_some(), if saved { "Remove bookmark" } else { "Bookmark this page" }) {
270 toggle_bookmark(app);
271 }
272 if pill(ui, Icon::Menu, true, "Bookmarks and settings") {
273 app.show_bookmarks = !app.show_bookmarks;
274 }
275 });
276 if let Page::Loading { .. } = app.page {
277 let t = ui.input(|i| i.time) as f32;
278 let (rect, _) = ui.allocate_exact_size(Vec2::new(ui.available_width(), 2.0), egui::Sense::hover());
279 let x = rect.left() + (t * 0.6).fract() * rect.width();
280 ui.painter().rect_filled(egui::Rect::from_min_size(egui::pos2(x - 60.0, rect.top()), Vec2::new(120.0, 2.0)).intersect(rect), 1.0, p.accent);
281 ui.ctx().request_repaint();
282 }
283 });
284 if app.show_bookmarks {
285 menu_panel(app, ui);
286 }
287}
288
289fn menu_panel(app: &mut Clearwake, ui: &mut egui::Ui) {
290 let p = palette(is_dark(ui.ctx()));
291 egui::Panel::right("menu")
292 .resizable(false)
293 .exact_size(300.0)
294 .frame(egui::Frame::new().fill(p.bar).inner_margin(16).stroke(Stroke::new(1.0, p.line)))
295 .show(ui, |ui| {
296 ui.label(RichText::new("Bookmarks").size(20.0).strong());
297 ui.add_space(6.0);
298 if app.bookmarks.items.is_empty() {
299 ui.label(RichText::new("Pages you bookmark show up here. They stay on this device.").color(p.soft));
300 }
301 let mut open = None;
302 let mut remove = None;
303 egui::ScrollArea::vertical().max_height(ui.available_height() - 190.0).show(ui, |ui| {
304 for b in &app.bookmarks.items {
305 ui.horizontal(|ui| {
306 if ui.add(egui::Link::new(RichText::new(&b.title).color(p.link))).on_hover_text(&b.url).clicked() {
307 open = Some(b.url.clone());
308 }
309 ui.with_layout(egui::Layout::right_to_left(egui::Align::Center), |ui| {
310 if ui.small_button("Remove").clicked() {
311 remove = Some(b.url.clone());
312 }
313 });
314 });
315 }
316 });
317 if let Some(u) = remove {
318 app.bookmarks.remove(&u);
319 }
320 if let Some(u) = open {
321 app.open(u, true);
322 }
323 ui.add_space(12.0);
324 ui.separator();
325 ui.label(RichText::new("Text size").strong());
326 ui.horizontal(|ui| {
327 if ui.button("A−").clicked() {
328 app.text_scale = (app.text_scale - 0.1).max(0.7);
329 }
330 ui.label(format!("{}%", (app.text_scale * 100.0).round()));
331 if ui.button("A+").clicked() {
332 app.text_scale = (app.text_scale + 0.1).min(2.0);
333 }
334 });
335 ui.label(RichText::new("Look").strong());
336 ui.horizontal(|ui| {
337 for (label, v) in [("System", None), ("Light", Some(false)), ("Dark", Some(true))] {
338 if ui.selectable_label(app.dark == v, label).clicked() {
339 app.dark = v;
340 install_style(ui.ctx(), v);
341 }
342 }
343 });
344 ui.add_space(8.0);
345 ui.label(RichText::new(format!("Clearwake {} · {} ads and trackers blocked so far", crate::VERSION, app.blocked_total)).small().color(p.soft));
346 });
347}
348
349pub fn status_bar(app: &mut Clearwake, ui: &mut egui::Ui) {
350 let p = palette(is_dark(ui.ctx()));
351 egui::Panel::bottom("status")
352 .frame(egui::Frame::new().fill(p.bar).inner_margin(egui::Margin::symmetric(14, 4)).stroke(Stroke::new(1.0, p.line)))
353 .show(ui, |ui| {
354 ui.horizontal(|ui| {
355 if let Some(h) = &app.hover {
356 ui.label(RichText::new(h).small().color(p.soft));
357 } else {
358 let msg = match &app.page {
359 Page::Doc(d) => {
360 let n = d.stats.blocked + d.stats.hidden;
361 if n > 0 || d.stats.scripts_skipped > 0 {
362 format!("Blocked {} ads and trackers · {} scripts never ran", n, d.stats.scripts_skipped)
363 } else {
364 String::from("No ads found on this page")
365 }
366 }
367 Page::Loading { .. } => String::from("Loading…"),
368 _ => String::from("Ad blocking is always on"),
369 };
370 shield(ui, p.accent);
371 ui.label(RichText::new(msg).small().color(p.soft));
372 }
373 });
374 });
375 app.hover = None;
376}
377
378pub fn content(app: &mut Clearwake, ui: &mut egui::Ui) {
379 let p = palette(is_dark(ui.ctx()));
380 egui::CentralPanel::default().frame(egui::Frame::new().fill(p.bg)).show(ui, |ui| {
381 let mut scroll = egui::ScrollArea::vertical().auto_shrink([false, false]);
382 if app.scroll_to_top {
383 scroll = scroll.vertical_scroll_offset(0.0);
384 app.scroll_to_top = false;
385 }
386 scroll.show(ui, |ui| {
387 let full = ui.available_width();
388 let col = full.min(COLUMN);
389 let side = ((full - col) / 2.0).max(0.0);
390 ui.horizontal(|ui| {
391 ui.add_space(side);
392 ui.vertical(|ui| {
393 ui.set_width(col - 24.0);
394 ui.add_space(28.0);
395 page(app, ui, &p);
396 ui.add_space(64.0);
397 });
398 });
399 });
400 });
401}
402
403enum Action {
404 Open(String),
405 Search(String),
406}
407
408fn page(app: &mut Clearwake, ui: &mut egui::Ui, p: &Palette) {
409 let mut action: Option<Action> = None;
410 let scale = app.text_scale;
411 match &app.page {
412 Page::Home => home(app, ui, p, &mut action),
413 Page::Loading { url } => {
414 ui.add_space(80.0);
415 ui.vertical_centered(|ui| {
416 ui.spinner();
417 ui.label(RichText::new(short(url)).color(p.soft));
418 });
419 }
420 Page::Error { url, message } => {
421 ui.add_space(60.0);
422 ui.label(RichText::new("This page didn't open").size(26.0 * scale).strong().color(p.ink));
423 ui.add_space(8.0);
424 ui.label(RichText::new(message).size(17.0 * scale).color(p.ink));
425 ui.add_space(4.0);
426 ui.label(RichText::new(short(url)).small().color(p.soft));
427 ui.add_space(14.0);
428 if ui.button("Try again").clicked() {
429 action = Some(Action::Open(url.clone()));
430 }
431 }
432 Page::Text { text, .. } => {
433 ui.add(egui::Label::new(RichText::new(text).monospace().size(14.5 * scale).color(p.ink)).wrap());
434 }
435 Page::Picture { url } => {
436 picture(ui, app.pictures.get(url), "", None, None, &mut action, p);
437 }
438 Page::Doc(doc) => {
439 if !doc.title.is_empty() {
440 ui.label(RichText::new(doc.url.display_host()).small().color(p.soft));
441 ui.add_space(2.0);
442 }
443 if doc.blocks.is_empty() {
444 ui.add_space(20.0);
445 ui.label(RichText::new("This page has nothing Clearwake can show without running its scripts.").color(p.soft));
446 }
447 for (i, block) in doc.blocks.iter().enumerate() {
448 match block {
449 Block::Heading { level, spans } => {
450 let size = match level {
451 1 => 32.0,
452 2 => 25.0,
453 3 => 21.0,
454 _ => 18.5,
455 } * scale;
456 ui.add_space(if *level <= 2 { 18.0 } else { 12.0 });
457 spans_ui(ui, spans, size, true, false, p, &mut app.hover, &mut action);
458 ui.add_space(4.0);
459 }
460 Block::Paragraph { spans, quote } => {
461 ui.add_space(6.0);
462 if *quote {
463 egui::Frame::new()
464 .inner_margin(egui::Margin { left: 14, right: 0, top: 2, bottom: 2 })
465 .show(ui, |ui| {
466 let r = ui.max_rect();
467 ui.painter().vline(r.left() - 12.0, r.y_range(), Stroke::new(3.0, p.line));
468 spans_ui(ui, spans, 17.0 * scale, false, true, p, &mut app.hover, &mut action);
469 });
470 } else {
471 spans_ui(ui, spans, 17.0 * scale, false, false, p, &mut app.hover, &mut action);
472 }
473 ui.add_space(6.0);
474 }
475 Block::ListItem { depth, marker, spans } => {
476 ui.horizontal_top(|ui| {
477 ui.add_space(8.0 + 20.0 * (*depth as f32 - 1.0).clamp(0.0, 6.0));
478 ui.add_sized([22.0, 20.0 * scale], egui::Label::new(RichText::new(marker).size(17.0 * scale).color(p.soft)));
479 ui.vertical(|ui| {
480 spans_ui(ui, spans, 17.0 * scale, false, false, p, &mut app.hover, &mut action);
481 });
482 });
483 ui.add_space(3.0);
484 }
485 Block::Code(code) => {
486 ui.add_space(6.0);
487 egui::Frame::new().fill(p.bar).corner_radius(8).inner_margin(12).show(ui, |ui| {
488 egui::ScrollArea::horizontal().id_salt(("code", i)).show(ui, |ui| {
489 ui.add(egui::Label::new(RichText::new(code).monospace().size(14.0 * scale).color(p.ink)).extend());
490 });
491 });
492 ui.add_space(6.0);
493 }
494 Block::Image { src, alt, width, link, .. } => {
495 ui.add_space(6.0);
496 picture(ui, app.pictures.get(src), alt, *width, link.as_deref(), &mut action, p);
497 ui.add_space(6.0);
498 }
499 Block::Rule => {
500 ui.add_space(8.0);
501 ui.separator();
502 ui.add_space(8.0);
503 }
504 Block::Form(form) => {
505 let text = app.forms.entry(i).or_insert_with(|| form.value.clone());
506 if let Some(t) = form_ui(ui, form, text, p) {
507 action = Some(Action::Open(form.target(&t)));
508 }
509 }
510 }
511 }
512 }
513 }
514 match action {
515 Some(Action::Open(u)) => app.open(u, true),
516 Some(Action::Search(s)) => app.open_input(&s),
517 None => {}
518 }
519}
520
521fn home(app: &mut Clearwake, ui: &mut egui::Ui, p: &Palette, action: &mut Option<Action>) {
522 ui.add_space(70.0);
523 ui.vertical_centered(|ui| {
524 let (rect, _) = ui.allocate_exact_size(Vec2::splat(64.0), egui::Sense::hover());
525 draw_mark(ui.painter(), rect, p);
526 ui.add_space(10.0);
527 ui.label(RichText::new("Clearwake").size(40.0).strong().color(p.ink));
528 ui.label(RichText::new("Less noise. More web.").size(18.0).color(p.soft));
529 ui.add_space(24.0);
530 let resp = ui.add(
531 egui::TextEdit::singleline(&mut app.home_search)
532 .hint_text("Search the web or type an address")
533 .desired_width(ui.available_width().min(520.0))
534 .margin(egui::Margin::symmetric(18, 11))
535 .font(FontId::new(17.0, FontFamily::Proportional)),
536 );
537 let go = resp.lost_focus() && ui.input(|i| i.key_pressed(Key::Enter));
538 ui.add_space(10.0);
539 let clicked = ui.add(egui::Button::new(RichText::new("Search").color(Color32::WHITE)).fill(p.accent).min_size(Vec2::new(120.0, 36.0))).clicked();
540 if (go || clicked) && !app.home_search.trim().is_empty() {
541 *action = Some(Action::Search(app.home_search.trim().to_string()));
542 }
543 ui.add_space(26.0);
544 ui.label(
545 RichText::new(format!(
546 "Ads and trackers are blocked before they load. Scripts never run. {} built-in rules, always on.",
547 app.filter.rule_count()
548 ))
549 .color(p.soft),
550 );
551 });
552 if !app.bookmarks.items.is_empty() {
553 ui.add_space(36.0);
554 ui.label(RichText::new("Bookmarks").size(20.0).strong().color(p.ink));
555 ui.add_space(6.0);
556 for b in &app.bookmarks.items {
557 if ui.add(egui::Link::new(RichText::new(&b.title).size(17.0).color(p.link))).on_hover_text(&b.url).clicked() {
558 *action = Some(Action::Open(b.url.clone()));
559 }
560 ui.add_space(2.0);
561 }
562 }
563}
564
565#[allow(clippy::too_many_arguments)]
566fn spans_ui(ui: &mut egui::Ui, spans: &[Inline], size: f32, heading: bool, quote: bool, p: &Palette, hover: &mut Option<String>, action: &mut Option<Action>) {
567 ui.horizontal_wrapped(|ui| {
568 ui.spacing_mut().item_spacing = Vec2::new(0.0, 3.0);
569 for sp in spans {
570 for (n, piece) in sp.text.split('\n').enumerate() {
571 if n > 0 {
572 ui.end_row();
573 }
574 if piece.is_empty() {
575 continue;
576 }
577 let mut rt = RichText::new(piece).size(size);
578 if sp.code {
579 rt = rt.monospace().size(size * 0.88).background_color(p.bar);
580 }
581 if heading || sp.bold {
582 rt = rt.strong();
583 }
584 if sp.italic || quote {
585 rt = rt.italics();
586 }
587 match &sp.link {
588 Some(href) => {
589 let r = ui.add(egui::Link::new(rt.color(p.link)));
590 if r.hovered() {
591 *hover = Some(href.clone());
592 }
593 if r.clicked() {
594 *action = Some(Action::Open(href.clone()));
595 }
596 }
597 None => {
598 let color = if heading { p.ink } else if quote { p.soft } else { p.ink };
599 ui.add(egui::Label::new(rt.color(color)).wrap());
600 }
601 }
602 }
603 }
604 });
605}
606
607fn picture(ui: &mut egui::Ui, pic: Option<&Picture>, alt: &str, width: Option<u32>, link: Option<&str>, action: &mut Option<Action>, p: &Palette) {
608 let max_w = ui.available_width();
609 match pic {
610 Some(Picture::Ready(tex)) => {
611 let [w, h] = tex.resolution();
612 let mut want = w as f32;
613 if let Some(attr) = width {
614 want = want.min(attr as f32 * 1.0);
615 }
616 let scale = (want.min(max_w)) / w as f32;
617 let size = Vec2::new(w as f32 * scale, h as f32 * scale);
618 let mut img = egui::Image::new(egui::load::SizedTexture::new(tex.id(), size)).corner_radius(6);
619 if link.is_some() {
620 img = img.sense(egui::Sense::click());
621 }
622 let r = ui.add(img);
623 let r = if alt.is_empty() { r } else { r.on_hover_text(alt) };
624 if let Some(l) = link {
625 if r.clicked() {
626 *action = Some(Action::Open(l.to_string()));
627 }
628 }
629 }
630 Some(Picture::Loading) => {
631 let h = 120.0_f32.min(max_w * 0.4);
632 let (rect, _) = ui.allocate_exact_size(Vec2::new(max_w.min(width.map(|w| w as f32).unwrap_or(max_w)), h), egui::Sense::hover());
633 ui.painter().rect_filled(rect, 6.0, p.bar);
634 if !alt.is_empty() {
635 ui.painter().text(rect.center(), egui::Align2::CENTER_CENTER, short(alt), FontId::proportional(14.0), p.soft);
636 }
637 }
638 _ => {
639 if !alt.is_empty() {
640 ui.label(RichText::new(format!("[{alt}]")).italics().color(p.soft));
641 }
642 }
643 }
644}
645
646fn form_ui(ui: &mut egui::Ui, form: &Form, text: &mut String, p: &Palette) -> Option<String> {
647 let mut out = None;
648 ui.add_space(6.0);
649 ui.horizontal(|ui| {
650 let hint = if form.placeholder.is_empty() { "Search" } else { form.placeholder.as_str() };
651 let r = ui.add(egui::TextEdit::singleline(text).hint_text(hint).desired_width((ui.available_width() - 110.0).max(120.0)).margin(egui::Margin::symmetric(12, 6)));
652 let enter = r.lost_focus() && ui.input(|i| i.key_pressed(Key::Enter));
653 let label = if form.submit.is_empty() { "Search" } else { form.submit.as_str() };
654 let click = ui.add(egui::Button::new(RichText::new(label).color(Color32::WHITE)).fill(p.accent)).clicked();
655 if enter || click {
656 out = Some(text.clone());
657 }
658 });
659 ui.add_space(6.0);
660 out
661}
662
663fn short(s: &str) -> String {
664 if s.chars().count() > 90 {
665 let mut t: String = s.chars().take(88).collect();
666 t.push('…');
667 t
668 } else {
669 s.to_string()
670 }
671}
672
673/// The Clearwake mark: a calm circle with a wake line through it.
674fn draw_mark(painter: &egui::Painter, rect: egui::Rect, p: &Palette) {
675 let c = rect.center();
676 let r = rect.width() / 2.0;
677 painter.circle_filled(c, r, p.accent);
678 let wave = |y: f32, amp: f32| -> Vec<egui::Pos2> {
679 (0..=24).map(|i| {
680 let t = i as f32 / 24.0;
681 let x = c.x - r * 0.62 + t * r * 1.24;
682 egui::pos2(x, y + (t * std::f32::consts::TAU).sin() * amp)
683 }).collect()
684 };
685 painter.add(egui::Shape::line(wave(c.y - r * 0.12, r * 0.12), Stroke::new(r * 0.11, Color32::WHITE)));
686 painter.add(egui::Shape::line(wave(c.y + r * 0.22, r * 0.08), Stroke::new(r * 0.08, Color32::from_white_alpha(170))));
687}
688
689/// The window icon, drawn in code so every platform shares it.
690#[allow(dead_code)]
691pub fn app_icon() -> egui::IconData {
692 let n = 128usize;
693 let mut rgba = vec![0u8; n * n * 4];
694 let (cr, cg, cb) = (0x1f_u8, 0x7a_u8, 0x6c_u8);
695 let c = n as f32 / 2.0;
696 let r = c - 4.0;
697 for y in 0..n {
698 for x in 0..n {
699 let (fx, fy) = (x as f32 + 0.5 - c, y as f32 + 0.5 - c);
700 let d = (fx * fx + fy * fy).sqrt();
701 let a = (r + 0.5 - d).clamp(0.0, 1.0);
702 if a <= 0.0 {
703 continue;
704 }
705 let t = (fx + r * 0.62) / (r * 1.24);
706 let mut px = (cr, cg, cb);
707 if (0.0..=1.0).contains(&t) {
708 let w1 = -r * 0.12 + (t * std::f32::consts::TAU).sin() * r * 0.12;
709 let w2 = r * 0.22 + (t * std::f32::consts::TAU).sin() * r * 0.08;
710 if (fy - w1).abs() < r * 0.055 {
711 px = (255, 255, 255);
712 } else if (fy - w2).abs() < r * 0.04 {
713 px = (190, 222, 216);
714 }
715 }
716 let i = (y * n + x) * 4;
717 rgba[i] = px.0;
718 rgba[i + 1] = px.1;
719 rgba[i + 2] = px.2;
720 rgba[i + 3] = (a * 255.0) as u8;
721 }
722 }
723 egui::IconData { rgba, width: n as u32, height: n as u32 }
724}