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
61 changes: 43 additions & 18 deletions crates/ui-egui/src/analysispane.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2300,13 +2300,20 @@ fn script(app: &mut RustGisApp, ui: &mut Ui, t: &Tokens, fit: bool) {
// `out.cursor_range` is None while the box isn't focused.
let size = vec2(ui.available_width(), script_cursor_height(ui));
ui.allocate_ui_with_layout(size, egui::Layout::right_to_left(egui::Align::Center), |ui| {
// The row keeps only what's drawn in it: its height, with no label in it yet.
// An empty row takes no height: without this the buttons below move when the first cursor
// appears.
ui.set_min_height(size.y);
let cursor = egui::widgets::text_edit::TextEditState::load(ui.ctx(), box_id).and_then(|s| s.cursor.char_range());
if let Some(range) = cursor {
if let Some(range) = crate::widgets::cursor_range(ui.ctx(), box_id) {
let (line, col) = line_col(&app.workflow.script, range.primary.index.0);
let at = crate::i18n::fmt("Ln {line}, Col {col}", &[("line", &line.to_string()), ("col", &col.to_string())]);
ui.label(egui::RichText::new(at).small().weak()).on_hover_text(tl!("The cursor's line and column"));
let selected = range.primary.index.0.abs_diff(range.secondary.index.0);
let (line, col, n) = (line.to_string(), col.to_string(), selected.to_string());
let place = if selected > 0 {
crate::i18n::fmt("Ln {line}, Col {col} ({n} selected)", &[("line", &line), ("col", &col), ("n", &n)])
} else {
crate::i18n::fmt("Ln {line}, Col {col}", &[("line", &line), ("col", &col)])
};
ui.label(egui::RichText::new(place).small().weak())
.on_hover_text(tl!("The cursor's line and column, and how many characters are selected"));
}
});
ui.horizontal(|ui| {
Expand Down Expand Up @@ -2448,9 +2455,7 @@ fn paint_stopped(ui: &Ui, out: &egui::text_edit::TextEditOutput, at: StoppedAt)
/// on the next frame (as it lays the box out: [`paint_stopped`]'s caller).
fn go_to_stopped(ui: &Ui, box_id: egui::Id, index: usize) {
use egui::text::{CCursor, CCursorRange};
let mut state = egui::widgets::text_edit::TextEditState::load(ui.ctx(), box_id).unwrap_or_default();
state.cursor.set_char_range(Some(CCursorRange::one(CCursor::new(index))));
state.store(ui.ctx(), box_id);
crate::widgets::set_cursor_range(ui.ctx(), box_id, CCursorRange::one(CCursor::new(index)));
ui.memory_mut(|m| m.request_focus(box_id));
ui.data_mut(|d| d.insert_temp(box_id.with("go_to_stopped"), true));
}
Expand Down Expand Up @@ -3126,18 +3131,14 @@ mod tests {
.build_ui_state(|ui, app: &mut RustGisApp| script_pane(app, ui, &Tokens::light()), app);
h.run();
let box_id = egui::Id::unique("script_text");
let cursor = |h: &egui_kittest::Harness<'_, RustGisApp>| {
egui::widgets::text_edit::TextEditState::load(&h.ctx, box_id).and_then(|s| s.cursor.char_range()).map(|r| r.primary.index.0)
};
let cursor = |h: &egui_kittest::Harness<'_, RustGisApp>| crate::widgets::cursor_range(&h.ctx, box_id).map(|r| r.primary.index.0);
assert_ne!(cursor(&h), Some(7 + 13 + 23));
h.get_by_label(&e).click();
h.run();
assert_eq!(cursor(&h), Some(7 + 13 + 23), "line 3, column 24");
assert!(h.ctx.memory(|m| m.has_focus(box_id)));
// From the keyboard: the error takes the focus (Tab), and Enter goes there too.
let mut state = egui::widgets::text_edit::TextEditState::load(&h.ctx, box_id).unwrap_or_default();
state.cursor.set_char_range(Some(egui::text::CCursorRange::one(egui::text::CCursor::new(0))));
state.store(&h.ctx, box_id);
crate::widgets::set_cursor_range(&h.ctx, box_id, egui::text::CCursorRange::one(egui::text::CCursor::new(0)));
h.get_by_label(&e).focus();
h.run();
assert_eq!(cursor(&h), Some(0), "moved away first");
Expand Down Expand Up @@ -3193,15 +3194,17 @@ mod tests {
.with_size(vec2(300.0, 800.0))
.build_ui_state(|ui, app: &mut RustGisApp| script_pane(app, ui, &Tokens::light()), app);
h.run();
// Any label of the row. The exact text is pinned below, once the box has a cursor.
assert!(h.query_by_label_contains("Ln ").is_none(), "no cursor yet");
let no_cursor = h.get_by_label("Clear").rect();
let box_id = egui::Id::unique("script_text");
let set = |h: &mut egui_kittest::Harness<'_, RustGisApp>, range: CCursorRange| {
let mut state = egui::widgets::text_edit::TextEditState::load(&h.ctx, box_id).unwrap_or_default();
state.cursor.set_char_range(Some(range));
state.store(&h.ctx, box_id);
crate::widgets::set_cursor_range(&h.ctx, box_id, range);
h.run();
};
// The first position, once the box has a cursor: no selection, so no count.
set(&mut h, CCursorRange::one(CCursor::new(0)));
assert!(h.query_by_label("Ln 1, Col 1").is_some(), "the first position");
// On the wrapped line, past its first row: one line, its column from the line's start.
set(&mut h, CCursorRange::one(CCursor::new(13 + 250)));
assert!(h.query_by_label("Ln 2, Col 251").is_some(), "on a wrapped line");
Expand All @@ -3224,7 +3227,29 @@ mod tests {
assert!(h.query_by_label("Ln 1, Col 6").is_some(), "typed");
// A selection made backwards, from line 2 up to line 1: where its caret is, not its end.
set(&mut h, CCursorRange { primary: CCursor::new(1), secondary: CCursor::new(14 + 4), h_pos: None });
assert!(h.query_by_label("Ln 1, Col 2").is_some(), "a selection's caret");
assert!(
h.query_by_label("Ln 1, Col 2 (17 selected)").is_some(),
"a selection's caret, its count to the other end, a line break in it"
);
let label = h.get_by_label("Ln 1, Col 2 (17 selected)").rect();
let clear = h.get_by_label("Clear").rect();
assert!(
!label.intersects(clear) && label.bottom() <= clear.top(),
"the longer label still clear of the buttons: {label:?} {clear:?}"
);
// Forwards, within a line; then all of it.
set(&mut h, CCursorRange { primary: CCursor::new(5), secondary: CCursor::new(1), h_pos: None });
assert!(h.query_by_label("Ln 1, Col 6 (4 selected)").is_some(), "forwards");
let script = h.state().workflow.script.clone();
let all = script.chars().count();
let last_line = script.rsplit('\n').next().map_or(0, |l| l.chars().count());
set(&mut h, CCursorRange { primary: CCursor::new(all), secondary: CCursor::new(0), h_pos: None });
let lines = script.matches('\n').count() + 1;
assert!(h.query_by_label(&format!("Ln {lines}, Col {} ({all} selected)", last_line + 1)).is_some(), "everything");
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
// A script loaded from a Windows file: the count is the cursor's, so the line break is two.
h.state_mut().workflow.script = "a\r\nb".into();
set(&mut h, CCursorRange { primary: CCursor::new(4), secondary: CCursor::new(0), h_pos: None });
assert!(h.query_by_label("Ln 2, Col 2 (4 selected)").is_some(), "a CRLF line break counts two");
}

/// Details… opens the last run in a window of its own, each line folded: a result the pane cuts
Expand Down
6 changes: 3 additions & 3 deletions crates/ui-egui/src/completion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
//! is each language's.

use egui::text::CCursor;
use egui::widgets::text_edit::{TextEditOutput, TextEditState};
use egui::widgets::text_edit::TextEditOutput;
use egui::{Id, Key, Modifiers, Ui};
use std::collections::HashMap;
use std::sync::Arc;
Expand Down Expand Up @@ -309,7 +309,7 @@ impl Asked {
if !ui.memory(|m| m.has_focus(id)) || !ui.input_mut(|i| i.consume_key(Modifiers::CTRL, Key::Space)) {
return false;
}
let at = TextEditState::load(ui.ctx(), id).and_then(|s| s.cursor.char_range()).map(|r| r.primary.index.0);
let at = crate::widgets::cursor_range(ui.ctx(), id).map(|r| r.primary.index.0);
self.0 = at.map(|at| (egui::util::hash(text), at));
true
}
Expand Down Expand Up @@ -1059,7 +1059,7 @@ pub(crate) fn before(ui: &Ui, box_id: Id, text: &str) -> Option<Before> {
if !ui.memory(|m| m.has_focus(box_id)) {
return None;
}
let at = TextEditState::load(ui.ctx(), box_id)?.cursor.char_range().filter(|r| r.is_empty())?.primary.index.0;
let at = crate::widgets::cursor_range(ui.ctx(), box_id).filter(|r| r.is_empty())?.primary.index.0;
Some(Before { at, prev: at.checked_sub(1).and_then(|i| char_at(text, i)), len: text.len() })
}

Expand Down
16 changes: 4 additions & 12 deletions crates/ui-egui/src/exprcomplete.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1217,9 +1217,7 @@ mod tests {
h.run();
h.ctx.memory_mut(|m| m.request_focus(id));
let at = |h: &mut egui_kittest::Harness<'_, String>, i: usize| {
let mut state = TextEditState::load(&h.ctx, id).unwrap_or_default();
state.cursor.set_char_range(Some(CCursorRange::one(CCursor::new(i))));
state.store(&h.ctx, id);
crate::widgets::set_cursor_range(&h.ctx, id, CCursorRange::one(CCursor::new(i)));
h.run();
};
let shows = |h: &egui_kittest::Harness<'_, String>, sig: &str| h.query_by_role_and_label(Role::Label, sig).is_some();
Expand All @@ -1241,9 +1239,7 @@ mod tests {
// Text selected in the call, then the cursor back in it: still closed.
h.key_press(Key::Escape);
h.run();
let mut state = TextEditState::load(&h.ctx, id).unwrap_or_default();
state.cursor.set_char_range(Some(CCursorRange::two(CCursor::new(6), CCursor::new(7))));
state.store(&h.ctx, id);
crate::widgets::set_cursor_range(&h.ctx, id, CCursorRange::two(CCursor::new(6), CCursor::new(7)));
h.run();
at(&mut h, 7);
assert!(!shows(&h, round), "a selection in the call keeps it closed");
Expand Down Expand Up @@ -1493,9 +1489,7 @@ mod tests {
let mut h = boxed(id, Some(ds()), &start);
h.run();
h.ctx.memory_mut(|m| m.request_focus(id));
let mut state = TextEditState::load(&h.ctx, id).unwrap_or_default();
state.cursor.set_char_range(Some(CCursorRange::one(CCursor::new(start.chars().count()))));
state.store(&h.ctx, id);
crate::widgets::set_cursor_range(&h.ctx, id, CCursorRange::one(CCursor::new(start.chars().count())));
h.run();
h.event(egui::Event::Text("o".into()));
h.run();
Expand Down Expand Up @@ -1604,9 +1598,7 @@ mod tests {
let mut h = boxed(id, None, "x");
h.run();
h.ctx.memory_mut(|m| m.request_focus(id));
let mut state = TextEditState::load(&h.ctx, id).unwrap_or_default();
state.cursor.set_char_range(Some(CCursorRange::one(CCursor::new(1))));
state.store(&h.ctx, id);
crate::widgets::set_cursor_range(&h.ctx, id, CCursorRange::one(CCursor::new(1)));
h.run();
h.event(egui::Event::Text("(".into()));
h.run();
Expand Down
2 changes: 1 addition & 1 deletion crates/ui-egui/src/fieldmenu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ pub fn play(app: &mut RustGisApp, ctx: &Context) {
if ctx.input(|i| i.pointer.button_pressed(PointerButton::Secondary)) {
let hovered: Vec<Id> = ctx.interaction_snapshot(|s| s.hovered.iter().copied().collect());
menu.kept = hovered.into_iter().find_map(|id| {
let range = TextEditState::load(ctx, id)?.cursor.char_range()?;
let range = crate::widgets::cursor_range(ctx, id)?;
(!range.is_empty()).then_some((id, range))
});
}
Expand Down
4 changes: 1 addition & 3 deletions crates/ui-egui/src/scriptcomplete.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2883,10 +2883,8 @@ mod tests {
assert!(h.query_by_role_and_label(Role::Label, "round(x[, places])").is_some(), "another command's call");
// Straight back into the first command's braces (a click there).
let id = egui::Id::unique("script_text");
let mut state = egui::widgets::text_edit::TextEditState::load(&h.ctx, id).unwrap_or_default();
let back = egui::text::CCursorRange::one(egui::text::CCursor::new("labels.style {".chars().count()));
state.cursor.set_char_range(Some(back));
state.store(&h.ctx, id);
crate::widgets::set_cursor_range(&h.ctx, id, back);
h.run();
h.get_by_label(&styled);
}
Expand Down
13 changes: 13 additions & 0 deletions crates/ui-egui/src/widgets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,19 @@ pub(crate) fn changed(r: &Response) -> bool {
r.changed() && !escaped(r)
}

/// The cursor (and selection) of the text box `id`, in characters; `None` before it has one.
pub(crate) fn cursor_range(ctx: &egui::Context, id: egui::Id) -> Option<egui::text::CCursorRange> {
egui::widgets::text_edit::TextEditState::load(ctx, id)?.cursor.char_range()
}

/// Puts the cursor (and selection) of the text box `id` at `range`, keeping what else its state
/// holds (the undo history).
pub(crate) fn set_cursor_range(ctx: &egui::Context, id: egui::Id, range: egui::text::CCursorRange) {
let mut state = egui::widgets::text_edit::TextEditState::load(ctx, id).unwrap_or_default();
state.cursor.set_char_range(Some(range));
state.store(ctx, id);
}

pub fn text_w(ui: &Ui, s: &str, font: egui::FontId) -> f32 {
ui.ctx().fonts_mut(|f| f.layout_no_wrap(s.to_string(), font, egui::Color32::WHITE).size().x)
}
Expand Down
Loading