Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 58 additions & 11 deletions crates/ui-egui/src/completion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,21 @@ pub(crate) fn place(cursor: egui::Rect, size: egui::Vec2, screen: egui::Rect, mi
around(cursor.expand2(egui::vec2(0.0, cursor.height() * 1.5)), size, screen, min, side)
}

/// Where help of `size` (last frame's) goes beside `pane`, the panel its box is in, level with
/// `line`: on the side with more room, moved up as far as it takes to stay on screen. Its corner,
/// the width it takes (narrower than `HELP_WIDTH` where that's all there is) and the height its
/// content may take. None with neither side wide enough to read it.
pub(crate) fn beside(pane: egui::Rect, line: egui::Rect, size: egui::Vec2, screen: egui::Rect) -> Option<(egui::Pos2, f32, f32)> {
let (left, right) = (pane.left() - screen.left(), screen.right() - pane.right());
let width = (left.max(right) - FRAME).min(HELP_WIDTH);
if width < HELP_MIN_WIDTH {
return None;
}
let x = if right >= left { pane.right() } else { (pane.left() - size.x).max(screen.left()) };
Comment thread
jgregoriorincon marked this conversation as resolved.
Resolved
let y = line.top().min(screen.bottom() - size.y).max(screen.top());
Some((egui::pos2(x, y), width, screen.height() - FRAME))
}

/// Where a popup of `size` goes by `anchor`: on side `side` if it fits, else on the other if it
/// fits, else on the side with more room; kept on screen sideways. Its corner, and the height
/// its content may take.
Expand Down Expand Up @@ -194,9 +209,11 @@ impl<W: PartialEq, T: Clone> Worked<W, T> {
}

