CHANGE right-click in the editor to show option to open folder in linux (#4125)

* CHANGE right-click in the editor to show option to open folder in linux

* FIX checks

* Use heuristics

* ./ninja format

* Use fallback from main instead of xdg-open
This commit is contained in:
GithubAnon0000 2025-06-28 18:33:17 +00:00 committed by GitHub
parent aae970bed9
commit cfeb71724d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 32 additions and 8 deletions

View file

@ -36,7 +36,7 @@ from anki.hooks import runFilter
from anki.httpclient import HttpClient from anki.httpclient import HttpClient
from anki.models import NotetypeDict, NotetypeId, StockNotetype from anki.models import NotetypeDict, NotetypeId, StockNotetype
from anki.notes import Note, NoteFieldsCheckResult, NoteId from anki.notes import Note, NoteFieldsCheckResult, NoteId
from anki.utils import checksum, is_lin, is_mac, is_win, namedtmp from anki.utils import checksum, is_lin, is_win, namedtmp
from aqt import AnkiQt, colors, gui_hooks from aqt import AnkiQt, colors, gui_hooks
from aqt.operations import QueryOp from aqt.operations import QueryOp
from aqt.operations.note import update_note from aqt.operations.note import update_note
@ -1735,10 +1735,9 @@ class EditorWebView(AnkiWebView):
assert a is not None assert a is not None
qconnect(a.triggered, lambda: openFolder(path)) qconnect(a.triggered, lambda: openFolder(path))
if is_win or is_mac: a = menu.addAction(tr.editing_show_in_folder())
a = menu.addAction(tr.editing_show_in_folder()) assert a is not None
assert a is not None qconnect(a.triggered, lambda: show_in_folder(path))
qconnect(a.triggered, lambda: show_in_folder(path))
def _clipboard(self) -> QClipboard: def _clipboard(self) -> QClipboard:
clipboard = self.editor.mw.app.clipboard() clipboard = self.editor.mw.app.clipboard()

View file

@ -936,9 +936,34 @@ def show_in_folder(path: str) -> None:
""" """
call(osascript_to_args(script)) call(osascript_to_args(script))
else: else:
# Just open the file in any other platform # For linux, there are multiple file managers. Let's test if one of the
with no_bundled_libs(): # most common file managers is found and use it in case it is installed.
QDesktopServices.openUrl(QUrl.fromLocalFile(path)) # If none of this list are installed, use a fallback. The fallback
# might open the image in a web browser, image viewer or others,
# depending on the users defaults.
file_managers = [
"nautilus", # GNOME
"dolphin", # KDE
"pcmanfm", # LXDE
"thunar", # XFCE
"nemo", # Cinnamon
"caja", # MATE
]
available_file_manager = None
# Test if a file manager is installed and use it, fallback otherwise
for file_manager in file_managers:
if shutil.which(file_manager):
available_file_manager = file_manager
break
if available_file_manager:
subprocess.run([available_file_manager, path], check=False)
else:
# Just open the file in any other platform
with no_bundled_libs():
QDesktopServices.openUrl(QUrl.fromLocalFile(path))
def _show_in_folder_win32(path: str) -> None: def _show_in_folder_win32(path: str) -> None: