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
10 changes: 8 additions & 2 deletions crates/ui-egui/src/chrome.rs
Original file line number Diff line number Diff line change
Expand Up @@ -917,6 +917,8 @@ pub fn shortcuts(app: &mut RustGisApp, ctx: &egui::Context) {
let element = app.session.active_layout.is_some() && app.layout.selected.is_some();
// The pane the user last pressed or scrolled in: Delete and Rename act on it.
let pane = crate::inuse::pane(app);
// The arrows nudge that element, unless the attribute table is in use: they are its cells'.
let nudges = element && pane != Pane::Table;
// What to run now, each with the pane that was in use as its key went down, and what to run
// once the panes are drawn ([`late_keys`]).
let mut run: Vec<(String, Value, Pane)> = Vec::new();
Expand Down Expand Up @@ -954,7 +956,7 @@ pub fn shortcuts(app: &mut RustGisApp, ctx: &egui::Context) {
}
// With a layout element selected, the arrows nudge it (below), whatever else they run;
// the nudge never takes Ctrl (⌘), so a shortcut with it still runs.
if element && !b.chord.command && matches!(b.chord.key, Key::ArrowLeft | Key::ArrowRight | Key::ArrowUp | Key::ArrowDown) {
if nudges && !b.chord.command && matches!(b.chord.key, Key::ArrowLeft | Key::ArrowRight | Key::ArrowUp | Key::ArrowDown) {
continue;
}
if i.consume_shortcut(&KeyboardShortcut::new(b.chord.modifiers(), b.chord.key)) {
Expand Down Expand Up @@ -988,7 +990,7 @@ pub fn shortcuts(app: &mut RustGisApp, ctx: &egui::Context) {
}
// Arrow keys nudge the selected layout element, further with Shift; held, it keeps
// going. Shift first: a plain arrow's pattern takes Shift-arrows too.
if element {
if nudges {
use crate::layoutview::{NUDGE_FAST_MM, NUDGE_MM};
for (key, dx, dy) in
[(Key::ArrowLeft, -1.0, 0.0), (Key::ArrowRight, 1.0, 0.0), (Key::ArrowUp, 0.0, -1.0), (Key::ArrowDown, 0.0, 1.0)]
Expand Down Expand Up @@ -1023,6 +1025,10 @@ pub fn shortcuts(app: &mut RustGisApp, ctx: &egui::Context) {
}
}
});
// After the shortcuts, so a key the user bound to a command still runs it.
if !typing {
crate::table::take_keys(app, ctx);
}
let dialogs = dialogs_up(app);
crate::inuse::keep_late(app, late, dialogs);
run_keys(app, ctx, &run);
Expand Down
6 changes: 4 additions & 2 deletions crates/ui-egui/src/mapview.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3075,8 +3075,10 @@ fn interact(app: &mut RustGisApp, ui: &mut Ui, resp: &egui::Response, rect: Rect
}
// Arrow keys pan; Shift+Left and Shift+Right turn the map, Shift+Up and Shift+Down tilt it
// (as in MapLibre). With Alt or Mod held the arrows are shortcuts (Alt+Shift+arrows move the
// active layer in the Layers pane's outline, #836), not map moves.
if !ui.ctx().egui_wants_keyboard_input() {
// active layer in the Layers pane's outline, #836), not map moves. With the attribute table
// in use they move its current cell, and the map stays: held keys are read here, so the
// table taking the key presses wouldn't stop them.
if !ui.ctx().egui_wants_keyboard_input() && crate::inuse::pane(app) != crate::inuse::Pane::Table {
let (mut dx, mut dy, mut turn, mut tilt) = (0.0, 0.0, 0.0, 0.0);
ui.input(|i| {
if i.modifiers.alt || i.modifiers.command {
Expand Down
558 changes: 544 additions & 14 deletions crates/ui-egui/src/table.rs

Large diffs are not rendered by default.

222 changes: 222 additions & 0 deletions crates/ui-egui/src/table/cursor.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,222 @@
//! The attribute table's current cell: the one last clicked, which the arrow keys move and Enter
//! edits. View state only: never saved, and no command sees it.

use std::sync::Weak;

use egui::{Context, Event, Key, Rangef, Stroke, StrokeKind, Ui};
use rustgis_engine::data::{Dataset, Feature, Value};

use super::{CellEdit, ViewKey, tag};
use crate::RustGisApp;
use crate::inuse::Pane;
use crate::theme::Tokens;

/// Takes the table's keys while it is the pane in use: the arrows with no modifier, and Enter
/// while there is a current cell. [`crate::chrome::shortcuts`] calls it once the shortcuts have
/// taken theirs, and not while text is being typed, so nothing drawn later in the frame takes
/// these too. [`super::show`] acts on them.
pub(crate) fn take_keys(app: &mut RustGisApp, ctx: &Context) {
// An open menu is over the table: no cell moves or opens under it. And a widget that has the
// keyboard (a toolbar button reached with Tab) keeps the keys: Enter presses it, and the
// arrows move the focus on.
if crate::inuse::pane(app) != Pane::Table || egui::Popup::is_any_open(ctx) || ctx.memory(|m| m.focused()).is_some() {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With a modal up (Save changes?, Recover unsaved work, the Confirm delete / Reload data / Shortcut conflict prompts), take_keys still runs. Its checks cover egui's focus, popups and app.dialogs.open, but not egui::Modal. So when no widget in the modal has focus, an arrow moves the table's current cell under the prompt, and Enter opens a cell editor behind it. The pane can still read as Table when the prompt is reached from the keyboard. chrome::behind_a_dialog already treats ctx.memory(|m| m.top_modal_layer()) as a dialog, so this would follow that precedent. Suggested change to the early return:

Suggested change
if crate::inuse::pane(app) != Pane::Table || egui::Popup::is_any_open(ctx) || ctx.memory(|m| m.focused()).is_some() {
if crate::inuse::pane(app) != Pane::Table
|| egui::Popup::is_any_open(ctx)
|| ctx.memory(|m| m.focused().is_some() || m.top_modal_layer().is_some())
{

Confidence: medium-low. I reasoned this from the code and did not run it, and I haven't confirmed that each of those prompts is an egui::Modal with no focused widget on open. A test that opens one and presses an arrow and Enter would settle it.

Category: bug

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not reached, so no change. take_keys is called only from chrome::shortcuts, and that returns at its first line when behind_a_dialog is true (Command Search, the save prompt, ctx.memory(|m| m.top_modal_layer()), shortcut capture), before any key is read. With one of those prompts up the table takes nothing, and the arrows and Enter are the prompt's.

return;
}
// A dialog that is up has Enter for itself.
let enter = app.table.current.is_some() && app.table.dates.is_none() && app.dialogs.open.is_none() && app.ui.choosing.is_empty();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Enter is taken here, before the panes draw, so a widget in the table's toolbar that has the keyboard focus (reached with Tab, which this app supports; see the comment at chrome.rs:914) no longer gets Enter while the table is in use and a current cell exists. egui activates a focused button on Enter, so that button does nothing and the cell editor opens instead. Taking Enter only when no widget holds the focus would fix it, for example:

Suggested change
let enter = app.table.current.is_some() && app.table.dates.is_none() && app.dialogs.open.is_none() && app.ui.choosing.is_empty();
let enter = app.table.current.is_some()
&& app.table.dates.is_none()
&& app.dialogs.open.is_none()
&& app.ui.choosing.is_empty()
&& ctx.memory(|m| m.focused()).is_none();

Check the trade-off before adopting it: if a click on a toolbar button leaves the focus on that button, Enter-to-edit stops until the focus moves. No new test presses Enter on a focused toolbar button. Confidence: medium (reasoned from the code, not run).

Category: bug

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 1ce72c7, a little wider than suggested: while any widget has the keyboard focus the table takes none of its keys, so Enter presses a focused button and the arrows move the focus on. The trade-off you named doesn't bite: a click on a cell leaves no widget focused, and the test checks it (Tab gives a widget the keyboard, an arrow then leaves the current cell where it is, a click on a cell gives the arrows back).

let keys = &mut app.table.keys;
ctx.input_mut(|input| {
input.events.retain(|event| {
let Event::Key { key, pressed: true, repeat, modifiers, .. } = event else { return true };
match key {
_ if !modifiers.is_none() => true,
Key::ArrowUp | Key::ArrowDown | Key::ArrowLeft | Key::ArrowRight => {
keys.push(*key);
false
}
Key::Enter if enter => {
// Held after it kept a value, its repeats would open the cell again.
if !*repeat {
keys.push(*key);
}
false
}
_ => true,
}
});
});
}

/// The table as the frame draws it.
pub(super) struct Shown<'a> {
pub view: ViewKey,
pub table_instance: Option<u64>,
pub ds: &'a Dataset,
/// The rows shown, each by its row in the dataset, or None for all of them in its order.
pub rows: Option<&'a [usize]>,
/// The fields shown, in the order of their columns, which follow Feature ID's.
pub columns: &'a [usize],
}

impl Shown<'_> {
/// The cell in the row at place `at` of those shown, in field `field` (None: Feature ID).
pub fn cell(&self, at: usize, field: Option<usize>) -> Option<Current> {
let row = match self.rows {
Some(rows) => *rows.get(at)?,
None => at,
};
let fid = self.ds.features.row(row)?.id();
let field = match field {
Some(i) => Some(tag(&self.ds.fields, i)?),
None => None,
};
let named = field.as_ref().map_or(0, |(name, _)| self.ds.fields.iter().filter(|f| &f.name == name).count());
let source = self.ds.features.allocation_key();
Some(Current { view: self.view, table_instance: self.table_instance, fid, field, named, row, at, source })
}

/// Where an arrow key puts the current cell: one row or column on, as shown, or None at an
/// edge. With no current cell, it goes to the first row and column shown; with its row or its
/// column no longer shown, to the first of those, keeping the other.
pub fn step(&self, current: Option<&mut Current>, key: Key) -> Option<Current> {
let (at, column) = current.map_or((None, None), |c| (c.place(self), c.column(self)));
let (at, column) = match (at, column) {
(Some(at), Some(column)) => match key {
Key::ArrowUp => (at.checked_sub(1)?, column),
Key::ArrowDown => (at.checked_add(1)?, column),
Key::ArrowLeft => (at, column.checked_sub(1)?),
Key::ArrowRight => (at, column.checked_add(1)?),
_ => return None,
},
(at, column) => (at.unwrap_or(0), column.unwrap_or(0)),
};
let field = match column.checked_sub(1) {
Some(i) => Some(*self.columns.get(i)?),
None => None,
};
self.cell(at, field)
}

/// Opens the editor on field `field` of feature `fid`, in row `row`: what a double-click on a
/// cell does, and Enter on the current one. What the editor open before it holds is kept first,
/// in `commits` (once: it may have been kept on losing the focus, or left by `cancel`). False,
/// with nothing changed, for a column whose name finds another field, where its value would be
/// written.
pub fn open_editor(
&self,
(row, fid, field): (usize, u64, usize),
editing: &mut Option<CellEdit>,
commits: &mut Vec<(u64, usize, String, String)>,
cancel: Option<(u64, usize)>,
) -> bool {
let fields = &self.ds.fields;
if fields.get(field).is_some_and(|f| self.ds.field_index(&f.name) != Some(field)) {
return false;
}
if let Some(e) = editing.take()
&& !commits.iter().any(|q| q.0 == e.fid && q.1 == e.col)
&& cancel != Some((e.fid, e.col))
{
commits.push((e.fid, e.col, e.text, e.was));
}
let value = self.ds.value(row, field);
let text = if matches!(value, Value::Null) { String::new() } else { value.to_string() };
*editing = Some(CellEdit::new(self.view, fid, field, text, fields, self.table_instance));
true
}
}

/// The current cell. Its feature is kept by id and its field by [`tag`], so sorting, filtering,
/// hiding or moving columns and edits to other rows and fields leave it where it is.
pub(super) struct Current {
view: ViewKey,
/// Which standalone table it is of: another can take its id.
table_instance: Option<u64>,
fid: u64,
/// None for the Feature ID cell.
field: Option<(String, usize)>,
/// How many fields had its field's name. With another number of them (one of a repeated name
/// added or deleted), which of them it was can't be told, and the cell is dropped.
named: usize,
/// The feature's row in the dataset, and its place among the rows shown, when last found.
/// Looked at first, so the rows are searched only once they or their order have changed.
row: usize,
at: usize,
/// The rows it was found in ([`rustgis_engine::data::Features::allocation_key`]).
source: Weak<Vec<Feature>>,
}

impl Current {
/// The index of its field: Some(None) for Feature ID, None when the field has gone.
fn field_index(&self, ds: &Dataset) -> Option<Option<usize>> {
let Some((name, nth)) = &self.field else { return Some(None) };
let named = || ds.fields.iter().enumerate().filter(|(_, f)| &f.name == name);
if named().count() != self.named {
return None;
}
named().nth(*nth).map(|(i, _)| Some(i))
}

/// Keeps up with the table as it is now. False, for the cell to be dropped, when the table
/// shows another layer or table, or the feature or the field has gone. The feature is looked
/// for only after a change to the rows, and in every row only when it left its own.
pub fn follow(&mut self, table: &Shown) -> bool {
if self.view != table.view || self.table_instance != table.table_instance || self.field_index(table.ds).is_none() {
return false;
}
let source = table.ds.features.allocation_key();
if !self.source.ptr_eq(&source) {
if table.ds.features.row(self.row).is_none_or(|r| r.id() != self.fid) {
let Some(row) = table.ds.features.rows().position(|r| r.id() == self.fid) else { return false };
self.row = row;
}
self.source = source;
}
true
}

/// The cell to outline: its row in the dataset and its field (None: Feature ID).
pub fn mark(&self, ds: &Dataset) -> Option<(usize, Option<usize>)> {
Some((self.row, self.field_index(ds)?))
}

/// Its place among the rows shown, or None when the filter or Selected only leaves it out.
pub fn place(&mut self, table: &Shown) -> Option<usize> {
match table.rows {
None => self.at = self.row,
Some(rows) if rows.get(self.at) == Some(&self.row) => {}
Some(rows) => self.at = rows.iter().position(|&r| r == self.row)?,
}
Some(self.at)
}

/// Its column among those shown, Feature ID's being the first; None when its field is hidden.
fn column(&self, table: &Shown) -> Option<usize> {
match self.field_index(table.ds)? {
Some(field) => table.columns.iter().position(|&c| c == field).map(|i| i + 1),
None => Some(0),
}
}

/// What Enter edits: the row, feature and field of a cell that is shown. None for a Feature ID
/// cell, which can't be edited, and for a cell the table leaves out.
pub fn editable(&mut self, table: &Shown) -> Option<(usize, u64, usize)> {
self.place(table)?;
let field = self.field_index(table.ds)??;
table.columns.contains(&field).then_some((self.row, self.fid, field))
}
}

/// The current cell's outline, around the cell `ui` is. It reaches halfway into the gap before
/// the cell, to stand clear of the text, but not past `view`, the table's width on screen.
pub(super) fn outline(ui: &Ui, t: &Tokens, view: Rangef) {
let gap = ui.spacing().item_spacing.x * 0.5;
let (mut rect, mut clip) = (ui.max_rect(), ui.clip_rect());
rect.min.x = (rect.min.x - gap).max(view.min);
clip.min.x = (clip.min.x - gap).max(view.min);
if rect.is_positive() {
let mut painter = ui.painter().clone();
painter.set_clip_rect(clip);
painter.rect_stroke(rect, 2.0, Stroke::new(1.5, t.accent), StrokeKind::Inside);
}
}
10 changes: 10 additions & 0 deletions docs/user-guide/analysis.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,16 @@ deletes the selection, adds or calculates a field, and calculates geometry attri
The first column, **Feature ID**, shows each feature's identifier, not its current row position
after sorting or filtering. It is not a dataset field: do not use it as a geoprocessing field name.

The cell last clicked is the current cell, shown with an outline. After a click or a scroll in the
table, the **arrow keys** move it: up and down through the rows as the table sorts and filters them,
left and right through the columns shown, **Feature ID** first. It stops at the edges, the table
scrolls to keep it in view, and a held key keeps moving. With no current cell, or with its row or
column no longer shown, an arrow starts at the first row or column shown. Moving it doesn't change
the selection. **Enter** edits it, as a double-click does (Feature ID can't be edited). While the
table is in use the arrow keys don't pan, turn or tilt the map or nudge a layout element; click or
scroll the map or layout and they do again. While you type in a cell or in **Filter rows…**, the
keys are the text box's. A shortcut you give an arrow key or Enter still runs its command.

Right-click a row for **Zoom To**, **Remove from Selection**, **Copy** (the feature, as
**Copy** in the ribbon does), **Copy Value** (the text of the cell clicked; a number is copied in
full and a picture gives its stored data) and **Delete** (Undo brings the rows back; greyed while
Expand Down
11 changes: 7 additions & 4 deletions docs/user-guide/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -549,6 +549,7 @@ with `{"layer":"parcels"}`.
| ++delete++ | Delete selected features; with Edit Vertices, the chosen vertex; in a layout, the selected element; in the layer list, remove the selected layers (++backspace++ too) |
| ++ctrl+d++ | Duplicate the selected layout element |
| Arrow keys / ++shift++ + arrow keys | Nudge the selected layout element / nudge it further |
| Arrow keys / ++enter++ | In the attribute table, move the current cell / edit it |
| ++alt+shift+right++ / ++alt+shift+left++ | Indent the active layer into the group above it / outdent it out of its group |
| ++alt+shift+up++ / ++alt+shift+down++ | Move the active layer up / down one step in the Layers pane |
| ++f2++ | Rename the active layer; on the map and layout tabs, and in a layout, the map or layout in view |
Expand All @@ -559,10 +560,12 @@ On macOS, ++cmd++ takes the place of ++ctrl++. **Help › Shortcuts** lists them
from the same table the app runs them from. While you type in a text field, undo and redo take
back its typing, not the project's last change.

++delete++ and ++f2++ act on the part of the window you last clicked, dragged or scrolled in.
Click a layer in the Layers pane and ++delete++ removes it (Undo brings it back), whatever is
selected on the map; click or scroll the map and it deletes the selected features again. When
++delete++ has nothing to act on, a notice says why.
++delete++, ++f2++ and the arrow keys act on the part of the window you last clicked, dragged or
scrolled in. Click a layer in the Layers pane and ++delete++ removes it (Undo brings it back),
whatever is selected on the map; click or scroll the map and it deletes the selected features
again. When ++delete++ has nothing to act on, a notice says why. In the
[attribute table](analysis.md#the-attribute-table) the arrow keys move between cells; on the map
they pan it, and in a layout they nudge the selected element.

Copy and Paste follow it too. In the Layers pane they copy and paste the selected layers, as **Copy
Layer** and **Paste Layer** in a layer's menu do: the copies go above the active layer, or at the
Expand Down
3 changes: 2 additions & 1 deletion docs/user-guide/layouts.md
Original file line number Diff line number Diff line change
Expand Up @@ -586,7 +586,8 @@ Layouts open as tabs next to the maps; while one is open, its groups lead the Pu
**Delete** (or **Backspace**, the delete key on a
Mac) deletes the selected element, and **Ctrl+D** (**⌘D** on a Mac) duplicates it: the copy is
placed 5 mm down and to the right, on top, and selected. The **arrow keys** nudge it 1 mm, or
10 mm with **Shift**, and keep it moving while held; a run of nudges is one step to undo.
10 mm with **Shift**, and keep it moving while held; a run of nudges is one step to undo. After
a click or a scroll in the attribute table, the arrows move between its cells instead.
**Right-click** an element to select it and open a menu of Duplicate, Delete, the four Arrange
steps and, on a map frame, **Zoom Frame** (the map view's extent, as a double-click does).
- **Arrange**: elements later in the layout are drawn over earlier ones, and a click picks the one
Expand Down
4 changes: 3 additions & 1 deletion docs/user-guide/maps.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,9 @@ view to it, and says so.
**Rotating and tilting the map.** Drag with the right mouse button, or hold ++ctrl++ and drag
with the Pan tool, to turn the map about the centre of the view (to the right turns it clockwise)
and to tilt it (up tilts it further), as in MapLibre; or hold ++shift++ and press ++arrow-left++ or ++arrow-right++ to turn it, ++arrow-up++ or ++arrow-down++
to tilt it. The north arrow turns with the map to point north and leans back as it tilts; click
to tilt it (after a click or a scroll in the
[attribute table](analysis.md#the-attribute-table), the arrow keys move between its cells
instead). The north arrow turns with the map to point north and leans back as it tilts; click
it to face north and look straight down again. The bearing (the compass direction the top of the
view faces, in degrees clockwise from north) and the pitch (the tilt from looking straight down,
0 to 85 degrees) work as in MapLibre. They are saved with the project, its bookmarks and bookmark
Expand Down
Loading