From 9fb41c45c44aafa888267e4236ab2efa8bc424a1 Mon Sep 17 00:00:00 2001 From: Erik Kundt Date: Wed, 12 Jul 2023 11:27:49 +0200 Subject: [PATCH] Adaptive shortcuts on issue page --- src/app.rs | 2 +- src/app/page.rs | 73 ++++++++++++++++++++++++++------- src/ui/layout.rs | 2 +- src/ui/widget/common/context.rs | 1 + 4 files changed, 61 insertions(+), 17 deletions(-) diff --git a/src/app.rs b/src/app.rs index 319d5a9f..c0cdb590 100644 --- a/src/app.rs +++ b/src/app.rs @@ -153,7 +153,7 @@ impl App { let repo = self.context.repository(); if let Some(issue) = cob::issue::find(repo, &id)? { - let view = Box::new(IssuePage::new((id, issue))); + let view = Box::new(IssuePage::new(theme.clone(), (id, issue))); self.pages.push(view, app, &self.context, theme)?; Ok(()) diff --git a/src/app/page.rs b/src/app/page.rs index 3067a339..86eb0341 100644 --- a/src/app/page.rs +++ b/src/app/page.rs @@ -1,15 +1,18 @@ -use anyhow::Result; +use std::collections::HashMap; + +use anyhow::{Ok, Result}; use radicle::cob::issue::{Issue, IssueId}; use radicle::cob::patch::{Patch, PatchId}; use radicle_tui::cob; +use radicle_tui::ui::widget::common::context::Shortcuts; use tuirealm::{Frame, NoUserEvent, Sub, SubClause}; use radicle_tui::ui::context::Context; use radicle_tui::ui::layout; use radicle_tui::ui::theme::Theme; -use radicle_tui::ui::widget; +use radicle_tui::ui::widget::{self, Widget}; use super::{subscription, Application, Cid, HomeCid, IssueCid, IssueMessage, Message, PatchCid}; @@ -149,11 +152,59 @@ impl ViewPage for HomeView { /// pub struct IssuePage { issue: (IssueId, Issue), + shortcuts: HashMap>, } impl IssuePage { - pub fn new(issue: (IssueId, Issue)) -> Self { - IssuePage { issue } + pub fn new(theme: Theme, issue: (IssueId, Issue)) -> Self { + let shortcuts = Self::build_shortcuts(&theme); + IssuePage { issue, shortcuts } + } + + fn build_shortcuts(theme: &Theme) -> HashMap> { + [ + ( + IssueCid::List, + widget::common::shortcuts( + theme, + vec![ + widget::common::shortcut(theme, "esc", "back"), + widget::common::shortcut(theme, "↑/↓", "navigate"), + widget::common::shortcut(theme, "enter", "show"), + widget::common::shortcut(theme, "q", "quit"), + ], + ), + ), + ( + IssueCid::Details, + widget::common::shortcuts( + theme, + vec![ + widget::common::shortcut(theme, "esc", "back"), + widget::common::shortcut(theme, "↑/↓", "scroll"), + widget::common::shortcut(theme, "q", "quit"), + ], + ), + ), + ] + .iter() + .cloned() + .collect() + } + + fn update_shortcuts( + &self, + app: &mut Application, + cid: IssueCid, + ) -> Result<()> { + if let Some(shortcuts) = self.shortcuts.get(&cid) { + app.remount( + Cid::Issue(IssueCid::Shortcuts), + shortcuts.clone().to_boxed(), + vec![], + )?; + } + Ok(()) } } @@ -177,21 +228,12 @@ impl ViewPage for IssuePage { ) .to_boxed(); - let shortcuts = widget::common::shortcuts( - theme, - vec![ - widget::common::shortcut(theme, "esc", "back"), - widget::common::shortcut(theme, "q", "quit"), - ], - ) - .to_boxed(); - app.remount(Cid::Issue(IssueCid::Header), header, vec![])?; app.remount(Cid::Issue(IssueCid::List), list, vec![])?; app.remount(Cid::Issue(IssueCid::Details), details, vec![])?; - app.remount(Cid::Issue(IssueCid::Shortcuts), shortcuts, vec![])?; app.active(&Cid::Issue(IssueCid::List))?; + self.update_shortcuts(app, IssueCid::List)?; Ok(()) } @@ -227,7 +269,8 @@ impl ViewPage for IssuePage { } } Message::Issue(IssueMessage::Focus(cid)) => { - app.active(&Cid::Issue(cid))?; + app.active(&Cid::Issue(cid.clone()))?; + self.update_shortcuts(app, cid)?; } _ => {} } diff --git a/src/ui/layout.rs b/src/ui/layout.rs index 15879a89..65358c27 100644 --- a/src/ui/layout.rs +++ b/src/ui/layout.rs @@ -221,6 +221,6 @@ pub fn issue_page(area: Rect, shortcuts_h: u16) -> IssuePage { header: root[0], left: split[0], right: split[1], - shortcuts: root[1], + shortcuts: root[2], } } diff --git a/src/ui/widget/common/context.rs b/src/ui/widget/common/context.rs index 8f01a356..b235fb6e 100644 --- a/src/ui/widget/common/context.rs +++ b/src/ui/widget/common/context.rs @@ -58,6 +58,7 @@ impl WidgetComponent for Shortcut { /// A shortcut bar that displays multiple shortcuts and separates them with a /// divider. +#[derive(Clone)] pub struct Shortcuts { shortcuts: Vec>, divider: Widget