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
149 changes: 130 additions & 19 deletions crates/ui-egui/src/completion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,25 @@ fn around(anchor: egui::Rect, size: egui::Vec2, screen: egui::Rect, min: f32, si
/// size, the part scrolled out of view included, for `place` to choose a side next frame by what
/// it holds, not by what last fit (it would stay on a side too short for it).
pub(crate) fn popup(ui: &Ui, id: Id, corner: egui::Pos2, room: f32, content: impl FnOnce(&mut Ui)) -> (egui::Rect, egui::Vec2) {
footed(ui, id, corner, room, content, None::<fn(&mut Ui)>)
}

/// `popup`, with a `footer` under its content that stays in view while that scrolls.
fn footed(
ui: &Ui,
id: Id,
corner: egui::Pos2,
room: f32,
content: impl FnOnce(&mut Ui),
footer: Option<impl FnOnce(&mut Ui)>,
) -> (egui::Rect, egui::Vec2) {
// A button as tall as the popup's own style draws it (its text and padding), and the gap.
let under = footer.as_ref().map_or(0.0, |_| {
let style = ui.ctx().global_style();
let s = &style.spacing;
(ui.text_style_height(&egui::TextStyle::Button) + 2.0 * s.button_padding.y).max(s.interact_size.y) + s.item_spacing.y
});
let room = (room - under).max(0.0);
let area = egui::Area::new(id).order(egui::Order::Foreground).fixed_pos(corner).show(ui.ctx(), |ui| {
egui::Frame::popup(ui.style())
.show(ui, |ui| {
Expand All @@ -107,6 +126,9 @@ pub(crate) fn popup(ui: &Ui, id: Id, corner: egui::Pos2, room: f32, content: imp
// it scrolls, as Command Search does (#255). Short content still shrinks it to fit.
let out =
egui::ScrollArea::vertical().max_height(room).min_scrolled_height(room).auto_shrink([true, true]).show(ui, content);
if let Some(footer) = footer {
footer(ui);
}
(out.content_size.y - out.inner_rect.height()).max(0.0)
})
.inner
Expand Down Expand Up @@ -208,12 +230,33 @@ impl<W: PartialEq, T: Clone> Worked<W, T> {
}
}

/// A press on a popup floating over a box, which takes the focus from the box once the button
/// is up (a click elsewhere): it goes back to the box then.
#[derive(Clone, Copy, Default)]
struct Press(bool);

impl Press {
/// Each frame the popup may be pressed: `on`, whether the pointer is over it.
fn give_back(&mut self, ui: &Ui, on: bool, id: Id) {
if on && ui.input(|i| i.pointer.any_pressed()) {
self.0 = true;
ui.ctx().request_repaint();
} else if self.0 && ui.input(|i| !i.pointer.any_down()) {
self.0 = false;
ui.memory_mut(|m| m.request_focus(id));
ui.ctx().request_repaint();
}
}
}

/// 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: 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`).
/// leaves what it was about (its key, `K`). Help with a short form, with no room beside the
/// panel, shows that one, with More under it for the whole help and Less to go back (or
/// Ctrl+Shift+Space for either), until the cursor leaves what it was about.
#[derive(Clone)]
pub(crate) struct Tip<K> {
/// What it shows the help about: what Escape acts on.
Expand All @@ -222,11 +265,17 @@ pub(crate) struct Tip<K> {
dismissed: Option<K>,
/// Its whole size last frame, scroll aside (`popup`): where it goes.
size: Option<egui::Vec2>,
/// What More showed the whole help of.
expanded: Option<K>,
/// With More or Less shown: where the whole popup was drawn and the cursor it was for, to
/// keep it while a press anywhere on it takes the focus from the box.
popup: Option<(egui::Rect, usize)>,
press: Press,
}

impl<K> Default for Tip<K> {
fn default() -> Self {
Self { shown: None, dismissed: None, size: None }
Self { shown: None, dismissed: None, size: None, expanded: None, popup: None, press: Press::default() }
}
}

Expand All @@ -236,6 +285,40 @@ impl<K: Clone + PartialEq> Tip<K> {
self.shown.is_some()
}

/// The cursor the help is kept for while its More or Less is pressed: the box may have no
/// focus then, nor a cursor of its own.
pub fn held(&self, ui: &Ui) -> Option<usize> {
let (rect, at) = self.popup?;
ui.input(|i| {
let pressed = i.pointer.any_pressed() && i.pointer.interact_pos().is_some_and(|p| rect.contains(p));
(i.focused && (self.press.0 || pressed)).then_some(at)
})
}

/// Before the box (its id `id`) sees the keys, with More or Less shown and the box focused:
/// Ctrl+Shift+Space does what that button does. Whether it did. Read before Ctrl+Space
/// (`Asked::keys`), which takes the key with Shift down too.
pub fn toggle_keys(&mut self, ui: &Ui, id: Id) -> bool {
let shortcut = Modifiers::CTRL | Modifiers::SHIFT;
if self.popup.is_none() || !ui.memory(|m| m.has_focus(id)) || !ui.input_mut(|i| i.consume_key(shortcut, Key::Space)) {
return false;
}
self.toggle();
true
}

/// The whole help shown, or the short one again.
fn toggle(&mut self) {
self.expanded = if self.expanded == self.shown { None } else { self.shown.clone() };
}

/// Nothing shown: where it goes and its button start over.
fn gone(&mut self) {
self.size = None;
self.popup = None;
self.press = Press::default();
}

/// Before the box (its id `id`) sees the keys, with the help shown and the box focused:
/// Escape closes it (and the box keeps the focus). Whether the keys were its to read.
pub fn keys(&mut self, ui: &Ui, id: Id) -> bool {
Expand All @@ -244,7 +327,8 @@ impl<K: Clone + PartialEq> Tip<K> {
}
if ui.input_mut(|i| i.consume_key(Modifiers::NONE, Key::Escape)) {
self.dismissed = self.shown.take();
self.size = None;
self.expanded = None;
self.gone();
}
true
}
Expand All @@ -256,14 +340,18 @@ impl<K: Clone + PartialEq> Tip<K> {
if here != self.dismissed.as_ref() {
self.dismissed = None;
}
if here != self.expanded.as_ref() {
self.expanded = None;
}
self.shown = None;
self.size = None;
self.gone();
}

/// 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; beside `pane` (the panel the box is in) with room there, else on side `side`
/// that starts, drawn by `draw` (given the width it takes, and whether to draw it whole:
/// false only when it has a `short` form and no room beside the pane), `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(
Expand All @@ -277,7 +365,8 @@ impl<K: Clone + PartialEq> Tip<K> {
rows: usize,
side: Side,
pane: Option<egui::Rect>,
draw: impl FnOnce(&mut Ui, f32),
short: bool,
draw: impl FnOnce(&mut Ui, f32, bool),
) {
// Out of what Escape closed (in nothing, or straight into something else), it may show
// again next time. Only with a cursor to show it by: with none (the window left, text
Expand All @@ -286,6 +375,9 @@ impl<K: Clone + PartialEq> Tip<K> {
if at.is_some() && here != self.dismissed.as_ref() {
self.dismissed = None;
}
if at.is_some() && here != self.expanded.as_ref() {
self.expanded = None;
}
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?);
Expand All @@ -295,7 +387,7 @@ impl<K: Clone + PartialEq> Tip<K> {
});
let Some((anchor, start, k)) = shown else {
self.shown = None;
self.size = None;
self.gone();
return;
};
let row = ui.text_style_height(&egui::TextStyle::Monospace) + ui.spacing().item_spacing.y + 4.0;
Expand All @@ -305,18 +397,42 @@ impl<K: Clone + PartialEq> Tip<K> {
// 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 placed = pane.and_then(|pane| beside(pane, level, size, screen));
let shortened = short && placed.is_none();
let whole = !shortened || self.expanded.as_ref() == Some(&k);
let (corner, width, room) = placed.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));
let mut pressed = false;
let footer = shortened.then_some(|ui: &mut Ui| {
let label = if whole { tl!("Less") } else { tl!("More") };
pressed = ui.button(label).on_hover_text(tl!("Ctrl+Shift+Space")).clicked();
});
let (rect, drawn) = footed(ui, id, corner, room, |ui| draw(ui, width, whole), footer);
match at.filter(|_| shortened) {
Some(at) => {
let on = ui.input(|i| i.pointer.interact_pos().is_some_and(|p| rect.contains(p)));
self.press.give_back(ui, on, out.response.response.id);
self.popup = Some((rect, at));
}
None => {
self.popup = None;
self.press = Press::default();
}
}

// 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.
if drawn != size {
ui.ctx().request_repaint();
}
self.shown = Some(k);
self.size = Some(drawn);
if pressed {
self.toggle();
ui.ctx().request_repaint();
}
}
}

Expand Down Expand Up @@ -369,12 +485,12 @@ pub(crate) struct List<O> {
scroll: Scroll,
/// Whether the button pressed on the list is still down: egui takes the focus from the box
/// when it comes up (a click elsewhere), and it goes back to the box then.
pressed: bool,
pressed: Press,
}

impl<O> Default for List<O> {
fn default() -> Self {
Self { open: None, dismissed: None, drawn: None, pointer_leads: false, scroll: Scroll::default(), pressed: false }
Self { open: None, dismissed: None, drawn: None, pointer_leads: false, scroll: Scroll::default(), pressed: Press::default() }
}
}

Expand Down Expand Up @@ -490,7 +606,7 @@ impl<O: Offered> List<O> {
if !ui.input(|i| i.focused) {
self.close();
self.drawn = None;
self.pressed = false;
self.pressed = Press::default();
return None;
}
// A press on the list: only then does it outlive the box's focus. Pressed this frame
Expand All @@ -503,12 +619,7 @@ impl<O: Offered> List<O> {
// clicked was the box's list, so the focus goes back to it, and the list shows again
// next frame if there's still something to offer there (more after the item accepted,
// or the item that didn't fit).
if on_list && ui.input(|i| i.pointer.any_pressed()) {
self.pressed = true;
} else if self.pressed && ui.input(|i| !i.pointer.any_down()) {
self.pressed = false;
ui.memory_mut(|m| m.request_focus(out.response.response.id));
}
self.pressed.give_back(ui, on_list, out.response.response.id);
let offer = if out.response.response.has_focus() {
// A cursor, not a selection: with text selected, Enter and Tab act on it as usual.
work(out.cursor_range.filter(|r| r.is_empty()).map(|r| r.primary.index.0))
Expand Down
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, None, |ui, width| {
state.tip.show(ui, id.with("expr_tip"), &out, visible, at, about, TIP_ROWS, Side::Above, None, false, |ui, width, _| {
if let Some(call) = &call {
draw_call(ui, call, width);
}
Expand Down
Loading
Loading