From bc72330e2a869181e56cc45e6705f5e925cd6c3a Mon Sep 17 00:00:00 2001 From: bflatastic Date: Sat, 21 Nov 2026 23:22:25 +0700 Subject: [PATCH] shell-w9: net change of 2 commits - shell-w9: written from its sheet - shell-w9: repair 1 --- crates/ui-egui/src/brand.rs | 92 ++--- crates/ui-egui/src/chrome.rs | 634 +++++++------------------------- crates/ui-egui/src/community.rs | 123 ++----- 3 files changed, 201 insertions(+), 648 deletions(-) diff --git a/crates/ui-egui/src/brand.rs b/crates/ui-egui/src/brand.rs index eb9a45043b..71c883516a 100644 --- a/crates/ui-egui/src/brand.rs +++ b/crates/ui-egui/src/brand.rs @@ -1,86 +1,44 @@ //! The VectorCraft brand mark: the app icon (the engraved dragon, `assets/app-icon/`, see its -//! README). The PNG carries the icon's rounded corners; it is decoded once per context into a -//! mipmapped texture, so it stays crisp from the 18 pt app-bar mark to the About box. +//! README). The PNG carries the icon's rounded corners; it is decoded once and shared by the app +//! bar's `Picture` and the About box, which upload and mipmap it themselves. -use egui::{Color32, Context, Id, Rect, TextureHandle, TextureOptions, Ui, pos2}; +use std::sync::{Arc, OnceLock}; -/// 128 px: sharp at 44 pt on a 2× display, and the mipmaps keep 22 pt clean. +use egui::{Color32, ColorImage}; + +/// 128 px: sharp at 44 pt on a 2× display. const ICON_PNG: &[u8] = include_bytes!("../../../assets/app-icon/hicolor/128x128/apps/ai.storyteller.vectorcraft.png"); +/// The key a `Picture` stores the brand mark's texture under. +pub(crate) const BRAND_KEY: u64 = 0x5643_4252_414e_4421; + /// The icon's pixels (one transparent pixel if it couldn't be decoded, which a test rules out). -fn decode() -> egui::ColorImage { +fn decode() -> ColorImage { match craft_image_codecs::decode_as(craft_image_codecs::Format::Png, ICON_PNG) { - Ok(img) => egui::ColorImage::from_rgba_unmultiplied([img.width() as usize, img.height() as usize], &img.to_rgba8()), - Err(_) => egui::ColorImage::filled([1, 1], Color32::TRANSPARENT), + Ok(img) => ColorImage::from_rgba_unmultiplied([img.width() as usize, img.height() as usize], &img.to_rgba8()), + Err(_) => ColorImage::filled([1, 1], Color32::TRANSPARENT), } } -/// The icon texture, uploaded on first use and kept in the context. -fn texture(ctx: &Context) -> TextureHandle { - let id = Id::unique("vectorcraft-brand-mark"); - if let Some(tex) = ctx.data(|d| d.get_temp::(id)) { - return tex; - } - let options = TextureOptions { mipmap_mode: Some(egui::TextureFilter::Linear), ..TextureOptions::LINEAR }; - let tex = ctx.load_texture("vectorcraft-brand-mark", decode(), options); - ctx.data_mut(|d| d.insert_temp(id, tex.clone())); - tex -} - -/// Paint the mark into `r` (square). -pub fn paint_mark(ui: &Ui, r: Rect) { - let uv = Rect::from_min_max(pos2(0.0, 0.0), pos2(1.0, 1.0)); - ui.painter().image(texture(ui.ctx()).id(), r, uv, Color32::WHITE); -} - -/// The mark's texture id (tests: find the painted marks among the shapes). -#[cfg(test)] -pub fn texture_id(ctx: &Context) -> egui::TextureId { - texture(ctx).id() +/// The brand mark's image, decoded on first use. +pub(crate) fn image() -> Arc { + static IMAGE: OnceLock> = OnceLock::new(); + IMAGE.get_or_init(|| Arc::new(decode())).clone() } #[cfg(test)] mod tests { use super::*; - use crate::VectorcraftApp; + /// A broken or replaced asset falls back to one transparent pixel, and the bar and About lose + /// their mark. #[test] - fn the_icon_decodes_to_a_rounded_128_px_tile() { - let img = decode(); - assert_eq!(img.size, [128, 128]); - // Rounded corners: the corner pixel is transparent, the centre opaque. - assert_eq!(img.pixels[0].a(), 0); - assert_eq!(img.pixels[64 * 128 + 64].a(), 255); - } - - #[test] - fn the_app_bar_paints_the_icon_from_one_cached_texture() { - let ctx = Context::default(); - crate::theme::install_fonts(&ctx); - let mut app = VectorcraftApp::new(vectorcraft_engine::Session::new(), Default::default()); - // The system title bar hides the in-app mark (the OS draws its own icon), so paint the - // app-drawn bar here. - app.custom_titlebar = true; - let mut frame = || { - let mut out = ctx.run_ui(egui::RawInput::default(), |ui| crate::chrome::app_bar(&mut app, ui)); - let uploads = out.textures_delta.set.values().flat_map(|d| d.iter()).filter(|d| d.image.size() == [128, 128]).count(); - out.textures_delta.clear(); - let marks: Vec = out - .shapes - .iter() - .filter_map(|c| match &c.shape { - egui::Shape::Mesh(m) if m.texture_id == texture(&ctx).id() => Some(m.calc_bounds()), - _ => None, - }) - .collect(); - (uploads, marks) - }; - let (uploads, marks) = frame(); - assert_eq!(uploads, 1); - assert_eq!(marks.len(), 1); - assert_eq!(marks[0].size(), egui::vec2(18.0, 18.0)); - // Later frames reuse the texture. - let (uploads, marks) = frame(); - assert_eq!((uploads, marks.len()), (0, 1)); + fn the_icon_is_not_the_transparent_fallback() { + let img = image(); + let [w, h] = img.size; + assert!(w > 1 && h > 1, "{:?}", img.size); + let centre = img.pixels[(h / 2) * w + w / 2].a(); + assert_eq!(centre, 255, "the centre is opaque"); + assert!(img.pixels[0].a() < centre, "the rounded corner is more transparent than the centre"); } } diff --git a/crates/ui-egui/src/chrome.rs b/crates/ui-egui/src/chrome.rs index 511967d45f..1dbd5b51a3 100644 --- a/crates/ui-egui/src/chrome.rs +++ b/crates/ui-egui/src/chrome.rs @@ -1,9 +1,21 @@ //! Window chrome: application bar (menus), Control bar, document tabs, status bar. use std::borrow::Cow; +use std::cell::RefCell; use std::sync::Arc; -use egui::{CornerRadius, Sense, Stroke, Ui, vec2}; +use craft_ui::bars::BarHome; +use craft_ui::buttons::{Button, ButtonChord, IconButton, PopupMark}; +use craft_ui::contract::{Bind, BodyCx, Props, Show, ShowContainer, Trigger}; +use craft_ui::display::{Divider, Picture}; +use craft_ui::layout::measure_slot; +use craft_ui::menu::{Menu, MenuItem}; +use craft_ui::primitives::{HAlign, IconSpec}; +use craft_ui::runtime::windows::WindowHost; +use craft_ui::shell::{Frame, Title, TitleBar}; +use craft_ui::style::Variant; +use craft::behavior::{FitItem, FitKeep, IconId, fit_items}; +use egui::{Sense, Stroke, Ui, vec2}; use serde_json::json; use vectorcraft_doc::NodeKind; @@ -11,7 +23,7 @@ use crate::panels::stroke as stroke_panel; use crate::state::{ROTATE_VIEW_ANGLES, ZOOM_STOPS, zoom_label}; use crate::theme::{self, Tokens}; use crate::widgets; -use crate::{VectorcraftApp, icons, menus, titlebar}; +use crate::{VectorcraftApp, brand, icons, menus}; mod control_bar; mod hints; @@ -39,329 +51,145 @@ pub(crate) use tabs::{document_window_title, show_document, window_tab}; /// The application bar's height, in points (compact). pub const APP_BAR_HEIGHT: f32 = 32.0; -/// The brand mark's side. -const MARK: f32 = 18.0; -/// Height of the workspace switcher. -const WIDGET_H: f32 = 22.0; -/// The application bar: brand mark, Home, menus, then Discord, search and the workspace switcher -/// at the right. With [`VectorcraftApp::custom_titlebar`] it is also the window's title bar -/// ([`titlebar`]): the caption buttons take the ends [`VectorcraftApp::caption_layout`] names and the -/// rest of the bar drags the window. -/// On macOS, under the system's transparent title bar, its empty space drags and zooms the window -/// too ([`titlebar::drag_area`]). With the system title bar (Windows and Linux, `system_title_bar`) the OS draws its own icon and -/// title, so the in-app bar shows Home, menus and workspace controls only: no brand mark. -pub fn app_bar(app: &mut VectorcraftApp, ui: &mut Ui) { - let t = Tokens::get(ui.ctx()); - let custom = app.custom_titlebar; - let system = system_title_bar(app); - let left = app.titlebar_inset.clamp(8.0, f32::from(i8::MAX)) as i8; - // The room the caption buttons take at each end of the bar. - let (captions_left, captions_right) = if custom { (app.caption_layout.left_width(), app.caption_layout.right_width()) } else { (0.0, 0.0) }; - let right_margin = if captions_right > 0.0 { 0 } else { 14 }; - let frame = egui::Frame::NONE.fill(t.app_bar).inner_margin(egui::Margin { left, right: right_margin, top: 0, bottom: 0 }); - let bar = egui::Panel::top("app_bar").exact_size(APP_BAR_HEIGHT).frame(frame.stroke(Stroke::new(1.0, t.border))).show(ui, |ui| { - // macOS draws the bar under its transparent title bar, so the bar gets the title bar's - // clicks: without this a double-click there zooms nothing (#1932). - if custom || cfg!(target_os = "macos") { - titlebar::drag_area(ui, ui.max_rect()); - } - ui.horizontal_centered(|ui| { - if captions_left > 0.0 { - ui.add_space(captions_left); - } - // With the system title bar the OS draws its own icon: no in-app brand mark. - if !system { - let (r, _) = ui.allocate_exact_size(vec2(MARK, MARK), Sense::hover()); - crate::brand::paint_mark(ui, r); - ui.add_space(6.0); - } - let on_home = menus::home_showing(app); - if widgets::icon_button(ui, "house", tl!("Home"), on_home, 20.0).clicked() { - app.run("app.home", json!({})).ok(); - } - ui.add_space(2.0); - // With the macOS menu bar the menus are at the top of the screen instead. - let menus_end = if app.services.native_menu.is_some() { ui.cursor().min.x } else { menus::menu_bar(app, ui) }; - // The right-side group fills the space after the menus from the right, in the order - // [`fit_right_group`] gives. - let full = ui.max_rect(); - let right_edge = if captions_right > 0.0 { full.right() - captions_right - 10.0 } else { full.right() }; - let right = egui::Rect::from_min_max(egui::pos2(menus_end + 8.0, full.top()), egui::pos2(right_edge, full.bottom())); - // In Safe Mode, a label that stays in the bar whatever document is open, and opens - // Help › Safe Mode; the other controls fit in the room it leaves, and the workspace - // switcher (also under Window › Workspace) goes when a minimal one doesn't fit. - let safe = vectorcraft_plugins::safe_mode::active().then(|| egui::RichText::new(tl!("Safe Mode")).color(t.warning).size(12.0)); - let safe_w = safe.as_ref().map_or(0.0, |label| { - let text = ui.painter().layout_no_wrap(label.text().to_string(), egui::FontId::proportional(12.0), t.warning); - text.size().x + 2.0 * ui.spacing().button_padding.x + 8.0 + ui.spacing().item_spacing.x - }); - // Built-in workspace names translate; the user's own stay as typed. - let ws_name = - if crate::workspaces::is_builtin(&app.ui.workspace) { tl!(&app.ui.workspace).to_string() } else { app.ui.workspace.clone() }; - let ws = ui.painter().layout_no_wrap(ws_name, egui::FontId::proportional(12.0), t.text); - let shortcut = menus::shortcut_of("help.commandPalette").map(menus::shown_shortcut); - let field = SearchField::new(ui, &t, shortcut); - let fit = fit_right_group(&RightWidths { - room: (right.width() - safe_w).max(0.0), - safe_mode: safe.is_some(), - switcher: (ws.size().x + 36.0).clamp(112.0, 190.0), - field: field.width, - icon: BAR_ICON, - gap: ui.spacing().item_spacing.x, - community: crate::community::discord_width(ui, false), - }); - let (switcher, ws_w, arrange, learn, discord) = (fit.switcher, fit.switcher_w, fit.arrange, fit.learn, fit.community); - let mut rui = ui.new_child(egui::UiBuilder::new().max_rect(right).layout(egui::Layout::right_to_left(egui::Align::Center))); - let ui = &mut rui; - // Workspace switcher: shows the current workspace, opens Window → Workspace. - if switcher { - let (wr, wresp) = ui.allocate_exact_size(vec2(ws_w, WIDGET_H), Sense::click()); - ui.painter().rect_filled(wr, CornerRadius::same(4), if wresp.hovered() { t.hover } else { t.panel }); - ui.painter().with_clip_rect(wr.shrink2(vec2(4.0, 0.0))).galley(wr.left_center() + vec2(10.0, -ws.size().y / 2.0), ws, t.text); - icons::paint(ui, "chevron-down", egui::Rect::from_center_size(wr.right_center() - vec2(12.0, 0.0), vec2(12.0, 12.0)), t.text_dim); - let wresp = wresp.on_hover_text(tl!("Switch workspace")); - egui::Popup::menu(&wresp).show(|ui| crate::workspaces::popup(app, ui)); - ui.add_space(8.0); - } - if let Some(label) = safe { - if ui.add(egui::Button::new(label).frame(false)).on_hover_text(tl!("Safe Mode…")).clicked() { +/// The command the search button runs and shows the chord of. +const PALETTE: &str = "help.commandPalette"; + +/// A control of the bar's trailing group. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +enum Trailing { + /// The Safe Mode label, shown only in Safe Mode. + Safe, + /// The community button. + Discord, + /// Explore tutorials. + Learn, + /// Arrange Documents. + Arrange, + /// The search that opens the command palette. + Search, + /// The workspace switcher. + Switcher, +} + +impl Trailing { + /// The controls in the order they are drawn. + const SHOWN: [Trailing; 6] = [Trailing::Discord, Trailing::Learn, Trailing::Search, Trailing::Arrange, Trailing::Safe, Trailing::Switcher]; + /// The controls in the order the fit gives them up, last first: each is also in a menu, so a hidden one goes nowhere. + const DROPPED_LAST_FIRST: [Trailing; 6] = + [Trailing::Safe, Trailing::Switcher, Trailing::Search, Trailing::Arrange, Trailing::Learn, Trailing::Discord]; + + /// Whether the fit may drop it. + fn keep(self) -> FitKeep { + if matches!(self, Trailing::Safe | Trailing::Switcher) { FitKeep::Pinned } else { FitKeep::Free } + } + + /// Shows the control; the fit measures it through the same call. + fn show(self, app: &mut VectorcraftApp, ui: &mut Ui) { + match self { + Trailing::Safe => { + let shown = Button::new(tl!("Safe Mode")).variant(Variant::GHOST).tooltip(tl!("Safe Mode…")).show(ui); + if shown.clicks > 0 { app.run("help.safeMode", json!({})).ok(); } - ui.add_space(8.0); } - // Arrange Documents: Consolidate All Windows and the Tile layouts. - if arrange { - let resp = widgets::icon_button(ui, "layout-grid", tl!("Arrange Documents"), app.ui.tiled, BAR_ICON); - egui::Popup::menu(&resp).show(|ui| arrange_documents_popup(app, ui)); + Trailing::Discord => crate::community::discord_button(app, ui), + Trailing::Learn => { + // PLAN: assumed `IconButton::command` is bound by a `&str` command id. + IconButton::command(IconId::new("graduation-cap"), "window.learn").selected(crate::panels::learn::visible(&app.ui)).show(ui); } - // Search → command palette (its shortcut opens it too). - let with_shortcut = |text: &str| match &field.shortcut { - Some(sc) => format!("{text} ({sc})"), - None => text.to_string(), - }; - let tip = with_shortcut(tl!("Search commands, tools, panels and effects")); - let name = with_shortcut(tl!("Search commands")); - // The field and the icon toggle the palette as its shortcut does, and like the shortcut - // they do nothing while a dialog is open, so they are drawn disabled then. - let enabled = app.ui.dialog.is_none(); - let open = app.ui.palette_open; - let clicked = ui - .add_enabled_ui(enabled, |ui| match fit.search { - Search::Field => field.show(ui, &t, &name, &tip, open).clicked(), - Search::Icon => { - let (_, resp) = ui.allocate_exact_size(vec2(BAR_ICON, BAR_ICON), Sense::click()); - widgets::paint_icon_button_named(ui, resp, "search", &name, &tip, open).clicked() + Trailing::Arrange => arrange_documents(app, ui), + Trailing::Search => { + // PLAN: assumed `Button::command` is bound by a `&str` command id; the command opens the palette. + Button::command(PALETTE) + .icon(IconSpec::svg(IconId::new("search"))) + .variant(Variant::SECONDARY) + .align(HAlign::Start) + .chord(ButtonChord::Trailing) + .show(ui); + } + // PLAN: assumed `crate::workspaces::switcher(app: &mut VectorcraftApp, ui: &mut Ui)` (chrome-w14's `WorkspaceSwitcher`). + Trailing::Switcher => crate::workspaces::switcher(app, ui), + } + } +} + +/// Arrange Documents: Consolidate All Windows and the Tile layouts, each item as the Window menu shows it. +fn arrange_documents(app: &mut VectorcraftApp, ui: &mut Ui) { + let opener = IconButton::new(IconId::new("layout_grid")).label(tl!("Arrange Documents")).selected(app.ui.tiled).popup_mark(PopupMark::Menu).show(ui); + let mut chosen = None; + let state = &*app; + // PLAN: the sheet names `Trigger::Manual`; `Trigger::Press` is the pop-up menu button's trigger and needs no open binding. + Menu::new(tl!("Arrange Documents")).opened_by(&opener.response, Trigger::Press).show(ui, |ui, _| { + for item in menus::arrange_documents_items() { + match item { + menus::Item::Sep => { + Divider::horizontal().show(ui); + } + menus::Item::Cmd(label, id, params) => { + let Some((enabled, checked)) = menus::shown_state(state, id, ¶ms) else { continue }; + let row = MenuItem::new(crate::i18n::t_id(id, label)).enabled(enabled).check(checked).show(ui); + if row.ran { + chosen = Some((id, if params.is_null() { json!({}) } else { params })); } - Search::Hidden => false, - }) - .inner; - if clicked && enabled { - crate::palette::toggle(app); - } - if learn { - let shown = crate::panels::learn::visible(&app.ui); - if widgets::icon_button(ui, "graduation-cap", tl!("Explore tutorials"), shown, BAR_ICON).clicked() - && let Err(e) = app.run("window.learn", json!({})) - { - app.ui.status = e; } + _ => {} } - if discord { - ui.add_space(10.0); - crate::community::discord_button(app, ui, false); - } - if app.services.native_menu.is_some() { - let title = ui.painter().layout_no_wrap("VectorCraft".into(), egui::FontId::proportional(13.5), t.text); - let title_rect = egui::Rect::from_center_size(full.center(), title.size()); - // Keep the title centered only while the interactive groups leave room for it. - if title_rect.left() >= menus_end + 8.0 && title_rect.right() <= ui.min_rect().left() - 8.0 { - ui.painter().galley(title_rect.min, title, t.text); - } - } - }); + } }); - if custom { - titlebar::caption_buttons(app, ui, bar.response.rect); + if let Some((id, params)) = chosen { + menus::invoke(app, id, params); } } -/// The Arrange Documents menu ([`menus::arrange_documents_items`]): each item as the Window menu -/// shows it, grayed out while its command can't run, the Tile layout the panes show checked. -fn arrange_documents_popup(app: &mut VectorcraftApp, ui: &mut Ui) { - ui.set_min_width(200.0); - let mut clicked = None; - for it in menus::arrange_documents_items() { - match it { - menus::Item::Sep => { - ui.separator(); +/// The start of the bar: the brand mark (the OS draws its own icon in a native frame), Home and the menus. +fn leading(ui: &mut Ui, app: &mut VectorcraftApp) { + if !matches!(WindowHost::of(ui.ctx()).frame(), Frame::Native) { + let image = || (*brand::image()).clone(); + Picture::new(tl!("VectorCraft"), brand::BRAND_KEY, &image).show(ui); + } + let mut home = menus::home_showing(app); + let before = home; + BarHome::new(tl!("Home"), Bind::bound(&mut home)).show(ui); + if home != before { + app.run("app.home", json!({})).ok(); + } + // With the macOS menu bar the menus are at the top of the screen instead. + if app.services.native_menu.is_none() { + // PLAN: assumed menus-w3's `menus::menu_bar(app, ui)` shows `MenuBarUi` and returns its `MenuBarOutput`; `opened` refreshes the scripts menu (menus-w6). + let _ = menus::menu_bar(app, ui); + } +} + +/// The end of the bar: the controls that fit in the room, the first-listed going last. +fn trailing(ui: &mut Ui, app: &mut VectorcraftApp) { + let safe = vectorcraft_plugins::safe_mode::active(); + let order: Vec = Trailing::DROPPED_LAST_FIRST.into_iter().filter(|t| safe || *t != Trailing::Safe).collect(); + let gap = ui.spacing().item_spacing.x; + let room = ui.available_width(); + let items: Vec = order.iter().map(|t| FitItem::new(measure_slot(ui, room, |ui| t.show(app, ui)).x, 0, t.keep())).collect(); + let fit = fit_items(&items, room, gap, gap, 0.0); + ui.horizontal_centered(|ui| { + for t in Trailing::SHOWN { + if order.iter().position(|o| *o == t).is_some_and(|at| fit.shown.contains(&at)) { + t.show(app, ui); } - menus::Item::Cmd(label, id, p) => { - let Some((enabled, checked)) = menus::shown_state(app, id, &p) else { continue }; - let label = crate::i18n::t_id(id, label); - let text = if checked == Some(true) { format!("✓ {label}") } else { format!(" {label}") }; - if ui.add_enabled(enabled, egui::Button::new(text).min_size(vec2(200.0, 0.0))).clicked() { - clicked = Some((id, if p.is_null() { json!({}) } else { p })); - ui.close(); - } - } - _ => {} } - } - if let Some((id, p)) = clicked { - menus::invoke(app, id, p); - } + }); } -/// The side of the app bar's square icon buttons (the search icon, Arrange Documents, Explore tutorials). -const BAR_ICON: f32 = 28.0; -/// The narrowest the workspace switcher gets. -const MIN_SWITCHER: f32 = 64.0; - -/// How the app bar shows the search that opens the command palette. -#[derive(Clone, Copy, Debug, PartialEq, Eq)] -enum Search { - /// A field with the magnifier, "Search commands" and the palette's shortcut. - Field, - /// The magnifier alone. - Icon, - /// Not in the bar; the palette stays under its shortcut and Help › Search Commands…. - Hidden, -} - -/// The widths [`fit_right_group`] fits into `room`, in points. -#[derive(Clone, Copy, Debug)] -struct RightWidths { - /// The room after the menus, less the Safe Mode label's. - room: f32, - /// Whether the bar shows the Safe Mode label. - safe_mode: bool, - /// The workspace switcher's full width. - switcher: f32, - /// The search field's width. - field: f32, - /// The side of a square icon button. - icon: f32, - /// The space between two controls. - gap: f32, - /// The community button's width. - community: f32, -} - -/// What the right-hand group of the app bar shows. -#[derive(Clone, Copy, Debug, PartialEq)] -struct RightGroup { - /// Whether the workspace switcher shows. - switcher: bool, - /// The workspace switcher's width. - switcher_w: f32, - /// How the search shows. - search: Search, - /// Whether Arrange Documents shows. - arrange: bool, - /// Whether Explore tutorials shows. - learn: bool, - /// Whether the community button shows. - community: bool, -} - -/// The right-hand group for the room it has. As the room runs short, the community button goes first -/// (it is also under Help), then Explore tutorials (also Window › Learn), then Arrange Documents (also Window › -/// Arrange), then the search field becomes the search icon, then the workspace switcher narrows to [`MIN_SWITCHER`], then the icon goes. In Safe -/// Mode the label's width is taken first, and the switcher (also under Window › Workspace) goes when -/// a minimal one does not fit. -fn fit_right_group(w: &RightWidths) -> RightGroup { - let room = w.room; - let switcher = !w.safe_mode || room >= MIN_SWITCHER + 8.0; - let icon = switcher && room >= MIN_SWITCHER + 8.0 + w.gap + w.icon; - let field = icon && room >= w.switcher + 8.0 + w.gap + w.field; - let search = if field { - Search::Field - } else if icon { - Search::Icon - } else { - Search::Hidden - }; - let search_w = match search { - Search::Field => w.gap + w.field, - Search::Icon => w.gap + w.icon, - Search::Hidden => 0.0, - }; - let switcher_w = w.switcher.min(room - 8.0 - search_w).max(MIN_SWITCHER); - let arrange_end = switcher_w + 8.0 + search_w + w.gap + w.icon; - let arrange = field && room >= arrange_end; - let learn_end = arrange_end + w.gap + w.icon; - let learn = arrange && room >= learn_end; - let community = learn && room >= learn_end + 10.0 + w.gap + w.community; - RightGroup { switcher, switcher_w, search, arrange, learn, community } -} - -/// The app bar's search field: a button drawn as a field. The command palette's popup holds the -/// only text field. -struct SearchField { - hint: Arc, - /// The palette's shortcut as the menus show it; none when the user cleared it. - shortcut: Option, - key: Option>, - width: f32, -} - -impl SearchField { - /// Space from the field's left edge to the hint text (the magnifier sits in it). - const TEXT_LEFT: f32 = 26.0; - /// The widest the field gets. - const MAX_WIDTH: f32 = 220.0; - - fn new(ui: &Ui, t: &Tokens, shortcut: Option) -> SearchField { - let hint = ui.painter().layout_no_wrap(tl!("Search commands").to_string(), egui::FontId::proportional(12.0), t.text_dim); - let text_w = Self::TEXT_LEFT + hint.size().x + 8.0; - // A shortcut that leaves no room for the hint in the widest field is left to the tooltip. - let key = shortcut - .as_ref() - .map(|s| ui.painter().layout_no_wrap(s.clone(), egui::FontId::proportional(11.0), t.text_dim)) - .filter(|k| text_w + 12.0 + k.size().x <= Self::MAX_WIDTH); - let key_w = key.as_ref().map_or(0.0, |k| 12.0 + k.size().x); - let width = (text_w + key_w + 24.0).clamp(120.0, Self::MAX_WIDTH); - SearchField { hint, shortcut, key, width } - } - - /// Draws the field. A click, or Enter or Space with the keyboard on it, clicks it. Screen - /// readers get a button called `name`, and hovering it shows `tip`. - fn show(self, ui: &mut Ui, t: &Tokens, name: &str, tip: &str, open: bool) -> egui::Response { - let (r, resp) = ui.allocate_exact_size(vec2(self.width, WIDGET_H), Sense::click()); - let fill = if open { - t.tool_active - } else if resp.hovered() { - t.hover - } else { - t.input - }; - let border = if resp.has_focus() { t.accent } else { t.input_border }; - ui.painter().rect(r, CornerRadius::same(4), fill, Stroke::new(1.0, border), egui::StrokeKind::Inside); - icons::paint(ui, "search", egui::Rect::from_center_size(r.left_center() + vec2(14.0, 0.0), vec2(14.0, 14.0)), t.icon); - let mut text_right = r.right() - 8.0; - if let Some(key) = self.key { - let at = egui::pos2(text_right - key.size().x, r.center().y - key.size().y / 2.0); - text_right = at.x - 12.0; - ui.painter().with_clip_rect(r).galley(at, key, t.text_dim); - } - let clip = egui::Rect::from_min_max(r.min, egui::pos2(text_right.max(r.left()), r.bottom())); - let at = egui::pos2(r.left() + Self::TEXT_LEFT, r.center().y - self.hint.size().y / 2.0); - ui.painter().with_clip_rect(clip).galley(at, self.hint, t.text_dim); - resp.widget_info(|| widgets::icon_button_info(ui.is_enabled(), open, name)); - resp.on_hover_text(tip) - } -} - -/// Whether the window shows the system's title bar instead of the app drawing its own -/// ([`VectorcraftApp::desktop_frame`]): Windows with Preferences › User Interface › System Title -/// Bar, and Linux whenever the desktop draws the frame, with or without the preference. The -/// preference is read when the app starts, so changing it later does not change this. macOS -/// always has system decorations, but keeps the in-app brand mark, so it never counts as system -/// mode here; nor does the web build (the browser tab has no document title), nor an embedder -/// without a window of its own. -pub fn system_title_bar(app: &VectorcraftApp) -> bool { - app.desktop_frame && !app.custom_titlebar && !cfg!(any(target_os = "macos", target_arch = "wasm32")) +/// The application bar, the body of the shell's title bar: brand mark, Home and menus, then Discord, +/// tutorials, Arrange Documents, search and the workspace switcher at the end. The title bar draws +/// the caption buttons, drags the window and shows the title. +pub fn app_bar(ui: &mut Ui, _cx: &mut BodyCx<'_>, app: &mut VectorcraftApp) { + let app = RefCell::new(app); + // Both closures need the app; the bar runs them one after the other. + let _ = TitleBar::new().show_titled( + ui, + Title::new(tl!("VectorCraft")), + |ui, _| leading(ui, &mut **app.borrow_mut()), + |ui, _| trailing(ui, &mut **app.borrow_mut()), + ); } /// The OS window title: the active document's name suffixed with the app name (a `*` prefix @@ -376,193 +204,5 @@ pub fn window_title(app: &VectorcraftApp) -> String { if vectorcraft_plugins::safe_mode::active() { format!("{title} ({})", tl!("Safe Mode")) } else { title } } -/// Keep the OS window title (and the taskbar / Alt-Tab entry) on the active file. Sends -/// `ViewportCommand::Title` only when the title changed since the last frame. -pub fn sync_window_title(app: &mut VectorcraftApp, ctx: &egui::Context) { - let want = window_title(app); - if app.last_window_title != want { - app.last_window_title = want.clone(); - ctx.send_viewport_cmd(egui::ViewportCommand::Title(want)); - // One more frame: a new title makes AppKit lay its title bar out again, putting the window - // buttons back where it keeps them until the next frame centres them on the bar (#968). - ctx.request_repaint(); - } -} - -/// Full Screen Mode and Presentation Mode fill the screen (#4365: the taskbar and the window's -/// frame stayed): the window goes full screen when one of them starts, and back when a mode with -/// the menu bar returns. A window that was full screen already stays so. -pub fn sync_fullscreen(app: &mut VectorcraftApp, ctx: &egui::Context) { - let want = app.ui.screen_mode >= 2; - let is = ctx.input(|i| i.viewport().fullscreen.unwrap_or(false)); - if want && !is && !app.fullscreen_for_mode { - ctx.send_viewport_cmd(egui::ViewportCommand::Fullscreen(true)); - app.fullscreen_for_mode = true; - } else if !want && app.fullscreen_for_mode { - if is { - ctx.send_viewport_cmd(egui::ViewportCommand::Fullscreen(false)); - } - app.fullscreen_for_mode = false; - } -} - #[cfg(test)] mod tests; - -#[cfg(test)] -mod visual_regressions { - use super::*; - - fn scaled_input(size: egui::Vec2, scale: f32) -> egui::RawInput { - let mut input = egui::RawInput { screen_rect: Some(egui::Rect::from_min_size(egui::Pos2::ZERO, size)), ..Default::default() }; - input.viewports.entry(egui::ViewportId::ROOT).or_default().native_pixels_per_point = Some(scale); - input - } - - struct NativeMenus; - impl crate::native_menu::Backend for NativeMenus { - fn sync(&mut self, _: &crate::native_menu::MenuBar) {} - fn drain(&mut self) -> Vec { - Vec::new() - } - } - - #[test] - fn capture_chrome_visual_fixtures() { - let Some(directory) = std::env::var_os("CRAFT_UI_VISUAL_FIXTURES").map(std::path::PathBuf::from) else { - return; - }; - std::fs::create_dir_all(&directory).unwrap(); - for brightness in theme::Brightness::ALL { - for width in [800.0, 1280.0] { - for scale in [1.0, 1.5, 2.0] { - let mut app = VectorcraftApp::new(vectorcraft_engine::Session::new(), Default::default()); - app.services.native_menu = Some(crate::native_menu::NativeMenu::new(Box::new(NativeMenus))); - app.ui.status = format!("Saved /Users/example/Documents/{}/illustration.svg", "long-folder-name/".repeat(10)); - let mut ready = false; - let mut harness = - egui_kittest::Harness::builder().with_size(vec2(width, 200.0)).with_pixels_per_point(scale).wgpu().build_ui_state( - move |ui, app: &mut VectorcraftApp| { - if !ready { - egui_extras::install_image_loaders(ui.ctx()); - theme::install_fonts(ui.ctx()); - theme::apply(ui.ctx(), brightness); - ready = true; - return; - } - app_bar(app, ui); - status_bar(app, ui); - }, - app, - ); - harness.input_mut().max_texture_side = Some(8192); - harness.run_steps(4); - let stem = format!("vectorcraft-chrome-{brightness:?}-{width}-{scale}x"); - harness.render().unwrap().save(directory.join(format!("{stem}.png"))).unwrap(); - let mut regions = serde_json::Map::new(); - for id in ["app_bar", "status_bar"] { - let rect = egui::containers::panel::PanelState::load(&harness.ctx, egui::Id::unique(id)).unwrap().outer_rect; - regions.insert(id.into(), json!([rect.left(), rect.top(), rect.width(), rect.height()])); - } - let dynamic: Vec<_> = harness - .output() - .shapes - .iter() - .filter_map(|shape| match &shape.shape { - egui::Shape::Text(text) if text.galley.job.text.contains(" fps") => { - let r = text.visual_bounding_rect(); - Some(json!([r.left(), r.top(), r.width(), r.height()])) - } - _ => None, - }) - .collect(); - std::fs::write( - directory.join(format!("{stem}.json")), - serde_json::to_vec_pretty(&json!({ - "screen": [width, 200.0], "scale": scale, "regions": regions, "native_dynamic_text_regions": dynamic - })) - .unwrap(), - ) - .unwrap(); - } - } - } - } - - #[test] - fn native_title_does_not_overlap_header_controls() { - for brightness in theme::Brightness::ALL { - for width in [800.0, 1280.0] { - for scale in [1.0, 1.5, 2.0] { - let ctx = egui::Context::default(); - egui_extras::install_image_loaders(&ctx); - theme::install_fonts(&ctx); - theme::apply(&ctx, brightness); - let mut app = VectorcraftApp::new(vectorcraft_engine::Session::new(), Default::default()); - app.services.native_menu = Some(crate::native_menu::NativeMenu::new(Box::new(NativeMenus))); - let mut output = ctx.run_ui(scaled_input(vec2(width, 640.0), scale), |ui| app_bar(&mut app, ui)); - output.textures_delta.clear(); - assert_eq!(ctx.content_rect().size(), vec2(width, 640.0)); - assert_eq!(ctx.pixels_per_point(), scale); - let texts: Vec<_> = output - .shapes - .iter() - .filter_map(|shape| match &shape.shape { - egui::Shape::Text(text) => Some(text), - _ => None, - }) - .collect(); - let title = texts.iter().find(|text| text.galley.job.text == "VectorCraft"); - if width >= 1280.0 { - assert!(title.is_some(), "the title remains visible when the header has room"); - } - if let Some(title) = title { - for text in texts.iter().filter(|text| text.galley.job.text != "VectorCraft") { - assert!( - !title.visual_bounding_rect().intersects(text.visual_bounding_rect()), - "{width}: title overlaps {:?}", - text.galley.job.text - ); - } - } - } - } - } - } - - #[test] - fn long_status_stays_between_navigation_and_performance_readouts() { - for brightness in theme::Brightness::ALL { - for width in [800.0, 1280.0] { - for scale in [1.0, 1.5, 2.0] { - let ctx = egui::Context::default(); - egui_extras::install_image_loaders(&ctx); - theme::install_fonts(&ctx); - theme::apply(&ctx, brightness); - let mut app = VectorcraftApp::new(vectorcraft_engine::Session::new(), Default::default()); - app.ui.status = format!("Saved /Users/example/Documents/{}/illustration.svg", "long-folder-name/".repeat(10)); - let mut output = ctx.run_ui(scaled_input(vec2(width, 640.0), scale), |ui| status_bar(&mut app, ui)); - output.textures_delta.clear(); - assert_eq!(ctx.content_rect().size(), vec2(width, 640.0)); - assert_eq!(ctx.pixels_per_point(), scale); - let texts: Vec<_> = output - .shapes - .iter() - .filter_map(|shape| match &shape.shape { - egui::Shape::Text(text) => Some(text), - _ => None, - }) - .collect(); - let status = texts.iter().find(|text| text.galley.job.text.starts_with("Saved ")).expect("status stays visible"); - assert_eq!(status.galley.rows.len(), 1, "status is one line"); - assert!(status.galley.elided, "long status retains the full text for the label tooltip"); - let bounds = status.visual_bounding_rect(); - assert!(bounds.left() >= 0.0 && bounds.right() <= width, "{width}: {bounds:?}"); - for text in texts.iter().filter(|text| !text.galley.job.text.starts_with("Saved ")) { - assert!(!bounds.intersects(text.visual_bounding_rect()), "{width}: status overlaps {:?}", text.galley.job.text); - } - } - } - } - } -} diff --git a/crates/ui-egui/src/community.rs b/crates/ui-egui/src/community.rs index 97f7dffdba..b88b6f488d 100644 --- a/crates/ui-egui/src/community.rs +++ b/crates/ui-egui/src/community.rs @@ -1,96 +1,42 @@ -//! Community and project links: the Discord button (header, About, Home) and the link list. +//! Community and project links: the Discord button (app bar) and the URLs About and Home list. //! Icons are Lucide's (ISC); Discord's own logo is a trademark, so the button uses a chat icon //! on Discord's colour with the word "Discord". -use egui::{Color32, CornerRadius, Sense, Ui, vec2}; +use craft_ui::buttons::Button; +use craft_ui::contract::{Props, Show}; +use craft_ui::primitives::IconSpec; +use craft_ui::style::{DataColor, Variant}; +use craft::behavior::IconId; +use egui::{Color32, Ui}; -use crate::theme::{self, Tokens}; -use crate::{VectorcraftApp, icons}; +use crate::VectorcraftApp; /// Discord's brand colour (a colour, not a logo). const DISCORD: Color32 = Color32::from_rgb(0x58, 0x65, 0xF2); -/// The Discord button's label, height and width. -fn discord_layout(ui: &Ui, large: bool) -> (std::sync::Arc, f32, f32) { - let (h, font, label) = - if large { (36.0, theme::semibold(14.0), tl!("Join our Discord")) } else { (24.0, egui::FontId::proportional(12.0), "Discord") }; - let galley = ui.painter().layout_no_wrap(label.to_string(), font, Color32::PLACEHOLDER); - let w = galley.size().x + h * 0.55 + h * 0.9; - (galley, h, w) -} - -/// The Discord button's width (the app bar drops it when space runs out). -pub fn discord_width(ui: &Ui, large: bool) -> f32 { - discord_layout(ui, large).2 -} - -/// The Discord button: compact and quiet for the app bar, large for About / Home. -pub fn discord_button(app: &mut VectorcraftApp, ui: &mut Ui, large: bool) -> egui::Response { - let (galley, h, w) = discord_layout(ui, large); - let icon = h * 0.55; - let (r, resp) = ui.allocate_exact_size(vec2(w, h), Sense::click()); - let at = egui::pos2(0.0, r.center().y - galley.size().y / 2.0); - let ir = egui::Rect::from_center_size(r.left_center() + vec2(h * 0.35 + icon / 2.0, 0.0), vec2(icon, icon)); - if large { - // The theme's accent, as the primary buttons use it, so the call to action matches the - // rest of the window (#4684); Discord's colour stays on the app-bar icon. - let t = Tokens::get(ui.ctx()); - let fill = if resp.hovered() { t.accent } else { t.accent_strong }; - ui.painter().rect_filled(r, CornerRadius::same((h / 2.0) as u8), fill); - icons::paint(ui, "message-circle", ir, Color32::WHITE); - ui.painter().galley(egui::pos2(ir.right() + h * 0.2, at.y), galley, Color32::WHITE); - } else { - // In the app bar it stays quiet: a soft button with Discord's colour on its icon alone. - let t = Tokens::get(ui.ctx()); - ui.painter().rect_filled(r, CornerRadius::same(6), if resp.hovered() { t.hover } else { t.button }); - icons::paint(ui, "message-circle", ir, DISCORD); - ui.painter().galley(egui::pos2(ir.right() + h * 0.2, at.y), galley, if resp.hovered() { t.text_strong } else { t.text }); - } - let resp = resp.on_hover_text(tl!("Join the ArtCraft community on Discord (discord.gg/artcraft)")); - if resp.clicked() { +/// The Discord button: quiet, with Discord's colour on its icon alone. It opens the community link. +pub fn discord_button(app: &mut VectorcraftApp, ui: &mut Ui) { + let icon = IconSpec::svg(IconId::new("message-circle")).data_color(DataColor::new(DISCORD)); + let shown = Button::new(tl!("Discord")) + .icon(icon) + .variant(Variant::GHOST) + .tooltip(tl!("Join the ArtCraft community on Discord (discord.gg/artcraft)")) + .show(ui); + if shown.clicks > 0 { app.open_link("help.discord"); } - resp } -/// One link row: icon, label, opens the command's URL. `centered` drops the icon so the label -/// alone sits in the middle of the column. -fn link(app: &mut VectorcraftApp, ui: &mut Ui, icon: &str, label: &str, cmd: &str, url: &str, centered: bool) { - let t = Tokens::get(ui.ctx()); - let button = |ui: &mut Ui| ui.add(egui::Button::new(egui::RichText::new(label).color(t.accent).size(13.0)).frame(false)).on_hover_text(url); - let r = if centered { - button(ui) - } else { - ui.horizontal(|ui| { - icons::icon(ui, icon, 16.0, t.icon); - button(ui) - }) - .inner - }; - if r.clicked() { - app.open_link(cmd); - } -} - -/// The Discord button and the website / app page / GitHub links. -pub fn links(app: &mut VectorcraftApp, ui: &mut Ui) { - links_in(app, ui, false); -} - -/// [`links`] for a centred column (the Home screen's, #4684): the Discord button -/// and the links, each in the middle. -pub fn links_centered(app: &mut VectorcraftApp, ui: &mut Ui) { - links_in(app, ui, true); -} - -fn links_in(app: &mut VectorcraftApp, ui: &mut Ui, centered: bool) { - discord_button(app, ui, true); - ui.add_space(8.0); +/// The website, app page and GitHub links as (English label, URL), in that order: the one source +/// About and Home list them from. +pub fn link_urls(app: &VectorcraftApp) -> [(&'static str, String); 3] { let l = app.session.execute("help.links", &serde_json::json!({})).unwrap_or_default(); - let s = |k: &str| l[k].as_str().unwrap_or("").to_string(); - link(app, ui, "globe", tl!("ArtCraft website"), "help.website", &s("website"), centered); - link(app, ui, "external-link", tl!("VectorCraft on getartcraft.com"), "help.appPage", &s("appPage"), centered); - link(app, ui, "git-branch", tl!("Source code on GitHub"), "help.github", &s("github"), centered); + let url = |k: &str| l[k].as_str().unwrap_or("").to_string(); + [ + (tl!("ArtCraft website"), url("website")), + (tl!("VectorCraft on getartcraft.com"), url("appPage")), + (tl!("Source code on GitHub"), url("github")), + ] } #[cfg(test)] @@ -102,6 +48,7 @@ mod tests { use std::sync::{Arc, Mutex}; let opened = Arc::new(Mutex::new(vec![])); let o = opened.clone(); + // PLAN: hub-w3 names the opened-URL seam once `Services::open_url` is deleted. let services = crate::Services { open_url: Some(Box::new(move |u: &str| o.lock().unwrap().push(u.to_string()))), ..Default::default() }; let mut app = VectorcraftApp::new(vectorcraft_engine::Session::new(), services); for id in ["help.discord", "help.website", "help.appPage", "help.github"] { @@ -120,9 +67,17 @@ mod tests { let v = app.run("help.discord", serde_json::json!({})).unwrap(); assert_eq!(v["url"], "https://discord.gg/artcraft"); assert_eq!(opened.lock().unwrap().len(), 4); - let ctx = egui::Context::default(); - theme::install_fonts(&ctx); - let mut out = ctx.run_ui(egui::RawInput::default(), |ui| links(&mut app, ui)); - out.textures_delta.clear(); + } + + #[test] + fn link_urls_reads_the_three_help_links() { + crate::i18n::set_current(crate::i18n::Lang::EN); + let app = VectorcraftApp::new(vectorcraft_engine::Session::new(), Default::default()); + let links = link_urls(&app); + assert_eq!(links.map(|(label, _)| label), ["ArtCraft website", "VectorCraft on getartcraft.com", "Source code on GitHub"]); + for (label, url) in &links { + assert!(url.starts_with("https://"), "{label}: {url}"); + } + assert_eq!(links, link_urls(&app), "the order is stable"); } }