From 7ba9d95f9605d8e2af71e8455ecd8e993ac27f7a Mon Sep 17 00:00:00 2001 From: Byson94 Date: Fri, 10 Oct 2025 20:04:02 +0530 Subject: [PATCH] feat: added the ability to modify widgets --- Cargo.lock | 1 + crates/ewwii/src/app.rs | 14 ++++++++++++++ crates/ewwii/src/plugin.rs | 13 ++++++++++++- crates/ewwii_plugin_api/Cargo.toml | 7 ++++--- crates/ewwii_plugin_api/src/lib.rs | 9 ++++++++- crates/ewwii_plugin_api/src/widget_backend.rs | 15 +++++++++++++++ 6 files changed, 54 insertions(+), 5 deletions(-) create mode 100644 crates/ewwii_plugin_api/src/widget_backend.rs diff --git a/Cargo.lock b/Cargo.lock index a10c151..ef1f940 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -529,6 +529,7 @@ dependencies = [ name = "ewwii_plugin_api" version = "0.2.0" dependencies = [ + "gtk4", "rhai", ] diff --git a/crates/ewwii/src/app.rs b/crates/ewwii/src/app.rs index ad07ce0..8f9febb 100644 --- a/crates/ewwii/src/app.rs +++ b/crates/ewwii/src/app.rs @@ -857,6 +857,20 @@ impl App { } } } + PluginRequest::WidgetRegistryAct(func) => { + let mut wgs_guard = wgs.lock().unwrap(); + if let Some(ref mut registry) = *wgs_guard { + let repr_map: HashMap = registry + .widgets + .iter_mut() + .map(|(id, entry)| (*id, &mut entry.widget)) + .collect(); + + func(&mut ewwii_plugin_api::widget_backend::WidgetRegistryRepr { + widgets: repr_map, + }); + } + } } } }); diff --git a/crates/ewwii/src/plugin.rs b/crates/ewwii/src/plugin.rs index 425e45a..c84d3c0 100644 --- a/crates/ewwii/src/plugin.rs +++ b/crates/ewwii/src/plugin.rs @@ -1,4 +1,4 @@ -use ewwii_plugin_api::EwwiiAPI; +use ewwii_plugin_api::{widget_backend, EwwiiAPI}; use rhai::Engine; use std::sync::mpsc::{channel as mpsc_channel, Receiver, Sender}; @@ -46,9 +46,20 @@ impl EwwiiAPI for EwwiiImpl { Err(e) => Err(e.to_string()), } } + + fn widget_reg_action( + &self, + f: Box, + ) -> Result<(), String> { + self.requestor + .send(PluginRequest::WidgetRegistryAct(f)) + .map_err(|_| "Failed to send request to host".to_string())?; + Ok(()) + } } pub(crate) enum PluginRequest { RhaiEngineAct(Box), ListWidgetIds(Sender>), + WidgetRegistryAct(Box), } diff --git a/crates/ewwii_plugin_api/Cargo.toml b/crates/ewwii_plugin_api/Cargo.toml index d65fe1a..9d04811 100644 --- a/crates/ewwii_plugin_api/Cargo.toml +++ b/crates/ewwii_plugin_api/Cargo.toml @@ -1,12 +1,13 @@ [package] name = "ewwii_plugin_api" -version = "0.2.0" +version = "0.3.0" authors = ["byson94 "] edition = "2021" license = "GPL-3.0-or-later" -description = "The full ewwii plugin API shared library for building plugins" +description = "A shared library for building plugins for ewwii" repository = "https://github.com/byson94/ewwii" homepage = "https://github.com/byson94/ewwii" [dependencies] -rhai.workspace = true \ No newline at end of file +rhai.workspace = true +gtk4.workspace = true \ No newline at end of file diff --git a/crates/ewwii_plugin_api/src/lib.rs b/crates/ewwii_plugin_api/src/lib.rs index ec51d56..2b49a7c 100644 --- a/crates/ewwii_plugin_api/src/lib.rs +++ b/crates/ewwii_plugin_api/src/lib.rs @@ -26,6 +26,7 @@ //! ``` pub mod export; +pub mod widget_backend; use rhai::Engine; @@ -42,12 +43,18 @@ pub trait EwwiiAPI: Send + Sync { fn error(&self, msg: &str); // == Rhai Manipulation Stuff == // - /// Perform an action on the current real-time rhai engine + /// Perform actions on the latest rhai engine fn rhai_engine_action(&self, f: Box) -> Result<(), String>; // == Widget Rendering & Logic == // /// Get the list of all widget id's fn list_widget_ids(&self) -> Result, String>; + + /// Perform actions on the latest widget registry + fn widget_reg_action( + &self, + f: Box, + ) -> Result<(), String>; } /// The API format that the plugin should follow diff --git a/crates/ewwii_plugin_api/src/widget_backend.rs b/crates/ewwii_plugin_api/src/widget_backend.rs new file mode 100644 index 0000000..a17cee2 --- /dev/null +++ b/crates/ewwii_plugin_api/src/widget_backend.rs @@ -0,0 +1,15 @@ +//! Module exposing structures and types from the +//! Widget rendering and definition backend in ewwii. + +use gtk4::Widget as GtkWidget; +use std::collections::HashMap; + +/// A representation of widget registry which holds all the +/// information needed for the dynamic runtime engine in ewwii. +/// +/// Not every change in this structure will be represented in the +/// original WidgetRegistry in ewwii. Only the change on gtk4::Widget +/// is reflected back. +pub struct WidgetRegistryRepr<'a> { + pub widgets: HashMap, +}