Refactored buttons and call structures

This commit is contained in:
itdominator 2024-09-07 16:46:57 -05:00
parent b7bac2d5ae
commit 63eec6f1af
7 changed files with 139 additions and 132 deletions

View File

@ -21,7 +21,7 @@ class CenterContainer(Gtk.Box):
self._subscribe_to_events() self._subscribe_to_events()
self._load_widgets() self._load_widgets()
self.show_all() self.show()
def _setup_styling(self): def _setup_styling(self):

View File

@ -9,43 +9,112 @@ from gi.repository import Gtk
class BottomButtonBox(Gtk.ButtonBox): class BottomButtonBox(Gtk.Box):
def __init__(self): def __init__(self):
super(BottomButtonBox, self).__init__() super(BottomButtonBox, self).__init__()
self._setup_styling() self._setup_styling()
self._setup_signals() self._setup_signals()
self._subscribe_to_events() self._subscribe_to_events()
self._load_widgets() self._load_buttons()
self.show_all() self.show()
def _setup_styling(self): def _setup_styling(self):
ctx = self.get_style_context() ctx = self.get_style_context()
ctx.add_class("bottom-button-box") ctx.add_class("bottom-button-box")
self.set_property("halign", Gtk.Align.END)
def _setup_signals(self): def _setup_signals(self):
... ...
def _subscribe_to_events(self): def _subscribe_to_events(self):
... ...
def _load_widgets(self): def _load_buttons(self):
self.initialize_btn = Gtk.Button(label = "Send Initialize Message") self.initialize_btn = Gtk.Button(label = "Initialize Message")
self.initialized_btn = Gtk.Button(label = "Initialized Message")
self.notification_btn = Gtk.Button(label = "Send Notification") self.notification_btn = Gtk.Button(label = "Send Notification")
self.message_btn = Gtk.Button(label = "Send Message") self.request_btn = Gtk.Button(label = "Send Request")
self.start_stop_lsp_btn = Gtk.Button(label = "Start LSP") self.full_init_btn = Gtk.Button(label = "Full Init")
self.start_stop_lsp_btn = Gtk.Button(label = "LSP")
self.start_stop_lsp_btn.running = False
self.add(self.initialize_btn) self.initialize_btn.connect("clicked", self.send_initialize_message)
self.add(self.notification_btn) self.initialized_btn.connect("clicked", self.send_initialized_message)
self.add(self.message_btn) self.notification_btn.connect("clicked", self.send_notification)
self.add(self.start_stop_lsp_btn) self.request_btn.connect("clicked", self.send_request)
self.start_stop_lsp_btn.connect("clicked", self.start_stop_lsp)
self.full_init_btn.connect("clicked", self.full_init)
def connect_widget_signals(self, parent): self.start_stop_lsp_btn.show()
if not parent: return self.full_init_btn.show()
self.initialize_btn.connect("clicked", parent.send_initialize_message) for child in [
self.notification_btn.connect("clicked", parent.send_notification) self.initialize_btn,
self.message_btn.connect("clicked", parent.send_message) self.initialized_btn,
self.start_stop_lsp_btn.connect("clicked", parent.start_stop_lsp) self.notification_btn,
self.request_btn,
self.start_stop_lsp_btn,
self.full_init_btn
]:
self.add(child)
# self.set_child_secondary(child, True)
child.set_always_show_image(True)
child.set_image( Gtk.Image.new_from_icon_name("gtk-media-play", Gtk.IconSize.BUTTON) )
self.start_stop_lsp_btn.set_image( Gtk.Image.new_from_icon_name("gtk-media-play", Gtk.IconSize.BUTTON) )
self.full_init_btn.set_image( Gtk.Image.new_from_icon_name("gtk-media-play", Gtk.IconSize.BUTTON) )
def send_initialize_message(self, button, eve = None):
button.hide()
self.initialized_btn.show()
self.get_parent().send_initialize_message()
def send_initialized_message(self, button, eve = None):
self.initialize_btn.hide()
self.initialized_btn.hide()
self.notification_btn.show()
self.request_btn.show()
self.get_parent().send_initialized_message()
def send_notification(self, button, eve = None):
children = self.get_children()
self.get_parent().button_send_notification()
def send_request(self, button, eve = None):
children = self.get_children()
self.get_parent().button_send_request()
def start_stop_lsp(self, button, eve = None):
if not button.running:
button.running = True
self.initialize_btn.show()
self.full_init_btn.hide()
button.set_image( Gtk.Image.new_from_icon_name("gtk-media-stop", Gtk.IconSize.BUTTON) )
else:
self.rest_buttons()
self.get_parent().start_stop_lsp()
def full_init(self, button, eve = None):
button.hide()
self.start_stop_lsp(self.start_stop_lsp_btn)
self.send_initialize_message(self.initialize_btn)
self.send_initialized_message(self.initialized_btn)
def rest_buttons(self):
self.start_stop_lsp_btn.running = False
self.initialize_btn.hide()
self.initialized_btn.hide()
self.notification_btn.hide()
self.request_btn.hide()
self.full_init_btn.show()
self.start_stop_lsp_btn.set_image( Gtk.Image.new_from_icon_name("gtk-media-play", Gtk.IconSize.BUTTON) )

