Externalized save dialog; separating some event logic

This commit is contained in:
itdominator 2023-10-03 19:39:11 -05:00
parent e95e37b1bd
commit 5512655f3d
5 changed files with 86 additions and 58 deletions

View File

@ -9,6 +9,7 @@ from gi.repository import Gtk
from .widgets.base.banner_controls import BannerControls from .widgets.base.banner_controls import BannerControls
from .editors_container import EditorsContainer from .editors_container import EditorsContainer
from .widgets.base.general_info_widget import GeneralInfoWidget from .widgets.base.general_info_widget import GeneralInfoWidget
from .widgets.save_file_dialog import SaveFileDialog
@ -33,6 +34,7 @@ class CoreWidget(Gtk.Box):
... ...
def _load_widgets(self): def _load_widgets(self):
SaveFileDialog()
self.add(BannerControls()) self.add(BannerControls())
GeneralInfoWidget() GeneralInfoWidget()
self.add(EditorsContainer()) self.add(EditorsContainer())

View File

@ -41,16 +41,6 @@ class SourceView(SourceViewEventsMixin, GtkSource.View):
self._buffer = self.get_buffer() self._buffer = self.get_buffer()
self._completion = self.get_completion() self._completion = self.get_completion()
self._file_filter_text = Gtk.FileFilter()
self._file_filter_text.set_name("Text Files")
# TODO: Need to externalize to settings file...
for p in settings.filters.code:
self._file_filter_text.add_pattern(p)
self._file_filter_all = Gtk.FileFilter()
self._file_filter_all.set_name("All Files")
self._file_filter_all.add_pattern("*.*")
self._setup_styling() self._setup_styling()
self._setup_signals() self._setup_signals()
self._set_up_dnd() self._set_up_dnd()
@ -79,9 +69,17 @@ class SourceView(SourceViewEventsMixin, GtkSource.View):
def _setup_signals(self): def _setup_signals(self):
self.connect("drag-data-received", self._on_drag_data_received) self.connect("drag-data-received", self._on_drag_data_received)
self.connect("focus", self._on_widget_focus)
self._buffer.connect("mark-set", self._on_cursor_move) self._buffer.connect("mark-set", self._on_cursor_move)
self._buffer.connect('changed', self._is_modified) self._buffer.connect('changed', self._is_modified)
def _subscribe_to_events(self):
...
def _load_widgets(self):
...
def _document_loaded(self): def _document_loaded(self):
for provider in self._completion.get_providers(): for provider in self._completion.get_providers():
self._completion.remove_provider(provider) self._completion.remove_provider(provider)
@ -99,12 +97,6 @@ class SourceView(SourceViewEventsMixin, GtkSource.View):
self._completion.add_provider(py_completion_provider) self._completion.add_provider(py_completion_provider)
def _subscribe_to_events(self):
...
def _load_widgets(self):
...
def _create_default_tag(self): def _create_default_tag(self):
self._general_style_tag = self._buffer.create_tag('general_style') self._general_style_tag = self._buffer.create_tag('general_style')
self._general_style_tag.set_property('size', 100) self._general_style_tag.set_property('size', 100)
@ -114,9 +106,7 @@ class SourceView(SourceViewEventsMixin, GtkSource.View):
self._is_changed = True self._is_changed = True
self.update_cursor_position() self.update_cursor_position()
def _on_cursor_move(self, buf, cursor_iter, mark, user_data = None): def _on_widget_focus(self, widget, eve = None):
if mark != buf.get_insert(): return
target = self.get_parent().get_parent().NAME target = self.get_parent().get_parent().NAME
path = self._current_file if self._current_file else "" path = self._current_file if self._current_file else ""
@ -124,6 +114,11 @@ class SourceView(SourceViewEventsMixin, GtkSource.View):
event_system.emit("set_path_label", (path,)) event_system.emit("set_path_label", (path,))
event_system.emit("set_encoding_label") event_system.emit("set_encoding_label")
event_system.emit("set_file_type_label", (self._current_filetype,)) event_system.emit("set_file_type_label", (self._current_filetype,))
return False
def _on_cursor_move(self, buf, cursor_iter, mark, user_data = None):
if mark != buf.get_insert(): return
self.update_cursor_position() self.update_cursor_position()
def _set_up_dnd(self): def _set_up_dnd(self):

View File

