| 1 |
# COPYRIGHT (C) 2023-2024 Nicotine+ Contributors |
| 2 |
# |
| 3 |
# GNU GENERAL PUBLIC LICENSE |
| 4 |
# Version 3, 29 June 2007 |
| 5 |
# |
| 6 |
# This program is free software: you can redistribute it and/or modify |
| 7 |
# it under the terms of the GNU General Public License as published by |
| 8 |
# the Free Software Foundation, either version 3 of the License, or |
| 9 |
# (at your option) any later version. |
| 10 |
# |
| 11 |
# This program is distributed in the hope that it will be useful, |
| 12 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 |
# GNU General Public License for more details. |
| 15 |
# |
| 16 |
# You should have received a copy of the GNU General Public License |
| 17 |
# along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 18 |
|
| 19 |
from gi.repository import GLib |
| 20 |
from gi.repository import Gtk |
| 21 |
from gi.repository import Pango |
| 22 |
|
| 23 |
from pynicotine.gtkgui.application import GTK_API_VERSION |
| 24 |
from pynicotine.gtkgui.widgets.accelerator import Accelerator |
| 25 |
from pynicotine.gtkgui.widgets.textentry import CompletionEntry |
| 26 |
from pynicotine.gtkgui.widgets.theme import add_css_class |
| 27 |
|
| 28 |
|
| 29 |
class ComboBox: |
| 30 |
|
| 31 |
def __init__(self, container, label=None, has_entry=False, has_entry_completion=False, |
| 32 |
entry=None, visible=True, items=None, item_selected_callback=None): |
| 33 |
|
| 34 |
self.widget = None |
| 35 |
self.dropdown = None |
| 36 |
self.entry = entry |
| 37 |
self.item_selected_callback = item_selected_callback |
| 38 |
|
| 39 |
self._ids = {} |
| 40 |
self._positions = {} |
| 41 |
self._model = None |
| 42 |
self._button = None |
| 43 |
self._popover = None |
| 44 |
self._entry_completion = None |
| 45 |
self._is_popup_visible = False |
| 46 |
|
| 47 |
self._create_combobox(container, label, has_entry, has_entry_completion) |
| 48 |
|
| 49 |
if items: |
| 50 |
self.freeze() |
| 51 |
|
| 52 |
for provided_item in items: |
| 53 |
if isinstance(provided_item, str): |
| 54 |
item = provided_item |
| 55 |
item_id = None |
| 56 |
else: |
| 57 |
item, item_id = provided_item |
| 58 |
|
| 59 |
self.append(item, item_id) |
| 60 |
|
| 61 |
self.unfreeze() |
| 62 |
|
| 63 |
self.set_visible(visible) |
| 64 |
|
| 65 |
def destroy(self): |
| 66 |
self.__dict__.clear() |
| 67 |
|
| 68 |
def _create_combobox_gtk4(self, container, label, has_entry): |
| 69 |
|
| 70 |
self._model = Gtk.StringList() |
| 71 |
self.dropdown = self._button = Gtk.DropDown( |
| 72 |
model=self._model, valign=Gtk.Align.CENTER, visible=True |
| 73 |
) |
| 74 |
|
| 75 |
list_factory = self.dropdown.get_factory() |
| 76 |
button_factory = None |
| 77 |
|
| 78 |
if not has_entry: |
| 79 |
button_factory = Gtk.SignalListItemFactory() |
| 80 |
|
| 81 |
button_factory.connect("setup", self._on_button_factory_setup) |
| 82 |
button_factory.connect("bind", self._on_button_factory_bind) |
| 83 |
|
| 84 |
self.dropdown.set_factory(button_factory) |
| 85 |
self.dropdown.set_list_factory(list_factory) |
| 86 |
|
| 87 |
self._popover = list(self.dropdown)[-1] |
| 88 |
self._popover.connect("notify::visible", self._on_dropdown_visible) |
| 89 |
|
| 90 |
try: |
| 91 |
scrollable = list(self._popover.get_child())[-1] |
| 92 |
list_view = scrollable.get_child() |
| 93 |
list_view.connect("activate", self._on_item_selected) |
| 94 |
|
| 95 |
except AttributeError: |
| 96 |
pass |
| 97 |
|
| 98 |
if not has_entry: |
| 99 |
self.widget = self.dropdown |
| 100 |
|
| 101 |
if label: |
| 102 |
label.set_mnemonic_widget(self.widget) |
| 103 |
|
| 104 |
container.append(self.widget) |
| 105 |
return |
| 106 |
|
| 107 |
list_factory = Gtk.SignalListItemFactory() |
| 108 |
|
| 109 |
list_factory.connect("setup", self._on_entry_list_factory_setup) |
| 110 |
list_factory.connect("bind", self._on_entry_list_factory_bind) |
| 111 |
|
| 112 |
self.dropdown.set_list_factory(list_factory) |
| 113 |
|
| 114 |
self.widget = Gtk.Box(valign=Gtk.Align.CENTER, visible=True) |
| 115 |
self._popover.connect("map", self._on_dropdown_map) |
| 116 |
|
| 117 |
if self.entry is None: |
| 118 |
self.entry = Gtk.Entry(hexpand=True, width_chars=8, visible=True) |
| 119 |
|
| 120 |
if label: |
| 121 |
label.set_mnemonic_widget(self.entry) |
| 122 |
|
| 123 |
self._button.set_sensitive(False) |
| 124 |
|
| 125 |
self.widget.append(self.entry) |
| 126 |
self.widget.append(self.dropdown) |
| 127 |
|
| 128 |
add_css_class(self.widget, "linked") |
| 129 |
add_css_class(self.dropdown, "entry") |
| 130 |
container.append(self.widget) |
| 131 |
|
| 132 |
def _create_combobox_gtk3(self, container, label, has_entry, has_entry_completion): |
| 133 |
|
| 134 |
self.dropdown = self.widget = Gtk.ComboBoxText(has_entry=has_entry, valign=Gtk.Align.CENTER, visible=True) |
| 135 |
self._model = self.dropdown.get_model() |
| 136 |
|
| 137 |
self.dropdown.connect("scroll-event", self._on_button_scroll_event) |
| 138 |
self.dropdown.connect("notify::active", self._on_item_selected) |
| 139 |
self.dropdown.connect("notify::popup-shown", self._on_dropdown_visible) |
| 140 |
|
| 141 |
if label: |
| 142 |
label.set_mnemonic_widget(self.widget) |
| 143 |
|
| 144 |
if not has_entry: |
| 145 |
for cell in self.dropdown.get_cells(): |
| 146 |
cell.props.ellipsize = Pango.EllipsizeMode.END |
| 147 |
|
| 148 |
container.add(self.widget) |
| 149 |
return |
| 150 |
|
| 151 |
if has_entry_completion: |
| 152 |
add_css_class(self.dropdown, "dropdown-scrollbar") |
| 153 |
|
| 154 |
if self.entry is None: |
| 155 |
self.entry = self.dropdown.get_child() |
| 156 |
self.entry.set_width_chars(8) |
| 157 |
else: |
| 158 |
self.dropdown.get_child().destroy() |
| 159 |
self.dropdown.add(self.entry) # pylint: disable=no-member |
| 160 |
|
| 161 |
self._button = list(self.entry.get_parent())[-1] |
| 162 |
container.add(self.widget) |
| 163 |
|
| 164 |
def _create_combobox(self, container, label, has_entry, has_entry_completion): |
| 165 |
|
| 166 |
if GTK_API_VERSION >= 4: |
| 167 |
self._create_combobox_gtk4(container, label, has_entry) |
| 168 |
else: |
| 169 |
self._create_combobox_gtk3(container, label, has_entry, has_entry_completion) |
| 170 |
|
| 171 |
if has_entry: |
| 172 |
Accelerator("Up", self.entry, self._on_arrow_key_accelerator, "up") |
| 173 |
Accelerator("Down", self.entry, self._on_arrow_key_accelerator, "down") |
| 174 |
|
| 175 |
if has_entry_completion: |
| 176 |
self._entry_completion = CompletionEntry(self.entry) |
| 177 |
|
| 178 |
def _update_item_entry_text(self): |
| 179 |
"""Set text entry text to the same value as selected item.""" |
| 180 |
|
| 181 |
if GTK_API_VERSION >= 4: |
| 182 |
item = self.dropdown.get_selected_item() |
| 183 |
|
| 184 |
if item is None: |
| 185 |
return |
| 186 |
|
| 187 |
item_text = item.get_string() |
| 188 |
|
| 189 |
if self.get_text() != item_text: |
| 190 |
self.set_text(item_text) |
| 191 |
|
| 192 |
self.entry.set_position(-1) |
| 193 |
|
| 194 |
def _update_item_positions(self, start_position, added=False): |
| 195 |
|
| 196 |
if added: |
| 197 |
end_position = self.get_num_items() + 1 |
| 198 |
else: |
| 199 |
end_position = self.get_num_items() |
| 200 |
|
| 201 |
new_ids = {} |
| 202 |
|
| 203 |
for position in range(start_position, end_position): |
| 204 |
if added: |
| 205 |
item_id = self._ids[position - 1] |
| 206 |
else: |
| 207 |
item_id = self._ids.pop(position + 1) |
| 208 |
|
| 209 |
new_ids[position] = item_id |
| 210 |
self._positions[item_id] = position |
| 211 |
|
| 212 |
self._ids.update(new_ids) |
| 213 |
|
| 214 |
# General # |
| 215 |
|
| 216 |
def freeze(self): |
| 217 |
"""Called before inserting/deleting items, to avoid redundant UI updates.""" |
| 218 |
|
| 219 |
if GTK_API_VERSION >= 4: |
| 220 |
self.dropdown.set_model(None) |
| 221 |
|
| 222 |
def unfreeze(self): |
| 223 |
"""Called after items have been inserted/deleted, to enable UI updates.""" |
| 224 |
|
| 225 |
if GTK_API_VERSION >= 4: |
| 226 |
self.dropdown.set_model(self._model) |
| 227 |
|
| 228 |
def insert(self, position, item, item_id=None): |
| 229 |
|
| 230 |
if item_id is None: |
| 231 |
item_id = item |
| 232 |
|
| 233 |
if item_id in self._positions: |
| 234 |
return |
| 235 |
|
| 236 |
last_position = self.get_num_items() |
| 237 |
|
| 238 |
if position == -1: |
| 239 |
position = last_position |
| 240 |
|
| 241 |
if GTK_API_VERSION >= 4: |
| 242 |
if last_position == position: |
| 243 |
self._model.append(item) |
| 244 |
else: |
| 245 |
num_removals = (last_position - position) |
| 246 |
inserted_items = [item] + [self._model.get_string(i) for i in range(position, last_position)] |
| 247 |
self._model.splice(position, num_removals, inserted_items) |
| 248 |
else: |
| 249 |
self.dropdown.insert_text(position, item) |
| 250 |
|
| 251 |
if self.entry and not self._positions: |
| 252 |
self._button.set_sensitive(True) |
| 253 |
|
| 254 |
if self._entry_completion: |
| 255 |
self._entry_completion.add_completion(item) |
| 256 |
|
| 257 |
self._update_item_positions(start_position=(position + 1), added=True) |
| 258 |
|
| 259 |
self._ids[position] = item_id |
| 260 |
self._positions[item_id] = position |
| 261 |
|
| 262 |
def append(self, item, item_id=None): |
| 263 |
self.insert(position=-1, item=item, item_id=item_id) |
| 264 |
|
| 265 |
def prepend(self, item, item_id=None): |
| 266 |
self.insert(position=0, item=item, item_id=item_id) |
| 267 |
|
| 268 |
def get_num_items(self): |
| 269 |
return len(self._positions) |
| 270 |
|
| 271 |
def get_selected_pos(self): |
| 272 |
|
| 273 |
if GTK_API_VERSION >= 4: |
| 274 |
return self.dropdown.get_selected() |
| 275 |
|
| 276 |
return self.dropdown.get_active() |
| 277 |
|
| 278 |
def get_selected_id(self): |
| 279 |
return self._ids.get(self.get_selected_pos()) |
| 280 |
|
| 281 |
def get_text(self): |
| 282 |
|
| 283 |
if self.entry: |
| 284 |
return self.entry.get_text() |
| 285 |
|
| 286 |
return self.get_selected_id() |
| 287 |
|
| 288 |
def set_selected_pos(self, position): |
| 289 |
|
| 290 |
if position is None: |
| 291 |
return |
| 292 |
|
| 293 |
if self.get_selected_pos() == position: |
| 294 |
return |
| 295 |
|
| 296 |
if GTK_API_VERSION >= 4: |
| 297 |
self.dropdown.set_selected(position) |
| 298 |
else: |
| 299 |
self.dropdown.set_active(position) |
| 300 |
|
| 301 |
def set_selected_id(self, item_id): |
| 302 |
self.set_selected_pos(self._positions.get(item_id)) |
| 303 |
|
| 304 |
def set_text(self, text): |
| 305 |
|
| 306 |
if self.entry: |
| 307 |
self.entry.set_text(text) |
| 308 |
return |
| 309 |
|
| 310 |
self.set_selected_id(text) |
| 311 |
|
| 312 |
def remove_pos(self, position): |
| 313 |
|
| 314 |
if position == -1: |
| 315 |
position = (self.get_num_items() - 1) |
| 316 |
|
| 317 |
item_id = self._ids.pop(position, None) |
| 318 |
|
| 319 |
if item_id is None: |
| 320 |
return |
| 321 |
|
| 322 |
if GTK_API_VERSION >= 4: |
| 323 |
self._model.remove(position) |
| 324 |
else: |
| 325 |
self.dropdown.remove(position) |
| 326 |
|
| 327 |
if self.entry and not self._ids: |
| 328 |
self._button.set_sensitive(False) |
| 329 |
|
| 330 |
if self._entry_completion: |
| 331 |
self._entry_completion.remove_completion(item_id) |
| 332 |
|
| 333 |
# Update positions for items after the removed one |
| 334 |
self._positions.pop(item_id, None) |
| 335 |
self._update_item_positions(start_position=position) |
| 336 |
|
| 337 |
def remove_id(self, item_id): |
| 338 |
position = self._positions.get(item_id) |
| 339 |
self.remove_pos(position) |
| 340 |
|
| 341 |
def clear(self): |
| 342 |
|
| 343 |
self._ids.clear() |
| 344 |
self._positions.clear() |
| 345 |
|
| 346 |
if GTK_API_VERSION >= 4: |
| 347 |
self._model.splice(position=0, n_removals=self._model.get_n_items()) |
| 348 |
else: |
| 349 |
self.dropdown.remove_all() |
| 350 |
|
| 351 |
if self.entry and self._button: |
| 352 |
self._button.set_sensitive(False) |
| 353 |
|
| 354 |
if self._entry_completion: |
| 355 |
self._entry_completion.clear() |
| 356 |
|
| 357 |
def grab_focus(self): |
| 358 |
self.entry.grab_focus() |
| 359 |
|
| 360 |
def set_visible(self, visible): |
| 361 |
self.widget.set_visible(visible) |
| 362 |
|
| 363 |
# Callbacks # |
| 364 |
|
| 365 |
def _on_button_factory_bind(self, _factory, list_item): |
| 366 |
label = list_item.get_child() |
| 367 |
label.set_text(list_item.get_item().get_string()) |
| 368 |
|
| 369 |
def _on_button_factory_setup(self, _factory, list_item): |
| 370 |
list_item.set_child( |
| 371 |
Gtk.Label(ellipsize=Pango.EllipsizeMode.END, mnemonic_widget=self.widget, xalign=0)) |
| 372 |
|
| 373 |
def _on_entry_list_factory_bind(self, _factory, list_item): |
| 374 |
label = list_item.get_child() |
| 375 |
label.set_text(list_item.get_item().get_string()) |
| 376 |
|
| 377 |
def _on_entry_list_factory_setup(self, _factory, list_item): |
| 378 |
list_item.set_child( |
| 379 |
Gtk.Label(ellipsize=Pango.EllipsizeMode.END, xalign=0)) |
| 380 |
|
| 381 |
def _on_arrow_key_accelerator(self, _widget, _unused, direction): |
| 382 |
|
| 383 |
if GTK_API_VERSION == 3: |
| 384 |
# Gtk.ComboBox already supports this functionality |
| 385 |
return False |
| 386 |
|
| 387 |
if not self._positions: |
| 388 |
return False |
| 389 |
|
| 390 |
if self._entry_completion: |
| 391 |
completion_popover = list(self.entry)[-1] |
| 392 |
|
| 393 |
if completion_popover.get_visible(): |
| 394 |
# Completion popup takes precedence |
| 395 |
return False |
| 396 |
|
| 397 |
current_position = self._positions.get(self.get_text(), -1) |
| 398 |
|
| 399 |
if direction == "up": |
| 400 |
new_position = max(0, current_position - 1) |
| 401 |
else: |
| 402 |
new_position = min(current_position + 1, len(self._positions) - 1) |
| 403 |
|
| 404 |
self.set_selected_pos(new_position) |
| 405 |
self._update_item_entry_text() |
| 406 |
return True |
| 407 |
|
| 408 |
def _on_button_scroll_event(self, widget, event, *_args): |
| 409 |
"""Prevent scrolling and pass scroll event to parent scrollable (GTK 3)""" |
| 410 |
|
| 411 |
scrollable = widget.get_ancestor(Gtk.ScrolledWindow) |
| 412 |
|
| 413 |
if scrollable is not None: |
| 414 |
scrollable.event(event) |
| 415 |
|
| 416 |
return True |
| 417 |
|
| 418 |
def _on_select_callback_status(self, enabled): |
| 419 |
self._is_popup_visible = enabled |
| 420 |
|
| 421 |
def _on_dropdown_map(self, *_args): |
| 422 |
|
| 423 |
# Align dropdown with entry and button |
| 424 |
popover_content = next(iter(self._popover)) |
| 425 |
container_width = self.entry.get_parent().get_width() |
| 426 |
button_width = self._button.get_width() |
| 427 |
|
| 428 |
self._popover.set_offset(x_offset=-container_width + button_width, y_offset=0) |
| 429 |
popover_content.set_size_request(container_width, height=-1) |
| 430 |
|
| 431 |
def _on_dropdown_visible(self, widget, param): |
| 432 |
|
| 433 |
visible = widget.get_property(param.name) |
| 434 |
|
| 435 |
# Only enable item selection callback when an item is selected from the UI |
| 436 |
GLib.idle_add(self._on_select_callback_status, visible, priority=GLib.PRIORITY_HIGH_IDLE) |
| 437 |
|
| 438 |
if self.entry is None: |
| 439 |
return |
| 440 |
|
| 441 |
if not visible: |
| 442 |
return |
| 443 |
|
| 444 |
text = self.get_text() |
| 445 |
|
| 446 |
if text: |
| 447 |
self.set_selected_id(text) |
| 448 |
|
| 449 |
def _on_item_selected(self, *_args): |
| 450 |
|
| 451 |
selected_id = self.get_selected_id() |
| 452 |
|
| 453 |
if selected_id is None: |
| 454 |
return |
| 455 |
|
| 456 |
if self.entry is not None: |
| 457 |
# Update text entry with text from the selected item |
| 458 |
self._update_item_entry_text() |
| 459 |
|
| 460 |
if not self._is_popup_visible: |
| 461 |
return |
| 462 |
|
| 463 |
if self.entry is not None: |
| 464 |
self.entry.grab_focus_without_selecting() |
| 465 |
|
| 466 |
if self.item_selected_callback is not None: |
| 467 |
self.item_selected_callback(self, selected_id) |