From 0acf0e6df56a43bb19476b69d8ffbbc7b500654d Mon Sep 17 00:00:00 2001 From: Vitaly Date: Mon, 5 Apr 2021 17:57:53 +0300 Subject: [PATCH 1/4] WIP - immutablity issues --- src/selection.rs | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/selection.rs b/src/selection.rs index 5b558918..dcffa7ae 100644 --- a/src/selection.rs +++ b/src/selection.rs @@ -56,6 +56,7 @@ pub struct Selection { // To avoid remember all items, we'll track the latest run_num and index. latest_select_run_num: u32, pre_selected_watermark: usize, + to_run_auto_line_select: bool, selector: Option>, } @@ -77,6 +78,7 @@ impl Selection { theme: Arc::new(*DEFAULT_THEME), latest_select_run_num: 0, pre_selected_watermark: 0, + to_run_auto_line_select: false, selector: None, } } @@ -136,6 +138,7 @@ impl Selection { if self.items.len() >= self.pre_selected_watermark { self.pre_select(&items); + self.to_run_auto_line_select = true; } self.items.append(items); @@ -554,6 +557,13 @@ impl Draw for Selection { let _ = self.draw_item(canvas, line_no, &item, line_cursor == self.line_cursor); } + if self.to_run_auto_line_select { + debug!("Running! to_run_auto_line_select"); + // let s = self as *mut Selector; + // s.to_run_auto_line_select = false; + } + + Ok(()) } } From 05ee0a4ccb6d16269c47869b38fbe68b755d33ab Mon Sep 17 00:00:00 2001 From: Vitaly Date: Mon, 5 Apr 2021 18:20:26 +0300 Subject: [PATCH 2/4] using EvHeartBeat to run code --- src/selection.rs | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/src/selection.rs b/src/selection.rs index dcffa7ae..bc3e6e7f 100644 --- a/src/selection.rs +++ b/src/selection.rs @@ -354,6 +354,15 @@ impl EventHandler for Selection { fn handle(&mut self, event: &Event) -> UpdateScreen { use crate::event::Event::*; match event { + Event::EvHeartBeat => { + debug!("HEARTBEAT!!!!!!!!!"); + if self.to_run_auto_line_select { + debug!("Running! to_run_auto_line_select!!!!!!!"); + self.act_move_line_cursor(1); + self.to_run_auto_line_select = false; + } + } + EvActUp(diff) => { self.act_move_line_cursor(*diff); } @@ -557,13 +566,6 @@ impl Draw for Selection { let _ = self.draw_item(canvas, line_no, &item, line_cursor == self.line_cursor); } - if self.to_run_auto_line_select { - debug!("Running! to_run_auto_line_select"); - // let s = self as *mut Selector; - // s.to_run_auto_line_select = false; - } - - Ok(()) } } From 2e5be8f1f35d1708215857fd5763fe7c6438b9cd Mon Sep 17 00:00:00 2001 From: Vitaly Date: Tue, 6 Apr 2021 02:32:00 +0300 Subject: [PATCH 3/4] POC - works but relies on heartbeat (to be able to mutate selected item) and on self.height to know when drawing occured --- src/selection.rs | 38 +++++++++++++++++++++++++++++++++----- 1 file changed, 33 insertions(+), 5 deletions(-) diff --git a/src/selection.rs b/src/selection.rs index bc3e6e7f..6e8e6e5a 100644 --- a/src/selection.rs +++ b/src/selection.rs @@ -189,6 +189,7 @@ impl Selection { let item_len = self.items.len() as i32; let height = self.height.load(Ordering::Relaxed) as i32; + debug!("VITALY: act_move_line_cursor - height: {}", height); line_cursor += diff; if line_cursor >= height { @@ -355,11 +356,35 @@ impl EventHandler for Selection { use crate::event::Event::*; match event { Event::EvHeartBeat => { - debug!("HEARTBEAT!!!!!!!!!"); - if self.to_run_auto_line_select { - debug!("Running! to_run_auto_line_select!!!!!!!"); - self.act_move_line_cursor(1); - self.to_run_auto_line_select = false; + debug!("Vitaly: HEARTBEAT!!!!!!!!!"); + if self.to_run_auto_line_select && (self.height.load(Ordering::Relaxed) as i32) > 0 { + debug!("Vitaly: Running! to_run_auto_line_select!!!!!!!"); + + if self.selector.is_none() == false { + debug!("Vitaly: LOOKING FOR SELECTOR"); + debug!("Vitaly: EXTRA INFO - items.len(): {}", self.items.len()); + + let mut diff = 0; + for (i, item) in self.items.iter().enumerate() { + if self + .selector + .as_ref() + .map(|s| s.should_select(item.item_idx as usize, item.item.as_ref())) + .unwrap_or(false) + { + debug!("Vitaly: FOUND ITEM: {} - FOUND ITEM INDEX: {} - FOUND I: {}", item.item.text(), item.item_idx, i); + debug!("Vitaly: EXTRA INFO - item_cursor: {} - line_cursor: {}", self.item_cursor, self.line_cursor); + diff = -((self.line_cursor as i32)-(i as i32)); + debug!("Vitaly: EXTRA INFO - item_cursor: {} - line_cursor: {}", self.item_cursor, self.line_cursor); + } + } + if diff != 0 { + self.act_move_line_cursor(diff); + } + } + + // VITALY: todo - enable + // self.to_run_auto_line_select = false; } } @@ -537,6 +562,8 @@ impl Selection { impl Draw for Selection { fn draw(&self, canvas: &mut dyn Canvas) -> DrawResult<()> { + debug!("VITALY: Draw Start"); + let (_screen_width, screen_height) = canvas.size()?; canvas.clear()?; @@ -565,6 +592,7 @@ impl Draw for Selection { let _ = self.draw_item(canvas, line_no, &item, line_cursor == self.line_cursor); } + debug!("VITALY: Draw End"); Ok(()) } From ee8370d55b7aff760c16d82cc176f7de77ec3296 Mon Sep 17 00:00:00 2001 From: Vitaly Date: Tue, 6 Apr 2021 09:50:33 +0300 Subject: [PATCH 4/4] cleanup - remove logging and adding comments --- src/selection.rs | 19 +++++-------------- 1 file changed, 5 insertions(+), 14 deletions(-) diff --git a/src/selection.rs b/src/selection.rs index 6e8e6e5a..9272de37 100644 --- a/src/selection.rs +++ b/src/selection.rs @@ -189,7 +189,6 @@ impl Selection { let item_len = self.items.len() as i32; let height = self.height.load(Ordering::Relaxed) as i32; - debug!("VITALY: act_move_line_cursor - height: {}", height); line_cursor += diff; if line_cursor >= height { @@ -356,26 +355,20 @@ impl EventHandler for Selection { use crate::event::Event::*; match event { Event::EvHeartBeat => { - debug!("Vitaly: HEARTBEAT!!!!!!!!!"); + // self is immutable in the draw function, so we're using the heartbeat to check whether the selection can be moved if self.to_run_auto_line_select && (self.height.load(Ordering::Relaxed) as i32) > 0 { - debug!("Vitaly: Running! to_run_auto_line_select!!!!!!!"); - if self.selector.is_none() == false { - debug!("Vitaly: LOOKING FOR SELECTOR"); - debug!("Vitaly: EXTRA INFO - items.len(): {}", self.items.len()); let mut diff = 0; for (i, item) in self.items.iter().enumerate() { + // use the same logic as in pre-select - reuse the existing arguments if self .selector .as_ref() .map(|s| s.should_select(item.item_idx as usize, item.item.as_ref())) .unwrap_or(false) { - debug!("Vitaly: FOUND ITEM: {} - FOUND ITEM INDEX: {} - FOUND I: {}", item.item.text(), item.item_idx, i); - debug!("Vitaly: EXTRA INFO - item_cursor: {} - line_cursor: {}", self.item_cursor, self.line_cursor); diff = -((self.line_cursor as i32)-(i as i32)); - debug!("Vitaly: EXTRA INFO - item_cursor: {} - line_cursor: {}", self.item_cursor, self.line_cursor); } } if diff != 0 { @@ -383,8 +376,9 @@ impl EventHandler for Selection { } } - // VITALY: todo - enable - // self.to_run_auto_line_select = false; + // pre-select is tried everytime the pre-select watermark changes + // TODO: if `act_move_line_cursor` is every called, don't allow pre-select to move again + self.to_run_auto_line_select = false; } } @@ -562,8 +556,6 @@ impl Selection { impl Draw for Selection { fn draw(&self, canvas: &mut dyn Canvas) -> DrawResult<()> { - debug!("VITALY: Draw Start"); - let (_screen_width, screen_height) = canvas.size()?; canvas.clear()?; @@ -592,7 +584,6 @@ impl Draw for Selection { let _ = self.draw_item(canvas, line_no, &item, line_cursor == self.line_cursor); } - debug!("VITALY: Draw End"); Ok(()) }