grc: Use GTK app chooser to select external editor.

Signed-off-by: Ryan Volz <ryan.volz@gmail.com>
This commit is contained in:
Ryan Volz 2023-08-09 18:46:16 -04:00 committed by Jeff Long
parent 2f985fc560
commit 3584caaf48

View File

@ -9,7 +9,7 @@ import sys
import textwrap import textwrap
from shutil import which as find_executable from shutil import which as find_executable
from gi.repository import Gtk, GLib, Gdk from gi.repository import Gtk, GLib, Gdk, Gio
from . import Utils, Actions, Constants from . import Utils, Actions, Constants
from ..core import Messages from ..core import Messages
@ -407,53 +407,26 @@ def choose_editor(parent, config):
""" """
Give the option to either choose an editor or use the default. Give the option to either choose an editor or use the default.
""" """
buttons = ( content_type = Gio.content_type_from_mime_type("text/x-python")
'Choose Editor', Gtk.ResponseType.YES, if content_type == "*":
'Use Default', Gtk.ResponseType.NO, # fallback to plain text on Windows if no useful x-python association
Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL content_type = Gio.content_type_from_mime_type("text/plain")
dialog = Gtk.AppChooserDialog.new_for_content_type(
parent,
Gtk.DialogFlags.MODAL,
content_type,
) )
response = MessageDialogWrapper( dialog.set_heading("Choose an editor below")
parent, message_type=Gtk.MessageType.QUESTION, buttons=Gtk.ButtonsType.NONE, widget = dialog.get_widget()
title='Choose Editor', markup='Would you like to choose the editor to use?', widget.set_default_text("Choose an editor")
default_response=Gtk.ResponseType.YES, extra_buttons=buttons widget.set_show_default(True)
).run_and_destroy() widget.set_show_recommended(True)
widget.set_show_fallback(True)
# Handle the initial default/choose/cancel response editor = None
# User wants to choose the editor to use response = dialog.run()
editor = '' if response == Gtk.ResponseType.OK:
if response == Gtk.ResponseType.YES: appinfo = dialog.get_app_info()
file_dialog = Gtk.FileChooserDialog( editor = config.editor = appinfo.get_executable()
'Select an Editor...', None, dialog.destroy()
Gtk.FileChooserAction.OPEN,
('gtk-cancel', Gtk.ResponseType.CANCEL,
'gtk-open', Gtk.ResponseType.OK),
transient_for=parent
)
file_dialog.set_select_multiple(False)
file_dialog.set_local_only(True)
file_dialog.set_current_folder('/usr/bin')
try:
if file_dialog.run() == Gtk.ResponseType.OK:
editor = config.editor = file_dialog.get_filename()
finally:
file_dialog.hide()
# Go with the default editor
elif response == Gtk.ResponseType.NO:
try:
process = None
if sys.platform.startswith('linux'):
process = find_executable('xdg-open')
elif sys.platform.startswith('darwin'):
process = find_executable('open')
if process is None:
raise ValueError("Can't find default editor executable")
# Save
editor = config.editor = process
except Exception:
Messages.send(
'>>> Unable to load the default editor. Please choose an editor.\n')
if editor == '':
Messages.send('>>> No editor selected.\n')
return editor return editor