Mostly completed tab inferastructure setup

This commit is contained in:
itdominator 2024-01-21 18:28:17 -06:00
parent 729b3850a0
commit 386628a949
13 changed files with 120 additions and 144 deletions

View File

@ -70,4 +70,5 @@ class BaseController(SignalsMixins, BaseControllerData):
settings_manager.register_signals_to_builder([self, self.core_widget]) settings_manager.register_signals_to_builder([self, self.core_widget])
def get_core_widget(self): def get_core_widget(self):
return self.core_widget return self.core_widget

View File

@ -31,12 +31,10 @@ class BridgeController:
event_system.emit(f"handle_file_event_{event.originator}", (event,)) event_system.emit(f"handle_file_event_{event.originator}", (event,))
case "load_buffer": case "load_buffer":
event_system.emit(f"handle_file_event_{event.originator}", (event,)) event_system.emit(f"handle_file_event_{event.originator}", (event,))
# case "add_tab": case "load_file":
# event_system.emit(f"add_tab_{event.originator}", (event,)) event_system.emit(f"handle_file_event_{event.originator}", (event,))
case "error": case "error":
content = base64.b64decode( event.content.encode() ).decode("utf-8") content = base64.b64decode( event.content.encode() ).decode("utf-8")
logger.info(content) logger.info(content)
case _: case _:
... ...

View File

@ -29,20 +29,25 @@ class FilesController:
event_system.subscribe(f"handle_file_event_{self.INDEX}", self.handle_file_event) event_system.subscribe(f"handle_file_event_{self.INDEX}", self.handle_file_event)
def set_pre_drop_dnd(self, gfiles): def set_pre_drop_dnd(self, gfiles):
keys = self.opened_files.keys()
for gfile in gfiles: for gfile in gfiles:
fhash = str(gfile.hash()) fname = gfile.get_basename()
if fhash in keys: continue fpath = gfile.get_path()
ftype, fhash = self.insert_to_sessions(fhash, gfile)
content = None 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") 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): def handle_file_event(self, event):
match event.topic: match event.topic:
@ -53,10 +58,13 @@ class FilesController:
if basename: if basename:
event_system.emit(f"updated_tab_{event.originator}", (event.target, basename,)) event_system.emit(f"updated_tab_{event.originator}", (event.target, basename,))
case "close": case "close":
self.close_session(event.target) event_system.emit(f"close_tab_{event.originator}", (event.target))
case "load_buffer": case "load_buffer":
self.load_buffer(event.target) self.load_buffer(event.target)
event_system.emit(f"add_tab_{event.originator}", (event.target, "buffer",)) 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 _: case _:
return return
@ -74,10 +82,6 @@ class FilesController:
self.update_session(fhash, gfile) self.update_session(fhash, gfile)
return gfile.get_basename() return gfile.get_basename()
def close_session(self, target):
del self.opened_files[target]
def update_session(self, fhash, gfile): def update_session(self, fhash, gfile):
info = gfile.query_info("standard::*", 0, cancellable = None) info = gfile.query_info("standard::*", 0, cancellable = None)
ftype = info.get_content_type().replace("x-", "").split("/")[1] ftype = info.get_content_type().replace("x-", "").split("/")[1]

View File

@ -47,6 +47,7 @@ class AceEditor(WebKit2.WebView):
event_system.subscribe(f"new_session_{self.INDEX}", self.new_session) 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"switch_session_{self.INDEX}", self.switch_session)
event_system.subscribe(f"close_session_{self.INDEX}", self.close_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) event_system.subscribe(f"ui_message_{self.INDEX}", self.ui_message)
def _load_settings(self): def _load_settings(self):
@ -78,8 +79,8 @@ class AceEditor(WebKit2.WebView):
except Exception as e: except Exception as e:
logger.info(e) logger.info(e)
def load_file(self, ftype: str, fhash: str, file: str, content: str): def load_file(self, ftype: str, fname: str, fpath: str, content: str):
command = f"loadFile('{ftype}', '{fhash}', '{file}', '{content}')" command = f"loadFile('{ftype}', '{fname}', '{fpath}', '{content}')"
self.run_javascript(command, None, None) self.run_javascript(command, None, None)
def new_session(self): def new_session(self):
@ -94,6 +95,10 @@ class AceEditor(WebKit2.WebView):
command = f"closeSession('{fhash}')" command = f"closeSession('{fhash}')"
self.run_javascript(command, None, None) 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): def ui_message(self, message, mtype):
command = f"displayMessage('{message}', '{mtype}', '3')" command = f"displayMessage('{message}', '{mtype}', '3')"
self.run_javascript(command, None, None) self.run_javascript(command, None, None)

View File

