Skip to content

Commit c129542

Browse files
committed
On enter when having a focused cell allow edit
1 parent 9f7b61d commit c129542

File tree

2 files changed

+55
-10
lines changed

2 files changed

+55
-10
lines changed

src/array_table.rs

Lines changed: 40 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,15 @@ use crate::parser::{replace_occurrences, row_number_entry, search_occurrences};
88
use crate::subtable_window::SubTable;
99
use crate::{
1010
concat_string, set_open, ArrayResponse, Window, ACTIVE_COLOR, SHORTCUT_COPY, SHORTCUT_DELETE,
11-
SHORTCUT_REPLACE, SHORTCUT_SAVE_AS,
11+
SHORTCUT_REPLACE,
1212
};
1313
use eframe::egui::scroll_area::ScrollBarVisibility;
1414
use eframe::egui::style::Spacing;
1515
use eframe::egui::{
1616
Align, Context, CursorIcon, Id, Key, Label, Sense, Style, TextEdit, Ui, Vec2, Widget,
1717
WidgetText,
1818
};
19-
use egui::Key::Tab;
20-
use egui::{EventFilter, Modifiers, TextBuffer};
19+
use egui::{Modifiers, TextBuffer};
2120
use indexmap::IndexSet;
2221
use json_flat_parser::serializer::serialize_to_json_with_option;
2322
use json_flat_parser::{
@@ -151,6 +150,7 @@ pub struct ArrayTable<'array> {
151150
pub changed_matching_row_selected: bool,
152151
pub changed_arrow_horizontal_scroll: bool,
153152
pub changed_arrow_vertical_scroll: bool,
153+
pub was_editing: bool,
154154

155155
#[cfg(not(target_arch = "wasm32"))]
156156
pub changed_scroll_to_row_value: Option<std::time::Instant>,
@@ -278,7 +278,7 @@ impl<'array> super::View<ArrayResponse> for ArrayTable<'array> {
278278
if self.editing_index.borrow().is_none() {
279279
self.handle_shortcut(ui, &mut array_response);
280280
}
281-
281+
self.was_editing = false;
282282
array_response
283283
}
284284
}
@@ -432,6 +432,7 @@ impl<'array> ArrayTable<'array> {
432432
cache: Default::default(),
433433
opened_windows: Default::default(),
434434
search_replace_panel: Default::default(),
435+
was_editing: false,
435436
}
436437
}
437438
pub fn windows(&mut self, ctx: &Context, array_response: &mut ArrayResponse) {
@@ -659,14 +660,15 @@ impl<'array> ArrayTable<'array> {
659660
} else {
660661
None
661662
};
663+
let focused_cell = self.focused_cell.or(self.editing_index.borrow().map(|(column_index, row_index, is_pinned_column_table)| CellLocation { column_index, row_index, is_pinned_column_table }));
662664
let table_response = table
663665
.header(text_height * 2.0, |header| {
664666
self.header(pinned_column_table, header);
665667
})
666668
.body(
667669
self.hovered_row_index,
668670
search_highlight_row,
669-
self.focused_cell,
671+
focused_cell,
670672
|body| {
671673
self.body(
672674
text_height,
@@ -859,10 +861,12 @@ impl<'array> ArrayTable<'array> {
859861
if editing_index.is_some()
860862
&& editing_index.unwrap() == (col_index, row_index, pinned_column_table)
861863
{
864+
focused_changed = true;
865+
focused_cell = None;
862866
let ref_mut = &mut *self.editing_value.borrow_mut();
863867
let textedit_response = ui.add(TextEdit::singleline(ref_mut));
864868
if textedit_response.lost_focus()
865-
|| ui.ctx().input(|input| input.key_pressed(Key::Enter))
869+
|| ui.ctx().input_mut(|input| input.consume_key(Modifiers::NONE, Key::Enter))
866870
{
867871
let pointer = PointerKey {
868872
pointer: Self::pointer_key(
@@ -875,7 +879,13 @@ impl<'array> ArrayTable<'array> {
875879
position: 0,
876880
column_id: columns[col_index].id,
877881
};
878-
updated_value = Some((pointer, mem::take(ref_mut)))
882+
updated_value = Some((pointer, mem::take(ref_mut)));
883+
focused_changed = true;
884+
focused_cell = Some(CellLocation {
885+
column_index: col_index,
886+
row_index: table_row_index,
887+
is_pinned_column_table: pinned_column_table,
888+
});
879889
} else {
880890
textedit_response.request_focus();
881891
}
@@ -1098,6 +1108,7 @@ impl<'array> ArrayTable<'array> {
10981108
};
10991109

11001110
self.edit_cell(array_response, value_changed, row_index);
1111+
self.was_editing = true;
11011112
}
11021113
if self.hovered_row_index != hover_data.hovered_row {
11031114
self.hovered_row_index = hover_data.hovered_row;
@@ -1515,6 +1526,28 @@ impl<'array> ArrayTable<'array> {
15151526
self.changed_arrow_vertical_scroll = true;
15161527
}
15171528
}
1529+
if i.consume_key(Modifiers::NONE, Key::Enter) && !self.was_editing {
1530+
*self.editing_index.borrow_mut() = Some((
1531+
focused_cell.column_index,
1532+
focused_cell.row_index,
1533+
focused_cell.is_pinned_column_table,
1534+
));
1535+
let row_index = self.filtered_nodes[focused_cell.row_index];
1536+
let mut editing_value = String::new();
1537+
let col_index = focused_cell.column_index;
1538+
let is_pinned_column_table = focused_cell.is_pinned_column_table;
1539+
{
1540+
let node = self.nodes().get(row_index);
1541+
if let Some(row_data) = node.as_ref() {
1542+
let index = self.get_pointer_index_from_cache(is_pinned_column_table, row_data, col_index, );
1543+
if let Some(index) = index {
1544+
row_data.entries()[index].value.clone().map(|v| editing_value = v);
1545+
}
1546+
}
1547+
}
1548+
1549+
*self.editing_value.borrow_mut() = editing_value;
1550+
}
15181551
}
15191552
if i.consume_shortcut(&SHORTCUT_DELETE) {
15201553
i.events.push(egui::Event::Key {

src/object_table.rs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,16 @@ pub struct ObjectTable {
1515
pub nodes: Vec<FlatJsonValue<String>>,
1616
filtered_nodes: Vec<usize>,
1717
arrays: Vec<FlatJsonValue<String>>,
18+
pub scroll_to_row_number: usize,
1819

19-
// Handling interaction
2020

2121
pub editing_index: RefCell<Option<usize>>,
2222
pub editing_value: RefCell<String>,
2323
pub focused_cell: Option<CellLocation>,
2424

25-
pub scroll_to_row_number: usize,
25+
// Handling interaction
2626
pub changed_arrow_vertical_scroll: bool,
27+
pub was_editing: bool,
2728
}
2829

2930
impl ObjectTable {
@@ -46,6 +47,7 @@ impl ObjectTable {
4647
focused_cell: None,
4748
scroll_to_row_number: 0,
4849
changed_arrow_vertical_scroll: false,
50+
was_editing: false,
4951
}
5052
}
5153

@@ -136,6 +138,7 @@ impl ObjectTable {
136138
let editing_index = mem::take(&mut *self.editing_index.borrow_mut());
137139
let row_index = editing_index.unwrap();
138140
self.update_value(&mut array_response, updated_pointer, value, row_index);
141+
self.was_editing = true;
139142
}
140143
});
141144
array_response
@@ -246,6 +249,13 @@ impl ObjectTable {
246249
self.changed_arrow_vertical_scroll = true;
247250
}
248251
}
252+
if i.consume_key(Modifiers::NONE, Key::Enter) && !self.was_editing {
253+
*self.editing_index.borrow_mut() = Some(focused_cell.row_index);
254+
255+
let row_index = self.filtered_nodes[focused_cell.row_index];
256+
let entry = &self.nodes[row_index];
257+
*self.editing_value.borrow_mut() = entry.value.clone().unwrap_or_default();
258+
}
249259
}
250260
if i.consume_shortcut(&SHORTCUT_DELETE) {
251261
i.events.push(egui::Event::Key {
@@ -296,7 +306,7 @@ impl super::View<ArrayResponse> for ObjectTable {
296306
fn ui(&mut self, ui: &mut egui::Ui) -> ArrayResponse {
297307
use egui_extras::{Size, StripBuilder};
298308
let mut array_response = ArrayResponse::default();
299-
StripBuilder::new(ui)
309+
let response = StripBuilder::new(ui)
300310
.size(Size::remainder())
301311
.vertical(|mut strip| {
302312
strip.cell(|ui| {
@@ -308,9 +318,11 @@ impl super::View<ArrayResponse> for ObjectTable {
308318
});
309319
});
310320
});
321+
311322
if self.editing_index.borrow().is_none() {
312323
self.handle_shortcut(ui, &mut array_response);
313324
}
325+
self.was_editing = false;
314326
array_response
315327
}
316328
}

0 commit comments

Comments
 (0)