mirror of
https://github.com/storytold/figcraft.git
synced 2026-12-01 19:04:29 +01:00
Merge pull request #2543 from MMexicann/patch-11
fix(ui): commit plugin fields before button callbacks
This commit is contained in:
2 files changed
+27
-8
No files matched your search
@@ -606,9 +606,10 @@ fn form_window(app: &mut FigApp, ctx: &Context) {
|
||||
/// Parameters or panel controls in two columns (label, field); buttons in a row share one line,
|
||||
/// the first button filled. Returns what the user did to which control.
|
||||
fn controls(ui: &mut Ui, salt: &str, params: &[Param], values: &Map<String, Value>, t: &Tokens) -> Vec<Used> {
|
||||
// In order: a tap's press and release share a frame, so a field that loses the keyboard
|
||||
// commits in the same frame as the button that took it.
|
||||
// A tap can commit a field and press a button in one frame. Apply the field change before
|
||||
// delivering the button event, regardless of the controls' visual order.
|
||||
let mut used = Vec::new();
|
||||
let mut button_hits = Vec::new();
|
||||
let mut first_button = true;
|
||||
egui::Grid::new(("plugin_controls", salt)).num_columns(2).spacing([12.0, 8.0]).show(ui, |ui| {
|
||||
for row in params.chunk_by(|a, b| a.kind == ParamKind::Button && b.kind == ParamKind::Button) {
|
||||
@@ -635,11 +636,14 @@ fn controls(ui: &mut Ui, salt: &str, params: &[Param], values: &Map<String, Valu
|
||||
None
|
||||
}
|
||||
ParamKind::Button => {
|
||||
ui.horizontal(|ui| {
|
||||
let pressed = row.iter().filter(|b| widgets::button(ui, b.title(), std::mem::take(&mut first_button), true).clicked());
|
||||
pressed.last().map(|b| Used::Set(b.name.clone(), Value::Null))
|
||||
})
|
||||
.inner
|
||||
let button = ui
|
||||
.horizontal(|ui| {
|
||||
let pressed = row.iter().filter(|b| widgets::button(ui, b.title(), std::mem::take(&mut first_button), true).clicked());
|
||||
pressed.last().map(|b| Used::Set(b.name.clone(), Value::Null))
|
||||
})
|
||||
.inner;
|
||||
button_hits.extend(button);
|
||||
None
|
||||
}
|
||||
_ => {
|
||||
let current = values.get(&p.name).cloned().unwrap_or_else(|| p.default_value());
|
||||
@@ -650,6 +654,7 @@ fn controls(ui: &mut Ui, salt: &str, params: &[Param], values: &Map<String, Valu
|
||||
ui.end_row();
|
||||
}
|
||||
});
|
||||
used.extend(button_hits);
|
||||
used
|
||||
}
|
||||
|
||||
|
||||
@@ -34,12 +34,17 @@ pub(crate) fn panel_module() -> Vec<u8> {
|
||||
r#"(module
|
||||
(import "figcraft" "call" (func $call (param i32 i32 i32 i32) (result i32)))
|
||||
(import "figcraft" "ui_show" (func $ui_show (param i32 i32) (result i32)))
|
||||
(import "figcraft" "reply_read" (func $reply_read (param i32 i32) (result i32)))
|
||||
(import "figcraft" "set_output" (func $set_output (param i32 i32)))
|
||||
(memory (export "memory") 1)
|
||||
(data (i32.const 1000) "{}") (data (i32.const 100) "shape.rectangle") (data (i32.const 200) "{}")
|
||||
(data (i32.const 49152) "event ")
|
||||
(func (export "figcraft_run") (param $n i32) (result i32)
|
||||
(i32.store (i32.const 0) (i32.add (i32.load (i32.const 0)) (i32.const 1)))
|
||||
(if (i32.eq (i32.load (i32.const 0)) (i32.const 1))
|
||||
(then (return (call $ui_show (i32.const 1000) (i32.const {})))))
|
||||
(drop (call $reply_read (i32.const 49158) (local.get $n)))
|
||||
(call $set_output (i32.const 49152) (i32.add (i32.const 6) (local.get $n)))
|
||||
(call $call (i32.const 100) (i32.const 15) (i32.const 200) (i32.const {}))))"#,
|
||||
spec.replace('"', "\\\""),
|
||||
rect.replace('"', "\\\""),
|
||||
@@ -188,7 +193,12 @@ fn a_slider_calls_the_plugin_back_once_when_let_go() {
|
||||
/// keyboard from commits in the frame the button is clicked, and the plugin gets both, in order.
|
||||
#[test]
|
||||
fn a_tap_on_a_button_keeps_what_was_typed_in_a_field() {
|
||||
let mut u = Ui::new(session());
|
||||
let mut session = session();
|
||||
let open = &mut session.plugins.panels[0];
|
||||
open.panel.controls.retain(|p| matches!(p.name.as_str(), "size" | "go"));
|
||||
open.panel.controls.swap(0, 1);
|
||||
open.values.retain(|name, _| name == "size");
|
||||
let mut u = Ui::new(session);
|
||||
u.ctx.enable_accesskit();
|
||||
u.frame(vec![]);
|
||||
let at = |u: &mut Ui, label: &str| {
|
||||
@@ -208,4 +218,8 @@ fn a_tap_on_a_button_keeps_what_was_typed_in_a_field() {
|
||||
u.frame(vec![]);
|
||||
assert_eq!(u.app.session.plugins.panel("org.test.panel").unwrap().values["size"], 33.0, "the field's value arrived");
|
||||
assert_eq!(layers(&u), before + 2, "the field's commit and the button each called the plugin");
|
||||
let (message, _) = u.app.ui.toast.as_ref().expect("plugin callback output");
|
||||
let event: Value = serde_json::from_str(message.strip_prefix("event ").expect("recorded event")).unwrap();
|
||||
assert_eq!(event["event"]["control"], "go");
|
||||
assert_eq!(event["values"]["size"], 33.0);
|
||||
}
|
||||
Reference in new issue
Block a user