Radish alpha
r
Radicle terminal user interface
Radicle
Git (anonymous pull)
Log in to clone via SSH
Adaptive shortcuts on issue page
Erik Kundt committed 2 years ago
commit 9fb41c45c44aafa888267e4236ab2efa8bc424a1
parent b66b44fe15aa71d7edb9df0f73992971960dcd2c
4 files changed +61 -17
modified 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(())
modified 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<IssueCid, Widget<Shortcuts>>,
}

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, Widget<Shortcuts>> {
+
        [
+
            (
+
                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, Message, NoUserEvent>,
+
        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)?;
            }
            _ => {}
        }
modified 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],
    }
}
modified 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<Widget<Shortcut>>,
    divider: Widget<Label>,