share theme instead of copying it all over the place

This commit is contained in:
Stephan Dilly 2020-06-22 12:21:21 +02:00
parent 79b8bdfae2
commit b899751c2b
21 changed files with 107 additions and 86 deletions

View file

@ -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<AsyncNotification>) -> 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,
}

View file

@ -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<DrawListEntry>,
cmd_infos: Vec<CommandInfo>,
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,

View file

@ -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(),

View file

@ -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,

View file

@ -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<CommitDetails>,
tags: Vec<String>,
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,
}
}

View file

@ -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<AsyncNotification>,
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(
"",

View file

@ -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<Tags>,
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),
}
}

View file

@ -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<Queue>,
theme: Theme,
theme: SharedTheme,
}
impl DiffComponent {
///
pub fn new(queue: Option<Queue>, theme: &Theme) -> Self {
pub fn new(queue: Option<Queue>, 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);

View file

@ -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<Queue>,
theme: Theme,
theme: SharedTheme,
}
impl FileTreeComponent {
@ -37,7 +36,7 @@ impl FileTreeComponent {
title: &str,
focus: bool,
queue: Option<Queue>,
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<Text> {
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(())

View file

@ -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<CommandInfo>,
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,
}
}
///

View file

@ -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<AsyncNotification>,
theme: &Theme,
theme: SharedTheme,
) -> Self {
Self {
details: CommitDetailsComponent::new(
queue, sender, theme,
queue,
sender,
theme.clone(),
),
diff: DiffComponent::new(None, theme),
commit_id: None,

View file

@ -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,
}
}
///

View file

@ -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<Action>,
visible: bool,
queue: Queue,
theme: Theme,
theme: Rc<Box<Theme>>,
}
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<Box<Theme>>) -> Self {
Self {
target: None,
visible: false,
queue,
theme: *theme,
theme,
}
}
///

View file

@ -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,

View file

@ -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,

View file

@ -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<AsyncNotification>,
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),

View file

@ -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<AsyncNotification>,
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(),
}

View file

@ -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),

View file

@ -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<AsyncNotification>,
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()),

View file

@ -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<usize>,
selected: bool,
theme: Theme,
theme: &SharedTheme,
) where
L: Iterator<Item = Text<'b>>,
{

View file

@ -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<Box<Theme>>;
#[derive(Serialize, Deserialize, Debug)]
pub struct Theme {
#[serde(with = "ColorDef")]
selected_tab: Color,