diff --git a/src/issue.rs b/src/issue.rs index c07a0cbb..96291058 100644 --- a/src/issue.rs +++ b/src/issue.rs @@ -24,7 +24,7 @@ use page::{HomeView, PatchView}; use self::page::{IssuePage, PageStack}; #[derive(Debug, Eq, PartialEq, Clone, Hash)] -pub enum HomeCid { +pub enum ListCid { Header, Dashboard, IssueBrowser, diff --git a/src/issue/app.rs b/src/issue/app.rs index 481a50d5..69c0b6e4 100644 --- a/src/issue/app.rs +++ b/src/issue/app.rs @@ -1,43 +1,31 @@ mod event; mod page; -mod subscription; +mod ui; use anyhow::Result; use radicle::cob::issue::IssueId; -use radicle::cob::patch::PatchId; use radicle::identity::{Id, Project}; use radicle::prelude::Signer; use radicle::profile::Profile; +use radicle_tui::ui::subscription; use radicle_tui::ui::widget; use tuirealm::application::PollStrategy; use tuirealm::{Application, Frame, NoUserEvent, Sub, SubClause}; +use radicle_tui::cob; use radicle_tui::ui::context::Context; use radicle_tui::ui::theme::{self, Theme}; +use radicle_tui::PageStack; use radicle_tui::Tui; -use radicle_tui::{cob, ui}; -use page::{HomeView, PatchView}; - -use self::page::{IssuePage, PageStack}; +use page::{IssuePage, ListPage}; #[derive(Debug, Eq, PartialEq, Clone, Hash)] -pub enum HomeCid { +pub enum ListCid { Header, - Dashboard, IssueBrowser, - PatchBrowser, - Context, - Shortcuts, -} - -#[derive(Debug, Eq, PartialEq, Clone, Hash)] -pub enum PatchCid { - Header, - Activity, - Files, Context, Shortcuts, } @@ -53,21 +41,15 @@ pub enum IssueCid { } /// All component ids known to this application. -#[derive(Debug, Eq, PartialEq, Clone, Hash)] +#[derive(Debug, Default, Eq, PartialEq, Clone, Hash)] pub enum Cid { - Home(HomeCid), + List(ListCid), Issue(IssueCid), - Patch(PatchCid), + #[default] GlobalListener, Popup, } -/// Messages handled by this application. -#[derive(Clone, Debug, Eq, PartialEq)] -pub enum HomeMessage { - RefreshIssues(Option), -} - #[derive(Clone, Debug, Eq, PartialEq)] pub enum IssueCobMessage { Create { @@ -85,17 +67,12 @@ pub enum IssueMessage { Focus(IssueCid), Created(IssueId), Cob(IssueCobMessage), + Reload(Option), OpenForm, HideForm, Leave(Option), } -#[derive(Clone, Debug, Eq, PartialEq)] -pub enum PatchMessage { - Show(PatchId), - Leave, -} - #[derive(Clone, Debug, Eq, PartialEq)] pub enum PopupMessage { Info(String), @@ -104,14 +81,13 @@ pub enum PopupMessage { Hide, } -#[derive(Clone, Debug, Eq, PartialEq)] +#[derive(Clone, Debug, Default, Eq, PartialEq)] pub enum Message { - Home(HomeMessage), Issue(IssueMessage), - Patch(PatchMessage), NavigationChanged(u16), FormSubmitted(String), Popup(PopupMessage), + #[default] Tick, Quit, Batch(Vec), @@ -120,7 +96,7 @@ pub enum Message { #[allow(dead_code)] pub struct App { context: Context, - pages: PageStack, + pages: PageStack, theme: Theme, quit: bool, } @@ -137,37 +113,17 @@ impl App { } } - fn view_home( + fn view_list( &mut self, app: &mut Application, theme: &Theme, ) -> Result<()> { - let home = Box::new(HomeView::new(theme.clone())); - self.pages.push(home, app, &self.context, theme)?; + let list = Box::new(ListPage::new(theme.clone())); + self.pages.push(list, app, &self.context, theme)?; Ok(()) } - fn view_patch( - &mut self, - app: &mut Application, - id: PatchId, - theme: &Theme, - ) -> Result<()> { - let repo = self.context.repository(); - - if let Some(patch) = cob::patch::find(repo, &id)? { - let view = Box::new(PatchView::new(theme.clone(), (id, patch))); - self.pages.push(view, app, &self.context, theme)?; - - Ok(()) - } else { - Err(anyhow::anyhow!( - "Could not mount 'page::PatchView'. Patch not found." - )) - } - } - fn view_issue( &mut self, app: &mut Application, @@ -244,15 +200,7 @@ impl App { } Message::Issue(IssueMessage::Leave(id)) => { self.pages.pop(app)?; - Ok(Some(Message::Home(HomeMessage::RefreshIssues(id)))) - } - Message::Patch(PatchMessage::Show(id)) => { - self.view_patch(app, id, &theme)?; - Ok(None) - } - Message::Patch(PatchMessage::Leave) => { - self.pages.pop(app)?; - Ok(None) + Ok(Some(Message::Issue(IssueMessage::Reload(id)))) } Message::Popup(PopupMessage::Info(info)) => { self.show_info_popup(app, &theme, &info)?; @@ -353,10 +301,10 @@ impl App { impl Tui for App { fn init(&mut self, app: &mut Application) -> Result<()> { - self.view_home(app, &self.theme.clone())?; + self.view_list(app, &self.theme.clone())?; // Add global key listener and subscribe to key events - let global = ui::widget::common::global_listener().to_boxed(); + let global = widget::common::global_listener().to_boxed(); app.mount( Cid::GlobalListener, global, diff --git a/src/issue/app/event.rs b/src/issue/app/event.rs index 0fdea58a..59932034 100644 --- a/src/issue/app/event.rs +++ b/src/issue/app/event.rs @@ -9,12 +9,11 @@ use radicle_tui::ui::widget::common::container::{ use radicle_tui::ui::widget::common::context::{ContextBar, Shortcuts}; use radicle_tui::ui::widget::common::form::Form; use radicle_tui::ui::widget::common::list::PropertyList; -use radicle_tui::ui::widget::home::{Dashboard, IssueBrowser, PatchBrowser}; -use radicle_tui::ui::widget::{issue, patch}; use radicle_tui::ui::widget::Widget; -use super::{IssueCid, IssueMessage, Message, PatchMessage, PopupMessage}; +use super::ui; +use super::{IssueCid, IssueMessage, Message, PopupMessage}; /// Since the framework does not know the type of messages that are being /// passed around in the app, the following handlers need to be implemented for @@ -49,7 +48,7 @@ impl tuirealm::Component for Widget { } } -impl tuirealm::Component for Widget { +impl tuirealm::Component for Widget { fn on(&mut self, event: Event) -> Option { match event { Event::Keyboard(KeyEvent { code: Key::Esc, .. }) => match self.state() { @@ -103,7 +102,7 @@ impl tuirealm::Component for Widget { } } -impl tuirealm::Component for Widget { +impl tuirealm::Component for Widget { fn on(&mut self, event: Event) -> Option { match event { Event::Keyboard(KeyEvent { code: Key::Up, .. }) @@ -233,45 +232,7 @@ impl tuirealm::Component for Widget
{ } } -impl tuirealm::Component for Widget { - fn on(&mut self, event: Event) -> Option { - match event { - Event::Keyboard(KeyEvent { code: Key::Up, .. }) - | Event::Keyboard(KeyEvent { - code: Key::Char('k'), - .. - }) => { - self.perform(Cmd::Move(MoveDirection::Up)); - Some(Message::Tick) - } - Event::Keyboard(KeyEvent { - code: Key::Down, .. - }) - | Event::Keyboard(KeyEvent { - code: Key::Char('j'), - .. - }) => { - self.perform(Cmd::Move(MoveDirection::Down)); - Some(Message::Tick) - } - Event::Keyboard(KeyEvent { - code: Key::Enter, .. - }) => { - let result = self.perform(Cmd::Submit); - match result { - CmdResult::Submit(State::One(StateValue::Usize(selected))) => { - let item = self.items().get(selected)?; - Some(Message::Patch(PatchMessage::Show(item.id().to_owned()))) - } - _ => None, - } - } - _ => None, - } - } -} - -impl tuirealm::Component for Widget { +impl tuirealm::Component for Widget { fn on(&mut self, event: Event) -> Option { let mut submit = || -> Option { let result = self.perform(Cmd::Submit); @@ -328,34 +289,6 @@ impl tuirealm::Component for Widget { } } -impl tuirealm::Component for Widget { - fn on(&mut self, _event: Event) -> Option { - None - } -} - -impl tuirealm::Component for Widget { - fn on(&mut self, event: Event) -> Option { - match event { - Event::Keyboard(KeyEvent { code: Key::Esc, .. }) => { - Some(Message::Patch(PatchMessage::Leave)) - } - _ => None, - } - } -} - -impl tuirealm::Component for Widget { - fn on(&mut self, event: Event) -> Option { - match event { - Event::Keyboard(KeyEvent { code: Key::Esc, .. }) => { - Some(Message::Patch(PatchMessage::Leave)) - } - _ => None, - } - } -} - impl tuirealm::Component for Widget { fn on(&mut self, event: Event) -> Option { match event { diff --git a/src/issue/app/page.rs b/src/issue/app/page.rs index afdcad68..aa3a1130 100644 --- a/src/issue/app/page.rs +++ b/src/issue/app/page.rs @@ -3,8 +3,6 @@ use std::collections::HashMap; use anyhow::Result; use radicle::cob::issue::{Issue, IssueId}; -use radicle::cob::patch::{Patch, PatchId}; - use radicle_tui::cob; use radicle_tui::ui::widget::common::context::{Progress, Shortcuts}; use tuirealm::{AttrValue, Attribute, Frame, NoUserEvent, State, StateValue, Sub, SubClause}; @@ -13,66 +11,28 @@ use radicle_tui::ui::context::Context; use radicle_tui::ui::layout; use radicle_tui::ui::theme::Theme; use radicle_tui::ui::widget::{self, Widget}; +use radicle_tui::ViewPage; use super::{ - subscription, Application, Cid, HomeCid, HomeMessage, IssueCid, IssueCobMessage, IssueMessage, - Message, PatchCid, PopupMessage, + Application, Cid, IssueCid, IssueCobMessage, IssueMessage, ListCid, Message, PopupMessage, }; -/// `tuirealm`'s event and prop system is designed to work with flat component hierarchies. -/// Building deep nested component hierarchies would need a lot more additional effort to -/// properly pass events and props down these hierarchies. This makes it hard to implement -/// full app views (home, patch details etc) as components. -/// -/// View pages take into account these flat component hierarchies, and provide -/// switchable sets of components. -pub trait ViewPage { - /// Will be called whenever a view page is pushed onto the page stack. Should create and mount all widgets. - fn mount( - &self, - app: &mut Application, - context: &Context, - theme: &Theme, - ) -> Result<()>; - - /// Will be called whenever a view page is popped from the page stack. Should unmount all widgets. - fn unmount(&self, app: &mut Application) -> Result<()>; - - /// Will be called whenever a view page is on top of the stack and can be used to update its internal - /// state depending on the message passed. - fn update( - &mut self, - app: &mut Application, - context: &Context, - theme: &Theme, - message: Message, - ) -> Result>; - - /// Will be called whenever a view page is on top of the page stack and needs to be rendered. - fn view(&mut self, app: &mut Application, frame: &mut Frame); - - /// Will be called whenever this view page is pushed to the stack, or it is on top of the stack again - /// after another view page was popped from the stack. - fn subscribe(&self, app: &mut Application) -> Result<()>; - - /// Will be called whenever this view page is on top of the stack and another view page is pushed - /// to the stack, or if this is popped from the stack. - fn unsubscribe(&self, app: &mut Application) -> Result<()>; -} +use super::subscription; +use super::ui; /// /// Home /// -pub struct HomeView { - active_component: HomeCid, - shortcuts: HashMap>, +pub struct ListPage { + active_component: ListCid, + shortcuts: HashMap>, } -impl HomeView { +impl ListPage { pub fn new(theme: Theme) -> Self { let shortcuts = Self::build_shortcuts(&theme); - HomeView { - active_component: HomeCid::Dashboard, + Self { + active_component: ListCid::IssueBrowser, shortcuts, } } @@ -80,54 +40,30 @@ impl HomeView { fn activate( &mut self, app: &mut Application, - cid: HomeCid, + cid: ListCid, ) -> Result<()> { self.active_component = cid; - let cid = Cid::Home(self.active_component.clone()); + let cid = Cid::List(self.active_component.clone()); app.active(&cid)?; app.attr(&cid, Attribute::Focus, AttrValue::Flag(true))?; Ok(()) } - fn build_shortcuts(theme: &Theme) -> HashMap> { - [ - ( - HomeCid::Dashboard, - widget::common::shortcuts( - theme, - vec![ - widget::common::shortcut(theme, "tab", "section"), - widget::common::shortcut(theme, "q", "quit"), - ], - ), + fn build_shortcuts(theme: &Theme) -> HashMap> { + [( + ListCid::IssueBrowser, + widget::common::shortcuts( + theme, + vec![ + widget::common::shortcut(theme, "tab", "section"), + widget::common::shortcut(theme, "↑/↓", "navigate"), + widget::common::shortcut(theme, "enter", "show"), + widget::common::shortcut(theme, "o", "open"), + widget::common::shortcut(theme, "q", "quit"), + ], ), - ( - HomeCid::IssueBrowser, - widget::common::shortcuts( - theme, - vec![ - widget::common::shortcut(theme, "tab", "section"), - widget::common::shortcut(theme, "↑/↓", "navigate"), - widget::common::shortcut(theme, "enter", "show"), - widget::common::shortcut(theme, "o", "open"), - widget::common::shortcut(theme, "q", "quit"), - ], - ), - ), - ( - HomeCid::PatchBrowser, - widget::common::shortcuts( - theme, - vec![ - widget::common::shortcut(theme, "tab", "section"), - widget::common::shortcut(theme, "↑/↓", "navigate"), - widget::common::shortcut(theme, "enter", "show"), - widget::common::shortcut(theme, "q", "quit"), - ], - ), - ), - ] + )] .iter() .cloned() .collect() @@ -138,37 +74,16 @@ impl HomeView { app: &mut Application, context: &Context, theme: &Theme, - cid: HomeCid, ) -> Result<()> { - let context = match cid { - HomeCid::IssueBrowser => { - let state = app.state(&Cid::Home(HomeCid::IssueBrowser))?; - let progress = match state { - State::Tup2((StateValue::Usize(step), StateValue::Usize(total))) => { - Progress::Step(step.saturating_add(1), total) - } - _ => Progress::None, - }; - let context = widget::issue::browse_context(context, theme, progress); - Some(context) + let state = app.state(&Cid::List(ListCid::IssueBrowser))?; + let progress = match state { + State::Tup2((StateValue::Usize(step), StateValue::Usize(total))) => { + Progress::Step(step.saturating_add(1), total) } - HomeCid::PatchBrowser => { - let state = app.state(&Cid::Home(HomeCid::PatchBrowser))?; - let progress = match state { - State::Tup2((StateValue::Usize(step), StateValue::Usize(total))) => { - Progress::Step(step.saturating_add(1), total) - } - _ => Progress::None, - }; - let context = widget::issue::browse_context(context, theme, progress); - Some(context) - } - _ => None, + _ => Progress::None, }; - - if let Some(context) = context { - app.remount(Cid::Home(HomeCid::Context), context.to_boxed(), vec![])?; - } + let context = ui::browse_context(context, theme, progress); + app.remount(Cid::List(ListCid::Context), context.to_boxed(), vec![])?; Ok(()) } @@ -176,11 +91,11 @@ impl HomeView { fn update_shortcuts( &self, app: &mut Application, - cid: HomeCid, + cid: ListCid, ) -> Result<()> { if let Some(shortcuts) = self.shortcuts.get(&cid) { app.remount( - Cid::Home(HomeCid::Shortcuts), + Cid::List(ListCid::Shortcuts), shortcuts.clone().to_boxed(), vec![], )?; @@ -189,41 +104,32 @@ impl HomeView { } } -impl ViewPage for HomeView { +impl ViewPage for ListPage { fn mount( &self, app: &mut Application, context: &Context, theme: &Theme, ) -> Result<()> { - let navigation = widget::home::navigation(theme); + let navigation = ui::list_navigation(theme); let header = widget::common::app_header(context, theme, Some(navigation)).to_boxed(); + let issue_browser = ui::issues(context, theme, None).to_boxed(); - let dashboard = widget::home::dashboard(context, theme).to_boxed(); - let issue_browser = widget::home::issues(context, theme, None).to_boxed(); - let patch_browser = widget::home::patches(context, theme, None).to_boxed(); + app.remount(Cid::List(ListCid::Header), header, vec![])?; + app.remount(Cid::List(ListCid::IssueBrowser), issue_browser, vec![])?; - app.remount(Cid::Home(HomeCid::Header), header, vec![])?; - - app.remount(Cid::Home(HomeCid::Dashboard), dashboard, vec![])?; - app.remount(Cid::Home(HomeCid::IssueBrowser), issue_browser, vec![])?; - app.remount(Cid::Home(HomeCid::PatchBrowser), patch_browser, vec![])?; - - let active_component = Cid::Home(self.active_component.clone()); - app.active(&active_component)?; + app.active(&Cid::List(self.active_component.clone()))?; self.update_shortcuts(app, self.active_component.clone())?; - self.update_context(app, context, theme, self.active_component.clone())?; + self.update_context(app, context, theme)?; Ok(()) } fn unmount(&self, app: &mut Application) -> Result<()> { - app.umount(&Cid::Home(HomeCid::Header))?; - app.umount(&Cid::Home(HomeCid::Dashboard))?; - app.umount(&Cid::Home(HomeCid::IssueBrowser))?; - app.umount(&Cid::Home(HomeCid::PatchBrowser))?; - app.umount(&Cid::Home(HomeCid::Context))?; - app.umount(&Cid::Home(HomeCid::Shortcuts))?; + app.umount(&Cid::List(ListCid::Header))?; + app.umount(&Cid::List(ListCid::IssueBrowser))?; + app.umount(&Cid::List(ListCid::Context))?; + app.umount(&Cid::List(ListCid::Shortcuts))?; Ok(()) } @@ -234,28 +140,19 @@ impl ViewPage for HomeView { theme: &Theme, message: Message, ) -> Result> { - match message { - Message::NavigationChanged(index) => { - self.activate(app, HomeCid::from(index as usize))?; - self.update_shortcuts(app, self.active_component.clone())?; - } - Message::Home(HomeMessage::RefreshIssues(id)) => { - let selected = match id { - Some(id) => { - cob::issue::find(context.repository(), &id)?.map(|issue| (id, issue)) - } - _ => None, - }; + if let Message::Issue(IssueMessage::Reload(id)) = message { + let selected = match id { + Some(id) => cob::issue::find(context.repository(), &id)?.map(|issue| (id, issue)), + _ => None, + }; - let issue_browser = widget::home::issues(context, theme, selected).to_boxed(); - app.remount(Cid::Home(HomeCid::IssueBrowser), issue_browser, vec![])?; + let issue_browser = ui::issues(context, theme, selected).to_boxed(); + app.remount(Cid::List(ListCid::IssueBrowser), issue_browser, vec![])?; - self.activate(app, HomeCid::IssueBrowser)?; - } - _ => {} + self.activate(app, ListCid::IssueBrowser)?; } - self.update_context(app, context, theme, self.active_component.clone())?; + self.update_context(app, context, theme)?; Ok(None) } @@ -265,23 +162,19 @@ impl ViewPage for HomeView { let shortcuts_h = 1u16; let layout = layout::default_page(area, shortcuts_h); - app.view(&Cid::Home(HomeCid::Header), frame, layout.navigation); + app.view(&Cid::List(ListCid::Header), frame, layout.navigation); app.view( - &Cid::Home(self.active_component.clone()), + &Cid::List(self.active_component.clone()), frame, layout.component, ); - if self.active_component != HomeCid::Dashboard { - app.view(&Cid::Home(HomeCid::Context), frame, layout.context); - } - - app.view(&Cid::Home(HomeCid::Shortcuts), frame, layout.shortcuts); + app.view(&Cid::List(ListCid::Shortcuts), frame, layout.shortcuts); } fn subscribe(&self, app: &mut Application) -> Result<()> { app.subscribe( - &Cid::Home(HomeCid::Header), + &Cid::List(ListCid::Header), Sub::new(subscription::navigation_clause(), SubClause::Always), )?; @@ -290,7 +183,7 @@ impl ViewPage for HomeView { fn unsubscribe(&self, app: &mut Application) -> Result<()> { app.unsubscribe( - &Cid::Home(HomeCid::Header), + &Cid::List(ListCid::Header), subscription::navigation_clause(), )?; @@ -391,7 +284,7 @@ impl IssuePage { } _ => Progress::None, }; - let context = widget::issue::browse_context(context, theme, progress); + let context = ui::browse_context(context, theme, progress); Some(context) } IssueCid::Details => { @@ -400,11 +293,11 @@ impl IssuePage { State::One(StateValue::Usize(scroll)) => Progress::Percentage(scroll), _ => Progress::None, }; - let context = widget::issue::description_context(context, theme, progress); + let context = ui::description_context(context, theme, progress); Some(context) } IssueCid::Form => { - let context = widget::issue::form_context(context, theme, Progress::None); + let context = ui::form_context(context, theme, Progress::None); Some(context) } _ => None, @@ -433,22 +326,23 @@ impl IssuePage { } } -impl ViewPage for IssuePage { +impl ViewPage for IssuePage { fn mount( &self, app: &mut Application, context: &Context, theme: &Theme, ) -> Result<()> { - let header = widget::common::app_header(context, theme, None).to_boxed(); - let list = widget::issue::list(context, theme, self.issue.clone()).to_boxed(); + let navigation = ui::list_navigation(theme); + let header = widget::common::app_header(context, theme, Some(navigation)).to_boxed(); + let list = ui::list(context, theme, self.issue.clone()).to_boxed(); app.remount(Cid::Issue(IssueCid::Header), header, vec![])?; app.remount(Cid::Issue(IssueCid::List), list, vec![])?; if let Some((id, issue)) = &self.issue { let comments = issue.comments().collect::>(); - let details = widget::issue::details( + let details = ui::details( context, theme, (*id, issue.clone()), @@ -496,10 +390,10 @@ impl ViewPage for IssuePage { if let Some(issue) = cob::issue::find(repo, &id)? { self.issue = Some((id, issue.clone())); - let list = widget::issue::list(context, theme, self.issue.clone()).to_boxed(); + let list = ui::list(context, theme, self.issue.clone()).to_boxed(); let comments = issue.comments().collect::>(); - let details = widget::issue::details( + let details = ui::details( context, theme, (id, issue.clone()), @@ -516,7 +410,7 @@ impl ViewPage for IssuePage { if let Some(issue) = cob::issue::find(repo, &id)? { self.issue = Some((id, issue.clone())); let comments = issue.comments().collect::>(); - let details = widget::issue::details( + let details = ui::details( context, theme, (id, issue.clone()), @@ -531,8 +425,8 @@ impl ViewPage for IssuePage { self.update_shortcuts(app, self.active_component.clone())?; } Message::Issue(IssueMessage::OpenForm) => { - let new_form = widget::issue::new_form(context, theme).to_boxed(); - let list = widget::issue::list(context, theme, None).to_boxed(); + let new_form = ui::new_form(context, theme).to_boxed(); + let list = ui::list(context, theme, None).to_boxed(); app.remount(Cid::Issue(IssueCid::List), list, vec![])?; app.remount(Cid::Issue(IssueCid::Form), new_form, vec![])?; @@ -545,7 +439,7 @@ impl ViewPage for IssuePage { Message::Issue(IssueMessage::HideForm) => { app.umount(&Cid::Issue(IssueCid::Form))?; - let list = widget::issue::list(context, theme, self.issue.clone()).to_boxed(); + let list = ui::list(context, theme, self.issue.clone()).to_boxed(); app.remount(Cid::Issue(IssueCid::List), list, vec![])?; app.subscribe( @@ -559,7 +453,7 @@ impl ViewPage for IssuePage { return Ok(Some(Message::Issue(IssueMessage::Focus(IssueCid::List)))); } Message::FormSubmitted(id) => { - if id == widget::issue::FORM_ID_EDIT { + if id == ui::FORM_ID_EDIT { let state = app.state(&Cid::Issue(IssueCid::Form))?; if let State::Linked(mut states) = state { let mut missing_values = vec![]; @@ -654,228 +548,3 @@ impl ViewPage for IssuePage { Ok(()) } } - -/// -/// Patch detail page -/// -pub struct PatchView { - active_component: PatchCid, - patch: (PatchId, Patch), - shortcuts: HashMap>, -} - -impl PatchView { - pub fn new(theme: Theme, patch: (PatchId, Patch)) -> Self { - let shortcuts = Self::build_shortcuts(&theme); - PatchView { - active_component: PatchCid::Activity, - patch, - shortcuts, - } - } - - fn build_shortcuts(theme: &Theme) -> HashMap> { - [ - ( - PatchCid::Activity, - widget::common::shortcuts( - theme, - vec![ - widget::common::shortcut(theme, "esc", "back"), - widget::common::shortcut(theme, "tab", "section"), - widget::common::shortcut(theme, "q", "quit"), - ], - ), - ), - ( - PatchCid::Files, - widget::common::shortcuts( - theme, - vec![ - widget::common::shortcut(theme, "esc", "back"), - widget::common::shortcut(theme, "tab", "section"), - widget::common::shortcut(theme, "q", "quit"), - ], - ), - ), - ] - .iter() - .cloned() - .collect() - } - - fn update_shortcuts( - &self, - app: &mut Application, - cid: PatchCid, - ) -> Result<()> { - if let Some(shortcuts) = self.shortcuts.get(&cid) { - app.remount( - Cid::Patch(PatchCid::Shortcuts), - shortcuts.clone().to_boxed(), - vec![], - )?; - } - Ok(()) - } -} - -impl ViewPage for PatchView { - fn mount( - &self, - app: &mut Application, - context: &Context, - theme: &Theme, - ) -> Result<()> { - let navigation = widget::patch::navigation(theme); - let header = widget::common::app_header(context, theme, Some(navigation)).to_boxed(); - let activity = widget::patch::activity(theme).to_boxed(); - let files = widget::patch::files(theme).to_boxed(); - let context = widget::patch::context(context, theme, self.patch.clone()).to_boxed(); - - app.remount(Cid::Patch(PatchCid::Header), header, vec![])?; - app.remount(Cid::Patch(PatchCid::Activity), activity, vec![])?; - app.remount(Cid::Patch(PatchCid::Files), files, vec![])?; - app.remount(Cid::Patch(PatchCid::Context), context, vec![])?; - - let active_component = Cid::Patch(self.active_component.clone()); - app.active(&active_component)?; - self.update_shortcuts(app, self.active_component.clone())?; - - Ok(()) - } - - fn unmount(&self, app: &mut Application) -> Result<()> { - app.umount(&Cid::Patch(PatchCid::Header))?; - app.umount(&Cid::Patch(PatchCid::Activity))?; - app.umount(&Cid::Patch(PatchCid::Files))?; - app.umount(&Cid::Patch(PatchCid::Context))?; - app.umount(&Cid::Patch(PatchCid::Shortcuts))?; - Ok(()) - } - - fn update( - &mut self, - app: &mut Application, - _context: &Context, - _theme: &Theme, - message: Message, - ) -> Result> { - if let Message::NavigationChanged(index) = message { - self.active_component = PatchCid::from(index as usize); - - let active_component = Cid::Patch(self.active_component.clone()); - app.active(&active_component)?; - self.update_shortcuts(app, self.active_component.clone())?; - } - - Ok(None) - } - - fn view(&mut self, app: &mut Application, frame: &mut Frame) { - let area = frame.size(); - let shortcuts_h = 1u16; - let layout = layout::default_page(area, shortcuts_h); - - app.view(&Cid::Patch(PatchCid::Header), frame, layout.navigation); - app.view( - &Cid::Patch(self.active_component.clone()), - frame, - layout.component, - ); - app.view(&Cid::Patch(PatchCid::Context), frame, layout.context); - app.view(&Cid::Patch(PatchCid::Shortcuts), frame, layout.shortcuts); - } - - fn subscribe(&self, app: &mut Application) -> Result<()> { - app.subscribe( - &Cid::Patch(PatchCid::Header), - Sub::new(subscription::navigation_clause(), SubClause::Always), - )?; - - Ok(()) - } - - fn unsubscribe(&self, app: &mut Application) -> Result<()> { - app.unsubscribe( - &Cid::Patch(PatchCid::Header), - subscription::navigation_clause(), - )?; - - Ok(()) - } -} - -/// View pages need to preserve their state (e.g. selected navigation tab, contents -/// and the selected row of a table). Therefor they should not be (re-)created -/// each time they are displayed. -/// Instead the application can push a new page onto the page stack if it needs to -/// be displayed. Its components are then created using the internal state. If a -/// new page needs to be displayed, it will also be pushed onto the stack. Leaving -/// that page again will pop it from the stack. The application can then return to -/// the previously displayed page in the state it was left. -#[derive(Default)] -pub struct PageStack { - pages: Vec>, -} - -impl PageStack { - pub fn push( - &mut self, - page: Box, - app: &mut Application, - context: &Context, - theme: &Theme, - ) -> Result<()> { - if let Some(page) = self.pages.last() { - page.unsubscribe(app)?; - } - - page.mount(app, context, theme)?; - page.subscribe(app)?; - - self.pages.push(page); - - Ok(()) - } - - pub fn pop(&mut self, app: &mut Application) -> Result<()> { - self.peek_mut()?.unsubscribe(app)?; - self.peek_mut()?.unmount(app)?; - self.pages.pop(); - - self.peek_mut()?.subscribe(app)?; - - Ok(()) - } - - pub fn peek_mut(&mut self) -> Result<&mut Box> { - match self.pages.last_mut() { - Some(page) => Ok(page), - None => Err(anyhow::anyhow!( - "Could not peek active page. Page stack is empty." - )), - } - } -} - -impl From for HomeCid { - fn from(index: usize) -> Self { - match index { - 0 => HomeCid::Dashboard, - 1 => HomeCid::IssueBrowser, - 2 => HomeCid::PatchBrowser, - _ => HomeCid::Dashboard, - } - } -} - -impl From for PatchCid { - fn from(index: usize) -> Self { - match index { - 0 => PatchCid::Activity, - 1 => PatchCid::Files, - _ => PatchCid::Activity, - } - } -} diff --git a/src/issue/app/subscription.rs b/src/issue/app/subscription.rs deleted file mode 100644 index e8a5ef45..00000000 --- a/src/issue/app/subscription.rs +++ /dev/null @@ -1,22 +0,0 @@ -use tuirealm::event::{Key, KeyEvent, KeyModifiers}; -use tuirealm::SubEventClause; - -pub fn navigation_clause() -> SubEventClause -where - UserEvent: Clone + Eq + PartialEq + PartialOrd, -{ - SubEventClause::Keyboard(KeyEvent { - code: Key::Tab, - modifiers: KeyModifiers::NONE, - }) -} - -pub fn global_clause() -> SubEventClause -where - UserEvent: Clone + Eq + PartialEq + PartialOrd, -{ - SubEventClause::Keyboard(KeyEvent { - code: Key::Char('q'), - modifiers: KeyModifiers::NONE, - }) -} diff --git a/src/ui/widget/issue.rs b/src/issue/app/ui.rs similarity index 75% rename from src/ui/widget/issue.rs rename to src/issue/app/ui.rs index 822a109f..4d69ccf3 100644 --- a/src/ui/widget/issue.rs +++ b/src/issue/app/ui.rs @@ -3,30 +3,103 @@ use radicle::cob::thread::CommentId; use radicle::cob::issue::Issue; use radicle::cob::issue::IssueId; -use tuirealm::tui::layout::{Constraint, Direction, Layout}; -use super::common::container::{Container, LabeledContainer}; -use super::common::context::{ContextBar, Progress}; -use super::common::form::Form; -use super::common::label::Textarea; -use super::common::list::List; -use super::common::list::Property; -use super::Widget; +use tuirealm::command::{Cmd, CmdResult}; +use tuirealm::tui::layout::{Constraint, Direction, Layout, Rect}; +use tuirealm::{AttrValue, Attribute, Frame, MockComponent, Props, State}; -use crate::ui::cob; -use crate::ui::cob::IssueItem; -use crate::ui::context::Context; -use crate::ui::theme::Theme; -use crate::ui::widget::common::form::TextArea; -use crate::ui::widget::common::form::TextField; +use radicle_tui::ui::cob; +use radicle_tui::ui::cob::IssueItem; +use radicle_tui::ui::context::Context; +use radicle_tui::ui::theme::Theme; +use radicle_tui::ui::widget::common; +use radicle_tui::ui::widget::{Widget, WidgetComponent}; -use super::*; +use common::container::{Container, Tabs}; +use common::context::{ContextBar, Progress}; +use common::form::{Form, TextArea, TextField}; +use common::label::Textarea; +use common::list::{ColumnWidth, List, Property, Table}; pub const FORM_ID_EDIT: &str = "edit-form"; +pub struct IssueBrowser { + items: Vec, + table: Widget>, +} + +impl IssueBrowser { + pub fn new(context: &Context, theme: &Theme, selected: Option<(IssueId, Issue)>) -> Self { + let header = [ + common::label(" ● "), + common::label("ID"), + common::label("Title"), + common::label("Author"), + common::label("Tags"), + common::label("Assignees"), + common::label("Opened"), + ]; + + let widths = [ + ColumnWidth::Fixed(3), + ColumnWidth::Fixed(7), + ColumnWidth::Grow, + ColumnWidth::Fixed(21), + ColumnWidth::Fixed(25), + ColumnWidth::Fixed(21), + ColumnWidth::Fixed(18), + ]; + + let repo = context.repository(); + let mut items = vec![]; + + for (id, issue) in context.issues() { + if let Ok(item) = IssueItem::try_from((context.profile(), repo, *id, issue.clone())) { + items.push(item); + } + } + + items.sort_by(|a, b| b.timestamp().cmp(a.timestamp())); + items.sort_by(|a, b| b.state().cmp(a.state())); + + let selected = match selected { + Some((id, issue)) => Some(IssueItem::from((context.profile(), repo, id, issue))), + _ => items.first().cloned(), + }; + + let table = Widget::new(Table::new(&items, selected, header, widths, theme.clone())) + .highlight(theme.colors.item_list_highlighted_bg); + + Self { items, table } + } + + pub fn items(&self) -> &Vec { + &self.items + } +} + +impl WidgetComponent for IssueBrowser { + fn view(&mut self, properties: &Props, frame: &mut Frame, area: Rect) { + let focus = properties + .get_or(Attribute::Focus, AttrValue::Flag(false)) + .unwrap_flag(); + + self.table.attr(Attribute::Focus, AttrValue::Flag(focus)); + self.table.view(frame, area); + } + + fn state(&self) -> State { + self.table.state() + } + + fn perform(&mut self, _properties: &Props, cmd: Cmd) -> CmdResult { + self.table.perform(cmd) + } +} + pub struct LargeList { items: Vec, - list: Widget, + list: Widget, } impl LargeList { @@ -48,7 +121,7 @@ impl LargeList { let list = Widget::new(List::new(&items, selected, theme.clone())) .highlight(theme.colors.item_list_highlighted_bg); - let container = common::labeled_container(theme, "Issues", list.to_boxed()); + let container = common::container(theme, list.to_boxed()); Self { items, @@ -176,7 +249,7 @@ impl IssueDetails { ) -> Self { Self { header: header(context, theme, issue), - description: issue::description(context, theme, description), + description: self::description(context, theme, description), } } } @@ -246,65 +319,11 @@ impl WidgetComponent for CommentBody { } } -pub struct NewForm { - /// The issue this form writes its input values to. - _issue: Issue, - /// The actual form. - form: Widget, -} - -impl NewForm { - pub fn new(theme: &Theme) -> Self { - use tuirealm::props::Layout; - - let title = Widget::new(TextField::new(theme.clone(), "Title")).to_boxed(); - let tags = Widget::new(TextField::new(theme.clone(), "Labels (bug, ...)")).to_boxed(); - let assignees = Widget::new(TextField::new( - theme.clone(), - "Assignees (z6MkvAdxCp1oLVVTsqYvev9YrhSN3gBQNUSM45hhy4pgkexk, ...)", - )) - .to_boxed(); - let description = Widget::new(TextArea::new(theme.clone(), "Description")).to_boxed(); - - let mut form = Widget::new(Form::new( - theme.clone(), - vec![title, tags, assignees, description], - )); - - form.attr( - Attribute::Layout, - AttrValue::Layout( - Layout::default().constraints( - [ - Constraint::Length(3), - Constraint::Length(3), - Constraint::Length(3), - Constraint::Min(3), - ] - .as_ref(), - ), - ), - ); - - Self { - _issue: Issue::default(), - form, - } - } -} - -impl WidgetComponent for NewForm { - fn view(&mut self, _properties: &Props, frame: &mut Frame, area: Rect) { - self.form.view(frame, area); - } - - fn state(&self) -> State { - State::None - } - - fn perform(&mut self, _properties: &Props, cmd: Cmd) -> CmdResult { - self.form.perform(cmd) - } +pub fn list_navigation(theme: &Theme) -> Widget { + common::tabs( + theme, + vec![common::reversable_label("issues").foreground(theme.colors.tabs_highlighted_fg)], + ) } pub fn list( @@ -406,3 +425,11 @@ pub fn form_context(_context: &Context, theme: &Theme, progress: Progress) -> Wi common::context::bar(theme, "Open", "", "", "", &progress.to_string()) .custom(ContextBar::PROP_EDIT_MODE, AttrValue::Flag(true)) } + +pub fn issues( + context: &Context, + theme: &Theme, + selected: Option<(IssueId, Issue)>, +) -> Widget { + Widget::new(IssueBrowser::new(context, theme, selected)) +} diff --git a/src/patch/app/event.rs b/src/patch/app/event.rs index 1e05bef3..e0248078 100644 --- a/src/patch/app/event.rs +++ b/src/patch/app/event.rs @@ -7,7 +7,6 @@ use radicle_tui::ui::widget::common::container::{ }; use radicle_tui::ui::widget::common::context::{ContextBar, Shortcuts}; use radicle_tui::ui::widget::common::list::PropertyList; -use radicle_tui::ui::widget::home::PatchBrowser; use radicle_tui::ui::widget::Widget; @@ -46,7 +45,7 @@ impl tuirealm::Component for Widget { } } -impl tuirealm::Component for Widget { +impl tuirealm::Component for Widget { fn on(&mut self, event: Event) -> Option { match event { Event::Keyboard(KeyEvent { code: Key::Up, .. }) diff --git a/src/patch/app/page.rs b/src/patch/app/page.rs index cc66cb15..986b5ef7 100644 --- a/src/patch/app/page.rs +++ b/src/patch/app/page.rs @@ -96,7 +96,7 @@ impl ViewPage for ListView { ) -> Result<()> { let navigation = ui::list_navigation(theme); let header = widget::common::app_header(context, theme, Some(navigation)).to_boxed(); - let patch_browser = widget::home::patches(context, theme, None).to_boxed(); + let patch_browser = ui::patches(context, theme, None).to_boxed(); app.remount(Cid::List(ListCid::Header), header, vec![])?; app.remount(Cid::List(ListCid::PatchBrowser), patch_browser, vec![])?; diff --git a/src/patch/app/ui.rs b/src/patch/app/ui.rs index 6273b221..6970db70 100644 --- a/src/patch/app/ui.rs +++ b/src/patch/app/ui.rs @@ -4,15 +4,96 @@ use tuirealm::command::{Cmd, CmdResult}; use tuirealm::tui::layout::Rect; use tuirealm::{AttrValue, Attribute, Frame, MockComponent, Props, State}; +use radicle_tui::ui::cob; +use radicle_tui::ui::cob::PatchItem; use radicle_tui::ui::context::Context; +use radicle_tui::ui::layout; use radicle_tui::ui::theme::Theme; use radicle_tui::ui::widget::common; use radicle_tui::ui::widget::{Widget, WidgetComponent}; -use radicle_tui::ui::{cob, layout}; use common::container::Tabs; use common::context::{ContextBar, Progress}; use common::label::Label; +use common::list::{ColumnWidth, Table}; + +pub struct PatchBrowser { + items: Vec, + table: Widget>, +} + +impl PatchBrowser { + pub fn new(context: &Context, theme: &Theme, selected: Option<(PatchId, Patch)>) -> Self { + let header = [ + common::label(" ● "), + common::label("ID"), + common::label("Title"), + common::label("Author"), + common::label("Head"), + common::label("+"), + common::label("-"), + common::label("Updated"), + ]; + + let widths = [ + ColumnWidth::Fixed(3), + ColumnWidth::Fixed(7), + ColumnWidth::Grow, + ColumnWidth::Fixed(21), + ColumnWidth::Fixed(7), + ColumnWidth::Fixed(4), + ColumnWidth::Fixed(4), + ColumnWidth::Fixed(18), + ]; + + let repo = context.repository(); + let mut items = vec![]; + + for (id, patch) in context.patches() { + if let Ok(item) = PatchItem::try_from((context.profile(), repo, *id, patch.clone())) { + items.push(item); + } + } + + items.sort_by(|a, b| b.timestamp().cmp(a.timestamp())); + items.sort_by(|a, b| a.state().cmp(b.state())); + + let selected = match selected { + Some((id, patch)) => { + Some(PatchItem::try_from((context.profile(), repo, id, patch)).unwrap()) + } + _ => items.first().cloned(), + }; + + let table = Widget::new(Table::new(&items, selected, header, widths, theme.clone())) + .highlight(theme.colors.item_list_highlighted_bg); + + Self { items, table } + } + + pub fn items(&self) -> &Vec { + &self.items + } +} + +impl WidgetComponent for PatchBrowser { + fn view(&mut self, properties: &Props, frame: &mut Frame, area: Rect) { + let focus = properties + .get_or(Attribute::Focus, AttrValue::Flag(false)) + .unwrap_flag(); + + self.table.attr(Attribute::Focus, AttrValue::Flag(focus)); + self.table.view(frame, area); + } + + fn state(&self) -> State { + self.table.state() + } + + fn perform(&mut self, _properties: &Props, cmd: Cmd) -> CmdResult { + self.table.perform(cmd) + } +} pub struct Activity { label: Widget