gitkraft 1.2.4

GitKraft — Git IDE desktop application (Iced GUI)
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
//! Sidebar branch list — shows local and remote branches, with checkout,
//! create, and delete actions.

use gitkraft_core::BranchType;
use iced::widget::{
    button, column, container, mouse_area, row, scrollable, text, text_input, Space,
};
use iced::{Alignment, Element, Length};

use crate::icons;
use crate::message::Message;
use crate::state::GitKraft;
use crate::theme;
use crate::view_utils;
use crate::view_utils::truncate_to_fit;

/// Render the branches sidebar panel.
pub(crate) fn view(state: &GitKraft) -> Element<'_, Message> {
    let tab = state.active_tab();
    let c = state.colors();
    let sidebar_width = state.sidebar_width;

    let header_icon = icon!(icons::GIT_BRANCH, 14, c.accent);

    let header_text = text("Branches").size(14).color(c.text_primary);

    let toggle_icon_char = if tab.show_branch_create {
        icons::DASH_CIRCLE
    } else {
        icons::PLUS_CIRCLE
    };
    let toggle_icon = icon!(toggle_icon_char, 14, c.accent);

    let toggle_btn = button(toggle_icon)
        .padding([2, 6])
        .style(theme::icon_button)
        .on_press(Message::ToggleBranchCreate);

    let header_row = row![
        header_icon,
        Space::new().width(6),
        header_text,
        Space::new().width(Length::Fill),
        toggle_btn,
    ]
    .align_y(Alignment::Center)
    .padding([8, 10]);

    // ── New branch form ───────────────────────────────────────────────────
    let create_form: Element<'_, Message> = if tab.show_branch_create {
        let input = text_input("new-branch-name", &tab.new_branch_name)
            .on_input(Message::NewBranchNameChanged)
            .padding(6)
            .size(13);

        let validation = gitkraft_core::validate_ref_name(&tab.new_branch_name);
        let can_create = validation.is_ok() && !tab.new_branch_name.trim().is_empty();

        let create_msg = can_create.then_some(Message::CreateBranch);
        let create_btn = view_utils::on_press_maybe(
            button(text("Create").size(13))
                .padding([4, 10])
                .style(theme::toolbar_button),
            create_msg,
        );

        // Show validation error inline
        let error_hint: Element<'_, Message> = if !tab.new_branch_name.is_empty() {
            if let Err(err) = validation {
                text(err).size(10).color(c.red).into()
            } else {
                Space::new().height(0).into()
            }
        } else {
            Space::new().height(0).into()
        };

        container(
            column![input, create_btn, error_hint]
                .spacing(4)
                .width(Length::Fill),
        )
        .padding([4, 10])
        .into()
    } else {
        Space::new().into()
    };

    // ── Inline rename form ────────────────────────────────────────────────
    let rename_form: Element<'_, Message> = if let Some(ref orig) = tab.rename_branch_target {
        let input = iced::widget::text_input("new branch name", &tab.rename_branch_input)
            .on_input(Message::RenameBranchInputChanged)
            .on_submit(Message::ConfirmRenameBranch)
            .padding(6)
            .size(13);

        let rename_validation = gitkraft_core::validate_ref_name(&tab.rename_branch_input);
        let rename_enabled = rename_validation.is_ok()
            && !tab.rename_branch_input.trim().is_empty()
            && tab.rename_branch_input.trim() != orig.as_str();
        let confirm_btn = view_utils::on_press_maybe(
            button(text("Rename").size(13))
                .padding([4, 10])
                .style(theme::toolbar_button),
            rename_enabled.then_some(Message::ConfirmRenameBranch),
        );

        let cancel_btn = button(text("Cancel").size(13))
            .padding([4, 10])
            .style(theme::toolbar_button)
            .on_press(Message::CancelRename);

        let hint = text(format!("Renaming '{orig}'")).size(11).color(c.muted);

        let rename_error_hint: Element<'_, Message> = if !tab.rename_branch_input.is_empty() {
            if let Err(err) = rename_validation {
                text(err).size(10).color(c.red).into()
            } else {
                Space::new().height(0).into()
            }
        } else {
            Space::new().height(0).into()
        };

        container(
            column![
                hint,
                input,
                row![confirm_btn, Space::new().width(4), cancel_btn],
                rename_error_hint,
            ]
            .spacing(4)
            .width(Length::Fill),
        )
        .padding([4, 10])
        .into()
    } else {
        Space::new().into()
    };

    // ── Tag creation form ─────────────────────────────────────────────────
    let tag_form: Element<'_, Message> = if let Some(ref oid) = tab.create_tag_target_oid {
        let short_oid = gitkraft_core::utils::short_oid_str(oid);
        let label = if tab.create_tag_annotated {
            format!("Creating annotated tag at {short_oid}")
        } else {
            format!("Creating lightweight tag at {short_oid}")
        };
        let hint = text(label).size(11).color(c.muted);

        let name_input = iced::widget::text_input("tag-name", &tab.create_tag_name)
            .on_input(Message::TagNameChanged)
            .on_submit(Message::ConfirmCreateTag)
            .padding(6)
            .size(13);

        let tag_validation = gitkraft_core::validate_ref_name(&tab.create_tag_name);
        let can_create_tag = tag_validation.is_ok() && !tab.create_tag_name.trim().is_empty();
        let tag_msg = can_create_tag.then_some(Message::ConfirmCreateTag);
        let confirm_btn = view_utils::on_press_maybe(
            button(text("Create tag").size(13))
                .padding([4, 10])
                .style(theme::toolbar_button),
            tag_msg,
        );

        let cancel_btn = button(text("Cancel").size(13))
            .padding([4, 10])
            .style(theme::toolbar_button)
            .on_press(Message::CancelCreateTag);

        let tag_error_hint: Element<'_, Message> = if !tab.create_tag_name.is_empty() {
            if let Err(err) = tag_validation {
                text(err).size(10).color(c.red).into()
            } else {
                Space::new().height(0).into()
            }
        } else {
            Space::new().height(0).into()
        };

        let mut form_col = column![hint, name_input].spacing(4).width(Length::Fill);

        if tab.create_tag_annotated {
            let msg_input = iced::widget::text_input("tag message", &tab.create_tag_message)
                .on_input(Message::TagMessageChanged)
                .padding(6)
                .size(13);
            form_col = form_col.push(msg_input);
        }

        form_col = form_col.push(row![confirm_btn, Space::new().width(4), cancel_btn]);
        form_col = form_col.push(tag_error_hint);

        container(form_col).padding([4, 10]).into()
    } else {
        Space::new().into()
    };

    // ── Create branch at commit form ──────────────────────────────────────
    let create_branch_at_form: Element<'_, Message> = if let Some(ref oid) =
        tab.create_branch_at_oid
    {
        let short_oid = gitkraft_core::utils::short_oid_str(oid);
        let hint = text(format!("Creating branch at {short_oid}"))
            .size(11)
            .color(c.muted);

        let name_input = iced::widget::text_input("branch-name", &tab.new_branch_name)
            .on_input(Message::NewBranchNameChanged)
            .on_submit(Message::ConfirmCreateBranchAtCommit)
            .padding(6)
            .size(13);

        let branch_at_validation = gitkraft_core::validate_ref_name(&tab.new_branch_name);
        let can_create_at = branch_at_validation.is_ok() && !tab.new_branch_name.trim().is_empty();
        let confirm_msg = can_create_at.then_some(Message::ConfirmCreateBranchAtCommit);
        let confirm_btn = view_utils::on_press_maybe(
            button(text("Create").size(13))
                .padding([4, 10])
                .style(theme::toolbar_button),
            confirm_msg,
        );

        let cancel_btn = button(text("Cancel").size(13))
            .padding([4, 10])
            .style(theme::toolbar_button)
            .on_press(Message::CancelCreateBranchAtCommit);

        let branch_at_error_hint: Element<'_, Message> = if !tab.new_branch_name.is_empty() {
            if let Err(err) = branch_at_validation {
                text(err).size(10).color(c.red).into()
            } else {
                Space::new().height(0).into()
            }
        } else {
            Space::new().height(0).into()
        };

        container(
            column![
                hint,
                name_input,
                row![confirm_btn, Space::new().width(4), cancel_btn],
                branch_at_error_hint,
            ]
            .spacing(4)
            .width(Length::Fill),
        )
        .padding([4, 10])
        .into()
    } else {
        Space::new().into()
    };

    // ── Branch list ───────────────────────────────────────────────────────
    let local_branches: Vec<Element<'_, Message>> = tab
        .branches
        .iter()
        .filter(|b| b.branch_type == BranchType::Local)
        .enumerate()
        .map(|(local_index, branch)| {
            let is_current = branch.is_head;

            let indicator: Element<'_, Message> = if is_current {
                icon!(icons::CHECK_CIRCLE_FILL, 12, c.green).into()
            } else {
                icon!(icons::GIT_BRANCH, 12, c.muted).into()
            };

            let name_color = if is_current { c.green } else { c.text_primary };

            // Available px: sidebar minus button padding(16) + indicator(14)
            // + gap(6) + delete-btn(28) + row-spacing(2) ≈ 66 px overhead.
            let name_available = (sidebar_width - 66.0).max(20.0);
            let display_name = truncate_to_fit(branch.name.as_str(), name_available, 7.5);

            let name_label = text(display_name)
                .size(13)
                .color(name_color)
                .wrapping(iced::widget::text::Wrapping::None);

            // Upstream ahead/behind badge
            let upstream_badge: Element<'_, Message> =
                if let Some(status) = branch.upstream_status() {
                    let badge_color = if status == "✓" {
                        c.green
                    } else if status.contains('↓') && !status.contains('↑') {
                        c.red
                    } else if status.contains('↑') && !status.contains('↓') {
                        c.green
                    } else {
                        c.yellow // both ahead and behind
                    };
                    container(
                        text(status)
                            .size(10)
                            .color(badge_color)
                            .font(iced::Font::MONOSPACE),
                    )
                    .padding([1, 4])
                    .into()
                } else {
                    Space::new().width(0).into()
                };

            // Always give the button an on_press so Iced keeps it in the
            // Active/Hovered state machine.  For the current branch we use
            // Noop (can't checkout the branch you're already on), but the
            // button still receives hover events — fixing the missing highlight.
            let checkout_msg = if is_current {
                Message::Noop
            } else {
                Message::CheckoutBranch(branch.name.clone())
            };
            let checkout_btn = button(
                row![
                    indicator,
                    Space::new().width(6),
                    name_label,
                    Space::new().width(4),
                    upstream_badge,
                ]
                .align_y(Alignment::Center),
            )
            .padding([4, 8])
            .width(Length::Fill)
            .style(theme::ghost_button)
            .on_press(checkout_msg);

            let delete_icon = icon!(icons::TRASH, 12, c.red);

            let delete_msg = (!is_current).then_some(Message::DeleteBranch(branch.name.clone()));
            let delete_btn = view_utils::on_press_maybe(
                button(delete_icon)
                    .padding([4, 6])
                    .style(theme::icon_button)
                    // Hide the delete button entirely for the current branch —
                    // visually cleaner since it can't be deleted anyway.
                    .width(if is_current {
                        Length::Fixed(0.0)
                    } else {
                        Length::Shrink
                    }),
                delete_msg,
            );

            let branch_row = row![checkout_btn, delete_btn]
                .spacing(2)
                .align_y(Alignment::Center)
                .width(Length::Fill);

            mouse_area(
                container(branch_row)
                    .width(Length::Fill)
                    .height(Length::Fixed(28.0))
                    .clip(true),
            )
            .on_right_press(Message::OpenBranchContextMenu(
                branch.name.clone(),
                local_index,
                branch.is_head,
            ))
            .into()
        })
        .collect();

    // ── Remote branches (with context menu) ───────────────────────────────
    let remote_branches: Vec<Element<'_, Message>> = tab
        .branches
        .iter()
        .filter(|b| b.branch_type == BranchType::Remote)
        .map(|branch| {
            let icon = icon!(icons::CLOUD, 12, c.muted);

            // Available px: sidebar minus item padding(16) + icon(14) + gap(6)
            // ≈ 36 px overhead.
            let label_available = (sidebar_width - 36.0).max(20.0);
            let display_remote = truncate_to_fit(branch.name.as_str(), label_available, 7.0);

            let label = text(display_remote)
                .size(12)
                .color(c.text_secondary)
                .wrapping(iced::widget::text::Wrapping::None);

            let branch_btn =
                button(row![icon, Space::new().width(6), label].align_y(Alignment::Center))
                    .padding([2, 8])
                    .width(Length::Fill)
                    .style(theme::ghost_button)
                    .on_press(Message::CheckoutRemoteBranch(branch.name.clone()));

            mouse_area(
                container(branch_btn)
                    .width(Length::Fill)
                    .height(Length::Fixed(22.0))
                    .clip(true),
            )
            .on_right_press(Message::OpenRemoteBranchContextMenu(branch.name.clone()))
            .into()
        })
        .collect();

    let mut list_col = column![].spacing(1).width(Length::Fill);

    if !local_branches.is_empty() || tab.local_branches_expanded {
        let local_count = tab
            .branches
            .iter()
            .filter(|b| b.branch_type == BranchType::Local)
            .count();

        let local_header_btn = view_utils::collapsible_header(
            tab.local_branches_expanded,
            "Local",
            local_count,
            Message::ToggleLocalBranches,
            c.muted,
        );
        list_col = list_col.push(local_header_btn);

        if tab.local_branches_expanded {
            for item in local_branches {
                list_col = list_col.push(item);
            }
        }
    }

    if !remote_branches.is_empty() || tab.remote_branches_expanded {
        let remote_count = tab
            .branches
            .iter()
            .filter(|b| b.branch_type == BranchType::Remote)
            .count();

        list_col = list_col.push(Space::new().height(4));

        let remote_header_btn = view_utils::collapsible_header(
            tab.remote_branches_expanded,
            "Remote",
            remote_count,
            Message::ToggleRemoteBranches,
            c.muted,
        );
        list_col = list_col.push(remote_header_btn);

        if tab.remote_branches_expanded {
            for item in remote_branches {
                list_col = list_col.push(item);
            }
        }
    }

    let content = column![
        header_row,
        create_form,
        rename_form,
        tag_form,
        create_branch_at_form,
        scrollable(list_col)
            .height(Length::Fill)
            .direction(view_utils::thin_scrollbar())
            .style(crate::theme::overlay_scrollbar),
    ]
    .width(Length::Fill)
    .height(Length::Fill);

    container(content)
        .width(Length::Fill)
        .height(Length::Fill)
        .style(theme::sidebar_style)
        .into()
}