@@ -349,26 +349,29 @@ def __init__(self):
349
349
super ().__init__ ()
350
350
self ._uri = None
351
351
self ._window = None
352
+
352
353
gsettings_source = Gio .SettingsSchemaSource .get_default ()
353
354
if gsettings_source .lookup (GSETTINGS_PATH , True ):
354
355
self ._gsettings = Gio .Settings .new (GSETTINGS_PATH )
355
356
if API_VERSION == "4.0" :
356
- self .setup_keybindings ()
357
+ self ._setup_keybindings ()
357
358
else :
358
- self ._gsettings .connect ("changed" , self ._bind_shortcut )
359
- self ._create_accel_group ()
359
+ self ._initialize_legacy_bindings ()
360
360
361
361
def _open_terminal (self , * _args ):
362
+ """Open the terminal at the specified URI."""
362
363
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 )
367
370
368
371
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."""
372
375
app = Gtk .Application .get_default ()
373
376
if app is None :
374
377
print ("No Gtk.Application found. Keybindings cannot be set." )
@@ -380,36 +383,42 @@ def setup_keybindings(self):
380
383
381
384
shortcut = self ._gsettings .get_string (GSETTINGS_KEYBINDINGS )
382
385
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 )
384
387
385
- def _bind_shortcut (self , _gsettings , key ):
388
+ def _update_shortcut (self , _gsettings , key ):
389
+ """Update keybindings when settings change."""
386
390
if key == GSETTINGS_KEYBINDINGS :
387
391
app = Gtk .Application .get_default ()
388
392
if app :
389
393
shortcut = self ._gsettings .get_string (GSETTINGS_KEYBINDINGS )
390
394
app .set_accels_for_action ("app.open_any_terminal" , [shortcut ])
391
395
392
396
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
396
399
return []
397
400
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."""
401
405
self ._accel_group = Gtk .AccelGroup ()
406
+ self ._bind_legacy_shortcut ()
407
+
408
+ def _bind_legacy_shortcut (self ):
409
+ """Bind keyboard shortcuts for older APIs."""
402
410
shortcut = self ._gsettings .get_string (GSETTINGS_KEYBINDINGS )
403
411
key , mod = Gtk .accelerator_parse (shortcut )
404
412
self ._accel_group .connect (key , mod , Gtk .AccelFlags .VISIBLE , self ._open_terminal )
405
413
406
- def _bind_shortcut (self , _gsettings , key ):
414
+ def _update_legacy_shortcut (self , _gsettings , key ):
415
+ """Update shortcuts when settings change in older APIs."""
407
416
if key == GSETTINGS_KEYBINDINGS :
408
417
self ._accel_group .disconnect (self ._open_terminal )
409
- self ._create_accel_group ()
418
+ self ._bind_legacy_shortcut ()
410
419
411
420
def get_widget (self , uri , window ):
412
- """follows uri and sets the correct window"""
421
+ """Set the correct window and URI. """
413
422
self ._uri = uri
414
423
if self ._window :
415
424
self ._window .remove_accel_group (self ._accel_group )
@@ -418,17 +427,6 @@ def get_widget(self, uri, window):
418
427
self ._window = window
419
428
420
429
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
-
432
430
class OpenAnyTerminalExtension (GObject .GObject , FileManager .MenuProvider ):
433
431
"""Provide context menu items for opening terminals in Nautilus."""
434
432
0 commit comments