@ -19,9 +19,11 @@ class DnDBox(DnDMixin, Gtk.DrawingArea):
understand how to acount for non DnD events. understand how to acount for non DnD events.
""" """
def __init__(self): def __init__(self, index):
super(DnDBox, self).__init__() super(DnDBox, self).__init__()
self.INDEX = index
self._setup_styling() self._setup_styling()
self._setup_signals() self._setup_signals()
self._subscribe_to_events() self._subscribe_to_events()

View File

@ -19,7 +19,7 @@ class FixedBox(Gtk.Fixed):
def __init__(self, index): def __init__(self, index):
super(FixedBox, self).__init__() super(FixedBox, self).__init__()
self.INDEX = index self.INDEX = index
self._setup_styling() self._setup_styling()
@ -42,7 +42,7 @@ class FixedBox(Gtk.Fixed):
def _load_widgets(self): def _load_widgets(self):
self.ace_editor = AceEditor(self.INDEX) self.ace_editor = AceEditor(self.INDEX)
self.dnd_box = DnDBox() self.dnd_box = DnDBox(self.INDEX)
self.add( self.ace_editor ) self.add( self.ace_editor )
self.add( self.dnd_box ) self.add( self.dnd_box )

View File

@ -14,7 +14,8 @@ class TabBar(Gtk.Notebook):
def __init__(self, index): def __init__(self, index):
super(TabBar, self).__init__() super(TabBar, self).__init__()
self.INDEX = index self.INDEX = index
self.added_tab = None
self.set_group_name("editor_widget") self.set_group_name("editor_widget")
@ -30,18 +31,20 @@ class TabBar(Gtk.Notebook):
... ...
def _setup_signals(self): 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): 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"update_tab_{self.INDEX}", self.update_tab)
event_system.subscribe(f"close_tab_{self.INDEX}", self.close_tab)
def _load_widgets(self): def _load_widgets(self):
start_box = Gtk.Box() start_box = Gtk.Box()
end_box = Gtk.Box() end_box = Gtk.Box()
add_tab = Gtk.Button(label = "+") 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) end_box.add(add_tab)
@ -51,19 +54,39 @@ class TabBar(Gtk.Notebook):
self.set_action_widget(start_box, 0) self.set_action_widget(start_box, 0)
self.set_action_widget(end_box, 1) self.set_action_widget(end_box, 1)
def _switch_page_update(self, notebook, page, page_num): def _button_add_tab(self, widget):
print(page_num)
...
def add_tab_click(self, widget):
event_system.emit(f"new_session_{self.INDEX}") 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]"): def add_tab(self, fhash, title = "[BAD TITLE]"):
container = Gtk.EventBox() container = Gtk.EventBox()
header = TabHeaderWidget(container, self._close_tab) header = TabHeaderWidget(container, self._button_close_tab)
page_num = self.append_page(container, header) page_num = self.append_page(container, header)
container.fhash = fhash container.fhash = fhash
self.added_tab = True
header.label.set_label(title) header.label.set_label(title)
self.set_tab_detachable(container, True) self.set_tab_detachable(container, True)
@ -73,44 +96,23 @@ class TabBar(Gtk.Notebook):
self.set_current_page(page_num) self.set_current_page(page_num)
def update_tab(self, fhash, title = "[BAD TITLE]"): def update_tab(self, fhash, title = "[BAD TITLE]"):
container = Gtk.EventBox() # container = Gtk.EventBox()
header = TabHeaderWidget(container, self._close_tab) # header = TabHeaderWidget(container, self._close_tab)
page_num = self.append_page(container, header) # page_num = self.append_page(container, header)
header.label.set_label(title) # header.label.set_label(title)
self.set_tab_detachable(container, True) # self.set_tab_detachable(container, True)
self.set_tab_reorderable(container, True) # self.set_tab_reorderable(container, True)
self.show_all() # self.show_all()
self.set_current_page(page_num) # self.set_current_page(page_num)
...
# Note: Need to get parent instead given we pass the close_tab method def close_tab(self, fhash):
# from a potentially former notebook. target = None
def _close_tab(self, widget, container): for child in self.get_children():
notebook = container.get_parent() if child.fhash == fhash:
target = child
break
if notebook.get_n_pages() < 2: return self._close_tab(None, target)
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",))

View File

@ -53,4 +53,4 @@ class TabHeaderWidget(Gtk.Box):
close.connect("released", self.close_tab, *(self.content,)) close.connect("released", self.close_tab, *(self.content,))
self.add(self.label) self.add(self.label)
self.add(close) self.add(close)

View File

@ -67,5 +67,4 @@ class DnDMixin:
files.append(gfile) files.append(gfile)
event_system.emit('set_pre_drop_dnd', (files,)) event_system.emit(f"set_pre_drop_dnd_{self.INDEX}", (files,))

View File

