From b899751c2bd46f95f83fa20506e3694ddacf02c9 Mon Sep 17 00:00:00 2001 From: Stephan Dilly Date: Mon, 22 Jun 2020 12:21:21 +0200 Subject: [PATCH] share theme instead of copying it all over the place --- src/app.rs | 38 +++++++++++++++--------- src/cmdbar.rs | 10 ++++--- src/components/changes.rs | 4 +-- src/components/commit.rs | 4 +-- src/components/commit_details/details.rs | 8 ++--- src/components/commit_details/mod.rs | 8 +++-- src/components/commitlist.rs | 8 ++--- src/components/diff.rs | 14 ++++----- src/components/filetree.rs | 15 +++++----- src/components/help.rs | 9 +++--- src/components/inspect_commit.rs | 8 +++-- src/components/msg.rs | 8 ++--- src/components/reset.rs | 8 ++--- src/components/stashmsg.rs | 4 +-- src/components/textinput.rs | 8 ++--- src/tabs/revlog.rs | 8 +++-- src/tabs/stashing.rs | 10 +++---- src/tabs/stashlist.rs | 4 +-- src/tabs/status.rs | 8 ++--- src/ui/mod.rs | 4 +-- src/ui/style.rs | 5 +++- 21 files changed, 107 insertions(+), 86 deletions(-) diff --git a/src/app.rs b/src/app.rs index ae3e36d5..3ec920bf 100644 --- a/src/app.rs +++ b/src/app.rs @@ -12,12 +12,13 @@ use crate::{ queue::{Action, InternalEvent, NeedsUpdate, Queue}, strings, tabs::{Revlog, StashList, Stashing, Status}, - ui::style::Theme, + ui::style::{SharedTheme, Theme}, }; use anyhow::{anyhow, Result}; use asyncgit::{sync, AsyncNotification, CWD}; use crossbeam_channel::Sender; use crossterm::event::{Event, KeyEvent}; +use std::rc::Rc; use strings::{commands, order}; use tui::{ backend::Backend, @@ -43,7 +44,7 @@ pub struct App { stashing_tab: Stashing, stashlist_tab: StashList, queue: Queue, - theme: Theme, + theme: SharedTheme, } // public interface @@ -52,27 +53,36 @@ impl App { pub fn new(sender: &Sender) -> Self { let queue = Queue::default(); - let theme = Theme::init(); + let theme = Rc::new(Box::new(Theme::init())); Self { - reset: ResetComponent::new(queue.clone(), &theme), - commit: CommitComponent::new(queue.clone(), &theme), + reset: ResetComponent::new(queue.clone(), theme.clone()), + commit: CommitComponent::new( + queue.clone(), + theme.clone(), + ), stashmsg_popup: StashMsgComponent::new( queue.clone(), - &theme, + theme.clone(), ), inspect_commit_popup: InspectCommitComponent::new( - &queue, sender, &theme, + &queue, + sender, + theme.clone(), ), do_quit: false, - cmdbar: CommandBar::new(&theme), - help: HelpComponent::new(&theme), - msg: MsgComponent::new(&theme), + cmdbar: CommandBar::new(theme.clone()), + help: HelpComponent::new(theme.clone()), + msg: MsgComponent::new(theme.clone()), tab: 0, - revlog: Revlog::new(&queue, sender, &theme), - status_tab: Status::new(sender, &queue, &theme), - stashing_tab: Stashing::new(sender, &queue, &theme), - stashlist_tab: StashList::new(&queue, &theme), + revlog: Revlog::new(&queue, sender, theme.clone()), + status_tab: Status::new(sender, &queue, theme.clone()), + stashing_tab: Stashing::new( + sender, + &queue, + theme.clone(), + ), + stashlist_tab: StashList::new(&queue, theme.clone()), queue, theme, } diff --git a/src/cmdbar.rs b/src/cmdbar.rs index 7f90c6ab..7c0a88bb 100644 --- a/src/cmdbar.rs +++ b/src/cmdbar.rs @@ -1,4 +1,6 @@ -use crate::{components::CommandInfo, strings, ui::style::Theme}; +use crate::{ + components::CommandInfo, strings, ui::style::SharedTheme, +}; use std::borrow::Cow; use tui::{ backend::Backend, @@ -24,7 +26,7 @@ struct Command { pub struct CommandBar { draw_list: Vec, cmd_infos: Vec, - theme: Theme, + theme: SharedTheme, lines: u16, width: u16, expandable: bool, @@ -34,11 +36,11 @@ pub struct CommandBar { const MORE_WIDTH: u16 = 11; impl CommandBar { - pub const fn new(theme: &Theme) -> Self { + pub const fn new(theme: SharedTheme) -> Self { Self { draw_list: Vec::new(), cmd_infos: Vec::new(), - theme: *theme, + theme, lines: 0, width: 0, expandable: false, diff --git a/src/components/changes.rs b/src/components/changes.rs index 9d0c2c59..262f6aba 100644 --- a/src/components/changes.rs +++ b/src/components/changes.rs @@ -8,7 +8,7 @@ use crate::{ keys, queue::{Action, InternalEvent, NeedsUpdate, Queue, ResetItem}, strings, - ui::style::Theme, + ui::style::SharedTheme, }; use anyhow::Result; use asyncgit::{sync, StatusItem, StatusItemType, CWD}; @@ -48,7 +48,7 @@ impl ChangesComponent { focus: bool, is_working_dir: bool, queue: Queue, - theme: &Theme, + theme: SharedTheme, ) -> Self { Self { title: title.into(), diff --git a/src/components/commit.rs b/src/components/commit.rs index a7d1737e..fdce3e11 100644 --- a/src/components/commit.rs +++ b/src/components/commit.rs @@ -6,7 +6,7 @@ use crate::{ keys, queue::{InternalEvent, NeedsUpdate, Queue}, strings, - ui::style::Theme, + ui::style::SharedTheme, }; use anyhow::Result; use asyncgit::{ @@ -107,7 +107,7 @@ impl Component for CommitComponent { impl CommitComponent { /// - pub fn new(queue: Queue, theme: &Theme) -> Self { + pub fn new(queue: Queue, theme: SharedTheme) -> Self { Self { queue, amend: None, diff --git a/src/components/commit_details/details.rs b/src/components/commit_details/details.rs index ece0ef6f..e32d11d0 100644 --- a/src/components/commit_details/details.rs +++ b/src/components/commit_details/details.rs @@ -4,7 +4,7 @@ use crate::{ CommandInfo, Component, DrawableComponent, }, strings, - ui::style::Theme, + ui::style::SharedTheme, }; use anyhow::Result; use asyncgit::{ @@ -25,16 +25,16 @@ use tui::{ pub struct DetailsComponent { data: Option, tags: Vec, - theme: Theme, + theme: SharedTheme, } impl DetailsComponent { /// - pub const fn new(theme: &Theme) -> Self { + pub const fn new(theme: SharedTheme) -> Self { Self { data: None, tags: Vec::new(), - theme: *theme, + theme: theme, } } diff --git a/src/components/commit_details/mod.rs b/src/components/commit_details/mod.rs index 7115330c..5b7c185a 100644 --- a/src/components/commit_details/mod.rs +++ b/src/components/commit_details/mod.rs @@ -4,7 +4,9 @@ use super::{ command_pump, event_pump, CommandBlocking, CommandInfo, Component, DrawableComponent, FileTreeComponent, }; -use crate::{accessors, queue::Queue, strings, ui::style::Theme}; +use crate::{ + accessors, queue::Queue, strings, ui::style::SharedTheme, +}; use anyhow::Result; use asyncgit::{ sync::{CommitId, Tags}, @@ -33,10 +35,10 @@ impl CommitDetailsComponent { pub fn new( queue: &Queue, sender: &Sender, - theme: &Theme, + theme: SharedTheme, ) -> Self { Self { - details: DetailsComponent::new(theme), + details: DetailsComponent::new(theme.clone()), git_commit_files: AsyncCommitFiles::new(sender), file_tree: FileTreeComponent::new( "", diff --git a/src/components/commitlist.rs b/src/components/commitlist.rs index b2574dfa..872d83d6 100644 --- a/src/components/commitlist.rs +++ b/src/components/commitlist.rs @@ -7,7 +7,7 @@ use crate::{ keys, strings::commands, ui::calc_scroll_top, - ui::style::Theme, + ui::style::{SharedTheme, Theme}, }; use anyhow::Result; use asyncgit::sync; @@ -34,12 +34,12 @@ pub struct CommitList { tags: Option, current_size: (u16, u16), scroll_top: usize, - theme: Theme, + theme: SharedTheme, } impl CommitList { /// - pub fn new(title: &str, theme: &Theme) -> Self { + pub fn new(title: &str, theme: SharedTheme) -> Self { Self { items: ItemBatch::default(), selection: 0, @@ -49,7 +49,7 @@ impl CommitList { tags: None, current_size: (0, 0), scroll_top: 0, - theme: *theme, + theme, title: String::from(title), } } diff --git a/src/components/diff.rs b/src/components/diff.rs index c8395f03..7988fa96 100644 --- a/src/components/diff.rs +++ b/src/components/diff.rs @@ -4,7 +4,7 @@ use crate::{ keys, queue::{Action, InternalEvent, NeedsUpdate, Queue, ResetItem}, strings, - ui::{calc_scroll_top, style::Theme}, + ui::{calc_scroll_top, style::SharedTheme}, }; use asyncgit::{hash, sync, DiffLine, DiffLineType, FileDiff, CWD}; use crossterm::event::Event; @@ -37,12 +37,12 @@ pub struct DiffComponent { current: Current, scroll_top: usize, queue: Option, - theme: Theme, + theme: SharedTheme, } impl DiffComponent { /// - pub fn new(queue: Option, theme: &Theme) -> Self { + pub fn new(queue: Option, theme: SharedTheme) -> Self { Self { focused: false, queue, @@ -52,7 +52,7 @@ impl DiffComponent { current_size: (0, 0), selection: 0, scroll_top: 0, - theme: *theme, + theme: theme, } } /// @@ -185,7 +185,7 @@ impl DiffComponent { selection == line_cursor, hunk_selected, i == hunk_len as usize - 1, - self.theme, + &self.theme, ); lines_added += 1; } @@ -207,7 +207,7 @@ impl DiffComponent { selected: bool, selected_hunk: bool, end_of_hunk: bool, - theme: Theme, + theme: &SharedTheme, ) { { let style = theme.diff_hunk_marker(selected_hunk); @@ -506,7 +506,7 @@ mod tests { false, false, false, - Theme::default(), + &SharedTheme::default(), ); assert_eq!(text.len(), 2); diff --git a/src/components/filetree.rs b/src/components/filetree.rs index 06549f7b..4be81475 100644 --- a/src/components/filetree.rs +++ b/src/components/filetree.rs @@ -10,9 +10,8 @@ use crate::{ keys, queue::{InternalEvent, NeedsUpdate, Queue}, strings, ui, - ui::style::Theme, + ui::style::SharedTheme, }; - use anyhow::Result; use asyncgit::{hash, StatusItem, StatusItemType}; use crossterm::event::Event; @@ -28,7 +27,7 @@ pub struct FileTreeComponent { focused: bool, show_selection: bool, queue: Option, - theme: Theme, + theme: SharedTheme, } impl FileTreeComponent { @@ -37,7 +36,7 @@ impl FileTreeComponent { title: &str, focus: bool, queue: Option, - theme: &Theme, + theme: SharedTheme, ) -> Self { Self { title: title.to_string(), @@ -46,7 +45,7 @@ impl FileTreeComponent { focused: focus, show_selection: focus, queue, - theme: *theme, + theme, } } @@ -133,7 +132,7 @@ impl FileTreeComponent { item: &FileTreeItem, width: u16, selected: bool, - theme: Theme, + theme: SharedTheme, ) -> Option { let indent_str = if item.info.indent == 0 { String::from("") @@ -243,7 +242,7 @@ impl DrawableComponent for FileTreeComponent { .tree .selection .map_or(false, |e| e == idx), - self.theme, + self.theme.clone(), ) }, ); @@ -255,7 +254,7 @@ impl DrawableComponent for FileTreeComponent { items, self.tree.selection.map(|idx| idx - selection_offset), self.focused, - self.theme, + &self.theme, ); Ok(()) diff --git a/src/components/help.rs b/src/components/help.rs index 2645c784..36bfcd2f 100644 --- a/src/components/help.rs +++ b/src/components/help.rs @@ -2,7 +2,7 @@ use super::{ visibility_blocking, CommandBlocking, CommandInfo, Component, DrawableComponent, }; -use crate::{keys, strings, ui, ui::style::Theme, version::Version}; +use crate::{keys, strings, ui, version::Version}; use asyncgit::hash; use crossterm::event::Event; use itertools::Itertools; @@ -17,13 +17,14 @@ use tui::{ }; use anyhow::Result; +use ui::style::SharedTheme; /// pub struct HelpComponent { cmds: Vec, visible: bool, selection: u16, - theme: Theme, + theme: SharedTheme, } impl DrawableComponent for HelpComponent { @@ -158,12 +159,12 @@ impl Component for HelpComponent { } impl HelpComponent { - pub const fn new(theme: &Theme) -> Self { + pub const fn new(theme: SharedTheme) -> Self { Self { cmds: vec![], visible: false, selection: 0, - theme: *theme, + theme, } } /// diff --git a/src/components/inspect_commit.rs b/src/components/inspect_commit.rs index b73efc26..b864d1af 100644 --- a/src/components/inspect_commit.rs +++ b/src/components/inspect_commit.rs @@ -4,7 +4,7 @@ use super::{ DrawableComponent, }; use crate::{ - accessors, keys, queue::Queue, strings, ui::style::Theme, + accessors, keys, queue::Queue, strings, ui::style::SharedTheme, }; use anyhow::Result; use asyncgit::{ @@ -154,11 +154,13 @@ impl InspectCommitComponent { pub fn new( queue: &Queue, sender: &Sender, - theme: &Theme, + theme: SharedTheme, ) -> Self { Self { details: CommitDetailsComponent::new( - queue, sender, theme, + queue, + sender, + theme.clone(), ), diff: DiffComponent::new(None, theme), commit_id: None, diff --git a/src/components/msg.rs b/src/components/msg.rs index 664ae98c..fe9c4a46 100644 --- a/src/components/msg.rs +++ b/src/components/msg.rs @@ -12,12 +12,12 @@ use tui::{ widgets::{Block, BorderType, Borders, Clear, Paragraph, Text}, Frame, }; -use ui::style::Theme; +use ui::style::SharedTheme; pub struct MsgComponent { msg: String, visible: bool, - theme: Theme, + theme: SharedTheme, } use anyhow::Result; @@ -97,11 +97,11 @@ impl Component for MsgComponent { } impl MsgComponent { - pub const fn new(theme: &Theme) -> Self { + pub const fn new(theme: SharedTheme) -> Self { Self { msg: String::new(), visible: false, - theme: *theme, + theme, } } /// diff --git a/src/components/reset.rs b/src/components/reset.rs index 04fe8b58..ffbdee2f 100644 --- a/src/components/reset.rs +++ b/src/components/reset.rs @@ -9,7 +9,7 @@ use crate::{ }; use anyhow::Result; use crossterm::event::{Event, KeyCode}; -use std::borrow::Cow; +use std::{borrow::Cow, rc::Rc}; use strings::commands; use tui::{ backend::Backend, @@ -23,7 +23,7 @@ pub struct ResetComponent { target: Option, visible: bool, queue: Queue, - theme: Theme, + theme: Rc>, } impl DrawableComponent for ResetComponent { @@ -111,12 +111,12 @@ impl Component for ResetComponent { impl ResetComponent { /// - pub fn new(queue: Queue, theme: &Theme) -> Self { + pub fn new(queue: Queue, theme: Rc>) -> Self { Self { target: None, visible: false, queue, - theme: *theme, + theme, } } /// diff --git a/src/components/stashmsg.rs b/src/components/stashmsg.rs index e5339037..f5ea37ad 100644 --- a/src/components/stashmsg.rs +++ b/src/components/stashmsg.rs @@ -6,7 +6,7 @@ use crate::{ queue::{InternalEvent, NeedsUpdate, Queue}, strings, tabs::StashingOptions, - ui::style::Theme, + ui::style::SharedTheme, }; use anyhow::Result; use asyncgit::{sync, CWD}; @@ -117,7 +117,7 @@ impl Component for StashMsgComponent { impl StashMsgComponent { /// - pub fn new(queue: Queue, theme: &Theme) -> Self { + pub fn new(queue: Queue, theme: SharedTheme) -> Self { Self { options: StashingOptions::default(), queue, diff --git a/src/components/textinput.rs b/src/components/textinput.rs index 7938e19b..94566eb6 100644 --- a/src/components/textinput.rs +++ b/src/components/textinput.rs @@ -4,7 +4,6 @@ use crate::{ CommandInfo, Component, DrawableComponent, }, strings, ui, - ui::style::Theme, }; use anyhow::Result; use crossterm::event::{Event, KeyCode, KeyModifiers}; @@ -16,6 +15,7 @@ use tui::{ widgets::{Clear, Text}, Frame, }; +use ui::style::SharedTheme; /// primarily a subcomponet for user input of text (used in `CommitComponent`) pub struct TextInputComponent { @@ -23,21 +23,21 @@ pub struct TextInputComponent { default_msg: String, msg: String, visible: bool, - theme: Theme, + theme: SharedTheme, cursor_position: usize, } impl TextInputComponent { /// pub fn new( - theme: &Theme, + theme: SharedTheme, title: &str, default_msg: &str, ) -> Self { Self { msg: String::default(), visible: false, - theme: *theme, + theme: theme, title: title.to_string(), default_msg: default_msg.to_string(), cursor_position: 0, diff --git a/src/tabs/revlog.rs b/src/tabs/revlog.rs index a07cf3cd..7ad1091c 100644 --- a/src/tabs/revlog.rs +++ b/src/tabs/revlog.rs @@ -7,7 +7,7 @@ use crate::{ keys, queue::{InternalEvent, Queue}, strings, - ui::style::Theme, + ui::style::SharedTheme, }; use anyhow::Result; use asyncgit::{sync, AsyncLog, AsyncNotification, FetchStatus, CWD}; @@ -37,12 +37,14 @@ impl Revlog { pub fn new( queue: &Queue, sender: &Sender, - theme: &Theme, + theme: SharedTheme, ) -> Self { Self { queue: queue.clone(), commit_details: CommitDetailsComponent::new( - queue, sender, theme, + queue, + sender, + theme.clone(), ), list: CommitList::new(strings::LOG_TITLE, theme), git_log: AsyncLog::new(sender), diff --git a/src/tabs/stashing.rs b/src/tabs/stashing.rs index 602cee78..d54e79b3 100644 --- a/src/tabs/stashing.rs +++ b/src/tabs/stashing.rs @@ -8,7 +8,7 @@ use crate::{ keys, queue::{InternalEvent, Queue}, strings, - ui::style::Theme, + ui::style::SharedTheme, }; use anyhow::Result; use asyncgit::{ @@ -34,7 +34,7 @@ pub struct Stashing { index: FileTreeComponent, visible: bool, options: StashingOptions, - theme: Theme, + theme: SharedTheme, git_status: AsyncStatus, queue: Queue, } @@ -46,21 +46,21 @@ impl Stashing { pub fn new( sender: &Sender, queue: &Queue, - theme: &Theme, + theme: SharedTheme, ) -> Self { Self { index: FileTreeComponent::new( strings::STASHING_FILES_TITLE, true, Some(queue.clone()), - theme, + theme.clone(), ), visible: false, options: StashingOptions { keep_index: false, stash_untracked: true, }, - theme: *theme, + theme, git_status: AsyncStatus::new(sender.clone()), queue: queue.clone(), } diff --git a/src/tabs/stashlist.rs b/src/tabs/stashlist.rs index 3d3c2b9d..768f0f0a 100644 --- a/src/tabs/stashlist.rs +++ b/src/tabs/stashlist.rs @@ -6,7 +6,7 @@ use crate::{ keys, queue::{Action, InternalEvent, Queue}, strings, - ui::style::Theme, + ui::style::SharedTheme, }; use anyhow::Result; use asyncgit::sync; @@ -23,7 +23,7 @@ pub struct StashList { impl StashList { /// - pub fn new(queue: &Queue, theme: &Theme) -> Self { + pub fn new(queue: &Queue, theme: SharedTheme) -> Self { Self { visible: false, list: CommitList::new(strings::STASHLIST_TITLE, theme), diff --git a/src/tabs/status.rs b/src/tabs/status.rs index e1a7e289..dcbe211c 100644 --- a/src/tabs/status.rs +++ b/src/tabs/status.rs @@ -8,7 +8,7 @@ use crate::{ keys, queue::{InternalEvent, Queue, ResetItem}, strings, - ui::style::Theme, + ui::style::SharedTheme, }; use anyhow::Result; use asyncgit::{ @@ -107,7 +107,7 @@ impl Status { pub fn new( sender: &Sender, queue: &Queue, - theme: &Theme, + theme: SharedTheme, ) -> Self { Self { queue: queue.clone(), @@ -119,14 +119,14 @@ impl Status { true, true, queue.clone(), - theme, + theme.clone(), ), index: ChangesComponent::new( strings::TITLE_INDEX, false, false, queue.clone(), - theme, + theme.clone(), ), diff: DiffComponent::new(Some(queue.clone()), theme), git_diff: AsyncDiff::new(sender.clone()), diff --git a/src/ui/mod.rs b/src/ui/mod.rs index 03e426e8..6fb6b2cf 100644 --- a/src/ui/mod.rs +++ b/src/ui/mod.rs @@ -1,7 +1,7 @@ mod scrolllist; pub mod style; -use crate::ui::style::Theme; use scrolllist::ScrollableList; +use style::SharedTheme; use tui::{ backend::Backend, layout::{Constraint, Direction, Layout, Rect}, @@ -76,7 +76,7 @@ pub fn draw_list<'b, B: Backend, L>( items: L, select: Option, selected: bool, - theme: Theme, + theme: &SharedTheme, ) where L: Iterator>, { diff --git a/src/ui/style.rs b/src/ui/style.rs index 57559850..8e9f14e5 100644 --- a/src/ui/style.rs +++ b/src/ui/style.rs @@ -10,10 +10,13 @@ use std::path::PathBuf; use std::{ fs::File, io::{Read, Write}, + rc::Rc, }; use tui::style::{Color, Modifier, Style}; -#[derive(Serialize, Deserialize, Debug, Clone, Copy)] +pub type SharedTheme = Rc>; + +#[derive(Serialize, Deserialize, Debug)] pub struct Theme { #[serde(with = "ColorDef")] selected_tab: Color,