Skip to content

Commit

Permalink
Scroll to column: we can go to all matching columns
Browse files Browse the repository at this point in the history
  • Loading branch information
nmeylan committed Jun 28, 2024
1 parent 98d2e87 commit aa5e608
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 20 deletions.
35 changes: 23 additions & 12 deletions src/array_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,6 @@ pub enum ScrollToRowMode {
}



impl ScrollToRowMode {
pub fn as_str(&self) -> &'static str {
match self {
Expand Down Expand Up @@ -114,13 +113,16 @@ pub struct ArrayTable {
seed2: usize, // seed for Id
pub matching_rows: Vec<usize>,
pub matching_row_selected: usize,
pub matching_columns: Vec<usize>,
pub matching_column_selected: usize,
pub scroll_to_column: String,
pub scroll_to_row: String,
pub scroll_to_row_mode: ScrollToRowMode,

// Handle interaction
pub next_frame_reset_scroll: bool,
pub changed_scroll_to_column_value: bool,
pub changed_matching_column_selected: bool,
pub changed_matching_row_selected: bool,
pub changed_scroll_to_row_value: Option<Instant>,

Expand Down Expand Up @@ -151,16 +153,23 @@ impl super::View<ArrayResponse> for ArrayTable {
let mut scroll_to_x = None;
if self.changed_scroll_to_column_value {
self.changed_scroll_to_column_value = false;
let mut index = self.column_selected.iter().position(|c| {
c.name.to_lowercase().eq(&concat_string!("/", &self.scroll_to_column.to_lowercase()))
});
if index.is_none() {
index = self.column_selected.iter().position(|c| {
c.name.to_lowercase().contains(&self.scroll_to_column.to_lowercase())
});
self.changed_matching_column_selected = true;
self.matching_columns.clear();
self.matching_column_selected = 0;
if !self.scroll_to_column.is_empty() {
for (index, column) in self.column_selected.iter().enumerate() {
if column.name.to_lowercase().eq(&concat_string!("/", &self.scroll_to_column.to_lowercase()))
|| column.name.to_lowercase().contains(&self.scroll_to_column.to_lowercase()) {
self.matching_columns.push(index);
}
}
}
if let Some(index) = index {
if let Some(offset) = self.columns_offset.get(index) {
}

if self.changed_matching_column_selected {
self.changed_matching_column_selected = false;
if !self.matching_columns.is_empty() {
if let Some(offset) = self.columns_offset.get(self.matching_columns[self.matching_column_selected]) {
scroll_to_x = Some(*offset);
}
}
Expand Down Expand Up @@ -243,6 +252,8 @@ impl ArrayTable {
windows: vec![],
matching_rows: vec![],
matching_row_selected: 0,
matching_columns: vec![],
matching_column_selected: 0,
scroll_to_column: "".to_string(),
changed_scroll_to_column_value: false,
last_parsed_max_depth,
Expand All @@ -251,6 +262,7 @@ impl ArrayTable {
scroll_to_row: "".to_string(),
changed_scroll_to_row_value: None,
changed_matching_row_selected: false,
changed_matching_column_selected: false,
editing_index: RefCell::new(None),
editing_value: RefCell::new(String::new()),
is_sub_table: false,
Expand Down Expand Up @@ -343,7 +355,6 @@ impl ArrayTable {
}

pub fn row_height(style: &Arc<Style>, spacing: &Spacing) -> f32 {

egui::TextStyle::Body
.resolve(style)
.size
Expand Down Expand Up @@ -452,7 +463,7 @@ impl ArrayTable {
if Self::is_filterable(column) {
let values = ui.memory_mut(|mem| {
let cache = mem.caches.cache::<ColumnFilterCache>();

cache.get((column, &self.nodes, &self.parent_pointer))
});
if !values.is_empty() {
Expand Down
54 changes: 46 additions & 8 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ impl MyApp {
}
}

fn goto_next_occurrence(table: &mut ArrayTable) -> bool {
fn goto_next_matching_row_occurrence(table: &mut ArrayTable) -> bool {
if table.matching_rows.len() == 0 {
return false;
}
Expand All @@ -249,6 +249,19 @@ impl MyApp {
table.changed_matching_row_selected = true;
true
}

fn goto_next_matching_column_occurrence(table: &mut ArrayTable) -> bool {
if table.matching_columns.len() == 0 {
return false;
}
if table.matching_column_selected == table.matching_columns.len() - 1 {
table.matching_column_selected = 0;
} else {
table.matching_column_selected += 1;
}
table.changed_matching_column_selected = true;
true
}
}

fn set_open(open: &mut BTreeSet<String>, key: &'static str, is_open: bool) {
Expand Down Expand Up @@ -308,14 +321,34 @@ impl eframe::App for MyApp {
}
if let Some(ref mut table) = self.table {
ui.separator();
let slider_response = ui.add(
let change_depth_slider_response = ui.add(
egui::Slider::new(&mut self.depth, self.min_depth..=self.max_depth).text("Depth"),
);
ui.add(Separator::default().vertical());
let scroll_to_column_response = ui.allocate_ui(Vec2::new(180.0, ui.spacing().interact_size.y), |ui| {
ui.add(Label::new("Scroll to column: ").wrap(false));
let text_edit = TextEdit::singleline(&mut table.scroll_to_column).hint_text("named");
ui.add(text_edit)
ui.horizontal(|ui| {
ui.add(Label::new("Scroll to column: ").wrap(false));
let text_edit = TextEdit::singleline(&mut table.scroll_to_column).hint_text("named");
let response = ui.add(text_edit);
if !table.matching_columns.is_empty() {
let response_prev = icon::button(ui, CHEVRON_UP, Some("Previous occurrence"), None);
let response_next = icon::button(ui, CHEVRON_DOWN, Some("Next occurrence"), None);
ui.label(RichText::new(format!("{}/{}", table.matching_column_selected + 1, table.matching_columns.len())));

if response_prev.clicked() {
if table.matching_column_selected == 0 {
table.matching_column_selected = table.matching_columns.len() - 1;
} else {
table.matching_column_selected -= 1;
}
table.changed_matching_column_selected = true;
}
if response_next.clicked() {
Self::goto_next_matching_column_occurrence(table);
}
}
response
}).inner
}).inner;

ui.add(Separator::default().vertical());
Expand Down Expand Up @@ -347,7 +380,7 @@ impl eframe::App for MyApp {
table.changed_matching_row_selected = true;
}
if response_next.clicked() {
Self::goto_next_occurrence(table);
Self::goto_next_matching_row_occurrence(table);
}
}
(scroll_to_row_mode_response, scroll_to_row_response)
Expand All @@ -358,21 +391,26 @@ impl eframe::App for MyApp {
// interaction handling
if scroll_to_column_response.changed() {
table.changed_scroll_to_column_value = true;
} else if scroll_to_column_response.lost_focus() && ctx.input(|i| i.key_pressed(Key::Enter)) {
if Self::goto_next_matching_column_occurrence(table) {
scroll_to_column_response.request_focus();
}
}
if scroll_to_row_response.changed() {
table.changed_scroll_to_row_value = Some(Instant::now());
if table.scroll_to_row.is_empty() {
table.reset_search();
}
} else if scroll_to_row_response.lost_focus() && ctx.input(|i| i.key_pressed(Key::Enter)) {
if Self::goto_next_occurrence(table) {
if Self::goto_next_matching_row_occurrence(table) {
scroll_to_row_response.request_focus();
}
}
if scroll_to_row_mode_response.inner.is_some() && scroll_to_row_mode_response.inner.unwrap() {
table.reset_search();
}
if slider_response.changed() {
if change_depth_slider_response.changed() {
table.changed_scroll_to_column_value = true;
if let Some(new_max_depth) = table.update_max_depth(self.depth) {
self.max_depth = new_max_depth as u8;
}
Expand Down

0 comments on commit aa5e608

Please sign in to comment.