View File

@ -46,27 +46,16 @@ class LogList(Gtk.ListBox):
def _load_widgets(self): def _load_widgets(self):
... ...
def add_log_entry(self, m_type: str, data: str or dict): def add_log_entry(self, m_type: str, data: dict):
message = None message = json.dumps(data, separators = (',', ':'), indent = 4)
json_data = None
if isinstance(data, str): frame = Gtk.Frame()
parts = data.split("\n", 2) revealer = Gtk.Revealer()
jsonStr = parts[2] label = Gtk.Label(label=message)
json_data = json.loads(jsonStr) lbl_str = f"> {m_type} Message: {data['id']}" if "id" in data.keys() else f"> {m_type} Notification"
messageStr = json.dumps(json_data, separators = (',', ':'), indent = 4)
message = f"{parts[0]}\r\n\r\n{messageStr}"
if isinstance(data, dict):
json_data = data
message = json.dumps(data, separators = (',', ':'), indent = 4)
frame = Gtk.Frame()
revealer = Gtk.Revealer()
label = Gtk.Label(label=message)
label.set_xalign(0.010) label.set_xalign(0.010)
frame.set_label( f"> {m_type}: {json_data['id']}" if "id" in json_data.keys() else f"> {m_type}" ) frame.set_label(lbl_str)
frame.set_label_align(0.010, 0.5) frame.set_label_align(0.010, 0.5)
revealer.set_reveal_child(False) revealer.set_reveal_child(False)
@ -77,4 +66,8 @@ class LogList(Gtk.ListBox):
frame.show_all() frame.show_all()
del message del message
del json_data del lbl_str
def clear(self):
for child in self.get_children():
self.remove(child)

View File

