git_ui.rs

 1use ::settings::Settings;
 2use git::repository::GitFileStatus;
 3use gpui::{actions, AppContext, Context, Global, Hsla, Model};
 4use settings::GitPanelSettings;
 5use ui::{Color, Icon, IconName, IntoElement, SharedString};
 6
 7pub mod git_panel;
 8mod settings;
 9
10actions!(
11    git_ui,
12    [
13        StageAll,
14        UnstageAll,
15        RevertAll,
16        CommitStagedChanges,
17        CommitAllChanges,
18        ClearMessage
19    ]
20);
21
22pub fn init(cx: &mut AppContext) {
23    GitPanelSettings::register(cx);
24    let git_state = cx.new_model(|_cx| GitState::new());
25    cx.set_global(GlobalGitState(git_state));
26}
27
28struct GlobalGitState(Model<GitState>);
29
30impl Global for GlobalGitState {}
31
32pub struct GitState {
33    commit_message: Option<SharedString>,
34}
35
36impl GitState {
37    pub fn new() -> Self {
38        GitState {
39            commit_message: None,
40        }
41    }
42
43    pub fn set_message(&mut self, message: Option<SharedString>) {
44        self.commit_message = message;
45    }
46
47    pub fn clear_message(&mut self) {
48        self.commit_message = None;
49    }
50
51    pub fn get_global(cx: &mut AppContext) -> Model<GitState> {
52        cx.global::<GlobalGitState>().0.clone()
53    }
54}
55
56const ADDED_COLOR: Hsla = Hsla {
57    h: 142. / 360.,
58    s: 0.68,
59    l: 0.45,
60    a: 1.0,
61};
62const MODIFIED_COLOR: Hsla = Hsla {
63    h: 48. / 360.,
64    s: 0.76,
65    l: 0.47,
66    a: 1.0,
67};
68const REMOVED_COLOR: Hsla = Hsla {
69    h: 355. / 360.,
70    s: 0.65,
71    l: 0.65,
72    a: 1.0,
73};
74
75// TODO: Add updated status colors to theme
76pub fn git_status_icon(status: GitFileStatus) -> impl IntoElement {
77    match status {
78        GitFileStatus::Added | GitFileStatus::Untracked => {
79            Icon::new(IconName::SquarePlus).color(Color::Custom(ADDED_COLOR))
80        }
81        GitFileStatus::Modified => {
82            Icon::new(IconName::SquareDot).color(Color::Custom(MODIFIED_COLOR))
83        }
84        GitFileStatus::Conflict => Icon::new(IconName::Warning).color(Color::Custom(REMOVED_COLOR)),
85        GitFileStatus::Deleted => {
86            Icon::new(IconName::SquareMinus).color(Color::Custom(REMOVED_COLOR))
87        }
88    }
89}