mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-12-01 02:35:37 +01:00
Merge main into fix/xwayland-tablet-stylus
This commit is contained in:
commit
81116cb786
1 file changed
+279
-60
@@ -1,10 +1,10 @@
|
||||
//! The Properties panel's Document sections, shown with nothing selected.
|
||||
|
||||
use craft::behavior::IconId;
|
||||
use craft::behavior::{EditEvent, IconId};
|
||||
use craft::keymap::CommandRef;
|
||||
use craft::ui::buttons::{Button, ToggleButton};
|
||||
use craft::ui::choice::Checkbox;
|
||||
use craft::ui::contract::{Bind, Entry, Props as _, Show as _, ShowContainer as _, Sizing};
|
||||
use craft::ui::contract::{Bind, Entry, MemoProps, MemoView, Outbox, Props as _, Show as _, ShowContainer as _, Sizing};
|
||||
use craft::ui::form::Field;
|
||||
use craft::ui::layout_components::{Grid, Row};
|
||||
use craft::ui::pickers::Select;
|
||||
@@ -17,84 +17,215 @@ use vectorcraft_doc::Unit;
|
||||
use super::artboard_sections::rearrange_button;
|
||||
use super::object_sections::HALVES;
|
||||
use crate::VectorcraftApp;
|
||||
use crate::edits::EditPath;
|
||||
use crate::panels::transform;
|
||||
|
||||
/// The panel with nothing selected: the document's units, Document Setup and artboards, the
|
||||
/// Appearance fill and stroke, the view's rulers, grids, guides and snapping, the keyboard
|
||||
/// increment and Scale Strokes & Effects, and Quick Actions.
|
||||
///
|
||||
/// The body of every section but Appearance shows in a `MemoView` keyed on what it reads, so a
|
||||
/// frame that changes none of it replays the body. What a body picks comes back through an `Outbox`
|
||||
/// and runs after its section.
|
||||
pub(super) fn document_sections(app: &mut VectorcraftApp, ui: &mut Ui) {
|
||||
let lang = crate::i18n::current();
|
||||
let units = app.session.general_unit();
|
||||
let unit = Unit::ALL.iter().position(|u| *u == units).unwrap_or(usize::MAX);
|
||||
let artboards = app.session.active().map_or(0, |d| d.doc.artboards.len());
|
||||
let mut picked_units = Vec::new();
|
||||
super::section(tl!("Document")).show(ui, |ui, _| {
|
||||
Field::new(tl!("Units")).show(ui, |ui, _| {
|
||||
let entries: Vec<Entry<Unit>> = Unit::ALL.iter().map(|u| Entry::new(*u, u.label())).collect();
|
||||
let out = Select::new(tl!("Units"), Bind::given(units), &entries).show(ui);
|
||||
if let Some(u) = out.events.committed() {
|
||||
app.run("document.setUnits", json!({ "units": u.label() })).ok();
|
||||
}
|
||||
});
|
||||
Grid::new(&HALVES).show(ui, |ui, _| {
|
||||
Button::command(CommandRef::new("file.documentSetup")).label(tl!("Document Setup")).sizing(Sizing::FillWidth).show(ui);
|
||||
Button::command(CommandRef::with_params("tool.select", json!({ "tool": "artboard" })))
|
||||
.label(tl!("Edit Artboards"))
|
||||
.sizing(Sizing::FillWidth)
|
||||
.show(ui);
|
||||
if artboards > 1 {
|
||||
rearrange_button(ui, artboards);
|
||||
}
|
||||
});
|
||||
let document = DocumentProps { unit, units, artboards, lang, picked: Outbox::new(&mut picked_units) };
|
||||
let _ = MemoView::new("properties-document", document, document_body).show(ui);
|
||||
});
|
||||
if let Some(u) = picked_units.pop() {
|
||||
app.run("document.setUnits", json!({ "units": u.label() })).ok();
|
||||
}
|
||||
super::section(tl!("Appearance")).show(ui, |ui, _| super::appearance_section::fill_stroke_rows(app, ui));
|
||||
super::section(tl!("Rulers & Grids")).show(ui, |ui, _| {
|
||||
Row::new().show(ui, |ui, _| {
|
||||
// The checked and enabled states and the tooltips come from the commands (Trim View turns the grid off).
|
||||
for (command, icon) in [("view.rulers", "ruler"), ("view.grid", "grid-3x3"), ("view.transparencyGrid", "square-dashed")] {
|
||||
ToggleButton::command(CommandRef::new(command)).icon(IconId::new(icon)).icon_only().show(ui);
|
||||
}
|
||||
});
|
||||
let _ = MemoView::new("properties-rulers", Language { lang }, rulers_body).show(ui);
|
||||
});
|
||||
super::section(tl!("Guides")).show(ui, |ui, _| {
|
||||
Row::new().show(ui, |ui, _| {
|
||||
for (command, icon) in [("view.guides", "layout-grid"), ("view.smartGuides", "sparkles")] {
|
||||
ToggleButton::command(CommandRef::new(command)).icon(IconId::new(icon)).icon_only().show(ui);
|
||||
}
|
||||
});
|
||||
let _ = MemoView::new("properties-guides", Language { lang }, guides_body).show(ui);
|
||||
});
|
||||
let mut snaps = Vec::new();
|
||||
let (point, grid) = (app.ui.view.snap_to_point, app.ui.view.snap_to_grid);
|
||||
super::section(tl!("Snap Options")).show(ui, |ui, _| {
|
||||
let (point, grid) = (app.ui.view.snap_to_point, app.ui.view.snap_to_grid);
|
||||
for (command, label, on) in [("view.snapToPoint", tl!("Snap to Point"), point), ("view.snapToGrid", tl!("Snap to Grid"), grid)] {
|
||||
if Checkbox::new(Bind::given(on)).label(label).show(ui).value().is_some() {
|
||||
app.run(command, json!({})).ok();
|
||||
}
|
||||
}
|
||||
let snap = SnapProps { point, grid, lang, picked: Outbox::new(&mut snaps) };
|
||||
let _ = MemoView::new("properties-snap", snap, snap_body).show(ui);
|
||||
});
|
||||
for command in snaps {
|
||||
app.run(command, json!({})).ok();
|
||||
}
|
||||
let mut prefs = Vec::new();
|
||||
// Read again after the Units commit above, so the key and `lengths` come from the same General unit.
|
||||
let general = app.session.general_unit();
|
||||
let unit = Unit::ALL.iter().position(|u| *u == general).unwrap_or(usize::MAX);
|
||||
let (increment, scale) = (app.session.prefs.keyboard_increment, app.session.prefs.scale_strokes);
|
||||
let (bare_points, lengths) = (app.session.prefs.numbers_without_units_are_points, crate::canvas::rulers::general_lengths(app));
|
||||
super::section(tl!("Preferences")).show(ui, |ui, _| {
|
||||
Field::new(tl!("Keyboard Increment")).show(ui, |ui, _| {
|
||||
// PLAN: assumed `canvas::rulers::general_lengths(app) -> LengthUnits` (cv.B5b)
|
||||
let lengths: LengthUnits = crate::canvas::rulers::general_lengths(app);
|
||||
// The engine's own range for the preference (`prefscmds.rs`): a larger value is an error.
|
||||
let out = NumberField::length(tl!("Keyboard Increment"), Bind::given(Some(app.session.prefs.keyboard_increment)), lengths)
|
||||
.range(0.001..=1296.0)
|
||||
.show(ui);
|
||||
crate::edits::apply_edits(app, &out.edit, crate::edits::EditPath::Scope, |app, v: &Option<f64>, _| {
|
||||
if let Some(value) = *v {
|
||||
app.run("prefs.set", json!({ "key": "keyboardIncrement", "value": value })).ok();
|
||||
}
|
||||
});
|
||||
});
|
||||
let scale = app.session.prefs.scale_strokes;
|
||||
let out = Checkbox::new(Bind::given(scale)).label(tl!("Scale Strokes & Effects")).show(ui);
|
||||
if let Some(on) = out.value().copied() {
|
||||
transform::set_pref(app, "scaleStrokes", on);
|
||||
let picked = Outbox::new(&mut prefs);
|
||||
let preferences = PreferencesProps { increment, unit, bare_points, lengths, scale, lang, picked };
|
||||
let _ = MemoView::new("properties-preferences", preferences, preferences_body).show(ui);
|
||||
});
|
||||
for pick in prefs {
|
||||
match pick {
|
||||
PreferencePick::Increment(event) => {
|
||||
crate::edits::apply_event(app, &event, &EditPath::Scope, &mut |app, v: &Option<f64>, _| {
|
||||
if let Some(value) = *v {
|
||||
app.run("prefs.set", json!({ "key": "keyboardIncrement", "value": value })).ok();
|
||||
}
|
||||
});
|
||||
}
|
||||
PreferencePick::ScaleStrokes(on) => transform::set_pref(app, "scaleStrokes", on),
|
||||
}
|
||||
}
|
||||
super::section(tl!("Quick Actions")).show(ui, |ui, _| {
|
||||
let _ = MemoView::new("properties-quick-actions", Language { lang }, quick_actions_body).show(ui);
|
||||
});
|
||||
}
|
||||
|
||||
/// What a section that reads only command states reads besides them: the interface language of its labels.
|
||||
#[derive(MemoProps)]
|
||||
struct Language {
|
||||
#[memo_props(hash = "an interface language: an index into the languages offered")]
|
||||
lang: crate::i18n::Lang,
|
||||
}
|
||||
|
||||
/// What the Document section reads: the document's units and how many artboards it has.
|
||||
#[derive(MemoProps)]
|
||||
struct DocumentProps<'a> {
|
||||
/// The index of `units` in [`Unit::ALL`].
|
||||
unit: usize,
|
||||
#[memo_props(covered_by = "unit", reason = "the unit's index in Unit::ALL")]
|
||||
units: Unit,
|
||||
/// How many artboards the document has (Rearrange All shows for more than one).
|
||||
artboards: usize,
|
||||
#[memo_props(hash = "an interface language: an index into the languages offered")]
|
||||
lang: crate::i18n::Lang,
|
||||
/// The units chosen in the Units menu.
|
||||
picked: Outbox<'a, Unit>,
|
||||
}
|
||||
|
||||
/// The Document section's body: Units, Document Setup, Edit Artboards and Rearrange All.
|
||||
fn document_body(p: &mut DocumentProps<'_>, ui: &mut Ui) {
|
||||
#[cfg(test)]
|
||||
tests::count_run();
|
||||
Field::new(tl!("Units")).show(ui, |ui, _| {
|
||||
let entries: Vec<Entry<Unit>> = Unit::ALL.iter().map(|u| Entry::new(*u, u.label())).collect();
|
||||
let out = Select::new(tl!("Units"), Bind::given(p.units), &entries).show(ui);
|
||||
if let Some(u) = out.events.committed() {
|
||||
p.picked.send(*u);
|
||||
}
|
||||
});
|
||||
super::section(tl!("Quick Actions")).show(ui, |ui, _| {
|
||||
Grid::new(&HALVES).show(ui, |ui, _| {
|
||||
// The reference panel's buttons name the dialog without the menu's ellipsis.
|
||||
for (command, label) in [("file.documentSetup", tl!("Document Setup")), ("edit.preferences", tl!("Preferences"))] {
|
||||
Button::command(CommandRef::new(command)).label(label).sizing(Sizing::FillWidth).show(ui);
|
||||
}
|
||||
});
|
||||
Grid::new(&HALVES).show(ui, |ui, _| {
|
||||
Button::command(CommandRef::new("file.documentSetup")).label(tl!("Document Setup")).sizing(Sizing::FillWidth).show(ui);
|
||||
Button::command(CommandRef::with_params("tool.select", json!({ "tool": "artboard" })))
|
||||
.label(tl!("Edit Artboards"))
|
||||
.sizing(Sizing::FillWidth)
|
||||
.show(ui);
|
||||
if p.artboards > 1 {
|
||||
rearrange_button(ui, p.artboards);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/// The Rulers & Grids section's body. The checked and enabled states and the tooltips come from the commands (Trim
|
||||
/// View turns the grid off).
|
||||
fn rulers_body(_: &mut Language, ui: &mut Ui) {
|
||||
#[cfg(test)]
|
||||
tests::count_run();
|
||||
Row::new().show(ui, |ui, _| {
|
||||
for (command, icon) in [("view.rulers", "ruler"), ("view.grid", "grid-3x3"), ("view.transparencyGrid", "square-dashed")] {
|
||||
ToggleButton::command(CommandRef::new(command)).icon(IconId::new(icon)).icon_only().show(ui);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/// The Guides section's body: Show Guides and Smart Guides, from their commands.
|
||||
fn guides_body(_: &mut Language, ui: &mut Ui) {
|
||||
#[cfg(test)]
|
||||
tests::count_run();
|
||||
Row::new().show(ui, |ui, _| {
|
||||
for (command, icon) in [("view.guides", "layout-grid"), ("view.smartGuides", "sparkles")] {
|
||||
ToggleButton::command(CommandRef::new(command)).icon(IconId::new(icon)).icon_only().show(ui);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/// What the Snap Options section reads: whether Snap to Point and Snap to Grid are on.
|
||||
#[derive(MemoProps)]
|
||||
struct SnapProps<'a> {
|
||||
point: bool,
|
||||
grid: bool,
|
||||
#[memo_props(hash = "an interface language: an index into the languages offered")]
|
||||
lang: crate::i18n::Lang,
|
||||
/// The command of each checkbox clicked.
|
||||
picked: Outbox<'a, &'static str>,
|
||||
}
|
||||
|
||||
/// The Snap Options section's body: Snap to Point and Snap to Grid.
|
||||
fn snap_body(p: &mut SnapProps<'_>, ui: &mut Ui) {
|
||||
#[cfg(test)]
|
||||
tests::count_run();
|
||||
for (command, label, on) in [("view.snapToPoint", tl!("Snap to Point"), p.point), ("view.snapToGrid", tl!("Snap to Grid"), p.grid)] {
|
||||
if Checkbox::new(Bind::given(on)).label(label).show(ui).value().is_some() {
|
||||
p.picked.send(command);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// What the Preferences section reads: the keyboard increment in the General unit, and Scale Strokes & Effects.
|
||||
#[derive(MemoProps)]
|
||||
struct PreferencesProps<'a> {
|
||||
/// The keyboard increment, in points.
|
||||
increment: f64,
|
||||
/// The index of the General unit in [`Unit::ALL`].
|
||||
unit: usize,
|
||||
/// Numbers without units are points (Preferences › Units).
|
||||
bare_points: bool,
|
||||
#[memo_props(covered_by = "unit", reason = "made from the General unit and bare_points alone (canvas::rulers::lengths)")]
|
||||
lengths: LengthUnits,
|
||||
/// Scale Strokes & Effects is on.
|
||||
scale: bool,
|
||||
#[memo_props(hash = "an interface language: an index into the languages offered")]
|
||||
lang: crate::i18n::Lang,
|
||||
/// The keyboard increment's edits and the Scale Strokes & Effects choice.
|
||||
picked: Outbox<'a, PreferencePick>,
|
||||
}
|
||||
|
||||
/// What the Preferences section reports: an edit of the keyboard increment, or Scale Strokes & Effects turned on or
|
||||
/// off.
|
||||
enum PreferencePick {
|
||||
Increment(EditEvent<Option<f64>>),
|
||||
ScaleStrokes(bool),
|
||||
}
|
||||
|
||||
/// The Preferences section's body: Keyboard Increment and Scale Strokes & Effects.
|
||||
fn preferences_body(p: &mut PreferencesProps<'_>, ui: &mut Ui) {
|
||||
#[cfg(test)]
|
||||
tests::count_run();
|
||||
Field::new(tl!("Keyboard Increment")).show(ui, |ui, _| {
|
||||
// The engine's own range for the preference (`prefscmds.rs`): a larger value is an error.
|
||||
let out = NumberField::length(tl!("Keyboard Increment"), Bind::given(Some(p.increment)), p.lengths).range(0.001..=1296.0).show(ui);
|
||||
for event in out.edit.events.iter() {
|
||||
p.picked.send(PreferencePick::Increment(event.clone()));
|
||||
}
|
||||
});
|
||||
let out = Checkbox::new(Bind::given(p.scale)).label(tl!("Scale Strokes & Effects")).show(ui);
|
||||
if let Some(on) = out.value().copied() {
|
||||
p.picked.send(PreferencePick::ScaleStrokes(on));
|
||||
}
|
||||
}
|
||||
|
||||
/// The Quick Actions section's body: Document Setup and Preferences. The reference panel's buttons name the dialog
|
||||
/// without the menu's ellipsis.
|
||||
fn quick_actions_body(_: &mut Language, ui: &mut Ui) {
|
||||
#[cfg(test)]
|
||||
tests::count_run();
|
||||
Grid::new(&HALVES).show(ui, |ui, _| {
|
||||
for (command, label) in [("file.documentSetup", tl!("Document Setup")), ("edit.preferences", tl!("Preferences"))] {
|
||||
Button::command(CommandRef::new(command)).label(label).sizing(Sizing::FillWidth).show(ui);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@@ -104,6 +235,19 @@ mod tests {
|
||||
use crate::tests::panel_harness::Panel;
|
||||
use craft::ui::contract::Role;
|
||||
|
||||
thread_local! {
|
||||
/// How many times this thread ran a memoized Document section.
|
||||
static SECTION_RUNS: std::cell::Cell<u64> = const { std::cell::Cell::new(0) };
|
||||
}
|
||||
|
||||
pub(super) fn count_run() {
|
||||
SECTION_RUNS.with(|n| n.set(n.get() + 1));
|
||||
}
|
||||
|
||||
fn runs() -> u64 {
|
||||
SECTION_RUNS.with(std::cell::Cell::get)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn canceling_the_units_popup_leaves_the_document_unchanged() {
|
||||
let mut app = VectorcraftApp::new(vectorcraft_engine::Session::new(), Default::default());
|
||||
@@ -135,4 +279,79 @@ mod tests {
|
||||
assert_eq!(panel.app().session.active().unwrap().doc.units, next, "Enter applies the chosen document unit");
|
||||
assert_eq!(panel.app().session.general_unit(), next);
|
||||
}
|
||||
|
||||
/// Frames that change nothing the sections show replay them instead of laying them out again. A change one of
|
||||
/// them reads, from a command run elsewhere, runs it again, and it shows the change.
|
||||
#[test]
|
||||
fn idle_frames_replay_the_document_sections() {
|
||||
let mut app = VectorcraftApp::new(vectorcraft_engine::Session::new(), Default::default());
|
||||
app.run("file.new", json!({"width": 300, "height": 200})).unwrap();
|
||||
let mut panel = Panel::new(app, document_sections);
|
||||
panel.room(egui::vec2(400.0, 1000.0));
|
||||
panel.frame();
|
||||
panel.frame();
|
||||
let before = runs();
|
||||
for _ in 0..10 {
|
||||
panel.frame();
|
||||
}
|
||||
assert_eq!(runs() - before, 0, "idle frames run the memoized sections no more");
|
||||
let point = panel.app().ui.view.snap_to_point;
|
||||
panel.app().run("view.snapToPoint", json!({})).unwrap();
|
||||
panel.frame();
|
||||
panel.frame();
|
||||
assert!(runs() > before, "a new snap setting runs the sections again");
|
||||
assert_eq!(panel.app().ui.view.snap_to_point, !point);
|
||||
assert_eq!(panel.toggled("Snap to Point"), Some(craft::behavior::Mixed::Same(!point)), "and the checkbox shows it");
|
||||
panel.app().run("view.rulers", json!({})).unwrap();
|
||||
let mut per_frame = Vec::new();
|
||||
for _ in 0..12 {
|
||||
let at = runs();
|
||||
panel.frame();
|
||||
per_frame.push(runs() - at);
|
||||
}
|
||||
// A toggled button animates over the next frames, and a section runs while an animation in it has not ended.
|
||||
assert_eq!(per_frame[6..], [0; 6], "idle frames after a command run them no more: {per_frame:?}");
|
||||
}
|
||||
|
||||
/// The frame that picks new units in the Units menu shows the Keyboard Increment in them: the Preferences
|
||||
/// section's key reads the General unit after the pick runs, as its lengths do.
|
||||
#[test]
|
||||
fn the_frame_that_picks_units_shows_the_increment_in_them() {
|
||||
let mut app = VectorcraftApp::new(vectorcraft_engine::Session::new(), Default::default());
|
||||
app.run("file.new", json!({"width": 300, "height": 200})).unwrap();
|
||||
let initial = app.session.general_unit();
|
||||
let mut panel = Panel::new(app, document_sections);
|
||||
panel.room(egui::vec2(400.0, 1000.0));
|
||||
panel.frame();
|
||||
let before = panel.value("Keyboard Increment");
|
||||
panel.click_role("Units", Role::COMBO_BOX);
|
||||
panel.key(egui::Key::ArrowDown, egui::Modifiers::NONE);
|
||||
panel.key(egui::Key::ArrowDown, egui::Modifiers::NONE);
|
||||
let enter =
|
||||
|pressed| egui::Event::Key { key: egui::Key::Enter, physical_key: None, pressed, repeat: false, modifiers: egui::Modifiers::NONE };
|
||||
panel.frame_with(vec![enter(true), enter(false)]);
|
||||
assert_ne!(panel.app().session.general_unit(), initial, "Enter applies the chosen unit");
|
||||
let picked = panel.value("Keyboard Increment");
|
||||
panel.frame();
|
||||
let settled = panel.value("Keyboard Increment");
|
||||
assert_ne!(settled, before, "the increment shows in the new unit");
|
||||
assert_eq!(picked, settled, "the frame of the pick already shows the increment in the new unit");
|
||||
}
|
||||
|
||||
/// The memoized sections still take input: a click on Snap to Grid turns it on, once, and Scale Strokes & Effects
|
||||
/// sets its preference.
|
||||
#[test]
|
||||
fn the_memoized_sections_run_their_picks() {
|
||||
let mut app = VectorcraftApp::new(vectorcraft_engine::Session::new(), Default::default());
|
||||
app.run("file.new", json!({"width": 300, "height": 200})).unwrap();
|
||||
let grid = app.ui.view.snap_to_grid;
|
||||
let scale = app.session.prefs.scale_strokes;
|
||||
let mut panel = Panel::new(app, document_sections);
|
||||
panel.room(egui::vec2(400.0, 1000.0));
|
||||
panel.frame();
|
||||
panel.click("Snap to Grid");
|
||||
assert_eq!(panel.app().ui.view.snap_to_grid, !grid, "the click toggles Snap to Grid once");
|
||||
panel.click("Scale Strokes & Effects");
|
||||
assert_eq!(panel.app().session.prefs.scale_strokes, !scale);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user