@ -44,7 +44,7 @@ class LSPMessageBox(Gtk.Box):
self._subscribe_to_events() self._subscribe_to_events()
self._load_widgets() self._load_widgets()
self.show_all() self.show()
def _setup_styling(self): def _setup_styling(self):
@ -67,30 +67,17 @@ class LSPMessageBox(Gtk.Box):
self.top_buttons = TopButtonBox() self.top_buttons = TopButtonBox()
self.bottom_buttons = BottomButtonBox() self.bottom_buttons = BottomButtonBox()
scrolled_win.show_all()
scrolled_win.add(self.lsp_msg_src_vw) scrolled_win.add(self.lsp_msg_src_vw)
self.add(self.top_buttons) self.add(self.top_buttons)
self.add(scrolled_win) self.add(scrolled_win)
self.add(self.bottom_buttons) self.add(self.bottom_buttons)
self.bottom_buttons.connect_widget_signals(self)
self._disable_buttons()
def _enable_buttons(self):
for button in self.bottom_buttons.get_children():
button.set_sensitive(True)
def _disable_buttons(self):
for button in self.bottom_buttons.get_children():
button.set_sensitive(False)
self.bottom_buttons.get_children()[-1].set_sensitive(True)
def update_message_id_label(self): def update_message_id_label(self):
self.top_buttons.update_message_id_lbl(self._message_id) self.top_buttons.update_message_id_lbl(self._message_id)
def send_initialize_message(self, button, eve = None): def send_initialize_message(self):
parent = self.get_parent() parent = self.get_parent()
folder_choser_btn = self.top_buttons.folder_choser_btn folder_choser_btn = self.top_buttons.folder_choser_btn
@ -110,9 +97,7 @@ class LSPMessageBox(Gtk.Box):
] ]
self._lsp_init_data["initializationOptions"] = json.loads(init_ops) self._lsp_init_data["initializationOptions"] = json.loads(init_ops)
self.send_request("initialize", self._lsp_init_data)
self.make_request("initialize", self._lsp_init_data)
button.set_sensitive(False)
del init_ops del init_ops
del workspace_file del workspace_file
@ -121,27 +106,20 @@ class LSPMessageBox(Gtk.Box):
del parent del parent
def send_initialized_message(self): def send_initialized_message(self):
self.make_request("initialized") self.send_notification("initialized")
def make_notification(self, method: str, params: {} = {}): def send_notification(self, method: str, params: {} = {}):
data = { data = {
"jsonrpc": "2.0", "jsonrpc": "2.0",
"method": method, "method": method,
"params": params "params": params
} }
message_str = json.dumps(data)
message_size = len(message_str)
message = f"Content-Length: {message_size}\r\n\r\n{message_str}"
self.lsp_process.stdin.write( message.encode("utf-8") ) self._send_message(data)
self.lsp_process.stdin.flush()
del data del data
del message_str
del message_size
del message
def make_request(self, method: str, params: {} = {}): def send_request(self, method: str, params: {} = {}):
self._monitor_lsp_response() self._monitor_lsp_response()
data = { data = {
@ -150,44 +128,49 @@ class LSPMessageBox(Gtk.Box):
"method": method, "method": method,
"params": params "params": params
} }
self._message_id += 1
self._send_message(data)
del data
def _send_message(self, data: dict):
if not data: return
message_str = json.dumps(data) message_str = json.dumps(data)
message_size = len(message_str) message_size = len(message_str)
message = f"Content-Length: {message_size}\r\n\r\n{message_str}" message = f"Content-Length: {message_size}\r\n\r\n{message_str}"
self.get_parent().log_list.add_log_entry("Client Message", message) self.get_parent().log_list.add_log_entry("Client", data)
with self.write_lock: with self.write_lock:
self.lsp_process.stdin.write( message.encode("utf-8") ) self.lsp_process.stdin.write( message.encode("utf-8") )
self.lsp_process.stdin.flush() self.lsp_process.stdin.flush()
self._message_id += 1
self.update_message_id_label() self.update_message_id_label()
del data
del message_str del message_str
del message_size del message_size
del message del message
def send_notification(self, button, eve = None): def button_send_notification(self):
pass
def send_message(self, button, eve = None):
message = json.loads( self.lsp_msg_src_vw.get_text_str() ) message = json.loads( self.lsp_msg_src_vw.get_text_str() )
self.make_request(message["method"], message["params"]) self.send_notification(message["method"], message["params"])
def start_stop_lsp(self, button, eve = None): def button_send_request(self):
message = json.loads( self.lsp_msg_src_vw.get_text_str() )
self.send_request(message["method"], message["params"])
def start_stop_lsp(self):
if self._lsp_pid == -1: if self._lsp_pid == -1:
pid = self.start_lsp() pid = self.start_lsp()
if not pid: return if not pid: return
button.set_label("Stop LSP")
self._lsp_pid = pid self._lsp_pid = pid
self._monitor_lsp_response() self._monitor_lsp_response()
self._enable_buttons()
else: else:
button.set_label("Start LSP")
self.stop_lsp() self.stop_lsp()
self._disable_buttons() self.get_parent().log_list.clear()
def start_lsp(self): def start_lsp(self):
parent = self.get_parent() parent = self.get_parent()
@ -260,7 +243,7 @@ class LSPMessageBox(Gtk.Box):
GLib.idle_add(self.handle_lsp_response, response_id, lsp_result) GLib.idle_add(self.handle_lsp_response, response_id, lsp_result)
def handle_lsp_response(self, id: int, lsp_result: {}): def handle_lsp_response(self, id: int, lsp_result: {}):
self.get_parent().log_list.add_log_entry("LSP Response Message", lsp_result) self.get_parent().log_list.add_log_entry("LSP Response", lsp_result)
keys = lsp_result.keys() keys = lsp_result.keys()
if "error" in keys: if "error" in keys:
@ -276,7 +259,6 @@ class LSPMessageBox(Gtk.Box):
keys = lsp_result.keys() keys = lsp_result.keys()
if "capabilities" in keys: if "capabilities" in keys:
logger.debug(f"LSP Capabilities Response:\n{lsp_result}") logger.debug(f"LSP Capabilities Response:\n{lsp_result}")
self.send_initialized_message()
if isinstance(lsp_result, list): if isinstance(lsp_result, list):

