Skip to content

Commit

Permalink
Allow removing plugins without relaunching OpenDeck
Browse files Browse the repository at this point in the history
  • Loading branch information
ninjadev64 committed Jun 2, 2024
1 parent 3160428 commit 07d7b97
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 24 deletions.
10 changes: 7 additions & 3 deletions src-tauri/src/events/frontend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ pub async fn install_plugin(app: AppHandle, id: String, url: Option<String>) ->
Err(error) => return Err(anyhow::Error::from(error).into()),
};

let _ = crate::plugins::deactivate_plugin(app.clone(), &format!("{}.sdPlugin", id)).await;
let _ = crate::plugins::deactivate_plugin(&app, &format!("{}.sdPlugin", id)).await;

let config_dir = app.path_resolver().app_config_dir().unwrap();
let _ = tokio::fs::create_dir_all(config_dir.join("temp")).await;
Expand Down Expand Up @@ -375,12 +375,16 @@ pub async fn remove_plugin(app: AppHandle, id: String) -> Result<(), Error> {
remove_instance(context).await?;
}

crate::plugins::deactivate_plugin(&app, &id).await?;
if let Err(error) = tokio::fs::remove_dir_all(app.path_resolver().app_config_dir().unwrap().join("plugins").join(&id)).await {
return Err(anyhow::Error::from(error).into());
}
let _ = tokio::fs::write(app.path_resolver().app_config_dir().unwrap().join("plugins").join("removed.txt"), id).await;

app.restart();
let mut categories = CATEGORIES.write().await;
for category in categories.values_mut() {
category.retain(|v| v.plugin != id);
}
categories.retain(|_, v| !v.is_empty());

Ok(())
}
Expand Down
7 changes: 1 addition & 6 deletions src-tauri/src/plugins/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ pub async fn initialise_plugin(path: &path::PathBuf) -> anyhow::Result<()> {
Ok(())
}

pub async fn deactivate_plugin(app: AppHandle, uuid: &str) -> Result<(), anyhow::Error> {
pub async fn deactivate_plugin(app: &AppHandle, uuid: &str) -> Result<(), anyhow::Error> {
let mut instances = INSTANCES.lock().await;
if let Some(instance) = instances.get_mut(uuid) {
match instance {
Expand All @@ -259,11 +259,6 @@ pub fn initialise_plugins(app: AppHandle) {
let plugin_dir = app.path_resolver().app_config_dir().unwrap().join("plugins");
let _ = fs::create_dir_all(&plugin_dir);

if let Ok(contents) = fs::read_to_string(plugin_dir.join("removed.txt")) {
let _ = fs::remove_dir_all(plugin_dir.join(contents));
let _ = fs::remove_file(plugin_dir.join("removed.txt"));
}

let entries = match fs::read_dir(&plugin_dir) {
Ok(p) => p,
Err(error) => {
Expand Down
14 changes: 8 additions & 6 deletions src/components/ActionList.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,13 @@

<div class="grow mt-1 overflow-auto">
{#each Object.entries(categories) as [ name, actions ]}
<h3 class="text-xl font-semibold dark:text-neutral-300"> {name} </h3>
{#each actions as action}
{#if action.visible_in_action_list}
<ListedAction {action} localisation={localiseAction(action, $localisations)} />
{/if}
{/each}
<details open class="mb-2">
<summary class="text-xl font-semibold dark:text-neutral-300"> {name} </summary>
{#each actions as action}
{#if action.visible_in_action_list}
<ListedAction {action} localisation={localiseAction(action, $localisations)} />
{/if}
{/each}
</details>
{/each}
</div>
2 changes: 1 addition & 1 deletion src/components/Key.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@
width="144" height="144"
style="scale: {(112 / 144) * scale};"
on:dragover on:drop
draggable="true" on:dragstart
draggable={slot && slot.length != 0} on:dragstart
role="cell" tabindex="-1"
on:click={select} on:keyup={select}
on:contextmenu={contextMenu}
Expand Down
15 changes: 9 additions & 6 deletions src/components/PluginManager.svelte
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<script lang="ts">
import type ActionList from "./ActionList.svelte";
import type ProfileSelector from "./ProfileSelector.svelte";
import CloudArrowDown from "phosphor-svelte/lib/CloudArrowDown";
import Trash from "phosphor-svelte/lib/Trash";
Expand All @@ -12,20 +13,19 @@
import { invoke } from "@tauri-apps/api";
export let actionList: ActionList;
export let profileSelector: ProfileSelector;
let showPopup: boolean;
async function installPlugin(id: string, name: string, url: string | undefined = undefined) {
if (!await confirm(`Install ${name}? It may take a while to download the plugin.`)) {
return;
}
if (!await confirm(`Install "${name}"? It may take a while to download the plugin.`)) return;
try {
await invoke("install_plugin", { id, url });
alert(`Successfully installed "${name}".`);
actionList.reload();
installed = await invoke("list_plugins");
} catch (error: any) {
alert(`Failed to install ${name}: ${error.description}`);
alert(`Failed to install ${name}: ${error}`);
}
}
Expand Down Expand Up @@ -74,12 +74,15 @@
}
async function removePlugin(plugin: any) {
if (!await confirm(`Are you sure you want to remove ${plugin.name}? This action will relaunch OpenDeck.`)) return;
if (!await confirm(`Are you sure you want to remove "${plugin.name}"?`)) return;
try {
await invoke("remove_plugin", { id: plugin.id });
alert(`Successfully removed "${plugin.name}".`);
actionList.reload();
profileSelector.reload();
installed = await invoke("list_plugins");
} catch (error: any) {
alert(`Failed to remove ${plugin.name}: ${error.description}`);
alert(`Failed to remove ${plugin.name}: ${error}`);
}
}
Expand Down
3 changes: 3 additions & 0 deletions src/components/ProfileSelector.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@
else folders[folder] = [ id ];
folders = folders;
}
export async function reload() {
await setProfile(value);
}
async function deleteProfile(id: string) {
await invoke("delete_profile", { device: device.id, profile: id });
Expand Down
9 changes: 7 additions & 2 deletions src/routes/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
$: _selectedProfile = selectedProfile;
let actionList: ActionList;
let profileSelector: ProfileSelector;
</script>

<div class="flex flex-col grow">
Expand All @@ -41,13 +42,17 @@
{#if !$inspectedMultiAction}
<DeviceSelector bind:device={selectedDevice} />
{#if selectedDevice}
<ProfileSelector bind:device={_selectedDevice} bind:profile={selectedProfile} />
<ProfileSelector
bind:device={_selectedDevice}
bind:profile={selectedProfile}
bind:this={profileSelector}
/>
{/if}
{/if}
<ActionList bind:this={actionList} />
<hr class="mt-2 border dark:border-neutral-700" />
<div class="flex flex-row">
<PluginManager {actionList} />
<PluginManager bind:actionList bind:profileSelector />
<SettingsView />
</div>
</div>

0 comments on commit 07d7b97

Please sign in to comment.