@ -48,18 +48,6 @@ class SourceViewEventsMixin:
row = iter.get_line() + 1 row = iter.get_line() + 1
col = self.get_visual_column(iter) + 1 col = self.get_visual_column(iter) + 1
classes = self._buffer.get_context_classes_at_iter(iter)
classes_str = ""
i = 0
for c in classes:
if len(classes) != i + 1:
classes_str += c + ", "
else:
classes_str += c
cursor_data = f"char: {chars}, line: {row}, column: {col}, classes: {classes_str}"
logger.debug(cursor_data)
event_system.emit("set_line_char_label", (f"{row}:{col}",)) event_system.emit("set_line_char_label", (f"{row}:{col}",))
def got_to_line(self, line: int = 0): def got_to_line(self, line: int = 0):
@ -89,7 +77,7 @@ class SourceViewEventsMixin:
def save_file(self): def save_file(self):
self.skip_file_load = True self.skip_file_load = True
gfile = self.save_file_dialog() if not self._current_file else self._current_file gfile = event_system.emit_and_await("save_file_dialog", (self._current_filename, self._current_file)) if not self._current_file else self._current_file
if not gfile: if not gfile:
self.skip_file_load = False self.skip_file_load = False
@ -99,9 +87,9 @@ class SourceViewEventsMixin:
self.skip_file_load = False self.skip_file_load = False
def save_file_as(self): def save_file_as(self):
gfile = self.save_file_dialog() gfile = event_system.emit_and_await("save_file_dialog", (self._current_filename, self._current_file))
self._write_file(gfile, True) self._write_file(gfile, True)
event_system.emit("create_view", (gfile,)) if gfile: event_system.emit("create_view", (gfile,))
def load_file_info(self, gfile): def load_file_info(self, gfile):
info = gfile.query_info("standard::*", 0, cancellable=None) info = gfile.query_info("standard::*", 0, cancellable=None)
@ -156,25 +144,3 @@ class SourceViewEventsMixin:
event_system.emit("set_bottom_labels", (gfile, None, self._current_filetype, None)) event_system.emit("set_bottom_labels", (gfile, None, self._current_filetype, None))
self.update_cursor_position() self.update_cursor_position()
def save_file_dialog(self) -> str:
# TODO: Move Chooser logic to own widget
dlg = Gtk.FileChooserDialog(title = "Please choose a file...", parent = None, action = 1)
dlg.add_buttons("Cancel", Gtk.ResponseType.CANCEL, "Save", Gtk.ResponseType.OK)
dlg.set_do_overwrite_confirmation(True)
dlg.add_filter(self._file_filter_text)
dlg.add_filter(self._file_filter_all)
if self._current_filename == "":
dlg.set_current_name("new.txt")
else:
dlg.set_current_folder(self._current_file.get_parent().get_path())
dlg.set_current_name(self._current_filename)
response = dlg.run()
file = dlg.get_filename() if response == Gtk.ResponseType.OK else ""
dlg.destroy()
return Gio.File.new_for_path(file) if not file == "" else None

View File

@ -0,0 +1,65 @@
# Python imports
# Lib imports
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
from gi.repository import Gio
# Application imports
class SaveFileDialog:
"""docstring for SaveFileDialog."""
def __init__(self):
super(SaveFileDialog, self).__init__()
self._setup_styling()
self._setup_signals()
self._subscribe_to_events()
self._load_widgets()
def _setup_styling(self):
...
def _setup_signals(self):
...
def _subscribe_to_events(self):
event_system.subscribe("save_file_dialog", self.save_file_dialog)
def _load_widgets(self):
self._file_filter_text = Gtk.FileFilter()
self._file_filter_text.set_name("Text Files")
for p in settings.filters.code:
self._file_filter_text.add_pattern(p)
self._file_filter_all = Gtk.FileFilter()
self._file_filter_all.set_name("All Files")
self._file_filter_all.add_pattern("*.*")
def save_file_dialog(self, current_filename: str = "", current_file: Gio.File = None) -> str:
# TODO: Move Chooser logic to own widget
dlg = Gtk.FileChooserDialog(title = "Please choose a file...", parent = None, action = 1)
dlg.add_buttons("Cancel", Gtk.ResponseType.CANCEL, "Save", Gtk.ResponseType.OK)
dlg.set_do_overwrite_confirmation(True)
dlg.add_filter(self._file_filter_text)
dlg.add_filter(self._file_filter_all)
if current_filename == "":
dlg.set_current_name("new.txt")
else:
dlg.set_current_folder(current_file.get_parent().get_path())
dlg.set_current_name(current_filename)
response = dlg.run()
file = dlg.get_filename() if response == Gtk.ResponseType.OK else ""
dlg.destroy()
return Gio.File.new_for_path(file) if not file == "" else None