Skip to main content

gitkraft_gui/features/branches/
update.rs

1//! Update logic for branch-related messages.
2
3use iced::Task;
4
5use crate::message::Message;
6use crate::state::GitKraft;
7
8use super::commands;
9
10/// Handle all branch-related messages, returning a [`Task`] for any follow-up
11/// async work.
12pub fn update(state: &mut GitKraft, message: Message) -> Task<Message> {
13    match message {
14        Message::CheckoutBranch(name) => {
15            state.active_tab_mut().context_menu = None;
16            with_repo!(
17                state,
18                loading,
19                format!("Checking out '{name}'…"),
20                |repo_path| commands::checkout_branch(repo_path, name)
21            )
22        }
23
24        Message::BranchCheckedOut(result) => {
25            state.on_ok_refresh(result, "Branch checked out.", "Checkout failed")
26        }
27
28        Message::ToggleLocalBranches => {
29            let tab = state.active_tab_mut();
30            tab.local_branches_expanded = !tab.local_branches_expanded;
31            Task::none()
32        }
33
34        Message::ToggleRemoteBranches => {
35            let tab = state.active_tab_mut();
36            tab.remote_branches_expanded = !tab.remote_branches_expanded;
37            Task::none()
38        }
39
40        Message::ToggleBranchCreate => {
41            let tab = state.active_tab_mut();
42            tab.show_branch_create = !tab.show_branch_create;
43            if !tab.show_branch_create {
44                tab.new_branch_name.clear();
45            }
46            Task::none()
47        }
48
49        Message::NewBranchNameChanged(name) => {
50            state.active_tab_mut().new_branch_name = name;
51            Task::none()
52        }
53
54        Message::CreateBranch => {
55            let name = state.active_tab().new_branch_name.trim().to_string();
56            if name.is_empty() {
57                return Task::none();
58            }
59            with_repo!(
60                state,
61                loading,
62                format!("Creating branch '{name}'…"),
63                |repo_path| {
64                    let tab = state.active_tab_mut();
65                    tab.show_branch_create = false;
66                    tab.new_branch_name.clear();
67                    commands::create_branch(repo_path, name)
68                }
69            )
70        }
71
72        Message::BranchCreated(result) => {
73            state.on_ok_refresh(result, "Branch created.", "Branch creation failed")
74        }
75
76        Message::DeleteBranch(name) => {
77            state.active_tab_mut().context_menu = None;
78            with_repo!(
79                state,
80                loading,
81                format!("Deleting branch '{name}'…"),
82                |repo_path| commands::delete_branch(repo_path, name)
83            )
84        }
85
86        Message::BranchDeleted(result) => {
87            state.on_ok_refresh(result, "Branch deleted.", "Branch deletion failed")
88        }
89
90        _ => Task::none(),
91    }
92}