From 71049a48040ad03d65815919e3d56a9951619323 Mon Sep 17 00:00:00 2001 From: ninjadev64 <63245705+ninjadev64@users.noreply.github.com> Date: Sun, 7 Apr 2024 19:00:04 +0100 Subject: [PATCH] Implement UI for managing Multi Actions --- src-tauri/src/events/frontend.rs | 18 +++ src-tauri/src/events/outbound/encoder.rs | 4 +- src-tauri/src/events/outbound/keypad.rs | 4 + src-tauri/src/main.rs | 1 + src-tauri/src/shared.rs | 4 +- src-tauri/src/store/profiles.rs | 9 +- src/components/DeviceSelector.svelte | 2 +- src/components/DeviceView.svelte | 113 ------------------ src/components/Key.svelte | 29 +++-- src/components/MultiActionView.svelte | 72 ++++++++++++ src/components/ProfileSelector.svelte | 19 +++- src/components/PropertyInspectorView.svelte | 120 ++++++++++++++++++++ src/components/Slider.svelte | 10 +- src/lib/propertyInspector.ts | 4 + src/routes/+page.svelte | 20 +++- static/cube.png | Bin 0 -> 1118 bytes static/rubbish.png | Bin 0 -> 1186 bytes 17 files changed, 282 insertions(+), 147 deletions(-) create mode 100644 src/components/MultiActionView.svelte create mode 100644 src/components/PropertyInspectorView.svelte create mode 100644 static/cube.png create mode 100644 static/rubbish.png diff --git a/src-tauri/src/events/frontend.rs b/src-tauri/src/events/frontend.rs index cd176d6..d75ec8c 100644 --- a/src-tauri/src/events/frontend.rs +++ b/src-tauri/src/events/frontend.rs @@ -167,6 +167,24 @@ pub async fn clear_slot(context: Context) -> Result<(), Error> { Ok(()) } +#[tauri::command] +pub async fn remove_instance(context: ActionContext) -> Result<(), Error> { + let mut locks = lock_mutexes().await; + let slot = get_slot(&(&context).into(), &mut locks).await?; + + for (index, instance) in slot.iter().enumerate() { + if instance.context == context { + let _ = crate::events::outbound::will_appear::will_disappear(instance, slot.len() > 1).await; + slot.remove(index); + break; + } + } + + save_profile(&context.device, &mut locks).await?; + + Ok(()) +} + #[tauri::command] pub async fn make_info(app: tauri::AppHandle, plugin: String) -> Result { let mut path = app.path_resolver().app_config_dir().unwrap(); diff --git a/src-tauri/src/events/outbound/encoder.rs b/src-tauri/src/events/outbound/encoder.rs index d061303..5338fc9 100644 --- a/src-tauri/src/events/outbound/encoder.rs +++ b/src-tauri/src/events/outbound/encoder.rs @@ -44,7 +44,7 @@ pub async fn dial_rotate(device: &str, index: u8, ticks: i16) -> Result<(), anyh let context = ActionContext { device: device.to_owned(), profile, - controller: "Keypad".to_owned(), + controller: "Encoder".to_owned(), position: index, index: 0, }; @@ -77,7 +77,7 @@ pub async fn dial_press(device: &str, event: &'static str, index: u8) -> Result< let context = ActionContext { device: device.to_owned(), profile, - controller: "Keypad".to_owned(), + controller: "Encoder".to_owned(), position: index, index: 0, }; diff --git a/src-tauri/src/events/outbound/keypad.rs b/src-tauri/src/events/outbound/keypad.rs index 624792f..673db0c 100644 --- a/src-tauri/src/events/outbound/keypad.rs +++ b/src-tauri/src/events/outbound/keypad.rs @@ -56,6 +56,7 @@ pub async fn key_down(device: &str, key: u8) -> Result<(), anyhow::Error> { tokio::time::sleep(Duration::from_millis(100)).await; + instance.current_state = (instance.current_state + 1) % (instance.states.len() as u16); send_to_plugin( &instance.action.plugin, &KeyEvent { @@ -70,6 +71,9 @@ pub async fn key_down(device: &str, key: u8) -> Result<(), anyhow::Error> { tokio::time::sleep(Duration::from_millis(100)).await; } + + save_profile(device, &mut locks).await?; + let _ = crate::events::frontend::update_state(crate::APP_HANDLE.get().unwrap(), context, &mut locks).await; } Ok(()) diff --git a/src-tauri/src/main.rs b/src-tauri/src/main.rs index 6f7f9c1..e6d6779 100644 --- a/src-tauri/src/main.rs +++ b/src-tauri/src/main.rs @@ -32,6 +32,7 @@ async fn main() { frontend::create_instance, frontend::move_slot, frontend::clear_slot, + frontend::remove_instance, frontend::get_profiles, frontend::get_selected_profile, frontend::set_selected_profile, diff --git a/src-tauri/src/shared.rs b/src-tauri/src/shared.rs index da31135..12035ac 100644 --- a/src-tauri/src/shared.rs +++ b/src-tauri/src/shared.rs @@ -94,7 +94,7 @@ pub struct Action { } /// Location metadata of a slot. -#[derive(Clone, Serialize, Deserialize)] +#[derive(Clone, PartialEq, Eq, Serialize, Deserialize)] pub struct Context { pub device: String, pub profile: String, @@ -103,7 +103,7 @@ pub struct Context { } /// Information about the slot and index an instance is located in. -#[derive(Clone, serde_with::SerializeDisplay, serde_with::DeserializeFromStr)] +#[derive(Clone, PartialEq, Eq, serde_with::SerializeDisplay, serde_with::DeserializeFromStr)] pub struct ActionContext { pub device: String, pub profile: String, diff --git a/src-tauri/src/store/profiles.rs b/src-tauri/src/store/profiles.rs index dddfd18..380107b 100644 --- a/src-tauri/src/store/profiles.rs +++ b/src-tauri/src/store/profiles.rs @@ -147,11 +147,12 @@ pub async fn get_slot<'a>(context: &crate::shared::Context, locks: &'a mut Locks pub async fn get_instance<'a>(context: &crate::shared::ActionContext, locks: &'a mut Locks<'_>) -> Result, anyhow::Error> { let slot = get_slot(&(context.into()), locks).await?; - if (context.index as usize) < slot.len() { - Ok(Some(&mut slot[context.index as usize])) - } else { - Ok(None) + for instance in slot { + if instance.context == *context { + return Ok(Some(instance)); + } } + Ok(None) } pub async fn save_profile<'a>(device: &str, locks: &'a mut Locks<'_>) -> Result<(), anyhow::Error> { diff --git a/src/components/DeviceSelector.svelte b/src/components/DeviceSelector.svelte index 65f6a2d..9db5492 100644 --- a/src/components/DeviceSelector.svelte +++ b/src/components/DeviceSelector.svelte @@ -1,6 +1,6 @@
@@ -158,28 +70,3 @@ {/each}
- -
- - {#each nonEmptySlots as slot (slot[0].context)} - {#each slot as instance} - {#if instance.action.property_inspector} -