Skip to content

Commit 3cf8d51

Browse files
committed
gtk4: add keybinding
1 parent a8bc1f9 commit 3cf8d51

File tree

1 file changed

+65
-21
lines changed

1 file changed

+65
-21
lines changed

nautilus_open_any_terminal/nautilus_open_any_terminal.py

Lines changed: 65 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -342,39 +342,83 @@ def set_terminal_args(*_args):
342342
print(f'open-any-terminal: terminal is set to "{terminal}" {new_tab_text} {flatpak_text}')
343343

344344

345-
if API_VERSION in ("3.0", "2.0"):
345+
class OpenAnyTerminalShortcutProvider(GObject.GObject):
346+
"""Provide keyboard shortcuts for opening terminals in Nautilus/Caja."""
346347

347-
class OpenAnyTerminalShortcutProvider(GObject.GObject, FileManager.LocationWidgetProvider):
348-
"""Provide keyboard shortcuts for opening terminals in Nautilus."""
348+
def __init__(self):
349+
super().__init__()
350+
self._uri = None
351+
self._window = None
352+
353+
gsettings_source = Gio.SettingsSchemaSource.get_default()
354+
if gsettings_source.lookup(GSETTINGS_PATH, True):
355+
self._gsettings = Gio.Settings.new(GSETTINGS_PATH)
356+
if API_VERSION == "4.0":
357+
self._setup_keybindings()
358+
elif API_VERSION in ("3.0", "2.0"):
359+
self._initialize_legacy_bindings()
360+
361+
def _open_terminal(self, *_args):
362+
"""Open the terminal at the specified URI."""
363+
if self._uri:
364+
open_func = (
365+
open_local_terminal_in_uri
366+
if self._gsettings.get_boolean(GSETTINGS_BIND_REMOTE)
367+
else open_remote_terminal_in_uri
368+
)
369+
open_func(self._uri)
370+
371+
if API_VERSION == "4.0":
372+
# Nautilus 4.0-specific implementation
373+
def _setup_keybindings(self):
374+
"""Set up custom keybindings for the extension."""
375+
app = Gtk.Application.get_default()
376+
if app is None:
377+
print("No Gtk.Application found. Keybindings cannot be set.")
378+
return
349379

350-
def __init__(self):
351-
gsettings_source = Gio.SettingsSchemaSource.get_default()
352-
if gsettings_source.lookup(GSETTINGS_PATH, True):
353-
self._gsettings = Gio.Settings.new(GSETTINGS_PATH)
354-
self._gsettings.connect("changed", self._bind_shortcut)
355-
self._create_accel_group()
356-
self._window = None
357-
self._uri = None
380+
action = Gio.SimpleAction.new("open_any_terminal", None)
381+
action.connect("activate", self._open_terminal)
382+
app.add_action(action)
358383

359-
def _create_accel_group(self):
384+
shortcut = self._gsettings.get_string(GSETTINGS_KEYBINDINGS)
385+
app.set_accels_for_action("app.open_any_terminal", [shortcut])
386+
self._gsettings.connect("changed", self._update_shortcut)
387+
388+
def _update_shortcut(self, _gsettings, key):
389+
"""Update keybindings when settings change."""
390+
if key == GSETTINGS_KEYBINDINGS:
391+
app = Gtk.Application.get_default()
392+
if app:
393+
shortcut = self._gsettings.get_string(GSETTINGS_KEYBINDINGS)
394+
app.set_accels_for_action("app.open_any_terminal", [shortcut])
395+
396+
def get_background_items(self, current_folder):
397+
"""Update current URI when folder changes."""
398+
self._uri = current_folder.get_uri() if current_folder else None
399+
return []
400+
401+
elif API_VERSION in ("3.0", "2.0"):
402+
# Nautilus/Caja 3.0/2.0-specific implementation
403+
def _initialize_legacy_bindings(self):
404+
"""Initialize legacy bindings for older APIs."""
360405
self._accel_group = Gtk.AccelGroup()
406+
self._bind_legacy_shortcut()
407+
408+
def _bind_legacy_shortcut(self):
409+
"""Bind keyboard shortcuts for older APIs."""
361410
shortcut = self._gsettings.get_string(GSETTINGS_KEYBINDINGS)
362411
key, mod = Gtk.accelerator_parse(shortcut)
363412
self._accel_group.connect(key, mod, Gtk.AccelFlags.VISIBLE, self._open_terminal)
364413

365-
def _bind_shortcut(self, _gsettings, key):
414+
def _update_legacy_shortcut(self, _gsettings, key):
415+
"""Update shortcuts when settings change in older APIs."""
366416
if key == GSETTINGS_KEYBINDINGS:
367417
self._accel_group.disconnect(self._open_terminal)
368-
self._create_accel_group()
369-
370-
def _open_terminal(self, *_args):
371-
if _gsettings.get_boolean(GSETTINGS_BIND_REMOTE):
372-
open_local_terminal_in_uri(self._uri)
373-
else:
374-
open_remote_terminal_in_uri(self._uri)
418+
self._bind_legacy_shortcut()
375419

376420
def get_widget(self, uri, window):
377-
"""follows uri and sets the correct window"""
421+
"""Set the correct window and URI."""
378422
self._uri = uri
379423
if self._window:
380424
self._window.remove_accel_group(self._accel_group)

0 commit comments

Comments
 (0)