mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-29 01:34:29 +01:00
Scripts: a ScriptUI list box shows only the rows that fit its height at the height it draws them
The list box counted its rows at 18 pixels each, while the list draws each row at the theme's control height, so a list box ran past its place and over the control below it, which then took clicks meant for the list's last row.
This commit is contained in:
1 parent
1d3e835927
commit
c1d4192ae3
2 files changed
+24
-18
No files matched your search
@@ -23,16 +23,18 @@ use craft::behavior::{Selection, SelectionChange, keyed};
|
||||
use craft::theme::Variant;
|
||||
use craft::ui::buttons::Button;
|
||||
use craft::ui::choice::{Checkbox, Radio, RadioGroup};
|
||||
use craft::ui::contract::{Bind, CollectionEvent, Entry, Props as _, Scroll, Show as _, ShowCollection as _, ShowContainer as _, clicked};
|
||||
use craft::ui::contract::{Bind, CollectionEvent, Entry, Props as _, RowHeight, Scroll, Show as _, ShowCollection as _, ShowContainer as _, clicked};
|
||||
use craft::ui::dialog::{Dialog, DialogId, DialogResult, DialogStack, FieldRefusal, StackDialog, dialog_id};
|
||||
use craft::ui::display::{Progress, Text};
|
||||
use craft::ui::layout_components::Stack;
|
||||
use craft::ui::list::{List, ListRow};
|
||||
use craft::ui::pickers::Select;
|
||||
use craft::ui::runtime::env::Env;
|
||||
use craft::ui::runtime::focus::Initial;
|
||||
use craft::ui::runtime::keys::take_submit;
|
||||
use craft::ui::runtime::overlay;
|
||||
use craft::ui::sections::{DialogRole, FooterButton, GroupBox};
|
||||
use craft::ui::style::StyleCx;
|
||||
use craft::ui::text::TextField;
|
||||
use craft::ui::value_drag::Slider;
|
||||
use egui::Ui;
|
||||
@@ -50,15 +52,13 @@ pub const KIND: &str = "scriptWindow";
|
||||
/// The most of the dialog's content shown at once; a larger dialog scrolls.
|
||||
const MAX_SHOWN: egui::Vec2 = egui::vec2(960.0, 640.0);
|
||||
|
||||
/// The height of a list box row in the engine's layout, and the room the layout adds around the
|
||||
/// rows.
|
||||
const LIST_ROW: f32 = 18.0;
|
||||
/// The room the engine's layout adds around a list box's rows.
|
||||
const LIST_PAD: f32 = 6.0;
|
||||
|
||||
/// How many rows a list box `height` tall shows; it scrolls the rest, inside the dialog's own
|
||||
/// scroll.
|
||||
fn list_rows(height: f32) -> u16 {
|
||||
let rows = ((height - LIST_PAD) / LIST_ROW).floor();
|
||||
/// How many rows `row` tall a list box `height` tall shows; it scrolls the rest, inside the
|
||||
/// dialog's own scroll.
|
||||
fn list_rows(height: f32, row: f32) -> u16 {
|
||||
let rows = ((height - LIST_PAD) / row).floor();
|
||||
if rows.is_finite() { rows.clamp(1.0, f32::from(u16::MAX)) as u16 } else { 1 }
|
||||
}
|
||||
|
||||
@@ -517,11 +517,13 @@ fn control(ui: &mut Ui, drafts: &BTreeMap<u64, String>, place: &Place, (i, n, pr
|
||||
// Screen readers read the box as a list box, by its name or the static text before
|
||||
// it, with the selected item as its value. The box has no opener to keep the keyboard
|
||||
// focus, so a press on a row gives it the focus and the arrow keys go on from that row.
|
||||
// The rows it shows are as many as fit in its height at the height the list draws them.
|
||||
let row = RowHeight::Control.of(&StyleCx::of_env(ui.ctx(), &Env::of(ui)));
|
||||
let out = List::<usize>::options(list_name(n, prompt))
|
||||
.focus_on_press(true)
|
||||
.id_salt(salt)
|
||||
.enabled(enabled)
|
||||
.max_rows(list_rows(n.rect.h))
|
||||
.max_rows(list_rows(n.rect.h, row))
|
||||
.selection(Bind::given(selection))
|
||||
.show_items(ui, &source, &mut |k, _| ListRow::new(n.items.get(*k).cloned().unwrap_or_default()));
|
||||
// A click activates a row; the arrow keys and typed letters select one.
|
||||
|
||||
@@ -801,14 +801,18 @@ fn an_arrow_key_and_a_click_in_a_list_box_give_the_script_the_change() {
|
||||
step(&mut app, Vec::new());
|
||||
access_nodes(&mut app);
|
||||
let nodes = access_nodes(&mut app);
|
||||
let b = nodes
|
||||
.iter()
|
||||
.map(|(_, n)| n)
|
||||
.find(|n| n.role() == egui::accesskit::Role::ListItem && n.label() == Some("plum"))
|
||||
.and_then(egui::accesskit::Node::bounds)
|
||||
.unwrap_or_else(|| panic!("no plum row"));
|
||||
// The top half of the row: the OK button below the list starts in the row's lower half.
|
||||
let at = egui::pos2((b.x0 + b.x1) as f32 * 0.5, b.y0 as f32 + 4.0);
|
||||
let bounds = |role: egui::accesskit::Role, label: Option<&str>| {
|
||||
nodes
|
||||
.iter()
|
||||
.map(|(_, n)| n)
|
||||
.find(|n| n.role() == role && (label.is_none() || n.label() == label))
|
||||
.and_then(egui::accesskit::Node::bounds)
|
||||
.unwrap_or_else(|| panic!("no {role:?} {label:?}"))
|
||||
};
|
||||
let (list, ok) = (bounds(egui::accesskit::Role::ListBox, None), bounds(egui::accesskit::Role::Button, Some("OK")));
|
||||
assert!(list.y1 <= ok.y0, "the list box ends above the OK button below it: {list:?} {ok:?}");
|
||||
let b = bounds(egui::accesskit::Role::ListItem, Some("apple"));
|
||||
let at = egui::pos2((b.x0 + b.x1) as f32 * 0.5, ((b.y0 + b.y1) * 0.5) as f32);
|
||||
let button = |pressed| egui::Event::PointerButton { pos: at, button: egui::PointerButton::Primary, pressed, modifiers: egui::Modifiers::NONE };
|
||||
step(&mut app, vec![egui::Event::PointerMoved(at)]);
|
||||
step(&mut app, vec![button(true)]);
|
||||
@@ -816,7 +820,7 @@ fn an_arrow_key_and_a_click_in_a_list_box_give_the_script_the_change() {
|
||||
assert!(until_window(&mut app), "the change handler runs again");
|
||||
give(&mut app, json!({"event": "click", "control": "OK"})).unwrap();
|
||||
assert!(!until_window(&mut app));
|
||||
assert_eq!(log(&app), json!([[1, [1, 2]]]), "the arrow key picked pear, the click picked plum, and the script was told each time");
|
||||
assert_eq!(log(&app), json!([[1, [1, 0]]]), "the arrow key picked pear, the click picked apple, and the script was told each time");
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
Reference in new issue
Block a user