|
| 1 | +/* |
| 2 | + * Copyright 2025 elementary, Inc. <https://elementary.io> |
| 3 | + * SPDX-License-Identifier: GPL-3.0-or-later |
| 4 | +* |
| 5 | +* Authored by: Jeremy Wootten <jeremywootten@gmail.com> |
| 6 | +*/ |
| 7 | +private class Scratch.Dialogs.BranchListBox : Gtk.Bin { |
| 8 | + |
| 9 | + public signal void branch_changed (string branch_name); |
| 10 | + |
| 11 | + public string text { |
| 12 | + get { |
| 13 | + return search_entry.text; |
| 14 | + } |
| 15 | + } |
| 16 | + |
| 17 | + public bool show_remotes { get; construct;} |
| 18 | + public BranchActionDialog dialog { get; construct;} |
| 19 | + |
| 20 | + private Gtk.ListBox list_box; |
| 21 | + private Gtk.SearchEntry search_entry; |
| 22 | + private Gtk.Label local_header; |
| 23 | + private Gtk.Label remote_header; |
| 24 | + private Gtk.Label recent_header; |
| 25 | + |
| 26 | + public BranchListBox (BranchActionDialog dialog, bool show_remotes) { |
| 27 | + Object ( |
| 28 | + dialog: dialog, |
| 29 | + show_remotes: show_remotes |
| 30 | + ); |
| 31 | + } |
| 32 | + |
| 33 | + construct { |
| 34 | + list_box = new Gtk.ListBox () { |
| 35 | + activate_on_single_click = false |
| 36 | + }; |
| 37 | + |
| 38 | + var scrolled_window = new Gtk.ScrolledWindow (null, null) { |
| 39 | + hscrollbar_policy = NEVER, |
| 40 | + vscrollbar_policy = AUTOMATIC, |
| 41 | + min_content_height = 200, |
| 42 | + vexpand = true |
| 43 | + }; |
| 44 | + scrolled_window.child = list_box; |
| 45 | + |
| 46 | + search_entry = new Gtk.SearchEntry () { |
| 47 | + placeholder_text = _("Enter search term") |
| 48 | + }; |
| 49 | + |
| 50 | + var box = new Gtk.Box (VERTICAL, 6); |
| 51 | + box.add (search_entry); |
| 52 | + box.add (scrolled_window); |
| 53 | + |
| 54 | + child = box; |
| 55 | + |
| 56 | + recent_header = new Granite.HeaderLabel (_("Recent Branches")); |
| 57 | + local_header = new Granite.HeaderLabel (_("Local Branches")); |
| 58 | + remote_header = new Granite.HeaderLabel (_("Remote Branches")); |
| 59 | + var branch_refs = dialog.project.get_all_branch_refs (); |
| 60 | + |
| 61 | + foreach (var branch_ref in branch_refs) { |
| 62 | + if (branch_ref.is_branch () || show_remotes) { |
| 63 | + var row = new BranchNameRow (branch_ref); |
| 64 | + if (dialog.project.is_recent_ref (branch_ref)) { |
| 65 | + row.is_recent = true; |
| 66 | + } |
| 67 | + |
| 68 | + list_box.add (row); |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + list_box.set_sort_func (listbox_sort_func); |
| 73 | + list_box.set_header_func (listbox_header_func); |
| 74 | + list_box.row_selected.connect ((listboxrow) => { |
| 75 | + //We want cursor to end up after the inserted text |
| 76 | + search_entry.text = ((BranchNameRow)(listboxrow)).branch_name; |
| 77 | + search_entry.grab_focus_without_selecting (); |
| 78 | + search_entry.move_cursor (DISPLAY_LINE_ENDS, 1, false); |
| 79 | + }); |
| 80 | + list_box.row_activated.connect ((listboxrow) => { |
| 81 | + dialog.page_activated (); |
| 82 | + }); |
| 83 | + list_box.set_filter_func ((listboxrow) => { |
| 84 | + return (((BranchNameRow)(listboxrow)).branch_name.contains (search_entry.text)); |
| 85 | + }); |
| 86 | + |
| 87 | + search_entry.changed.connect (() => { |
| 88 | + list_box.invalidate_filter (); |
| 89 | + branch_changed (text); |
| 90 | + }); |
| 91 | + search_entry.activate.connect (() => { |
| 92 | + dialog.page_activated (); |
| 93 | + }); |
| 94 | + } |
| 95 | + |
| 96 | + public BranchNameRow? get_selected_row () { |
| 97 | + int index = 0; |
| 98 | + var row = list_box.get_row_at_index (index); |
| 99 | + while (row != null && |
| 100 | + ((BranchNameRow)row).branch_name != search_entry.text) { |
| 101 | + |
| 102 | + row = list_box.get_row_at_index (++index); |
| 103 | + } |
| 104 | + |
| 105 | + return (BranchNameRow)row; |
| 106 | + } |
| 107 | + |
| 108 | + |
| 109 | + private int listbox_sort_func (Gtk.ListBoxRow rowa, Gtk.ListBoxRow rowb) { |
| 110 | + var a = (BranchNameRow)(rowa); |
| 111 | + var b = (BranchNameRow)(rowb); |
| 112 | + |
| 113 | + if (a.is_recent && !b.is_recent) { |
| 114 | + return -1; |
| 115 | + } else if (b.is_recent && !a.is_recent) { |
| 116 | + return 1; |
| 117 | + } |
| 118 | + |
| 119 | + if (a.is_remote && !b.is_remote) { |
| 120 | + return 1; |
| 121 | + } else if (b.is_remote && !a.is_remote) { |
| 122 | + return -1; |
| 123 | + } |
| 124 | + |
| 125 | + return (a.branch_name.collate (b.branch_name)); |
| 126 | + } |
| 127 | + |
| 128 | + private void listbox_header_func (Gtk.ListBoxRow row, Gtk.ListBoxRow? row_before) { |
| 129 | + var a = (BranchNameRow)row; |
| 130 | + var b = (BranchNameRow?)row_before; |
| 131 | + a.set_header (null); |
| 132 | + if (b == null) { |
| 133 | + if (a.is_recent) { |
| 134 | + a.set_header (recent_header); |
| 135 | + } else if (!a.is_remote) { |
| 136 | + a.set_header (local_header); |
| 137 | + } else { |
| 138 | + a.set_header (remote_header); |
| 139 | + } |
| 140 | + } else if (b.is_recent) { |
| 141 | + if (!a.is_remote) { |
| 142 | + a.set_header (local_header); |
| 143 | + } else if (a.is_remote) { |
| 144 | + a.set_header (remote_header); |
| 145 | + } |
| 146 | + } else if (!b.is_remote) { |
| 147 | + if (a.is_remote) { |
| 148 | + a.set_header (remote_header); |
| 149 | + } |
| 150 | + } |
| 151 | + } |
| 152 | + |
| 153 | + public new void grab_focus () { |
| 154 | + search_entry.grab_focus (); |
| 155 | + } |
| 156 | +} |
0 commit comments