mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-25 12:16:39 +00:00
Revert dialog scrolling; it moves to its own pull request
This reverts commit ae1afda79d. Dialogs that are too large for the window are handled in a separate pull request.
This commit is contained in:
@@ -856,16 +856,14 @@ pub fn show(app: &mut VectorcraftApp, ctx: &egui::Context) {
|
||||
if focus_first {
|
||||
ui.data_mut(|m| m.insert_temp(widgets::dialog_focus_flag(), true));
|
||||
}
|
||||
// A body taller than the window has room for scrolls, so the heading and the buttons stay
|
||||
// in view.
|
||||
if let Some(body) = spec.body_with_actions {
|
||||
match modal::body_rows(ui, |ui| body(app, ui, &mut d)) {
|
||||
match body(app, ui, &mut d) {
|
||||
DialogAction::None => {}
|
||||
DialogAction::Confirm => ok = true,
|
||||
DialogAction::Cancel => cancel = true,
|
||||
}
|
||||
} else {
|
||||
cancel = modal::body_rows(ui, |ui| (spec.body)(app, ui, &mut d));
|
||||
cancel = (spec.body)(app, ui, &mut d);
|
||||
}
|
||||
// Taken (the flag is gone): done. Still there (the window's measuring frame): next frame.
|
||||
if focus_first && ui.data_mut(|m| m.remove_temp::<bool>(widgets::dialog_focus_flag())).is_none() {
|
||||
@@ -1052,6 +1050,3 @@ mod tests_shortcut_import;
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests_warp;
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests_scroll;
|
||||
|
||||
@@ -1036,7 +1036,6 @@ fn content_id(ui: &egui::Ui) -> egui::Id {
|
||||
/// (as drawn last frame). The section list stays put while the content scrolls. Returns whether
|
||||
/// the dialog asks to be canceled (never: Cancel comes from the button row in `dialogs::show`).
|
||||
fn body(app: &mut VectorcraftApp, ui: &mut egui::Ui, d: &mut Dialog) -> bool {
|
||||
super::modal::fits_itself(ui);
|
||||
let window = ui.ctx().memory(|m| m.area_rect(ui.layer_id().id)).map_or(0.0, |r| r.height());
|
||||
let content = ui.ctx().data(|m| m.get_temp::<f32>(content_id(ui))).unwrap_or(0.0);
|
||||
let rest = (window - content).max(0.0);
|
||||
|
||||
@@ -354,7 +354,6 @@ fn header(ui: &mut Ui, title: &str) {
|
||||
/// Draws the responsive picker and updates its color fields and live preview.
|
||||
/// Returns the requested action for the shared frame to confirm or cancel.
|
||||
fn body(app: &mut VectorcraftApp, ui: &mut Ui, d: &mut Dialog) -> DialogAction {
|
||||
super::modal::fits_itself(ui);
|
||||
let channel = Channel::parse(&d.str("channel")).unwrap_or_default();
|
||||
let web = d.bool("webOnly");
|
||||
let swatches = d.bool("swatches");
|
||||
|
||||
@@ -2,9 +2,8 @@
|
||||
//! window with no title bar that opens centred and moves by dragging its heading (the band from its
|
||||
//! top edge down to the heading), as a title bar would, showing the arrow until it is dragged. Where
|
||||
//! it was moved to stays, per window, for the session, and the window never leaves the screen. Rows
|
||||
//! too many for the screen's height scroll inside it ([`scrolled_rows`], [`body_rows`]), so its
|
||||
//! heading and buttons stay in view. A window without such rows that is still too tall or too wide
|
||||
//! for the screen scrolls as a whole on that axis.
|
||||
//! too many for the screen's height scroll inside it ([`scrolled_rows`]), so its heading and buttons
|
||||
//! stay in view.
|
||||
|
||||
use egui::{Id, Rect, Vec2};
|
||||
|
||||
@@ -26,16 +25,6 @@ pub(crate) fn show_at<R>(ctx: &egui::Context, title: &str, id: Id, initial: Vec2
|
||||
// Not focusable (`Sense::click()` is): Tab goes through the dialog's controls, not the backdrop.
|
||||
ui.allocate_rect(ctx.content_rect(), egui::Sense::CLICK);
|
||||
});
|
||||
// Not drawn in the pass before this one: the window just opened, and what scrolls in it starts
|
||||
// at its top, wherever it was left when the window last closed.
|
||||
let pass = ctx.cumulative_pass_nr();
|
||||
let last = ctx.data_mut(|m| std::mem::replace(m.get_temp_mut_or(id.with(SHOWN_KEY), u64::MAX), pass));
|
||||
if last == u64::MAX || last.saturating_add(1) < pass {
|
||||
ctx.data_mut(|m| {
|
||||
m.insert_temp(id.with(TOP_ROWS_KEY), true);
|
||||
m.insert_temp(id.with(TOP_WHOLE_KEY), true);
|
||||
});
|
||||
}
|
||||
let shown = egui::Window::new(title)
|
||||
.id(id)
|
||||
.order(egui::Order::Foreground)
|
||||
@@ -44,43 +33,11 @@ pub(crate) fn show_at<R>(ctx: &egui::Context, title: &str, id: Id, initial: Vec2
|
||||
.title_bar(false)
|
||||
.anchor(egui::Align2::CENTER_CENTER, offset(ctx, id, initial))
|
||||
.frame(egui::Frame::window(&ctx.global_style()).fill(t.panel).inner_margin(egui::Margin::same(margin)))
|
||||
.show(ctx, |ui| {
|
||||
// A window whose contents are wider than the screen has room for, or taller without
|
||||
// rows that scroll ([`scrolled_rows`], [`body_rows`]), scrolls as a whole on that axis,
|
||||
// so none of it is out of reach. The contents keep their ids whether the window scrolls
|
||||
// or not, so a field being edited keeps its text when that changes.
|
||||
let [wide, tall] = ctx.data(|m| m.get_temp::<[bool; 2]>(id.with(WHOLE_KEY))).unwrap_or_default();
|
||||
let scope = egui::UiBuilder::new().scope_id(id.with("modal-contents"));
|
||||
let (r, contents) = if wide || tall {
|
||||
let room = whole_room(ctx, margin);
|
||||
let mut area = egui::ScrollArea::new([wide, tall]);
|
||||
if ctx.data_mut(|m| m.remove_temp::<bool>(id.with(TOP_WHOLE_KEY))).is_some() {
|
||||
area = area.scroll_offset(Vec2::ZERO);
|
||||
}
|
||||
let out = area
|
||||
.id_salt("modal-whole")
|
||||
.max_width(room.x)
|
||||
.max_height(room.y)
|
||||
.min_scrolled_width(room.x)
|
||||
.min_scrolled_height(room.y)
|
||||
.show(ui, |ui| ui.scope_builder(scope, add).inner);
|
||||
(out.inner, out.content_size)
|
||||
} else {
|
||||
let r = ui.scope_builder(scope, add).inner;
|
||||
(r, ui.min_rect().size())
|
||||
};
|
||||
ctx.data_mut(|m| m.insert_temp(id.with(CONTENTS_KEY), contents));
|
||||
r
|
||||
})?;
|
||||
keep_rest(ctx, id, shown.response.rect.height(), margin);
|
||||
.show(ctx, add)?;
|
||||
keep_rest(ctx, id, shown.response.rect.height());
|
||||
shown.inner
|
||||
}
|
||||
|
||||
/// The room for the contents of a modal window with `margin` inside its frame.
|
||||
fn whole_room(ctx: &egui::Context, margin: i8) -> Vec2 {
|
||||
(ctx.content_rect().size() - Vec2::splat(2.0 * (super::EDGE_GAP + f32::from(margin)))).max(Vec2::ZERO)
|
||||
}
|
||||
|
||||
/// The layer of the backdrop behind a modal dialog window, which takes the clicks the rest of
|
||||
/// the app would get.
|
||||
pub(crate) fn backdrop() -> egui::LayerId {
|
||||
@@ -116,119 +73,34 @@ pub(super) fn handle(id: Id) -> Id {
|
||||
id.with("modal-drag")
|
||||
}
|
||||
|
||||
/// Under a modal window's id: how tall its [`scrolled_rows`] (or [`body_rows`]) were drawn this
|
||||
/// frame, and how tall the rest of it was when last drawn.
|
||||
/// Under a modal window's id: how tall its [`scrolled_rows`] were drawn this frame, and how tall
|
||||
/// the rest of it was when last drawn.
|
||||
const ROWS_KEY: &str = "modal-rows";
|
||||
const REST_KEY: &str = "modal-rest";
|
||||
/// Under a modal window's id: its contents drew [`scrolled_rows`] of their own or fit themselves to
|
||||
/// the screen ([`fits_itself`]) this frame (`OWN_ROWS_KEY`), or last frame (`OWN_BEFORE_KEY`).
|
||||
const OWN_ROWS_KEY: &str = "modal-own-rows";
|
||||
const OWN_BEFORE_KEY: &str = "modal-own-before";
|
||||
/// Under a modal window's id: how tall its [`body_rows`] would be without scrolling, this frame.
|
||||
const BODY_KEY: &str = "modal-body";
|
||||
/// Under a modal window's id: how large its contents would be without scrolling as a whole, this
|
||||
/// frame.
|
||||
const CONTENTS_KEY: &str = "modal-contents";
|
||||
/// Under a modal window's id, for the next frame: its [`body_rows`] scroll (`TALL_KEY`), and on
|
||||
/// which axes ([wide, tall]) it scrolls as a whole (`WHOLE_KEY`).
|
||||
const TALL_KEY: &str = "modal-tall";
|
||||
const WHOLE_KEY: &str = "modal-whole";
|
||||
/// Under a modal window's id: the pass it was last drawn in (`SHOWN_KEY`), and, since it opened,
|
||||
/// its rows (`TOP_ROWS_KEY`) or the window as a whole (`TOP_WHOLE_KEY`) have not scrolled yet, so
|
||||
/// they start at the top when they first do.
|
||||
const SHOWN_KEY: &str = "modal-shown";
|
||||
const TOP_ROWS_KEY: &str = "modal-top-rows";
|
||||
const TOP_WHOLE_KEY: &str = "modal-top-whole";
|
||||
|
||||
/// Rows of the modal window `ui` is in (one list of them per window) that scroll if the window
|
||||
/// would be taller than the screen. Their height is the screen's less the rest of the window as
|
||||
/// last drawn (a frame late, as only the drawn window knows its height).
|
||||
/// Rows that fit are as tall as they are.
|
||||
pub(crate) fn scrolled_rows<R>(ui: &mut egui::Ui, salt: impl egui::AsIdSalt, add: impl FnOnce(&mut egui::Ui) -> R) -> R {
|
||||
fits_itself(ui);
|
||||
let (r, drawn, _) = rows(ui, salt, true, add);
|
||||
ui.ctx().data_mut(|m| m.insert_temp(ui.layer_id().id.with(ROWS_KEY), drawn));
|
||||
r
|
||||
}
|
||||
|
||||
/// Mark the contents of the modal window `ui` is in as fitting themselves to the screen (sizing
|
||||
/// what scrolls in them from the screen's height), so neither [`body_rows`] nor the window
|
||||
/// scroll them again.
|
||||
pub(crate) fn fits_itself(ui: &egui::Ui) {
|
||||
ui.ctx().data_mut(|m| m.insert_temp(ui.layer_id().id.with(OWN_ROWS_KEY), true));
|
||||
}
|
||||
|
||||
/// The body of a shared dialog window. While the window would be taller than the screen has room
|
||||
/// for, the body scrolls as [`scrolled_rows`] do, so the heading and the buttons stay in view;
|
||||
/// otherwise it is drawn as it is. A body that fits itself ([`fits_itself`]) is drawn as it is.
|
||||
pub(crate) fn body_rows<R>(ui: &mut egui::Ui, add: impl FnOnce(&mut egui::Ui) -> R) -> R {
|
||||
let id = ui.layer_id().id;
|
||||
let own = ui.ctx().data(|m| m.get_temp::<bool>(id.with(OWN_BEFORE_KEY))).unwrap_or(false);
|
||||
let tall = !own && ui.ctx().data(|m| m.get_temp::<bool>(id.with(TALL_KEY))).unwrap_or(false);
|
||||
let (r, drawn, body) = rows(ui, "dialog-body", tall, add);
|
||||
if !ui.ctx().data(|m| m.get_temp::<bool>(id.with(OWN_ROWS_KEY))).unwrap_or(false) {
|
||||
ui.ctx().data_mut(|m| {
|
||||
m.insert_temp(id.with(ROWS_KEY), drawn);
|
||||
m.insert_temp(id.with(BODY_KEY), body);
|
||||
});
|
||||
}
|
||||
r
|
||||
}
|
||||
|
||||
/// [`scrolled_rows`] without marking the window's body as having rows of its own or keeping their
|
||||
/// height. Also returns how tall the rows were drawn and how tall they would be without scrolling.
|
||||
/// Rows that do not `scroll` are as tall as they are, with no
|
||||
/// scroll area (which would take the scroll requests of their fields from the window's). The rows
|
||||
/// keep their ids either way, so a field being edited keeps its text when they start or stop
|
||||
/// scrolling.
|
||||
fn rows<R>(ui: &mut egui::Ui, salt: impl egui::AsIdSalt, scroll: bool, add: impl FnOnce(&mut egui::Ui) -> R) -> (R, f32, f32) {
|
||||
let id = ui.layer_id().id;
|
||||
let rest = ui.ctx().data(|m| m.get_temp::<f32>(id.with(REST_KEY))).unwrap_or(0.0);
|
||||
let room = (ui.ctx().content_rect().height() - rest - 2.0 * super::EDGE_GAP).max(0.0);
|
||||
let scope = egui::UiBuilder::new().scope_id(ui.make_persistent_id(&salt).with("modal-rows"));
|
||||
if !scroll {
|
||||
let rows = ui.scope_builder(scope, add);
|
||||
let height = rows.response.rect.height();
|
||||
return (rows.inner, height, height);
|
||||
}
|
||||
// As tall as the room at once: by itself a scroll area grows a frame at a time with the window.
|
||||
let mut area = egui::ScrollArea::vertical().id_salt(salt).max_height(room).min_scrolled_height(room);
|
||||
if ui.ctx().data_mut(|m| m.remove_temp::<bool>(id.with(TOP_ROWS_KEY))).is_some() {
|
||||
area = area.scroll_offset(Vec2::ZERO);
|
||||
}
|
||||
let rows = ui.scope(|ui| area.show(ui, |ui| ui.scope_builder(scope, add).inner));
|
||||
(rows.inner.inner, rows.response.rect.height(), rows.inner.content_size.y)
|
||||
let rows = ui.scope(|ui| egui::ScrollArea::vertical().id_salt(salt).max_height(room).min_scrolled_height(room).show(ui, add).inner);
|
||||
ui.ctx().data_mut(|m| m.insert_temp(id.with(ROWS_KEY), rows.response.rect.height()));
|
||||
rows.inner
|
||||
}
|
||||
|
||||
/// After modal window `id` (with `margin` inside its frame) is drawn `height` tall: keep the height
|
||||
/// of all of it but its [`scrolled_rows`] for the next frame. If the rows were sized by a different
|
||||
/// one (the window just opened, or what is around them changed), the next frame is drawn at once,
|
||||
/// for the rows to fit without waiting for input. From the next frame on, [`body_rows`] scroll
|
||||
/// while the window would be taller than the screen has room for without them scrolling; a window
|
||||
/// without rows that scroll scrolls as a whole while its contents would be taller than the room,
|
||||
/// and any window scrolls sideways while its contents would be wider.
|
||||
fn keep_rest(ctx: &egui::Context, id: Id, height: f32, margin: i8) {
|
||||
let rows = ctx.data_mut(|m| m.remove_temp::<f32>(id.with(ROWS_KEY)));
|
||||
let body = ctx.data_mut(|m| m.remove_temp::<f32>(id.with(BODY_KEY)));
|
||||
let contents = ctx.data_mut(|m| m.remove_temp::<Vec2>(id.with(CONTENTS_KEY))).unwrap_or_default();
|
||||
let own = ctx.data_mut(|m| m.remove_temp::<bool>(id.with(OWN_ROWS_KEY))).unwrap_or(false);
|
||||
ctx.data_mut(|m| m.insert_temp(id.with(OWN_BEFORE_KEY), own));
|
||||
let room = whole_room(ctx, margin);
|
||||
let screen = ctx.content_rect().height() - 2.0 * super::EDGE_GAP;
|
||||
let tall = match (rows, body) {
|
||||
(Some(rows), Some(body)) if !own => height - rows + body > screen + 0.5,
|
||||
_ => false,
|
||||
};
|
||||
let whole = [!own && contents.x > room.x + 0.5, !own && rows.is_none() && contents.y > room.y + 0.5];
|
||||
let was_tall = ctx.data_mut(|m| std::mem::replace(m.get_temp_mut_or(id.with(TALL_KEY), false), tall));
|
||||
let was_whole = ctx.data_mut(|m| std::mem::replace(m.get_temp_mut_or(id.with(WHOLE_KEY), [false; 2]), whole));
|
||||
let mut repaint = tall != was_tall || whole != was_whole;
|
||||
if let Some(rows) = rows {
|
||||
let rest = (height - rows).max(0.0);
|
||||
let used = ctx.data_mut(|m| std::mem::replace(m.get_temp_mut_or(id.with(REST_KEY), 0.0), rest));
|
||||
repaint |= (used - rest).abs() > 0.5;
|
||||
}
|
||||
if repaint {
|
||||
/// After modal window `id` is drawn `height` tall: keep the height of all of it but its
|
||||
/// [`scrolled_rows`] for the next frame. If the rows were sized by a different one (the window just
|
||||
/// opened, or what is around them changed), the next frame is drawn at once, for the rows to fit
|
||||
/// without waiting for input.
|
||||
fn keep_rest(ctx: &egui::Context, id: Id, height: f32) {
|
||||
let Some(rows) = ctx.data_mut(|m| m.remove_temp::<f32>(id.with(ROWS_KEY))) else { return };
|
||||
let rest = (height - rows).max(0.0);
|
||||
let used = ctx.data_mut(|m| std::mem::replace(m.get_temp_mut_or(id.with(REST_KEY), 0.0), rest));
|
||||
if (used - rest).abs() > 0.5 {
|
||||
ctx.request_repaint();
|
||||
}
|
||||
}
|
||||
@@ -274,109 +146,4 @@ mod tests {
|
||||
// Larger than the screen: its top left stays in view.
|
||||
assert_eq!(on_screen(r(-10.0, -10.0, 900.0, 700.0), screen), r(0.0, 0.0, 900.0, 700.0));
|
||||
}
|
||||
|
||||
/// Draw `frames` frames of a 400×300 screen from `time`, the first with `events`, with a modal
|
||||
/// window open when `open`: a heading and 60 rows, in [`scrolled_rows`] when `in_rows`. Returns
|
||||
/// whether the first row could be seen in the last frame (inside the screen and its clip).
|
||||
fn first_row_seen(ctx: &egui::Context, open: bool, in_rows: bool, events: Vec<egui::Event>, time: &mut f64, frames: usize) -> bool {
|
||||
let screen = Rect::from_min_size(egui::Pos2::ZERO, egui::vec2(400.0, 300.0));
|
||||
let add = |ui: &mut egui::Ui| {
|
||||
for i in 0..60 {
|
||||
ui.label(format!("Row {i}"));
|
||||
}
|
||||
};
|
||||
let mut events = Some(events);
|
||||
let mut seen = false;
|
||||
for _ in 0..frames {
|
||||
*time += 1.0 / 60.0;
|
||||
let input =
|
||||
egui::RawInput { screen_rect: Some(screen), events: events.take().unwrap_or_default(), time: Some(*time), ..Default::default() };
|
||||
let mut out = ctx.run_ui(input, |ui| {
|
||||
if open {
|
||||
show(ui.ctx(), "Test", Id::unique("modal-reopen-test"), 0.0, 8, |ui| {
|
||||
heading(ui, "Top");
|
||||
if in_rows {
|
||||
scrolled_rows(ui, "rows", add);
|
||||
} else {
|
||||
add(ui);
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
out.textures_delta.clear();
|
||||
seen = out.shapes.iter().any(|c| match &c.shape {
|
||||
egui::Shape::Text(t) => {
|
||||
let r = t.visual_bounding_rect();
|
||||
t.galley.text() == "Row 0" && screen.contains_rect(r) && c.clip_rect.contains_rect(r)
|
||||
}
|
||||
_ => false,
|
||||
});
|
||||
}
|
||||
seen
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_window_that_scrolled_opens_again_at_its_top() {
|
||||
for in_rows in [false, true] {
|
||||
let ctx = egui::Context::default();
|
||||
crate::theme::apply(&ctx, Default::default());
|
||||
let mut time = 0.0;
|
||||
assert!(first_row_seen(&ctx, true, in_rows, vec![], &mut time, 4), "rows {in_rows}: the window opens at its top");
|
||||
let wheel = egui::Event::MouseWheel {
|
||||
unit: egui::MouseWheelUnit::Point,
|
||||
delta: egui::vec2(0.0, -3000.0),
|
||||
phase: egui::TouchPhase::Move,
|
||||
source: egui::MouseWheelSource::Unknown,
|
||||
modifiers: egui::Modifiers::NONE,
|
||||
};
|
||||
let events = vec![egui::Event::PointerMoved(egui::pos2(200.0, 200.0)), wheel];
|
||||
assert!(!first_row_seen(&ctx, true, in_rows, events, &mut time, 60), "rows {in_rows}: the wheel scrolls the first row away");
|
||||
first_row_seen(&ctx, false, in_rows, vec![], &mut time, 2);
|
||||
assert!(first_row_seen(&ctx, true, in_rows, vec![], &mut time, 4), "rows {in_rows}: opened again, the window starts at its top");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_field_keeps_its_id_when_the_window_starts_or_stops_scrolling() {
|
||||
// A field's typed text and focus are kept under its id, so the id must not change when the
|
||||
// screen's height changes whether the body or the window scrolls.
|
||||
for in_body in [false, true] {
|
||||
let ctx = egui::Context::default();
|
||||
crate::theme::apply(&ctx, Default::default());
|
||||
let window = Id::unique("modal-field-id-test");
|
||||
let mut time = 0.0;
|
||||
let mut field_at = |height: f32| {
|
||||
let screen = Rect::from_min_size(egui::Pos2::ZERO, egui::vec2(400.0, height));
|
||||
let mut field = None;
|
||||
for _ in 0..4 {
|
||||
time += 1.0 / 60.0;
|
||||
let input = egui::RawInput { screen_rect: Some(screen), time: Some(time), ..Default::default() };
|
||||
let mut out = ctx.run_ui(input, |ui| {
|
||||
show(ui.ctx(), "Test", window, 0.0, 8, |ui| {
|
||||
heading(ui, "Top");
|
||||
let add = |ui: &mut egui::Ui| {
|
||||
let r = ui.text_edit_singleline(&mut String::from("12"));
|
||||
for i in 0..30 {
|
||||
ui.label(format!("Row {i}"));
|
||||
}
|
||||
r.id
|
||||
};
|
||||
field = Some(if in_body { body_rows(ui, add) } else { add(ui) });
|
||||
});
|
||||
});
|
||||
out.textures_delta.clear();
|
||||
}
|
||||
let key = if in_body { window.with(TALL_KEY) } else { window.with(WHOLE_KEY) };
|
||||
let scrolls = ctx.data(|m| m.get_temp::<bool>(key).unwrap_or(false) || m.get_temp::<[bool; 2]>(key).is_some_and(|a| a[1]));
|
||||
(field, scrolls)
|
||||
};
|
||||
let (tall_id, tall_scrolls) = field_at(900.0);
|
||||
let (short_id, short_scrolls) = field_at(300.0);
|
||||
let (again_id, again_scrolls) = field_at(900.0);
|
||||
assert!(!tall_scrolls && short_scrolls && !again_scrolls, "body {in_body}: only the short screen scrolls");
|
||||
assert!(tall_id.is_some(), "body {in_body}: the field is drawn");
|
||||
assert_eq!(tall_id, short_id, "body {in_body}: the field keeps its id when the window starts scrolling");
|
||||
assert_eq!(short_id, again_id, "body {in_body}: the field keeps its id when the window stops scrolling");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -982,7 +982,6 @@ fn content_id(ui: &egui::Ui) -> egui::Id {
|
||||
/// (as drawn last frame). The section list stays put while the content scrolls. Returns whether
|
||||
/// the dialog asks to be canceled (never: Cancel comes from the button row in `dialogs::show`).
|
||||
fn body(app: &mut VectorcraftApp, ui: &mut egui::Ui, d: &mut Dialog) -> bool {
|
||||
super::modal::fits_itself(ui);
|
||||
let window = ui.ctx().memory(|m| m.area_rect(ui.layer_id().id)).map_or(0.0, |r| r.height());
|
||||
let content = ui.ctx().data(|m| m.get_temp::<f32>(content_id(ui))).unwrap_or(0.0);
|
||||
let rest = (window - content).max(0.0);
|
||||
|
||||
@@ -52,8 +52,6 @@ pub(super) const SPEC: DialogSpec = DialogSpec {
|
||||
|
||||
/// The preview area (panes and their captions) and the settings column.
|
||||
const PREVIEW_W: f32 = 620.0;
|
||||
/// The narrowest the panes get in a narrow window.
|
||||
const MIN_PREVIEW_W: f32 = 240.0;
|
||||
const PREVIEW_H: f32 = 470.0;
|
||||
const SIDE_W: f32 = 296.0;
|
||||
const GAP: f32 = 16.0;
|
||||
@@ -534,18 +532,10 @@ fn body(app: &mut VectorcraftApp, ui: &mut egui::Ui, d: &mut Dialog) -> bool {
|
||||
}
|
||||
let settings = settings_of(app, d);
|
||||
let mut browser = false;
|
||||
// In a window too narrow for both, the panes are narrower, so the settings beside them and the
|
||||
// buttons below stay in view. The room is the screen's (the window's own width follows what it
|
||||
// drew last), less a column for the bar of the body when it scrolls; the settings column is as
|
||||
// wide as it was drawn last.
|
||||
let room = ui.ctx().content_rect().width() - 2.0 * (f32::from(super::MARGIN) + super::EDGE_GAP) - ui.spacing().scroll.allocated_width();
|
||||
let rest_id = egui::Id::unique("save-for-web-beside-panes");
|
||||
let rest = ui.ctx().data(|m| m.get_temp::<f32>(rest_id)).unwrap_or(ui.spacing().item_spacing.x + GAP + SIDE_W);
|
||||
let preview_w = (room.min(ui.available_width()) - rest).clamp(MIN_PREVIEW_W, PREVIEW_W);
|
||||
let row = ui.horizontal_top(|ui| {
|
||||
ui.horizontal_top(|ui| {
|
||||
ui.vertical(|ui| {
|
||||
ui.set_width(preview_w);
|
||||
browser = preview_area(app, ui, d, &settings, preview_w);
|
||||
ui.set_width(PREVIEW_W);
|
||||
browser = preview_area(app, ui, d, &settings);
|
||||
});
|
||||
ui.add_space(GAP);
|
||||
ui.vertical(|ui| {
|
||||
@@ -553,10 +543,6 @@ fn body(app: &mut VectorcraftApp, ui: &mut egui::Ui, d: &mut Dialog) -> bool {
|
||||
side(app, ui, d, settings.as_ref().ok());
|
||||
});
|
||||
});
|
||||
let drawn = row.response.rect.width() - preview_w;
|
||||
if ui.ctx().data_mut(|m| std::mem::replace(m.get_temp_mut_or(rest_id, rest), drawn)) != drawn {
|
||||
ui.ctx().request_repaint();
|
||||
}
|
||||
optimize_on_enter(ui.ctx(), d);
|
||||
if browser && let Err(e) = app.run("file.saveForWeb.browser", params(d)) {
|
||||
app.status(e);
|
||||
@@ -564,16 +550,16 @@ fn body(app: &mut VectorcraftApp, ui: &mut egui::Ui, d: &mut Dialog) -> bool {
|
||||
false
|
||||
}
|
||||
|
||||
/// The view tabs, the panes (`width` wide) and the zoom and speed row. Returns whether Preview in
|
||||
/// Browser was clicked.
|
||||
fn preview_area(app: &mut VectorcraftApp, ui: &mut egui::Ui, d: &mut Dialog, settings: &Result<WebSettings, String>, width: f32) -> bool {
|
||||
/// The view tabs, the panes and the zoom and speed row. Returns whether Preview in Browser was
|
||||
/// clicked.
|
||||
fn preview_area(app: &mut VectorcraftApp, ui: &mut egui::Ui, d: &mut Dialog, settings: &Result<WebSettings, String>) -> bool {
|
||||
let t = Tokens::get(ui.ctx());
|
||||
let view = VIEWS.iter().position(|v| *v == d.str("__view")).unwrap_or(2);
|
||||
if let Some(i) = widgets::tab_bar(ui, &VIEW_LABELS, view) {
|
||||
d.fields.insert("__view".into(), json!(VIEWS[i]));
|
||||
}
|
||||
ui.add_space(4.0);
|
||||
let (area, resp) = ui.allocate_exact_size(egui::vec2(width, PREVIEW_H), egui::Sense::click_and_drag());
|
||||
let (area, resp) = ui.allocate_exact_size(egui::vec2(PREVIEW_W, PREVIEW_H), egui::Sense::click_and_drag());
|
||||
let pan_id = egui::Id::unique("save-for-web-pan");
|
||||
let mut pan: egui::Vec2 = ui.ctx().data(|m| m.get_temp(pan_id)).unwrap_or_default();
|
||||
if resp.dragged() {
|
||||
|
||||
@@ -670,7 +670,6 @@ pub fn show(app: &mut VectorcraftApp, ctx: &egui::Context) {
|
||||
let (keys_h, rows_h) = list_heights(ctx.content_rect().height() - rest, keys_open);
|
||||
let (mut apply, mut cancel, mut copy, mut lists) = (false, false, false, 0.0);
|
||||
crate::dialogs::modal::show(ctx, tl!("Import Shortcut Set"), id, -20.0, 20, |ui| {
|
||||
crate::dialogs::modal::fits_itself(ui);
|
||||
ui.set_width(NAME_W + TARGET_W + CHORD_W + 220.0);
|
||||
crate::dialogs::modal::heading(ui, tl!("Import Shortcut Set"));
|
||||
ui.add_space(10.0);
|
||||
|
||||
@@ -428,17 +428,13 @@ fn request(action: egui::accesskit::Action, target_node: egui::accesskit::NodeId
|
||||
egui::Event::AccessKitActionRequest(ActionRequest { action, target_node, target_tree: TreeId::ROOT, data: None })
|
||||
}
|
||||
|
||||
/// Type `typed` into Scale W, which shows `shown` (the leftmost field that does, before H), and Tab out.
|
||||
/// Type `typed` into Scale W, which shows `shown` (the first field that does, before H), and Tab out.
|
||||
fn type_w(screen: &mut Screen, app: &mut VectorcraftApp, shown: &str, typed: &str) {
|
||||
let key = |key, modifiers| egui::Event::Key { key, physical_key: None, pressed: true, repeat: false, modifiers };
|
||||
screen.run(app, vec![]);
|
||||
let out = screen.run(app, vec![]);
|
||||
let nodes = &out.platform_output.accesskit_update.as_ref().expect("an accessibility tree").nodes;
|
||||
// W is left of H on their row (the tree lists its nodes in no particular order).
|
||||
let w = nodes
|
||||
.iter()
|
||||
.filter(|(_, n)| n.role() == egui::accesskit::Role::TextInput && n.value().is_some_and(|v| v.starts_with(shown)))
|
||||
.min_by(|(_, a), (_, b)| a.bounds().map_or(f64::MAX, |r| r.x0).total_cmp(&b.bounds().map_or(f64::MAX, |r| r.x0)));
|
||||
let w = nodes.iter().find(|(_, n)| n.role() == egui::accesskit::Role::TextInput && n.value().is_some_and(|v| v.starts_with(shown)));
|
||||
screen.run(app, vec![request(egui::accesskit::Action::Focus, w.expect("the Scale W field").0)]);
|
||||
screen.run(app, vec![key(egui::Key::A, egui::Modifiers::COMMAND), egui::Event::Text(typed.into())]);
|
||||
screen.run(app, vec![key(egui::Key::Tab, egui::Modifiers::NONE)]);
|
||||
|
||||
@@ -1,186 +0,0 @@
|
||||
//! Dialogs in a short or narrow window: options that run past their list show its scroll bar at
|
||||
//! rest, and a dialog too large for the window scrolls instead of running off the screen.
|
||||
use egui::{Pos2, Rect, Shape, vec2};
|
||||
use serde_json::json;
|
||||
use vectorcraft_engine::Session;
|
||||
|
||||
use super::*;
|
||||
|
||||
type Open = fn(&mut VectorcraftApp);
|
||||
|
||||
/// What a dialog drew: the shapes, and the dialog window's rectangle.
|
||||
struct Shown {
|
||||
shapes: Vec<egui::epaint::ClippedShape>,
|
||||
window: Rect,
|
||||
}
|
||||
|
||||
/// Open a dialog with `open` in a `size` window, with the pointer in the window's corner (away
|
||||
/// from the dialog), and draw it until it settles.
|
||||
fn show_in(open: Open, size: egui::Vec2) -> Shown {
|
||||
let mut app = VectorcraftApp::new(Session::new(), Default::default());
|
||||
app.run("file.new", json!({"width": 400, "height": 400})).unwrap();
|
||||
app.run("shape.rectangle", json!({"x": 100, "y": 100, "width": 80, "height": 60})).unwrap();
|
||||
let ctx = egui::Context::default();
|
||||
crate::theme::install_fonts(&ctx);
|
||||
crate::theme::apply(&ctx, Default::default());
|
||||
open(&mut app);
|
||||
let screen = Rect::from_min_size(Pos2::ZERO, size);
|
||||
let mut shapes = vec![];
|
||||
for i in 0..12 {
|
||||
let events = if i == 0 { vec![egui::Event::PointerMoved(screen.max - vec2(2.0, 2.0))] } else { vec![] };
|
||||
let input = egui::RawInput { screen_rect: Some(screen), events, time: Some(f64::from(i)), ..Default::default() };
|
||||
let mut out = ctx.run_ui(input, |ui| show(&mut app, ui.ctx()));
|
||||
out.textures_delta.clear();
|
||||
shapes = out.shapes;
|
||||
}
|
||||
let window = ctx.memory(|m| {
|
||||
m.areas()
|
||||
.visible_layer_ids()
|
||||
.into_iter()
|
||||
.filter(|l| l.order == egui::Order::Foreground)
|
||||
.filter_map(|l| m.area_rect(l.id))
|
||||
.max_by(|a, b| a.area().total_cmp(&b.area()))
|
||||
});
|
||||
Shown { shapes, window: window.unwrap_or(Rect::NOTHING) }
|
||||
}
|
||||
|
||||
/// Filled upright bars 4 to 10 pt wide and taller than 12 pt (vertical scroll bars and their
|
||||
/// handles) inside the dialog window.
|
||||
fn bars(shown: &Shown) -> Vec<Rect> {
|
||||
fn walk(s: &Shape, clip: Rect, out: &mut Vec<Rect>) {
|
||||
match s {
|
||||
Shape::Rect(r) if r.fill != egui::Color32::TRANSPARENT && (4.0..=10.0).contains(&r.rect.width()) && r.rect.height() > 12.0 => {
|
||||
if clip.contains_rect(r.rect) {
|
||||
out.push(r.rect);
|
||||
}
|
||||
}
|
||||
Shape::Vec(v) => v.iter().for_each(|s| walk(s, clip, out)),
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
let mut out = vec![];
|
||||
shown.shapes.iter().for_each(|c| walk(&c.shape, c.clip_rect, &mut out));
|
||||
out.retain(|r| shown.window.expand(0.5).contains_rect(*r));
|
||||
out
|
||||
}
|
||||
|
||||
/// The texts `labels` the dialog drew, each with whether it can be seen (inside the screen and
|
||||
/// its clip).
|
||||
fn buttons(shown: &Shown, screen: Rect, labels: &[&str]) -> Vec<(String, bool)> {
|
||||
fn walk(s: &Shape, clip: Rect, out: &mut Vec<(String, Rect, Rect)>) {
|
||||
match s {
|
||||
Shape::Text(t) => out.push((t.galley.text().to_string(), t.visual_bounding_rect(), clip)),
|
||||
Shape::Vec(v) => v.iter().for_each(|s| walk(s, clip, out)),
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
let mut texts = vec![];
|
||||
shown.shapes.iter().for_each(|c| walk(&c.shape, c.clip_rect, &mut texts));
|
||||
texts
|
||||
.into_iter()
|
||||
.filter(|(s, ..)| labels.contains(&s.as_str()))
|
||||
.map(|(s, r, clip)| (s, screen.contains_rect(r) && clip.contains_rect(r)))
|
||||
.collect()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_export_options_that_run_past_their_list_show_its_bar_at_rest() {
|
||||
let opens: [(&str, Open); 2] = [
|
||||
("Save for Web", |app| {
|
||||
save_for_web::open(app).unwrap();
|
||||
}),
|
||||
("Print", |app| {
|
||||
print::open(app).unwrap();
|
||||
}),
|
||||
];
|
||||
for (name, open) in opens {
|
||||
let shown = show_in(open, vec2(1280.0, 900.0));
|
||||
assert!(!bars(&shown).is_empty(), "{name}: the settings scroll and their bar is drawn at rest");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_dialog_taller_than_the_window_scrolls_its_body_and_keeps_its_buttons_in_view() {
|
||||
let opens: [(&str, Open, &[&str]); 6] = [
|
||||
("SVG Options", |app| svg_options::open(app, svg_options::Mode::Export, Some("/tmp/x.svg")), &["OK", "Cancel"]),
|
||||
("EPS Options", |app| eps_options::open(app, &json!({"format": "eps", "path": "/tmp/x.eps"})), &["Export", "Cancel"]),
|
||||
("DXF Options", |app| dxf_options::open(app, &json!({"format": "dxf", "path": "/tmp/x.dxf"})), &["Export", "Cancel"]),
|
||||
(
|
||||
"Save PDF",
|
||||
|app| {
|
||||
save_pdf::open(app, &json!({"path": "/tmp/x.pdf"})).unwrap();
|
||||
},
|
||||
&["Cancel"],
|
||||
),
|
||||
(
|
||||
"Save for Web",
|
||||
|app| {
|
||||
save_for_web::open(app).unwrap();
|
||||
},
|
||||
&["Cancel", "Done"],
|
||||
),
|
||||
("Export for Screens", export_for_screens::open, &["Cancel"]),
|
||||
];
|
||||
let screen = Rect::from_min_size(Pos2::ZERO, vec2(1024.0, 480.0));
|
||||
for (name, open, labels) in opens {
|
||||
let tall = show_in(open, vec2(1280.0, 900.0));
|
||||
assert!(tall.window.height() > screen.height(), "{name}: the dialog is taller than the short window ({:?})", tall.window);
|
||||
let shown = show_in(open, screen.size());
|
||||
assert!(screen.contains_rect(shown.window), "{name}: the dialog stays on the screen: {:?}", shown.window);
|
||||
let found = buttons(&shown, screen, labels);
|
||||
assert_eq!(found.len(), labels.len(), "{name}: {found:?}");
|
||||
assert!(found.iter().all(|(_, seen)| *seen), "{name}: the buttons stay in view: {found:?}");
|
||||
assert!(!bars(&shown).is_empty(), "{name}: the body scrolls and its bar is drawn");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn save_for_web_in_the_smallest_window_keeps_its_settings_and_buttons_in_view() {
|
||||
let open: Open = |app| {
|
||||
save_for_web::open(app).unwrap();
|
||||
};
|
||||
// The app window's smallest size.
|
||||
let screen = Rect::from_min_size(Pos2::ZERO, vec2(800.0, 500.0));
|
||||
let shown = show_in(open, screen.size());
|
||||
assert!(screen.contains_rect(shown.window), "the dialog stays on the screen: {:?}", shown.window);
|
||||
let found = buttons(&shown, screen, &["Optimize to File Size…", "Cancel", "Done"]);
|
||||
assert_eq!(found.len(), 3, "{found:?}");
|
||||
assert!(found.iter().all(|(_, seen)| *seen), "the settings and the buttons stay in view: {found:?}");
|
||||
assert!(!bars(&shown).is_empty(), "the settings scroll and their bar is drawn");
|
||||
// A sideways bar: flat, and wider than any slider in the dialog.
|
||||
fn sideways(s: &Shape, out: &mut Vec<Rect>) {
|
||||
match s {
|
||||
Shape::Rect(r) if r.fill != egui::Color32::TRANSPARENT && (4.0..=10.0).contains(&r.rect.height()) && r.rect.width() > 300.0 => {
|
||||
out.push(r.rect)
|
||||
}
|
||||
Shape::Vec(v) => v.iter().for_each(|s| sideways(s, out)),
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
let mut found = vec![];
|
||||
shown.shapes.iter().for_each(|c| sideways(&c.shape, &mut found));
|
||||
assert!(found.is_empty(), "the dialog fits the window's width and does not scroll sideways: {found:?}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_window_too_large_for_the_screen_scrolls_as_a_whole() {
|
||||
let opens: [(&str, Open); 4] = [
|
||||
("Preferences", |app| crate::prefs_dialog::open(app, None)),
|
||||
("Keyboard Shortcuts", crate::shortcut_editor::open),
|
||||
("New Document", new_document::open),
|
||||
("Find Font", crate::find_font::open),
|
||||
];
|
||||
// The app window's smallest size.
|
||||
let screen = Rect::from_min_size(Pos2::ZERO, vec2(800.0, 500.0));
|
||||
for (name, open) in opens {
|
||||
let tall = show_in(open, vec2(1280.0, 900.0));
|
||||
assert!(
|
||||
tall.window.height() > screen.height() || tall.window.width() > screen.width(),
|
||||
"{name}: the dialog is larger than the small window ({:?})",
|
||||
tall.window
|
||||
);
|
||||
let shown = show_in(open, screen.size());
|
||||
assert!(screen.contains_rect(shown.window), "{name}: the dialog stays on the screen: {:?}", shown.window);
|
||||
assert!(!bars(&shown).is_empty(), "{name}: the dialog scrolls and its bar is drawn");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user