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