Preferences docs name host-labelled rows; trials for their search and for a palette's context menu popup

This commit is contained in:
aiongg committed 2026-11-21 10:18:40 +00:00
1 parent 69ed953094
commit 9bb77ccba4
3 files changed
+99 -7

No files matched your search

@@ -102,8 +102,8 @@ pub struct RowSpec<'a> {
pub key: &'a str,
/// Translated; also the row's accessible name.
pub label: &'a str,
/// Extra search text: the key, the English label, synonyms. A [`RowKind::Custom`] row, which the search can't look
/// into, lists the terms of its control here.
/// Extra search text: the key, the English label, synonyms. A [`RowKind::Custom`] or [`RowKind::CustomRows`] row,
/// which the search can't look into, lists the terms of what the host draws here.
pub keywords: &'a str,
/// Help under the control: "Applies at next launch." A line break starts a new paragraph. The search matches it.
pub help: Option<&'a str>,
@@ -206,8 +206,9 @@ pub enum RowKind<'a> {
Text(&'a str),
/// The host draws the control ([`PreferencesModel::custom_row`]); the window labels it beside the control.
Custom,
/// The host draws whole rows, labels included ([`PreferencesModel::custom_row`]): the window adds no label. The
/// row's label stays its search text and its group's name.
/// The host draws whole rows, labels included ([`PreferencesModel::custom_row`]): the window adds no label, and
/// shows the row's help under them and its enabled state on them. The row's label stays its search text and its
/// group's name.
CustomRows,
}
@@ -301,7 +302,8 @@ pub trait PreferencesModel {
fn set_color(&mut self, key: &str, color: Srgba) {
self.set(key, PrefValue::Color(color.into()));
}
/// Draws a [`RowKind::Custom`] row's control.
/// Draws a [`RowKind::Custom`] row's control, which the window labels from the row's label (an empty label shows
/// an empty one), or a [`RowKind::CustomRows`] row's whole rows, labels included.
fn custom_row(&mut self, ui: &mut Ui, key: &str);
/// Draws a [`PageSpec::custom`] page.
fn custom_page(&mut self, ui: &mut Ui, page: &str);
+69 -2
View File
@@ -1,20 +1,23 @@
//! Native dock windows: when a float gets an OS window, what the dock records when the OS moves it, the OS close, and
//! docking the whole window by dragging its title row.
//! docking the whole window by dragging its title row. A context menu opened in a palette places its popup window as
//! it places the menu in the main window.
use craft_behavior::dock::{Action, Column, DockEdge, Floating, Group, Layout, Node, Permissions, Workspace};
use craft_behavior::placement::Placement;
use craft_host::target::{Backend, Caps, Os, ShellInfo};
use craft_ui::buttons::Button;
use craft_ui::contract::Role;
use craft_ui::contract::{Bind, BodyCx, DropMark, PlainData, Show as _, ShowContainer as _, SizeLimits, Tracked};
use craft_ui::display::Text;
use craft_ui::docking::{DockConfig, DockGrid, DockPanels, PanelSpec, PanelSpecs};
use craft_ui::menu::{Menu, MenuItem};
use craft_ui::patterns::dock::DockArea;
use craft_ui::popover::DropVeil;
use craft_ui::runtime::InstallSpec;
use craft_ui::runtime::drag;
use craft_ui::testing::access::AccessTree;
use craft_ui::testing::input::key_press;
use craft_ui::testing::pointer::{moved, press, release};
use craft_ui::testing::pointer::{moved, press, release, secondary};
use craft_ui::testing::trial;
use craft_ui::testing::windows::FakeSystem;
use craft_ui_test_kit::windows::{Out, Windows};
@@ -1218,3 +1221,67 @@ fn scaled(focused: bool, rect: Rect, pixels_per_point: f32) -> ViewportInfo {
..ViewportInfo::default()
}
}
/// How far down panel 1's body the context menu's target sits, and the menu's rows: in the native palette at
/// `FLOAT_AT`, the pointer on the target has about 300 points above it and below it in the windows' span, and the
/// menu is taller than either and shorter than the span.
const CONTEXT_DROP: f32 = 220.0;
const CONTEXT_ROWS: usize = 18;
/// A palette whose panel 1 holds a button with a context menu of `CONTEXT_ROWS` rows.
struct ContextTarget;
impl DockPanels<u32> for ContextTarget {
fn body(&mut self, panel: &u32, ui: &mut Ui, _: &mut BodyCx<'_>) {
if *panel != 1 {
return;
}
ui.add_space(CONTEXT_DROP);
let target = Button::new("Target").show(ui);
let _ = Menu::context("Target menu", &target).show(ui, |ui, _| {
for at in 0..CONTEXT_ROWS {
MenuItem::new(format!("Row {at}")).show(ui);
}
});
}
}
/// A context menu opened in a native palette shows in a popup OS window; too tall for the room on either side of the
/// pointer but not for the room the popup has, it covers the pointer and shows every row, as it does in the main window.
#[trial]
fn a_context_menu_from_a_native_palette_too_tall_for_either_side_covers_the_pointer_in_its_popup_window() {
let mut dock = NativeDock::on(Os::Windows, Arc::new(FakeSystem::new()));
dock.windows.root_info(scaled(true, MAIN_AT, COLLAPSED_SCALE));
let mut at = FLOAT_AT;
dock.windows.child_info(scaled(true, at, COLLAPSED_SCALE));
let mut panels = ContextTarget;
let out = collapsed_frame(&mut dock, &mut panels, &mut at, vec![], vec![]);
let palette = dock.palette.expect("the palette's window opened");
let target = node_rect(tree_of_viewport(&out, palette), "Target", Role::BUTTON).expect("the palette shows the target").center();
for events in [vec![moved(target)], vec![secondary(target, true)], vec![secondary(target, false)]] {
collapsed_frame(&mut dock, &mut panels, &mut at, vec![], events);
}
let mut out = collapsed_frame(&mut dock, &mut panels, &mut at, vec![], vec![]);
for _ in 0..4 {
out = collapsed_frame(&mut dock, &mut panels, &mut at, vec![], vec![]);
}
let menu = popups(&dock, &out)
.into_iter()
.find(|viewport| !tree_of_viewport(&out, *viewport).with_role(Role::MENU).is_empty())
.expect("the menu shows in a popup window");
let rect = window_rect(&dock, menu);
let pointer = at.min + target.to_vec2();
let room = MAIN_AT.union(at);
assert!(
rect.height() > pointer.y - room.top() && rect.height() > room.bottom() - pointer.y,
"the menu {rect:?} fits on neither side of {pointer:?}"
);
assert!(room.contains_rect(rect.shrink(0.5)), "the menu {rect:?} stays inside the windows' span {room:?}");
assert!(rect.contains(pointer), "the menu {rect:?} covers the pointer at {pointer:?}");
let tree = tree_of_viewport(&out, menu);
let window = Rect::from_min_size(Pos2::ZERO, rect.size()).expand(FIT_ROUNDING);
for row in [0, CONTEXT_ROWS - 1] {
let shown = node_rect(tree, &format!("Row {row}"), Role::MENU_ITEM).unwrap_or_else(|| panic!("row {row} shows"));
assert!(window.contains_rect(shown), "row {row} {shown:?} lies whole in the menu's window {window:?}");
}
}
@@ -1,8 +1,11 @@
//! The settings document without a window: rows, applied edits, rebase and row conditions.
use craft_i18n::Folder;
use craft_i18n::matcher::Matcher;
use craft_theme::Rgba;
use craft_ui::patterns::preferences::{
CustomSettings, PageInfo, PrefValue, PreferencesDraft, PreferencesModel, RowKind, SettingRule, SettingsDocument, SettingsDraft, StandardPage,
list_pages, row_shown,
};
use craft_ui::testing::trial;
use serde::{Deserialize, Serialize};
@@ -276,3 +279,23 @@ fn a_removed_draft_value_applies_as_no_value() {
assert_eq!(edits, vec![("memory.percent".to_owned(), None)]);
assert_eq!(doc.saved().memory.percent, None);
}
/// A row whose app draws its own labels is found by its rule's label: the search lists its page and shows it.
#[trial]
fn the_search_finds_a_host_labelled_row_by_its_rules_label() {
let rules = vec![
SettingRule::new("export.format").choices(&[("png", "PNG"), ("webp", "WebP")]),
SettingRule::new("export.tablet").label("Tablet pressure").custom_rows(),
];
let doc = SettingsDocument::new(ExportPrefs::default()).expect("serializes").rules(rules);
let pages = PreferencesModel::pages(&doc);
let matcher = Matcher::new(&Folder::new("en"), "tablet");
let listed = list_pages(&pages, &matcher);
let found: Vec<(&str, bool)> = listed
.iter()
.flat_map(|listed| pages[listed.index].groups.iter().flat_map(|group| group.rows.iter()).map(move |row| (row, listed)))
.filter(|(row, listed)| row_shown(&matcher, listed, row))
.map(|(row, _)| (row.key, matches!(row.kind, RowKind::CustomRows)))
.collect();
assert_eq!(found, [("export.tablet", true)], "the search shows the host-labelled row alone");
}