Add Toggle Action built-in action

This commit is contained in:
ninjadev64 committed 2024-08-13 11:27:16 +01:00
1 parent a09c770cee
commit 90d77c4807
9 files changed
+135 -66

No files matched your search

+17 -6
View File
@@ -91,7 +91,7 @@ pub async fn set_selected_profile(app: AppHandle, device: String, id: String, pr
if selected_profile != id {
let old_profile = &profile_stores.get_profile_store(devices.get(&device).unwrap(), selected_profile)?.value;
for instance in old_profile.keys.iter().flatten().chain(&mut old_profile.sliders.iter().flatten()) {
if instance.action.uuid != "com.amansprojects.opendeck.multiaction" {
if !matches!(instance.action.uuid.as_str(), "com.amansprojects.opendeck.multiaction" | "com.amansprojects.opendeck.toggleaction") {
let _ = crate::events::outbound::will_appear::will_disappear(instance, false).await;
} else {
for child in instance.children.as_ref().unwrap() {
@@ -108,7 +108,7 @@ pub async fn set_selected_profile(app: AppHandle, device: String, id: String, pr
*new_profile = profile;
}
for instance in new_profile.keys.iter().flatten().chain(&mut new_profile.sliders.iter().flatten()) {
if instance.action.uuid != "com.amansprojects.opendeck.multiaction" {
if !matches!(instance.action.uuid.as_str(), "com.amansprojects.opendeck.multiaction" | "com.amansprojects.opendeck.toggleaction") {
let _ = crate::events::outbound::will_appear::will_appear(instance, false).await;
} else {
for child in instance.children.as_ref().unwrap() {
@@ -165,8 +165,16 @@ pub async fn create_instance(action: Action, context: Context) -> Result<Option<
context: ActionContext::from_context(context.clone(), 0),
states: action.states.clone(),
current_state: 0,
settings: serde_json::Value::Object(serde_json::Map::new()),
children: if action.uuid == "com.amansprojects.opendeck.multiaction" { Some(vec![]) } else { None },
settings: if action.uuid == "com.amansprojects.opendeck.toggleaction" {
serde_json::Value::from(0)
} else {
serde_json::Value::Object(serde_json::Map::new())
},
children: if matches!(action.uuid.as_str(), "com.amansprojects.opendeck.multiaction" | "com.amansprojects.opendeck.toggleaction") {
Some(vec![])
} else {
None
},
};
*slot = Some(instance.clone());
@@ -225,7 +233,7 @@ pub async fn clear_slot(context: Context) -> Result<(), Error> {
let slot = get_slot_mut(&context, &mut locks).await?;
if let Some(instance) = slot {
if instance.action.uuid != "com.amansprojects.opendeck.multiaction" {
if !matches!(instance.action.uuid.as_str(), "com.amansprojects.opendeck.multiaction" | "com.amansprojects.opendeck.toggleaction") {
let _ = crate::events::outbound::will_appear::will_disappear(instance, false).await;
} else {
for child in instance.children.as_ref().unwrap() {
@@ -248,7 +256,7 @@ pub async fn remove_instance(context: ActionContext) -> Result<(), Error> {
return Ok(());
};
if instance.action.uuid != "com.amansprojects.opendeck.multiaction" {
if !matches!(instance.action.uuid.as_str(), "com.amansprojects.opendeck.multiaction" | "com.amansprojects.opendeck.toggleaction") {
let _ = crate::events::outbound::will_appear::will_disappear(instance, false).await;
*slot = None;
} else {
@@ -260,6 +268,9 @@ pub async fn remove_instance(context: ActionContext) -> Result<(), Error> {
break;
}
}
if instance.action.uuid == "com.amansprojects.opendeck.toggleaction" && instance.settings.as_u64().unwrap() as usize >= children.len() {
instance.settings = serde_json::Value::from(if children.is_empty() { 0 } else { children.len() - 1 });
}
}
save_profile(&context.device, &mut locks).await?;
+61 -28
View File
@@ -29,19 +29,7 @@ pub async fn key_down(device: &str, key: u8) -> Result<(), anyhow::Error> {
let _ = crate::events::frontend::key_moved(crate::APP_HANDLE.get().unwrap(), context.clone(), true).await;
let Some(instance) = get_slot_mut(&context, &mut locks).await? else { return Ok(()) };
if instance.action.uuid != "com.amansprojects.opendeck.multiaction" {
send_to_plugin(
&instance.action.plugin,
&KeyEvent {
event: "keyDown",
action: instance.action.uuid.clone(),
context: instance.context.clone(),
device: instance.context.device.clone(),
payload: GenericInstancePayload::new(instance, false),
},
)
.await?;
} else {
if instance.action.uuid == "com.amansprojects.opendeck.multiaction" {
for child in instance.children.as_mut().unwrap() {
send_to_plugin(
&child.action.plugin,
@@ -78,6 +66,31 @@ pub async fn key_down(device: &str, key: u8) -> Result<(), anyhow::Error> {
save_profile(device, &mut locks).await?;
let _ = crate::events::frontend::update_state(crate::APP_HANDLE.get().unwrap(), context, &mut locks).await;
} else if instance.action.uuid == "com.amansprojects.opendeck.toggleaction" {
let child = &instance.children.as_ref().unwrap()[instance.settings.as_u64().unwrap() as usize];
send_to_plugin(
&child.action.plugin,
&KeyEvent {
event: "keyDown",
action: child.action.uuid.clone(),
context: child.context.clone(),
device: child.context.device.clone(),
payload: GenericInstancePayload::new(child, false),
},
)
.await?;
} else {
send_to_plugin(
&instance.action.plugin,
&KeyEvent {
event: "keyDown",
action: instance.action.uuid.clone(),
context: instance.context.clone(),
device: instance.context.device.clone(),
payload: GenericInstancePayload::new(instance, false),
},
)
.await?;
}
Ok(())
@@ -98,21 +111,41 @@ pub async fn key_up(device: &str, key: u8) -> Result<(), anyhow::Error> {
let slot = get_slot_mut(&context, &mut locks).await?;
let Some(instance) = slot else { return Ok(()) };
if instance.states.len() == 2 && !instance.action.disable_automatic_states {
instance.current_state = (instance.current_state + 1) % (instance.states.len() as u16);
}
send_to_plugin(
&instance.action.plugin,
&KeyEvent {
event: "keyUp",
action: instance.action.uuid.clone(),
context: instance.context.clone(),
device: instance.context.device.clone(),
payload: GenericInstancePayload::new(instance, false),
},
)
.await?;
if instance.action.uuid == "com.amansprojects.opendeck.toggleaction" {
let index = instance.settings.as_u64().unwrap() as usize;
let children = instance.children.as_ref().unwrap();
if children.is_empty() {
return Ok(());
};
let child = &children[index];
send_to_plugin(
&child.action.plugin,
&KeyEvent {
event: "keyUp",
action: child.action.uuid.clone(),
context: child.context.clone(),
device: child.context.device.clone(),
payload: GenericInstancePayload::new(child, false),
},
)
.await?;
instance.settings = serde_json::Value::from((index + 1) % instance.children.as_ref().unwrap().len());
} else if instance.action.uuid != "com.amansprojects.opendeck.multiaction" {
if instance.states.len() == 2 && !instance.action.disable_automatic_states {
instance.current_state = (instance.current_state + 1) % (instance.states.len() as u16);
}
send_to_plugin(
&instance.action.plugin,
&KeyEvent {
event: "keyUp",
action: instance.action.uuid.clone(),
context: instance.context.clone(),
device: instance.context.device.clone(),
payload: GenericInstancePayload::new(instance, false),
},
)
.await?
};
save_profile(device, &mut locks).await?;
let _ = crate::events::frontend::update_state(crate::APP_HANDLE.get().unwrap(), context, &mut locks).await;
+28 -13
View File
@@ -206,19 +206,34 @@ pub static CATEGORIES: Lazy<RwLock<HashMap<String, Vec<Action>>>> = Lazy::new(||
let mut hashmap = HashMap::new();
hashmap.insert(
"OpenDeck".to_owned(),
vec![serde_json::from_str(
r#"{
"name": "Multi Action",
"icon": "opendeck/multi-action.png",
"plugin": "com.amansprojects.opendeck",
"uuid": "com.amansprojects.opendeck.multiaction",
"tooltip": "Execute multiple actions",
"controllers": [ "Keypad" ],
"states": [ { "image": "opendeck/multi-action.png" } ],
"supported_in_multi_actions": false
}"#,
)
.unwrap()],
vec![
serde_json::from_str(
r#"{
"name": "Multi Action",
"icon": "opendeck/multi-action.png",
"plugin": "com.amansprojects.opendeck",
"uuid": "com.amansprojects.opendeck.multiaction",
"tooltip": "Execute multiple actions",
"controllers": [ "Keypad" ],
"states": [ { "image": "opendeck/multi-action.png" } ],
"supported_in_multi_actions": false
}"#,
)
.unwrap(),
serde_json::from_str(
r#"{
"name": "Toggle Action",
"icon": "opendeck/toggle-action.png",
"plugin": "com.amansprojects.opendeck",
"uuid": "com.amansprojects.opendeck.toggleaction",
"tooltip": "Cycle through multiple actions",
"controllers": [ "Keypad" ],
"states": [ { "image": "opendeck/toggle-action.png" } ],
"supported_in_multi_actions": false
}"#,
)
.unwrap(),
],
);
RwLock::new(hashmap)
});
+2 -2
View File
@@ -7,7 +7,7 @@
import Key from "./Key.svelte";
import Slider from "./Slider.svelte";
import { inspectedMultiAction } from "$lib/propertyInspector";
import { inspectedParentAction } from "$lib/propertyInspector";
import { invoke } from "@tauri-apps/api";
export let device: DeviceInfo;
@@ -61,7 +61,7 @@
</script>
{#key device}
<div class="flex flex-row" class:hidden={$inspectedMultiAction || selectedDevice != device.id}>
<div class="flex flex-row" class:hidden={$inspectedParentAction || selectedDevice != device.id}>
{#each { length: device.sliders } as _, i}
<Slider
context={{ device: device.id, profile: profile.id, controller: "Encoder", position: i }}
+3 -3
View File
@@ -9,7 +9,7 @@
import Trash from "phosphor-svelte/lib/Trash";
import InstanceEditor from "./InstanceEditor.svelte";
import { copiedContext, inspectedInstance, inspectedMultiAction, openContextMenu } from "$lib/propertyInspector";
import { copiedContext, inspectedInstance, inspectedParentAction, openContextMenu } from "$lib/propertyInspector";
import { renderImage } from "$lib/rendererHelper";
import { invoke } from "@tauri-apps/api";
@@ -50,8 +50,8 @@
function select() {
if (!slot) return;
if (slot.action.uuid == "com.amansprojects.opendeck.multiaction") {
inspectedMultiAction.set(context);
if (slot.action.uuid == "com.amansprojects.opendeck.multiaction" || slot.action.uuid == "com.amansprojects.opendeck.toggleaction") {
inspectedParentAction.set(context);
} else {
inspectedInstance.set(slot.context);
}
@@ -6,13 +6,15 @@
import Trash from "phosphor-svelte/lib/Trash";
import Key from "./Key.svelte";
import { inspectedMultiAction } from "$lib/propertyInspector";
import { inspectedParentAction } from "$lib/propertyInspector";
import { invoke } from "@tauri-apps/api";
export let profile: Profile;
let children: ActionInstance[];
$: children = profile.keys[$inspectedMultiAction!.position]!.children!;
$: children = profile.keys[$inspectedParentAction!.position]!.children!;
let parentUuid: string;
$: parentUuid = profile.keys[$inspectedParentAction!.position]!.action.uuid;
function handleDragOver(event: DragEvent) {
event.preventDefault();
@@ -22,16 +24,24 @@
async function handleDrop({ dataTransfer }: DragEvent) {
if (dataTransfer?.getData("action")) {
let action = JSON.parse(dataTransfer?.getData("action"));
if (!action.supported_in_multi_actions) return;
let response: ActionInstance | null = await invoke("create_instance", { context: $inspectedMultiAction, action });
if (response) profile.keys[$inspectedMultiAction!.position]!.children = [ ...children, response ];
if (
(parentUuid == "com.amansprojects.opendeck.multiaction" && !action.supported_in_multi_actions) ||
(
parentUuid == "com.amansprojects.opendeck.toggleaction" &&
(action.uuid == "com.amansprojects.opendeck.multiaction" || action.uuid == "com.amansprojects.opendeck.toggleaction")
)
) {
return;
}
let response: ActionInstance | null = await invoke("create_instance", { context: $inspectedParentAction, action });
if (response) profile.keys[$inspectedParentAction!.position]!.children = [ ...children, response ];
}
}
async function removeInstance(index: number) {
await invoke("remove_instance", { context: children[index].context });
children.splice(index, 1);
profile.keys[$inspectedMultiAction!.position]!.children = children;
profile.keys[$inspectedParentAction!.position]!.children = children;
}
let context: Context;
@@ -39,8 +49,8 @@
</script>
<div class="px-6 pt-6 pb-4 dark:text-neutral-300">
<button class="float-right text-xl" on:click={() => $inspectedMultiAction = null}> ✕ </button>
<h1 class="font-semibold text-2xl"> Multi Action </h1>
<button class="float-right text-xl" on:click={() => $inspectedParentAction = null}> ✕ </button>
<h1 class="font-semibold text-2xl"> {parentUuid == "com.amansprojects.opendeck.toggleaction" ? "Toggle Action" : "Multi Action"} </h1>
</div>
<div class="flex flex-col h-80 overflow-scroll">
+1 -1
View File
@@ -11,7 +11,7 @@ inspectedInstance.subscribe(async (value) => {
old = value;
});
export const inspectedMultiAction: Writable<Context | null> = writable(null);
export const inspectedParentAction: Writable<Context | null> = writable(null);
export const openContextMenu: Writable<{ context: Context, x: number, y: number } | null> = writable(null);
document.addEventListener("click", () => openContextMenu.set(null));
+5 -5
View File
@@ -2,13 +2,13 @@
import type { DeviceInfo } from "$lib/DeviceInfo";
import type { Profile } from "$lib/Profile";
import { inspectedMultiAction } from "$lib/propertyInspector";
import { inspectedParentAction } from "$lib/propertyInspector";
import ActionList from "../components/ActionList.svelte";
import DeviceSelector from "../components/DeviceSelector.svelte";
import DeviceView from "../components/DeviceView.svelte";
import MultiActionView from "../components/MultiActionView.svelte";
import NoDevicesDetected from "../components/NoDevicesDetected.svelte";
import ParentActionView from "../components/ParentActionView.svelte";
import PluginManager from "../components/PluginManager.svelte";
import ProfileSelector from "../components/ProfileSelector.svelte";
import PropertyInspectorView from "../components/PropertyInspectorView.svelte";
@@ -24,8 +24,8 @@
</script>
<div class="flex flex-col grow">
{#if $inspectedMultiAction}
<MultiActionView bind:profile={selectedProfiles[selectedDevice]} />
{#if $inspectedParentAction}
<ParentActionView bind:profile={selectedProfiles[selectedDevice]} />
{/if}
{#if Object.keys(devices).length > 0 && selectedProfiles}
{#each Object.entries(devices) as [ id, device ]}
@@ -43,7 +43,7 @@
</div>
<div class="flex flex-col p-2 grow max-w-[18rem] h-full border-l dark:border-neutral-700">
{#if !$inspectedMultiAction}
{#if !$inspectedParentAction}
<DeviceSelector
bind:devices={devices}
bind:value={selectedDevice}
Binary file not shown.

After

Width:  |  Height:  |  Size: 5.5 KiB