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) => with_repo!(
15            state,
16            loading,
17            format!("Checking out '{name}'…"),
18            |repo_path| commands::checkout_branch(repo_path, name)
19        ),
20
21        Message::BranchCheckedOut(result) => {
22            state.on_ok_refresh(result, "Branch checked out.", "Checkout failed")
23        }
24
25        Message::ToggleLocalBranches => {
26            let tab = state.active_tab_mut();
27            tab.local_branches_expanded = !tab.local_branches_expanded;
28            Task::none()
29        }
30
31        Message::ToggleRemoteBranches => {
32            let tab = state.active_tab_mut();
33            tab.remote_branches_expanded = !tab.remote_branches_expanded;
34            Task::none()
35        }
36
37        Message::ToggleBranchCreate => {
38            let tab = state.active_tab_mut();
39            tab.show_branch_create = !tab.show_branch_create;
40            if !tab.show_branch_create {
41                tab.new_branch_name.clear();
42            }
43            Task::none()
44        }
45
46        Message::NewBranchNameChanged(name) => {
47            state.active_tab_mut().new_branch_name = name;
48            Task::none()
49        }
50
51        Message::CreateBranch => {
52            let name = state.active_tab().new_branch_name.trim().to_string();
53            if name.is_empty() {
54                return Task::none();
55            }
56            with_repo!(
57                state,
58                loading,
59                format!("Creating branch '{name}'…"),
60                |repo_path| {
61                    let tab = state.active_tab_mut();
62                    tab.show_branch_create = false;
63                    tab.new_branch_name.clear();
64                    commands::create_branch(repo_path, name)
65                }
66            )
67        }
68
69        Message::BranchCreated(result) => {
70            state.on_ok_refresh(result, "Branch created.", "Branch creation failed")
71        }
72
73        Message::DeleteBranch(name) => with_repo!(
74            state,
75            loading,
76            format!("Deleting branch '{name}'…"),
77            |repo_path| commands::delete_branch(repo_path, name)
78        ),
79
80        Message::BranchDeleted(result) => {
81            state.on_ok_refresh(result, "Branch deleted.", "Branch deletion failed")
82        }
83
84        _ => Task::none(),
85    }
86}