feat: added the ability to modify widgets
This commit is contained in:
1
Cargo.lock
generated
1
Cargo.lock
generated
@@ -529,6 +529,7 @@ dependencies = [
|
||||
name = "ewwii_plugin_api"
|
||||
version = "0.2.0"
|
||||
dependencies = [
|
||||
"gtk4",
|
||||
"rhai",
|
||||
]
|
||||
|
||||
|
||||
@@ -857,6 +857,20 @@ impl<B: DisplayBackend> App<B> {
|
||||
}
|
||||
}
|
||||
}
|
||||
PluginRequest::WidgetRegistryAct(func) => {
|
||||
let mut wgs_guard = wgs.lock().unwrap();
|
||||
if let Some(ref mut registry) = *wgs_guard {
|
||||
let repr_map: HashMap<u64, &mut gtk4::Widget> = registry
|
||||
.widgets
|
||||
.iter_mut()
|
||||
.map(|(id, entry)| (*id, &mut entry.widget))
|
||||
.collect();
|
||||
|
||||
func(&mut ewwii_plugin_api::widget_backend::WidgetRegistryRepr {
|
||||
widgets: repr_map,
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
@@ -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<dyn FnOnce(&mut widget_backend::WidgetRegistryRepr) + Send>,
|
||||
) -> 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<dyn FnOnce(&mut Engine) + Send>),
|
||||
ListWidgetIds(Sender<Vec<u64>>),
|
||||
WidgetRegistryAct(Box<dyn FnOnce(&mut widget_backend::WidgetRegistryRepr) + Send>),
|
||||
}
|
||||
|
||||
@@ -1,12 +1,13 @@
|
||||
[package]
|
||||
name = "ewwii_plugin_api"
|
||||
version = "0.2.0"
|
||||
version = "0.3.0"
|
||||
authors = ["byson94 <byson94wastaken@gmail.com>"]
|
||||
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
|
||||
rhai.workspace = true
|
||||
gtk4.workspace = true
|
||||
@@ -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<dyn FnOnce(&mut Engine) + Send>) -> Result<(), String>;
|
||||
|
||||
// == Widget Rendering & Logic == //
|
||||
/// Get the list of all widget id's
|
||||
fn list_widget_ids(&self) -> Result<Vec<u64>, String>;
|
||||
|
||||
/// Perform actions on the latest widget registry
|
||||
fn widget_reg_action(
|
||||
&self,
|
||||
f: Box<dyn FnOnce(&mut widget_backend::WidgetRegistryRepr) + Send>,
|
||||
) -> Result<(), String>;
|
||||
}
|
||||
|
||||
/// The API format that the plugin should follow
|
||||
|
||||
15
crates/ewwii_plugin_api/src/widget_backend.rs
Normal file
15
crates/ewwii_plugin_api/src/widget_backend.rs
Normal file
@@ -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<u64, &'a mut GtkWidget>,
|
||||
}
|
||||
Reference in New Issue
Block a user