View File

@ -4,11 +4,9 @@
import gi import gi
gi.require_version('Gtk', '3.0') gi.require_version('Gtk', '3.0')
from gi.repository import Gtk from gi.repository import Gtk
from gi.repository import GLib
# Application imports # Application imports
from .lsp_window import LSPWindow from .lsp_ui import LSPUI
class LSPNotebook(Gtk.Notebook): class LSPNotebook(Gtk.Notebook):
@ -23,7 +21,7 @@ class LSPNotebook(Gtk.Notebook):
self._subscribe_to_events() self._subscribe_to_events()
self._load_widgets() self._load_widgets()
self.show_all() self.show()
def _setup_styling(self): def _setup_styling(self):
@ -46,9 +44,9 @@ class LSPNotebook(Gtk.Notebook):
if "hidden" in data.keys() and data["hidden"]: if "hidden" in data.keys() and data["hidden"]:
continue continue
tab_widget = Gtk.Label(label=language) tab_widget = Gtk.Label(label = language)
lsp_window = LSPWindow(language, data) lsp_ui = LSPUI(language, data)
self.append_page(lsp_window, tab_widget) self.append_page(lsp_ui, tab_widget)
self.set_tab_detachable(lsp_window, False) self.set_tab_detachable(lsp_ui, False)
self.set_tab_reorderable(lsp_window, False) self.set_tab_reorderable(lsp_ui, False)

View File

@ -28,7 +28,7 @@ class LSPUI(Gtk.Grid):
self._subscribe_to_events() self._subscribe_to_events()
self._load_widgets() self._load_widgets()
self.show_all() self.show()
def _setup_styling(self): def _setup_styling(self):
@ -95,3 +95,6 @@ class LSPUI(Gtk.Grid):
self.attach(log_list_lbl, 3, 0, 3, 1) self.attach(log_list_lbl, 3, 0, 3, 1)
self.attach(scrolled_win2, 3, 1, 3, 11) self.attach(scrolled_win2, 3, 1, 3, 11)
for child in self.get_children():
child.show()

View File

@ -1,38 +0,0 @@
# Python imports
# Lib imports
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
# Application imports
from .lsp_ui import LSPUI
class LSPWindow(Gtk.ScrolledWindow):
def __init__(self, language, data):
super(LSPWindow, self).__init__()
self._builder = settings_manager.get_builder()
self._setup_styling()
self._setup_signals()
self._subscribe_to_events()
self._load_widgets(language, data)
def _setup_styling(self):
ctx = self.get_style_context()
ctx.add_class("lsp-window")
def _setup_signals(self):
...
def _subscribe_to_events(self):
...
def _load_widgets(self, language, data):
viewport = Gtk.Viewport()
viewport.add( LSPUI(language, data) )
self.add(viewport)