| 1 |
# COPYRIGHT (C) 2020-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 |
import os |
| 20 |
|
| 21 |
import gi |
| 22 |
from gi.repository import Gtk |
| 23 |
|
| 24 |
from pynicotine.config import config |
| 25 |
from pynicotine.gtkgui.application import GTK_API_VERSION |
| 26 |
from pynicotine.gtkgui.widgets.accelerator import Accelerator |
| 27 |
from pynicotine.gtkgui.widgets.theme import add_css_class |
| 28 |
|
| 29 |
|
| 30 |
class ChatEntry: |
| 31 |
"""Custom text entry with support for chat commands and completions.""" |
| 32 |
|
| 33 |
def __init__(self, application, send_message_callback, command_callback, enable_spell_check=False): |
| 34 |
|
| 35 |
self.application = application |
| 36 |
self.widget = Gtk.Entry( |
| 37 |
hexpand=True, placeholder_text=_("Send message…"), primary_icon_name="mail-send-symbolic", |
| 38 |
sensitive=False, show_emoji_icon=True, width_chars=8, visible=True |
| 39 |
) |
| 40 |
self.send_message_callback = send_message_callback |
| 41 |
self.command_callback = command_callback |
| 42 |
self.spell_checker = None |
| 43 |
self.entity = None |
| 44 |
self.chat_view = None |
| 45 |
self.unsent_messages = {} |
| 46 |
self.completions = {} |
| 47 |
self.current_completions = [] |
| 48 |
self.completion_index = 0 |
| 49 |
self.midway_completion = False # True if the user just used tab completion |
| 50 |
self.selecting_completion = False # True if the list box is open with suggestions |
| 51 |
self.is_inserting_completion = False |
| 52 |
|
| 53 |
self.model = Gtk.ListStore(str) |
| 54 |
self.column_numbers = list(range(self.model.get_n_columns())) |
| 55 |
|
| 56 |
self.entry_completion = Gtk.EntryCompletion(model=self.model, popup_single_match=False) |
| 57 |
self.entry_completion.set_text_column(0) |
| 58 |
self.entry_completion.set_match_func(self.entry_completion_find_match) |
| 59 |
self.entry_completion.connect("match-selected", self.entry_completion_found_match) |
| 60 |
|
| 61 |
self.widget.set_completion(self.entry_completion) |
| 62 |
CompletionEntry.patch_popover_hide_broadway(self.widget) |
| 63 |
|
| 64 |
self.widget.connect("activate", self.on_send_message) |
| 65 |
self.widget.connect("changed", self.on_changed) |
| 66 |
self.widget.connect("icon-press", self.on_icon_pressed) |
| 67 |
|
| 68 |
Accelerator("<Shift>Tab", self.widget, self.on_tab_complete_accelerator, True) |
| 69 |
Accelerator("Tab", self.widget, self.on_tab_complete_accelerator) |
| 70 |
Accelerator("Down", self.widget, self.on_page_down_accelerator) |
| 71 |
Accelerator("Page_Down", self.widget, self.on_page_down_accelerator) |
| 72 |
Accelerator("Page_Up", self.widget, self.on_page_up_accelerator) |
| 73 |
|
| 74 |
self.set_spell_check_enabled(enable_spell_check) |
| 75 |
|
| 76 |
def destroy(self): |
| 77 |
|
| 78 |
if self.spell_checker is not None: |
| 79 |
self.spell_checker.destroy() |
| 80 |
|
| 81 |
self.__dict__.clear() |
| 82 |
|
| 83 |
def clear_unsent_message(self, entity): |
| 84 |
self.unsent_messages.pop(entity, None) |
| 85 |
|
| 86 |
def grab_focus(self): |
| 87 |
self.widget.grab_focus() |
| 88 |
|
| 89 |
def grab_focus_without_selecting(self): |
| 90 |
self.widget.grab_focus_without_selecting() |
| 91 |
|
| 92 |
def get_buffer(self): |
| 93 |
return self.widget.get_buffer() |
| 94 |
|
| 95 |
def get_position(self): |
| 96 |
return self.widget.get_position() |
| 97 |
|
| 98 |
def get_sensitive(self): |
| 99 |
return self.widget.get_sensitive() |
| 100 |
|
| 101 |
def get_text(self): |
| 102 |
return self.widget.get_text() |
| 103 |
|
| 104 |
def is_completion_enabled(self): |
| 105 |
return config.sections["words"]["tab"] or config.sections["words"]["dropdown"] |
| 106 |
|
| 107 |
def insert_text(self, new_text, position): |
| 108 |
self.widget.insert_text(new_text, position) |
| 109 |
|
| 110 |
def delete_text(self, start_pos, end_pos): |
| 111 |
self.widget.delete_text(start_pos, end_pos) |
| 112 |
|
| 113 |
def add_completion(self, item): |
| 114 |
|
| 115 |
if not self.is_completion_enabled(): |
| 116 |
return |
| 117 |
|
| 118 |
if item in self.completions: |
| 119 |
return |
| 120 |
|
| 121 |
if config.sections["words"]["dropdown"]: |
| 122 |
iterator = self.model.insert_with_valuesv(-1, self.column_numbers, [item]) |
| 123 |
else: |
| 124 |
iterator = None |
| 125 |
|
| 126 |
self.completions[item] = iterator |
| 127 |
|
| 128 |
def remove_completion(self, item): |
| 129 |
|
| 130 |
if not self.is_completion_enabled(): |
| 131 |
return |
| 132 |
|
| 133 |
iterator = self.completions.pop(item, None) |
| 134 |
|
| 135 |
if iterator is not None: |
| 136 |
self.model.remove(iterator) |
| 137 |
|
| 138 |
def set_parent(self, entity=None, container=None, chat_view=None): |
| 139 |
|
| 140 |
if self.entity: |
| 141 |
self.unsent_messages[self.entity] = self.widget.get_text() |
| 142 |
|
| 143 |
self.entity = entity |
| 144 |
self.chat_view = chat_view |
| 145 |
|
| 146 |
parent = self.widget.get_parent() |
| 147 |
|
| 148 |
if parent is not None: |
| 149 |
parent.remove(self.widget) |
| 150 |
|
| 151 |
if container is None: |
| 152 |
return |
| 153 |
|
| 154 |
unsent_message = self.unsent_messages.get(entity, "") |
| 155 |
|
| 156 |
if GTK_API_VERSION >= 4: |
| 157 |
container.append(self.widget) # pylint: disable=no-member |
| 158 |
else: |
| 159 |
container.add(self.widget) # pylint: disable=no-member |
| 160 |
|
| 161 |
self.widget.set_text(unsent_message) |
| 162 |
self.widget.set_position(-1) |
| 163 |
|
| 164 |
def set_completions(self, completions): |
| 165 |
|
| 166 |
if self.entry_completion is None: |
| 167 |
return |
| 168 |
|
| 169 |
config_words = config.sections["words"] |
| 170 |
|
| 171 |
self.entry_completion.set_popup_completion(config_words["dropdown"]) |
| 172 |
self.entry_completion.set_minimum_key_length(config_words["characters"]) |
| 173 |
|
| 174 |
self.model.clear() |
| 175 |
self.completions.clear() |
| 176 |
|
| 177 |
if not self.is_completion_enabled(): |
| 178 |
return |
| 179 |
|
| 180 |
for word in sorted(completions): |
| 181 |
word = str(word) |
| 182 |
|
| 183 |
if config_words["dropdown"]: |
| 184 |
iterator = self.model.insert_with_valuesv(-1, self.column_numbers, [word]) |
| 185 |
else: |
| 186 |
iterator = None |
| 187 |
|
| 188 |
self.completions[word] = iterator |
| 189 |
|
| 190 |
def set_spell_check_enabled(self, enabled): |
| 191 |
|
| 192 |
if self.spell_checker is not None: |
| 193 |
self.spell_checker.set_enabled(enabled) |
| 194 |
return |
| 195 |
|
| 196 |
if enabled and SpellChecker.is_available(): |
| 197 |
self.spell_checker = SpellChecker(self.widget) |
| 198 |
|
| 199 |
def set_position(self, position): |
| 200 |
self.widget.set_position(position) |
| 201 |
|
| 202 |
def set_sensitive(self, sensitive): |
| 203 |
self.widget.set_sensitive(sensitive) |
| 204 |
|
| 205 |
def set_text(self, text): |
| 206 |
self.widget.set_text(text) |
| 207 |
|
| 208 |
def set_visible(self, visible): |
| 209 |
self.widget.set_visible(visible) |
| 210 |
|
| 211 |
def on_send_message(self, *_args): |
| 212 |
|
| 213 |
text = self.widget.get_text().strip() |
| 214 |
|
| 215 |
if not text: |
| 216 |
self.chat_view.scroll_bottom() |
| 217 |
return |
| 218 |
|
| 219 |
is_double_slash_cmd = text.startswith("//") |
| 220 |
is_single_slash_cmd = (text.startswith("/") and not is_double_slash_cmd) |
| 221 |
|
| 222 |
if not is_single_slash_cmd: |
| 223 |
# Regular chat message |
| 224 |
|
| 225 |
self.widget.set_text("") |
| 226 |
|
| 227 |
if is_double_slash_cmd: |
| 228 |
# Remove first slash and send the rest of the command as plain text |
| 229 |
text = text[1:] |
| 230 |
|
| 231 |
self.send_message_callback(self.entity, text) |
| 232 |
return |
| 233 |
|
| 234 |
cmd, _separator, args = text.partition(" ") |
| 235 |
args = args.strip() |
| 236 |
|
| 237 |
if not self.command_callback(self.entity, cmd[1:], args): |
| 238 |
return |
| 239 |
|
| 240 |
# Clear chat entry |
| 241 |
self.widget.set_text("") |
| 242 |
|
| 243 |
def on_icon_pressed(self, _entry, icon_pos, *_args): |
| 244 |
if icon_pos == Gtk.EntryIconPosition.PRIMARY: |
| 245 |
self.on_send_message() |
| 246 |
|
| 247 |
def entry_completion_find_match(self, _completion, entry_text, iterator): |
| 248 |
|
| 249 |
if not entry_text: |
| 250 |
return False |
| 251 |
|
| 252 |
# Get word to the left of current position |
| 253 |
if " " in entry_text: |
| 254 |
i = self.widget.get_position() |
| 255 |
split_key = entry_text[:i].split(" ")[-1] |
| 256 |
else: |
| 257 |
split_key = entry_text |
| 258 |
|
| 259 |
if not split_key or len(split_key) < config.sections["words"]["characters"]: |
| 260 |
return False |
| 261 |
|
| 262 |
# Case-insensitive matching |
| 263 |
item_text = self.model.get_value(iterator, 0).lower() |
| 264 |
|
| 265 |
if item_text.startswith(split_key) and item_text != split_key: |
| 266 |
self.selecting_completion = True |
| 267 |
return True |
| 268 |
|
| 269 |
return False |
| 270 |
|
| 271 |
def entry_completion_found_match(self, _completion, model, iterator): |
| 272 |
|
| 273 |
current_text = self.widget.get_text() |
| 274 |
completion_value = model.get_value(iterator, 0) |
| 275 |
|
| 276 |
# if more than a word has been typed, we throw away the |
| 277 |
# one to the left of our current position because we want |
| 278 |
# to replace it with the matching word |
| 279 |
|
| 280 |
if " " in current_text: |
| 281 |
i = self.widget.get_position() |
| 282 |
prefix = " ".join(current_text[:i].split(" ")[:-1]) |
| 283 |
suffix = " ".join(current_text[i:].split(" ")) |
| 284 |
|
| 285 |
# add the matching word |
| 286 |
new_text = f"{prefix} {completion_value}{suffix}" |
| 287 |
# set back the whole text |
| 288 |
self.widget.set_text(new_text) |
| 289 |
# move the cursor at the end |
| 290 |
self.widget.set_position(len(prefix) + len(completion_value) + 1) |
| 291 |
else: |
| 292 |
self.widget.set_text(completion_value) |
| 293 |
self.widget.set_position(-1) |
| 294 |
|
| 295 |
# stop the event propagation |
| 296 |
return True |
| 297 |
|
| 298 |
def on_changed(self, *_args): |
| 299 |
|
| 300 |
# If the entry was modified, and we don't block entry_changed_handler, we're no longer completing |
| 301 |
if not self.is_inserting_completion: |
| 302 |
self.midway_completion = self.selecting_completion = False |
| 303 |
|
| 304 |
def on_tab_complete_accelerator(self, _widget, _state, backwards=False): |
| 305 |
"""Tab and Shift+Tab: tab complete chat.""" |
| 306 |
|
| 307 |
if not config.sections["words"]["tab"]: |
| 308 |
return False |
| 309 |
|
| 310 |
text = self.widget.get_text() |
| 311 |
|
| 312 |
if not text: |
| 313 |
return False |
| 314 |
|
| 315 |
# "Hello there Miss<tab> how are you doing" |
| 316 |
# "0 3 6 9 12 15 18 21 24 27 30 33 |
| 317 |
# 1 4 7 10 13 16 19 22 25 28 31 |
| 318 |
# 2 5 8 11 14 17 20 23 26 29 32 |
| 319 |
# |
| 320 |
# i = 16 |
| 321 |
# last_word = Miss |
| 322 |
# preix = 12 |
| 323 |
|
| 324 |
i = self.widget.get_position() |
| 325 |
last_word = text[:i].split(" ")[-1] |
| 326 |
last_word_len = len(last_word) |
| 327 |
preix = i - last_word_len |
| 328 |
|
| 329 |
if not self.midway_completion: |
| 330 |
if last_word_len < 1: |
| 331 |
return False |
| 332 |
|
| 333 |
last_word_lower = last_word.lower() |
| 334 |
self.current_completions = [ |
| 335 |
x for x in self.completions if x.lower().startswith(last_word_lower) and len(x) >= last_word_len |
| 336 |
] |
| 337 |
|
| 338 |
if self.current_completions: |
| 339 |
self.midway_completion = True |
| 340 |
self.completion_index = -1 |
| 341 |
current_word = last_word |
| 342 |
else: |
| 343 |
current_word = self.current_completions[self.completion_index] |
| 344 |
|
| 345 |
if self.midway_completion: |
| 346 |
# We're still completing, block entry_changed_handler to avoid modifying midway_completion value |
| 347 |
self.is_inserting_completion = True |
| 348 |
self.widget.delete_text(i - len(current_word), i) |
| 349 |
|
| 350 |
direction = -1 if backwards else 1 |
| 351 |
self.completion_index = ((self.completion_index + direction) % len(self.current_completions)) |
| 352 |
|
| 353 |
new_word = self.current_completions[self.completion_index] |
| 354 |
self.widget.insert_text(new_word, preix) |
| 355 |
self.widget.set_position(preix + len(new_word)) |
| 356 |
self.is_inserting_completion = False |
| 357 |
|
| 358 |
return True |
| 359 |
|
| 360 |
def on_page_down_accelerator(self, *_args): |
| 361 |
"""Page_Down, Down - Scroll chat view to bottom, and keep input focus in |
| 362 |
entry widget.""" |
| 363 |
|
| 364 |
if self.selecting_completion: |
| 365 |
return False |
| 366 |
|
| 367 |
self.chat_view.scroll_bottom() |
| 368 |
return True |
| 369 |
|
| 370 |
def on_page_up_accelerator(self, *_args): |
| 371 |
"""Page_Up - Move up into view to begin scrolling message history.""" |
| 372 |
|
| 373 |
if self.selecting_completion: |
| 374 |
return False |
| 375 |
|
| 376 |
self.chat_view.grab_focus() |
| 377 |
return True |
| 378 |
|
| 379 |
|
| 380 |
class CompletionEntry: |
| 381 |
|
| 382 |
def __init__(self, widget, model=None, column=0): |
| 383 |
|
| 384 |
self.model = model |
| 385 |
self.column = column |
| 386 |
self.completions = {} |
| 387 |
|
| 388 |
if model is None: |
| 389 |
self.model = model = Gtk.ListStore(str) |
| 390 |
self.column_numbers = list(range(self.model.get_n_columns())) |
| 391 |
|
| 392 |
completion = Gtk.EntryCompletion(inline_completion=True, inline_selection=True, |
| 393 |
popup_single_match=False, model=model) |
| 394 |
completion.set_text_column(column) |
| 395 |
completion.set_match_func(self.entry_completion_find_match) |
| 396 |
|
| 397 |
widget.set_completion(completion) |
| 398 |
self.patch_popover_hide_broadway(widget) |
| 399 |
|
| 400 |
def destroy(self): |
| 401 |
self.__dict__.clear() |
| 402 |
|
| 403 |
def add_completion(self, item): |
| 404 |
if item not in self.completions: |
| 405 |
self.completions[item] = self.model.insert_with_valuesv(-1, self.column_numbers, [item]) |
| 406 |
|
| 407 |
def remove_completion(self, item): |
| 408 |
|
| 409 |
iterator = self.completions.pop(item, None) |
| 410 |
|
| 411 |
if iterator is not None: |
| 412 |
self.model.remove(iterator) |
| 413 |
|
| 414 |
def clear(self): |
| 415 |
self.model.clear() |
| 416 |
|
| 417 |
def entry_completion_find_match(self, _completion, entry_text, iterator): |
| 418 |
|
| 419 |
if not entry_text: |
| 420 |
return False |
| 421 |
|
| 422 |
item_text = self.model.get_value(iterator, self.column) |
| 423 |
|
| 424 |
if not item_text: |
| 425 |
return False |
| 426 |
|
| 427 |
if item_text.lower().startswith(entry_text.lower()): |
| 428 |
return True |
| 429 |
|
| 430 |
return False |
| 431 |
|
| 432 |
@classmethod |
| 433 |
def patch_popover_hide_broadway(cls, entry): |
| 434 |
|
| 435 |
# Workaround for GTK 4 bug where broadwayd uses a lot of CPU after hiding popover |
| 436 |
if GTK_API_VERSION >= 4 and os.environ.get("GDK_BACKEND") == "broadway": |
| 437 |
completion_popover = list(entry)[-1] |
| 438 |
completion_popover.connect("hide", cls.on_popover_hide_broadway) |
| 439 |
|
| 440 |
@staticmethod |
| 441 |
def on_popover_hide_broadway(popover): |
| 442 |
popover.unrealize() |
| 443 |
|
| 444 |
|
| 445 |
class SpellChecker: |
| 446 |
|
| 447 |
checker = None |
| 448 |
module = None |
| 449 |
|
| 450 |
def __init__(self, entry): |
| 451 |
|
| 452 |
self.buffer = None |
| 453 |
self.entry = None |
| 454 |
|
| 455 |
self._set_entry(entry) |
| 456 |
|
| 457 |
@classmethod |
| 458 |
def _load_module(cls): |
| 459 |
|
| 460 |
if GTK_API_VERSION >= 4: |
| 461 |
# No spell checkers available in GTK 4 yet |
| 462 |
return |
| 463 |
|
| 464 |
if SpellChecker.module is not None: |
| 465 |
return |
| 466 |
|
| 467 |
try: |
| 468 |
gi.require_version("Gspell", "1") |
| 469 |
from gi.repository import Gspell |
| 470 |
SpellChecker.module = Gspell |
| 471 |
|
| 472 |
except (ImportError, ValueError): |
| 473 |
pass |
| 474 |
|
| 475 |
def _set_entry(self, entry): |
| 476 |
|
| 477 |
if self.entry is not None: |
| 478 |
return |
| 479 |
|
| 480 |
# Attempt to load spell check module in case it was recently installed |
| 481 |
self._load_module() |
| 482 |
|
| 483 |
if SpellChecker.module is None: |
| 484 |
return |
| 485 |
|
| 486 |
if SpellChecker.checker is None: |
| 487 |
SpellChecker.checker = SpellChecker.module.Checker() |
| 488 |
|
| 489 |
self.buffer = SpellChecker.module.EntryBuffer.get_from_gtk_entry_buffer(entry.get_buffer()) |
| 490 |
self.buffer.set_spell_checker(SpellChecker.checker) |
| 491 |
|
| 492 |
self.entry = SpellChecker.module.Entry.get_from_gtk_entry(entry) |
| 493 |
self.entry.set_inline_spell_checking(True) |
| 494 |
|
| 495 |
@classmethod |
| 496 |
def is_available(cls): |
| 497 |
cls._load_module() |
| 498 |
return bool(SpellChecker.module) |
| 499 |
|
| 500 |
def set_enabled(self, enabled): |
| 501 |
self.entry.set_inline_spell_checking(enabled) |
| 502 |
|
| 503 |
def destroy(self): |
| 504 |
self.__dict__.clear() |
| 505 |
|
| 506 |
|
| 507 |
class TextSearchBar: |
| 508 |
|
| 509 |
def __init__(self, textview, search_bar, controller_widget=None, focus_widget=None, |
| 510 |
placeholder_text=None): |
| 511 |
|
| 512 |
self.textview = textview |
| 513 |
self.search_bar = search_bar |
| 514 |
self.focus_widget = focus_widget |
| 515 |
|
| 516 |
container = Gtk.Box(spacing=6, visible=True) |
| 517 |
self.entry = Gtk.SearchEntry( |
| 518 |
max_width_chars=75, placeholder_text=placeholder_text, width_chars=24, visible=True |
| 519 |
) |
| 520 |
self.previous_button = Gtk.Button(tooltip_text=_("Find Previous Match"), visible=True) |
| 521 |
self.next_button = Gtk.Button(tooltip_text=_("Find Next Match"), visible=True) |
| 522 |
|
| 523 |
if GTK_API_VERSION >= 4: |
| 524 |
self.previous_button.set_icon_name("go-up-symbolic") # pylint: disable=no-member |
| 525 |
self.next_button.set_icon_name("go-down-symbolic") # pylint: disable=no-member |
| 526 |
|
| 527 |
container.append(self.entry) # pylint: disable=no-member |
| 528 |
container.append(self.previous_button) # pylint: disable=no-member |
| 529 |
container.append(self.next_button) # pylint: disable=no-member |
| 530 |
self.search_bar.set_child(container) # pylint: disable=no-member |
| 531 |
else: |
| 532 |
self.previous_button.set_image(Gtk.Image(icon_name="go-up-symbolic")) # pylint: disable=no-member |
| 533 |
self.next_button.set_image(Gtk.Image(icon_name="go-down-symbolic")) # pylint: disable=no-member |
| 534 |
|
| 535 |
container.add(self.entry) # pylint: disable=no-member |
| 536 |
container.add(self.previous_button) # pylint: disable=no-member |
| 537 |
container.add(self.next_button) # pylint: disable=no-member |
| 538 |
self.search_bar.add(container) # pylint: disable=no-member |
| 539 |
|
| 540 |
if not controller_widget: |
| 541 |
controller_widget = textview |
| 542 |
|
| 543 |
for button in (self.previous_button, self.next_button): |
| 544 |
add_css_class(button, "flat") |
| 545 |
|
| 546 |
self.search_bar.connect_entry(self.entry) |
| 547 |
|
| 548 |
self.entry.connect("activate", self.on_search_next_match) |
| 549 |
self.entry.connect("search-changed", self.on_search_changed) |
| 550 |
self.entry.connect("stop-search", self.on_hide_search_accelerator) |
| 551 |
|
| 552 |
self.entry.connect("previous-match", self.on_search_previous_match) |
| 553 |
self.entry.connect("next-match", self.on_search_next_match) |
| 554 |
self.previous_button.connect("clicked", self.on_search_previous_match) |
| 555 |
self.next_button.connect("clicked", self.on_search_next_match) |
| 556 |
|
| 557 |
Accelerator("Up", self.entry, self.on_search_previous_match) |
| 558 |
Accelerator("Down", self.entry, self.on_search_next_match) |
| 559 |
Accelerator("<Primary>f", controller_widget, self.on_show_search_accelerator) |
| 560 |
Accelerator("Escape", controller_widget, self.on_hide_search_accelerator) |
| 561 |
|
| 562 |
for accelerator in ("<Primary>g", "F3"): |
| 563 |
Accelerator(accelerator, controller_widget, self.on_search_next_match) |
| 564 |
|
| 565 |
for accelerator in ("<Shift><Primary>g", "<Shift>F3"): |
| 566 |
Accelerator(accelerator, controller_widget, self.on_search_previous_match) |
| 567 |
|
| 568 |
def destroy(self): |
| 569 |
self.__dict__.clear() |
| 570 |
|
| 571 |
def on_search_match(self, search_type, restarted=False): |
| 572 |
|
| 573 |
if not self.search_bar.get_search_mode(): |
| 574 |
return |
| 575 |
|
| 576 |
text_buffer = self.textview.get_buffer() |
| 577 |
query = self.entry.get_text() |
| 578 |
|
| 579 |
self.textview.emit("select-all", False) |
| 580 |
|
| 581 |
if search_type == "typing": |
| 582 |
start, end = text_buffer.get_bounds() |
| 583 |
iterator = start |
| 584 |
else: |
| 585 |
current = text_buffer.get_mark("insert") |
| 586 |
iterator = text_buffer.get_iter_at_mark(current) |
| 587 |
|
| 588 |
if search_type == "previous": |
| 589 |
match = iterator.backward_search( |
| 590 |
query, Gtk.TextSearchFlags.TEXT_ONLY | Gtk.TextSearchFlags.CASE_INSENSITIVE, limit=None) |
| 591 |
else: |
| 592 |
match = iterator.forward_search( |
| 593 |
query, Gtk.TextSearchFlags.TEXT_ONLY | Gtk.TextSearchFlags.CASE_INSENSITIVE, limit=None) |
| 594 |
|
| 595 |
if match is not None and len(match) == 2: |
| 596 |
match_start, match_end = match |
| 597 |
|
| 598 |
if search_type == "previous": |
| 599 |
text_buffer.place_cursor(match_start) |
| 600 |
text_buffer.select_range(match_start, match_end) |
| 601 |
else: |
| 602 |
text_buffer.place_cursor(match_end) |
| 603 |
text_buffer.select_range(match_end, match_start) |
| 604 |
|
| 605 |
self.textview.scroll_to_iter(match_start, 0, False, 0.5, 0.5) |
| 606 |
|
| 607 |
elif not restarted and search_type != "typing": |
| 608 |
start, end = text_buffer.get_bounds() |
| 609 |
|
| 610 |
if search_type == "previous": |
| 611 |
text_buffer.place_cursor(end) |
| 612 |
elif search_type == "next": |
| 613 |
text_buffer.place_cursor(start) |
| 614 |
|
| 615 |
self.on_search_match(search_type, restarted=True) |
| 616 |
|
| 617 |
def on_search_changed(self, *_args): |
| 618 |
self.on_search_match(search_type="typing") |
| 619 |
|
| 620 |
def on_search_previous_match(self, *_args): |
| 621 |
self.on_search_match(search_type="previous") |
| 622 |
return True |
| 623 |
|
| 624 |
def on_search_next_match(self, *_args): |
| 625 |
self.on_search_match(search_type="next") |
| 626 |
return True |
| 627 |
|
| 628 |
def on_hide_search_accelerator(self, *_args): |
| 629 |
"""Escape - hide search bar.""" |
| 630 |
|
| 631 |
self.set_visible(False) |
| 632 |
return True |
| 633 |
|
| 634 |
def on_show_search_accelerator(self, *_args): |
| 635 |
"""Ctrl+F - show search bar.""" |
| 636 |
|
| 637 |
self.set_visible(True) |
| 638 |
return True |
| 639 |
|
| 640 |
def set_visible(self, visible): |
| 641 |
|
| 642 |
self.search_bar.set_search_mode(visible) |
| 643 |
|
| 644 |
if visible: |
| 645 |
text_buffer = self.textview.get_buffer() |
| 646 |
selection_bounds = text_buffer.get_selection_bounds() |
| 647 |
|
| 648 |
if selection_bounds: |
| 649 |
selection_start, selection_end = selection_bounds |
| 650 |
selection_content = text_buffer.get_text( |
| 651 |
selection_start, selection_end, include_hidden_chars=True) |
| 652 |
|
| 653 |
if self.entry.get_text().lower() != selection_content.lower(): |
| 654 |
self.entry.set_text(selection_content) |
| 655 |
|
| 656 |
self.entry.grab_focus() |
| 657 |
self.entry.set_position(-1) |
| 658 |
return |
| 659 |
|
| 660 |
if self.focus_widget is not None and self.focus_widget.get_sensitive(): |
| 661 |
self.focus_widget.grab_focus() |
| 662 |
return |
| 663 |
|
| 664 |
self.textview.grab_focus() |