Radish alpha
r
rad:z39mP9rQAaGmERfUMPULfPUi473tY
Radicle terminal user interface
Radicle
Git
lib/ui: Reorganize types
Erik Kundt committed 3 months ago
commit e6c95f0578d4aab94a4630f5614d1df44d80a13a
parent 670f615
11 files changed +142 -132
modified bin/commands/inbox/list.rs
@@ -22,8 +22,11 @@ use tui::event::Key;
use tui::store;
use tui::task::{Process, Task};
use tui::ui;
-
use tui::ui::widget::{ContainerState, TableState, TextEditState, TextViewState, Window};
-
use tui::ui::{Borders, BufferedValue, Column, Show, Spacing, Ui};
+
use tui::ui::layout::Spacing;
+
use tui::ui::widget::{
+
    Borders, Column, ContainerState, TableState, TextEditState, TextViewState, Window,
+
};
+
use tui::ui::{BufferedValue, Show, Ui};
use tui::{Channel, Exit};

use super::common::RepositoryMode;
modified bin/commands/issue/list.rs
@@ -20,11 +20,12 @@ use tui::event::Key;
use tui::store;
use tui::task::EmptyProcessors;
use tui::ui;
+
use tui::ui::layout::Spacing;
use tui::ui::span;
use tui::ui::widget::{
-
    ContainerState, TableState, TextEditState, TextViewState, TreeState, Window,
+
    Borders, Column, ContainerState, TableState, TextEditState, TextViewState, TreeState, Window,
};
-
use tui::ui::{Borders, BufferedValue, Column, Show, Spacing, ToRow, Ui};
+
use tui::ui::{BufferedValue, Show, ToRow, Ui};
use tui::{Channel, Exit};

use crate::cob::issue;
modified bin/commands/patch/list.rs
@@ -21,8 +21,11 @@ use tui::event::Key;
use tui::store;
use tui::task::EmptyProcessors;
use tui::ui;
-
use tui::ui::widget::{ContainerState, TableState, TextEditState, TextViewState, Window};
-
use tui::ui::{Borders, BufferedValue, Column, Show, Spacing, Ui};
+
use tui::ui::layout::Spacing;
+
use tui::ui::widget::{
+
    Borders, Column, ContainerState, TableState, TextEditState, TextViewState, Window,
+
};
+
use tui::ui::{BufferedValue, Show, Ui};
use tui::{Channel, Exit};

use crate::ui::items::filter::Filter;
modified bin/commands/patch/review.rs
@@ -23,10 +23,10 @@ use radicle_tui as tui;
use tui::event::Key;
use tui::store;
use tui::task::EmptyProcessors;
+
use tui::ui::layout::Spacing;
use tui::ui::span;
-
use tui::ui::widget::{ContainerState, TableState, TextViewState, Window};
-
use tui::ui::{Borders, Context, Show, Ui};
-
use tui::ui::{Column, Spacing};
+
use tui::ui::widget::{Borders, Column, ContainerState, TableState, TextViewState, Window};
+
use tui::ui::{Context, Show, Ui};
use tui::{Channel, Exit};

use crate::git::HunkState;
modified bin/ui.rs
@@ -3,17 +3,16 @@ pub mod items;
pub mod layout;
pub mod span;

-
use radicle_tui::ui::Spacing;
-

use ratatui::layout::{Constraint, Layout};
use ratatui::Frame;

use radicle_tui as tui;

use tui::event::Key;
-
use tui::ui::widget::{TableState, TextEditState, Widget};
-
use tui::ui::{Borders, Response, Ui};
-
use tui::ui::{BufferedValue, Column, ToRow};
+
use tui::ui::layout::Spacing;
+
use tui::ui::widget::{Borders, Column, TableState, TextEditState, Widget};
+
use tui::ui::{BufferedValue, ToRow};
+
use tui::ui::{Response, Ui};

