|
| 1 | +/* |
| 2 | + * Copyright 2026 elementary, Inc. (https://elementary.io) |
| 3 | + * SPDX-License-Identifier: GPL-3.0-or-later |
| 4 | + * |
| 5 | + * Authored by: Leonhard Kargl <leo.kargl@proton.me> |
| 6 | + */ |
| 7 | + |
| 8 | +public class Gala.Daemon.CandidateArea : Granite.Bin { |
| 9 | + private const string[] DEFAULT_LABELS = { |
| 10 | + "1", "2", "3", "4", "5", "6", "7", "8", "9", "0", "a", "b", "c", "d", "e", "f" |
| 11 | + }; |
| 12 | + |
| 13 | + public IBus.PanelService service { get; construct; } |
| 14 | + |
| 15 | + private ListStore model; |
| 16 | + private Gtk.SingleSelection selection_model; |
| 17 | + private Gtk.ListView list_view; |
| 18 | + |
| 19 | + private Gtk.Button prev_page_button; |
| 20 | + private Gtk.Button next_page_button; |
| 21 | + |
| 22 | + private Granite.Box content_box; |
| 23 | + |
| 24 | + public CandidateArea (IBus.PanelService service) { |
| 25 | + Object (service: service); |
| 26 | + } |
| 27 | + |
| 28 | + construct { |
| 29 | + model = new ListStore (typeof (Candidate)); |
| 30 | + |
| 31 | + selection_model = new Gtk.SingleSelection (model); |
| 32 | + |
| 33 | + var factory = new Gtk.SignalListItemFactory (); |
| 34 | + factory.setup.connect (on_setup); |
| 35 | + factory.bind.connect (on_bind); |
| 36 | + |
| 37 | + list_view = new Gtk.ListView (selection_model, factory); |
| 38 | + |
| 39 | + prev_page_button = new Gtk.Button (); |
| 40 | + prev_page_button.clicked.connect (service.page_up); |
| 41 | + |
| 42 | + next_page_button = new Gtk.Button (); |
| 43 | + next_page_button.clicked.connect (service.page_down); |
| 44 | + |
| 45 | + var button_box = new Granite.Box (HORIZONTAL, LINKED) { |
| 46 | + hexpand = true |
| 47 | + }; |
| 48 | + button_box.append (prev_page_button); |
| 49 | + button_box.append (next_page_button); |
| 50 | + |
| 51 | + content_box = new Granite.Box (VERTICAL); |
| 52 | + content_box.append (list_view); |
| 53 | + content_box.append (button_box); |
| 54 | + |
| 55 | + child = content_box; |
| 56 | + } |
| 57 | + |
| 58 | + private void on_setup (Object obj) { |
| 59 | + var item = (Gtk.ListItem) obj; |
| 60 | + item.child = new CandidateBox (service, item); |
| 61 | + } |
| 62 | + |
| 63 | + private void on_bind (Object obj) { |
| 64 | + var item = (Gtk.ListItem) obj; |
| 65 | + var candidate = (Candidate) item.item; |
| 66 | + |
| 67 | + var box = (CandidateBox) item.child; |
| 68 | + box.bind_candidate (candidate); |
| 69 | + } |
| 70 | + |
| 71 | + public void update (IBus.LookupTable table) { |
| 72 | + model.remove_all (); |
| 73 | + |
| 74 | + if (table.get_orientation () == IBus.Orientation.HORIZONTAL) { |
| 75 | + update_orientation (HORIZONTAL); |
| 76 | + } else { /* VERTICAL or SYSTEM */ |
| 77 | + update_orientation (VERTICAL); |
| 78 | + } |
| 79 | + |
| 80 | + var n_candidates = table.get_number_of_candidates (); |
| 81 | + var page_size = table.get_page_size (); |
| 82 | + var cursor_pos = table.get_cursor_pos (); |
| 83 | + var page = (uint) (cursor_pos / page_size); |
| 84 | + |
| 85 | + var start_index = page * page_size; |
| 86 | + var end_index = uint.min (start_index + page_size, n_candidates); |
| 87 | + |
| 88 | + for (uint i = start_index; i < end_index; i++) { |
| 89 | + var label = table.get_label (i)?.text ?? ( |
| 90 | + i - start_index < DEFAULT_LABELS.length ? DEFAULT_LABELS[i - start_index] : null |
| 91 | + ); |
| 92 | + |
| 93 | + var candidate = table.get_candidate (i)?.text; |
| 94 | + |
| 95 | + model.append (new Candidate (label, candidate)); |
| 96 | + } |
| 97 | + |
| 98 | + selection_model.selected = table.get_cursor_in_page (); |
| 99 | + |
| 100 | + update_buttons (table.is_round (), page, (uint) ((n_candidates + page_size - 1) / page_size)); |
| 101 | + } |
| 102 | + |
| 103 | + private void update_orientation (Gtk.Orientation orientation) { |
| 104 | + content_box.orientation = orientation; |
| 105 | + list_view.orientation = orientation; |
| 106 | + |
| 107 | + if (orientation == HORIZONTAL) { |
| 108 | + prev_page_button.icon_name = "go-previous"; |
| 109 | + next_page_button.icon_name = "go-next"; |
| 110 | + } else { |
| 111 | + prev_page_button.icon_name = "go-up"; |
| 112 | + next_page_button.icon_name = "go-down"; |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + private void update_buttons (bool wraps_around, uint page, uint n_pages) { |
| 117 | + prev_page_button.sensitive = wraps_around || page > 0; |
| 118 | + next_page_button.sensitive = wraps_around || page < n_pages - 1; |
| 119 | + } |
| 120 | +} |
0 commit comments