From 4e06bc3334e06e448b18e579335982a5f15b605d Mon Sep 17 00:00:00 2001 From: itdominator <1itdominator@gmail.com> Date: Sun, 21 Jan 2024 23:45:29 -0600 Subject: [PATCH] Completed tab CRUD flow --- src/core/controllers/files_controller.py | 36 +++++++------ src/core/widgets/base/webkit/ace_editor.py | 5 ++ src/core/widgets/controls/tab_bar.py | 16 +++--- src/libs/data_types/event.py | 4 +- .../resources/js/newton/keybinding-newton.js | 2 +- .../resources/js/newton/ui-logic.js | 50 ++++++++----------- .../context_path/resources/js/newton/utils.js | 6 ++- 7 files changed, 60 insertions(+), 59 deletions(-) diff --git a/src/core/controllers/files_controller.py b/src/core/controllers/files_controller.py index d90b121..4e3c172 100644 --- a/src/core/controllers/files_controller.py +++ b/src/core/controllers/files_controller.py @@ -6,6 +6,7 @@ import base64 import gi gi.require_version('Gtk', '3.0') from gi.repository import Gtk +from gi.repository import Gio # Application imports @@ -52,35 +53,38 @@ class FilesController: def handle_file_event(self, event): match event.topic: case "save": - content = base64.b64decode( event.content.encode() ).decode("utf-8") - basename = self.save_session(event.target, content) + content = base64.b64decode( event.content.encode() ).decode("utf-8") + ftype, fname, fpath = self.save_session(event.ftype, event.fpath, content) - if basename: - event_system.emit(f"updated_tab_{event.originator}", (event.target, basename,)) + if ftype and fname and fpath: + event_system.emit(f"update_tab_{event.originator}", (event.fhash, fname,)) + event_system.emit(f"updated_session_{event.originator}", (event.fhash, ftype, fname, fpath)) case "close": - event_system.emit(f"close_tab_{event.originator}", (event.target)) + event_system.emit(f"close_tab_{event.originator}", (event.fhash)) case "load_buffer": - self.load_buffer(event.target) - event_system.emit(f"add_tab_{event.originator}", (event.target, "buffer",)) + self.load_buffer(event.fhash) + event_system.emit(f"add_tab_{event.originator}", (event.fhash, "buffer",)) case "load_file": content = base64.b64decode( event.content.encode() ).decode("utf-8") - event_system.emit(f"add_tab_with_name_{event.originator}", (event.target, content,)) + event_system.emit(f"add_tab_with_name_{event.originator}", (event.fhash, content,)) case _: return def load_buffer(self, fhash): self.opened_files[fhash] = {"file": None, "ftype": "buffer"} - def save_session(self, fhash, content): - ftype = self.opened_files[fhash]["ftype"] + + def save_session(self, ftype, fpath, content): gfile = event_system.emit_and_await( "save_file_dialog", ("", None) - ) if fhash == "buffer" else self.opened_files[fhash]["file"] + ) if ftype == "buffer" else Gio.File.new_for_path(fpath) - file_written = self.write_to_file(fhash, gfile, content) - if fhash == "buffer" and file_written: - self.update_session(fhash, gfile) - return gfile.get_basename() + file_written = self.write_to_file(gfile, content) + if ftype == "buffer" and file_written: + info = gfile.query_info("standard::*", 0, cancellable = None) + ftype = info.get_content_type().replace("x-", "").split("/")[1] + + return ftype, gfile.get_basename(), gfile.get_path() def update_session(self, fhash, gfile): info = gfile.query_info("standard::*", 0, cancellable = None) @@ -90,7 +94,7 @@ class FilesController: return ftype, fhash - def write_to_file(self, fhash, gfile, content): + def write_to_file(self, gfile, content): with open(gfile.get_path(), 'w') as outfile: try: outfile.write(content) diff --git a/src/core/widgets/base/webkit/ace_editor.py b/src/core/widgets/base/webkit/ace_editor.py index f6256bf..77f2d7f 100644 --- a/src/core/widgets/base/webkit/ace_editor.py +++ b/src/core/widgets/base/webkit/ace_editor.py @@ -46,6 +46,7 @@ class AceEditor(WebKit2.WebView): event_system.subscribe(f"load_file_{self.INDEX}", self.load_file) event_system.subscribe(f"new_session_{self.INDEX}", self.new_session) event_system.subscribe(f"switch_session_{self.INDEX}", self.switch_session) + event_system.subscribe(f"updated_session_{self.INDEX}", self.updated_session) event_system.subscribe(f"close_session_{self.INDEX}", self.close_session) event_system.subscribe(f"remove_session_{self.INDEX}", self.remove_session) event_system.subscribe(f"ui_message_{self.INDEX}", self.ui_message) @@ -91,6 +92,10 @@ class AceEditor(WebKit2.WebView): command = f"switchSession('{fhash}')" self.run_javascript(command, None, None) + def updated_session(self, fhash, ftype, fname, fpath): + command = f"updateSession('{fhash}', '{ftype}', '{fname}', '{fpath}')" + self.run_javascript(command, None, None) + def close_session(self, fhash): command = f"closeSession('{fhash}')" self.run_javascript(command, None, None) diff --git a/src/core/widgets/controls/tab_bar.py b/src/core/widgets/controls/tab_bar.py index 8d95288..2938bca 100644 --- a/src/core/widgets/controls/tab_bar.py +++ b/src/core/widgets/controls/tab_bar.py @@ -96,17 +96,13 @@ class TabBar(Gtk.Notebook): self.set_current_page(page_num) def update_tab(self, fhash, title = "[BAD TITLE]"): - # container = Gtk.EventBox() - # header = TabHeaderWidget(container, self._close_tab) - # page_num = self.append_page(container, header) + for child in self.get_children(): + if child.fhash == fhash: + target = child + break - # header.label.set_label(title) - # self.set_tab_detachable(container, True) - # self.set_tab_reorderable(container, True) - - # self.show_all() - # self.set_current_page(page_num) - ... + label = self.get_tab_label(target).get_children()[0] + label.set_label(title) def close_tab(self, fhash): target = None diff --git a/src/libs/data_types/event.py b/src/libs/data_types/event.py index 712d590..506123d 100644 --- a/src/libs/data_types/event.py +++ b/src/libs/data_types/event.py @@ -11,6 +11,8 @@ import gi @dataclass class Event: topic: str - target: str + ftype: str + fhash: str + fpath: str content: str originator: int = -1 \ No newline at end of file diff --git a/user_config/usr/share/newton/context_path/resources/js/newton/keybinding-newton.js b/user_config/usr/share/newton/context_path/resources/js/newton/keybinding-newton.js index 28c028b..13797d8 100644 --- a/user_config/usr/share/newton/context_path/resources/js/newton/keybinding-newton.js +++ b/user_config/usr/share/newton/context_path/resources/js/newton/keybinding-newton.js @@ -21,7 +21,7 @@ const editorCommands = [ name: "saveSession", bindKey: {win: "ctrl-s", mac: "ctrl-s"}, exec: function(editor) { - saveSession(); + saveSession(currentSession); }, readOnly: true }, { diff --git a/user_config/usr/share/newton/context_path/resources/js/newton/ui-logic.js b/user_config/usr/share/newton/context_path/resources/js/newton/ui-logic.js index ceed511..4ceeb3a 100644 --- a/user_config/usr/share/newton/context_path/resources/js/newton/ui-logic.js +++ b/user_config/usr/share/newton/context_path/resources/js/newton/ui-logic.js @@ -33,12 +33,13 @@ const loadInitialSessionTab = () => { const newSession = (eve = null, session = null) => { let ftype = "buffer"; let fhash = Date.now().toString(); + let fpath = "" session = ( isNotNullOrUndefined(session) ) ? session : ace.createEditSession(""); - aceSessions[fhash] = {"ftype": ftype, "fname": "", "path": "", "session": session}; + aceSessions[fhash] = {"ftype": ftype, "fname": "", "fpath": fpath, "session": session}; setSession(ftype, fhash, session); - sendMessage("load_buffer", fhash, ""); + sendMessage("load_buffer", ftype, fhash, fpath, ""); } const switchSession = (fhash) => { @@ -57,9 +58,18 @@ const setSession = (ftype, fhash, session) => { } } +const updateSession = (fhash, ftype, fname, fpath) => { + aceSessions[fhash]["ftype"] = ftype; + aceSessions[fhash]["fname"] = fname; + aceSessions[fhash]["fpath"] = fpath; +} + const closeSession = (fhash) => { + let ftype = aceSessions["ftype"]; + let fpath = aceSessions["fpath"]; + delete aceSessions[fhash]; - sendMessage("close", fhash, ""); + sendMessage("close", ftype, fhash, fpath, ""); } const removeSession = (fhash) => { @@ -69,35 +79,17 @@ const removeSession = (fhash) => { const loadFile = (ftype, fname, fpath, content) => { let fhash = Date.now().toString(); session = ace.createEditSession( atob(content) ); - aceSessions[fhash] = {"ftype": ftype, "fname": fname, "path": fpath, "session": session}; + aceSessions[fhash] = {"ftype": ftype, "fname": fname, "fpath": fpath, "session": session}; setSession(ftype, fhash, session); - sendMessage("load_file", fhash, fname); + sendMessage("load_file", ftype, fhash, fpath, fname); } +const saveSession = (fhash) => { + let ftype = aceSessions[fhash]["ftype"]; + let fpath = aceSessions[fhash]["fpath"]; + let session = aceSessions[fhash]["session"]; + let content = session.getValue(); - - - -const updatedTab = (ftype, fname) => { -// let elm = document.querySelectorAll(`[fhash="${currentSession}"]`)[0]; -// let tabTitleElm = elm.children[0]; - - aceSessions[currentSession]["ftype"] = ftype; - aceSessions[currentSession]["file"] = fname; - -// elm.setAttribute("ftype", ftype); -// tabTitleElm.textContent = fname; + sendMessage("save", ftype, fhash, fpath, content); } - - - - -const saveSession = () => { -// let fhash = currentSession; -// let session = aceSessions[fhash]["session"]; -// let data = session.getValue(); - -// sendMessage("save", fhash, data); - console.log(""); -} \ No newline at end of file diff --git a/user_config/usr/share/newton/context_path/resources/js/newton/utils.js b/user_config/usr/share/newton/context_path/resources/js/newton/utils.js index 2e7a6e8..c778273 100644 --- a/user_config/usr/share/newton/context_path/resources/js/newton/utils.js +++ b/user_config/usr/share/newton/context_path/resources/js/newton/utils.js @@ -27,10 +27,12 @@ const displayMessage = (message, type, timeout, msgWindow = "page-alert-zone") = } } -const sendMessage = (topic, target, content) => { +const sendMessage = (topic, ftype, fhash, fpath, content) => { const messageBody = { "topic": topic, - 'target': target, + "ftype": ftype, + "fhash": fhash, + "fpath": fpath, "content": btoa(content) };