plugins can now listen for events!
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
from textual_window import Window
|
from textual_window import Window
|
||||||
from textual.widgets import RichLog, Button
|
from textual.widgets import RichLog, Button
|
||||||
from textual import work
|
from textual import work, on
|
||||||
|
from textual.message import Message
|
||||||
import textual.widgets
|
import textual.widgets
|
||||||
from typing import Any
|
from typing import Any
|
||||||
from traceback import format_exception
|
from traceback import format_exception
|
||||||
@@ -10,6 +11,24 @@ from lupa import lua51
|
|||||||
import os, json, asyncio
|
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):
|
class PluginLoader(Window):
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
super().__init__(
|
super().__init__(
|
||||||
@@ -21,6 +40,8 @@ class PluginLoader(Window):
|
|||||||
start_open=True,
|
start_open=True,
|
||||||
allow_maximize=True
|
allow_maximize=True
|
||||||
)
|
)
|
||||||
|
|
||||||
|
self.event_handler = PluginEventHandler()
|
||||||
|
|
||||||
# region Lua functions
|
# region Lua functions
|
||||||
|
|
||||||
@@ -52,8 +73,9 @@ class PluginLoader(Window):
|
|||||||
def create_widget(self, widget_type: str, *args):
|
def create_widget(self, widget_type: str, *args):
|
||||||
return getattr(textual.widgets, widget_type)(*args)
|
return getattr(textual.widgets, widget_type)(*args)
|
||||||
|
|
||||||
def run_on_message(self, event, function):
|
def run_on_message(self, widget, event, function):
|
||||||
raise NotImplementedError("onMessage is not implemented yet.")
|
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"):
|
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(
|
new_window = Window(
|
||||||
@@ -93,7 +115,7 @@ class PluginLoader(Window):
|
|||||||
"createWindow": self.create_window,
|
"createWindow": self.create_window,
|
||||||
"createWidget": self.create_widget,
|
"createWidget": self.create_widget,
|
||||||
"app": self.app,
|
"app": self.app,
|
||||||
"onMessage": self.run_on_message
|
"onMessage": self.run_on_message,
|
||||||
},
|
},
|
||||||
"config": {
|
"config": {
|
||||||
"get": self.app.config_handler.get,
|
"get": self.app.config_handler.get,
|
||||||
@@ -166,7 +188,7 @@ class PluginLoader(Window):
|
|||||||
sandbox = self.lua_runtime.eval("{}")
|
sandbox = self.lua_runtime.eval("{}")
|
||||||
setfenv = self.lua_runtime.eval("setfenv")
|
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.math = self.lua_runtime.globals().math
|
||||||
sandbox.string = self.lua_runtime.globals().string
|
sandbox.string = self.lua_runtime.globals().string
|
||||||
sandbox.tostring = self.lua_runtime.globals().tostring
|
sandbox.tostring = self.lua_runtime.globals().tostring
|
||||||
|
|||||||
14
plugins/Git/lua/main.lua
Normal file
14
plugins/Git/lua/main.lua
Normal file
@@ -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
|
||||||
@@ -1,5 +1,5 @@
|
|||||||
{
|
{
|
||||||
"name": "my plugin",
|
"name": "Git",
|
||||||
"author": "SpookyDervish",
|
"author": "SpookyDervish",
|
||||||
"version": "1.0.0",
|
"version": "1.0.0",
|
||||||
"dependencies": []
|
"dependencies": []
|
||||||
@@ -1,7 +0,0 @@
|
|||||||
local plugin = {}
|
|
||||||
|
|
||||||
function plugin.run()
|
|
||||||
berry.config.defineSetting("my plugin", "test setting", "description", "boolean", "1")
|
|
||||||
end
|
|
||||||
|
|
||||||
return plugin
|
|
||||||
Reference in New Issue
Block a user