feat: improve epapi; bump epapi to 0.7.0;

This commit is contained in:
Byson94
2025-11-22 12:05:30 +05:30
parent 0d803fd962
commit 04ca79a5af
7 changed files with 59 additions and 15 deletions

9
Cargo.lock generated
View File

@@ -525,9 +525,16 @@ dependencies = [
"x11rb",
]
[[package]]
name = "ewwii-widget-controller"
version = "0.1.0"
dependencies = [
"ewwii_plugin_api",
]
[[package]]
name = "ewwii_plugin_api"
version = "0.6.7"
version = "0.6.8"
dependencies = [
"gtk4",
"rhai",

View File

@@ -7,7 +7,7 @@ resolver = "2"
shared_utils = { version = "0.1.0", path = "crates/shared_utils" }
rhai_impl = { version = "0.1.0", path = "crates/rhai_impl" }
scan_prop_proc = { version = "0.1.0", path = "proc_macros/scan_prop_proc" }
ewwii_plugin_api = { version = "0.6.8", path = "crates/ewwii_plugin_api" }
ewwii_plugin_api = { version = "0.7.0", path = "crates/ewwii_plugin_api" }
anyhow = "1.0.86"
ahash = "0.8.12"

View File

@@ -23,6 +23,7 @@ use crate::{
*,
};
use anyhow::{anyhow, bail};
use ewwii_plugin_api as epapi;
use gdk::Monitor;
use gtk4::Window;
use gtk4::{gdk, glib};
@@ -915,11 +916,9 @@ impl<B: DisplayBackend> App<B> {
unsafe {
// Each plugin exposes: extern "C" fn create_plugin() -> Box<dyn Plugin>
let constructor: libloading::Symbol<
unsafe extern "C" fn() -> Box<dyn ewwii_plugin_api::Plugin>,
> = lib
.get(b"create_plugin")
.map_err(|e| anyhow!("Failed to find create_plugin: {}", e))?;
let constructor: libloading::Symbol<unsafe extern "C" fn() -> Box<dyn epapi::Plugin>> =
lib.get(b"create_plugin")
.map_err(|e| anyhow!("Failed to find create_plugin: {}", e))?;
let plugin = constructor(); // instantiate plugin
@@ -934,11 +933,18 @@ impl<B: DisplayBackend> App<B> {
let handle_request = move |req: PluginRequest| match req {
PluginRequest::RhaiEngineAct(func) => {
cp.borrow_mut().action_with_engine(func);
}
PluginRequest::RegisterFunc((name, func)) => {
cp.borrow_mut().engine.register_fn(name, func);
func(&mut cp.borrow_mut().engine);
}
PluginRequest::RegisterFunc((name, namespace, func)) => match namespace {
epapi::rhai_backend::RhaiFnNamespace::Custom(ns) => {
let mut module = rhai::Module::new();
module.set_native_fn(name, func);
cp.borrow_mut().engine.register_static_module(&ns, module.into());
}
epapi::rhai_backend::RhaiFnNamespace::Global => {
cp.borrow_mut().engine.register_fn(name, func);
}
},
PluginRequest::ListWidgetIds(res_tx) => {
let wgs_guard = wgs.lock().unwrap();
if let Some(wgs_brw) = wgs_guard.as_ref() {

View File

@@ -1,4 +1,4 @@
use ewwii_plugin_api::{widget_backend, EwwiiAPI};
use ewwii_plugin_api::{rhai_backend, widget_backend, EwwiiAPI};
use rhai::{Array, Dynamic, Engine, EvalAltResult};
use std::sync::mpsc::{channel as mpsc_channel, Receiver, Sender};
@@ -36,9 +36,10 @@ impl EwwiiAPI for EwwiiImpl {
fn register_function(
&self,
name: String,
namespace: rhai_backend::RhaiFnNamespace,
f: Box<dyn Fn(Array) -> Result<Dynamic, Box<EvalAltResult>> + Send + Sync>,
) -> Result<(), String> {
let func_info = (name, f);
let func_info = (name, namespace, f);
self.requestor
.send(PluginRequest::RegisterFunc(func_info))
@@ -73,7 +74,13 @@ impl EwwiiAPI for EwwiiImpl {
pub(crate) enum PluginRequest {
RhaiEngineAct(Box<dyn FnOnce(&mut Engine) + Send>),
RegisterFunc((String, Box<dyn Fn(Array) -> Result<Dynamic, Box<EvalAltResult>> + Send + Sync>)),
RegisterFunc(
(
String,
rhai_backend::RhaiFnNamespace,
Box<dyn Fn(Array) -> Result<Dynamic, Box<EvalAltResult>> + Send + Sync>,
),
),
ListWidgetIds(Sender<Vec<u64>>),
WidgetRegistryAct(Box<dyn FnOnce(&mut widget_backend::WidgetRegistryRepr) + Send>),
}

View File

@@ -1,6 +1,6 @@
[package]
name = "ewwii_plugin_api"
version = "0.6.8"
version = "0.7.0"
authors = ["byson94 <byson94wastaken@gmail.com>"]
edition = "2021"
license = "GPL-3.0-or-later"

View File

@@ -29,6 +29,7 @@
mod export_macros;
pub mod example;
pub mod rhai_backend;
pub mod widget_backend;
#[cfg(feature = "include-rhai")]
@@ -103,10 +104,19 @@ pub trait EwwiiAPI: Send + Sync {
/// }
/// }
/// ```
///
/// This example will register a function with signature "my_func(Array)" in rhai.
///
/// ## Example use in rhai
///
/// ```js
/// print(my_func(["param1", "param2"]));
/// ```
#[cfg(feature = "include-rhai")]
fn register_function(
&self,
name: String,
namespace: rhai_backend::RhaiFnNamespace,
f: Box<
dyn Fn(rhai::Array) -> Result<rhai::Dynamic, Box<rhai::EvalAltResult>> + Send + Sync,
>,

View File

@@ -0,0 +1,14 @@
//! Module exposing extra utilities for rhai.
#[cfg(feature = "include-rhai")]
mod rhai_included {
/// _(include-rhai)_ An enumrate providing options for
/// function registaration namespaces.
pub enum RhaiFnNamespace {
Custom(String),
Global,
}
}
#[cfg(feature = "include-rhai")]
pub use rhai_included::*;