#[derive(Clone, Debug)]
pub struct TerminalInfo {
modified bin/ui/items/patch.rs
@@ -30,7 +30,8 @@ use radicle_tui as tui;
use tui::ui::span;
use tui::ui::utils::LineMerger;
use tui::ui::utils::MergeLocation;
-
use tui::ui::{Column, ToRow};
+
use tui::ui::widget::Column;
+
use tui::ui::ToRow;

use crate::git::{self, Blobs, DiffStats, HunkDiff, HunkState, HunkStats};
use crate::ui;
modified examples/hello.rs
@@ -9,8 +9,8 @@ use radicle_tui as tui;
use tui::event::Key;
use tui::store;
use tui::task::EmptyProcessors;
-
use tui::ui::widget::Window;
-
use tui::ui::{Borders, Context, Show};
+
use tui::ui::widget::{Borders, Window};
+
use tui::ui::{Context, Show};
use tui::{Channel, Exit};

const ALIEN: &str = r#"
modified examples/selection.rs
@@ -13,8 +13,9 @@ use radicle_tui as tui;
use tui::event::Key;
use tui::store::Update;
use tui::task::EmptyProcessors;
-
use tui::ui::widget::{TableState, Window};
-
use tui::ui::{Borders, Column, Context, Show, Spacing, ToRow};
+
use tui::ui::layout::Spacing;
+
use tui::ui::widget::{Borders, Column, TableState, Window};
+
use tui::ui::{Context, Show, ToRow};
use tui::Channel;
use tui::Exit;

modified src/ui.rs
@@ -26,15 +26,11 @@ use tui_tree_widget::TreeItem;
use crate::event::{Event, Key};
use crate::store::Update;
use crate::terminal::Terminal;
+
use crate::ui::layout::Spacing;
use crate::ui::theme::Theme;
-
use crate::ui::widget::{AddContentFn, Widget};
+
use crate::ui::widget::{AddContentFn, Borders, Column, Widget};
use crate::{Interrupted, Share};

-
pub const RENDER_WIDTH_XSMALL: usize = 50;
-
pub const RENDER_WIDTH_SMALL: usize = 70;
-
pub const RENDER_WIDTH_MEDIUM: usize = 150;
-
pub const RENDER_WIDTH_LARGE: usize = usize::MAX;
-

const RENDERING_TICK_RATE: Duration = Duration::from_millis(250);

/// The main UI trait for the ability to render an application.
@@ -188,17 +184,6 @@ impl<M> Context<M> {
    }
}

-
/// `Borders` defines which borders should be drawn around a widget.
-
pub enum Borders {
-
    None,
-
    Spacer { top: usize, left: usize },
-
    All,
-
    Top,
-
    Sides,
-
    Bottom,
-
    BottomSides,
-
}
-

/// A `Layout` is used to support pre-defined layouts. It either represents
/// such a predefined layout or a wrapped `ratatui` layout. It's used internally
/// but can be build from a `ratatui` layout.
@@ -669,99 +654,6 @@ where
    }
}

-
#[derive(Clone, Debug, Default)]
-
pub struct ColumnView {
-
    small: bool,
-
    medium: bool,
-
    large: bool,
-
}
-

-
impl ColumnView {
-
    pub fn all() -> Self {
-
        Self {
-
            small: true,
-
            medium: true,
-
            large: true,
-
        }
-
    }
-

-
    pub fn small(mut self) -> Self {
-
        self.small = true;
-
        self
-
    }
-

-
    pub fn medium(mut self) -> Self {
-
        self.medium = true;
-
        self
-
    }
-

-
    pub fn large(mut self) -> Self {
-
        self.large = true;
-
        self
-
    }
-
}
-

-
#[derive(Clone, Debug)]
-
pub struct Column<'a> {
-
    pub text: Text<'a>,
-
    pub width: Constraint,
-
    pub skip: bool,
-
    pub view: ColumnView,
-
}
-

-
impl<'a> Column<'a> {
-
    pub fn new(text: impl Into<Text<'a>>, width: Constraint) -> Self {
-
        Self {
-
            text: text.into(),
-
            width,
-
            skip: false,
-
            view: ColumnView::all(),
-
        }
-
    }
-

-
    pub fn skip(mut self, skip: bool) -> Self {
-
        self.skip = skip;
-
        self
-
    }
-

-
    pub fn hide_small(mut self) -> Self {
-
        self.view = ColumnView::default().medium().large();
-
        self
-
    }
-

-
    pub fn hide_medium(mut self) -> Self {
-
        self.view = ColumnView::default().large();
-
        self
-
    }
-

-
    pub fn displayed(&self, area_width: usize) -> bool {
-
        if area_width < RENDER_WIDTH_SMALL {
-
            self.view.small
-
        } else if area_width < RENDER_WIDTH_MEDIUM {
-
            self.view.medium
-
        } else if area_width < RENDER_WIDTH_LARGE {
-
            self.view.large
-
        } else {
-
            true
-
        }
-
    }
-
}
-

-
#[derive(Default)]
-
pub struct Spacing(u16);
-

-
impl From<u16> for Spacing {
-
    fn from(value: u16) -> Self {
-
        Self(value)
-
    }
-
}
-

