From 386628a949bc9a1a631e43db74d4a570f5334431 Mon Sep 17 00:00:00 2001 From: itdominator <1itdominator@gmail.com> Date: Sun, 21 Jan 2024 18:28:17 -0600 Subject: [PATCH] Mostly completed tab inferastructure setup --- src/core/controllers/base_controller.py | 3 +- src/core/controllers/bridge_controller.py | 8 +- src/core/controllers/files_controller.py | 34 +++--- src/core/widgets/base/webkit/ace_editor.py | 9 +- src/core/widgets/base/webkit/dnd_box.py | 4 +- src/core/widgets/base/webkit/fixed_box.py | 4 +- src/core/widgets/controls/tab_bar.py | 100 +++++++++--------- src/core/widgets/tab_header_widget.py | 2 +- src/libs/mixins/dnd_mixin.py | 3 +- .../usr/share/newton/context_path/index.html | 3 +- .../resources/js/newton/events.js | 1 - .../resources/js/newton/keybinding-newton.js | 8 +- .../resources/js/newton/ui-logic.js | 85 +++++---------- 13 files changed, 120 insertions(+), 144 deletions(-) diff --git a/src/core/controllers/base_controller.py b/src/core/controllers/base_controller.py index 96d355a..cfe4d38 100644 --- a/src/core/controllers/base_controller.py +++ b/src/core/controllers/base_controller.py @@ -70,4 +70,5 @@ class BaseController(SignalsMixins, BaseControllerData): settings_manager.register_signals_to_builder([self, self.core_widget]) def get_core_widget(self): - return self.core_widget \ No newline at end of file + return self.core_widget + diff --git a/src/core/controllers/bridge_controller.py b/src/core/controllers/bridge_controller.py index b068704..79735ca 100644 --- a/src/core/controllers/bridge_controller.py +++ b/src/core/controllers/bridge_controller.py @@ -31,12 +31,10 @@ class BridgeController: event_system.emit(f"handle_file_event_{event.originator}", (event,)) case "load_buffer": event_system.emit(f"handle_file_event_{event.originator}", (event,)) - # case "add_tab": - # event_system.emit(f"add_tab_{event.originator}", (event,)) - + case "load_file": + event_system.emit(f"handle_file_event_{event.originator}", (event,)) case "error": content = base64.b64decode( event.content.encode() ).decode("utf-8") logger.info(content) case _: - ... - + ... \ No newline at end of file diff --git a/src/core/controllers/files_controller.py b/src/core/controllers/files_controller.py index 9a6412c..d90b121 100644 --- a/src/core/controllers/files_controller.py +++ b/src/core/controllers/files_controller.py @@ -29,20 +29,25 @@ class FilesController: event_system.subscribe(f"handle_file_event_{self.INDEX}", self.handle_file_event) def set_pre_drop_dnd(self, gfiles): - keys = self.opened_files.keys() - for gfile in gfiles: - fhash = str(gfile.hash()) - if fhash in keys: continue - - ftype, fhash = self.insert_to_sessions(fhash, gfile) - + fname = gfile.get_basename() + fpath = gfile.get_path() content = None - path = gfile.get_path() - with open(path) as f: + + with open(fpath) as f: content = base64.b64encode( f.read().encode(), altchars = None ).decode("utf-8") - event_system.emit("load_file", (ftype, fhash, gfile.get_basename(), content)) + info = gfile.query_info("standard::*", 0, cancellable = None) + ftype = info.get_content_type().replace("x-", "").split("/")[1] + event_system.emit( + f"load_file_{self.INDEX}", + ( + ftype, + fname, + fpath, + content + ) + ) def handle_file_event(self, event): match event.topic: @@ -53,10 +58,13 @@ class FilesController: if basename: event_system.emit(f"updated_tab_{event.originator}", (event.target, basename,)) case "close": - self.close_session(event.target) + event_system.emit(f"close_tab_{event.originator}", (event.target)) case "load_buffer": self.load_buffer(event.target) event_system.emit(f"add_tab_{event.originator}", (event.target, "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,)) case _: return @@ -74,10 +82,6 @@ class FilesController: self.update_session(fhash, gfile) return gfile.get_basename() - - def close_session(self, target): - del self.opened_files[target] - def update_session(self, fhash, gfile): info = gfile.query_info("standard::*", 0, cancellable = None) ftype = info.get_content_type().replace("x-", "").split("/")[1] diff --git a/src/core/widgets/base/webkit/ace_editor.py b/src/core/widgets/base/webkit/ace_editor.py index 6992107..f6256bf 100644 --- a/src/core/widgets/base/webkit/ace_editor.py +++ b/src/core/widgets/base/webkit/ace_editor.py @@ -47,6 +47,7 @@ class AceEditor(WebKit2.WebView): 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"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) def _load_settings(self): @@ -78,8 +79,8 @@ class AceEditor(WebKit2.WebView): except Exception as e: logger.info(e) - def load_file(self, ftype: str, fhash: str, file: str, content: str): - command = f"loadFile('{ftype}', '{fhash}', '{file}', '{content}')" + def load_file(self, ftype: str, fname: str, fpath: str, content: str): + command = f"loadFile('{ftype}', '{fname}', '{fpath}', '{content}')" self.run_javascript(command, None, None) def new_session(self): @@ -94,6 +95,10 @@ class AceEditor(WebKit2.WebView): command = f"closeSession('{fhash}')" self.run_javascript(command, None, None) + def remove_session(self, fhash): + command = f"removeSession('{fhash}')" + self.run_javascript(command, None, None) + def ui_message(self, message, mtype): command = f"displayMessage('{message}', '{mtype}', '3')" self.run_javascript(command, None, None) diff --git a/src/core/widgets/base/webkit/dnd_box.py b/src/core/widgets/base/webkit/dnd_box.py index 91dad8f..a6cdb89 100644 --- a/src/core/widgets/base/webkit/dnd_box.py +++ b/src/core/widgets/base/webkit/dnd_box.py @@ -19,9 +19,11 @@ class DnDBox(DnDMixin, Gtk.DrawingArea): understand how to acount for non DnD events. """ - def __init__(self): + def __init__(self, index): super(DnDBox, self).__init__() + self.INDEX = index + self._setup_styling() self._setup_signals() self._subscribe_to_events() diff --git a/src/core/widgets/base/webkit/fixed_box.py b/src/core/widgets/base/webkit/fixed_box.py index d806492..6d63000 100644 --- a/src/core/widgets/base/webkit/fixed_box.py +++ b/src/core/widgets/base/webkit/fixed_box.py @@ -19,7 +19,7 @@ class FixedBox(Gtk.Fixed): def __init__(self, index): super(FixedBox, self).__init__() - + self.INDEX = index self._setup_styling() @@ -42,7 +42,7 @@ class FixedBox(Gtk.Fixed): def _load_widgets(self): self.ace_editor = AceEditor(self.INDEX) - self.dnd_box = DnDBox() + self.dnd_box = DnDBox(self.INDEX) self.add( self.ace_editor ) self.add( self.dnd_box ) diff --git a/src/core/widgets/controls/tab_bar.py b/src/core/widgets/controls/tab_bar.py index dc8bb8b..8d95288 100644 --- a/src/core/widgets/controls/tab_bar.py +++ b/src/core/widgets/controls/tab_bar.py @@ -14,7 +14,8 @@ class TabBar(Gtk.Notebook): def __init__(self, index): super(TabBar, self).__init__() - self.INDEX = index + self.INDEX = index + self.added_tab = None self.set_group_name("editor_widget") @@ -30,18 +31,20 @@ class TabBar(Gtk.Notebook): ... def _setup_signals(self): - self.connect("switch-page", self._switch_page_update) + self.connect("switch-page", self._switch_page_update) def _subscribe_to_events(self): - event_system.subscribe(f"add_tab_{self.INDEX}", self.add_tab) + event_system.subscribe(f"add_tab_{self.INDEX}", self.add_tab) + event_system.subscribe(f"add_tab_with_name_{self.INDEX}", self.add_tab) event_system.subscribe(f"update_tab_{self.INDEX}", self.update_tab) + event_system.subscribe(f"close_tab_{self.INDEX}", self.close_tab) def _load_widgets(self): start_box = Gtk.Box() end_box = Gtk.Box() add_tab = Gtk.Button(label = "+") - add_tab.connect("released", self.add_tab_click) + add_tab.connect("released", self._button_add_tab) end_box.add(add_tab) @@ -51,19 +54,39 @@ class TabBar(Gtk.Notebook): self.set_action_widget(start_box, 0) self.set_action_widget(end_box, 1) - def _switch_page_update(self, notebook, page, page_num): - print(page_num) - ... - - def add_tab_click(self, widget): + def _button_add_tab(self, widget): event_system.emit(f"new_session_{self.INDEX}") + def _button_close_tab(self, widget, container): + self._close_tab(widget, container) + event_system.emit(f"remove_session_{self.INDEX}", (container.fhash)) + + # Note: Need to get parent instead given we pass the close_tab method + # from a potentially former notebook. + def _close_tab(self, widget, container): + notebook = container.get_parent() + + if notebook.get_n_pages() < 2: return + + page_num = notebook.page_num(container) + notebook.remove_page(page_num) + # container._cancel_current_file_watchers() + + def _switch_page_update(self, notebook, container, page_num): + if self.added_tab or self.added_tab == None: + self.added_tab = False + return + + event_system.emit(f"switch_session_{self.INDEX}", (container.fhash,)) + + def add_tab(self, fhash, title = "[BAD TITLE]"): - container = Gtk.EventBox() - header = TabHeaderWidget(container, self._close_tab) - page_num = self.append_page(container, header) + container = Gtk.EventBox() + header = TabHeaderWidget(container, self._button_close_tab) + page_num = self.append_page(container, header) container.fhash = fhash + self.added_tab = True header.label.set_label(title) self.set_tab_detachable(container, True) @@ -73,44 +96,23 @@ 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) + # container = Gtk.EventBox() + # header = TabHeaderWidget(container, self._close_tab) + # page_num = self.append_page(container, header) - header.label.set_label(title) - self.set_tab_detachable(container, True) - self.set_tab_reorderable(container, True) + # 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) + # self.show_all() + # self.set_current_page(page_num) + ... - # Note: Need to get parent instead given we pass the close_tab method - # from a potentially former notebook. - def _close_tab(self, widget, container): - notebook = container.get_parent() + def close_tab(self, fhash): + target = None + for child in self.get_children(): + if child.fhash == fhash: + target = child + break - if notebook.get_n_pages() < 2: return - - page_num = notebook.page_num(container) - - event_system.emit(f"close_session_{self.INDEX}", (container.fhash)) - notebook.remove_page(page_num) - - - # def close_tab(self, button, container, source_view, eve = None): - # notebook = container.get_parent() - # if notebook.NAME == "notebook_1" and notebook.get_n_pages() == 1: - # return - - # file_type = source_view.get_filetype() - # if not file_type == "buffer": - # uri = source_view.get_current_file().get_uri() - # event_system.emit("textDocument/didClose", (file_type, uri,)) - - # page_num = notebook.page_num(container) - # source_view._cancel_current_file_watchers() - # notebook.remove_page(page_num) - - # if notebook.NAME == "notebook_2" and notebook.get_n_pages() == 0: - # notebook.hide() - # event_system.emit("focused_target_changed", ("notebook_1",)) \ No newline at end of file + self._close_tab(None, target) \ No newline at end of file diff --git a/src/core/widgets/tab_header_widget.py b/src/core/widgets/tab_header_widget.py index 4d079ef..21ffa83 100644 --- a/src/core/widgets/tab_header_widget.py +++ b/src/core/widgets/tab_header_widget.py @@ -53,4 +53,4 @@ class TabHeaderWidget(Gtk.Box): close.connect("released", self.close_tab, *(self.content,)) self.add(self.label) - self.add(close) \ No newline at end of file + self.add(close) diff --git a/src/libs/mixins/dnd_mixin.py b/src/libs/mixins/dnd_mixin.py index 4825a99..644dc07 100644 --- a/src/libs/mixins/dnd_mixin.py +++ b/src/libs/mixins/dnd_mixin.py @@ -67,5 +67,4 @@ class DnDMixin: files.append(gfile) - event_system.emit('set_pre_drop_dnd', (files,)) - + event_system.emit(f"set_pre_drop_dnd_{self.INDEX}", (files,)) diff --git a/user_config/usr/share/newton/context_path/index.html b/user_config/usr/share/newton/context_path/index.html index ab219b1..26b553e 100644 --- a/user_config/usr/share/newton/context_path/index.html +++ b/user_config/usr/share/newton/context_path/index.html @@ -17,8 +17,7 @@
-
-                
+

             
diff --git a/user_config/usr/share/newton/context_path/resources/js/newton/events.js b/user_config/usr/share/newton/context_path/resources/js/newton/events.js index 9f1e405..7fbf25a 100644 --- a/user_config/usr/share/newton/context_path/resources/js/newton/events.js +++ b/user_config/usr/share/newton/context_path/resources/js/newton/events.js @@ -5,7 +5,6 @@ window.onload = (eve) => { console.log("Window Loaded..."); } - window.onerror = function(msg, url, line, col, error) { // Note that col & error are new to the HTML 5 spec and may not be supported in every browser. const suppressErrorAlert = false; 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 64d606e..28c028b 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 @@ -28,16 +28,14 @@ const editorCommands = [ name: "newSession", bindKey: {win: "ctrl-t", mac: "ctrl-t"}, exec: function(editor) { - let elm = document.querySelectorAll(`[fhash="${currentSession}"]`)[0]; - newSession(elm); + newSession(); }, readOnly: true }, { name: "closeSession", bindKey: {win: "ctrl-w", mac: "ctrl-w"}, exec: function(editor) { - let elm = document.querySelectorAll(`[fhash="${currentSession}"]`)[0]; - closeSession(elm.children[1]); + closeSession(currentSession); }, readOnly: true }, { @@ -71,4 +69,4 @@ const editorCommands = [ } -]; +]; \ No newline at end of file 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 15b3a02..ceed511 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 @@ -24,16 +24,18 @@ const loadEditor = () => { editor.setTheme("ace/theme/one_dark"); } -const loadInitialSessionTab = async () => { +const loadInitialSessionTab = () => { newSession(null, editor.getSession()); } -const newSession = async (elm = null, session = null) => { + + +const newSession = (eve = null, session = null) => { let ftype = "buffer"; - let fhash = await getSHA256Hash( new Date().toString() ); + let fhash = Date.now().toString(); session = ( isNotNullOrUndefined(session) ) ? session : ace.createEditSession(""); - aceSessions[fhash] = {"ftype": ftype, "file": "", "session": session}; + aceSessions[fhash] = {"ftype": ftype, "fname": "", "path": "", "session": session}; setSession(ftype, fhash, session); sendMessage("load_buffer", fhash, ""); @@ -46,15 +48,6 @@ const switchSession = (fhash) => { setSession(ftype, fhash, session); } - -const closeSession = (fhash) => { - delete aceSessions[fhash]; - - keys = Object.keys(aceSessions); - console.log(keys.length); -} - - const setSession = (ftype, fhash, session) => { currentSession = fhash; editor.setSession(session); @@ -64,53 +57,28 @@ const setSession = (ftype, fhash, session) => { } } +const closeSession = (fhash) => { + delete aceSessions[fhash]; + sendMessage("close", fhash, ""); +} +const removeSession = (fhash) => { + delete aceSessions[fhash]; +} - - - - - - - - - - - - - - - - - - - - - - - -const loadFile = (ftype, fhash, file, content) => { - session = ace.createEditSession( atob(content) ); - aceSessions[fhash] = {"ftype": ftype, "file": file, "session": session}; - -// let tab = ` -// -// `; - - // TODO: Need to account for given editor we have focused when implimented... -// document.getElementsByClassName("nav-tabs")[0] -// .insertAdjacentHTML('beforeend', tab); +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}; setSession(ftype, fhash, session); + sendMessage("load_file", fhash, fname); } + + + + const updatedTab = (ftype, fname) => { // let elm = document.querySelectorAll(`[fhash="${currentSession}"]`)[0]; // let tabTitleElm = elm.children[0]; @@ -126,9 +94,10 @@ const updatedTab = (ftype, fname) => { const saveSession = () => { - let fhash = currentSession; - let session = aceSessions[fhash]["session"]; - let data = session.getValue(); +// let fhash = currentSession; +// let session = aceSessions[fhash]["session"]; +// let data = session.getValue(); - sendMessage("save", fhash, data); +// sendMessage("save", fhash, data); + console.log(""); } \ No newline at end of file