diff --git a/ui/src/components/contacts.rs b/ui/src/components/contacts.rs index 1d91e878..4b50ae56 100644 --- a/ui/src/components/contacts.rs +++ b/ui/src/components/contacts.rs @@ -21,6 +21,10 @@ use super::*; +mod contact_list; + +pub use self::contact_list::*; + macro_rules! write_field { ($title:expr, $value:expr, $target_grid:expr, $fg_color:expr, $bg_color:expr, $width:expr, $y:expr) => {{ let (x, y) = write_string_to_grid( @@ -43,9 +47,19 @@ macro_rules! write_field { }} } +#[derive(Debug)] +enum ViewMode { + ReadOnly, + Read, + Edit, + New, +} + #[derive(Debug)] pub struct ContactManager { + id: Uuid, pub card: Card, + mode: ViewMode, content: CellBuffer, dirty: bool, initialized: bool, @@ -54,7 +68,9 @@ pub struct ContactManager { impl Default for ContactManager { fn default() -> Self { ContactManager { + id: Uuid::nil(), card: Card::new(), + mode: ViewMode::Read, content: CellBuffer::new(200, 100, Cell::with_char(' ')), dirty: true, initialized: false, @@ -133,6 +149,16 @@ impl Component for ContactManager { } fn process_event(&mut self, event: &UIEvent, context: &mut Context) -> bool { + match event.event_type { + UIEventType::Input(Key::Char('\n')) => { + context.replies.push_back(UIEvent { + id: 0, + event_type: UIEventType::EntityKill(self.id), + }); + return true; + }, + _ => {}, + } false } @@ -145,6 +171,7 @@ impl Component for ContactManager { self.initialized = false; } - fn kill(&mut self, uuid: Uuid) { + fn set_id(&mut self, uuid: Uuid) { + self.id = uuid; } } diff --git a/ui/src/components/contacts/contact_list.rs b/ui/src/components/contacts/contact_list.rs new file mode 100644 index 00000000..73f8e743 --- /dev/null +++ b/ui/src/components/contacts/contact_list.rs @@ -0,0 +1,146 @@ +use super::*; + +const MAX_COLS: usize = 500; + +#[derive(Debug, PartialEq)] +enum ViewMode { + List, + View(Uuid), +} + +#[derive(Debug)] +pub struct ContactList { + cursor_pos: usize, + new_cursor_pos: usize, + account_pos: usize, + length: usize, + content: CellBuffer, + + uuid_positions: Vec, + + mode: ViewMode, + initialized: bool, + dirty: bool, + view: Option, +} + +impl Default for ContactList { + fn default() -> Self { + Self::new() + } +} + +impl fmt::Display for ContactList { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + write!(f, "contacts") + } +} + +impl ContactList { + pub fn new() -> Self { + let content = CellBuffer::new(0, 0, Cell::with_char(' ')); + ContactList { + cursor_pos: 0, + new_cursor_pos: 0, + length: 0, + account_pos: 0, + uuid_positions: Vec::new(), + mode: ViewMode::List, + content, + initialized: false, + dirty: true, + view: None, + } + } + + fn initialize(&mut self, context: &mut Context) { + let account = &mut context.accounts[self.account_pos]; + let book = &mut account.address_book; + self.content.resize(MAX_COLS, book.len(), Cell::with_char(' ')); + eprintln!("{:?}", book); + + self.uuid_positions.clear(); + if self.uuid_positions.capacity() < book.len() { + self.uuid_positions.reserve(book.len()); + } + + for (i, c) in book.values().enumerate() { + self.uuid_positions.push(*c.uuid()); + + write_string_to_grid( + c.email(), + &mut self.content, + Color::Default, + Color::Default, + ((0, i), (MAX_COLS - 1, book.len() - 1)), + false + ); + } + } +} + +impl Component for ContactList { + fn draw(&mut self, grid: &mut CellBuffer, area: Area, context: &mut Context) { + if !self.initialized { + self.initialize(context); + self.initialized = true; + } + + if let Some(mgr) = self.view.as_mut() { + mgr.draw(grid, area, context); + self.dirty = false; + return; + } + + if self.dirty { + clear_area(grid, area); + copy_area(grid, &self.content, area, ((0, 0), (MAX_COLS - 1, self.content.size().1 - 1))); + context.dirty_areas.push_back(area); + self.dirty = false; + } + } + fn process_event(&mut self, event: &UIEvent, context: &mut Context) -> bool { + if let Some(ref mut v) = self.view { + if v.process_event(event, context) { + return true; + } + } + match event.event_type { + UIEventType::Input(Key::Char('e')) => { + let account = &mut context.accounts[self.account_pos]; + let book = &mut account.address_book; + let card = book[&self.uuid_positions[self.cursor_pos]].clone(); + let mut manager = ContactManager::default(); + manager.card = card; + + + + let entity = Entity::from(Box::new(manager)); + + self.mode = ViewMode::View(*entity.uuid()); + self.view = Some(entity); + self.set_dirty(); + + return true; + }, + UIEventType::EntityKill(ref kill_id) if self.mode == ViewMode::View(*kill_id) => { + self.mode = ViewMode::List; + self.view.take(); + self.set_dirty(); + return true; + + }, + _ => {}, + } + false + } + fn is_dirty(&self) -> bool { + self.dirty + } + fn set_dirty(&mut self) { + if let Some(p) = self.view.as_mut() { + p.set_dirty(); + }; + self.dirty = true; + } +}