mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-25 20:14:31 +01:00
ui-widgets: IconButton::choice reads as a combo box named by its label, valued by the picked row, expanded while the container hung from it is open (contract::overlay::opened_from)
This commit is contained in:
1 parent
879b8f82a8
commit
80e46cf00e
5 files changed
+86
-12
No files matched your search
@@ -16,7 +16,7 @@ mod props;
|
||||
mod toggles;
|
||||
mod ui;
|
||||
|
||||
pub use container_open::Popup;
|
||||
pub use container_open::{Popup, opened_from};
|
||||
pub(crate) use container_open::{opener_open, set_opener_open};
|
||||
pub use host::paint_frame_layers;
|
||||
pub(crate) use host::{Layer, forget_layer, host, host_window};
|
||||
|
||||
@@ -61,8 +61,11 @@ pub(crate) fn opener_open(stage: &mut MemoryStage, first: &Opener) -> bool {
|
||||
open
|
||||
}
|
||||
|
||||
/// Whether the container hung from the opener `opener` (the id of its response) is open, as its slot holds it.
|
||||
pub(crate) fn opened_from(ctx: &egui::Context, opener: Id) -> bool {
|
||||
/// Whether the container hung from the opener `opener` (the id of its response, [`Overlay::opened_by`]) is open, as
|
||||
/// its slot holds it: an opener reports its expanded state by it.
|
||||
///
|
||||
/// [`Overlay::opened_by`]: super::Overlay::opened_by
|
||||
pub fn opened_from(ctx: &egui::Context, opener: Id) -> bool {
|
||||
MemoryStage::peek_session::<OpenerOpen>(ctx, sub(opener, "opens")).open
|
||||
}
|
||||
|
||||
|
||||
@@ -6,6 +6,7 @@ use crate::contract::{Access, Common, Component, Cx, MeasureCx, PaintUi, PopupKi
|
||||
use crate::controls::disclosure::DisclosureGlyph;
|
||||
use crate::primitives::{IconSpec, PaintShape, StrokeWidth};
|
||||
use crate::runtime::commands::CommandRef;
|
||||
use crate::runtime::env::Label;
|
||||
use crate::style::{ComponentStyle, FocusLook, IconLook, StyleColor, StyleCx, SurfaceLook};
|
||||
use craft_behavior::{Metrics, Sense};
|
||||
use craft_i18n::Direction;
|
||||
@@ -65,7 +66,9 @@ pub enum PopupMark {
|
||||
|
||||
/// A square button showing only an icon. Its label is its accessible name; a tooltip is the caller's. A selected
|
||||
/// button (`selected(true)`) shows the on look: the host owns the state and a click is reported in the response.
|
||||
/// [`ButtonOutput::held`] reports the press from its start to its end.
|
||||
/// [`ButtonOutput::held`] reports the press from its start to its end. A button whose menu picks one of its rows (an
|
||||
/// Align To button) gives the picked row as its [`choice`](Self::choice): it then reads as a combo box named by its
|
||||
/// label, with the choice as its value.
|
||||
#[derive(crate::contract::Component, crate::contract::Props)]
|
||||
pub struct IconButton<'a> {
|
||||
#[common]
|
||||
@@ -82,15 +85,27 @@ pub struct IconButton<'a> {
|
||||
/// that opens a menu have one.
|
||||
#[prop]
|
||||
popup_mark: PopupMark,
|
||||
/// Whether the popup the button opens is open, as the host knows it.
|
||||
/// Whether the popup the button opens is open, as the host knows it. A button with a [`choice`](Self::choice)
|
||||
/// reports, without it, whether the container hung from it is open.
|
||||
#[prop(into)]
|
||||
expanded: Option<bool>,
|
||||
/// The row the menu it opens has picked, for a button that picks one of its menu's rows.
|
||||
#[prop(into)]
|
||||
choice: Option<Label>,
|
||||
}
|
||||
|
||||
impl<'a> IconButton<'a> {
|
||||
/// An icon button showing `icon`; `label` it with the accessible name.
|
||||
pub fn new(icon: impl Into<IconSpec<'a>>) -> Self {
|
||||
IconButton { common: Common::new(), icon: icon.into(), focusable: true, popup: None, popup_mark: PopupMark::None, expanded: None }
|
||||
IconButton {
|
||||
common: Common::new(),
|
||||
icon: icon.into(),
|
||||
focusable: true,
|
||||
popup: None,
|
||||
popup_mark: PopupMark::None,
|
||||
expanded: None,
|
||||
choice: None,
|
||||
}
|
||||
}
|
||||
|
||||
/// An icon button bound to `command`: its label, enabled state and activation follow the command.
|
||||
@@ -101,6 +116,7 @@ impl<'a> IconButton<'a> {
|
||||
|
||||
impl Component for IconButton<'_> {
|
||||
const ROLE: Role = Role::BUTTON;
|
||||
const ALT_ROLES: &'static [Role] = &[Role::COMBO_BOX];
|
||||
const PARTS: bool = false;
|
||||
const SHOWS_LABEL: bool = false;
|
||||
const OVERLAYS: bool = false;
|
||||
@@ -176,15 +192,23 @@ impl Component for IconButton<'_> {
|
||||
}
|
||||
}
|
||||
|
||||
fn access(&self, access: &mut Access, _: &Cx<'_, Self>) {
|
||||
fn access(&self, access: &mut Access, cx: &Cx<'_, Self>) {
|
||||
if let Some(kind) = self.popup {
|
||||
access.has_popup(kind);
|
||||
}
|
||||
if let Some(open) = self.expanded {
|
||||
if self.choice.is_some() {
|
||||
access.role(Role::COMBO_BOX);
|
||||
}
|
||||
let shown = || self.choice.is_some().then(|| crate::contract::overlay::opened_from(&cx.ctx, cx.response.id));
|
||||
if let Some(open) = self.expanded.or_else(shown) {
|
||||
access.expanded(open);
|
||||
}
|
||||
}
|
||||
|
||||
fn value_text(&self, cx: &Cx<'_, Self>) -> Option<String> {
|
||||
self.choice.as_ref().map(|choice| cx.env.resolve(choice).as_str().to_owned())
|
||||
}
|
||||
|
||||
fn finish(self, cx: Cx<'_, Self>) -> ButtonOutput {
|
||||
ButtonOutput::of(&cx, self.activated(&cx))
|
||||
}
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
//! Stories of the buttons.
|
||||
|
||||
use super::{Arrangement, Button, ButtonChord, ControlPlace, GroupLevel, IconButton, Link, SplitButton, SplitMenu, ToggleButton};
|
||||
use crate::contract::{BodyCx, Entry, LeastWidth, Props as _, Show as _, ShowContainer as _, Sizing, Validity};
|
||||
use super::{Arrangement, Button, ButtonChord, ControlPlace, GroupLevel, IconButton, Link, PopupMark, SplitButton, SplitMenu, ToggleButton};
|
||||
use crate::contract::{BodyCx, Entry, LeastWidth, PopupKind, Props as _, Show as _, ShowContainer as _, Sizing, Trigger, Validity};
|
||||
use crate::display::{Picture, Shade, Text};
|
||||
use crate::layout_components::{Grid, Row, Stack};
|
||||
use crate::list::ThumbAspect;
|
||||
use crate::menu::MenuItem;
|
||||
use crate::menu::{Menu, MenuItem};
|
||||
use crate::primitives::{AppIcons, HAlign, IconMark, IconSpec, register_app_icons};
|
||||
use crate::runtime::commands::EditCommands;
|
||||
use crate::runtime::env::{Message, with_env};
|
||||
@@ -79,6 +79,28 @@ fn icon_button(ui: &mut Ui, _: &mut StoryData) -> StoryOutput {
|
||||
IconButton::new(IconId::new("settings")).label("Settings").show(ui).into()
|
||||
}
|
||||
|
||||
/// A menu button that picks one of its menu's rows, as an Align To button does: it reads as a combo box named by its
|
||||
/// label, with the picked row as its value, and the picked row carries the check mark.
|
||||
#[story(IconButton<'static>, covers = [], title = "Choice")]
|
||||
fn icon_button_choice(ui: &mut Ui, data: &mut StoryData) -> StoryOutput {
|
||||
const CHOICES: [&str; 3] = ["Align to Selection", "Align to Key Object", "Align to Artboard"];
|
||||
let picked = *data.value("picked", 0_usize);
|
||||
let button = IconButton::new(IconId::new("align_edge_left"))
|
||||
.label("Align To")
|
||||
.popup(PopupKind::Menu)
|
||||
.popup_mark(PopupMark::Menu)
|
||||
.choice(CHOICES[picked])
|
||||
.show(ui);
|
||||
Menu::new("Align To").opened_by(&button.response, Trigger::Press).show(ui, |ui, _| {
|
||||
for (at, choice) in CHOICES.into_iter().enumerate() {
|
||||
if MenuItem::new(choice).check(at == picked).id_salt(at).show(ui).ran {
|
||||
*data.value("picked", 0_usize) = at;
|
||||
}
|
||||
}
|
||||
});
|
||||
button.into()
|
||||
}
|
||||
|
||||
/// A tool strip: the first tool selected, as the gallery card shows it.
|
||||
#[story(IconButton<'static>, covers = [], drive = Drive::Never)]
|
||||
fn icon_button_selected(ui: &mut Ui, _: &mut StoryData) -> StoryOutput {
|
||||
|
||||
@@ -8,9 +8,10 @@ use craft_theme::ControlSize;
|
||||
use craft_theme::Variant;
|
||||
use craft_ui::buttons::{Arrangement, Button, ButtonChord, ControlPlace, GroupLevel, IconButton, SplitButton, SplitMenu, ToggleButton};
|
||||
use craft_ui::contract::tooltip::DELAY;
|
||||
use craft_ui::contract::{Bind, CommandSet, Entry, Hover, Props as _, Role, Show as _, ShowContainer as _};
|
||||
use craft_ui::contract::{Bind, CommandSet, Entry, Hover, PopupKind, Props as _, Role, Show as _, ShowContainer as _, Trigger};
|
||||
use craft_ui::display::Picture;
|
||||
use craft_ui::list::ThumbAspect;
|
||||
use craft_ui::menu::{Menu, MenuItem};
|
||||
use craft_ui::pickers::ComboBox;
|
||||
use craft_ui::primitives::IconSpec;
|
||||
use craft_ui::runtime::commands::{AppCatalog, Binding, CommandDef, CommandInput, CommandRef, CommandState, Scope};
|
||||
@@ -816,3 +817,27 @@ fn an_icon_only_toggle_in_a_field_without_label_text_keeps_its_name() {
|
||||
let toggle = fixture.by_label("Convert Cyan to process").expect("the toggle keeps its name");
|
||||
assert!(fixture.by_role(Role::BUTTON).iter().any(|button| button.node == toggle.node), "the named node is the toggle button");
|
||||
}
|
||||
|
||||
/// A menu button given the row its menu picked reads as a combo box named by its label, with that row as its value; a
|
||||
/// menu button without one stays a button with no value.
|
||||
#[trial]
|
||||
fn a_menu_button_with_a_choice_reads_as_a_combo_box_valued_by_it() {
|
||||
let mut fixture = Fixture::new(|ui| {
|
||||
let align = IconButton::new(IconId::new("align_edge_left")).label("Align To").popup(PopupKind::Menu).choice("Align to Artboard").show(ui);
|
||||
Menu::new("Align To").opened_by(&align.response, Trigger::Press).show(ui, |ui, _| {
|
||||
MenuItem::new("Align to Artboard").check(true).show(ui);
|
||||
});
|
||||
IconButton::new(IconId::new("more")).label("More").popup(PopupKind::Menu).show(ui);
|
||||
});
|
||||
fixture.settle();
|
||||
let align = fixture.by_label("Align To").expect("the choice button is in the access tree");
|
||||
let more = fixture.by_label("More").expect("the plain menu button is in the access tree");
|
||||
let tree = fixture.tree();
|
||||
assert_eq!(tree.role_of(align.node), Some(Role::COMBO_BOX), "a button with a choice is a combo box");
|
||||
assert_eq!(tree.value_of(align.node), Some("Align to Artboard"), "its value is the choice");
|
||||
assert_eq!(tree.expanded_of(align.node), Some(false), "its menu is closed");
|
||||
assert_eq!(tree.role_of(more.node), Some(Role::BUTTON), "a menu button without a choice stays a button");
|
||||
assert_eq!(tree.value_of(more.node), None, "and has no value");
|
||||
fixture.click_at(align.center()).settle();
|
||||
assert_eq!(fixture.tree().expanded_of(align.node), Some(true), "it reports its menu open");
|
||||
}
|
||||
Reference in new issue
Block a user