@ -17,8 +17,7 @@
<div class="container editor-container"> <div class="container editor-container">
<div class="row"> <div class="row">
<div class="col"> <div class="col">
<pre id="editor"> <pre id="editor"></pre>
</pre>
</div> </div>
</div> </div>
</div> </div>

View File

@ -5,7 +5,6 @@ window.onload = (eve) => {
console.log("Window Loaded..."); console.log("Window Loaded...");
} }
window.onerror = function(msg, url, line, col, error) { 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. // Note that col & error are new to the HTML 5 spec and may not be supported in every browser.
const suppressErrorAlert = false; const suppressErrorAlert = false;

View File

@ -28,16 +28,14 @@ const editorCommands = [
name: "newSession", name: "newSession",
bindKey: {win: "ctrl-t", mac: "ctrl-t"}, bindKey: {win: "ctrl-t", mac: "ctrl-t"},
exec: function(editor) { exec: function(editor) {
let elm = document.querySelectorAll(`[fhash="${currentSession}"]`)[0]; newSession();
newSession(elm);
}, },
readOnly: true readOnly: true
}, { }, {
name: "closeSession", name: "closeSession",
bindKey: {win: "ctrl-w", mac: "ctrl-w"}, bindKey: {win: "ctrl-w", mac: "ctrl-w"},
exec: function(editor) { exec: function(editor) {
let elm = document.querySelectorAll(`[fhash="${currentSession}"]`)[0]; closeSession(currentSession);
closeSession(elm.children[1]);
}, },
readOnly: true readOnly: true
}, { }, {
@ -71,4 +69,4 @@ const editorCommands = [
} }
]; ];

View File

@ -24,16 +24,18 @@ const loadEditor = () => {
editor.setTheme("ace/theme/one_dark"); editor.setTheme("ace/theme/one_dark");
} }
const loadInitialSessionTab = async () => { const loadInitialSessionTab = () => {
newSession(null, editor.getSession()); newSession(null, editor.getSession());
} }
const newSession = async (elm = null, session = null) => {
const newSession = (eve = null, session = null) => {
let ftype = "buffer"; let ftype = "buffer";
let fhash = await getSHA256Hash( new Date().toString() ); let fhash = Date.now().toString();
session = ( isNotNullOrUndefined(session) ) ? session : ace.createEditSession(""); 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); setSession(ftype, fhash, session);
sendMessage("load_buffer", fhash, ""); sendMessage("load_buffer", fhash, "");
@ -46,15 +48,6 @@ const switchSession = (fhash) => {
setSession(ftype, fhash, session); setSession(ftype, fhash, session);
} }
const closeSession = (fhash) => {
delete aceSessions[fhash];
keys = Object.keys(aceSessions);
console.log(keys.length);
}
const setSession = (ftype, fhash, session) => { const setSession = (ftype, fhash, session) => {
currentSession = fhash; currentSession = fhash;
editor.setSession(session); 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, fname, fpath, content) => {
let fhash = Date.now().toString();
session = ace.createEditSession( atob(content) );
aceSessions[fhash] = {"ftype": ftype, "fname": fname, "path": fpath, "session": session};
const loadFile = (ftype, fhash, file, content) => {
session = ace.createEditSession( atob(content) );
aceSessions[fhash] = {"ftype": ftype, "file": file, "session": session};
// let tab = `
// <li class='tab active-tab' role="presentation" fhash='${fhash}' ftype='${ftype}' draggable="true"
// ondragend="dragEnd()" ondragover="dragOver(event)" ondragstart="dragStart(event)"
// >
// <span class='file-name' onclick='switchSession(this)'>${file}</span>
// <span class='close-button' onclick='closeSession(this)'>
// <i class="bi bi-x-square" aria-hidden="true"></i>
// </span>
// </li>
// `;
// TODO: Need to account for given editor we have focused when implimented...
// document.getElementsByClassName("nav-tabs")[0]
// .insertAdjacentHTML('beforeend', tab);
setSession(ftype, fhash, session); setSession(ftype, fhash, session);
sendMessage("load_file", fhash, fname);
} }
const updatedTab = (ftype, fname) => { const updatedTab = (ftype, fname) => {
// let elm = document.querySelectorAll(`[fhash="${currentSession}"]`)[0]; // let elm = document.querySelectorAll(`[fhash="${currentSession}"]`)[0];
// let tabTitleElm = elm.children[0]; // let tabTitleElm = elm.children[0];
@ -126,9 +94,10 @@ const updatedTab = (ftype, fname) => {
const saveSession = () => { const saveSession = () => {
let fhash = currentSession; // let fhash = currentSession;
let session = aceSessions[fhash]["session"]; // let session = aceSessions[fhash]["session"];
let data = session.getValue(); // let data = session.getValue();
sendMessage("save", fhash, data); // sendMessage("save", fhash, data);
console.log("");
} }