Skip to content

Commit

Permalink
On enter when having a focused cell allow edit
Browse files Browse the repository at this point in the history
  • Loading branch information
nmeylan committed Dec 13, 2024
1 parent 9f7b61d commit c129542
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 10 deletions.
47 changes: 40 additions & 7 deletions src/array_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,15 @@ use crate::parser::{replace_occurrences, row_number_entry, search_occurrences};
use crate::subtable_window::SubTable;
use crate::{
concat_string, set_open, ArrayResponse, Window, ACTIVE_COLOR, SHORTCUT_COPY, SHORTCUT_DELETE,
SHORTCUT_REPLACE, SHORTCUT_SAVE_AS,
SHORTCUT_REPLACE,
};
use eframe::egui::scroll_area::ScrollBarVisibility;
use eframe::egui::style::Spacing;
use eframe::egui::{
Align, Context, CursorIcon, Id, Key, Label, Sense, Style, TextEdit, Ui, Vec2, Widget,
WidgetText,
};
use egui::Key::Tab;
use egui::{EventFilter, Modifiers, TextBuffer};
use egui::{Modifiers, TextBuffer};
use indexmap::IndexSet;
use json_flat_parser::serializer::serialize_to_json_with_option;
use json_flat_parser::{
Expand Down Expand Up @@ -151,6 +150,7 @@ pub struct ArrayTable<'array> {
pub changed_matching_row_selected: bool,
pub changed_arrow_horizontal_scroll: bool,
pub changed_arrow_vertical_scroll: bool,
pub was_editing: bool,

#[cfg(not(target_arch = "wasm32"))]
pub changed_scroll_to_row_value: Option<std::time::Instant>,
Expand Down Expand Up @@ -278,7 +278,7 @@ impl<'array> super::View<ArrayResponse> for ArrayTable<'array> {
if self.editing_index.borrow().is_none() {
self.handle_shortcut(ui, &mut array_response);
}

self.was_editing = false;
array_response
}
}
Expand Down Expand Up @@ -432,6 +432,7 @@ impl<'array> ArrayTable<'array> {
cache: Default::default(),
opened_windows: Default::default(),
search_replace_panel: Default::default(),
was_editing: false,
}
}
pub fn windows(&mut self, ctx: &Context, array_response: &mut ArrayResponse) {
Expand Down Expand Up @@ -659,14 +660,15 @@ impl<'array> ArrayTable<'array> {
} else {
None
};
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 }));
let table_response = table
.header(text_height * 2.0, |header| {
self.header(pinned_column_table, header);
})
.body(
self.hovered_row_index,
search_highlight_row,
self.focused_cell,
focused_cell,
|body| {
self.body(
text_height,
Expand Down Expand Up @@ -859,10 +861,12 @@ impl<'array> ArrayTable<'array> {
if editing_index.is_some()
&& editing_index.unwrap() == (col_index, row_index, pinned_column_table)
{
focused_changed = true;
focused_cell = None;
let ref_mut = &mut *self.editing_value.borrow_mut();
let textedit_response = ui.add(TextEdit::singleline(ref_mut));
if textedit_response.lost_focus()
|| ui.ctx().input(|input| input.key_pressed(Key::Enter))
|| ui.ctx().input_mut(|input| input.consume_key(Modifiers::NONE, Key::Enter))
{
let pointer = PointerKey {
pointer: Self::pointer_key(
Expand All @@ -875,7 +879,13 @@ impl<'array> ArrayTable<'array> {
position: 0,
column_id: columns[col_index].id,
};
updated_value = Some((pointer, mem::take(ref_mut)))
updated_value = Some((pointer, mem::take(ref_mut)));
focused_changed = true;
focused_cell = Some(CellLocation {
column_index: col_index,
row_index: table_row_index,
is_pinned_column_table: pinned_column_table,
});
} else {
textedit_response.request_focus();
}
Expand Down Expand Up @@ -1098,6 +1108,7 @@ impl<'array> ArrayTable<'array> {
};

self.edit_cell(array_response, value_changed, row_index);
self.was_editing = true;
}
if self.hovered_row_index != hover_data.hovered_row {
self.hovered_row_index = hover_data.hovered_row;
Expand Down Expand Up @@ -1515,6 +1526,28 @@ impl<'array> ArrayTable<'array> {
self.changed_arrow_vertical_scroll = true;
}
}
if i.consume_key(Modifiers::NONE, Key::Enter) && !self.was_editing {
*self.editing_index.borrow_mut() = Some((
focused_cell.column_index,
focused_cell.row_index,
focused_cell.is_pinned_column_table,
));
let row_index = self.filtered_nodes[focused_cell.row_index];
let mut editing_value = String::new();
let col_index = focused_cell.column_index;
let is_pinned_column_table = focused_cell.is_pinned_column_table;
{
let node = self.nodes().get(row_index);
if let Some(row_data) = node.as_ref() {
let index = self.get_pointer_index_from_cache(is_pinned_column_table, row_data, col_index, );
if let Some(index) = index {
row_data.entries()[index].value.clone().map(|v| editing_value = v);
}
}
}

*self.editing_value.borrow_mut() = editing_value;
}
}
if i.consume_shortcut(&SHORTCUT_DELETE) {
i.events.push(egui::Event::Key {
Expand Down
18 changes: 15 additions & 3 deletions src/object_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,16 @@ pub struct ObjectTable {
pub nodes: Vec<FlatJsonValue<String>>,
filtered_nodes: Vec<usize>,
arrays: Vec<FlatJsonValue<String>>,
pub scroll_to_row_number: usize,

// Handling interaction

pub editing_index: RefCell<Option<usize>>,
pub editing_value: RefCell<String>,
pub focused_cell: Option<CellLocation>,

pub scroll_to_row_number: usize,
// Handling interaction
pub changed_arrow_vertical_scroll: bool,
pub was_editing: bool,
}

impl ObjectTable {
Expand All @@ -46,6 +47,7 @@ impl ObjectTable {
focused_cell: None,
scroll_to_row_number: 0,
changed_arrow_vertical_scroll: false,
was_editing: false,
}
}

Expand Down Expand Up @@ -136,6 +138,7 @@ impl ObjectTable {
let editing_index = mem::take(&mut *self.editing_index.borrow_mut());
let row_index = editing_index.unwrap();
self.update_value(&mut array_response, updated_pointer, value, row_index);
self.was_editing = true;
}
});
array_response
Expand Down Expand Up @@ -246,6 +249,13 @@ impl ObjectTable {
self.changed_arrow_vertical_scroll = true;
}
}
if i.consume_key(Modifiers::NONE, Key::Enter) && !self.was_editing {
*self.editing_index.borrow_mut() = Some(focused_cell.row_index);

let row_index = self.filtered_nodes[focused_cell.row_index];
let entry = &self.nodes[row_index];
*self.editing_value.borrow_mut() = entry.value.clone().unwrap_or_default();
}
}
if i.consume_shortcut(&SHORTCUT_DELETE) {
i.events.push(egui::Event::Key {
Expand Down Expand Up @@ -296,7 +306,7 @@ impl super::View<ArrayResponse> for ObjectTable {
fn ui(&mut self, ui: &mut egui::Ui) -> ArrayResponse {
use egui_extras::{Size, StripBuilder};
let mut array_response = ArrayResponse::default();
StripBuilder::new(ui)
let response = StripBuilder::new(ui)
.size(Size::remainder())
.vertical(|mut strip| {
strip.cell(|ui| {
Expand All @@ -308,9 +318,11 @@ impl super::View<ArrayResponse> for ObjectTable {
});
});
});

if self.editing_index.borrow().is_none() {
self.handle_shortcut(ui, &mut array_response);
}
self.was_editing = false;
array_response
}
}

0 comments on commit c129542

Please sign in to comment.