mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-30 18:27:00 +01:00
Merge pull request #4299 from davidmann4/fix/viewport-dialog-moves
Dialogs move by their title row: a dialog in its own window on Windows, and alerts (photocraft#17952)
This commit is contained in:
6 files changed
+123
-9
No files matched your search
@@ -4,19 +4,22 @@
|
||||
|
||||
use egui::{AreaState, Context, Id, Order, Rect};
|
||||
|
||||
/// The interact rects, in screen space, of the interactive egui widgets of the running pass that egui's hit test
|
||||
/// The ids and interact rects, in screen space, of the interactive egui widgets of the running pass that egui's hit test
|
||||
/// puts above the widget `below`: those on a layer higher in its order, or later on `below`'s layer. A layer that is
|
||||
/// not interactable is click-through and holds none. Widgets in `skip` are left out. Empty when `below` is not shown
|
||||
/// in the pass.
|
||||
pub(crate) fn above(ctx: &Context, below: Id, skip: &[Id]) -> Vec<Rect> {
|
||||
pub(crate) fn above(ctx: &Context, below: Id, skip: &[Id]) -> Vec<(Id, Rect)> {
|
||||
let (base, layers) = ctx.viewport(|viewport| {
|
||||
let widgets = &viewport.this_pass.widgets;
|
||||
let Some(base) = widgets.get(below).map(|rect| rect.layer_id) else { return (None, Vec::new()) };
|
||||
let mut layers = Vec::new();
|
||||
for (layer, rects) in widgets.layers().filter(|(layer, _)| layer.order.allow_interaction()) {
|
||||
let after = if *layer == base { rects.iter().position(|rect| rect.id == below).map_or(rects.len(), |at| at + 1) } else { 0 };
|
||||
let hits: Vec<Rect> =
|
||||
rects[after..].iter().filter(|rect| rect.sense.interactive() && !skip.contains(&rect.id)).map(|rect| rect.interact_rect).collect();
|
||||
let hits: Vec<(Id, Rect)> = rects[after..]
|
||||
.iter()
|
||||
.filter(|rect| rect.sense.interactive() && !skip.contains(&rect.id))
|
||||
.map(|rect| (rect.id, rect.interact_rect))
|
||||
.collect();
|
||||
if !hits.is_empty() {
|
||||
layers.push((*layer, hits));
|
||||
}
|
||||
@@ -36,7 +39,7 @@ pub(crate) fn above(ctx: &Context, below: Id, skip: &[Id]) -> Vec<Rect> {
|
||||
continue;
|
||||
}
|
||||
let to_screen = ctx.layer_transform_to_global(layer).unwrap_or(egui::emath::TSTransform::IDENTITY);
|
||||
found.extend(hits.into_iter().map(|rect| to_screen * rect));
|
||||
found.extend(hits.into_iter().map(|(id, rect)| (id, to_screen * rect)));
|
||||
}
|
||||
found
|
||||
}
|
||||
|
||||
@@ -918,6 +918,44 @@ pub(crate) fn over_part(ctx: &Context, viewport: ViewportId, root: Id, part: Par
|
||||
OverPart { widgets, above }
|
||||
}
|
||||
|
||||
/// The hit areas, in screen points and clipped to where each shows, of the press-taking shapes of the root whose egui widgets include `widget` in the
|
||||
/// running pass: where a press over that widget goes to the root's parts (8.3). `None` when no root recorded it, so it
|
||||
/// is a plain egui widget that takes a press anywhere in its rect.
|
||||
pub(crate) fn press_shapes_of_widget(ctx: &Context, viewport: ViewportId, widget: Id) -> Option<Vec<egui::Rect>> {
|
||||
let layers: Vec<(Layer, Vec<egui::Rect>)> = PerViewport::<Shapes>::with(ctx, viewport, |record| {
|
||||
let mut found = None::<Vec<(Layer, Vec<egui::Rect>)>>;
|
||||
for (layer, roots) in &record.this {
|
||||
for entry in roots.values().filter(|entry| entry.widgets.contains(&widget)) {
|
||||
// A press reaches a shape across its hit area, grown to its least size across (`Shape::hits`), and only
|
||||
// where it shows through its clip (`find_under`).
|
||||
let bounds = entry
|
||||
.shapes
|
||||
.iter()
|
||||
.zip(&entry.placed)
|
||||
.filter(|(shape, _)| shape.sense.takes_press())
|
||||
.map(|(shape, placed)| {
|
||||
let bounds = shape.shape.bounds();
|
||||
let grow = (egui::Vec2::splat(shape.min_across) - bounds.size()).max(egui::Vec2::ZERO) / 2.0;
|
||||
bounds.expand2(grow).intersect(placed.clip)
|
||||
})
|
||||
.filter(|rect| rect.is_positive())
|
||||
.collect();
|
||||
found.get_or_insert_with(Vec::new).push((*layer, bounds));
|
||||
}
|
||||
}
|
||||
found
|
||||
})?;
|
||||
Some(
|
||||
layers
|
||||
.into_iter()
|
||||
.flat_map(|(layer, bounds)| {
|
||||
let to_screen = areas::layer_to_screen(ctx, layer);
|
||||
bounds.into_iter().map(move |rect| to_screen * rect)
|
||||
})
|
||||
.collect(),
|
||||
)
|
||||
}
|
||||
|
||||
/// The hit areas the running pass has registered so far for the components `of` takes, in screen points: each shape's
|
||||
/// bounds grown to its least size across, with the component and part it belongs to.
|
||||
pub(crate) fn hit_areas(ctx: &Context, of: impl Fn(Id) -> bool) -> Vec<(Id, Option<PartRef>, egui::Rect)> {
|
||||
|
||||
@@ -248,11 +248,17 @@ fn regions_of(ctx: &Context, viewport: ViewportId, parts: &[ExclusivePart]) -> O
|
||||
/// clipped to the band: each interactive egui widget of the running pass that egui's hit test puts above the
|
||||
/// component's widgets, and each of the component's own shapes that a press there goes to instead of the band (a
|
||||
/// member over it, a higher part). The component's widgets resolve as the component (8.3), so they are not hits
|
||||
/// over the band themselves.
|
||||
/// over the band themselves. Another component's widget resolves the same way: only its parts that take a press
|
||||
/// cover the band, so a dialog's title row covers it with its close button and not with its whole row, whose title
|
||||
/// leaves the press to the band (photocraft: Preferences in its own window did not move by its title row).
|
||||
fn covers_of(ctx: &Context, viewport: ViewportId, owner: Id, part: PartRef, band: Rect) -> Vec<Rect> {
|
||||
let over = pointer::over_part(ctx, viewport, owner, part);
|
||||
widgets::above(ctx, owner, &over.widgets)
|
||||
.into_iter()
|
||||
.flat_map(|(widget, rect)| match pointer::press_shapes_of_widget(ctx, viewport, widget) {
|
||||
Some(shapes) => shapes.into_iter().map(|shape| shape.intersect(rect)).collect(),
|
||||
None => vec![rect],
|
||||
})
|
||||
.chain(over.above)
|
||||
.map(|cover| cover.intersect(band))
|
||||
.filter(|cover| cover.is_positive())
|
||||
|
||||
@@ -133,10 +133,10 @@ impl<'a> Dialog<'a> {
|
||||
Dialog::on(title, overlay, HEADER.band(DragBand::Move))
|
||||
}
|
||||
|
||||
/// A prompt titled `title`: a dialog centred with its content capped, fixed in place and
|
||||
/// not remembered.
|
||||
/// A prompt titled `title`: a dialog centred with its content capped and not remembered. It moves by its title band,
|
||||
/// as every dialog does, so it can be moved off what the user needs to see while answering it.
|
||||
pub fn prompt(title: impl Into<Label>) -> Self {
|
||||
Dialog::on(title, Overlay::dialog().movable(false).resizable(false), HEADER)
|
||||
Dialog::on(title, Overlay::dialog().resizable(false), HEADER.band(DragBand::Move))
|
||||
}
|
||||
|
||||
fn on(title: impl Into<Label>, overlay: Overlay, header: HeaderRegion) -> Self {
|
||||
|
||||
@@ -1339,3 +1339,22 @@ fn a_dialog_takes_its_padding_and_footer_button_height_from_the_dialog_sizes() {
|
||||
"both follow the panel padding and the push button: {follows:?} and {dialog:?}"
|
||||
);
|
||||
}
|
||||
|
||||
/// An alert moves by its title row, as every dialog does: a prompt was fixed in place, so a message over the part of
|
||||
/// the window the user had to see could not be moved aside.
|
||||
#[trial]
|
||||
fn an_alert_moves_by_its_title_row() {
|
||||
let dialog = Rc::new(Cell::new(Rect::NOTHING));
|
||||
let seen = Rc::clone(&dialog);
|
||||
let mut fixture = Fixture::new(move |ui| {
|
||||
let mut open = true;
|
||||
seen.set(Alert::new("Could not open", "The file is damaged.").opened(Bind::bound(&mut open)).show(ui).response.rect);
|
||||
});
|
||||
fixture.idle(6);
|
||||
let before = dialog.get();
|
||||
// In the title row, past the title text and short of the row's end.
|
||||
let at = Pos2::new(before.center().x + 40.0, before.top() + 20.0);
|
||||
assert!(before.is_positive(), "the alert shows");
|
||||
fixture.drag_at(at, vec2(40.0, 30.0)).idle(4);
|
||||
assert_eq!(dialog.get().min - before.min, vec2(40.0, 30.0), "the alert moved with the drag");
|
||||
}
|
||||
@@ -62,3 +62,51 @@ fn a_modals_strip_stands_for_the_hit_sink() {
|
||||
}
|
||||
assert!(sent.iter().all(|(_, command)| *command != ViewportCommand::StartDrag), "the platform moves the window, not the app: {sent:?}");
|
||||
}
|
||||
|
||||
/// photocraft: Edit › Preferences, a dialog in an OS window of its own in a window the app frames, did not move by its
|
||||
/// title row: on Windows the platform answers the hit test there (a sink installed), and the sink had no drag area for
|
||||
/// the title row, so a press on it was a plain click.
|
||||
#[trial]
|
||||
fn a_dialog_in_its_own_window_moves_it_by_its_title_row() {
|
||||
let shell = ShellInfo::default();
|
||||
let shell = shell.clone().caps(shell.caps.union(Caps::OS_RESIZES_UNDECORATED));
|
||||
let layout = craft_window::Layout { left: vec![], right: vec![Caption::Minimize, Caption::Maximize, Caption::Close] };
|
||||
let frame = Frame::Integrated(Captions { family: CaptionFamily::Windows, layout });
|
||||
let desktop = Desktop { kind: DesktopKind::Other, ssd: None, gtk_theme: None, icon_theme: None };
|
||||
let windows = Windows::with(InstallSpec::new(shell, Arc::new(NoWindows)).frame(Decision { frame, overridden: None, desktop }));
|
||||
let rect = Rect::from_min_size(Pos2::new(100.0, 100.0), vec2(640.0, 480.0));
|
||||
windows.child_info(egui::ViewportInfo {
|
||||
inner_rect: Some(rect),
|
||||
outer_rect: Some(rect),
|
||||
native_pixels_per_point: Some(1.0),
|
||||
..Default::default()
|
||||
});
|
||||
let title = Cell::new(Rect::NOTHING);
|
||||
let frame = |events: Vec<egui::Event>| {
|
||||
windows.frame(vec![], events, |ui| {
|
||||
let mut open = true;
|
||||
let shown = Dialog::new("Preferences")
|
||||
.resizable(true)
|
||||
.opened(Bind::bound(&mut open))
|
||||
.buttons([FooterButton::new("OK", DialogRole::Accept), FooterButton::new("Cancel", DialogRole::Cancel)])
|
||||
.viewport()
|
||||
.show(ui, |ui, _| {
|
||||
Block::new(Vec2::new(300.0, 200.0)).show(ui);
|
||||
});
|
||||
let rect = shown.output.response.rect;
|
||||
title.set(Rect::from_min_size(rect.min, vec2(rect.width(), 40.0)));
|
||||
})
|
||||
};
|
||||
let child = frame(vec![]).builders.iter().map(|(id, _)| *id).find(|id| *id != ViewportId::ROOT).expect("the dialog opened a window");
|
||||
let host = WindowHost::of(&windows.ctx);
|
||||
let _sink = host.install_hit_sink(child);
|
||||
for _ in 0..4 {
|
||||
frame(vec![]);
|
||||
}
|
||||
let regions = host.caption_regions(child).expect("the dialog's window registered its parts");
|
||||
let on_title = Pos2::new(title.get().center().x + 60.0, title.get().top() + 16.0);
|
||||
assert_eq!(regions.hit(on_title), Some(WindowHit::Move), "the title row is a drag area for the sink at {on_title:?}: {regions:?}");
|
||||
// The close action at the row's end keeps its press: the platform does not move the window from it.
|
||||
let on_close = Pos2::new(title.get().right() - 20.0, title.get().top() + 16.0);
|
||||
assert_eq!(regions.hit(on_close), None, "the close action is no drag area at {on_close:?}: {regions:?}");
|
||||
}
|
||||
Reference in new issue
Block a user