make ListSegments a ListView instead of SequenceView

This commit is contained in:
Michael Sippel 2025-01-07 16:34:58 +01:00
parent 278296cfbb
commit 0d073ba82e
Signed by: senvas
GPG key ID: F96CF119C34B64A6
4 changed files with 83 additions and 49 deletions

View file

@ -139,7 +139,8 @@ impl ListEditor {
.set_editor(editor.clone()) .set_editor(editor.clone())
.set_nav(editor.clone()) .set_nav(editor.clone())
.set_cmd(editor.clone()) .set_cmd(editor.clone())
.set_diag(e.get_edittree_seq() .set_diag(e.get_edittree_list()
.to_sequence()
.enumerate() .enumerate()
.map(|(idx, item_editor)| { .map(|(idx, item_editor)| {
let idx = *idx; let idx = *idx;
@ -201,10 +202,6 @@ impl ListEditor {
}) })
} }
pub fn get_edittree_seq(&self) -> OuterViewPort<dyn SequenceView<Item = EditTree>> {
self.get_edittree_list().to_sequence()
}
/* /*
pub fn get_data(&self) -> Arc<RwLock<ReprTree>> { pub fn get_data(&self) -> Arc<RwLock<ReprTree>> {
let data_view = self.get_data_port(); let data_view = self.get_data_port();

View file

@ -9,8 +9,7 @@ pub mod ctx;
pub use { pub use {
cursor::{ListCursor, ListCursorMode}, cursor::{ListCursor, ListCursorMode},
editor::ListEditor, editor::ListEditor,
segment::{ListSegment, ListSegmentSequence}, segment::{ListSegment, ListSegments},
cmd::ListCmd, cmd::ListCmd,
ctx::init_ctx ctx::init_ctx
}; };

View file

@ -4,7 +4,9 @@ use {
Observer, ObserverBroadcast, ObserverExt, OuterViewPort, View, ViewPort, Observer, ObserverBroadcast, ObserverExt, OuterViewPort, View, ViewPort,
singleton::*, singleton::*,
sequence::*, sequence::*,
list::*
}, },
buffer::vec::*,
projection::projection_helper::* projection::projection_helper::*
}, },
crate::{ crate::{
@ -15,6 +17,7 @@ use {
std::sync::RwLock, std::sync::RwLock,
}; };
#[derive(Clone)]
pub enum ListSegment { pub enum ListSegment {
InsertCursor, InsertCursor,
Item { Item {
@ -25,24 +28,22 @@ pub enum ListSegment {
/* todo: switch to ListView instead of SequenceView /* todo: switch to ListView instead of SequenceView
*/ */
pub struct ListSegmentSequence { pub struct ListSegments {
data: Arc<dyn SequenceView<Item = EditTree>>, data: Arc<dyn ListView<EditTree>>,
cursor: Arc<dyn SingletonView<Item = ListCursor>>, cursor: Arc<dyn SingletonView<Item = ListCursor>>,
cur_cursor: ListCursor, cur_cursor: ListCursor,
port: ViewPort<dyn SequenceView<Item = ListSegment>>, port: ViewPort<dyn ListView<ListSegment>>,
cast: Arc<RwLock<ObserverBroadcast<dyn SequenceView<Item = ListSegment>>>>, cast: Arc<RwLock<ObserverBroadcast<dyn ListView<ListSegment>>>>,
proj_helper: ProjectionHelper<usize, Self>, proj_helper: ProjectionHelper<usize, Self>,
} }
impl View for ListSegmentSequence { impl View for ListSegments {
type Msg = usize; type Msg = ListDiff<ListSegment>;
} }
impl SequenceView for ListSegmentSequence { impl ListView<ListSegment> for ListSegments {
type Item = ListSegment;
fn len(&self) -> Option<usize> { fn len(&self) -> Option<usize> {
let l = self.data.len()?; let l = self.data.len()?;
match self.cur_cursor.mode { match self.cur_cursor.mode {
@ -53,7 +54,7 @@ impl SequenceView for ListSegmentSequence {
} }
} }
fn get(&self, idx: &usize) -> Option<Self::Item> { fn get(&self, idx: &usize) -> Option<ListSegment> {
let c = self.cursor.get(); let c = self.cursor.get();
Some(if let Some(cur) = c.idx { Some(if let Some(cur) = c.idx {
match c.mode { match c.mode {
@ -88,41 +89,82 @@ impl SequenceView for ListSegmentSequence {
} }
} }
impl ListSegmentSequence { impl ListSegments {
pub fn new( pub fn new(
cursor_port: OuterViewPort<dyn SingletonView<Item = ListCursor>>, cursor_port: OuterViewPort<dyn SingletonView<Item = ListCursor>>,
data_port: OuterViewPort<dyn SequenceView<Item = EditTree>>, data_port: OuterViewPort<dyn ListView<EditTree>>,
) -> Arc<RwLock<Self>> { ) -> Arc<RwLock<Self>> {
let out_port = ViewPort::new(); let out_port = ViewPort::new();
let mut proj_helper = ProjectionHelper::new(out_port.update_hooks.clone()); let mut proj_helper = ProjectionHelper::new(out_port.update_hooks.clone());
let proj = Arc::new(RwLock::new(ListSegmentSequence { let proj = Arc::new(RwLock::new(ListSegments {
cur_cursor: cursor_port.get_view().get(), cur_cursor: cursor_port.get_view().get(),
port: out_port.clone(), port: out_port.clone(),
cursor: proj_helper.new_singleton_arg(0, cursor_port, |s: &mut Self, _msg| { cursor: proj_helper.new_singleton_arg(0, cursor_port, |s: &mut Self, _msg| {
let _old_cursor = s.cur_cursor; let old_cursor = s.cur_cursor;
s.cur_cursor = s.cursor.get(); let new_cursor = s.cursor.get();
s.cur_cursor = new_cursor;
// todo: optimize if old_cursor.mode == ListCursorMode::Insert {
s.cast.notify_each(0..=s.data.len().unwrap_or(0) + 1); if let Some(idx) = old_cursor.idx {
s.cast.notify(&ListDiff::Remove(idx as usize));
}
}
if new_cursor.mode == ListCursorMode::Insert {
if let Some(idx) = new_cursor.idx {
s.cast.notify(&ListDiff::Insert { idx: idx as usize, val: ListSegment::InsertCursor });
}
}
}), }),
data: proj_helper.new_sequence_arg(1, data_port, |s: &mut Self, idx| { data: proj_helper.new_list_arg(1, data_port, |s: &mut Self, diff| {
let mut target_idx = match diff {
ListDiff::Clear => 0,
ListDiff::Remove(idx) => *idx,
ListDiff::Insert { idx, val } => *idx,
ListDiff::Update { idx, val } => *idx
};
let mut cur_dist = 0;
if let Some(cur_idx) = s.cur_cursor.idx { if let Some(cur_idx) = s.cur_cursor.idx {
match s.cur_cursor.mode { if s.cur_cursor.mode == ListCursorMode::Insert {
ListCursorMode::Insert => { if cur_idx < target_idx as isize {
if *idx < cur_idx as usize { target_idx += 1;
s.cast.notify(idx);
} else {
s.cast.notify(&(*idx + 1));
}
}
_ => {
s.cast.notify(idx);
} }
} }
} else {
s.cast.notify(idx); cur_dist = target_idx as isize - cur_idx;
}
match diff {
ListDiff::Clear => {
s.cast.notify(&ListDiff::Clear);
if s.cur_cursor.mode == ListCursorMode::Insert {
s.cast.notify(&ListDiff::Insert { idx: 0, val: ListSegment::InsertCursor });
}
}
ListDiff::Remove(idx) => {
s.cast.notify(&ListDiff::Remove(target_idx));
}
ListDiff::Insert { idx, val } => {
s.cast.notify(&ListDiff::Insert {
idx: target_idx,
val: ListSegment::Item {
editor: val.clone(),
cur_dist
}
});
}
ListDiff::Update { idx, val } => {
s.cast.notify(&ListDiff::Update {
idx: target_idx,
val: ListSegment::Item {
editor: val.clone(),
cur_dist
}
})
}
} }
}), }),
cast: out_port.inner().get_broadcast(), cast: out_port.inner().get_broadcast(),
@ -131,11 +173,10 @@ impl ListSegmentSequence {
proj.write().unwrap().proj_helper.set_proj(&proj); proj.write().unwrap().proj_helper.set_proj(&proj);
out_port.inner().set_view(Some(proj.clone())); out_port.inner().set_view(Some(proj.clone()));
proj proj
} }
pub fn get_view(&self) -> OuterViewPort<dyn SequenceView<Item = ListSegment>> { pub fn get_view(&self) -> OuterViewPort<dyn ListView<ListSegment>> {
self.port.outer() self.port.outer()
} }
} }

View file

@ -1,6 +1,6 @@
use { use {
r3vi::{ r3vi::{
view::{ViewPort, OuterViewPort, sequence::*}, view::{ViewPort, OuterViewPort, sequence::*, list::*},
projection::decorate_sequence::*, projection::decorate_sequence::*,
}, },
nested::{ nested::{
@ -67,19 +67,19 @@ impl PTYListStyle {
} }
} }
pub fn get_seg_seq_view(&self, editor: &ListEditor) -> OuterViewPort<dyn SequenceView<Item = OuterViewPort<dyn TerminalView>>> { pub fn get_seg_view_list(&self, editor: &ListEditor) -> OuterViewPort<dyn ListView<OuterViewPort<dyn TerminalView>>> {
let seg_seq = ListSegmentSequence::new( let seg_seq = ListSegments::new(
editor.get_cursor_port(), editor.get_cursor_port(),
editor.get_edittree_seq() editor.get_edittree_list()
); );
let se = seg_seq.read().unwrap(); let se = seg_seq.read().unwrap();
se.get_view().map(move |segment| segment.display_view()) se.get_view().map(move |segment| segment.display_view())
} }
pub fn pty_view(&self, editor: &ListEditor) -> OuterViewPort<dyn TerminalView> { pub fn pty_view(&self, editor: &ListEditor) -> OuterViewPort<dyn TerminalView> {
let seg_seq = ListSegmentSequence::new( let seg_seq = ListSegments::new(
editor.get_cursor_port(), editor.get_cursor_port(),
editor.get_edittree_seq() editor.get_edittree_list()
); );
let seg_seq0 = seg_seq.read().unwrap(); let seg_seq0 = seg_seq.read().unwrap();
@ -88,6 +88,7 @@ impl PTYListStyle {
seg_seq seg_seq
.map(move |segment| segment.display_view()) .map(move |segment| segment.display_view())
.to_sequence()
.separate(make_label(&self.style.1)) .separate(make_label(&self.style.1))
.wrap(make_label(&self.style.0), make_label(&self.style.2)) .wrap(make_label(&self.style.0), make_label(&self.style.2))
.to_grid_horizontal() .to_grid_horizontal()
@ -163,10 +164,6 @@ impl PTYListController {
node.ctrl.close_char.set(close_char); node.ctrl.close_char.set(close_char);
} }
pub fn get_data_port(&self) -> OuterViewPort<dyn SequenceView<Item = EditTree>> {
self.editor.read().unwrap().get_edittree_seq()
}
pub fn clear(&mut self) { pub fn clear(&mut self) {
self.editor.write().unwrap().clear(); self.editor.write().unwrap().clear();
} }