From 44c3ebe88764f6b8ea69bd30ebcf10620780424e Mon Sep 17 00:00:00 2001 From: SpookyDervish Date: Sat, 16 May 2026 16:56:29 +1000 Subject: [PATCH] plugins can now listen for events! --- plugin_loader.py | 32 ++++++++++++++++++++++---- plugins/Git/lua/main.lua | 14 +++++++++++ plugins/{my plugin => Git}/plugin.json | 2 +- plugins/my plugin/lua/main.lua | 7 ------ 4 files changed, 42 insertions(+), 13 deletions(-) create mode 100644 plugins/Git/lua/main.lua rename plugins/{my plugin => Git}/plugin.json (76%) delete mode 100644 plugins/my plugin/lua/main.lua diff --git a/plugin_loader.py b/plugin_loader.py index d9cf0df..e6c57c6 100644 --- a/plugin_loader.py +++ b/plugin_loader.py @@ -1,6 +1,7 @@ from textual_window import Window from textual.widgets import RichLog, Button -from textual import work +from textual import work, on +from textual.message import Message import textual.widgets from typing import Any from traceback import format_exception @@ -10,6 +11,24 @@ from lupa import lua51 import os, json, asyncio +class PluginEventHandler: + def __init__(self): + self.listeners = {} + + def subscribe(self, widget, event_name, plugin_fn): + self.listeners.setdefault(widget, {}).setdefault(event_name, []).append(plugin_fn) + + async def message_handler(self, widget, e: Message): + + event_name = str(e)[:str(e).find('(')] + await self.emit(widget, event_name, e) + + async def emit(self, widget, event_name, payload): + for widget_event, functions in self.listeners.get(widget, {}).items(): + if event_name == widget_event: + for fn in functions: + fn(payload) + class PluginLoader(Window): def __init__(self): super().__init__( @@ -21,6 +40,8 @@ class PluginLoader(Window): start_open=True, allow_maximize=True ) + + self.event_handler = PluginEventHandler() # region Lua functions @@ -52,8 +73,9 @@ class PluginLoader(Window): def create_widget(self, widget_type: str, *args): return getattr(textual.widgets, widget_type)(*args) - def run_on_message(self, event, function): - raise NotImplementedError("onMessage is not implemented yet.") + def run_on_message(self, widget, event, function): + self.event_handler.subscribe(widget, event, function) + widget.message_signal.subscribe(self, lambda e: self.event_handler.message_handler(widget, e)) def create_window(self, title: str, icon: str = "", show_title: bool = True, can_close: bool = True, can_maximize: bool = True, can_resize: bool = True, start_horizontal: str = "center", start_vertical: str = "middle"): new_window = Window( @@ -93,7 +115,7 @@ class PluginLoader(Window): "createWindow": self.create_window, "createWidget": self.create_widget, "app": self.app, - "onMessage": self.run_on_message + "onMessage": self.run_on_message, }, "config": { "get": self.app.config_handler.get, @@ -166,7 +188,7 @@ class PluginLoader(Window): sandbox = self.lua_runtime.eval("{}") setfenv = self.lua_runtime.eval("setfenv") - sandbox.print = self.log #self.lua_runtime.globals().print + sandbox.print = log.write #self.lua_runtime.globals().print sandbox.math = self.lua_runtime.globals().math sandbox.string = self.lua_runtime.globals().string sandbox.tostring = self.lua_runtime.globals().tostring diff --git a/plugins/Git/lua/main.lua b/plugins/Git/lua/main.lua new file mode 100644 index 0000000..bbf92be --- /dev/null +++ b/plugins/Git/lua/main.lua @@ -0,0 +1,14 @@ + local plugin = {} + +function plugin.run() + --berry.config.defineSetting("my plugin", "test setting", "description", "boolean", "1") + + button = berry.ui.button("Click me!", "success") + berry.ui.mount(button) + + berry.ui.onMessage(button, "Pressed", function() + print("hi") + end) +end + +return plugin \ No newline at end of file diff --git a/plugins/my plugin/plugin.json b/plugins/Git/plugin.json similarity index 76% rename from plugins/my plugin/plugin.json rename to plugins/Git/plugin.json index 6bdf85c..dd6f6e8 100644 --- a/plugins/my plugin/plugin.json +++ b/plugins/Git/plugin.json @@ -1,5 +1,5 @@ { - "name": "my plugin", + "name": "Git", "author": "SpookyDervish", "version": "1.0.0", "dependencies": [] diff --git a/plugins/my plugin/lua/main.lua b/plugins/my plugin/lua/main.lua deleted file mode 100644 index 86211cf..0000000 --- a/plugins/my plugin/lua/main.lua +++ /dev/null @@ -1,7 +0,0 @@ - local plugin = {} - -function plugin.run() - berry.config.defineSetting("my plugin", "test setting", "description", "boolean", "1") -end - -return plugin \ No newline at end of file