Nacre/src/ui/mod.rs
astar76 11713adaf3 S016: R1-15/17/18 one protection flyout, QR sized to content, flyout polish
The shield flyout holds the ad blocker, privacy mode and the VPN as separate
groups; the VPN toolbar button only shows its state and opens it. Flyout
action rows no longer wrap, which stretched the QR flyout to the window's
bottom. Panel flyouts get the original's look: title with icon actions,
accent group labels, two-line entries and designed empty states.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-11-12 07:28:48 -06:00

315 lines
12 KiB
Rust

//! The browser interface, drawn with egui from the theme tokens (docs/DESIGN.md "UI system"). Drawing code reads
//! the shared stores, may edit simple things (settings, bookmarks, history) directly, and asks the app for
//! everything else with `Command`s.
pub mod chrome;
pub mod dialogs;
pub mod icons;
pub mod menus;
pub mod paint;
pub mod pages;
pub mod widgets;
use std::rc::Rc;
use egui::{Align2, Color32, RichText};
use winit::window::WindowId;
use crate::actions::Command;
use crate::app::Shared;
use crate::hub::Hub;
use crate::tabs::Tab;
use crate::theme::Palette;
use crate::window::Kind;
/// Small windows that open from the toolbar and menus.
#[derive(Clone, Debug, PartialEq)]
pub enum Flyout {
Main,
Downloads,
Shield,
Media,
SiteInfo,
Profiles,
EditBookmark(String),
Qr(String),
TabMenu(u64, egui::Pos2),
BookmarkMenu(u64, egui::Pos2),
BackHistory(egui::Pos2),
Apps,
}
static OUTFIT: &[u8] = include_bytes!("../../assets/fonts/Outfit[wght].ttf");
static SYMBOLS: &[u8] = include_bytes!("../../assets/fonts/MaterialSymbolsOutlined-Nacre.ttf");
/// The interface fonts: Outfit at each `theme::Weight` (one variable font), Material Symbols for `icons`, then
/// egui's own fonts for scripts Outfit lacks (Cyrillic, Greek). Set once per window.
pub fn fonts() -> egui::FontDefinitions {
use egui::{FontData, FontFamily, FontTweak};
use std::sync::Arc;
let mut fonts = egui::FontDefinitions::default();
let fallback: Vec<String> = fonts.families.get(&FontFamily::Proportional).cloned().unwrap_or_default();
let variable = |bytes: &'static [u8], coords: &[(&str, f32)]| Arc::new(FontData::from_static(bytes).tweak(FontTweak { coords: egui::epaint::text::VariationCoords::new(coords.iter().copied()), ..Default::default() }));
for weight in crate::theme::Weight::ALL {
let name = format!("outfit-{}", weight.wght());
fonts.font_data.insert(name.clone(), variable(OUTFIT, &[("wght", weight.wght())]));
let family = [name, "symbols".to_owned()].into_iter().chain(fallback.iter().cloned()).collect();
fonts.families.insert(weight.family(), family);
}
fonts.font_data.insert("symbols".to_owned(), variable(SYMBOLS, &[("FILL", 0.0)]));
fonts.font_data.insert("symbols-filled".to_owned(), variable(SYMBOLS, &[("FILL", 1.0)]));
fonts.families.insert(FontFamily::Name("icons".into()), vec!["symbols".to_owned()]);
fonts.families.insert(FontFamily::Name("icons-filled".into()), vec!["symbols-filled".to_owned()]);
if let Some(mono) = fonts.families.get_mut(&FontFamily::Monospace) {
mono.push("symbols".to_owned());
}
fonts
}
#[derive(Default)]
pub struct WindowUi {
pub palette: Option<Palette>,
/// The site colour `palette` was built from; the window restyles when the active tab's differs.
pub site_colour: Option<Color32>,
/// Address bar text while the user edits it (None = show the page's URL).
pub omnibox_edit: Option<String>,
pub focus_omnibox: bool,
pub omnibox_focused: bool,
pub suggestion_index: Option<usize>,
pub flyout: Option<Flyout>,
/// Where the last flyout button was, to place the flyout under it.
pub flyout_anchor: Option<egui::Rect>,
pub ime_control: Option<servo::EmbedderControlId>,
/// The web tab developer tools look at.
pub devtools_target: Option<u64>,
pub pages: pages::PageState,
/// Tab being dragged in the tab strip: (tab id, grab offset x).
pub dragging_tab: Option<(u64, f32)>,
pub qr_texture: Option<(String, egui::TextureHandle)>,
pub art_texture: Option<(String, egui::TextureHandle)>,
/// New tab background image or GIF (its own slot, so it doesn't fight the artwork for `art_texture`).
pub ntp_backdrop: Option<pages::backdrop::Backdrop>,
/// Visualizer bars shown in the media flyout.
pub levels: Vec<f32>,
pub last_level_poll: f64,
pub media_seek: Option<f32>,
/// Address bar suggestions for `omnibox_query`.
pub omnibox_query: String,
pub omnibox_items: Vec<chrome::Suggestion>,
}
impl WindowUi {
pub fn close_popups(&mut self) {
self.flyout = None;
self.suggestion_index = None;
}
/// A flyout is open: the first click on the page closes it instead of reaching the page.
pub fn blocks_page(&self) -> bool {
self.flyout.is_some()
}
pub fn has_popup(&self) -> bool {
self.flyout.is_some() || self.omnibox_focused
}
pub fn palette(&self) -> Palette {
self.palette.unwrap_or_else(|| Palette::new(Color32::from_rgb(0x67, 0x50, 0xA4), true, "tonal_spot", 0.0))
}
pub fn toggle(&mut self, flyout: Flyout, anchor: Option<egui::Rect>) {
if self.flyout.as_ref() == Some(&flyout) {
self.flyout = None;
} else {
self.flyout = Some(flyout);
self.flyout_anchor = anchor;
}
}
}
/// What a frame of drawing works with.
pub struct Frame<'a> {
pub shared: &'a Rc<Shared>,
pub hub: &'a Rc<Hub>,
pub tabs: &'a mut Vec<Tab>,
pub active: &'a mut usize,
pub ui: &'a mut WindowUi,
pub page_rect: &'a mut Option<egui::Rect>,
pub kind: &'a Kind,
pub fullscreen: bool,
pub pip_frame: Option<&'a egui::TextureHandle>,
pub pip_size: [u32; 2],
pub commands: &'a mut Vec<Command>,
pub window_id: WindowId,
}
impl Frame<'_> {
pub fn send(&mut self, command: Command) {
self.commands.push(command);
}
pub fn tab(&self) -> Option<&Tab> {
self.tabs.get(*self.active)
}
pub fn tab_mut(&mut self) -> Option<&mut Tab> {
self.tabs.get_mut(*self.active)
}
pub fn palette(&self) -> Palette {
self.ui.palette()
}
pub fn incognito(&self) -> bool {
self.shared.incognito
}
}
pub fn draw(root: &mut egui::Ui, f: &mut Frame) {
let ctx = root.ctx().clone();
if let Kind::Pip { .. } = f.kind {
chrome::pip_window(root, f);
return;
}
let app_window = matches!(f.kind, Kind::App(_));
let page_fullscreen = f.fullscreen && f.tab().is_some_and(|t| t.fullscreen);
if !page_fullscreen {
let custom_title = f.shared.settings.borrow().ui.custom_title_bar && *f.kind == Kind::Normal;
if custom_title {
chrome::title_bar(root, f);
}
if app_window {
chrome::app_bar(root, f);
} else {
chrome::tab_strip(root, f);
chrome::toolbar(root, f);
if chrome::bookmarks_bar_visible(f) {
chrome::bookmarks_bar(root, f);
}
}
dialogs::prompt_bars(root, f);
dialogs::find_bar(root, f);
}
let floating = f.shared.settings.borrow().ui.floating_card && !page_fullscreen && !app_window;
// The page floats as a rounded card with a margin, on the chrome's colour.
let gap = crate::theme::GAP_S as i8;
let margin = if floating { egui::Margin { left: gap, right: gap, top: 0, bottom: gap } } else { egui::Margin::ZERO };
let frame = egui::Frame::NONE.fill(chrome::chrome_fill(&f.palette())).inner_margin(margin);
egui::CentralPanel::no_frame().frame(frame).show(root, |ui| page_area(ui, f, floating));
dialogs::page_dialogs(&ctx, f);
menus::flyouts(&ctx, f);
toasts(&ctx, f);
}
/// The page itself: a web page's framebuffer, or one of Nacre's pages.
fn page_area(ui: &mut egui::Ui, f: &mut Frame, floating: bool) {
let rect = ui.max_rect();
page_contents(ui, f);
if floating {
card_corners(ui, rect, f.palette());
}
}
/// Rounds the page card's corners. The web page is a framebuffer blitted as a rectangle, so the corners are masked
/// afterwards with a ring in the chrome colour whose inner edge is the card's rounded outline.
fn card_corners(ui: &egui::Ui, rect: egui::Rect, p: Palette) {
let radius = crate::theme::radius(ui.ctx(), crate::theme::L);
let painter = ui.painter().with_clip_rect(rect);
painter.rect_stroke(rect.expand(radius), egui::CornerRadius::same((radius * 2.0).round().min(255.0) as u8), egui::Stroke::new(radius, chrome::chrome_fill(&p)), egui::StrokeKind::Inside);
}
fn page_contents(ui: &mut egui::Ui, f: &mut Frame) {
let rect = ui.max_rect();
let Some(tab) = f.tabs.get(*f.active) else {
*f.page_rect = None;
return;
};
if let Some(page) = tab.page.clone() {
*f.page_rect = None;
egui::Frame::NONE.fill(f.palette().surface_low).show(ui, |ui| {
ui.set_min_size(rect.size());
pages::draw(ui, f, &page);
});
return;
}
if tab.pip_size.is_some() {
*f.page_rect = None;
ui.centered_and_justified(|ui| {
ui.vertical_centered(|ui| {
ui.add_space(rect.height() * 0.35);
ui.label(RichText::new("Playing in picture in picture").size(18.0));
if ui.button("Bring it back").clicked() {
let tab_id = f.tabs[*f.active].id;
f.send(Command::ClosePip(tab_id));
}
});
});
return;
}
if let Some(reason) = tab.crashed.clone() {
*f.page_rect = None;
ui.vertical_centered(|ui| {
ui.add_space(rect.height() * 0.25);
ui.heading("This page crashed");
ui.label("Something on the page broke the engine. Reloading usually fixes it.");
ui.add_space(8.0);
if ui.button("Reload").clicked() {
f.send(Command::CrashReload);
}
ui.collapsing("Details", |ui| {
ui.monospace(reason);
});
});
return;
}
*f.page_rect = Some(rect);
match tab.web.as_ref() {
Some(web) => {
if let Some(callback) = crate::window::page_callback(rect, &web.context) {
ui.painter().add(callback);
}
// Status text (link targets) at the bottom left, like other browsers.
if let Some(status) = tab.status_text.as_ref().filter(|s| !s.is_empty()) {
let text = crate::util::ellipsize(status, 120);
let galley = ui.painter().layout_no_wrap(text, egui::FontId::proportional(12.0), f.palette().text);
let r = egui::Rect::from_min_size(rect.left_bottom() - egui::vec2(0.0, galley.size().y + 6.0), galley.size() + egui::vec2(12.0, 6.0));
ui.painter().rect_filled(r, 4.0, f.palette().surface_high);
ui.painter().galley(r.min + egui::vec2(6.0, 3.0), galley, f.palette().text);
}
},
None => {
ui.painter().rect_filled(rect, 0.0, f.palette().surface_low);
ui.put(egui::Rect::from_center_size(rect.center(), egui::vec2(40.0, 40.0)), egui::Spinner::new());
},
}
}
fn toasts(ctx: &egui::Context, f: &mut Frame) {
let toasts: Vec<(String, Option<(String, Command)>)> = f.shared.toasts.borrow().iter().map(|t| (t.text.clone(), t.action.clone())).collect();
if toasts.is_empty() {
return;
}
let palette = f.palette();
egui::Area::new(egui::Id::new("toasts")).anchor(Align2::CENTER_BOTTOM, egui::vec2(0.0, -16.0)).order(egui::Order::Tooltip).show(ctx, |ui| {
for (i, (text, action)) in toasts.iter().enumerate() {
egui::Frame::popup(ui.style()).fill(palette.surface_high).show(ui, |ui| {
ui.horizontal(|ui| {
ui.label(text);
if let Some((label, command)) = action
&& ui.button(label).clicked()
{
f.commands.push(command.clone());
if let Some(t) = f.shared.toasts.borrow_mut().get_mut(i) {
t.until = std::time::Instant::now();
}
}
if ui.small_button(icons::CLOSE).clicked()
&& let Some(t) = f.shared.toasts.borrow_mut().get_mut(i)
{
t.until = std::time::Instant::now();
}
});
});
}
});
}