Skip to content

Commit 2184870

Browse files
committed
split classes again
1 parent 3969186 commit 2184870

File tree

1 file changed

+30
-32
lines changed

1 file changed

+30
-32
lines changed

nautilus_open_any_terminal/nautilus_open_any_terminal.py

Lines changed: 30 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -349,26 +349,29 @@ def __init__(self):
349349
super().__init__()
350350
self._uri = None
351351
self._window = None
352+
352353
gsettings_source = Gio.SettingsSchemaSource.get_default()
353354
if gsettings_source.lookup(GSETTINGS_PATH, True):
354355
self._gsettings = Gio.Settings.new(GSETTINGS_PATH)
355356
if API_VERSION == "4.0":
356-
self.setup_keybindings()
357+
self._setup_keybindings()
357358
else:
358-
self._gsettings.connect("changed", self._bind_shortcut)
359-
self._create_accel_group()
359+
self._initialize_legacy_bindings()
360360

361361
def _open_terminal(self, *_args):
362+
"""Open the terminal at the specified URI."""
362363
if self._uri:
363-
if _gsettings.get_boolean(GSETTINGS_BIND_REMOTE):
364-
open_local_terminal_in_uri(self._uri)
365-
else:
366-
open_remote_terminal_in_uri(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)
367370

368371
if API_VERSION == "4.0":
369-
# Nautilus 4.0 implementation
370-
def setup_keybindings(self):
371-
"""Setup custom keybindings for the extension."""
372+
# Nautilus 4.0-specific implementation
373+
def _setup_keybindings(self):
374+
"""Set up custom keybindings for the extension."""
372375
app = Gtk.Application.get_default()
373376
if app is None:
374377
print("No Gtk.Application found. Keybindings cannot be set.")
@@ -380,36 +383,42 @@ def setup_keybindings(self):
380383

381384
shortcut = self._gsettings.get_string(GSETTINGS_KEYBINDINGS)
382385
app.set_accels_for_action("app.open_any_terminal", [shortcut])
383-
self._gsettings.connect("changed", self._bind_shortcut)
386+
self._gsettings.connect("changed", self._update_shortcut)
384387

385-
def _bind_shortcut(self, _gsettings, key):
388+
def _update_shortcut(self, _gsettings, key):
389+
"""Update keybindings when settings change."""
386390
if key == GSETTINGS_KEYBINDINGS:
387391
app = Gtk.Application.get_default()
388392
if app:
389393
shortcut = self._gsettings.get_string(GSETTINGS_KEYBINDINGS)
390394
app.set_accels_for_action("app.open_any_terminal", [shortcut])
391395

392396
def get_background_items(self, current_folder):
393-
"""Update current URI when folder changes"""
394-
if current_folder is not None:
395-
self._uri = current_folder.get_uri()
397+
"""Update current URI when folder changes."""
398+
self._uri = current_folder.get_uri() if current_folder else None
396399
return []
397400

398-
else:
399-
# Nautilus/Caja 3.0/2.0 implementation
400-
def _create_accel_group(self):
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."""
401405
self._accel_group = Gtk.AccelGroup()
406+
self._bind_legacy_shortcut()
407+
408+
def _bind_legacy_shortcut(self):
409+
"""Bind keyboard shortcuts for older APIs."""
402410
shortcut = self._gsettings.get_string(GSETTINGS_KEYBINDINGS)
403411
key, mod = Gtk.accelerator_parse(shortcut)
404412
self._accel_group.connect(key, mod, Gtk.AccelFlags.VISIBLE, self._open_terminal)
405413

406-
def _bind_shortcut(self, _gsettings, key):
414+
def _update_legacy_shortcut(self, _gsettings, key):
415+
"""Update shortcuts when settings change in older APIs."""
407416
if key == GSETTINGS_KEYBINDINGS:
408417
self._accel_group.disconnect(self._open_terminal)
409-
self._create_accel_group()
418+
self._bind_legacy_shortcut()
410419

411420
def get_widget(self, uri, window):
412-
"""follows uri and sets the correct window"""
421+
"""Set the correct window and URI."""
413422
self._uri = uri
414423
if self._window:
415424
self._window.remove_accel_group(self._accel_group)
@@ -418,17 +427,6 @@ def get_widget(self, uri, window):
418427
self._window = window
419428

420429

421-
# Add the appropriate interface based on API version
422-
if API_VERSION in ("3.0", "2.0"):
423-
OpenAnyTerminalShortcutProvider = type(
424-
"OpenAnyTerminalShortcutProvider", (OpenAnyTerminalShortcutProvider, FileManager.LocationWidgetProvider), {}
425-
)
426-
elif API_VERSION == "4.0":
427-
OpenAnyTerminalShortcutProvider = type(
428-
"OpenAnyTerminalShortcutProvider", (OpenAnyTerminalShortcutProvider, FileManager.MenuProvider), {}
429-
)
430-
431-
432430
class OpenAnyTerminalExtension(GObject.GObject, FileManager.MenuProvider):
433431
"""Provide context menu items for opening terminals in Nautilus."""
434432

0 commit comments

Comments
 (0)