From 5512655f3de0481de9920ef889a40de3977f0222 Mon Sep 17 00:00:00 2001 From: itdominator <1itdominator@gmail.com> Date: Tue, 3 Oct 2023 19:39:11 -0500 Subject: [PATCH] Externalized save dialog; separating some event logic --- src/app.py | 2 +- src/core/core_widget.py | 4 +- .../widgets/base/sourceview/source_view.py | 33 ++++------ .../base/sourceview/source_view_events.py | 40 +----------- src/core/widgets/save_file_dialog.py | 65 +++++++++++++++++++ 5 files changed, 86 insertions(+), 58 deletions(-) create mode 100644 src/core/widgets/save_file_dialog.py diff --git a/src/app.py b/src/app.py index d738b38..d6a1010 100644 --- a/src/app.py +++ b/src/app.py @@ -32,4 +32,4 @@ class Application(IPCServer): raise AppLaunchException(f"{app_name} IPC Server Exists: Will send path(s) to it and close...") - Window(args, unknownargs) + Window(args, unknownargs) \ No newline at end of file diff --git a/src/core/core_widget.py b/src/core/core_widget.py index 016682f..12dceaa 100644 --- a/src/core/core_widget.py +++ b/src/core/core_widget.py @@ -9,6 +9,7 @@ from gi.repository import Gtk from .widgets.base.banner_controls import BannerControls from .editors_container import EditorsContainer 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): + SaveFileDialog() self.add(BannerControls()) GeneralInfoWidget() - self.add(EditorsContainer()) + self.add(EditorsContainer()) \ No newline at end of file diff --git a/src/core/widgets/base/sourceview/source_view.py b/src/core/widgets/base/sourceview/source_view.py index 1a17291..95b93c2 100644 --- a/src/core/widgets/base/sourceview/source_view.py +++ b/src/core/widgets/base/sourceview/source_view.py @@ -41,16 +41,6 @@ class SourceView(SourceViewEventsMixin, GtkSource.View): self._buffer = self.get_buffer() 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_signals() self._set_up_dnd() @@ -79,9 +69,17 @@ class SourceView(SourceViewEventsMixin, GtkSource.View): def _setup_signals(self): 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('changed', self._is_modified) + def _subscribe_to_events(self): + ... + + def _load_widgets(self): + ... + + def _document_loaded(self): for provider in self._completion.get_providers(): self._completion.remove_provider(provider) @@ -99,12 +97,6 @@ class SourceView(SourceViewEventsMixin, GtkSource.View): self._completion.add_provider(py_completion_provider) - def _subscribe_to_events(self): - ... - - def _load_widgets(self): - ... - def _create_default_tag(self): self._general_style_tag = self._buffer.create_tag('general_style') self._general_style_tag.set_property('size', 100) @@ -114,9 +106,7 @@ class SourceView(SourceViewEventsMixin, GtkSource.View): self._is_changed = True self.update_cursor_position() - def _on_cursor_move(self, buf, cursor_iter, mark, user_data = None): - if mark != buf.get_insert(): return - + def _on_widget_focus(self, widget, eve = None): target = self.get_parent().get_parent().NAME 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_encoding_label") 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() def _set_up_dnd(self): diff --git a/src/core/widgets/base/sourceview/source_view_events.py b/src/core/widgets/base/sourceview/source_view_events.py index cc0400a..33aeec7 100644 --- a/src/core/widgets/base/sourceview/source_view_events.py +++ b/src/core/widgets/base/sourceview/source_view_events.py @@ -48,18 +48,6 @@ class SourceViewEventsMixin: row = iter.get_line() + 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}",)) def got_to_line(self, line: int = 0): @@ -89,7 +77,7 @@ class SourceViewEventsMixin: def save_file(self): 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: self.skip_file_load = False @@ -99,9 +87,9 @@ class SourceViewEventsMixin: self.skip_file_load = False 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) - event_system.emit("create_view", (gfile,)) + if gfile: event_system.emit("create_view", (gfile,)) def load_file_info(self, gfile): 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)) 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 diff --git a/src/core/widgets/save_file_dialog.py b/src/core/widgets/save_file_dialog.py new file mode 100644 index 0000000..6c28eec --- /dev/null +++ b/src/core/widgets/save_file_dialog.py @@ -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