/// Help floating by a box's cursor while it's in something the help is about (a command's
/// braces, a function's call), so it stays in view while that is typed: by the cursor's line,
/// which it doesn't cover, and anchored sideways at where that starts, so it doesn't slide with
/// each key typed. Escape closes it until the cursor leaves what it was about (its key, `K`).
/// braces, a function's call), so it stays in view while that is typed: beside the box's panel
/// when it has one with room next to it (`beside`), level with the line where that starts, so the
/// box stays in view; else by the cursor's line, which it doesn't cover, and anchored sideways at
/// where that starts, so it doesn't slide with each key typed. Escape closes it until the cursor
/// leaves what it was about (its key, `K`).
#[derive(Clone)]
pub(crate) struct Tip<K> {
/// What it shows the help about: what Escape acts on.
Expand Down Expand Up @@ -245,9 +262,9 @@ impl<K: Clone + PartialEq> Tip<K> {

/// After the box (`out`): the help `found` for the cursor at char `at` (None: no cursor, or
/// none to show it by, such as a list open over it), as what it's about and the char where
/// that starts, drawn by `draw`
/// (given the width it takes), `rows` monospace lines high at first guess, on side `side` of
/// the cursor's line when it fits there. Not with the cursor out of the box's view
/// that starts, drawn by `draw` (given the width it takes), `rows` monospace lines high at
/// first guess; beside `pane` (the panel the box is in) with room there, else on side `side`
/// of the cursor's line when it fits there. Not with the cursor out of the box's view
/// (`visible`) or what Escape closed.
pub fn show(
&mut self,
Expand All @@ -259,6 +276,7 @@ impl<K: Clone + PartialEq> Tip<K> {
found: Option<(K, usize)>,
rows: usize,
side: Side,
pane: Option<egui::Rect>,
draw: impl FnOnce(&mut Ui, f32),
) {
// Out of what Escape closed (in nothing, or straight into something else), it may show
Expand All @@ -271,11 +289,11 @@ impl<K: Clone + PartialEq> Tip<K> {
let galley_rect = |i: usize| out.galley.pos_from_cursor(CCursor::new(i)).translate(out.galley_pos.to_vec2());
let shown = found.filter(|(k, _)| self.dismissed.as_ref() != Some(k)).and_then(|(k, start)| {
let cursor = galley_rect(at?);
let left = galley_rect(start).left();
let anchor = egui::Rect::from_x_y_ranges(left..=left, cursor.y_range());
visible.intersects(cursor).then_some((anchor, k))
let start = galley_rect(start);
let anchor = egui::Rect::from_x_y_ranges(start.left()..=start.left(), cursor.y_range());
visible.intersects(cursor).then_some((anchor, start, k))
});
let Some((anchor, k)) = shown else {
let Some((anchor, start, k)) = shown else {
self.shown = None;
self.size = None;
return;
Expand All @@ -284,7 +302,13 @@ impl<K: Clone + PartialEq> Tip<K> {
let screen = ui.ctx().content_rect();
let width = (screen.width() - FRAME).clamp(0.0, HELP_WIDTH);
let size = self.size.unwrap_or(egui::vec2(width + FRAME, row * rows as f32));
let (corner, room) = place(anchor, size, screen, row * 3.0, side);
// Beside the pane it's level with the line `start` is on, which stays put as the cursor
// moves down a wrapped line; with that line scrolled out over the box, with the box's top.
let level = start.translate(egui::vec2(0.0, (visible.top() - start.top()).max(0.0)));
let (corner, width, room) = pane.and_then(|pane| beside(pane, level, size, screen)).unwrap_or_else(|| {
let (corner, room) = place(anchor, size, screen, row * 3.0, side);
(corner, width, room)
});
let (_, drawn) = popup(ui, id, corner, room, |ui| draw(ui, width));
// Placed by a guess or by other help's size: over the line, its bottom edge would sit
// off the line until something repaints, so place it again by what it holds now.
Expand Down Expand Up @@ -2046,4 +2070,27 @@ mod tests {
let (corner, _) = super::place(cursor, egui::vec2(200.0, 100.0), screen, 60.0, super::Side::Above);
assert_eq!(corner, egui::pos2(100.0, 150.0), "no room over it: under");
}

/// With room beside its pane, help goes there, level with the line: on the side with more
/// room, narrower when that's all there is, and moved up (or scrolled) to stay on screen.
#[test]
fn help_goes_beside_its_pane() {
let screen = egui::Rect::from_min_size(egui::pos2(0.0, 0.0), egui::vec2(1200.0, 800.0));
let pane = |left: f32, right: f32| egui::Rect::from_x_y_ranges(left..=right, 0.0..=800.0);
let line = |top: f32| egui::Rect::from_min_size(egui::pos2(500.0, top), egui::vec2(0.0, 16.0));
let size = egui::vec2(super::HELP_WIDTH + super::FRAME, 300.0);
let room = 800.0 - super::FRAME;
let at = |pane, top, size| super::beside(pane, line(top), size, screen);
assert_eq!(at(pane(800.0, 1200.0), 100.0, size), Some((egui::pos2(416.0, 100.0), 360.0, room)), "left of the pane");
assert_eq!(at(pane(0.0, 400.0), 100.0, size), Some((egui::pos2(400.0, 100.0), 360.0, room)), "right of it");
assert_eq!(at(pane(300.0, 600.0), 100.0, size), Some((egui::pos2(600.0, 100.0), 360.0, room)), "the side with more room");
let narrow = egui::vec2(300.0, 300.0);
assert_eq!(at(pane(0.0, 900.0), 100.0, narrow), Some((egui::pos2(900.0, 100.0), 276.0, room)), "narrower");
assert_eq!(at(pane(0.0, 976.0), 100.0, narrow).map(|(_, width, _)| width), Some(200.0), "the narrowest");
assert_eq!(at(pane(0.0, 977.0), 100.0, narrow), None, "no room to read it");
assert_eq!(at(pane(0.0, 1200.0), 100.0, size), None, "the pane is the whole width");
assert_eq!(at(pane(800.0, 1200.0), 700.0, size).map(|(c, _, _)| c.y), Some(500.0), "moved up to fit");
let tall = egui::vec2(size.x, 1000.0);
assert_eq!(at(pane(800.0, 1200.0), 100.0, tall), Some((egui::pos2(416.0, 0.0), 360.0, room)), "taller than the window");
}
}
2 changes: 1 addition & 1 deletion crates/ui-egui/src/exprcomplete.rs
Original file line number Diff line number Diff line change
Expand Up @@ -403,7 +403,7 @@ pub(crate) fn expr_edit_lines(
let at = cursor.filter(|_| !state.list.is_open());
// Kept by its `(`, and anchored there; over the line, as under the box are its error and help.
let about = call.map(|c| (c.open, c.open));
state.tip.show(ui, id.with("expr_tip"), &out, visible, at, about, TIP_ROWS, Side::Above, |ui, width| {
state.tip.show(ui, id.with("expr_tip"), &out, visible, at, about, TIP_ROWS, Side::Above, None, |ui, width| {
if let Some(call) = &call {
draw_call(ui, call, width);
}
Expand Down
80 changes: 77 additions & 3 deletions crates/ui-egui/src/scriptcomplete.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1590,19 +1590,21 @@ pub fn hint(ui: &Ui, box_id: Id, out: &TextEditOutput, visible: egui::Rect, scri
}
None => (None, None),
};
// Anchored at the line's `{`, the signature at the call's `(`.
// Anchored at the line's `{`, the signature at the call's `(`; both beside the pane the box is
// in (what this `ui` is clipped to) with room there, so the script stays in view.
let pane = Some(ui.clip_rect());
if call.is_none() {
let rows = help.as_ref().map_or(0, |(_, help)| help.params.len() + 3);
let about = help.as_ref().map(|(h, _)| (h.clone(), h.brace));
state.hint.tip.show(ui, box_id.with("complete_hint"), out, visible, at, about, rows, Side::Below, |ui, width| {
state.hint.tip.show(ui, box_id.with("complete_hint"), out, visible, at, about, rows, Side::Below, pane, |ui, width| {
if let Some((_, help)) = &help {
draw_help(ui, help, width);
}
});
}
let about = call.as_ref().map(|(open, _)| (*open, *open));
let rows = crate::exprcomplete::TIP_ROWS;
state.hint.call.show(ui, box_id.with("complete_call"), out, visible, at, about, rows, Side::Below, |ui, width| {
state.hint.call.show(ui, box_id.with("complete_call"), out, visible, at, about, rows, Side::Below, pane, |ui, width| {
if let Some((_, call)) = &call {
crate::exprcomplete::draw_call(ui, call, width);
}
Expand Down Expand Up @@ -2856,6 +2858,78 @@ mod tests {
assert!(!shows(&h, round));
}

/// The Script pane docked at a side of a wide window, `typed` into its box.
fn docked(left: bool, typed: &str) -> egui_kittest::Harness<'static, crate::RustGisApp> {
use egui_kittest::kittest::Queryable;
let mut h = egui_kittest::Harness::builder().allow_missing_glyphs().with_size(egui::vec2(1200.0, 700.0)).build_ui_state(
move |ui, app: &mut crate::RustGisApp| {
let panel = if left { egui::Panel::left("script_side") } else { egui::Panel::right("script_side") };
panel.exact_size(400.0).show(ui, |ui| crate::analysispane::script_pane(app, ui, &crate::theme::Tokens::light()));
},
crate::RustGisApp::with_sample(),
);
h.run();
h.get_by_role(egui::accesskit::Role::MultilineTextInput).focus();
h.run();
type_in(&mut h, typed);
h
}

/// With room beside the Script pane, a command's help is there, level with the line, so the
/// script stays in view.
#[test]
fn a_commands_help_goes_beside_the_script_pane() {
use egui_kittest::kittest::Queryable;
for left in [false, true] {
// A long help (`layout.addElement`, taller than this window) and a short one.
for id in ["layout.addElement", "map.goToXY"] {
let mut h = docked(left, &format!("{id} {{"));
let help = super::help(&h.state().session, id).unwrap();
let label = format!("{}{}", help.id, help.keys);
let sig = h.get_by_label(&label).rect();
let script = h.get_by_role(egui::accesskit::Role::MultilineTextInput).rect();
let clear = if left { sig.min.x > script.max.x } else { sig.max.x < script.min.x };
assert!(clear, "left {left}, {id}: beside the pane: {sig:?} {script:?}");
if id == "map.goToXY" {
assert!((sig.min.y - script.min.y).abs() < 20.0, "left {left}: level with the line: {sig:?} {script:?}");
// Typed on until the line wraps: the help stays where it was.
type_in(&mut h, r#""lon": 2.35, "lat": 48.85, "scale": 5000000, "name": "long enough to wrap", "#);
let on = h.get_by_label(&label).rect();
assert!((on.min - sig.min).length() < 0.5, "left {left}: it didn't move: {on:?} {sig:?}");
}
}
}
}

/// A line wrapped over more rows than the box shows, its `{` scrolled out: the help beside the
/// pane is level with the box's top, not with a line above it.
#[test]
fn the_help_beside_the_pane_stays_by_a_scrolled_script() {
use egui_kittest::kittest::Queryable;
// Short enough to scroll a long line; `map.goToXY`'s help still fits below the box's top.
let long = format!("map.goToXY {{\"name\": \"{}", "word ".repeat(900));
let mut h = docked(false, &long);
// A cursor key scrolls the box to its cursor, over the next frames.
h.key_press(egui::Key::ArrowLeft);
h.run_steps(60);
let help = super::help(&h.state().session, "map.goToXY").unwrap();
let sig = h.get_by_label(&format!("{}{}", help.id, help.keys)).rect();
let script = h.get_by_role(egui::accesskit::Role::MultilineTextInput).rect();
let hint = h.get_by_label_contains("One command per line").rect();
assert!(script.min.y < hint.max.y, "the line's start scrolled out of the box: {script:?} {hint:?}");
assert!(sig.min.y > hint.max.y, "level with the box, not over what's above it: {sig:?} {hint:?}");
}

/// A function's signature goes where its command's help does.
#[test]
fn a_calls_signature_goes_beside_the_script_pane() {
use egui_kittest::kittest::Queryable;
let h = docked(false, r#"select.byAttributes {"layer": "Countries", "where": "round(pop_est, "#);
let sig = h.get_by_role_and_label(egui::accesskit::Role::Label, "round(x[, places])").rect();
let script = h.get_by_role(egui::accesskit::Role::MultilineTextInput).rect();
assert!(sig.max.x < script.min.x, "beside the pane: {sig:?} {script:?}");
}

/// A command's help closed with Escape stays closed through a call in its expression: the
/// signature shows in the call, and out of it the help doesn't come back, as the cursor never
/// left the braces. A call in another command's braces is leaving them.
Expand Down
6 changes: 4 additions & 2 deletions docs/user-guide/analysis.md
Original file line number Diff line number Diff line change
Expand Up @@ -1906,8 +1906,10 @@ moves to is read with a short form of its help.
Accepting a command on a line of its own adds its braces: with its required keys ready for their
values, the cursor at the first (`map.goToXY {"lon": , "lat": }`), or empty with the cursor inside
when every key may be left out; a command with no parameters gets none, and a line that has
parameters keeps them. While you type a command's parameters (the cursor inside its `{…}`), the same help stays by the cursor;
Escape closes it. A quote that starts a key in a command's braces opens the list of its keys not
parameters keeps them. While you type a command's parameters (the cursor inside its `{…}`), the same
help shows beside the Script pane, level with the line, so the script stays in view (with no room on
either side, by the cursor); Escape closes it. A quote that starts a key in a command's braces opens
the list of its keys not
given yet, beside the highlighted key's description; accepting one closes it with its colon
(`"size": `). The same goes in a key's own object, or in each object of its list, for the keys the
command's description gives it (`topology.rules {"add": {"rule": …}}`). In a `gp.run` line, a
Expand Down
Loading