-
impl From<Spacing> for u16 {
-
    fn from(spacing: Spacing) -> Self {
-
        spacing.0
-
    }
-
}
-

/// Needs to be implemented for items that are supposed to be rendered in tables.
pub trait ToRow<const W: usize> {
    fn to_row(&self) -> [Cell<'_>; W];
modified src/ui/layout.rs
@@ -54,3 +54,18 @@ pub fn centered_rect(r: Rect, percent_x: u16, percent_y: u16) -> Rect {
pub fn fill() -> Layout {
    Layout::vertical([Constraint::Fill(1)].to_vec())
}
+

+
#[derive(Default)]
+
pub struct Spacing(u16);
+

+
impl From<u16> for Spacing {
+
    fn from(value: u16) -> Self {
+
        Self(value)
+
    }
+
}
+

+
impl From<Spacing> for u16 {
+
    fn from(spacing: Spacing) -> Self {
+
        spacing.0
+
    }
+
}
modified src/ui/widget.rs
@@ -14,20 +14,115 @@ use ratatui::{layout::Constraint, widgets::Paragraph};

use crate::event::Key;
use crate::ui::ext::{FooterBlock, FooterBlockType, HeaderBlock};
+
use crate::ui::layout::Spacing;
use crate::ui::theme::style;
-
use crate::ui::{layout, span, Spacing, ToTree};
-
use crate::ui::{Column, ToRow};
+
use crate::ui::ToRow;
+
use crate::ui::{layout, span, ToTree};

-
use super::{Borders, Context, InnerResponse, Response, Ui};
+
use super::{Context, InnerResponse, Response, Ui};

pub type AddContentFn<'a, M, R> = dyn FnOnce(&mut Ui<M>) -> R + 'a;

+
pub const RENDER_WIDTH_XSMALL: usize = 50;
+
pub const RENDER_WIDTH_SMALL: usize = 70;
+
pub const RENDER_WIDTH_MEDIUM: usize = 150;
+
pub const RENDER_WIDTH_LARGE: usize = usize::MAX;
+

pub trait Widget {
    fn ui<M>(self, ui: &mut Ui<M>, frame: &mut Frame) -> Response
    where
        M: Clone;
}

+
/// `Borders` defines which borders should be drawn around a widget.
+
pub enum Borders {
+
    None,
+
    Spacer { top: usize, left: usize },
+
    All,
+
    Top,
+
    Sides,
+
    Bottom,
+
    BottomSides,
+
}
+

+
#[derive(Clone, Debug, Default)]
+
pub struct ColumnView {
+
    small: bool,
+
    medium: bool,
+
    large: bool,
+
}
+

+
impl ColumnView {
+
    pub fn all() -> Self {
+
        Self {
+
            small: true,
+
            medium: true,
+
            large: true,
+
        }
+
    }
+

+
    pub fn small(mut self) -> Self {
+
        self.small = true;
+
        self
+
    }
+

+
    pub fn medium(mut self) -> Self {
+
        self.medium = true;
+
        self
+
    }
+

+
    pub fn large(mut self) -> Self {
+
        self.large = true;
+
        self
+
    }
+
}
+

+
#[derive(Clone, Debug)]
+
pub struct Column<'a> {
+
    pub text: Text<'a>,
+
    pub width: Constraint,
+
    pub skip: bool,
+
    pub view: ColumnView,
+
}
+

+
impl<'a> Column<'a> {
+
    pub fn new(text: impl Into<Text<'a>>, width: Constraint) -> Self {
+
        Self {
+
            text: text.into(),
+
            width,
+
            skip: false,
+
            view: ColumnView::all(),
+
        }
+
    }
+

+
    pub fn skip(mut self, skip: bool) -> Self {
+
        self.skip = skip;
+
        self
+
    }
+

+
    pub fn hide_small(mut self) -> Self {
+
        self.view = ColumnView::default().medium().large();
+
        self
+
    }
+

+
    pub fn hide_medium(mut self) -> Self {
+
        self.view = ColumnView::default().large();
+
        self
+
    }
+

+
    pub fn displayed(&self, area_width: usize) -> bool {
+
        if area_width < RENDER_WIDTH_SMALL {
+
            self.view.small
+
        } else if area_width < RENDER_WIDTH_MEDIUM {
+
            self.view.medium
+
        } else if area_width < RENDER_WIDTH_LARGE {
+
            self.view.large
+
        } else {
+
            true
+
        }
+
    }
+
}
+

#[derive(Default)]
pub struct Window {}