mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-29 09:46:19 +01:00
UI: a highlighted Blend Mode, Library or font Style applies only once it is chosen
The Transparency panel's Blend Mode, the Libraries panel's Library and the font fields' Style select are bound to a value the code compares with the document after each pass. Under the default write-through policy, an entry highlighted with the arrow keys was written to that value, so the code ran transparency.set, library.setCurrent or the font command before the entry was chosen, and Escape ran it again to put the value back. Each binding now writes only on commit (EditPolicy::OnCommit), so Enter or a click applies the entry.
This commit is contained in:
1 parent
170d491cb9
commit
893f0db16a
5 files changed
+103
-5
No files matched your search
@@ -16,7 +16,7 @@ use std::hash::{Hash, Hasher};
|
||||
use std::sync::Arc;
|
||||
use std::time::SystemTime;
|
||||
|
||||
use craft::behavior::{EditEvent, Preview};
|
||||
use craft::behavior::{EditEvent, EditPolicy, Preview};
|
||||
use craft::font_manager::picker::{FamilySet, FilterLayout, FontFilters, FontPicker, FontPickerSettings, FontPreviewSize, FontScript};
|
||||
use craft::ui::contract::{Bind, Entry, Show as _};
|
||||
use craft::ui::fonts::{FaceBytes, FaceState};
|
||||
@@ -204,7 +204,7 @@ pub(crate) fn font_fields(app: &mut VectorcraftApp, ui: &mut Ui) {
|
||||
let styles = FontDb::global().styles(&s.font_family);
|
||||
let entries: Vec<Entry<String>> = styles.iter().map(|name| Entry::new(name.clone(), name.clone())).collect();
|
||||
let mut shown = s.font_style.clone();
|
||||
Select::new(tl!("Style"), Bind::bound(&mut shown), &entries).show(ui);
|
||||
Select::new(tl!("Style"), Bind::bound(&mut shown).policy(EditPolicy::OnCommit), &entries).show(ui);
|
||||
if shown != s.font_style {
|
||||
apply_font(app, &s.font_family, Some(&shown), Live::Released);
|
||||
}
|
||||
|
||||
@@ -270,3 +270,43 @@ fn a_preview_size_asked_for_sets_the_preference_and_keeps_the_preview() {
|
||||
"undo takes the type away: no step was added for the size"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_highlighted_font_style_applies_only_once_it_is_chosen() {
|
||||
use craft::ui::contract::Role;
|
||||
use egui::{Key, Modifiers};
|
||||
|
||||
use crate::tests::panel_harness::Panel;
|
||||
|
||||
let styles = vectorcraft_text::FontDb::global().styles("Source Sans 3");
|
||||
assert!(styles.len() >= 2, "{styles:?}");
|
||||
let mut app = app_with_type(&[]);
|
||||
let params = serde_json::json!({"x": 20, "y": 90, "text": "Style", "font": "Source Sans 3", "style": styles[0]});
|
||||
let id = app.run("text.create", params).unwrap()["id"].as_u64().unwrap();
|
||||
app.run("select.set", serde_json::json!({"ids": [id]})).unwrap();
|
||||
let mut panel = Panel::new(app, super::font_fields);
|
||||
let state = |panel: &Panel| {
|
||||
let app = panel.app();
|
||||
let style = match &app.session.active().unwrap().doc.node(vectorcraft_doc::NodeId(id)).unwrap().kind {
|
||||
vectorcraft_doc::NodeKind::Text(t) => t.first_style().font_style,
|
||||
_ => String::new(),
|
||||
};
|
||||
(style, app.session.doc().unwrap().history.undo.len())
|
||||
};
|
||||
let before = state(&panel);
|
||||
assert_eq!(before.0, styles[0]);
|
||||
panel.click_role("Style", Role::COMBO_BOX);
|
||||
panel.key(Key::ArrowDown, Modifiers::NONE);
|
||||
panel.frame();
|
||||
assert_eq!(state(&panel), before, "a highlighted style changes nothing");
|
||||
panel.key(Key::Escape, Modifiers::NONE);
|
||||
panel.frame();
|
||||
assert_eq!(state(&panel), before, "Escape leaves the style as it was");
|
||||
panel.click_role("Style", Role::COMBO_BOX);
|
||||
panel.key(Key::ArrowDown, Modifiers::NONE);
|
||||
panel.key(Key::Enter, Modifiers::NONE);
|
||||
panel.frame();
|
||||
let (style, undo) = state(&panel);
|
||||
assert_eq!(style, styles[1], "Enter applies the style");
|
||||
assert_eq!(undo, before.1 + 1, "the choice is one undo step");
|
||||
}
|
||||
@@ -14,7 +14,7 @@ use std::cell::RefCell;
|
||||
use std::collections::{HashMap, HashSet};
|
||||
use std::sync::Arc;
|
||||
|
||||
use craft::behavior::{Axis, EditEvent, IconId, keyed};
|
||||
use craft::behavior::{Axis, EditEvent, EditPolicy, IconId, keyed};
|
||||
use craft::host::PickRequest;
|
||||
use craft::keymap::CommandRef;
|
||||
use craft::theme::variant::Tone;
|
||||
@@ -843,7 +843,7 @@ fn header(ui: &mut Ui, libraries: &Libraries, current: &str, list_view: &mut boo
|
||||
];
|
||||
let mut picked = current.to_string();
|
||||
Row::new().fill_width().show(ui, |ui, _| {
|
||||
Select::new(tl!("Library"), Bind::bound(&mut picked), &names).sizing(Sizing::FillWidth).show(ui);
|
||||
Select::new(tl!("Library"), Bind::bound(&mut picked).policy(EditPolicy::OnCommit), &names).sizing(Sizing::FillWidth).show(ui);
|
||||
Segmented::new(tl!("View"), Bind::bound(&mut *list_view), &views).show(ui);
|
||||
});
|
||||
if picked != current {
|
||||
|
||||
@@ -566,6 +566,33 @@ fn menu_rows_and_dialogs_run_library_commands() {
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_highlighted_library_becomes_current_only_once_it_is_chosen() {
|
||||
use craft::ui::contract::Role;
|
||||
|
||||
let mut app = app();
|
||||
for name in ["Brand", "House"] {
|
||||
app.run("library.create", json!({"name": name})).unwrap();
|
||||
}
|
||||
let ids: Vec<String> = app.session.libraries.all().iter().map(|(id, _)| id.clone()).collect();
|
||||
app.run("library.setCurrent", json!({"library": ids[0]})).unwrap();
|
||||
let mut panel = Harness::new(app, show);
|
||||
let current = |panel: &Harness| panel.app().session.libraries.current().map(|id| id.to_string());
|
||||
assert_eq!(current(&panel).as_deref(), Some(ids[0].as_str()));
|
||||
panel.click_role("Library", Role::COMBO_BOX);
|
||||
panel.key(Key::ArrowDown, Modifiers::NONE);
|
||||
panel.frame();
|
||||
assert_eq!(current(&panel).as_deref(), Some(ids[0].as_str()), "a highlighted library does not become current");
|
||||
panel.key(Key::Escape, Modifiers::NONE);
|
||||
panel.frame();
|
||||
assert_eq!(current(&panel).as_deref(), Some(ids[0].as_str()), "Escape leaves the current library as it was");
|
||||
panel.click_role("Library", Role::COMBO_BOX);
|
||||
panel.key(Key::ArrowDown, Modifiers::NONE);
|
||||
panel.key(Key::Enter, Modifiers::NONE);
|
||||
panel.frame();
|
||||
assert_eq!(current(&panel).as_deref(), Some(ids[1].as_str()), "Enter makes the chosen library current");
|
||||
}
|
||||
|
||||
/// Export Library… writes the library's file under its name and Import Library… adds it back as a
|
||||
/// new library.
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
|
||||
@@ -7,6 +7,7 @@
|
||||
use std::cell::RefCell;
|
||||
use std::sync::Arc;
|
||||
|
||||
use craft::behavior::EditPolicy;
|
||||
use craft::behavior::IconId;
|
||||
use craft::behavior::Mixed;
|
||||
use craft::behavior::item_source::keyed;
|
||||
@@ -245,7 +246,7 @@ pub fn show(app: &mut VectorcraftApp, ui: &mut Ui) {
|
||||
let entries = blend_entries();
|
||||
let mut opacity = None;
|
||||
Row::new().show(ui, |ui, _| {
|
||||
Select::new(tl!("Blend Mode"), Bind::bound_mixed(&mut shown_blend), &entries).enabled(has).show(ui);
|
||||
Select::new(tl!("Blend Mode"), Bind::bound_mixed(&mut shown_blend).policy(EditPolicy::OnCommit), &entries).enabled(has).show(ui);
|
||||
opacity = Some(
|
||||
NumberField::new(tl!("Opacity"), Bind::given(op.map(|o| f64::from(o) * 100.0)))
|
||||
.unit_kind(UnitKind::Percent)
|
||||
@@ -692,4 +693,34 @@ mod tests {
|
||||
assert_eq!((info.opacity, undone.opacity), (Some(100.0), 1.0));
|
||||
assert!(Arc::ptr_eq(&undone, &held(&app, n.id)), "the node Undo put back");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_highlighted_blend_mode_applies_only_once_it_is_chosen() {
|
||||
use egui::{Key, Modifiers};
|
||||
|
||||
let mut app = new_app();
|
||||
let id = run_cmd(&mut app, "shape.rectangle", json!({"x": 10, "y": 10, "width": 40, "height": 40}))["id"].clone();
|
||||
run_cmd(&mut app, "select.set", json!({"ids": [id]}));
|
||||
let mut panel = Panel::new(app, show);
|
||||
let state = |panel: &Panel| {
|
||||
let app = panel.app();
|
||||
(app.session.transparency_info().blend, app.session.active().unwrap().history.undo.len())
|
||||
};
|
||||
let before = state(&panel);
|
||||
assert_eq!(before.0, Some(vectorcraft_color::BlendMode::Normal));
|
||||
panel.click_role("Blend Mode", Role::COMBO_BOX);
|
||||
panel.key(Key::ArrowDown, Modifiers::NONE);
|
||||
panel.frame();
|
||||
assert_eq!(state(&panel), before, "a highlighted blend mode changes nothing");
|
||||
panel.key(Key::Escape, Modifiers::NONE);
|
||||
panel.frame();
|
||||
assert_eq!(state(&panel), before, "Escape leaves the blend mode as it was");
|
||||
panel.click_role("Blend Mode", Role::COMBO_BOX);
|
||||
panel.key(Key::ArrowDown, Modifiers::NONE);
|
||||
panel.key(Key::Enter, Modifiers::NONE);
|
||||
panel.frame();
|
||||
let (blend, undo) = state(&panel);
|
||||
assert_ne!(blend, before.0, "Enter applies the blend mode");
|
||||
assert_eq!(undo, before.1 + 1, "the choice is one undo step");
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user