Skip to content
Merged
106 changes: 86 additions & 20 deletions crates/ui-egui/src/completion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ const HELP_MIN_WIDTH: f32 = 200.0;
pub(crate) const GAP: f32 = 12.0;
/// The popup frame's margins, both sides together.
pub(crate) const FRAME: f32 = 24.0;
/// The height of the grip over a help that moves (`Tip::show`'s `moved`).
const GRIP: f32 = 10.0;

/// What a list offers: the items, and where the word they replace ends (a char index of the
/// box's text), which the list goes under.
Expand Down Expand Up @@ -99,28 +101,35 @@ 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)>)
footed(ui, id, corner, room, content, None::<fn(&mut Ui)>, None::<fn(&mut Ui)>)
}

/// `popup`, with a `footer` under its content that stays in view while that scrolls.
/// `popup`, with a `footer` under its content that stays in view while that scrolls, and a
/// `header` over it (the grip that moves it, `grip_bar`).
fn footed(
ui: &Ui,
id: Id,
corner: egui::Pos2,
room: f32,
content: impl FnOnce(&mut Ui),
footer: Option<impl FnOnce(&mut Ui)>,
header: 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.
// The popup's own style, for the room the footer and the header take.
let style = ui.ctx().global_style();
let s = &style.spacing;
// A button as tall as that 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 over = header.as_ref().map_or(0.0, |_| GRIP + s.item_spacing.y);
let room = (room - under - over).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| {
if let Some(header) = header {
header(ui);
}
// The area starts from its last size, so after a short list the scroll area would get
// only that room back, scrolling with room to spare: ask for the full height whenever
// it scrolls, as Command Search does (#255). Short content still shrinks it to fit.
Expand Down Expand Up @@ -256,7 +265,9 @@ impl Press {
/// 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`). 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.
/// Ctrl+Shift+Space for either), until the cursor leaves what it was about. A box that lets it be
/// moved (`show`'s `moved`) has a grip over it: dragging that moves the help, and a double-click
/// puts it back where it goes by itself.
#[derive(Clone)]
pub(crate) struct Tip<K> {
/// What it shows the help about: what Escape acts on.
Expand All @@ -267,15 +278,17 @@ pub(crate) struct Tip<K> {
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.
/// With More, Less or the grip 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)>,
/// Whether the short form is shown (with no room beside the pane), so More or Less with it.
shortened: bool,
press: Press,
}

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

Expand All @@ -285,8 +298,8 @@ 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.
/// The cursor the help is kept for while its More, Less or grip 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| {
Expand All @@ -297,10 +310,15 @@ impl<K: Clone + PartialEq> Tip<K> {

/// 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.
/// (`Asked::keys`), which takes the key with Shift down too: with a help that has no More or
/// Less (the whole help, with only its grip), it's left to Ctrl+Space, as it always was.
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)) {
if !self.shortened
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
|| 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();
Expand All @@ -316,6 +334,7 @@ impl<K: Clone + PartialEq> Tip<K> {
fn gone(&mut self) {
self.size = None;
self.popup = None;
self.shortened = false;
self.press = Press::default();
}

Expand Down Expand Up @@ -353,7 +372,9 @@ impl<K: Clone + PartialEq> Tip<K> {
/// 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.
/// (`visible`) or what Escape closed. With `moved` (a box whose help may be moved), a grip
/// over it: dragged, it leaves the corner there, which every help drawn with the same `moved`
/// takes (kept on the screen); double-clicked, it clears it.
pub fn show(
&mut self,
ui: &Ui,
Expand All @@ -366,6 +387,7 @@ impl<K: Clone + PartialEq> Tip<K> {
side: Side,
pane: Option<egui::Rect>,
short: bool,
moved: Option<&mut Option<egui::Pos2>>,
draw: impl FnOnce(&mut Ui, f32, bool),
) {
// Out of what Escape closed (in nothing, or straight into something else), it may show
Expand All @@ -392,8 +414,8 @@ impl<K: Clone + PartialEq> Tip<K> {
};
let row = ui.text_style_height(&egui::TextStyle::Monospace) + ui.spacing().item_spacing.y + 4.0;
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 full_width = (screen.width() - FRAME).clamp(0.0, HELP_WIDTH);
let size = self.size.unwrap_or(egui::vec2(full_width + FRAME, row * rows as f32));
Comment thread
jgregoriorincon marked this conversation as resolved.
Resolved
// 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)));
Expand All @@ -402,15 +424,45 @@ impl<K: Clone + PartialEq> Tip<K> {
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)
(corner, full_width, room)
});
// Where it was left, as far as the screen holds it (its size last frame): the width it
// takes anywhere, and the room under its corner.
let (corner, width, room) = match moved.as_deref().copied().flatten() {
Some(left) => {
// Held on the screen for the width it is drawn at now, not last frame's: a help that
// was beside a narrow pane was narrower than a moved one is.
let size = egui::vec2(size.x.max(full_width + FRAME), size.y);
let kept = egui::pos2(
left.x.min(screen.right() - size.x).max(screen.left()),
left.y.min(screen.bottom() - size.y).max(screen.top()),
);
(kept, full_width, (screen.bottom() - kept.y - FRAME).max(row * 3.0))
}
None => (corner, width, room),
};
Comment thread
jgregoriorincon marked this conversation as resolved.
Resolved
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) {
let mut grip = None;
let header = moved.is_some().then_some(|ui: &mut Ui| grip = Some(grip_bar(ui, width)));
let (rect, drawn) = footed(ui, id, corner, room, |ui| draw(ui, width, whole), footer, header);
if let (Some(grip), Some(pos)) = (&grip, moved) {
if grip.double_clicked() {
*pos = None;
ui.ctx().request_repaint();
} else if grip.dragged() && grip.drag_delta() != egui::Vec2::ZERO {
// Only while it moves: held still, nothing changes and nothing repaints. It adds to
// where it was left, not to where the screen let it be, so it comes back with the
// pointer after going past an edge.
*pos = Some(pos.unwrap_or(corner) + grip.drag_delta());
ui.ctx().request_repaint();
}
}
self.shortened = shortened;
match at.filter(|_| shortened || grip.is_some()) {
Comment thread
jgregoriorincon marked this conversation as resolved.
Resolved
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);
Expand All @@ -436,6 +488,20 @@ impl<K: Clone + PartialEq> Tip<K> {
}
}

/// The grip over a help that moves, `width` wide: dragging it moves the help, a double-click puts
/// it back. A button for a screen reader; the pointer shows a hand over it.
fn grip_bar(ui: &mut Ui, width: f32) -> egui::Response {
let (rect, resp) = ui.allocate_exact_size(egui::vec2(width, GRIP), egui::Sense::click_and_drag());
resp.widget_info(|| egui::WidgetInfo::labeled(egui::Role::Button, true, tl!("Move the help")));
let visuals = ui.visuals();
let colour = if resp.hovered() || resp.dragged() { visuals.text_color() } else { visuals.weak_text_color() };
ui.painter().rect_filled(egui::Rect::from_center_size(rect.center(), egui::vec2(24.0, 3.0)), 1.5, colour);
if resp.dragged() {
ui.ctx().set_cursor_icon(egui::CursorIcon::Grabbing);
}
resp.on_hover_cursor(egui::CursorIcon::Grab).on_hover_text(tl!("Drag to move it. Double-click to put it back."))
}

/// Where Ctrl+Space asked a box for its list (the text's hash and the cursor): the list shows
/// there with nothing typed, until the text or the cursor moves on.
#[derive(Clone, Default)]
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, false, |ui, width, _| {
state.tip.show(ui, id.with("expr_tip"), &out, visible, at, about, TIP_ROWS, Side::Above, None, false, None, |ui, width, _| {
if let Some(call) = &call {
draw_call(ui, call, width);
}
Expand Down
Loading
Loading