diff --git a/src/ui/menus.rs b/src/ui/menus.rs index 2242acf..25c2345 100644 --- a/src/ui/menus.rs +++ b/src/ui/menus.rs @@ -210,7 +210,7 @@ pub fn one_line(ui: &Ui, text: &str, font: egui::FontId, color: Color32, width: } /// A menu row that opens a submenu to its right. -fn submenu(ui: &mut Ui, glyph: &str, label: &str, contents: impl FnOnce(&mut Ui)) { +pub fn submenu(ui: &mut Ui, glyph: &str, label: &str, contents: impl FnOnce(&mut Ui)) { let p = theme::palette(ui.ctx()); let r = menu_item(ui, Some(glyph), label, "", false); icon(ui, pos2(r.rect.right() - theme::GAP_M - 10.0, r.rect.center().y), icons::CHEVRON_RIGHT, theme::ICON, false, p.on_surface_variant); diff --git a/src/ui/pages/apps.rs b/src/ui/pages/apps.rs index c8b08aa..1fc2fec 100644 --- a/src/ui/pages/apps.rs +++ b/src/ui/pages/apps.rs @@ -3,69 +3,87 @@ use egui::{RichText, Ui}; -use super::super::widgets::{card, page_column, switch}; +use super::super::widgets::{ButtonKind, button, card, empty_state, icon_button, icon_row, page_column, page_header, row, section, switch, text_field}; use super::super::Frame; use crate::actions::Command; use crate::store::scripts::{self, Kind}; -use crate::util; +use crate::theme::{self, Weight}; use crate::ui::icons; +use crate::util; const NEW_SCRIPT: &str = "// ==UserScript==\n// @name My script\n// @match https://example.com/*\n// @run-at document-end\n// ==/UserScript==\n\n"; pub fn draw(ui: &mut Ui, f: &mut Frame) { page_column(ui, "apps", 900.0, |ui| { - ui.heading("Apps"); - ui.label(RichText::new("Sites installed as apps open in their own window and have a launcher entry.").weak()); - let apps = f.shared.apps.borrow().items.clone(); - card(ui, |ui| { - if apps.is_empty() { - ui.label(RichText::new("No apps yet. Open a site and choose “Install this site as an app” from the menu.").weak()); - } - for app in apps { - ui.horizontal(|ui| { - ui.label(RichText::new(&app.name).strong()); - ui.label(RichText::new(&app.scope).weak().small()); - ui.with_layout(egui::Layout::right_to_left(egui::Align::Center), |ui| { - if ui.small_button("Remove").clicked() { - f.send(Command::UninstallApp(app.id.clone())); - } - let mut launcher = app.launcher; - if switch(ui, &mut launcher) { - f.send(Command::SetAppLauncher { id: app.id.clone(), on: launcher }); - } - ui.label(RichText::new("Launcher entry").small()); - if ui.button("Open").clicked() { - f.send(Command::OpenApp(app.id.clone())); - } - }); - }); - ui.separator(); - } - if f.tab().is_some_and(|t| t.page.is_none() && t.url.starts_with("http")) && ui.button("Install the current site").clicked() { + let can_install = f.tab().is_some_and(|t| t.page.is_none() && t.url.starts_with("http")); + page_header(ui, "Apps", "Sites installed as apps open in their own window and have a launcher entry.", |ui| { + if can_install && button(ui, ButtonKind::Tonal, Some(icons::INSTALL_DESKTOP), "Install this site").clicked() { f.send(Command::InstallApp); } }); - ui.add_space(16.0); - ui.heading("Scripts and styles"); - ui.label(RichText::new("User scripts (.user.js) and user styles (.user.css) change sites the way extensions do. They run on matching pages from their next load. Opening a .user.js link offers to install it.").weak()); - ui.horizontal(|ui| { - ui.add(egui::TextEdit::singleline(&mut f.ui.pages.script_url).hint_text("https://…/script.user.js").desired_width(420.0)); - if ui.button("Install from address").clicked() && f.ui.pages.script_url.starts_with("http") { - let url = std::mem::take(&mut f.ui.pages.script_url); - f.send(Command::InstallScript(url)); + let apps = f.shared.apps.borrow().items.clone(); + section(ui, "Installed apps"); + card(ui, |ui| { + if apps.is_empty() { + empty_state(ui, icons::APPS, "No apps yet", "Open a site, then choose Install this site as an app from the menu. It opens in its own window.", None); + ui.add_space(theme::GAP_L); } - if ui.button("New script").clicked() { - f.ui.pages.script_edit = Some((0, NEW_SCRIPT.to_owned())); - } - if ui.button("New style").clicked() { - f.ui.pages.script_edit = Some((u64::MAX, "/* ==UserStyle==\n@name My style\n@match https://example.com/*\n==/UserStyle== */\n\n".to_owned())); + for app in apps { + icon_row(ui, icons::WEB, &app.name, &app.scope, |ui| { + if icon_button(ui, icons::DELETE, "Remove app").clicked() { + f.send(Command::UninstallApp(app.id.clone())); + } + let mut launcher = app.launcher; + if switch(ui, &mut launcher) { + f.send(Command::SetAppLauncher { id: app.id.clone(), on: launcher }); + } + ui.label(RichText::new("Launcher entry").font(theme::font(theme::SMALL, Weight::Regular)).color(f.palette().on_surface_variant)); + if button(ui, ButtonKind::Tonal, Some(icons::OPEN_IN_NEW), "Open").clicked() { + f.send(Command::OpenApp(app.id.clone())); + } + }); } }); + section(ui, "Scripts and styles"); + ui.horizontal(|ui| { + ui.add_space(theme::GAP_L); + ui.scope(|ui| { + ui.set_max_width(ui.available_width() - theme::GAP_L); + ui.label(RichText::new("User scripts (.user.js) and user styles (.user.css) change sites the way extensions do. They run on matching pages from their next load. Opening a .user.js link offers to install it.").font(theme::font(theme::SMALL, Weight::Regular)).color(f.palette().on_surface_variant)); + }); + }); + ui.add_space(theme::GAP_S); + card(ui, |ui| { + row(ui, "Install from an address", "A link to a .user.js or .user.css file.", |ui| { + let valid = f.ui.pages.script_url.starts_with("http"); + if ui.add_enabled_ui(valid, |ui| button(ui, ButtonKind::Tonal, None, "Install")).inner.clicked() { + let url = std::mem::take(&mut f.ui.pages.script_url); + f.send(Command::InstallScript(url)); + } + text_field(ui, &mut f.ui.pages.script_url, "https://example.com/script.user.js", 280.0); + }); + row(ui, "Write your own", "Start from a template and edit it here.", |ui| { + if button(ui, ButtonKind::Outlined, Some(icons::PALETTE), "New style").clicked() { + f.ui.pages.script_edit = Some((u64::MAX, "/* ==UserStyle==\n@name My style\n@match https://example.com/*\n==/UserStyle== */\n\n".to_owned())); + } + if button(ui, ButtonKind::Outlined, Some(icons::CODE), "New script").clicked() { + f.ui.pages.script_edit = Some((0, NEW_SCRIPT.to_owned())); + } + }); + }); if let Some((id, mut source)) = f.ui.pages.script_edit.clone() { + section(ui, if id == 0 || id == u64::MAX { "New" } else { "Edit" }); card(ui, |ui| { - ui.add(egui::TextEdit::multiline(&mut source).code_editor().desired_rows(16).desired_width(f32::INFINITY)); ui.horizontal(|ui| { - if ui.button("Save").clicked() { + ui.add_space(theme::GAP_L); + ui.add(egui::TextEdit::multiline(&mut source).code_editor().desired_rows(16).desired_width(ui.available_width() - theme::GAP_L)); + }); + ui.add_space(theme::GAP_S); + let mut finished = false; + ui.horizontal(|ui| { + ui.add_space(theme::GAP_L); + ui.spacing_mut().item_spacing.x = theme::GAP_S; + if button(ui, ButtonKind::Filled, Some(icons::SAVE), "Save").clicked() { match id { 0 => { f.shared.scripts.borrow_mut().add(Kind::Script, &source, ""); @@ -75,57 +93,50 @@ pub fn draw(ui: &mut Ui, f: &mut Frame) { }, id => f.shared.scripts.borrow_mut().set_source(id, &source), } - f.ui.pages.script_edit = None; + finished = true; f.send(Command::Toast("Saved. It applies on matching pages from their next load.".into())); f.send(Command::ReloadScripts); - return; + } else if button(ui, ButtonKind::Text, None, "Cancel").clicked() { + finished = true; } - if ui.button("Cancel").clicked() { - f.ui.pages.script_edit = None; - return; - } - f.ui.pages.script_edit = Some((id, source.clone())); }); + ui.add_space(theme::GAP_S); + f.ui.pages.script_edit = if finished { None } else { Some((id, source)) }; }); } let items = f.shared.scripts.borrow().items.clone(); + ui.add_space(theme::GAP_M); card(ui, |ui| { if items.is_empty() { - ui.label(RichText::new("No scripts or styles installed.").weak()); + empty_state(ui, icons::CODE, "No scripts or styles", "Install one from an address above, or write your own.", None); + ui.add_space(theme::GAP_L); } for item in items { let header = item.header(); - ui.horizontal(|ui| { + let mut detail = header.matches.join(", "); + if !header.version.is_empty() { + detail = format!("v{} · {detail}", header.version); + } + if !header.description.is_empty() { + detail = format!("{} · {}", util::ellipsize(&header.description, 80), detail); + } + icon_row(ui, if item.kind == Kind::Script { icons::CODE } else { icons::PALETTE }, &header.name, &util::ellipsize(&detail, 140), |ui| { + if icon_button(ui, icons::DELETE, "Delete").clicked() { + f.shared.scripts.borrow_mut().remove(item.id); + f.send(Command::ReloadScripts); + } + if icon_button(ui, icons::EDIT, "Edit").clicked() { + f.ui.pages.script_edit = Some((item.id, item.source.clone())); + } + if !item.origin_url.is_empty() && icon_button(ui, icons::REFRESH, "Update from its address").clicked() { + f.send(Command::InstallScript(item.origin_url.clone())); + } let mut on = item.enabled; if switch(ui, &mut on) { f.shared.scripts.borrow_mut().set_enabled(item.id, on); f.send(Command::ReloadScripts); } - ui.vertical(|ui| { - ui.label(RichText::new(format!("{} {}", if item.kind == Kind::Script { icons::CODE } else { icons::PALETTE }, header.name)).strong()); - let mut detail = header.matches.join(", "); - if !header.version.is_empty() { - detail = format!("v{} · {detail}", header.version); - } - ui.label(RichText::new(util::ellipsize(&detail, 100)).weak().small()); - if !header.description.is_empty() { - ui.label(RichText::new(util::ellipsize(&header.description, 120)).small()); - } - }); - ui.with_layout(egui::Layout::right_to_left(egui::Align::Center), |ui| { - if ui.small_button("Delete").clicked() { - f.shared.scripts.borrow_mut().remove(item.id); - f.send(Command::ReloadScripts); - } - if ui.small_button("Edit").clicked() { - f.ui.pages.script_edit = Some((item.id, item.source.clone())); - } - if !item.origin_url.is_empty() && ui.small_button("Update").on_hover_text(&item.origin_url).clicked() { - f.send(Command::InstallScript(item.origin_url.clone())); - } - }); }); - ui.separator(); } }); let _ = scripts::pattern_to_regex; diff --git a/src/ui/pages/bookmarks.rs b/src/ui/pages/bookmarks.rs index 8e93ec4..918ce5e 100644 --- a/src/ui/pages/bookmarks.rs +++ b/src/ui/pages/bookmarks.rs @@ -1,110 +1,130 @@ //! nacre://bookmarks — the bookmark manager: folders, editing, moving, import and export, //! and the reading list. -use egui::{RichText, Ui}; +use egui::Ui; -use super::super::menus::{folder_list, parent_of}; -use super::super::widgets::page_column; +use super::super::menus::{folder_list, menu_item, submenu}; +use super::super::widgets::{ButtonKind, button, card, chip, empty_state, icon_button, list_item, more_menu, page_column, page_header, search_pill, section, text_field}; use super::super::Frame; use crate::actions::{Command, Disposition}; -use crate::store::bookmarks::{Node, BAR}; -use crate::util; +use crate::store::bookmarks::{BAR, Node}; use crate::ui::icons; +use crate::util; pub fn draw(ui: &mut Ui, f: &mut Frame) { page_column(ui, "bookmarks", 900.0, |ui| { - ui.horizontal(|ui| { - ui.heading("Bookmarks"); - ui.with_layout(egui::Layout::right_to_left(egui::Align::Center), |ui| { - if ui.button("Export").clicked() { - f.send(Command::ExportBookmarks); - } - if ui.button("Import file").clicked() { - f.send(Command::ImportBookmarksFile); - } - if ui.button("Import from a browser").clicked() { - f.send(Command::ShowPage(crate::tabs::Page::Settings("import".into()))); - } - }); + page_header(ui, "Bookmarks", "", |ui| { + if button(ui, ButtonKind::Outlined, Some(icons::DOWNLOAD), "Export").clicked() { + f.send(Command::ExportBookmarks); + } + if button(ui, ButtonKind::Outlined, Some(icons::UPLOAD), "Import file").clicked() { + f.send(Command::ImportBookmarksFile); + } + if button(ui, ButtonKind::Text, None, "Import from a browser").clicked() { + f.send(Command::ShowPage(crate::tabs::Page::Settings("import".into()))); + } }); - ui.add(egui::TextEdit::singleline(&mut f.ui.pages.bookmarks_search).hint_text("Search bookmarks").desired_width(f32::INFINITY)); - ui.add_space(8.0); + search_pill(ui, "bookmarks", &mut f.ui.pages.bookmarks_search, "Search bookmarks"); let query = f.ui.pages.bookmarks_search.clone(); if !query.trim().is_empty() { let found = f.shared.bookmarks.borrow().search(&query, 200); if found.is_empty() { - ui.label(RichText::new("Nothing matches.").weak()); + empty_state(ui, icons::SEARCH, "Nothing matches", &format!("No bookmark matches \"{}\". Try a different word or part of an address.", query.trim()), None); + return; } - for (url, title) in found { - if ui.link(util::ellipsize(&title, 90)).on_hover_text(&url).clicked() { - f.send(Command::Open { url, how: Disposition::Current, from: None }); + section(ui, &format!("{} found", found.len())); + card(ui, |ui| { + for (index, (url, title)) in found.into_iter().enumerate() { + let name = if title.is_empty() { url.clone() } else { title }; + let r = list_item(ui, ("found", index), Some(icons::BOOKMARK), &name, &util::host_of_str(&url), |_| {}).on_hover_text(&url); + if r.clicked() && !url.is_empty() { + f.send(Command::Open { url, how: Disposition::Current, from: None }); + } } - } + }); return; } let folders = folder_list(&f.shared.bookmarks.borrow()); - let current = f.ui.pages.bookmarks_folder.unwrap_or(BAR); + let current = f.ui.pages.bookmarks_folder.filter(|id| folders.iter().any(|(i, _)| i == id)).unwrap_or(BAR); + ui.add_space(crate::theme::GAP_S); ui.horizontal_wrapped(|ui| { for (id, name) in &folders { - if ui.selectable_label(current == *id, format!("{} {name}", icons::FOLDER)).clicked() { + if chip(ui, Some(icons::FOLDER), name, current == *id).clicked() { f.ui.pages.bookmarks_folder = Some(*id); } } }); - ui.horizontal(|ui| { - ui.add(egui::TextEdit::singleline(&mut f.ui.pages.new_folder).hint_text("New folder name")); - if ui.button("Add folder here").clicked() { - let name = std::mem::take(&mut f.ui.pages.new_folder); - f.shared.bookmarks.borrow_mut().add_folder(current, &name); - } - if let Some(tab) = f.tab().filter(|t| t.page.is_none()) - && ui.button("Add the current page here").clicked() - { - let (title, url) = (tab.display_title(), tab.url.clone()); - f.shared.bookmarks.borrow_mut().add(current, &title, &url); - } - }); - ui.separator(); let node: Option = f.shared.bookmarks.borrow().get(current).cloned(); let Some(node) = node else { return }; - if node.children.is_empty() { - ui.label(RichText::new("This folder is empty.").weak()); - } - let count = node.children.len(); - for (index, child) in node.children.iter().enumerate() { - bookmark_row(ui, f, child, current, index, count, &folders); - } - // Reading list. - ui.add_space(16.0); - ui.horizontal(|ui| { - ui.label(RichText::new("Reading list").strong()); - if let Some(tab) = f.tab().filter(|t| t.page.is_none()) - && ui.small_button("Add the current page").clicked() - { - let (url, title) = (tab.url.clone(), tab.display_title()); - f.shared.reading.borrow_mut().add(&url, &title); + let on_page = f.tab().filter(|t| t.page.is_none()).map(|t| (t.display_title(), t.url.clone())); + section(ui, &folders.iter().find(|(id, _)| *id == current).map_or("Bookmarks", |(_, n)| n.as_str()).to_owned()); + card(ui, |ui| { + ui.horizontal(|ui| { + ui.add_space(crate::theme::GAP_L - ui.spacing().item_spacing.x); + ui.spacing_mut().item_spacing.x = crate::theme::GAP_S; + let width = (ui.available_width() - 2.0 * crate::theme::GAP_L - if on_page.is_some() { 300.0 } else { 140.0 }).clamp(120.0, 260.0); + let submitted = text_field(ui, &mut f.ui.pages.new_folder, "New folder name", width).lost_focus() && ui.input(|i| i.key_pressed(egui::Key::Enter)); + let named = !f.ui.pages.new_folder.trim().is_empty(); + if (ui.add_enabled_ui(named, |ui| button(ui, ButtonKind::Tonal, Some(icons::ADD), "Add folder")).inner.clicked() || submitted) && named { + let name = std::mem::take(&mut f.ui.pages.new_folder); + f.shared.bookmarks.borrow_mut().add_folder(current, name.trim()); + } + if let Some((title, url)) = &on_page + && button(ui, ButtonKind::Text, Some(icons::BOOKMARK_ADD), "Add this page").clicked() + { + f.shared.bookmarks.borrow_mut().add(current, title, url); + } + }); + ui.add_space(crate::theme::GAP_S); + if node.children.is_empty() { + empty_state(ui, icons::BOOKMARKS, "This folder is empty", "Press Ctrl+D on a page to bookmark it, or import bookmarks from a file or another browser.", None); + ui.add_space(crate::theme::GAP_L); + } + let count = node.children.len(); + for (index, child) in node.children.iter().enumerate() { + bookmark_row(ui, f, child, current, index, count, &folders); } }); - let items: Vec = f.shared.reading.borrow().items.clone(); + reading_list(ui, f); + }); +} + + +fn reading_list(ui: &mut Ui, f: &mut Frame) { + section(ui, "Reading list"); + let items: Vec = f.shared.reading.borrow().items.clone(); + let on_page = f.tab().filter(|t| t.page.is_none()).map(|t| (t.display_title(), t.url.clone())); + card(ui, |ui| { + if let Some((title, url)) = &on_page { + ui.horizontal(|ui| { + ui.add_space(crate::theme::GAP_L - ui.spacing().item_spacing.x); + if button(ui, ButtonKind::Tonal, Some(icons::ADD), "Add this page").clicked() { + f.shared.reading.borrow_mut().add(url, title); + } + }); + ui.add_space(crate::theme::GAP_S); + } if items.is_empty() { - ui.label(RichText::new("Pages saved for later show here.").weak()); + empty_state(ui, icons::STICKY_NOTE_2, "Nothing saved for later", "Open a page, come back here and choose Add this page to keep it for when you have time.", None); + ui.add_space(crate::theme::GAP_L); } for item in items { - ui.horizontal(|ui| { - let text = if item.read { RichText::new(util::ellipsize(&item.title, 80)).weak() } else { RichText::new(util::ellipsize(&item.title, 80)) }; - if ui.link(text).on_hover_text(&item.url).clicked() { - f.shared.reading.borrow_mut().set_read(&item.url, true); - f.send(Command::Navigate(item.url.clone())); + let name = if item.title.is_empty() { item.url.clone() } else { item.title.clone() }; + let host = util::host_of_str(&item.url); + let state = if item.read { "Read" } else { "Unread" }; + let detail = if host.is_empty() { state.to_owned() } else { format!("{state} · {host}") }; + let r = list_item(ui, ("reading", &item.url), Some(icons::STICKY_NOTE_2), &name, &detail, |ui| { + if icon_button(ui, icons::CLOSE, "Remove from the reading list").clicked() { + f.shared.reading.borrow_mut().remove(&item.url); + } + if icon_button(ui, if item.read { icons::UNDO } else { icons::CHECK }, if item.read { "Mark unread" } else { "Mark read" }).clicked() { + f.shared.reading.borrow_mut().set_read(&item.url, !item.read); } - ui.with_layout(egui::Layout::right_to_left(egui::Align::Center), |ui| { - if ui.small_button("Remove").clicked() { - f.shared.reading.borrow_mut().remove(&item.url); - } - if ui.small_button(if item.read { "Mark unread" } else { "Mark read" }).clicked() { - f.shared.reading.borrow_mut().set_read(&item.url, !item.read); - } - }); }); + if r.on_hover_text(&item.url).clicked() { + f.shared.reading.borrow_mut().set_read(&item.url, true); + f.send(Command::Navigate(item.url.clone())); + } } }); } @@ -113,65 +133,75 @@ fn bookmark_row(ui: &mut Ui, f: &mut Frame, node: &Node, parent: u64, index: usi let editing = f.ui.pages.bookmark_edit.as_ref().is_some_and(|(id, ..)| *id == node.id); if editing { let (id, mut title, mut url) = f.ui.pages.bookmark_edit.clone().expect("checked"); - ui.horizontal(|ui| { - ui.add(egui::TextEdit::singleline(&mut title).desired_width(200.0)); + let mut done = false; + ui.horizontal_wrapped(|ui| { + ui.spacing_mut().item_spacing = egui::vec2(crate::theme::GAP_S, crate::theme::GAP_S); + ui.add_space(crate::theme::GAP_L); + text_field(ui, &mut title, "Name", 200.0); if node.url.is_some() { - ui.add(egui::TextEdit::singleline(&mut url).desired_width(360.0)); + text_field(ui, &mut url, "Address", 320.0); } - if ui.button("Save").clicked() { + if button(ui, ButtonKind::Filled, None, "Save").clicked() { f.shared.bookmarks.borrow_mut().edit(id, &title, node.url.as_ref().map(|_| url.as_str())); f.ui.pages.bookmark_edit = None; - return; - } - if ui.button("Cancel").clicked() { + done = true; + } else if button(ui, ButtonKind::Text, None, "Cancel").clicked() { f.ui.pages.bookmark_edit = None; - return; + done = true; } - f.ui.pages.bookmark_edit = Some((id, title, url)); }); + ui.add_space(crate::theme::GAP_S); + if !done { + f.ui.pages.bookmark_edit = Some((id, title, url)); + } return; } - ui.horizontal(|ui| { - match &node.url { - Some(url) => { - let r = ui.link(util::ellipsize(&node.title, 60)).on_hover_text(url); - if r.clicked() { - f.send(Command::Open { url: url.clone(), how: Disposition::Current, from: None }); - } - if r.middle_clicked() { - f.send(Command::Open { url: url.clone(), how: Disposition::NewBackgroundTab, from: None }); - } - ui.label(RichText::new(util::host_of_str(url)).weak().small()); - }, - None => { - if ui.link(format!("{} {}", icons::FOLDER, node.title)).clicked() { - f.ui.pages.bookmarks_folder = Some(node.id); - } - ui.label(RichText::new(format!("{} items", node.children.len())).weak().small()); - }, - } - ui.with_layout(egui::Layout::right_to_left(egui::Align::Center), |ui| { - if ui.small_button("Delete").clicked() { - f.shared.bookmarks.borrow_mut().remove(node.id); - } - if ui.small_button("Edit").clicked() { + let (glyph, detail) = match &node.url { + Some(url) => (icons::BOOKMARK, util::host_of_str(url)), + None => (icons::FOLDER, format!("{} {}", node.children.len(), if node.children.len() == 1 { "item" } else { "items" })), + }; + let r = list_item(ui, node.id, Some(glyph), &node.title, &detail, |ui| { + more_menu(ui, |ui| { + if menu_item(ui, Some(icons::EDIT), "Edit", "", false).clicked() { f.ui.pages.bookmark_edit = Some((node.id, node.title.clone(), node.url.clone().unwrap_or_default())); + ui.close(); } - ui.menu_button("Move to", |ui| { + if index > 0 && menu_item(ui, Some(icons::ARROW_UPWARD), "Move up", "", false).clicked() { + f.shared.bookmarks.borrow_mut().move_to(node.id, parent, Some(index - 1)); + ui.close(); + } + if index + 1 < count && menu_item(ui, Some(icons::ARROW_DOWNWARD), "Move down", "", false).clicked() { + f.shared.bookmarks.borrow_mut().move_to(node.id, parent, Some(index + 1)); + ui.close(); + } + submenu(ui, icons::FOLDER, "Move to", |ui| { for (id, name) in folders { - if *id != node.id && *id != parent && ui.button(name).clicked() { + if *id != node.id && *id != parent && menu_item(ui, None, name, "", false).clicked() { f.shared.bookmarks.borrow_mut().move_to(node.id, *id, None); ui.close(); } } }); - if index + 1 < count && ui.small_button(icons::ARROW_DOWNWARD).clicked() { - f.shared.bookmarks.borrow_mut().move_to(node.id, parent, Some(index + 1)); - } - if index > 0 && ui.small_button(icons::ARROW_UPWARD).clicked() { - f.shared.bookmarks.borrow_mut().move_to(node.id, parent, Some(index - 1)); + if menu_item(ui, Some(icons::DELETE), "Delete", "", false).clicked() { + f.shared.bookmarks.borrow_mut().remove(node.id); + ui.close(); } }); }); - let _ = parent_of; + match &node.url { + Some(url) => { + let r = r.on_hover_text(url); + if r.clicked() { + f.send(Command::Open { url: url.clone(), how: Disposition::Current, from: None }); + } + if r.middle_clicked() { + f.send(Command::Open { url: url.clone(), how: Disposition::NewBackgroundTab, from: None }); + } + }, + None => { + if r.clicked() { + f.ui.pages.bookmarks_folder = Some(node.id); + } + }, + } } diff --git a/src/ui/pages/downloads.rs b/src/ui/pages/downloads.rs index 6d6199c..3ae3640 100644 --- a/src/ui/pages/downloads.rs +++ b/src/ui/pages/downloads.rs @@ -3,42 +3,49 @@ use egui::{RichText, Ui}; use super::super::menus::download_row; -use super::super::widgets::page_column; +use super::super::widgets::{ButtonKind, button, card, empty_state, page_column, page_header, search_pill}; use super::super::Frame; use crate::actions::{Command, DownloadOp}; +use crate::theme::{self, Weight}; use crate::ui::icons; pub fn draw(ui: &mut Ui, f: &mut Frame) { page_column(ui, "downloads", 860.0, |ui| { - ui.horizontal(|ui| { - ui.heading("Downloads"); - ui.with_layout(egui::Layout::right_to_left(egui::Align::Center), |ui| { - if ui.button("Clear finished").clicked() { - f.send(Command::Download(DownloadOp::ClearFinished)); - } - if ui.button(format!("{} Open folder", icons::FOLDER_OPEN)).clicked() { - f.send(Command::Download(DownloadOp::OpenFolder)); - } - if ui.button("Settings").clicked() { - f.send(Command::ShowPage(crate::tabs::Page::Settings("downloads".into()))); - } - }); - }); let dir = f.shared.settings.borrow().downloads.dir.display().to_string(); - ui.label(RichText::new(format!("Saving to {dir}")).weak().small()); - ui.add(egui::TextEdit::singleline(&mut f.ui.pages.downloads_search).hint_text("Search downloads").desired_width(f32::INFINITY)); - ui.add_space(8.0); + page_header(ui, "Downloads", &format!("Saving to {dir}"), |ui| { + if button(ui, ButtonKind::Text, Some(icons::SETTINGS), "Settings").clicked() { + f.send(Command::ShowPage(crate::tabs::Page::Settings("downloads".into()))); + } + if button(ui, ButtonKind::Outlined, None, "Clear finished").clicked() { + f.send(Command::Download(DownloadOp::ClearFinished)); + } + if button(ui, ButtonKind::Tonal, Some(icons::FOLDER_OPEN), "Open folder").clicked() { + f.send(Command::Download(DownloadOp::OpenFolder)); + } + }); + search_pill(ui, "downloads", &mut f.ui.pages.downloads_search, "Search downloads"); + ui.add_space(theme::GAP_M); let query = f.ui.pages.downloads_search.to_lowercase(); let items: Vec = f.shared.downloads.borrow().items.iter().filter(|d| query.is_empty() || d.file_name().to_lowercase().contains(&query) || d.url.to_lowercase().contains(&query)).cloned().collect(); if items.is_empty() { - ui.label(RichText::new(if query.is_empty() { "Files you download show up here." } else { "Nothing matches." }).weak()); + if query.is_empty() { + empty_state(ui, icons::DOWNLOAD, "No downloads yet", "Files you download show up here with their progress, and open from here when they finish.", None); + } else { + empty_state(ui, icons::SEARCH, "Nothing matches", &format!("No download matches \"{}\".", query.trim()), None); + } + return; } + let p = f.palette(); for d in items { - super::super::widgets::card(ui, |ui| { + card(ui, |ui| { download_row(ui, f, &d); - ui.label(RichText::new(crate::util::ellipsize(&d.url, 110)).weak().small()); + ui.horizontal(|ui| { + ui.add_space(theme::GAP_L); + ui.add(egui::Label::new(RichText::new(crate::util::ellipsize(&d.url, 110)).font(theme::font(theme::SMALL, Weight::Regular)).color(p.on_surface_variant)).truncate()); + }); + ui.add_space(theme::GAP_S); }); - ui.add_space(4.0); + ui.add_space(theme::GAP_S); } }); } diff --git a/src/ui/pages/history.rs b/src/ui/pages/history.rs index 92caa8c..0995f50 100644 --- a/src/ui/pages/history.rs +++ b/src/ui/pages/history.rs @@ -1,77 +1,78 @@ //! nacre://history — visits grouped by day, with search and deleting. -use egui::{RichText, Ui}; +use egui::Ui; -use super::super::widgets::page_column; +use super::super::menus::menu_item; +use super::super::widgets::{ButtonKind, button, card, empty_state, icon_button, list_item, more_menu, page_column, page_header, search_pill, section}; use super::super::Frame; use crate::actions::{Command, Disposition}; +use crate::store::history::Visit; use crate::tabs::Page; -use crate::util; use crate::ui::icons; +use crate::util; pub fn draw(ui: &mut Ui, f: &mut Frame) { page_column(ui, "history", 860.0, |ui| { - ui.horizontal(|ui| { - ui.heading("History"); - ui.with_layout(egui::Layout::right_to_left(egui::Align::Center), |ui| { - if ui.button("Delete browsing data…").clicked() { - f.send(Command::ShowPage(Page::ClearData)); - } - }); + let subtitle = if f.incognito() { "Incognito windows don't keep history." } else { "" }; + page_header(ui, "History", subtitle, |ui| { + if button(ui, ButtonKind::Tonal, Some(icons::DELETE), "Clear browsing data").clicked() { + f.send(Command::ShowPage(Page::ClearData)); + } }); - if f.incognito() { - ui.label(RichText::new("Incognito windows don't keep history.").weak()); - } - ui.add(egui::TextEdit::singleline(&mut f.ui.pages.history_search).hint_text("Search history").desired_width(f32::INFINITY)); - ui.add_space(8.0); + search_pill(ui, "history", &mut f.ui.pages.history_search, "Search history"); let query = f.ui.pages.history_search.clone(); - let visits: Vec = f.shared.history.borrow().search(&query, 600).into_iter().cloned().collect(); - if visits.is_empty() { - ui.label(RichText::new(if query.is_empty() { "Pages you visit show up here." } else { "Nothing matches." }).weak()); + let visits: Vec = f.shared.history.borrow().search(&query, 600).into_iter().cloned().collect(); + let closed: Vec<(usize, String, String)> = f.shared.session.borrow().closed.iter().enumerate().take(10).map(|(i, t)| (i, t.title.clone(), t.url.clone())).collect(); + if visits.is_empty() && (closed.is_empty() || !query.is_empty()) { + if query.is_empty() { + empty_state(ui, icons::HISTORY, "No history yet", "Pages you visit show up here, newest first, so you can find your way back.", None); + } else { + empty_state(ui, icons::SEARCH, "Nothing matches", &format!("No visited page matches \"{}\". Try a different word or part of an address.", query.trim()), None); + } + return; } - let mut last_day = i64::MIN; let mut remove: Option<(String, u64)> = None; let mut remove_host: Option = None; - for visit in &visits { - let day = util::local_day(visit.time); - if day != last_day { - last_day = day; - ui.add_space(10.0); - ui.label(RichText::new(util::format_day_heading(visit.time)).strong()); - } - ui.horizontal(|ui| { - ui.label(RichText::new(util::format_clock(visit.time)).weak().monospace()); - let title = if visit.title.is_empty() { visit.url.clone() } else { visit.title.clone() }; - let r = ui.link(util::ellipsize(&title, 80)).on_hover_text(&visit.url); - if r.clicked() { - let new_tab = ui.input(|i| i.modifiers.command); - f.send(Command::Open { url: visit.url.clone(), how: if new_tab { Disposition::NewBackgroundTab } else { Disposition::Current }, from: None }); - } - if r.middle_clicked() { - f.send(Command::Open { url: visit.url.clone(), how: Disposition::NewBackgroundTab, from: None }); - } - ui.label(RichText::new(util::host_of_str(&visit.url)).weak().small()); - ui.with_layout(egui::Layout::right_to_left(egui::Align::Center), |ui| { - ui.menu_button(icons::MORE_VERT, |ui| { - if ui.button("Open in new tab").clicked() { - f.send(Command::Open { url: visit.url.clone(), how: Disposition::NewBackgroundTab, from: None }); - ui.close(); - } - if ui.button("Copy address").clicked() { - ui.ctx().copy_text(visit.url.clone()); - ui.close(); - } - if ui.button("Remove from history").clicked() { + let mut start = 0; + while start < visits.len() { + let day = util::local_day(visits[start].time); + let end = visits[start..].iter().position(|v| util::local_day(v.time) != day).map_or(visits.len(), |n| start + n); + section(ui, &util::format_day_heading(visits[start].time)); + card(ui, |ui| { + for visit in &visits[start..end] { + let title = if visit.title.is_empty() { visit.url.clone() } else { visit.title.clone() }; + let host = util::host_of_str(&visit.url); + let detail = if host.is_empty() { util::format_clock(visit.time) } else { format!("{} · {host}", util::format_clock(visit.time)) }; + let r = list_item(ui, (&visit.url, visit.time), Some(icons::LANGUAGE), &title, &detail, |ui| { + more_menu(ui, |ui| { + if menu_item(ui, Some(icons::OPEN_IN_NEW), "Open in new tab", "", false).clicked() { + f.send(Command::Open { url: visit.url.clone(), how: Disposition::NewBackgroundTab, from: None }); + ui.close(); + } + if menu_item(ui, Some(icons::CONTENT_COPY), "Copy address", "", false).clicked() { + ui.ctx().copy_text(visit.url.clone()); + ui.close(); + } + if menu_item(ui, Some(icons::BLOCK), "Remove everything from this site", "", false).clicked() { + remove_host = Some(util::host_of_str(&visit.url)); + ui.close(); + } + }); + if icon_button(ui, icons::CLOSE, "Remove from history").clicked() { remove = Some((visit.url.clone(), visit.time)); - ui.close(); - } - if ui.button("Remove everything from this site").clicked() { - remove_host = Some(util::host_of_str(&visit.url)); - ui.close(); } }); - }); + let r = r.on_hover_text(&visit.url); + if r.clicked() { + let new_tab = ui.input(|i| i.modifiers.command); + f.send(Command::Open { url: visit.url.clone(), how: if new_tab { Disposition::NewBackgroundTab } else { Disposition::Current }, from: None }); + } + if r.middle_clicked() { + f.send(Command::Open { url: visit.url.clone(), how: Disposition::NewBackgroundTab, from: None }); + } + } }); + start = end; } if let Some((url, time)) = remove { f.shared.history.borrow_mut().remove_visit(&url, time); @@ -80,15 +81,19 @@ pub fn draw(ui: &mut Ui, f: &mut Frame) { f.shared.history.borrow_mut().remove_host(&host); } // Recently closed tabs, for quick reopening. - let closed: Vec<(usize, String, String)> = f.shared.session.borrow().closed.iter().enumerate().take(10).map(|(i, t)| (i, t.title.clone(), t.url.clone())).collect(); if !closed.is_empty() && query.is_empty() { - ui.add_space(16.0); - ui.label(RichText::new("Recently closed tabs").strong()); - for (i, title, url) in closed { - if ui.link(util::ellipsize(if title.is_empty() { &url } else { &title }, 80)).clicked() { - f.send(Command::ReopenClosed(Some(i))); + section(ui, "Recently closed tabs"); + card(ui, |ui| { + for (i, title, url) in closed { + let name = if title.is_empty() { url.clone() } else { title }; + let r = list_item(ui, ("closed", i), Some(icons::TAB), &name, &util::host_of_str(&url), |ui| { + icon_button(ui, icons::UNDO, "Reopen"); + }); + if r.clicked() { + f.send(Command::ReopenClosed(Some(i))); + } } - } + }); } }); } diff --git a/src/ui/pages/passwords.rs b/src/ui/pages/passwords.rs index 17130fd..c6034a1 100644 --- a/src/ui/pages/passwords.rs +++ b/src/ui/pages/passwords.rs @@ -3,32 +3,32 @@ use egui::{RichText, Ui}; -use super::super::widgets::{card, page_column}; +use super::super::menus::menu_item; +use super::super::widgets::{ButtonKind, button, card, choice_chips, empty_state, icon_button, list_item, more_menu, number, page_column, page_header, password_field, row, search_pill, section, strength_meter, switch_row, text_field}; use super::super::Frame; -use crate::actions::Command; +use crate::actions::{Command, Disposition}; use crate::store::vault::{self, Address, Card, Login, Mode}; +use crate::theme::{self, Weight}; +use crate::ui::icons; use crate::util; -const TABS: &[&str] = &["Passwords", "Addresses", "Cards", "Generator", "Check", "Master password"]; +const TABS: &[(usize, &str)] = &[(0, "Passwords"), (1, "Addresses"), (2, "Cards"), (3, "Generator"), (4, "Check"), (5, "Master password")]; pub fn draw(ui: &mut Ui, f: &mut Frame) { page_column(ui, "passwords", 900.0, |ui| { - ui.heading("Passwords and autofill"); - if f.incognito() { - ui.label(RichText::new("Incognito windows have their own empty password store that is thrown away when they close.").weak()); - } + let subtitle = if f.incognito() { "Incognito windows have their own empty store that is thrown away when they close." } else { "" }; + page_header(ui, "Passwords and autofill", subtitle, |_| {}); if f.shared.vault.borrow().locked() { unlock(ui, f); return; } - ui.horizontal(|ui| { - for (i, name) in TABS.iter().enumerate() { - if ui.selectable_label(f.ui.pages.passwords_tab == i, *name).clicked() { - f.ui.pages.passwords_tab = i; - } + ui.horizontal_wrapped(|ui| { + ui.add_space(theme::GAP_S); + let mut tab = f.ui.pages.passwords_tab; + if choice_chips(ui, &mut tab, TABS) { + f.ui.pages.passwords_tab = tab; } }); - ui.separator(); match f.ui.pages.passwords_tab { 0 => logins(ui, f), 1 => addresses(ui, f), @@ -40,20 +40,41 @@ pub fn draw(ui: &mut Ui, f: &mut Frame) { }); } +/// The padding that lines a button row up with a card's rows. +fn pad(ui: &mut Ui) { + ui.add_space(theme::GAP_L - ui.spacing().item_spacing.x); +} + +fn error_line(ui: &mut Ui, f: &Frame, text: &str) { + ui.label(RichText::new(text).font(theme::font(theme::SMALL, Weight::Regular)).color(f.palette().error)); +} + fn unlock(ui: &mut Ui, f: &mut Frame) { - card(ui, |ui| { - ui.label(RichText::new("Your passwords are locked").strong()); - let r = ui.add(egui::TextEdit::singleline(&mut f.ui.pages.unlock).password(true).hint_text("Master password")); - let go = ui.button("Unlock").clicked() || (r.lost_focus() && ui.input(|i| i.key_pressed(egui::Key::Enter))); + let p = f.palette(); + ui.add_space(theme::GAP_XL); + ui.vertical_centered(|ui| { + let (rect, _) = ui.allocate_exact_size(egui::Vec2::splat(72.0), egui::Sense::hover()); + ui.painter().circle_filled(rect.center(), 36.0, p.secondary_container); + super::super::widgets::icon(ui, rect.center(), icons::LOCK, theme::ICON_LARGE, true, p.on_secondary_container); + ui.add_space(theme::GAP_L); + ui.label(RichText::new("Your passwords are locked").font(theme::font(theme::TITLE, Weight::Medium)).color(p.on_surface)); + ui.label(RichText::new("Enter your master password to see and fill them.").font(theme::font(theme::BODY, Weight::Regular)).color(p.on_surface_variant)); + ui.add_space(theme::GAP_L); + let r = password_field(ui, &mut f.ui.pages.unlock, "Master password", 280.0); + ui.add_space(theme::GAP_M); + let go = button(ui, ButtonKind::Filled, Some(icons::LOCK_OPEN), "Unlock").clicked() || (r.lost_focus() && ui.input(|i| i.key_pressed(egui::Key::Enter))); if go { let pw = std::mem::take(&mut f.ui.pages.unlock); match f.shared.vault.borrow_mut().unlock(&pw) { Ok(()) => f.ui.pages.unlock_error.clear(), - Err(e) => f.ui.pages.unlock_error = e.to_string(), + Err(_) => f.ui.pages.unlock_error = "That master password is not right. Try again.".into(), } + r.request_focus(); } if !f.ui.pages.unlock_error.is_empty() { - ui.label(RichText::new(&f.ui.pages.unlock_error).color(f.palette().error)); + ui.add_space(theme::GAP_S); + let text = f.ui.pages.unlock_error.clone(); + error_line(ui, f, &text); } }); } @@ -84,141 +105,173 @@ fn may_reveal(ui: &mut Ui, f: &mut Frame) -> bool { if !f.shared.vault.borrow().needs_reauth(reauth, minutes) { return true; } - ui.horizontal(|ui| { - ui.label("Enter the master password to see passwords:"); - let r = ui.add(egui::TextEdit::singleline(&mut f.ui.pages.reauth).password(true).desired_width(180.0)); - if ui.button("OK").clicked() || (r.lost_focus() && ui.input(|i| i.key_pressed(egui::Key::Enter))) { - let pw = std::mem::take(&mut f.ui.pages.reauth); - if f.shared.vault.borrow_mut().check_password(&pw) { - f.ui.pages.reauth_error.clear(); - } else { - f.ui.pages.reauth_error = "That password is not right.".into(); + card(ui, |ui| { + row(ui, "Enter the master password", "To see or copy passwords.", |ui| { + let r = password_field(ui, &mut f.ui.pages.reauth, "Master password", 200.0); + if button(ui, ButtonKind::Filled, None, "Continue").clicked() || (r.lost_focus() && ui.input(|i| i.key_pressed(egui::Key::Enter))) { + let pw = std::mem::take(&mut f.ui.pages.reauth); + if f.shared.vault.borrow_mut().check_password(&pw) { + f.ui.pages.reauth_error.clear(); + } else { + f.ui.pages.reauth_error = "That master password is not right.".into(); + } } + }); + if !f.ui.pages.reauth_error.is_empty() { + ui.horizontal(|ui| { + pad(ui); + let text = f.ui.pages.reauth_error.clone(); + error_line(ui, f, &text); + }); + ui.add_space(theme::GAP_S); } }); - if !f.ui.pages.reauth_error.is_empty() { - ui.label(RichText::new(&f.ui.pages.reauth_error).color(f.palette().error)); - } false } -fn logins(ui: &mut Ui, f: &mut Frame) { +/// Save and Cancel under an editor card's rows. `save` returns true when the editor should close. +fn editor_buttons(ui: &mut Ui, save: impl FnOnce() -> bool) -> Option { + let mut result = None; + ui.add_space(theme::GAP_S); ui.horizontal(|ui| { - ui.add(egui::TextEdit::singleline(&mut f.ui.pages.passwords_search).hint_text("Search passwords").desired_width(360.0)); - if ui.button("Add").clicked() { + pad(ui); + ui.spacing_mut().item_spacing.x = theme::GAP_S; + if button(ui, ButtonKind::Filled, None, "Save").clicked() { + result = Some(save()); + } else if button(ui, ButtonKind::Text, None, "Cancel").clicked() { + result = Some(true); + } + }); + ui.add_space(theme::GAP_S); + result +} + +fn logins(ui: &mut Ui, f: &mut Frame) { + ui.add_space(theme::GAP_S); + search_pill(ui, "passwords", &mut f.ui.pages.passwords_search, "Search passwords"); + ui.add_space(theme::GAP_M); + ui.horizontal_wrapped(|ui| { + ui.spacing_mut().item_spacing = egui::vec2(theme::GAP_S, theme::GAP_S); + ui.add_space(theme::GAP_S); + if button(ui, ButtonKind::Filled, Some(icons::ADD), "Add password").clicked() { f.ui.pages.login_edit = Some(Login::default()); } - if ui.button("Import CSV").clicked() { + if button(ui, ButtonKind::Outlined, Some(icons::UPLOAD), "Import CSV").clicked() { f.send(Command::ImportPasswords); } - if ui.button("Export CSV").clicked() { + if button(ui, ButtonKind::Outlined, Some(icons::DOWNLOAD), "Export CSV").clicked() { f.send(Command::ExportPasswords); } }); if let Some(mut edit) = f.ui.pages.login_edit.clone() { + section(ui, if edit.id == 0 { "New password" } else { "Edit password" }); + let mut finished = false; card(ui, |ui| { - ui.label(RichText::new(if edit.id == 0 { "New password" } else { "Edit password" }).strong()); - egui::Grid::new("login-edit").num_columns(2).show(ui, |ui| { - ui.label("Site"); - ui.add(egui::TextEdit::singleline(&mut edit.origin).hint_text("https://example.com").desired_width(360.0)); - ui.end_row(); - ui.label("User name"); - ui.add(egui::TextEdit::singleline(&mut edit.username).desired_width(360.0)); - ui.end_row(); - ui.label("Password"); - ui.horizontal(|ui| { - ui.add(egui::TextEdit::singleline(&mut edit.password).password(true).desired_width(280.0)); - if ui.small_button("Generate").clicked() { - let (len, sym) = { - let s = f.shared.settings.borrow(); - (s.vault.gen_length, s.vault.gen_symbols) - }; - edit.password = vault::generate(len as usize, sym); - } - }); - ui.end_row(); - ui.label("Note"); - ui.add(egui::TextEdit::singleline(&mut edit.note).desired_width(360.0)); - ui.end_row(); - }); - let (level, label) = vault::strength(&edit.password); - ui.add(egui::ProgressBar::new(level as f32 / 4.0).text(label).desired_width(360.0)); - ui.horizontal(|ui| { - if ui.button("Save").clicked() { - let result = if edit.id == 0 { - f.shared.vault.borrow_mut().upsert_login(&edit.origin, &edit.username, &edit.password, &edit.note).map(|_| ()) - } else { - edit.origin = vault::origin_of(&edit.origin); - f.shared.vault.borrow_mut().update_login(edit.clone()) + row(ui, "Site", "", |ui| text_field(ui, &mut edit.origin, "https://example.com", 300.0)); + row(ui, "User name", "", |ui| text_field(ui, &mut edit.username, "", 300.0)); + row(ui, "Password", "", |ui| { + if button(ui, ButtonKind::Text, None, "Generate").clicked() { + let (len, sym) = { + let s = f.shared.settings.borrow(); + (s.vault.gen_length, s.vault.gen_symbols) }; - match result { - Ok(()) => f.ui.pages.login_edit = None, - Err(e) => f.shared.toast(e.to_string()), - } - return; + edit.password = vault::generate(len as usize, sym); } - if ui.button("Cancel").clicked() { - f.ui.pages.login_edit = None; - return; - } - f.ui.pages.login_edit = Some(edit.clone()); + password_field(ui, &mut edit.password, "", 220.0); }); + row(ui, "Note", "", |ui| text_field(ui, &mut edit.note, "", 300.0)); + ui.horizontal(|ui| { + pad(ui); + let (level, label) = vault::strength(&edit.password); + strength_meter(ui, level, label); + }); + let saved = editor_buttons(ui, || { + let result = if edit.id == 0 { + f.shared.vault.borrow_mut().upsert_login(&edit.origin, &edit.username, &edit.password, &edit.note).map(|_| ()) + } else { + edit.origin = vault::origin_of(&edit.origin); + f.shared.vault.borrow_mut().update_login(edit.clone()) + }; + match result { + Ok(()) => true, + Err(e) => { + f.shared.toast(format!("Couldn't save the password: {e}")); + false + }, + } + }); + finished = saved == Some(true); }); + f.ui.pages.login_edit = if finished { None } else { Some(edit) }; } let query = f.ui.pages.passwords_search.to_lowercase(); let mut list: Vec = f.shared.vault.borrow().contents.logins.iter().filter(|l| query.is_empty() || l.origin.to_lowercase().contains(&query) || l.username.to_lowercase().contains(&query)).cloned().collect(); list.sort_by(|a, b| util::host_of_str(&a.origin).cmp(&util::host_of_str(&b.origin)).then(a.username.cmp(&b.username))); - if list.is_empty() { - ui.label(RichText::new("Passwords you save show up here.").weak()); + if list.is_empty() && f.ui.pages.login_edit.is_none() { + if query.is_empty() { + if empty_state(ui, icons::PASSWORD, "No saved passwords", "When you sign in to a site, Nacre offers to save the password here. You can also add one yourself or import a file.", Some((icons::ADD, "Add password"))) { + f.ui.pages.login_edit = Some(Login::default()); + } + } else { + empty_state(ui, icons::SEARCH, "Nothing matches", &format!("No saved password matches \"{}\".", query.trim()), None); + } } let can_reveal = f.ui.pages.revealed.is_empty() || may_reveal(ui, f); let mut delete = None; - egui::Grid::new("logins").num_columns(4).striped(true).show(ui, |ui| { - for login in &list { - ui.label(util::host_of_str(&login.origin)); - ui.label(if login.username.is_empty() { "—".into() } else { login.username.clone() }); - let shown = f.ui.pages.revealed.contains(&login.id) && can_reveal; - ui.monospace(if shown { login.password.clone() } else { "••••••••".into() }); - ui.horizontal(|ui| { - if ui.small_button(if shown { "Hide" } else { "Show" }).clicked() { - if shown { - f.ui.pages.revealed.remove(&login.id); - } else { - f.ui.pages.revealed.insert(login.id); + if !list.is_empty() { + section(ui, &format!("{} saved", list.len())); + card(ui, |ui| { + for login in &list { + let shown = f.ui.pages.revealed.contains(&login.id) && can_reveal; + let user = if login.username.is_empty() { "No user name".to_owned() } else { login.username.clone() }; + let detail = if shown { format!("{user} · {}", login.password) } else { user }; + let r = list_item(ui, login.id, Some(icons::LANGUAGE), &util::host_of_str(&login.origin), &detail, |ui| { + more_menu(ui, |ui| { + if menu_item(ui, Some(icons::EDIT), "Edit", "", false).clicked() { + f.ui.pages.login_edit = Some(login.clone()); + ui.close(); + } + if menu_item(ui, Some(icons::OPEN_IN_NEW), "Open site", "", false).clicked() { + f.send(Command::Open { url: login.origin.clone(), how: Disposition::NewForegroundTab, from: None }); + ui.close(); + } + if menu_item(ui, Some(icons::DELETE), "Delete", "", false).clicked() { + delete = Some(login.id); + ui.close(); + } + }); + if icon_button(ui, icons::CONTENT_COPY, "Copy password").clicked() { + let ok = !f.shared.vault.borrow().needs_reauth(f.shared.settings.borrow().vault.reauth, f.shared.settings.borrow().vault.reauth_min); + if ok { + copy_secret(f, &login.password); + } else { + f.ui.pages.revealed.insert(login.id); + } } - } - if ui.small_button("Copy").clicked() { - let ok = !f.shared.vault.borrow().needs_reauth(f.shared.settings.borrow().vault.reauth, f.shared.settings.borrow().vault.reauth_min); - if ok { - copy_secret(f, &login.password); - } else { - f.ui.pages.revealed.insert(login.id); + if icon_button(ui, if shown { icons::VISIBILITY_OFF } else { icons::VISIBILITY }, if shown { "Hide password" } else { "Show password" }).clicked() { + if shown { + f.ui.pages.revealed.remove(&login.id); + } else { + f.ui.pages.revealed.insert(login.id); + } } - } - if ui.small_button("Edit").clicked() { + }); + if r.clicked() { f.ui.pages.login_edit = Some(login.clone()); } - if ui.small_button("Delete").clicked() { - delete = Some(login.id); - } - if ui.small_button("Open").clicked() { - f.send(Command::Open { url: login.origin.clone(), how: crate::actions::Disposition::NewForegroundTab, from: None }); - } - }); - ui.end_row(); - } - }); + } + }); + } if let Some(id) = delete { let _ = f.shared.vault.borrow_mut().delete_login(id); } let never = f.shared.vault.borrow().contents.never.clone(); if !never.is_empty() { - ui.add_space(12.0); - ui.collapsing(format!("Never saved for {} sites", never.len()), |ui| { + section(ui, &format!("Never saved for {} {}", never.len(), if never.len() == 1 { "site" } else { "sites" })); + card(ui, |ui| { for origin in never { - ui.horizontal(|ui| { - ui.label(&origin); - if ui.small_button("Remove").clicked() { + list_item(ui, ("never", &origin), Some(icons::BLOCK), &util::host_of_str(&origin), "Nacre doesn't offer to save passwords here", |ui| { + if icon_button(ui, icons::CLOSE, "Offer to save again").clicked() { let _ = f.shared.vault.borrow_mut().remove_never(&origin); } }); @@ -228,124 +281,159 @@ fn logins(ui: &mut Ui, f: &mut Frame) { } fn addresses(ui: &mut Ui, f: &mut Frame) { - if ui.button("Add address").clicked() { - f.ui.pages.address_edit = Some(Address::default()); - } + ui.add_space(theme::GAP_S); + ui.horizontal(|ui| { + ui.add_space(theme::GAP_S); + if button(ui, ButtonKind::Filled, Some(icons::ADD), "Add address").clicked() { + f.ui.pages.address_edit = Some(Address::default()); + } + }); if let Some(mut a) = f.ui.pages.address_edit.clone() { + section(ui, if a.id == 0 { "New address" } else { "Edit address" }); + let mut finished = false; card(ui, |ui| { - egui::Grid::new("address-edit").num_columns(2).show(ui, |ui| { - for (label, value) in [("Name", &mut a.name), ("Organization", &mut a.organization), ("Street", &mut a.street), ("City", &mut a.city), ("Region", &mut a.region), ("Postal code", &mut a.postal_code), ("Country", &mut a.country), ("Phone", &mut a.phone), ("Email", &mut a.email)] { - ui.label(label); - ui.add(egui::TextEdit::singleline(value).desired_width(340.0)); - ui.end_row(); - } - }); - ui.horizontal(|ui| { - if ui.button("Save").clicked() { - let _ = f.shared.vault.borrow_mut().upsert_address(a.clone()); - f.ui.pages.address_edit = None; - return; - } - if ui.button("Cancel").clicked() { - f.ui.pages.address_edit = None; - return; - } - f.ui.pages.address_edit = Some(a.clone()); - }); + row(ui, "Name", "", |ui| text_field(ui, &mut a.name, "", 300.0)); + row(ui, "Organization", "", |ui| text_field(ui, &mut a.organization, "", 300.0)); + row(ui, "Street", "", |ui| text_field(ui, &mut a.street, "", 300.0)); + row(ui, "City", "", |ui| text_field(ui, &mut a.city, "", 300.0)); + row(ui, "Region", "", |ui| text_field(ui, &mut a.region, "", 300.0)); + row(ui, "Postal code", "", |ui| text_field(ui, &mut a.postal_code, "", 300.0)); + row(ui, "Country", "", |ui| text_field(ui, &mut a.country, "", 300.0)); + row(ui, "Phone", "", |ui| text_field(ui, &mut a.phone, "", 300.0)); + row(ui, "Email", "", |ui| text_field(ui, &mut a.email, "", 300.0)); + finished = editor_buttons(ui, || { + let _ = f.shared.vault.borrow_mut().upsert_address(a.clone()); + true + }) == Some(true); }); + f.ui.pages.address_edit = if finished { None } else { Some(a) }; } let list = f.shared.vault.borrow().contents.addresses.clone(); - if list.is_empty() { - ui.label(RichText::new("Addresses you save are offered when a form asks for one.").weak()); + if list.is_empty() && f.ui.pages.address_edit.is_none() { + if empty_state(ui, icons::PIN_DROP, "No saved addresses", "Addresses you save are offered when a form asks for one.", Some((icons::ADD, "Add address"))) { + f.ui.pages.address_edit = Some(Address::default()); + } + return; } - for a in list { - ui.horizontal(|ui| { - ui.label(format!("{} · {}, {} {}", a.name, a.street, a.postal_code, a.city)); - if ui.small_button("Edit").clicked() { + if list.is_empty() { + return; + } + section(ui, &format!("{} saved", list.len())); + let mut delete = None; + card(ui, |ui| { + for a in &list { + let name = if a.name.is_empty() { a.street.clone() } else { a.name.clone() }; + let place = [a.street.as_str(), a.postal_code.as_str(), a.city.as_str()].iter().filter(|s| !s.is_empty()).copied().collect::>().join(", "); + let r = list_item(ui, a.id, Some(icons::PIN_DROP), &name, &place, |ui| { + if icon_button(ui, icons::DELETE, "Delete address").clicked() { + delete = Some(a.id); + } + }); + if r.clicked() { f.ui.pages.address_edit = Some(a.clone()); } - if ui.small_button("Delete").clicked() { - let _ = f.shared.vault.borrow_mut().delete_address(a.id); - } - }); + } + }); + if let Some(id) = delete { + let _ = f.shared.vault.borrow_mut().delete_address(id); } } fn cards(ui: &mut Ui, f: &mut Frame) { - if ui.button("Add card").clicked() { - f.ui.pages.card_edit = Some(Card::default()); - } + ui.add_space(theme::GAP_S); + ui.horizontal(|ui| { + ui.add_space(theme::GAP_S); + if button(ui, ButtonKind::Filled, Some(icons::ADD), "Add card").clicked() { + f.ui.pages.card_edit = Some(Card::default()); + } + }); if let Some(mut c) = f.ui.pages.card_edit.clone() { + section(ui, if c.id == 0 { "New card" } else { "Edit card" }); + let mut finished = false; card(ui, |ui| { - egui::Grid::new("card-edit").num_columns(2).show(ui, |ui| { - ui.label("Name on card"); - ui.add(egui::TextEdit::singleline(&mut c.name).desired_width(300.0)); - ui.end_row(); - ui.label("Number"); - ui.add(egui::TextEdit::singleline(&mut c.number).desired_width(300.0)); - ui.end_row(); - ui.label("Expires"); - ui.horizontal(|ui| { - ui.add(egui::DragValue::new(&mut c.exp_month).range(1..=12)); - ui.label("/"); - ui.add(egui::DragValue::new(&mut c.exp_year).range(2000..=2100)); - }); - ui.end_row(); - ui.label("Nickname"); - ui.add(egui::TextEdit::singleline(&mut c.nickname).desired_width(300.0)); - ui.end_row(); - }); - ui.label(RichText::new("The security code is never saved.").weak().small()); + row(ui, "Name on card", "", |ui| text_field(ui, &mut c.name, "", 300.0)); + row(ui, "Number", "", |ui| text_field(ui, &mut c.number, "", 300.0)); + row(ui, "Expires: month", "", |ui| number(ui, &mut c.exp_month, 1..=12, "")); + row(ui, "Expires: year", "", |ui| number(ui, &mut c.exp_year, 2000..=2100, "")); + row(ui, "Nickname", "Shown in place of the name.", |ui| text_field(ui, &mut c.nickname, "", 300.0)); ui.horizontal(|ui| { - if ui.button("Save").clicked() { - let _ = f.shared.vault.borrow_mut().upsert_card(c.clone()); - f.ui.pages.card_edit = None; - return; - } - if ui.button("Cancel").clicked() { - f.ui.pages.card_edit = None; - return; - } - f.ui.pages.card_edit = Some(c.clone()); + pad(ui); + ui.label(RichText::new("The security code is never saved.").font(theme::font(theme::SMALL, Weight::Regular)).color(f.palette().on_surface_variant)); }); + finished = editor_buttons(ui, || { + let _ = f.shared.vault.borrow_mut().upsert_card(c.clone()); + true + }) == Some(true); }); + f.ui.pages.card_edit = if finished { None } else { Some(c) }; } let list = f.shared.vault.borrow().contents.cards.clone(); - if list.is_empty() { - ui.label(RichText::new("Cards you save are offered when a payment form asks for one.").weak()); + if list.is_empty() && f.ui.pages.card_edit.is_none() { + if empty_state(ui, icons::CREDIT_CARD, "No saved cards", "Cards you save are offered when a payment form asks for one. The security code is never kept.", Some((icons::ADD, "Add card"))) { + f.ui.pages.card_edit = Some(Card::default()); + } + return; } - for c in list { - ui.horizontal(|ui| { - ui.label(format!("{} {} · {:02}/{}", if c.nickname.is_empty() { &c.name } else { &c.nickname }, c.masked(), c.exp_month, c.exp_year)); - if ui.small_button("Edit").clicked() { + if list.is_empty() { + return; + } + section(ui, &format!("{} saved", list.len())); + let mut delete = None; + card(ui, |ui| { + for c in &list { + let name = if c.nickname.is_empty() { c.name.clone() } else { c.nickname.clone() }; + let detail = format!("{} · expires {:02}/{}", c.masked(), c.exp_month, c.exp_year); + let r = list_item(ui, c.id, Some(icons::CREDIT_CARD), &name, &detail, |ui| { + if icon_button(ui, icons::DELETE, "Delete card").clicked() { + delete = Some(c.id); + } + }); + if r.clicked() { f.ui.pages.card_edit = Some(c.clone()); } - if ui.small_button("Delete").clicked() { - let _ = f.shared.vault.borrow_mut().delete_card(c.id); - } - }); + } + }); + if let Some(id) = delete { + let _ = f.shared.vault.borrow_mut().delete_card(id); } } fn generator(ui: &mut Ui, f: &mut Frame) { + let p = f.palette(); let mut s = f.shared.settings.borrow().vault.clone(); + section(ui, "Generator"); card(ui, |ui| { - ui.checkbox(&mut f.ui.pages.gen_passphrase, "Passphrase (words) instead of characters"); + switch_row(ui, "Passphrase", "Words joined with dashes instead of random characters.", &mut f.ui.pages.gen_passphrase); if !f.ui.pages.gen_passphrase { - ui.add(egui::Slider::new(&mut s.gen_length, 8..=64).text("characters")); - ui.checkbox(&mut s.gen_symbols, "Symbols"); - } - if ui.button("Generate").clicked() || f.ui.pages.generated.is_empty() { - f.ui.pages.generated = if f.ui.pages.gen_passphrase { vault::passphrase(5, "-") } else { vault::generate(s.gen_length as usize, s.gen_symbols) }; + row(ui, "Length", "Characters in the password.", |ui| number(ui, &mut s.gen_length, 8..=64, "")); + switch_row(ui, "Symbols", "Include punctuation.", &mut s.gen_symbols); } + ui.add_space(theme::GAP_S); ui.horizontal(|ui| { - ui.monospace(RichText::new(&f.ui.pages.generated).size(16.0)); - if ui.button("Copy").clicked() { + pad(ui); + let (rect, _) = ui.allocate_exact_size(egui::vec2(ui.available_width() - theme::GAP_L, 56.0), egui::Sense::hover()); + ui.painter().rect_filled(rect, theme::corner_radius(theme::radius(ui.ctx(), theme::M)), p.surface_container_highest); + let galley = super::super::menus::one_line(ui, &f.ui.pages.generated, egui::FontId::monospace(18.0), p.on_surface, rect.width() - 2.0 * theme::GAP_L); + ui.painter().galley(egui::pos2(rect.left() + theme::GAP_L, rect.center().y - galley.size().y / 2.0), galley, p.on_surface); + }); + ui.add_space(theme::GAP_S); + ui.horizontal(|ui| { + pad(ui); + let (level, label) = vault::strength(&f.ui.pages.generated); + strength_meter(ui, level, label); + }); + ui.add_space(theme::GAP_S); + ui.horizontal(|ui| { + pad(ui); + ui.spacing_mut().item_spacing.x = theme::GAP_S; + if button(ui, ButtonKind::Filled, Some(icons::REFRESH), "Generate").clicked() || f.ui.pages.generated.is_empty() { + f.ui.pages.generated = if f.ui.pages.gen_passphrase { vault::passphrase(5, "-") } else { vault::generate(s.gen_length as usize, s.gen_symbols) }; + } + if button(ui, ButtonKind::Tonal, Some(icons::CONTENT_COPY), "Copy").clicked() { copy_secret(f, &f.ui.pages.generated.clone()); } }); - let (level, label) = vault::strength(&f.ui.pages.generated); - ui.add(egui::ProgressBar::new(level as f32 / 4.0).text(label)); + ui.add_space(theme::GAP_S); }); if s != f.shared.settings.borrow().vault { f.shared.settings.borrow_mut().vault = s; @@ -355,68 +443,92 @@ fn generator(ui: &mut Ui, f: &mut Frame) { fn check(ui: &mut Ui, f: &mut Frame) { let issues = f.shared.vault.borrow().weak_or_reused(); if issues.is_empty() { - ui.label("No weak or reused passwords found."); + empty_state(ui, icons::VERIFIED_USER, "No weak or reused passwords", "Every saved password is different and hard enough to guess.", None); return; } - ui.label(format!("{} passwords could be stronger", issues.len())); let logins = f.shared.vault.borrow().contents.logins.clone(); - for (id, reason) in issues { - if let Some(login) = logins.iter().find(|l| l.id == id) { - ui.horizontal(|ui| { - ui.label(format!("{} · {}", util::host_of_str(&login.origin), if login.username.is_empty() { "—" } else { &login.username })); - ui.label(RichText::new(reason).color(f.palette().warning)); - if ui.small_button("Change").clicked() { - f.send(Command::Open { url: login.origin.clone(), how: crate::actions::Disposition::NewForegroundTab, from: None }); - } - if ui.small_button("Edit").clicked() { - f.ui.pages.login_edit = Some(login.clone()); - f.ui.pages.passwords_tab = 0; + section(ui, &format!("{} {} could be stronger", issues.len(), if issues.len() == 1 { "password" } else { "passwords" })); + card(ui, |ui| { + for (id, reason) in issues { + let Some(login) = logins.iter().find(|l| l.id == id) else { continue }; + let user = if login.username.is_empty() { "No user name" } else { &login.username }; + let r = list_item(ui, ("check", id), Some(icons::WARNING), &util::host_of_str(&login.origin), &format!("{reason} · {user}"), |ui| { + if icon_button(ui, icons::OPEN_IN_NEW, "Open the site to change it").clicked() { + f.send(Command::Open { url: login.origin.clone(), how: Disposition::NewForegroundTab, from: None }); } }); + if r.clicked() { + f.ui.pages.login_edit = Some(login.clone()); + f.ui.pages.passwords_tab = 0; + } } - } + }); } fn master(ui: &mut Ui, f: &mut Frame) { + let p = f.palette(); let mode = f.shared.vault.borrow().mode(); + let intro = match mode { + Mode::Device => "Your passwords are encrypted with a key kept in your profile folder. A master password also protects them from anyone who can read your files.", + Mode::Master => "A master password protects your passwords. Nacre asks for it after it locks.", + }; + section(ui, "Master password"); card(ui, |ui| { - match mode { - Mode::Device => { - ui.label("Your passwords are encrypted with a key kept in your profile folder. A master password also protects them from anyone who can read your files."); - }, - Mode::Master => { - ui.label("A master password protects your passwords. Nacre asks for it after it locks."); - }, - } - let (a, b) = &mut f.ui.pages.master; - ui.add(egui::TextEdit::singleline(a).password(true).hint_text("New master password")); - ui.add(egui::TextEdit::singleline(b).password(true).hint_text("Type it again")); - let ok = !a.is_empty() && a == b; - let level = vault::strength(a).0; - if !a.is_empty() && level < 2 { - ui.label(RichText::new("That master password is easy to guess.").color(f.palette().warning)); - } ui.horizontal(|ui| { - if ui.add_enabled(ok, egui::Button::new(if mode == Mode::Master { "Change master password" } else { "Set master password" })).clicked() { + pad(ui); + ui.add_space(theme::GAP_S); + ui.scope(|ui| { + ui.set_max_width(ui.available_width() - theme::GAP_L); + ui.label(RichText::new(intro).font(theme::font(theme::BODY, Weight::Regular)).color(p.on_surface_variant)); + }); + }); + ui.add_space(theme::GAP_S); + let (a, b) = &mut f.ui.pages.master; + row(ui, "New master password", "", |ui| password_field(ui, a, "", 260.0)); + row(ui, "Type it again", "", |ui| password_field(ui, b, "", 260.0)); + let ok = !a.is_empty() && a == b; + let (level, label) = vault::strength(a); + let mismatch = !b.is_empty() && a != b; + ui.horizontal(|ui| { + pad(ui); + if mismatch { + ui.label(RichText::new("The two passwords are different.").font(theme::font(theme::SMALL, Weight::Regular)).color(p.error)); + } else if !a.is_empty() && level < 2 { + ui.label(RichText::new("That master password is easy to guess.").font(theme::font(theme::SMALL, Weight::Regular)).color(p.warning)); + } else if !a.is_empty() { + strength_meter(ui, level, label); + } + }); + ui.add_space(theme::GAP_S); + ui.horizontal_wrapped(|ui| { + pad(ui); + ui.spacing_mut().item_spacing = egui::vec2(theme::GAP_S, theme::GAP_S); + let set = ui.add_enabled_ui(ok, |ui| button(ui, ButtonKind::Filled, None, if mode == Mode::Master { "Change master password" } else { "Set master password" })).inner; + if set.clicked() { let pw = std::mem::take(&mut f.ui.pages.master.0); f.ui.pages.master.1.clear(); match f.shared.vault.borrow_mut().set_master(&pw) { Ok(()) => f.shared.toast("Master password set"), - Err(e) => f.shared.toast(e.to_string()), + Err(e) => f.shared.toast(format!("Couldn't set the master password: {e}")), } } if mode == Mode::Master { - if ui.button("Remove master password").clicked() { + if button(ui, ButtonKind::Outlined, Some(icons::LOCK), "Lock now").clicked() { + f.shared.vault.borrow_mut().lock(); + } + if button(ui, ButtonKind::Text, None, "Remove master password").clicked() { match f.shared.vault.borrow_mut().remove_master() { Ok(()) => f.shared.toast("Master password removed"), - Err(e) => f.shared.toast(e.to_string()), + Err(e) => f.shared.toast(format!("Couldn't remove the master password: {e}")), } } - if ui.button("Lock now").clicked() { - f.shared.vault.borrow_mut().lock(); - } } }); - ui.label(RichText::new("If you forget the master password, the saved passwords can't be recovered.").weak().small()); + ui.add_space(theme::GAP_S); + ui.horizontal(|ui| { + pad(ui); + ui.label(RichText::new(format!("{} If you forget the master password, the saved passwords can't be recovered.", icons::WARNING)).font(theme::font(theme::SMALL, Weight::Regular)).color(p.on_surface_variant)); + }); + ui.add_space(theme::GAP_S); }); } diff --git a/src/ui/widgets.rs b/src/ui/widgets.rs index 29e9d33..38e4970 100644 --- a/src/ui/widgets.rs +++ b/src/ui/widgets.rs @@ -806,3 +806,196 @@ mod tests { assert_eq!(hover(&ctx), 0.0); } } + +/// A page title (Headline) with an optional line under it and the page's actions beside it, or under it when the +/// page is narrow. Lines up with the rows' text. +pub fn page_header(ui: &mut Ui, title: &str, subtitle: &str, actions: impl FnOnce(&mut Ui)) { + let p = theme::palette(ui.ctx()); + let wide = ui.available_width() >= 640.0; + let text = |ui: &mut Ui| { + ui.vertical(|ui| { + ui.label(RichText::new(title).font(theme::font(theme::HEADLINE, Weight::Medium)).color(p.on_surface)); + if !subtitle.is_empty() { + ui.label(RichText::new(subtitle).font(theme::font(theme::BODY, Weight::Regular)).color(p.on_surface_variant)); + } + }); + }; + ui.add_space(theme::GAP_S); + if wide { + ui.horizontal(|ui| { + ui.add_space(theme::GAP_L - ui.spacing().item_spacing.x); + text(ui); + ui.with_layout(egui::Layout::right_to_left(egui::Align::Center), |ui| { + ui.spacing_mut().item_spacing.x = theme::GAP_S; + actions(ui); + }); + }); + } else { + ui.horizontal(|ui| { + ui.add_space(theme::GAP_L - ui.spacing().item_spacing.x); + text(ui); + }); + ui.add_space(theme::GAP_S); + ui.horizontal_wrapped(|ui| { + ui.spacing_mut().item_spacing = vec2(theme::GAP_S, theme::GAP_S); + ui.add_space(theme::GAP_S); + actions(ui); + }); + } + ui.add_space(theme::GAP_M); +} + +/// A search pill: icon, field, and a clear button once something is typed. Escape clears it. Returns true when +/// the text changed. +pub fn search_pill(ui: &mut Ui, id: &str, text: &mut String, hint: &str) -> bool { + let p = theme::palette(ui.ctx()); + let (rect, _) = ui.allocate_exact_size(vec2(ui.available_width(), theme::CONTROL), Sense::hover()); + let id = Id::new(("search-pill", id)); + let focused = ui.memory(|m| m.has_focus(id)); + let ring = theme::effect(ui.ctx(), id.with("ring"), focused); + let radius = theme::corner_radius(theme::FULL); + ui.painter().rect_filled(rect, radius, p.surface_container_high); + if ring > 0.0 { + ui.painter().rect_stroke(rect, radius, Stroke::new(2.0, p.primary.gamma_multiply(ring)), StrokeKind::Inside); + } + icon(ui, pos2(rect.left() + theme::GAP_L + theme::ICON / 2.0, rect.center().y), icons::SEARCH, theme::ICON, false, p.on_surface_variant); + let before = text.clone(); + let clear = !text.is_empty(); + let field = Rect::from_min_max(pos2(rect.left() + theme::GAP_L + theme::ICON + theme::GAP_M, rect.top()), pos2(rect.right() - if clear { 40.0 } else { theme::GAP_L }, rect.bottom())); + let mut child = ui.new_child(egui::UiBuilder::new().max_rect(field).layout(egui::Layout::left_to_right(egui::Align::Center))); + let edit = egui::TextEdit::singleline(text).id(id).frame(egui::Frame::NONE).hint_text(hint).font(theme::font(theme::BODY, Weight::Regular)).desired_width(field.width()); + let response = child.add(edit); + if response.has_focus() && ui.input(|i| i.key_pressed(egui::Key::Escape)) { + text.clear(); + } + if clear { + let mut x = ui.new_child(egui::UiBuilder::new().max_rect(Rect::from_center_size(pos2(rect.right() - 24.0, rect.center().y), Vec2::splat(theme::CONTROL_SMALL)))); + if icon_button(&mut x, icons::CLOSE, "Clear search").clicked() { + text.clear(); + } + } + *text != before +} + +/// What a page shows when there is nothing to list: a round icon, what will appear and how, and optionally the +/// button that gets something there. Returns true when that button was clicked. +pub fn empty_state(ui: &mut Ui, glyph: &str, title: &str, body: &str, action: Option<(&str, &str)>) -> bool { + let p = theme::palette(ui.ctx()); + let mut clicked = false; + ui.add_space(theme::GAP_XL * 2.0); + ui.vertical_centered(|ui| { + let (rect, _) = ui.allocate_exact_size(Vec2::splat(72.0), Sense::hover()); + ui.painter().circle_filled(rect.center(), 36.0, p.secondary_container); + icon(ui, rect.center(), glyph, theme::ICON_LARGE, false, p.on_secondary_container); + ui.add_space(theme::GAP_L); + ui.label(RichText::new(title).font(theme::font(theme::TITLE, Weight::Medium)).color(p.on_surface)); + ui.add_space(theme::GAP_XS); + ui.scope(|ui| { + ui.set_max_width(360.0); + ui.label(RichText::new(body).font(theme::font(theme::BODY, Weight::Regular)).color(p.on_surface_variant)); + }); + if let Some((glyph, label)) = action { + ui.add_space(theme::GAP_L); + clicked = button(ui, ButtonKind::Tonal, Some(glyph), label).clicked(); + } + }); + clicked +} + +/// A page that is still loading or failed to load: a message in the page's own style, with a retry when there is one. +pub fn status_state(ui: &mut Ui, glyph: &str, title: &str, body: &str, error: bool) { + let p = theme::palette(ui.ctx()); + ui.add_space(theme::GAP_XL * 2.0); + ui.vertical_centered(|ui| { + let (rect, _) = ui.allocate_exact_size(Vec2::splat(72.0), Sense::hover()); + let (fill, content) = if error { (p.error_container, p.on_error_container) } else { (p.surface_container_high, p.on_surface_variant) }; + ui.painter().circle_filled(rect.center(), 36.0, fill); + icon(ui, rect.center(), glyph, theme::ICON_LARGE, false, content); + ui.add_space(theme::GAP_L); + ui.label(RichText::new(title).font(theme::font(theme::TITLE, Weight::Medium)).color(p.on_surface)); + ui.add_space(theme::GAP_XS); + ui.scope(|ui| { + ui.set_max_width(360.0); + ui.label(RichText::new(body).font(theme::font(theme::BODY, Weight::Regular)).color(p.on_surface_variant)); + }); + }); +} + +/// One entry of a list inside a card: a round icon (or `None`), a title and a detail line cut with an ellipsis, and +/// whatever `trailing` adds at the right end (icon buttons). The whole row answers hover and press and the returned +/// response is its click. `id` must be unique on the page. +pub fn list_item(ui: &mut Ui, id: impl std::hash::Hash + std::fmt::Debug, glyph: Option<&str>, title: &str, detail: &str, trailing: impl FnOnce(&mut Ui)) -> Response { + let p = theme::palette(ui.ctx()); + let first = ui.data_mut(|d| d.remove_temp::(first_row_id(ui))).unwrap_or(false); + let (rect, _) = ui.allocate_exact_size(vec2(ui.available_width(), theme::ROW), Sense::hover()); + let response = ui.interact(rect, ui.id().with(("list-item", id)), Sense::click()); + let s = interaction(ui, &response); + let hit = rect.shrink2(vec2(theme::GAP_XS, 2.0)); + let radius = theme::radius(ui.ctx(), theme::M); + ui.painter().rect_filled(hit, theme::corner_radius(radius), p.on_surface.gamma_multiply(s.amount())); + if !first { + ui.painter().hline(rect.x_range().shrink(theme::GAP_L), rect.top(), Stroke::new(1.0, p.outline_variant)); + } + if s.focus { + focus_ring(ui, hit, radius, &p); + } + let mut end = ui.new_child(egui::UiBuilder::new().max_rect(rect.shrink2(vec2(theme::GAP_S, 0.0))).layout(egui::Layout::right_to_left(egui::Align::Center))); + end.spacing_mut().item_spacing.x = theme::GAP_XS; + trailing(&mut end); + let right = end.min_rect().left().min(rect.right() - theme::GAP_S) - theme::GAP_S; + let mut left = rect.left() + theme::GAP_L; + if let Some(glyph) = glyph { + let c = pos2(left + theme::CONTROL / 2.0, rect.center().y); + ui.painter().circle_filled(c, theme::CONTROL / 2.0, p.surface_container_highest); + icon(ui, c, glyph, theme::ICON, false, p.on_surface_variant); + left += theme::CONTROL + theme::GAP_M; + } + let width = (right - left).max(40.0); + let title_galley = super::menus::one_line(ui, title, theme::font(theme::BODY, Weight::Medium), p.on_surface, width); + let detail_galley = (!detail.is_empty()).then(|| super::menus::one_line(ui, detail, theme::font(theme::SMALL, Weight::Regular), p.on_surface_variant, width)); + let height = title_galley.size().y + detail_galley.as_ref().map_or(0.0, |g| g.size().y + 2.0); + let mut y = rect.center().y - height / 2.0; + let title_height = title_galley.size().y; + ui.painter().galley(pos2(left, y), title_galley, p.on_surface); + y += title_height + 2.0; + if let Some(g) = detail_galley { + ui.painter().galley(pos2(left, y), g, p.on_surface_variant); + } + response +} + +/// A "more" button that opens a menu of `menus::menu_item`s. Close it with `ui.close()` after a choice. +pub fn more_menu(ui: &mut Ui, contents: impl FnOnce(&mut Ui)) { + let p = theme::palette(ui.ctx()); + let r = icon_button(ui, icons::MORE_VERT, "More"); + egui::Popup::menu(&r).align(egui::RectAlign::BOTTOM_END).gap(theme::GAP_XS).frame(super::menus::surface(&p, ui.ctx())).show(|ui| { + ui.spacing_mut().item_spacing.y = 2.0; + ui.set_min_width(220.0); + contents(ui); + }); +} + +/// A text field for secrets: dots instead of letters. +pub fn password_field(ui: &mut Ui, text: &mut String, hint: &str, width: f32) -> Response { + ui.add(egui::TextEdit::singleline(text).password(true).hint_text(hint).desired_width(width).margin(vec2(theme::GAP_M, theme::GAP_S))) +} + +/// Password strength as four segments (`level` 0..=4, from `vault::strength`) and its word. +pub fn strength_meter(ui: &mut Ui, level: u8, label: &str) { + let p = theme::palette(ui.ctx()); + let color = match level { + 0..=1 => p.error, + 2 => p.warning, + _ => p.success, + }; + ui.horizontal(|ui| { + ui.spacing_mut().item_spacing.x = theme::GAP_XS; + for i in 0..4u8 { + let (rect, _) = ui.allocate_exact_size(vec2(36.0, 6.0), Sense::hover()); + let on = theme::effect(ui.ctx(), ui.id().with(("strength", i)), i < level); + ui.painter().rect_filled(rect, theme::corner_radius(theme::FULL), theme::layer(p.surface_container_highest, color, on)); + } + ui.add_space(theme::GAP_S); + ui.label(RichText::new(label).font(theme::font(theme::SMALL, Weight::Medium)).color(p.on_surface_variant)); + }); +} diff --git a/tests/smoke/pages-ui.txt b/tests/smoke/pages-ui.txt new file mode 100644 index 0000000..30b1899 --- /dev/null +++ b/tests/smoke/pages-ui.txt @@ -0,0 +1,33 @@ +# Nacre's own pages (S020): history, bookmarks, downloads, passwords, apps, devtools, task manager, about, clear data, +# site settings, source, error, welcome. Empty first, then with a few visits and a bookmark; dark and light. Offline. +# Run with SMOKE_VISIBLE=1 SMOKE_SIZE=1280x900 tools/smoke.sh tests/smoke/pages-ui.txt; read the PNGs. +wait 3 +resize 1280 900 +set /theme/mode "dark" +page nacre://history +wait 0.6 +shot history-empty-dark +page nacre://bookmarks +wait 0.6 +shot bookmarks-empty-dark +open @PAGES@/hello.html +waitload +open @PAGES@/color.html +waitload +cmd bookmark +wait 0.5 +page nacre://history +wait 0.6 +shot history-dark +page nacre://bookmarks +wait 0.6 +shot bookmarks-dark +set /theme/mode "light" +page nacre://history +wait 0.6 +shot history-light +page nacre://bookmarks +wait 0.6 +shot bookmarks-light +status done +quit