mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-26 00:57:26 +00:00
Preferences: one scrolling page of cards, segmented short choices, a search with its match count (L-311)
This commit is contained in:
@@ -1,34 +1,23 @@
|
||||
//! The contents of the preferences window: a search field over a page list and the shown page.
|
||||
//! The contents of the preferences window: a search field with its match count over one scrolling page of cards.
|
||||
|
||||
use super::model::{PageSpec, PrefValue, PreferencesModel};
|
||||
use super::rows::{RowsOut, ShownRow, reset_button, show_row};
|
||||
use super::search::{self, Listed};
|
||||
use super::window::PreferencesState;
|
||||
use super::words::PreferencesWords;
|
||||
use crate::contract::{
|
||||
ActivateOn, Bind, CollectionEvent, Props as _, Scroll, SelectMode, Show as _, ShowCollection as _, ShowContainer as _, Sizing,
|
||||
};
|
||||
use crate::display::{Divider, Shade, Text};
|
||||
use crate::contract::{Bind, Pad, Props as _, Scroll, Show as _, ShowContainer as _};
|
||||
use crate::display::{Shade, Text};
|
||||
use crate::form::FieldGroup;
|
||||
use crate::layout_components::{Row, Stack};
|
||||
use crate::list::{List, ListRow, RowInk};
|
||||
use crate::notices::{Notice, NoticeLayout};
|
||||
use crate::runtime::env::{Label, Units};
|
||||
use crate::runtime::env::{Env, Label, Units};
|
||||
use crate::sections::Section;
|
||||
use crate::style::TypeRole;
|
||||
use crate::style::{SurfaceKind, TypeRole};
|
||||
use crate::text::TextField;
|
||||
use craft_behavior::{DisplayOrder, ItemSource, Selection, SelectionChange};
|
||||
use craft_i18n::matcher::Matcher;
|
||||
use craft_theme::variant::Tone;
|
||||
use egui::Ui;
|
||||
|
||||
/// One entry of the page list.
|
||||
pub(super) struct PageEntry {
|
||||
pub id: String,
|
||||
pub title: Label,
|
||||
pub dimmed: bool,
|
||||
}
|
||||
|
||||
/// A group of the shown page: its title and the rows the search keeps.
|
||||
struct ShownGroup {
|
||||
title: Option<Label>,
|
||||
@@ -44,24 +33,33 @@ pub(super) struct ShownPage {
|
||||
groups: Vec<ShownGroup>,
|
||||
}
|
||||
|
||||
/// What the host's pages look like under the search: the pages to list and the page to show.
|
||||
/// What the host's pages look like under the search: the cards to show, in order, and how many matches the search
|
||||
/// found.
|
||||
pub(super) struct Snapshot {
|
||||
pub list: Vec<PageEntry>,
|
||||
pub shown: Option<ShownPage>,
|
||||
pub shown: Vec<ShownPage>,
|
||||
pub searching: bool,
|
||||
/// The settings the search matched: rows whose text matches, and a listed page that has none counts itself.
|
||||
pub matches: usize,
|
||||
}
|
||||
|
||||
/// The listed pages of `host` under `matcher`, and the part of the selected page the search keeps (the first listed
|
||||
/// page when the search hides the selected one).
|
||||
pub(super) fn snapshot<H: PreferencesModel + ?Sized>(host: &H, matcher: &Matcher, selected: &str) -> Snapshot {
|
||||
/// The cards of `host` under `matcher`: every listed page, each with the rows the search keeps.
|
||||
pub(super) fn snapshot<H: PreferencesModel + ?Sized>(host: &H, matcher: &Matcher) -> Snapshot {
|
||||
let pages = host.pages();
|
||||
let listed = search::list_pages(&pages, matcher);
|
||||
let list = listed
|
||||
let searching = !matcher.is_empty();
|
||||
let mut matches = 0;
|
||||
let shown = listed
|
||||
.iter()
|
||||
.map(|l| PageEntry { id: pages[l.index].id.to_owned(), title: Label::from(pages[l.index].title.to_owned()), dimmed: l.dimmed })
|
||||
.map(|l| {
|
||||
let page = &pages[l.index];
|
||||
if searching {
|
||||
let rows = page.groups.iter().flat_map(|g| &g.rows).filter(|r| r.visible && search::row_matches(matcher, r)).count();
|
||||
matches += rows.max(usize::from(!page.has_content() || l.all_rows && rows == 0));
|
||||
}
|
||||
shown_page(matcher, l, page)
|
||||
})
|
||||
.collect();
|
||||
let shown = search::shown_position(&listed, &pages, selected).map(|at| shown_page(matcher, &listed[at], &pages[listed[at].index]));
|
||||
Snapshot { list, shown, searching: !matcher.is_empty() }
|
||||
Snapshot { shown, searching, matches }
|
||||
}
|
||||
|
||||
fn shown_page(matcher: &Matcher, listed: &Listed, page: &PageSpec<'_>) -> ShownPage {
|
||||
@@ -86,7 +84,8 @@ fn shown_page(matcher: &Matcher, listed: &Listed, page: &PageSpec<'_>) -> ShownP
|
||||
#[derive(Default)]
|
||||
pub(super) struct BodyOut {
|
||||
pub rows: RowsOut,
|
||||
pub reset: bool,
|
||||
/// The page whose Reset Section was pressed.
|
||||
pub reset: Option<String>,
|
||||
pub custom_changed: bool,
|
||||
}
|
||||
|
||||
@@ -96,27 +95,8 @@ impl BodyOut {
|
||||
}
|
||||
}
|
||||
|
||||
/// The page list's rows: one per listed page, keyed by the page id.
|
||||
struct Pages<'a>(&'a [PageEntry]);
|
||||
|
||||
impl DisplayOrder<String> for Pages<'_> {
|
||||
fn len(&self) -> usize {
|
||||
self.0.len()
|
||||
}
|
||||
|
||||
fn key(&self, at: usize) -> String {
|
||||
self.0[at].id.clone()
|
||||
}
|
||||
|
||||
fn text(&self, at: usize) -> Option<std::borrow::Cow<'_, str>> {
|
||||
Some(std::borrow::Cow::Borrowed(self.0[at].title.as_str()))
|
||||
}
|
||||
}
|
||||
|
||||
impl ItemSource<String> for Pages<'_> {}
|
||||
|
||||
/// Shows the search field, the page list and the page. `state.search` and `state.selected` change as the user types
|
||||
/// and picks.
|
||||
/// Shows the search field with its match count over the cards: one scrolling page, a card per page. `state.search`
|
||||
/// changes as the user types; a `state.selected` the page has not scrolled to yet is brought into view once.
|
||||
pub(super) fn show_body<H: PreferencesModel + ?Sized>(
|
||||
ui: &mut Ui,
|
||||
host: &mut H,
|
||||
@@ -126,49 +106,38 @@ pub(super) fn show_body<H: PreferencesModel + ?Sized>(
|
||||
units: &Units,
|
||||
out: &mut BodyOut,
|
||||
) {
|
||||
let list_id = ui.scope_id().with("pages");
|
||||
TextField::search(words.search_label.clone(), Bind::bound(&mut state.search))
|
||||
.placeholder(words.search_placeholder.clone())
|
||||
.drives(list_id)
|
||||
.show(ui);
|
||||
let Some(page) = &snap.shown else {
|
||||
Row::new().fill_width().show(ui, |ui, _| {
|
||||
TextField::search(words.search_label.clone(), Bind::bound(&mut state.search)).placeholder(words.search_placeholder.clone()).show(ui);
|
||||
if snap.searching {
|
||||
let env = Env::of(ui);
|
||||
let n = env.format().integer(snap.matches as u64);
|
||||
let text = if snap.matches == 1 { words.match_one.fill(&[("n", &n)]) } else { words.match_other.fill(&[("n", &n)]) };
|
||||
let shade = if snap.matches == 0 { Shade::Faint } else { Shade::Muted };
|
||||
Text::new(text).role(TypeRole::Caption).shade(shade).show(ui);
|
||||
}
|
||||
});
|
||||
if snap.shown.is_empty() {
|
||||
Notice::new(Tone::Info, words.no_matches.clone()).layout(NoticeLayout::Centred).show(ui);
|
||||
return;
|
||||
};
|
||||
Row::new().fill().show(ui, |ui, _| {
|
||||
let current = Selection::single(page.id.clone());
|
||||
let source = Pages(&snap.list);
|
||||
let listed = List::<String>::new(words.pages_label.clone())
|
||||
.id(list_id)
|
||||
.select(SelectMode::Single)
|
||||
.activate(ActivateOn::Click)
|
||||
.allow_empty(false)
|
||||
.header(false)
|
||||
.sizing(Sizing::Content)
|
||||
.selection(Bind::given(current))
|
||||
.show_items(ui, &source, &mut |key, _| {
|
||||
let entry = snap.list.iter().find(|entry| entry.id == *key);
|
||||
let row = ListRow::new(entry.map_or_else(Label::default, |entry| entry.title.clone()));
|
||||
if entry.is_some_and(|entry| entry.dimmed) { row.ink(RowInk::Dimmed) } else { row }
|
||||
});
|
||||
for event in listed.events {
|
||||
if let CollectionEvent::Selection(SelectionChange::Set(keys)) = event
|
||||
&& let Some(key) = keys.into_iter().last()
|
||||
{
|
||||
state.selected = key;
|
||||
}
|
||||
// The scroll goes to the page the host named once, when it is first shown or changes.
|
||||
let jump = !state.selected.is_empty() && state.scrolled_to != state.selected && snap.shown.iter().any(|page| page.id == state.selected);
|
||||
Stack::new().fill().scroll(Scroll::vertical()).label(words.pages_label.clone()).show(ui, |ui, _| {
|
||||
for page in &snap.shown {
|
||||
let top = ui.cursor().min;
|
||||
show_card(ui, host, page, snap.searching, words, units, out);
|
||||
if jump && page.id == state.selected {
|
||||
ui.scroll_to_rect(egui::Rect::from_min_size(top, egui::vec2(1.0, 1.0)), Some(egui::Align::TOP));
|
||||
}
|
||||
}
|
||||
Divider::vertical().show(ui);
|
||||
Stack::new()
|
||||
.fill()
|
||||
.scroll(Scroll::vertical())
|
||||
.label(page.title.clone())
|
||||
.show(ui, |ui, _| show_page(ui, host, page, snap.searching, words, units, out));
|
||||
});
|
||||
if jump {
|
||||
state.scrolled_to = state.selected.clone();
|
||||
}
|
||||
}
|
||||
|
||||
/// The page's title, groups, footnote and Reset Section.
|
||||
fn show_page<H: PreferencesModel + ?Sized>(
|
||||
/// One page as a card: its title, groups, footnote and Reset Section.
|
||||
fn show_card<H: PreferencesModel + ?Sized>(
|
||||
ui: &mut Ui,
|
||||
host: &mut H,
|
||||
page: &ShownPage,
|
||||
@@ -177,32 +146,34 @@ fn show_page<H: PreferencesModel + ?Sized>(
|
||||
units: &Units,
|
||||
out: &mut BodyOut,
|
||||
) {
|
||||
Text::new(page.title.clone()).role(TypeRole::Title).show(ui);
|
||||
if page.custom {
|
||||
Stack::new().show(ui, |ui, _| out.custom_changed |= host.custom_page(ui, &page.id));
|
||||
} else if page.groups.is_empty() && !searching {
|
||||
Notice::new(Tone::Info, words.empty_page.clone()).layout(NoticeLayout::Centred).show(ui);
|
||||
}
|
||||
for (at, group) in page.groups.iter().enumerate() {
|
||||
let mut rows = |ui: &mut Ui| {
|
||||
let name = group.title.clone().unwrap_or_else(|| page.title.clone());
|
||||
FieldGroup::new(name).show(ui, |ui, _| {
|
||||
for row in &group.rows {
|
||||
show_row(ui, host, row, units, &mut out.rows);
|
||||
}
|
||||
});
|
||||
};
|
||||
match &group.title {
|
||||
Some(title) => {
|
||||
Section::fixed(title.clone()).id_salt(at).show(ui, |ui, _| rows(ui));
|
||||
}
|
||||
None => rows(ui),
|
||||
Stack::new().fill_width().surface(SurfaceKind::Raised).pad(Pad::Regular).label(page.title.clone()).id_salt(page.id.as_str()).show(ui, |ui, _| {
|
||||
Text::new(page.title.clone()).role(TypeRole::Heading).show(ui);
|
||||
if page.custom {
|
||||
Stack::new().show(ui, |ui, _| out.custom_changed |= host.custom_page(ui, &page.id));
|
||||
} else if page.groups.is_empty() && !searching {
|
||||
Notice::new(Tone::Info, words.empty_page.clone()).layout(NoticeLayout::Centred).show(ui);
|
||||
}
|
||||
}
|
||||
if let Some(note) = &page.footnote {
|
||||
Text::new(note.clone()).role(TypeRole::Caption).shade(Shade::Muted).show(ui);
|
||||
}
|
||||
if !searching && (page.custom || !page.groups.is_empty()) && reset_button(ui, words.reset_section.clone()) {
|
||||
out.reset = true;
|
||||
}
|
||||
for (at, group) in page.groups.iter().enumerate() {
|
||||
let mut rows = |ui: &mut Ui| {
|
||||
let name = group.title.clone().unwrap_or_else(|| page.title.clone());
|
||||
FieldGroup::new(name).show(ui, |ui, _| {
|
||||
for row in &group.rows {
|
||||
show_row(ui, host, row, units, &mut out.rows);
|
||||
}
|
||||
});
|
||||
};
|
||||
match &group.title {
|
||||
Some(title) => {
|
||||
Section::fixed(title.clone()).id_salt(at).show(ui, |ui, _| rows(ui));
|
||||
}
|
||||
None => rows(ui),
|
||||
}
|
||||
}
|
||||
if let Some(note) = &page.footnote {
|
||||
Text::new(note.clone()).role(TypeRole::Caption).shade(Shade::Muted).show(ui);
|
||||
}
|
||||
if !searching && (page.custom || !page.groups.is_empty()) && reset_button(ui, words.reset_section.clone()) {
|
||||
out.reset = Some(page.id.clone());
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@@ -122,6 +122,9 @@ pub struct RowSpec<'a> {
|
||||
/// The step of a [`RowKind::Float`] row (arrow keys, wheel, drag); without one the step comes from the range and
|
||||
/// decimals. Ignored by other kinds.
|
||||
pub step: Option<f64>,
|
||||
/// A choice row: `Some(true)` draws its options as a segmented (pill) control, `Some(false)` as a drop-down; by
|
||||
/// default a short choice of up to five options is segmented. Ignored by other kinds.
|
||||
pub pills: Option<bool>,
|
||||
/// The control and the value it shows.
|
||||
pub kind: RowKind<'a>,
|
||||
}
|
||||
@@ -129,7 +132,7 @@ pub struct RowSpec<'a> {
|
||||
impl<'a> RowSpec<'a> {
|
||||
/// A visible, enabled row with no keywords or help.
|
||||
pub fn new(key: &'a str, label: &'a str, kind: RowKind<'a>) -> Self {
|
||||
Self { key, label, keywords: "", help: None, visible: true, enabled: true, step: None, kind }
|
||||
Self { key, label, keywords: "", help: None, visible: true, enabled: true, step: None, pills: None, kind }
|
||||
}
|
||||
|
||||
/// Sets extra search text for the row.
|
||||
@@ -160,6 +163,13 @@ impl<'a> RowSpec<'a> {
|
||||
self
|
||||
}
|
||||
|
||||
/// Chooses how a [`RowKind::Choice`] row draws its options: `true` as segments, `false` as a drop-down.
|
||||
#[must_use]
|
||||
pub fn pills(mut self, pills: bool) -> Self {
|
||||
self.pills = Some(pills);
|
||||
self
|
||||
}
|
||||
|
||||
/// Sets the step of a [`RowKind::Float`] row.
|
||||
#[must_use]
|
||||
pub fn step(mut self, step: f64) -> Self {
|
||||
|
||||
@@ -2,7 +2,9 @@
|
||||
|
||||
use super::model::{PrefValue, PreferencesModel, RowKind, RowSpec};
|
||||
use crate::buttons::Button;
|
||||
use crate::choice::{Checkbox, Swatch};
|
||||
use crate::choice::{Checkbox, Segmented, Swatch};
|
||||
use crate::display::{Shade, Text};
|
||||
use crate::style::TypeRole;
|
||||
use crate::contract::{Bind, Entry, FieldLayout, Props as _, Show as _, ShowContainer as _, clicked};
|
||||
use crate::form::{Field, Help};
|
||||
use crate::layout_components::{Row, Stack};
|
||||
@@ -23,6 +25,7 @@ pub(super) struct ShownRow {
|
||||
pub label: Label,
|
||||
pub help: Option<Label>,
|
||||
pub enabled: bool,
|
||||
pub pills: Option<bool>,
|
||||
pub kind: Kind,
|
||||
}
|
||||
|
||||
@@ -65,6 +68,7 @@ impl ShownRow {
|
||||
label: Label::from(row.label.to_owned()),
|
||||
help: row.help.map(|help| Label::from(help.to_owned())),
|
||||
enabled: row.enabled,
|
||||
pills: row.pills,
|
||||
kind,
|
||||
}
|
||||
}
|
||||
@@ -98,6 +102,28 @@ fn counted_option(units: &Units, kind: UnitKind, option: &Entry<String>) -> Entr
|
||||
}
|
||||
}
|
||||
|
||||
/// The most options and the most label characters of a choice shown as a segmented control; a longer choice is a
|
||||
/// drop-down.
|
||||
const SEGMENTS_MAX: usize = 5;
|
||||
const SEGMENTS_CHARS: usize = 48;
|
||||
|
||||
fn as_segments(options: &[Entry<String>], unit: Option<UnitKind>, pills: Option<bool>) -> bool {
|
||||
let plain = options.iter().all(|option| !option.is_separator());
|
||||
let count = options.len();
|
||||
let chars: usize = options.iter().map(|option| option.label().as_str().chars().count()).sum();
|
||||
match pills {
|
||||
Some(want) => want && plain && count >= 2,
|
||||
None => plain && unit.is_none() && (2..=SEGMENTS_MAX).contains(&count) && chars <= SEGMENTS_CHARS,
|
||||
}
|
||||
}
|
||||
|
||||
/// The help line under a row: a caption in the muted shade.
|
||||
fn help_line(ui: &mut Ui, help: &Option<Label>) {
|
||||
if let Some(help) = help {
|
||||
Text::new(help.clone()).role(TypeRole::Caption).shade(Shade::Muted).show(ui);
|
||||
}
|
||||
}
|
||||
|
||||
/// Shows `row` and records what it edited in `out`.
|
||||
pub(super) fn show_row<H: PreferencesModel + ?Sized>(ui: &mut Ui, host: &mut H, row: &ShownRow, units: &Units, out: &mut RowsOut) {
|
||||
let key = row.key.as_str();
|
||||
@@ -132,14 +158,19 @@ pub(super) fn show_row<H: PreferencesModel + ?Sized>(ui: &mut Ui, host: &mut H,
|
||||
}
|
||||
None => options,
|
||||
};
|
||||
field().show(ui, |ui, _| {
|
||||
next = Select::new(label.clone(), Bind::given(value.clone()), options).show(ui).events.landed().cloned();
|
||||
let segments = as_segments(options, *unit, row.pills);
|
||||
inline(ui, row, |ui| {
|
||||
next = if segments {
|
||||
Segmented::new(label.clone(), Bind::given(value.clone()), options).show(ui).value().cloned()
|
||||
} else {
|
||||
Select::new(label.clone(), Bind::given(value.clone()), options).show(ui).events.landed().cloned()
|
||||
};
|
||||
});
|
||||
record(next.map(PrefValue::Choice));
|
||||
}
|
||||
Kind::Int { value, range, unit } => {
|
||||
let mut next = None;
|
||||
field().show(ui, |ui, _| {
|
||||
inline(ui, row, |ui| {
|
||||
let shown = number(units, label, *value as f64, *unit, 0).range(*range.start() as f64..=*range.end() as f64).step(1.0);
|
||||
next = shown.show(ui).edit.events.landed().cloned().flatten();
|
||||
});
|
||||
@@ -147,7 +178,7 @@ pub(super) fn show_row<H: PreferencesModel + ?Sized>(ui: &mut Ui, host: &mut H,
|
||||
}
|
||||
Kind::Float { value, range, decimals, step, unit } => {
|
||||
let mut next = None;
|
||||
field().show(ui, |ui, _| {
|
||||
inline(ui, row, |ui| {
|
||||
let shown = number(units, label, *value, *unit, usize::from(*decimals))
|
||||
.range(range.clone())
|
||||
.step(step.unwrap_or_else(|| step_for(range, *decimals)));
|
||||
@@ -157,7 +188,7 @@ pub(super) fn show_row<H: PreferencesModel + ?Sized>(ui: &mut Ui, host: &mut H,
|
||||
}
|
||||
Kind::Text(value) => {
|
||||
let mut next = None;
|
||||
field().show(ui, |ui, _| {
|
||||
inline(ui, row, |ui| {
|
||||
next = TextField::new(label.clone(), Bind::given(value.clone())).show(ui).events.landed().cloned();
|
||||
});
|
||||
record(next.map(PrefValue::Text));
|
||||
@@ -165,12 +196,10 @@ pub(super) fn show_row<H: PreferencesModel + ?Sized>(ui: &mut Ui, host: &mut H,
|
||||
Kind::Color(color) => {
|
||||
let mut next = None;
|
||||
let mut ask = None;
|
||||
field().show(ui, |ui, _| {
|
||||
Row::new().show(ui, |ui, _| {
|
||||
let swatch = Swatch::new(label.clone(), Srgba::from(*color)).picker(key).id_salt("choose").show(ui);
|
||||
ask = swatch.pick_color;
|
||||
next = HexField::hex(label.clone(), Bind::given(*color)).id_salt("hex").show(ui).events.landed().cloned();
|
||||
});
|
||||
inline(ui, row, |ui| {
|
||||
let swatch = Swatch::new(label.clone(), Srgba::from(*color)).picker(key).id_salt("choose").show(ui);
|
||||
ask = swatch.pick_color;
|
||||
next = HexField::hex(label.clone(), Bind::given(*color)).id_salt("hex").show(ui).events.landed().cloned();
|
||||
});
|
||||
record(next.map(PrefValue::Color));
|
||||
if ask.is_some() {
|
||||
@@ -188,6 +217,20 @@ pub(super) fn show_row<H: PreferencesModel + ?Sized>(ui: &mut Ui, host: &mut H,
|
||||
}
|
||||
}
|
||||
|
||||
/// One row on a line, as production's cards draw them: the label, then the control, then the help under both.
|
||||
fn inline(ui: &mut Ui, row: &ShownRow, control: impl FnOnce(&mut Ui)) {
|
||||
let mut control = Some(control);
|
||||
Stack::new().id_salt(row.key.as_str()).enabled(row.enabled).show(ui, |ui, _| {
|
||||
Row::new().show(ui, |ui, _| {
|
||||
Text::new(row.label.clone()).role(TypeRole::Body).show(ui);
|
||||
if let Some(control) = control.take() {
|
||||
control(ui);
|
||||
}
|
||||
});
|
||||
help_line(ui, &row.help);
|
||||
});
|
||||
}
|
||||
|
||||
/// The Reset Section button of a page. Returns whether it was pressed.
|
||||
pub(super) fn reset_button(ui: &mut Ui, label: Label) -> bool {
|
||||
clicked(&Button::new(label).id_salt("reset").show(ui))
|
||||
|
||||
@@ -23,9 +23,9 @@ use std::cell::RefCell;
|
||||
use std::sync::Arc;
|
||||
|
||||
/// The size the window opens at until the user resizes it.
|
||||
pub const DEFAULT_SIZE: Vec2 = vec2(1040.0, 800.0); // design: allow[literal-size/vec2](the window's own opening size in points)
|
||||
pub const DEFAULT_SIZE: Vec2 = vec2(640.0, 720.0); // design: allow[literal-size/vec2](the window's own opening size in points)
|
||||
/// The least size the window may be resized to.
|
||||
pub const MIN_SIZE: Vec2 = vec2(560.0, 400.0); // design: allow[literal-size/vec2](the window's own least size in points)
|
||||
pub const MIN_SIZE: Vec2 = vec2(420.0, 320.0); // design: allow[literal-size/vec2](the window's own least size in points)
|
||||
|
||||
/// When preferences changes are applied.
|
||||
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
|
||||
@@ -47,6 +47,8 @@ pub struct PreferencesState {
|
||||
pub selected: String,
|
||||
/// The text in the search field. Empty lists every page.
|
||||
pub search: String,
|
||||
/// The page the view last scrolled to for `selected`, so a page the host names is brought into view once.
|
||||
pub scrolled_to: String,
|
||||
/// The message of a failed Apply, shown in the footer until the next edit or Apply.
|
||||
pub error: Option<String>,
|
||||
}
|
||||
@@ -218,29 +220,28 @@ impl<'a> PreferencesWindow<'a> {
|
||||
let session = &mut *guard;
|
||||
session.begin(cx);
|
||||
let matcher = Matcher::new(&env.folder(), session.work.search.trim());
|
||||
let snap = body::snapshot(&*session.host, &matcher, &session.work.selected);
|
||||
let snap = body::snapshot(&*session.host, &matcher);
|
||||
let mut out = BodyOut::default();
|
||||
body::show_body(ui, &mut *session.host, &snap, &mut session.work, &words, &units, &mut out);
|
||||
if let Some(request) = out.rows.pick_color.take() {
|
||||
pick_color = Some(request);
|
||||
}
|
||||
let edits = out.edits();
|
||||
if !edits.is_empty() || out.reset || out.custom_changed || out.rows.custom_changed {
|
||||
if !edits.is_empty() || out.reset.is_some() || out.custom_changed || out.rows.custom_changed {
|
||||
session.work.error = None;
|
||||
}
|
||||
for (key, value) in edits {
|
||||
session.host.set(&key, value);
|
||||
}
|
||||
if out.reset
|
||||
&& let Some(page) = &snap.shown
|
||||
{
|
||||
session.host.reset_page(&page.id);
|
||||
if let Some(page) = &out.reset {
|
||||
session.host.reset_page(page);
|
||||
}
|
||||
session.host.nested(ui);
|
||||
if mode == PreferencesMode::Live && session.work.error.is_none() && session.host.differs() {
|
||||
session.apply(cx);
|
||||
}
|
||||
session.page = snap.shown.map(|page| page.id);
|
||||
let selected = session.work.selected.clone();
|
||||
session.page = snap.shown.iter().find(|page| page.id == selected).or(snap.shown.first()).map(|page| page.id.clone());
|
||||
session.commit(cx);
|
||||
});
|
||||
|
||||
|
||||
@@ -8,7 +8,9 @@ craft_ui::words! {
|
||||
search_label: "preferences.searchLabel" = "Search settings";
|
||||
search_placeholder: "preferences.searchPlaceholder" = "Search";
|
||||
clear_search: "preferences.clearSearch" = "Clear search";
|
||||
pages_label: "preferences.pagesLabel" = "Pages";
|
||||
pages_label: "preferences.pagesLabel" = "Settings";
|
||||
match_one: template "preferences.matchOne" = "{n} match";
|
||||
match_other: template "preferences.matchOther" = "{n} matches";
|
||||
no_matches: "preferences.noMatches" = "No matching settings";
|
||||
empty_page: "preferences.emptyPage" = "These settings aren't available yet.";
|
||||
apply: "preferences.apply" = "Apply";
|
||||
|
||||
@@ -4,3 +4,4 @@
|
||||
mod live;
|
||||
mod model;
|
||||
mod path_setting;
|
||||
mod page;
|
||||
|
||||
@@ -0,0 +1,74 @@
|
||||
//! Preferences is one scrolling page of cards: every page is shown at once, short choices are segmented and the
|
||||
//! search counts its matches.
|
||||
|
||||
use craft_ui::contract::{Bind, Entry};
|
||||
use craft_ui::patterns::preferences::{PageSpec, PrefValue, PreferencesModel, PreferencesState, PreferencesWindow, RowKind, RowSpec};
|
||||
use craft_ui::testing::{Fixture, trial};
|
||||
use egui::Ui;
|
||||
|
||||
struct Model {
|
||||
side: String,
|
||||
}
|
||||
|
||||
impl PreferencesModel for Model {
|
||||
fn pages(&self) -> Vec<PageSpec<'_>> {
|
||||
let sides = vec![Entry::new("left", "Left"), Entry::new("right", "Right")];
|
||||
vec![
|
||||
PageSpec::new("general", "General", vec![RowSpec::new("side", "Tools side", RowKind::choice(&self.side, sides))]),
|
||||
PageSpec::new("canvas", "Canvas", vec![RowSpec::new("snap", "Snap", RowKind::Bool(true))]),
|
||||
]
|
||||
}
|
||||
fn set(&mut self, _: &str, value: PrefValue) {
|
||||
if let PrefValue::Choice(side) = value {
|
||||
self.side = side;
|
||||
}
|
||||
}
|
||||
fn custom_row(&mut self, _: &mut Ui, _: &str) -> bool {
|
||||
false
|
||||
}
|
||||
fn custom_page(&mut self, _: &mut Ui, _: &str) -> bool {
|
||||
false
|
||||
}
|
||||
fn differs(&self) -> bool {
|
||||
false
|
||||
}
|
||||
fn apply(&mut self) -> Result<(), String> {
|
||||
Ok(())
|
||||
}
|
||||
fn cancel(&mut self) {}
|
||||
fn reset_page(&mut self, _: &str) {}
|
||||
}
|
||||
|
||||
fn fixture() -> Fixture {
|
||||
let mut model = Model { side: "left".into() };
|
||||
let mut state = PreferencesState::default();
|
||||
Fixture::new(move |ui| {
|
||||
let _ = PreferencesWindow::new(Bind::bound(&mut state)).embedded().show(ui, &mut model);
|
||||
})
|
||||
}
|
||||
|
||||
#[trial]
|
||||
fn every_page_is_a_card_on_one_scrolling_page() {
|
||||
let mut fixture = fixture();
|
||||
fixture.room(egui::vec2(700.0, 900.0)).settle();
|
||||
assert!(fixture.by_label("General").is_some() && fixture.by_label("Canvas").is_some(), "both cards show at once");
|
||||
}
|
||||
|
||||
#[trial]
|
||||
fn a_short_choice_is_a_row_of_segments() {
|
||||
let mut fixture = fixture();
|
||||
fixture.room(egui::vec2(700.0, 900.0)).settle();
|
||||
let right = fixture.by_label("Right").expect("a segment for each option");
|
||||
fixture.click(&right).settle();
|
||||
assert!(fixture.texts().iter().any(|t| t.text == "Left"), "the other option stays shown beside it");
|
||||
}
|
||||
|
||||
#[trial]
|
||||
fn the_search_shows_how_many_settings_match() {
|
||||
let mut fixture = fixture();
|
||||
fixture.room(egui::vec2(700.0, 900.0)).settle();
|
||||
assert!(!fixture.texts().iter().any(|t| t.text.contains("match")), "no count without a search");
|
||||
let search = fixture.by_label("Search settings").expect("the search field");
|
||||
fixture.click(&search).type_text("snap").settle();
|
||||
assert!(fixture.texts().iter().any(|t| t.text == "1 match"), "{:?}", fixture.texts().iter().map(|t| &t.text).collect::<Vec<_>>());
|
||||
}
|
||||
Reference in New Issue
Block a user