generated from itdominator/Python-With-Gtk-Template
Updated save and switch logic
This commit is contained in:
parent
3706ca50ed
commit
75b3313d27
@ -32,4 +32,4 @@ class Application(IPCServer):
|
|||||||
|
|
||||||
raise AppLaunchException(f"{app_name} IPC Server Exists: Will send path(s) to it and close...")
|
raise AppLaunchException(f"{app_name} IPC Server Exists: Will send path(s) to it and close...")
|
||||||
|
|
||||||
Window(args, unknownargs)
|
Window(args, unknownargs)
|
@ -28,11 +28,11 @@ class EditorNotebook(EditorEventsMixin, EditorControllerMixin, Gtk.Notebook):
|
|||||||
def __init__(self):
|
def __init__(self):
|
||||||
super(EditorNotebook, self).__init__()
|
super(EditorNotebook, self).__init__()
|
||||||
|
|
||||||
self.NAME = f"notebook_{self.ccount}"
|
self.NAME = f"notebook_{self.ccount}"
|
||||||
|
self.builder = settings.get_builder()
|
||||||
self.is_editor_focused = False
|
self.is_editor_focused = False
|
||||||
self.set_group_name("editor_widget")
|
|
||||||
self.builder = settings.get_builder()
|
|
||||||
|
|
||||||
|
self.set_group_name("editor_widget")
|
||||||
self.builder.expose_object(self.NAME, self)
|
self.builder.expose_object(self.NAME, self)
|
||||||
|
|
||||||
self._add_action_widgets()
|
self._add_action_widgets()
|
||||||
@ -118,12 +118,13 @@ class EditorNotebook(EditorEventsMixin, EditorControllerMixin, Gtk.Notebook):
|
|||||||
def _switch_page_update(self, notebook, page, page_num):
|
def _switch_page_update(self, notebook, page, page_num):
|
||||||
source_view = page.get_source_view()
|
source_view = page.get_source_view()
|
||||||
gfile = source_view.get_current_file()
|
gfile = source_view.get_current_file()
|
||||||
if gfile:
|
if not gfile:
|
||||||
source_view.load_file_info( gfile )
|
|
||||||
source_view.update_cursor_position()
|
|
||||||
else:
|
|
||||||
event_system.emit("set_path_label", ("",))
|
event_system.emit("set_path_label", ("",))
|
||||||
event_system.emit("set_file_type_label", (source_view._current_filetype,))
|
event_system.emit("set_file_type_label", (source_view._current_filetype,))
|
||||||
|
else:
|
||||||
|
source_view.load_file_info(gfile)
|
||||||
|
source_view.update_cursor_position()
|
||||||
|
source_view.set_bottom_labels(gfile)
|
||||||
|
|
||||||
def _create_view(self, gfile = None):
|
def _create_view(self, gfile = None):
|
||||||
if not self.is_editor_focused: # TODO: Find way to converge this
|
if not self.is_editor_focused: # TODO: Find way to converge this
|
||||||
|
@ -35,6 +35,7 @@ class SourceView(SourceViewEventsMixin, GtkSource.View):
|
|||||||
self._current_filepath: str = None
|
self._current_filepath: str = None
|
||||||
self._current_filetype: str = "buffer"
|
self._current_filetype: str = "buffer"
|
||||||
|
|
||||||
|
self.skip_file_load = False
|
||||||
self._is_changed = False
|
self._is_changed = False
|
||||||
self._ignore_internal_change = False
|
self._ignore_internal_change = False
|
||||||
self._buffer = self.get_buffer()
|
self._buffer = self.get_buffer()
|
||||||
@ -218,8 +219,4 @@ class SourceView(SourceViewEventsMixin, GtkSource.View):
|
|||||||
f.write(text)
|
f.write(text)
|
||||||
f.close()
|
f.close()
|
||||||
|
|
||||||
if (self._current_filename == "" and save_as) or \
|
return gfile
|
||||||
(self._current_filename != "" and not save_as):
|
|
||||||
self.open_file(gfile)
|
|
||||||
else:
|
|
||||||
event_system.emit("create_view", (gfile,))
|
|
||||||
|
@ -79,34 +79,22 @@ class SourceViewEventsMixin:
|
|||||||
self.grab_focus()
|
self.grab_focus()
|
||||||
|
|
||||||
def save_file(self):
|
def save_file(self):
|
||||||
if not self._current_file:
|
self.skip_file_load = True
|
||||||
self.save_file_as()
|
gfile = self._current_file
|
||||||
return
|
|
||||||
|
|
||||||
self._write_file(self._current_file)
|
if not gfile:
|
||||||
|
gfile = self.save_file_dialog()
|
||||||
|
self.skip_file_load = False
|
||||||
|
if not gfile: return
|
||||||
|
|
||||||
|
self.open_file( self._write_file(gfile) )
|
||||||
|
self.skip_file_load = False
|
||||||
|
|
||||||
def save_file_as(self):
|
def save_file_as(self):
|
||||||
# TODO: Move Chooser logic to own widget
|
gfile = self.save_file_dialog()
|
||||||
dlg = Gtk.FileChooserDialog(title="Please choose a file...", parent = None, action = 1)
|
if gfile:
|
||||||
|
|
||||||
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()
|
|
||||||
|
|
||||||
if not file == "":
|
|
||||||
gfile = Gio.File.new_for_path(file)
|
|
||||||
self._write_file(gfile, True)
|
self._write_file(gfile, True)
|
||||||
|
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)
|
||||||
@ -126,6 +114,8 @@ class SourceViewEventsMixin:
|
|||||||
self._current_filetype = info.get_content_type()
|
self._current_filetype = info.get_content_type()
|
||||||
|
|
||||||
def load_file_async(self, gfile):
|
def load_file_async(self, gfile):
|
||||||
|
if self.skip_file_load: return
|
||||||
|
|
||||||
file = GtkSource.File()
|
file = GtkSource.File()
|
||||||
file.set_location(gfile)
|
file.set_location(gfile)
|
||||||
self._file_loader = GtkSource.FileLoader.new(self._buffer, file)
|
self._file_loader = GtkSource.FileLoader.new(self._buffer, file)
|
||||||
@ -134,10 +124,7 @@ class SourceViewEventsMixin:
|
|||||||
self._file_loader.load_finish(res)
|
self._file_loader.load_finish(res)
|
||||||
self._is_changed = False
|
self._is_changed = False
|
||||||
self._document_loaded()
|
self._document_loaded()
|
||||||
|
self.update_labels(gfile)
|
||||||
tab_widget = self.get_parent().get_tab_widget()
|
|
||||||
tab_widget.set_tab_label(self._current_filename)
|
|
||||||
event_system.emit("set_bottom_labels", (gfile, None, self._current_filetype, None))
|
|
||||||
|
|
||||||
self._file_loader.load_async(io_priority = 98,
|
self._file_loader.load_async(io_priority = 98,
|
||||||
cancellable = None,
|
cancellable = None,
|
||||||
@ -145,3 +132,37 @@ class SourceViewEventsMixin:
|
|||||||
progress_callback_data = None,
|
progress_callback_data = None,
|
||||||
callback = finish_load_callback,
|
callback = finish_load_callback,
|
||||||
user_data = (None))
|
user_data = (None))
|
||||||
|
|
||||||
|
|
||||||
|
def update_labels(self, gfile = None):
|
||||||
|
if not gfile: return
|
||||||
|
|
||||||
|
tab_widget = self.get_parent().get_tab_widget()
|
||||||
|
tab_widget.set_tab_label(self._current_filename)
|
||||||
|
self.set_bottom_labels(gfile)
|
||||||
|
|
||||||
|
def set_bottom_labels(self, gfile = None):
|
||||||
|
if not gfile: return
|
||||||
|
event_system.emit("set_bottom_labels", (gfile, None, self._current_filetype, None))
|
||||||
|
|
||||||
|
|
||||||
|
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
|
||||||
|
Loading…
Reference in New Issue
Block a user