S019: settings pages redesign: navigation rail with sliding pill and search, cards of rows that stack when narrow, search through every row, colour section with live scheme preview and scheme swatches, limit and unavailable rows, shared chips/slider/stepper, selftest tap
Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
parent
62892f8122
commit
c0bfe16595
5 changed files with 1466 additions and 653 deletions
|
|
@ -19,6 +19,8 @@
|
|||
//! js <script> run JavaScript in the current tab; the result goes to the log
|
||||
//! click <x> <y> click in the page at CSS-ish pixel x,y (from the page's top left)
|
||||
//! pointer <x> <y> | off move the interface's pointer to x,y in the page area (new tab backgrounds follow it)
|
||||
//! tap <x> <y> click the interface (not the web page) at x,y in the window (logical px, as in a
|
||||
//! 1:1 screenshot): press, then release a frame later; for the chrome and Nacre's pages
|
||||
//! customize [close] open (or close) the new tab page's Customize panel
|
||||
//! log <text> write a line to the log
|
||||
//! quit
|
||||
|
|
@ -42,6 +44,8 @@ pub struct SelfTest {
|
|||
dir: PathBuf,
|
||||
/// A screenshot was asked for and hasn't been written yet.
|
||||
pending_shot: bool,
|
||||
/// `tap` pressed here; release once a frame has seen the press.
|
||||
pending_release: Option<egui::Pos2>,
|
||||
}
|
||||
|
||||
impl SelfTest {
|
||||
|
|
@ -50,7 +54,7 @@ impl SelfTest {
|
|||
let text = std::fs::read_to_string(&path).ok()?;
|
||||
let dir = path.parent().map(|p| p.to_owned()).unwrap_or_else(|| PathBuf::from("."));
|
||||
let lines = text.lines().map(|l| l.trim().to_owned()).filter(|l| !l.is_empty() && !l.starts_with('#')).collect();
|
||||
Some(SelfTest { lines, next: 0, wait_until: None, wait_load_until: None, dir, pending_shot: false })
|
||||
Some(SelfTest { lines, next: 0, wait_until: None, wait_load_until: None, dir, pending_shot: false, pending_release: None })
|
||||
}
|
||||
|
||||
fn log(&self, text: &str) {
|
||||
|
|
@ -73,6 +77,14 @@ impl App {
|
|||
}
|
||||
test.wait_until = None;
|
||||
}
|
||||
if let Some(pos) = test.pending_release.take() {
|
||||
test.wait_until = Some(Instant::now() + Duration::from_millis(150));
|
||||
if let Some(win) = self.windows.get_mut(&window) {
|
||||
win.egui.egui_winit.egui_input_mut().events.push(egui::Event::PointerButton { pos, button: egui::PointerButton::Primary, pressed: false, modifiers: egui::Modifiers::NONE });
|
||||
}
|
||||
self.redraw(window);
|
||||
continue;
|
||||
}
|
||||
if test.pending_shot {
|
||||
// Written by the window after its next frame.
|
||||
if let Some(shooting) = self.windows.iter().find(|(_, w)| w.shot_request.is_some()).map(|(id, _)| *id) {
|
||||
|
|
@ -298,6 +310,20 @@ impl App {
|
|||
}
|
||||
self.redraw(window);
|
||||
},
|
||||
"tap" => {
|
||||
let mut parts = rest.split_whitespace().filter_map(|p| p.parse::<f32>().ok());
|
||||
if let (Some(x), Some(y), Some(win)) = (parts.next(), parts.next(), self.windows.get_mut(&window)) {
|
||||
let pos = egui::pos2(x, y);
|
||||
let input = win.egui.egui_winit.egui_input_mut();
|
||||
input.events.push(egui::Event::PointerMoved(pos));
|
||||
input.events.push(egui::Event::PointerButton { pos, button: egui::PointerButton::Primary, pressed: true, modifiers: egui::Modifiers::NONE });
|
||||
if let Some(t) = self.selftest.as_mut() {
|
||||
t.pending_release = Some(pos);
|
||||
t.wait_until = Some(Instant::now() + Duration::from_millis(150));
|
||||
}
|
||||
}
|
||||
self.redraw(window);
|
||||
},
|
||||
"log" => self.selftest_log(rest),
|
||||
"quit" => self.quit(),
|
||||
_ => self.selftest_log(&format!("unknown line: {line}")),
|
||||
|
|
|
|||
|
|
@ -162,6 +162,15 @@ pub const WAVING_HAND: &str = "\u{e766}";
|
|||
pub const GRAIN: &str = "\u{e3ea}";
|
||||
pub const GIF_BOX: &str = "\u{e7a3}";
|
||||
pub const BRIGHTNESS_6: &str = "\u{e3ab}";
|
||||
pub const TOOLBAR: &str = "\u{e9f7}";
|
||||
pub const KEYBOARD: &str = "\u{e312}";
|
||||
pub const SPELLCHECK: &str = "\u{e8ce}";
|
||||
pub const INPUT: &str = "\u{e890}";
|
||||
pub const DASHBOARD_CUSTOMIZE: &str = "\u{e99b}";
|
||||
pub const MEMORY: &str = "\u{e322}";
|
||||
pub const COLORIZE: &str = "\u{e3b8}";
|
||||
pub const FORMAT_COLOR_FILL: &str = "\u{e23a}";
|
||||
pub const RESTART_ALT: &str = "\u{f053}";
|
||||
|
||||
/// Every icon with its name, for the gallery.
|
||||
pub const ALL: &[(&str, &str)] = &[
|
||||
|
|
@ -326,4 +335,13 @@ pub const ALL: &[(&str, &str)] = &[
|
|||
("grain", GRAIN),
|
||||
("gif_box", GIF_BOX),
|
||||
("brightness_6", BRIGHTNESS_6),
|
||||
("toolbar", TOOLBAR),
|
||||
("keyboard", KEYBOARD),
|
||||
("spellcheck", SPELLCHECK),
|
||||
("input", INPUT),
|
||||
("dashboard_customize", DASHBOARD_CUSTOMIZE),
|
||||
("memory", MEMORY),
|
||||
("colorize", COLORIZE),
|
||||
("format_color_fill", FORMAT_COLOR_FILL),
|
||||
("restart_alt", RESTART_ALT),
|
||||
];
|
||||
|
|
|
|||
|
|
@ -5,7 +5,7 @@ use egui::{Align2, Id, Rect, RichText, Sense, Stroke, StrokeKind, Ui, Vec2, pos2
|
|||
|
||||
use super::super::Frame;
|
||||
use super::super::menus::one_line;
|
||||
use super::super::widgets::{ButtonKind, button, focus_ring, icon, icon_button, interaction, segmented, switch};
|
||||
use super::super::widgets::{ButtonKind, button, chip, focus_ring, icon, icon_button, interaction, segmented, slider, switch};
|
||||
use crate::actions::Command;
|
||||
use crate::store::settings::{Ntp, NtpBackground};
|
||||
use crate::theme::{self, Weight};
|
||||
|
|
@ -160,81 +160,6 @@ fn line<R>(ui: &mut Ui, glyph: &str, label: &str, control: impl FnOnce(&mut Ui)
|
|||
.inner
|
||||
}
|
||||
|
||||
/// A filter chip: icon and label in an outlined pill; the selected one is filled.
|
||||
fn chip(ui: &mut Ui, glyph: &str, label: &str, selected: bool) -> egui::Response {
|
||||
let p = theme::palette(ui.ctx());
|
||||
let font = theme::font(theme::BODY, Weight::Medium);
|
||||
let galley = ui.painter().layout_no_wrap(label.to_owned(), font, p.on_surface);
|
||||
let size = vec2(theme::GAP_M + 18.0 + theme::GAP_S + galley.size().x + theme::GAP_L, theme::CONTROL_SMALL + 4.0);
|
||||
let (rect, response) = ui.allocate_exact_size(size, Sense::click());
|
||||
let s = interaction(ui, &response);
|
||||
let on = theme::effect(ui.ctx(), response.id.with("on"), selected);
|
||||
let radius = theme::radius(ui.ctx(), theme::S);
|
||||
let container = theme::layer(p.surface_container_high, p.secondary_container, on);
|
||||
let content = theme::layer(p.on_surface_variant, p.on_secondary_container, on);
|
||||
ui.painter().rect_filled(rect, theme::corner_radius(radius), s.layer(container, content));
|
||||
if on < 0.99 {
|
||||
ui.painter().rect_stroke(rect, theme::corner_radius(radius), Stroke::new(1.0, p.outline_variant.gamma_multiply(1.0 - on)), StrokeKind::Inside);
|
||||
}
|
||||
icon(ui, pos2(rect.left() + theme::GAP_M + 9.0, rect.center().y), glyph, 18.0, on > 0.5, content);
|
||||
ui.painter().galley(pos2(rect.left() + theme::GAP_M + 18.0 + theme::GAP_S, rect.center().y - galley.size().y / 2.0), galley, content);
|
||||
if s.focus {
|
||||
focus_ring(ui, rect, radius, &p);
|
||||
}
|
||||
response
|
||||
}
|
||||
|
||||
/// An M3 Expressive slider: a thick track, the active part in the accent, a bar for a handle with a gap round
|
||||
/// it. Drag or click to set; arrow keys step by a twentieth. Returns true when the value changed.
|
||||
fn slider(ui: &mut Ui, value: &mut f32, range: std::ops::RangeInclusive<f32>, width: f32, tooltip: &str) -> bool {
|
||||
let p = theme::palette(ui.ctx());
|
||||
let (rect, response) = ui.allocate_exact_size(vec2(width, theme::CONTROL), Sense::click_and_drag());
|
||||
let (lo, hi) = (*range.start(), *range.end());
|
||||
let mut fraction = ((*value - lo) / (hi - lo)).clamp(0.0, 1.0);
|
||||
let before = fraction;
|
||||
let track = rect.shrink2(vec2(2.0, 0.0));
|
||||
if (response.dragged() || response.clicked())
|
||||
&& let Some(at) = response.interact_pointer_pos()
|
||||
{
|
||||
fraction = ((at.x - track.left()) / track.width()).clamp(0.0, 1.0);
|
||||
}
|
||||
if response.has_focus() {
|
||||
let step = ui.input(|i| i.key_pressed(egui::Key::ArrowRight) as i32 - i.key_pressed(egui::Key::ArrowLeft) as i32);
|
||||
fraction = (fraction + step as f32 * 0.05).clamp(0.0, 1.0);
|
||||
}
|
||||
let changed = fraction != before;
|
||||
if changed {
|
||||
*value = lo + fraction * (hi - lo);
|
||||
}
|
||||
let s = interaction(ui, &response);
|
||||
let enabled = s.enabled;
|
||||
let dim = |c: egui::Color32, k: f32| if enabled { c } else { p.on_surface.gamma_multiply(k) };
|
||||
let x = track.left() + fraction * track.width();
|
||||
let thickness = 16.0;
|
||||
let gap = 6.0;
|
||||
let handle_width = 4.0 - 2.0 * s.press.clamp(0.0, 1.0);
|
||||
let r = theme::corner_radius(thickness / 2.0);
|
||||
let y = track.center().y;
|
||||
let active = Rect::from_min_max(pos2(track.left(), y - thickness / 2.0), pos2((x - gap).max(track.left()), y + thickness / 2.0));
|
||||
let rest = Rect::from_min_max(pos2((x + gap).min(track.right()), y - thickness / 2.0), pos2(track.right(), y + thickness / 2.0));
|
||||
if active.width() > 0.5 {
|
||||
ui.painter().rect_filled(active, r, dim(p.primary, theme::DISABLED_CONTENT));
|
||||
}
|
||||
if rest.width() > 0.5 {
|
||||
ui.painter().rect_filled(rest, r, dim(p.secondary_container, theme::DISABLED_CONTAINER));
|
||||
}
|
||||
let handle = Rect::from_center_size(pos2(x, y), vec2(handle_width, 36.0));
|
||||
if s.amount() > 0.0 {
|
||||
ui.painter().rect_filled(handle.expand(4.0), theme::corner_radius(6.0), p.primary.gamma_multiply(s.amount()));
|
||||
}
|
||||
ui.painter().rect_filled(handle, theme::corner_radius(2.0), dim(p.primary, theme::DISABLED_CONTENT));
|
||||
if s.focus {
|
||||
focus_ring(ui, handle.expand(4.0), 6.0, &p);
|
||||
}
|
||||
response.on_hover_text(tooltip);
|
||||
changed
|
||||
}
|
||||
|
||||
fn background_section(ui: &mut Ui, ntp: &mut Ntp) {
|
||||
heading(ui, "Background");
|
||||
ui.horizontal_wrapped(|ui| {
|
||||
|
|
@ -242,7 +167,7 @@ fn background_section(ui: &mut Ui, ntp: &mut Ntp) {
|
|||
// The order of the video, None first (the enum's first variant is the default, Shapes).
|
||||
let order = [NtpBackground::Off, NtpBackground::Shapes, NtpBackground::Blobs, NtpBackground::Particles, NtpBackground::Waves, NtpBackground::Dots];
|
||||
for bg in order {
|
||||
if chip(ui, background_icon(bg), bg.label(), ntp.bg == bg).clicked() {
|
||||
if chip(ui, Some(background_icon(bg)), bg.label(), ntp.bg == bg).clicked() {
|
||||
ntp.bg = bg;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
File diff suppressed because it is too large
Load diff
|
|
@ -238,46 +238,164 @@ fn first_row_id(ui: &Ui) -> Id {
|
|||
ui.id().with("first-row")
|
||||
}
|
||||
|
||||
/// A setting: title and description on the left, the control on the right.
|
||||
pub fn row<R>(ui: &mut Ui, title: &str, description: &str, control: impl FnOnce(&mut Ui) -> R) -> R {
|
||||
row_frame(ui, title, description, None, control).0
|
||||
/// Rows whose title or description contains this text (lower case) are highlighted and counted.
|
||||
fn highlight_id() -> Id {
|
||||
Id::new("row-highlight")
|
||||
}
|
||||
|
||||
fn row_frame<R>(ui: &mut Ui, title: &str, description: &str, click: Option<Id>, control: impl FnOnce(&mut Ui) -> R) -> (R, Option<Response>) {
|
||||
/// Highlight rows matching `query` from now on (empty: none). `highlighted` counts the matches since.
|
||||
pub fn highlight_rows(ctx: &egui::Context, query: &str) {
|
||||
ctx.data_mut(|d| {
|
||||
d.insert_temp(highlight_id(), query.to_lowercase());
|
||||
d.insert_temp(highlight_id().with("count"), 0usize);
|
||||
});
|
||||
}
|
||||
|
||||
pub fn highlighted(ctx: &egui::Context) -> usize {
|
||||
ctx.data(|d| d.get_temp(highlight_id().with("count"))).unwrap_or(0)
|
||||
}
|
||||
|
||||
/// What a row shows besides its control.
|
||||
#[derive(Clone, Copy, Default)]
|
||||
struct RowText<'a> {
|
||||
title: &'a str,
|
||||
description: &'a str,
|
||||
/// Leading icon.
|
||||
lead: Option<&'a str>,
|
||||
/// What the engine can't do for this setting, under the description.
|
||||
note: Option<&'a str>,
|
||||
/// The control always goes under the text (chips and lists that wrap).
|
||||
below: bool,
|
||||
}
|
||||
|
||||
/// Text needs at least this much room beside a control; with less the control goes under the text.
|
||||
const ROW_TEXT_MIN: f32 = 220.0;
|
||||
|
||||
/// A setting: title and description on the left, the control on the right.
|
||||
pub fn row<R>(ui: &mut Ui, title: &str, description: &str, control: impl FnOnce(&mut Ui) -> R) -> R {
|
||||
row_frame(ui, RowText { title, description, ..Default::default() }, None, control).0
|
||||
}
|
||||
|
||||
/// A row with a leading icon.
|
||||
pub fn icon_row<R>(ui: &mut Ui, glyph: &str, title: &str, description: &str, control: impl FnOnce(&mut Ui) -> R) -> R {
|
||||
row_frame(ui, RowText { title, description, lead: Some(glyph), ..Default::default() }, None, control).0
|
||||
}
|
||||
|
||||
/// A row whose control sits under the text, full width: wrapping chips, lists, wide fields.
|
||||
pub fn wide_row<R>(ui: &mut Ui, title: &str, description: &str, control: impl FnOnce(&mut Ui) -> R) -> R {
|
||||
row_frame(ui, RowText { title, description, below: true, ..Default::default() }, None, control).0
|
||||
}
|
||||
|
||||
/// A setting the engine only partly supports: `limit` says what doesn't work, under the description.
|
||||
pub fn limited_row<R>(ui: &mut Ui, title: &str, description: &str, limit: &str, control: impl FnOnce(&mut Ui) -> R) -> R {
|
||||
row_frame(ui, RowText { title, description, note: Some(limit), ..Default::default() }, None, control).0
|
||||
}
|
||||
|
||||
/// A setting the engine can't do at all: kept in its place with the reason and a "Not available" label.
|
||||
pub fn unavailable_row(ui: &mut Ui, title: &str, reason: &str) {
|
||||
let p = theme::palette(ui.ctx());
|
||||
row_frame(ui, RowText { title, description: reason, ..Default::default() }, None, |ui| {
|
||||
let font = theme::font(theme::SMALL, Weight::Medium);
|
||||
let galley = ui.painter().layout_no_wrap("Not available".to_owned(), font, p.on_surface_variant);
|
||||
let (rect, _) = ui.allocate_exact_size(vec2(theme::GAP_M + 16.0 + theme::GAP_XS + galley.size().x + theme::GAP_M, theme::CONTROL_SMALL - 4.0), Sense::hover());
|
||||
let r = theme::corner_radius(theme::radius(ui.ctx(), theme::S));
|
||||
ui.painter().rect_stroke(rect, r, Stroke::new(1.0, p.outline_variant), StrokeKind::Inside);
|
||||
icon(ui, pos2(rect.left() + theme::GAP_M + 8.0, rect.center().y), icons::BLOCK, 16.0, false, p.on_surface_variant);
|
||||
ui.painter().galley(pos2(rect.left() + theme::GAP_M + 16.0 + theme::GAP_XS, rect.center().y - galley.size().y / 2.0), galley, p.on_surface_variant);
|
||||
});
|
||||
}
|
||||
|
||||
fn row_frame<R>(ui: &mut Ui, text: RowText, click: Option<Id>, control: impl FnOnce(&mut Ui) -> R) -> (R, Option<Response>) {
|
||||
let p = theme::palette(ui.ctx());
|
||||
let first = ui.data_mut(|d| d.remove_temp::<bool>(first_row_id(ui))).unwrap_or(false);
|
||||
let query: String = ui.data(|d| d.get_temp(highlight_id())).unwrap_or_default();
|
||||
let matched = !query.is_empty() && (text.title.to_lowercase().contains(&query) || text.description.to_lowercase().contains(&query));
|
||||
if matched {
|
||||
ui.data_mut(|d| *d.get_temp_mut_or(highlight_id().with("count"), 0usize) += 1);
|
||||
}
|
||||
let background = ui.painter().add(Shape::Noop);
|
||||
// The control's width from the last frame decides whether it fits beside the text.
|
||||
let width_id = ui.id().with(("row-control", text.title));
|
||||
let known: Option<Vec2> = ui.data(|d| d.get_temp(width_id));
|
||||
let lead = if text.lead.is_some() { theme::ICON + theme::GAP_L } else { 0.0 };
|
||||
let inner_width = ui.available_width() - 2.0 * theme::GAP_L - lead;
|
||||
let control_width = known.map_or(260.0, |k| k.x);
|
||||
let control_height = known.map_or(0.0, |k| k.y);
|
||||
let stacked = text.below || control_width > inner_width - ROW_TEXT_MIN;
|
||||
let mut result = None;
|
||||
let mut measured = Vec2::ZERO;
|
||||
// Runs in one of two places below.
|
||||
let mut control = Some(control);
|
||||
let lines = 20.0 + if text.description.is_empty() { 0.0 } else { 16.0 } + if text.note.is_some() { 16.0 } else { 0.0 };
|
||||
let inner = egui::Frame::NONE.inner_margin(egui::Margin::symmetric(theme::GAP_L as i8, theme::GAP_M as i8)).show(ui, |ui| {
|
||||
ui.set_min_height(theme::ROW - 2.0 * theme::GAP_M);
|
||||
ui.set_min_width(ui.available_width());
|
||||
ui.horizontal(|ui| {
|
||||
if lead > 0.0 {
|
||||
ui.add_space(lead - ui.spacing().item_spacing.x);
|
||||
}
|
||||
ui.vertical(|ui| {
|
||||
ui.set_max_width((ui.available_width() - 260.0).max(200.0));
|
||||
ui.add_space((theme::ROW - 2.0 * theme::GAP_M - if description.is_empty() { 20.0 } else { 36.0 }).max(0.0) / 2.0);
|
||||
ui.label(RichText::new(title).font(theme::font(theme::BODY, Weight::Regular)).color(p.on_surface));
|
||||
if !description.is_empty() {
|
||||
ui.label(RichText::new(description).font(theme::font(theme::SMALL, Weight::Regular)).color(p.on_surface_variant));
|
||||
let width = if stacked { ui.available_width() } else { (ui.available_width() - control_width - theme::GAP_L).max(ROW_TEXT_MIN) };
|
||||
ui.set_max_width(width);
|
||||
if !stacked {
|
||||
ui.add_space(((theme::ROW - 2.0 * theme::GAP_M).max(control_height) - lines).max(0.0) / 2.0);
|
||||
}
|
||||
ui.label(RichText::new(text.title).font(theme::font(theme::BODY, Weight::Regular)).color(p.on_surface));
|
||||
if !text.description.is_empty() {
|
||||
ui.label(RichText::new(text.description).font(theme::font(theme::SMALL, Weight::Regular)).color(p.on_surface_variant));
|
||||
}
|
||||
if let Some(note) = text.note {
|
||||
let mut job = egui::text::LayoutJob::default();
|
||||
let format = |color| egui::TextFormat { font_id: theme::font(theme::SMALL, Weight::Regular), color, ..Default::default() };
|
||||
job.append(&format!("{} ", icons::INFO), 0.0, format(p.tertiary));
|
||||
job.append(note, 0.0, format(p.on_surface_variant));
|
||||
ui.label(job);
|
||||
}
|
||||
if stacked {
|
||||
ui.add_space(theme::GAP_S);
|
||||
result = control.take().map(|c| ui.horizontal_wrapped(|ui| c(ui)).inner);
|
||||
}
|
||||
});
|
||||
ui.with_layout(egui::Layout::right_to_left(egui::Align::Center), |ui| {
|
||||
result = Some(control(ui));
|
||||
});
|
||||
if !stacked {
|
||||
ui.with_layout(egui::Layout::right_to_left(egui::Align::Center), |ui| {
|
||||
let right = ui.max_rect().right();
|
||||
result = control.take().map(|c| c(ui));
|
||||
measured = vec2(right - ui.min_rect().left(), ui.min_rect().height());
|
||||
});
|
||||
}
|
||||
});
|
||||
});
|
||||
// Remember the control's natural width (measured beside the text); redo the frame unseen when the control
|
||||
// turns out not to fit, so it moves under the text without a flicker.
|
||||
if !stacked && known.is_none_or(|k| (k - measured).length() > 1.0) {
|
||||
ui.data_mut(|d| d.insert_temp(width_id, measured));
|
||||
if known.is_none() || measured.x > inner_width - ROW_TEXT_MIN {
|
||||
ui.ctx().request_discard("row control moves under its text");
|
||||
}
|
||||
}
|
||||
let rect = inner.response.rect;
|
||||
if !first {
|
||||
ui.painter().hline(rect.x_range().shrink(theme::GAP_L), rect.top(), Stroke::new(1.0, p.outline_variant));
|
||||
}
|
||||
if let Some(glyph) = text.lead {
|
||||
let y = if stacked { rect.top() + theme::GAP_M + 10.0 } else { rect.center().y };
|
||||
icon(ui, pos2(rect.left() + theme::GAP_L + theme::ICON / 2.0, y), glyph, theme::ICON, false, p.on_surface_variant);
|
||||
}
|
||||
let highlight = if matched { theme::layer(p.surface_container, p.secondary_container, 0.7) } else { Color32::TRANSPARENT };
|
||||
let radius = theme::corner_radius(theme::radius(ui.ctx(), theme::M));
|
||||
let response = click.map(|id| {
|
||||
let response = ui.interact(rect, id, Sense::click());
|
||||
let s = interaction(ui, &response);
|
||||
let tint = p.on_surface.gamma_multiply(s.amount());
|
||||
ui.painter().set(background, Shape::rect_filled(rect, theme::corner_radius(theme::radius(ui.ctx(), theme::XS)), tint));
|
||||
let fill = if matched { s.layer(highlight, p.on_surface) } else { p.on_surface.gamma_multiply(s.amount()) };
|
||||
ui.painter().set(background, Shape::rect_filled(rect.shrink2(vec2(theme::GAP_XS, 2.0)), radius, fill));
|
||||
if s.focus {
|
||||
focus_ring(ui, rect.shrink(2.0), theme::radius(ui.ctx(), theme::XS), &p);
|
||||
focus_ring(ui, rect.shrink(2.0), theme::radius(ui.ctx(), theme::M), &p);
|
||||
}
|
||||
response
|
||||
});
|
||||
if response.is_none() && matched {
|
||||
ui.painter().set(background, Shape::rect_filled(rect.shrink2(vec2(theme::GAP_XS, 2.0)), radius, highlight));
|
||||
}
|
||||
(result.expect("control ran"), response)
|
||||
}
|
||||
|
||||
|
|
@ -288,9 +406,18 @@ pub fn switch_row(ui: &mut Ui, title: &str, description: &str, on: &mut bool) ->
|
|||
|
||||
/// A row that opens something: the whole row is the button, with a chevron at the end.
|
||||
pub fn link_row(ui: &mut Ui, title: &str, description: &str) -> bool {
|
||||
link(ui, RowText { title, description, ..Default::default() })
|
||||
}
|
||||
|
||||
/// `link_row` with a leading icon.
|
||||
pub fn icon_link_row(ui: &mut Ui, glyph: &str, title: &str, description: &str) -> bool {
|
||||
link(ui, RowText { title, description, lead: Some(glyph), ..Default::default() })
|
||||
}
|
||||
|
||||
fn link(ui: &mut Ui, text: RowText) -> bool {
|
||||
let p = theme::palette(ui.ctx());
|
||||
let id = ui.id().with("link-row").with(title);
|
||||
let (_, response) = row_frame(ui, title, description, Some(id), |ui| {
|
||||
let id = ui.id().with("link-row").with(text.title);
|
||||
let (_, response) = row_frame(ui, text, Some(id), |ui| {
|
||||
let (rect, _) = ui.allocate_exact_size(Vec2::splat(24.0), Sense::hover());
|
||||
icon(ui, rect.center(), icons::CHEVRON_RIGHT, theme::ICON, false, p.on_surface_variant);
|
||||
});
|
||||
|
|
@ -353,9 +480,190 @@ pub fn text_field(ui: &mut Ui, text: &mut String, hint: &str, width: f32) -> Res
|
|||
ui.add(egui::TextEdit::singleline(text).hint_text(hint).desired_width(width).margin(vec2(theme::GAP_M, theme::GAP_S)))
|
||||
}
|
||||
|
||||
/// A number field limited to `range`. Returns true when it changed.
|
||||
/// A number with − and + buttons either side; the number itself can be dragged or typed. Returns true when it
|
||||
/// changed.
|
||||
pub fn number(ui: &mut Ui, value: &mut u32, range: std::ops::RangeInclusive<u32>, suffix: &str) -> bool {
|
||||
ui.add(egui::DragValue::new(value).range(range).suffix(suffix)).changed()
|
||||
let before = *value;
|
||||
let (lo, hi) = (*range.start(), *range.end());
|
||||
// Steps that reach the far end in about 30 presses.
|
||||
let step = match hi - lo {
|
||||
0..=60 => 1,
|
||||
61..=300 => 5,
|
||||
301..=900 => 15,
|
||||
_ => 30,
|
||||
};
|
||||
ui.scope(|ui| {
|
||||
ui.spacing_mut().item_spacing.x = theme::GAP_XS;
|
||||
// Laid out right to left inside rows, so the + comes first.
|
||||
let rtl = ui.layout().prefer_right_to_left();
|
||||
let plus = |ui: &mut Ui, value: &mut u32| {
|
||||
if ui.add_enabled_ui(*value < hi, |ui| icon_button(ui, icons::ADD, "More")).inner.clicked() {
|
||||
*value = (*value + step - *value % step).min(hi);
|
||||
}
|
||||
};
|
||||
if rtl {
|
||||
plus(ui, value);
|
||||
} else {
|
||||
minus(ui, value, lo, step);
|
||||
}
|
||||
// One width for every stepper, so the buttons line up from row to row.
|
||||
ui.add_sized(vec2(112.0, theme::CONTROL_SMALL), egui::DragValue::new(value).range(range.clone()).suffix(suffix));
|
||||
if rtl {
|
||||
minus(ui, value, lo, step);
|
||||
} else {
|
||||
plus(ui, value);
|
||||
}
|
||||
});
|
||||
*value != before
|
||||
}
|
||||
|
||||
fn minus(ui: &mut Ui, value: &mut u32, lo: u32, step: u32) {
|
||||
if ui.add_enabled_ui(*value > lo, |ui| icon_button(ui, icons::REMOVE, "Less")).inner.clicked() {
|
||||
let down = if *value % step == 0 { step } else { *value % step };
|
||||
*value = value.saturating_sub(down).max(lo);
|
||||
}
|
||||
}
|
||||
|
||||
/// A filter chip: icon (optional) and label in an outlined pill; the selected one is filled.
|
||||
pub fn chip(ui: &mut Ui, glyph: Option<&str>, label: &str, selected: bool) -> Response {
|
||||
let p = theme::palette(ui.ctx());
|
||||
let font = theme::font(theme::BODY, Weight::Medium);
|
||||
let galley = ui.painter().layout_no_wrap(label.to_owned(), font, p.on_surface);
|
||||
let lead = if glyph.is_some() { 18.0 + theme::GAP_S } else { 0.0 };
|
||||
let size = vec2(theme::GAP_M + lead + galley.size().x + if glyph.is_some() { theme::GAP_L } else { theme::GAP_M }, theme::CONTROL_SMALL + 4.0);
|
||||
let (rect, response) = ui.allocate_exact_size(size, Sense::click());
|
||||
if !ui.is_rect_visible(rect) {
|
||||
return response;
|
||||
}
|
||||
let s = interaction(ui, &response);
|
||||
let on = theme::effect(ui.ctx(), response.id.with("on"), selected);
|
||||
let radius = theme::radius(ui.ctx(), theme::S);
|
||||
let container = theme::layer(p.surface_container_high, p.secondary_container, on);
|
||||
let content = theme::layer(p.on_surface_variant, p.on_secondary_container, on);
|
||||
let content = disabled(content, s.enabled, theme::DISABLED_CONTENT);
|
||||
ui.painter().rect_filled(rect, theme::corner_radius(radius), s.layer(container, content));
|
||||
if on < 0.99 {
|
||||
ui.painter().rect_stroke(rect, theme::corner_radius(radius), Stroke::new(1.0, p.outline_variant.gamma_multiply(1.0 - on)), StrokeKind::Inside);
|
||||
}
|
||||
if let Some(glyph) = glyph {
|
||||
icon(ui, pos2(rect.left() + theme::GAP_M + 9.0, rect.center().y), glyph, 18.0, on > 0.5, content);
|
||||
}
|
||||
ui.painter().galley(pos2(rect.left() + theme::GAP_M + lead, rect.center().y - galley.size().y / 2.0), galley, content);
|
||||
if s.focus {
|
||||
focus_ring(ui, rect, radius, &p);
|
||||
}
|
||||
response
|
||||
}
|
||||
|
||||
/// One of `options` as a wrapping row of chips, the chosen one with a check. For lists too long for a segmented
|
||||
/// button. Returns true when it changed.
|
||||
pub fn choice_chips<T: Clone + PartialEq>(ui: &mut Ui, value: &mut T, options: &[(T, &str)]) -> bool {
|
||||
let mut changed = false;
|
||||
ui.horizontal_wrapped(|ui| {
|
||||
ui.spacing_mut().item_spacing = vec2(theme::GAP_S, theme::GAP_S);
|
||||
for (option, label) in options {
|
||||
let selected = *value == *option;
|
||||
if chip(ui, selected.then_some(icons::CHECK), label, selected).clicked() && !selected {
|
||||
*value = option.clone();
|
||||
changed = true;
|
||||
}
|
||||
}
|
||||
});
|
||||
changed
|
||||
}
|
||||
|
||||
/// Any of `options` (keys in `chosen`) as toggling chips. Returns true when it changed.
|
||||
pub fn toggle_chips(ui: &mut Ui, chosen: &mut Vec<String>, options: &[(&str, &str)]) -> bool {
|
||||
let mut changed = false;
|
||||
ui.horizontal_wrapped(|ui| {
|
||||
ui.spacing_mut().item_spacing = vec2(theme::GAP_S, theme::GAP_S);
|
||||
for (key, label) in options {
|
||||
let on = chosen.iter().any(|k| k == key);
|
||||
if chip(ui, on.then_some(icons::CHECK), label, on).clicked() {
|
||||
if on {
|
||||
chosen.retain(|k| k != key);
|
||||
} else {
|
||||
chosen.push((*key).to_owned());
|
||||
}
|
||||
changed = true;
|
||||
}
|
||||
}
|
||||
});
|
||||
changed
|
||||
}
|
||||
|
||||
/// An input chip: a label with a × that removes it. Returns true when the × was clicked.
|
||||
pub fn input_chip(ui: &mut Ui, label: &str, remove_tooltip: &str) -> bool {
|
||||
let p = theme::palette(ui.ctx());
|
||||
let font = theme::font(theme::BODY, Weight::Regular);
|
||||
let galley = ui.painter().layout_no_wrap(label.to_owned(), font, p.on_surface_variant);
|
||||
let size = vec2(theme::GAP_M + galley.size().x + theme::GAP_XS + 24.0 + theme::GAP_XS, theme::CONTROL_SMALL);
|
||||
let (rect, _) = ui.allocate_exact_size(size, Sense::hover());
|
||||
let radius = theme::corner_radius(theme::radius(ui.ctx(), theme::S));
|
||||
ui.painter().rect_stroke(rect, radius, Stroke::new(1.0, p.outline_variant), StrokeKind::Inside);
|
||||
ui.painter().galley(pos2(rect.left() + theme::GAP_M, rect.center().y - galley.size().y / 2.0), galley, p.on_surface_variant);
|
||||
let x = Rect::from_center_size(pos2(rect.right() - theme::GAP_XS - 12.0, rect.center().y), Vec2::splat(24.0));
|
||||
let response = ui.interact(x, ui.id().with(("input-chip", label)), Sense::click());
|
||||
let s = interaction(ui, &response);
|
||||
if s.amount() > 0.0 {
|
||||
ui.painter().circle_filled(x.center(), 12.0, p.on_surface.gamma_multiply(s.amount()));
|
||||
}
|
||||
icon(ui, x.center(), icons::CLOSE, 16.0, false, p.on_surface_variant);
|
||||
if s.focus {
|
||||
focus_ring(ui, x, 12.0, &p);
|
||||
}
|
||||
response.on_hover_text(remove_tooltip).clicked()
|
||||
}
|
||||
|
||||
/// An M3 Expressive slider: a thick track, the active part in the accent, a bar for a handle with a gap round
|
||||
/// it. Drag or click to set; arrow keys step by a twentieth. Returns true when the value changed.
|
||||
pub fn slider(ui: &mut Ui, value: &mut f32, range: std::ops::RangeInclusive<f32>, width: f32, tooltip: &str) -> bool {
|
||||
let p = theme::palette(ui.ctx());
|
||||
let (rect, response) = ui.allocate_exact_size(vec2(width, theme::CONTROL), Sense::click_and_drag());
|
||||
let (lo, hi) = (*range.start(), *range.end());
|
||||
let mut fraction = ((*value - lo) / (hi - lo)).clamp(0.0, 1.0);
|
||||
let before = fraction;
|
||||
let track = rect.shrink2(vec2(2.0, 0.0));
|
||||
if (response.dragged() || response.clicked())
|
||||
&& let Some(at) = response.interact_pointer_pos()
|
||||
{
|
||||
fraction = ((at.x - track.left()) / track.width()).clamp(0.0, 1.0);
|
||||
}
|
||||
if response.has_focus() {
|
||||
let step = ui.input(|i| i.key_pressed(egui::Key::ArrowRight) as i32 - i.key_pressed(egui::Key::ArrowLeft) as i32);
|
||||
fraction = (fraction + step as f32 * 0.05).clamp(0.0, 1.0);
|
||||
}
|
||||
let changed = fraction != before;
|
||||
if changed {
|
||||
*value = lo + fraction * (hi - lo);
|
||||
}
|
||||
let s = interaction(ui, &response);
|
||||
let enabled = s.enabled;
|
||||
let dim = |c: Color32, k: f32| if enabled { c } else { p.on_surface.gamma_multiply(k) };
|
||||
let x = track.left() + fraction * track.width();
|
||||
let thickness = 16.0;
|
||||
let gap = 6.0;
|
||||
let handle_width = 4.0 - 2.0 * s.press.clamp(0.0, 1.0);
|
||||
let r = theme::corner_radius(thickness / 2.0);
|
||||
let y = track.center().y;
|
||||
let active = Rect::from_min_max(pos2(track.left(), y - thickness / 2.0), pos2((x - gap).max(track.left()), y + thickness / 2.0));
|
||||
let rest = Rect::from_min_max(pos2((x + gap).min(track.right()), y - thickness / 2.0), pos2(track.right(), y + thickness / 2.0));
|
||||
if active.width() > 0.5 {
|
||||
ui.painter().rect_filled(active, r, dim(p.primary, theme::DISABLED_CONTENT));
|
||||
}
|
||||
if rest.width() > 0.5 {
|
||||
ui.painter().rect_filled(rest, r, dim(p.secondary_container, theme::DISABLED_CONTAINER));
|
||||
}
|
||||
let handle = Rect::from_center_size(pos2(x, y), vec2(handle_width, 36.0));
|
||||
if s.amount() > 0.0 {
|
||||
ui.painter().rect_filled(handle.expand(4.0), theme::corner_radius(6.0), p.primary.gamma_multiply(s.amount()));
|
||||
}
|
||||
ui.painter().rect_filled(handle, theme::corner_radius(2.0), dim(p.primary, theme::DISABLED_CONTENT));
|
||||
if s.focus {
|
||||
focus_ring(ui, handle.expand(4.0), 6.0, &p);
|
||||
}
|
||||
response.on_hover_text(tooltip);
|
||||
changed
|
||||
}
|
||||
|
||||
/// Icon for a toolbar button key.
|
||||
|
|
@ -449,6 +757,32 @@ mod tests {
|
|||
assert_eq!(value, 2);
|
||||
}
|
||||
|
||||
/// The stepper's + moves to the next step (15 min steps to 30 in 0..=1440), − back down, and both stop at the range.
|
||||
#[test]
|
||||
fn number_steps_and_clamps() {
|
||||
let ctx = egui::Context::default();
|
||||
let mut value = 15;
|
||||
let mut draw = |ui: &mut Ui| {
|
||||
number(ui, &mut value, 0..=1440, " min");
|
||||
};
|
||||
let rect = frame(&ctx, 0.0, vec![], &mut draw);
|
||||
let plus = egui::pos2(rect.right() - theme::CONTROL_SMALL / 2.0, rect.center().y);
|
||||
let minus = egui::pos2(rect.left() + theme::CONTROL_SMALL / 2.0, rect.center().y);
|
||||
let mut t = 0.0;
|
||||
let mut click = |pos, draw: &mut dyn FnMut(&mut Ui)| {
|
||||
for events in click_at(pos) {
|
||||
t += 0.02;
|
||||
frame(&ctx, t, events, draw);
|
||||
}
|
||||
};
|
||||
click(plus, &mut draw);
|
||||
click(minus, &mut draw);
|
||||
click(minus, &mut draw);
|
||||
click(minus, &mut draw);
|
||||
drop(draw);
|
||||
assert_eq!(value, 0, "15 → 30 → 0 → stays at 0");
|
||||
}
|
||||
|
||||
/// Hover fades in with the effect spring and a press squeezes, both settling back once the pointer leaves.
|
||||
#[test]
|
||||
fn hover_and_press_animate_and_settle() {
|
||||
|
|
|
|||
Loading…
Reference in a new issue