mirror of
https://github.com/nekename/OpenDeck.git
synced 2026-10-28 09:44:01 +00:00
Implement plugin-global settings
This commit is contained in:
1 parent
193f6882fa
commit
42c351159b
5 files changed
+58
-16
No files matched your search
@@ -12,8 +12,8 @@ pub struct RegisterEvent {
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
pub struct ContextEvent {
|
||||
pub context: ActionContext
|
||||
pub struct ContextEvent<C = ActionContext> {
|
||||
pub context: C
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
@@ -22,8 +22,8 @@ pub struct PayloadEvent<T> {
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
pub struct ContextAndPayloadEvent<T> {
|
||||
pub context: ActionContext,
|
||||
pub struct ContextAndPayloadEvent<T, C = ActionContext> {
|
||||
pub context: C,
|
||||
pub payload: T
|
||||
}
|
||||
|
||||
@@ -33,7 +33,9 @@ pub struct ContextAndPayloadEvent<T> {
|
||||
pub enum InboundEventType {
|
||||
OpenUrl(PayloadEvent<misc::OpenUrlEvent>),
|
||||
SetSettings(ContextAndPayloadEvent<serde_json::Value>),
|
||||
GetSettings(ContextEvent)
|
||||
GetSettings(ContextEvent),
|
||||
SetGlobalSettings(ContextAndPayloadEvent<serde_json::Value, String>),
|
||||
GetGlobalSettings(ContextEvent<String>)
|
||||
}
|
||||
|
||||
pub async fn process_incoming_message(data: tokio_tungstenite::tungstenite::Message) -> Result<(), tokio_tungstenite::tungstenite::Error> {
|
||||
@@ -46,7 +48,9 @@ pub async fn process_incoming_message(data: tokio_tungstenite::tungstenite::Mess
|
||||
if let Err(error) = match decoded {
|
||||
InboundEventType::OpenUrl(event) => misc::open_url(event).await,
|
||||
InboundEventType::SetSettings(event) => settings::set_settings(event).await,
|
||||
InboundEventType::GetSettings(event) => settings::get_settings(event).await
|
||||
InboundEventType::GetSettings(event) => settings::get_settings(event).await,
|
||||
InboundEventType::SetGlobalSettings(event) => settings::set_global_settings(event).await,
|
||||
InboundEventType::GetGlobalSettings(event) => settings::get_global_settings(event).await
|
||||
} {
|
||||
warn!("Failed to process incoming event from plugin: {}\n\tCaused by: {}", error, error.root_cause())
|
||||
}
|
||||
|
||||
@@ -35,3 +35,19 @@ pub async fn get_settings(event: super::ContextEvent) -> Result<(), anyhow::Erro
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn set_global_settings(event: super::ContextAndPayloadEvent<serde_json::Value, String>) -> Result<(), anyhow::Error> {
|
||||
let app = crate::APP_HANDLE.lock().await;
|
||||
let app = app.as_ref().unwrap();
|
||||
|
||||
let settings_dir = app.path_resolver().app_config_dir().unwrap().join("settings/");
|
||||
std::fs::create_dir_all(&settings_dir)?;
|
||||
|
||||
let path = settings_dir.join(event.context + ".json");
|
||||
std::fs::write(path, event.payload.to_string())?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn get_global_settings(event: super::ContextEvent<String>) -> Result<(), anyhow::Error> {
|
||||
crate::events::outbound::settings::did_receive_global_settings(&event.context).await
|
||||
}
|
||||
@@ -19,6 +19,17 @@ pub struct DidReceiveSettings {
|
||||
payload: DidReceiveSettingsPayload
|
||||
}
|
||||
|
||||
#[derive(Serialize)]
|
||||
pub struct DidReceiveGlobalSettingsPayload {
|
||||
settings: serde_json::Value
|
||||
}
|
||||
|
||||
#[derive(Serialize)]
|
||||
pub struct DidReceiveGlobalSettings {
|
||||
event: String,
|
||||
payload: DidReceiveGlobalSettingsPayload
|
||||
}
|
||||
|
||||
pub async fn did_receive_settings(context: crate::shared::ActionContext, instance: &crate::shared::ActionInstance) -> Result<(), anyhow::Error> {
|
||||
send_to_plugin(&instance.action.plugin, DidReceiveSettings {
|
||||
event: "didReceiveSettings".to_owned(),
|
||||
@@ -34,3 +45,19 @@ pub async fn did_receive_settings(context: crate::shared::ActionContext, instanc
|
||||
}
|
||||
}).await
|
||||
}
|
||||
|
||||
pub async fn did_receive_global_settings(context: &str) -> Result<(), anyhow::Error> {
|
||||
let app = crate::APP_HANDLE.lock().await;
|
||||
let app = app.as_ref().unwrap();
|
||||
|
||||
let settings_dir = app.path_resolver().app_config_dir().unwrap().join("settings/");
|
||||
let path = settings_dir.join(format!("{}.json", context));
|
||||
let settings: serde_json::Value = serde_json::from_slice(&std::fs::read(path)?)?;
|
||||
|
||||
send_to_plugin(context, DidReceiveGlobalSettings {
|
||||
event: "didReceiveGlobalSettings".to_owned(),
|
||||
payload: DidReceiveGlobalSettingsPayload {
|
||||
settings
|
||||
}
|
||||
}).await
|
||||
}
|
||||
@@ -18,8 +18,7 @@ use log::{warn, error};
|
||||
/// Initialise a plugin from a given directory.
|
||||
async fn initialise_plugin(path: path::PathBuf) -> anyhow::Result<()> {
|
||||
let plugin_uuid = path.file_name().unwrap().to_str().unwrap();
|
||||
let mut manifest_path = path.clone();
|
||||
manifest_path.push("manifest.json");
|
||||
let manifest_path = path.join("manifest.json");
|
||||
|
||||
let manifest =
|
||||
fs::read(&manifest_path)
|
||||
@@ -31,16 +30,14 @@ async fn initialise_plugin(path: path::PathBuf) -> anyhow::Result<()> {
|
||||
for action in &mut manifest.actions {
|
||||
action.plugin = plugin_uuid.to_owned();
|
||||
|
||||
let mut action_icon_path = path.clone();
|
||||
action_icon_path.push(action.icon.clone());
|
||||
let action_icon_path = path.join(action.icon.clone());
|
||||
action.icon = convert_icon(action_icon_path.to_str().unwrap().to_owned());
|
||||
|
||||
for state in &mut action.states {
|
||||
if state.image == "actionDefaultImage" {
|
||||
state.image = action.icon.clone();
|
||||
} else {
|
||||
let mut state_icon = path.clone();
|
||||
state_icon.push(state.image.clone());
|
||||
let state_icon = path.join(state.image.clone());
|
||||
state.image = convert_icon(state_icon.to_str().unwrap().to_owned());
|
||||
}
|
||||
}
|
||||
@@ -189,8 +186,7 @@ pub fn initialise_plugins(app: AppHandle) {
|
||||
tokio::spawn(init_websocket_server());
|
||||
tokio::spawn(init_browser_server());
|
||||
|
||||
let mut plugin_dir = app.path_resolver().app_config_dir().unwrap();
|
||||
plugin_dir.push("plugins/");
|
||||
let plugin_dir = app.path_resolver().app_config_dir().unwrap().join("plugins/");
|
||||
let _ = fs::create_dir_all(&plugin_dir);
|
||||
|
||||
let entries = match fs::read_dir(&plugin_dir) {
|
||||
|
||||
@@ -17,8 +17,7 @@ pub struct Store<T> where T: Serialize + for<'a> Deserialize<'a> {
|
||||
impl<T> Store<T> where T: Serialize + for<'a> Deserialize<'a> {
|
||||
/// Create a new Store given an ID and storage directory.
|
||||
pub fn new(id: &str, config_dir: PathBuf, default: T) -> Result<Self, anyhow::Error> {
|
||||
let mut path = config_dir.clone();
|
||||
path.push(format!("{}.json", id));
|
||||
let path = config_dir.join(format!("{}.json", id));
|
||||
|
||||
if path.exists() {
|
||||
let file_contents = fs::read(&path)
|
||||
|
||||
Reference in new issue
Block a user