foreground app listener support
This commit is contained in:
@@ -1,21 +1,13 @@
|
||||
use tauri::Manager;
|
||||
use tracing::{error, info, warn};
|
||||
|
||||
use crate::{get_app_handle, lock_w};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use crate::{get_app_handle, lock_w, services::presence_modules::models::ModuleMetadata};
|
||||
use serde_json;
|
||||
use std::fs;
|
||||
|
||||
pub mod models;
|
||||
pub mod runtime;
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug)]
|
||||
pub struct ModuleMetadata {
|
||||
pub name: String,
|
||||
pub version: String,
|
||||
pub description: Option<String>,
|
||||
}
|
||||
|
||||
fn get_module_metadata(path: &std::path::Path) -> Option<ModuleMetadata> {
|
||||
let metadata_path = path.join("metadata.json");
|
||||
if metadata_path.exists() {
|
||||
|
||||
@@ -9,3 +9,10 @@ pub struct PresenceStatus {
|
||||
pub subtitle: Option<String>,
|
||||
pub graphics_b64: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug)]
|
||||
pub struct ModuleMetadata {
|
||||
pub name: String,
|
||||
pub version: String,
|
||||
pub description: Option<String>,
|
||||
}
|
||||
|
||||
@@ -20,6 +20,14 @@ impl UserData for Engine {
|
||||
thread::sleep(Duration::from_secs(seconds));
|
||||
Ok(())
|
||||
});
|
||||
methods.add_method("path_from_relative", |lua, _, relative_path: String| {
|
||||
let script_path: String = lua.globals().get("script_path")?;
|
||||
let path = Path::new(&script_path);
|
||||
let parent = path.parent().unwrap_or(path);
|
||||
let full_path = parent.join(&relative_path);
|
||||
Ok(full_path.to_string_lossy().to_string())
|
||||
});
|
||||
methods.add_method("get_platform", |_, _, ()| Ok(std::env::consts::OS));
|
||||
methods.add_method("update_status", |lua, _, value: Value| {
|
||||
let status: PresenceStatus = lua.from_value(value)?;
|
||||
let rt = Runtime::new().unwrap();
|
||||
@@ -52,13 +60,27 @@ async fn update_status_async(status: PresenceStatus) {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn spawn_lua_runtime(script: &str) -> thread::JoinHandle<()> {
|
||||
let script = script.to_string();
|
||||
pub fn spawn_lua_runtime(script_path: &Path) -> thread::JoinHandle<()> {
|
||||
let script_path = script_path.to_owned();
|
||||
|
||||
thread::spawn(move || {
|
||||
let lua = Lua::new();
|
||||
let lua = unsafe { Lua::unsafe_new() };
|
||||
let globals = lua.globals();
|
||||
|
||||
// Set script_path for engine methods to access
|
||||
if let Err(e) = globals.set("script_path", script_path.to_string_lossy().to_string()) {
|
||||
error!("Failed to set script_path global: {}", e);
|
||||
return;
|
||||
}
|
||||
|
||||
let script = match std::fs::read_to_string(&script_path) {
|
||||
Ok(s) => s,
|
||||
Err(e) => {
|
||||
error!("Failed to read script: {}", e);
|
||||
return;
|
||||
}
|
||||
};
|
||||
|
||||
if let Err(e) = globals.set("engine", Engine) {
|
||||
error!("Failed to set engine global: {}", e);
|
||||
return;
|
||||
@@ -71,6 +93,5 @@ pub fn spawn_lua_runtime(script: &str) -> thread::JoinHandle<()> {
|
||||
}
|
||||
|
||||
pub fn spawn_lua_runtime_from_path(path: &Path) -> Result<thread::JoinHandle<()>, std::io::Error> {
|
||||
let script = std::fs::read_to_string(path)?;
|
||||
Ok(spawn_lua_runtime(&script))
|
||||
Ok(spawn_lua_runtime(path))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user