mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-24 03:21:34 +01:00
Merge pull request #3049 from batistalucasv/fix/companion-layer-inbar-padding
ui-widgets: add bar padding, InBar env, and start alignment to Menu::companion CompanionLayer
This commit is contained in:
2 files changed
+75
-6
No files matched your search
@@ -5,9 +5,9 @@ use super::level::{LevelMemory, MenuLevel, level_env};
|
||||
use super::words::MenuWords;
|
||||
use crate::contract::overlay::{Anchor, Dismiss, Outside, Place, Popup};
|
||||
use crate::contract::{
|
||||
Bind, BindMut, BodyCx, Common, Component, Container, ContainerProps, Cx, EndRegion, Entry, Focus as _, Gap, InMenu, LeastWidth, Mount, NoValue,
|
||||
Outcome, Overlay, POPUP_PAD, Pad, Pointer as _, Props as _, Remember as _, Role, Scroll, Show as _, ShowContainer as _, Sizing, TooltipSpec,
|
||||
Trigger,
|
||||
Bind, BindMut, BodyCx, Common, Component, Container, ContainerProps, Cx, EndRegion, Entry, Focus as _, Gap, InBar, InMenu, LeastWidth, Mount,
|
||||
NoValue, Outcome, Overlay, POPUP_PAD, Pad, Pointer as _, Props as _, Remember as _, Role, Scroll, Show as _, ShowContainer as _, Sizing,
|
||||
TooltipSpec, Trigger,
|
||||
};
|
||||
use crate::display::Divider;
|
||||
use crate::runtime::env::{Env, Label, with_env};
|
||||
@@ -471,8 +471,8 @@ impl CompanionLayer {
|
||||
.body_scroll(false)
|
||||
.focus(FocusPolicy::NONE)
|
||||
.dismiss(Dismiss::new().outside(Outside::Keep))
|
||||
.place(Place::anchored(Anchor::Parent, Side::Above, Align::Center));
|
||||
let props = ContainerProps::new().mount(Mount::Layer(overlay)).surface(SurfaceKind::Floating).pad(Pad::None).sizing(Sizing::Content);
|
||||
.place(Place::anchored(Anchor::Parent, Side::Above, Align::Start));
|
||||
let props = ContainerProps::new().mount(Mount::Layer(overlay)).surface(SurfaceKind::Floating).pad(Pad::Control).sizing(Sizing::Content);
|
||||
CompanionLayer { common: Common::new(), props, open: Bind::given(true) }.label(label).id_salt("menu-companion")
|
||||
}
|
||||
}
|
||||
@@ -492,6 +492,10 @@ impl Component for CompanionLayer {
|
||||
Sense::empty()
|
||||
}
|
||||
|
||||
fn content_env(&self, _: &Cx<'_, Self>, values: &mut crate::runtime::env::AppValues) {
|
||||
values.set(InBar(true));
|
||||
}
|
||||
|
||||
fn finish(self, cx: Cx<'_, Self>) -> CompanionOutput {
|
||||
CompanionOutput { response: cx.response.clone() }
|
||||
}
|
||||
|
||||
@@ -1,8 +1,10 @@
|
||||
//! A menu's companion bar: a layer above the menu in its press family.
|
||||
|
||||
use craft_ui::buttons::Button;
|
||||
use craft_ui::contract::{Role, Show as _, ShowContainer as _, Trigger};
|
||||
use craft_ui::contract::{InBar, Role, Show as _, ShowContainer as _, Trigger};
|
||||
use craft_ui::display::Divider;
|
||||
use craft_ui::menu::{Menu, MenuItem};
|
||||
use craft_ui::runtime::env::Env;
|
||||
use craft_ui::testing::{Fixture, pointer, trial};
|
||||
use egui::{Key, Modifiers, Pos2, Ui, Vec2};
|
||||
use std::cell::Cell;
|
||||
@@ -91,3 +93,66 @@ fn escape_closes_the_topmost_layer_first() {
|
||||
assert!(fixture.by_label("Copy").is_none(), "the next Escape closes the menu");
|
||||
assert!(fixture.by_label("Size").is_none(), "the bar goes with the menu");
|
||||
}
|
||||
|
||||
#[trial]
|
||||
fn companion_layer_has_bar_padding_inbar_env_and_start_alignment() {
|
||||
let in_bar_seen = Rc::new(Cell::new(false));
|
||||
let in_bar_clone = Rc::clone(&in_bar_seen);
|
||||
|
||||
let mut fixture = Fixture::new(move |ui| {
|
||||
ui.add_space(120.0);
|
||||
let target = Button::new("Target").show(ui);
|
||||
let in_bar = Rc::clone(&in_bar_clone);
|
||||
let mut bar = move |ui: &mut Ui| {
|
||||
in_bar.set(Env::of(ui).get::<InBar>().0);
|
||||
Button::new("Bold").show(ui);
|
||||
Divider::vertical().show(ui);
|
||||
Button::new("Italic").show(ui);
|
||||
Button::new("Underline").show(ui);
|
||||
};
|
||||
Menu::context("Target menu", &target).companion(&mut bar).show(ui, |ui, _| {
|
||||
MenuItem::new("Copy").show(ui);
|
||||
});
|
||||
});
|
||||
fixture.room(Vec2::new(800.0, 640.0));
|
||||
fixture.settle();
|
||||
|
||||
let target = fixture.by_label("Target").expect("the target has a node").center();
|
||||
right_click(&mut fixture, target);
|
||||
|
||||
assert!(fixture.by_label("Copy").is_some(), "the right-click opens the menu");
|
||||
assert!(fixture.by_label("Bold").is_some(), "the companion bar shows its buttons");
|
||||
|
||||
// 1. InBar environment propagation
|
||||
assert!(in_bar_seen.get(), "InBar(true) is set in the companion bar content env");
|
||||
|
||||
// 2. Bar padding (Pad::Control) around the companion layer's controls
|
||||
let companion_bar = fixture.by_label("Menu bar").expect("companion bar group container");
|
||||
let bold = fixture.by_label("Bold").expect("first button in companion bar");
|
||||
let underline = fixture.by_label("Underline").expect("last button in companion bar");
|
||||
|
||||
assert!(companion_bar.rect.left() < bold.rect.left(), "companion bar has left padding");
|
||||
assert!(companion_bar.rect.top() < bold.rect.top(), "companion bar has top padding");
|
||||
assert!(companion_bar.rect.right() > underline.rect.right(), "companion bar has right padding");
|
||||
assert!(companion_bar.rect.bottom() > underline.rect.bottom(), "companion bar has bottom padding");
|
||||
|
||||
// 3. Start alignment (Align::Start) relative to parent Menu panel
|
||||
let menu = fixture
|
||||
.by_role(Role::MENU)
|
||||
.into_iter()
|
||||
.find(|m| fixture.by_label("Copy").is_some_and(|c| m.rect.contains(c.center())))
|
||||
.expect("parent context menu panel");
|
||||
|
||||
assert!(
|
||||
(companion_bar.rect.left() - menu.rect.left()).abs() < 1.0,
|
||||
"companion bar aligns with menu's start edge: bar left={}, menu left={}",
|
||||
companion_bar.rect.left(),
|
||||
menu.rect.left()
|
||||
);
|
||||
assert!(
|
||||
companion_bar.rect.bottom() <= menu.rect.top() + 1.0,
|
||||
"companion bar is anchored above the menu: bar bottom={}, menu top={}",
|
||||
companion_bar.rect.bottom(),
|
||||
menu.rect.top()
|
||||
);
|
||||
}
|
||||
Reference in new issue
Block a user