Skip to content

Commit

Permalink
App autostart (#59)
Browse files Browse the repository at this point in the history
* feat(dataans): implement app autostart;

* feat(dataans): format code;
  • Loading branch information
TheBestTvarynka authored Dec 3, 2024
1 parent daf5adc commit 1f240b9
Show file tree
Hide file tree
Showing 7 changed files with 120 additions and 6 deletions.
66 changes: 61 additions & 5 deletions dataans/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions dataans/src-tauri/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ syntect = { version = "5.2", default-features = false, features = [
] }
tauri-plugin-fs = "2"
tauri-plugin-shell = "2"
tauri-plugin-autostart = "2"

[features]
# This feature is used for production builds or when a dev server is not specified, DO NOT REMOVE!!
Expand Down
4 changes: 4 additions & 0 deletions dataans/src-tauri/capabilities/desktop.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
"main"
],
"permissions": [
"autostart:default",
"autostart:allow-enable",
"autostart:allow-disable",
"autostart:allow-is-enabled",
"dataans:allow-list-spaces",
"dataans:allow-create-space",
"dataans:allow-update-space",
Expand Down
4 changes: 4 additions & 0 deletions dataans/src-tauri/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,10 @@ fn init_tracing(app_data: &Path) {

fn main() {
tauri::Builder::default()
.plugin(tauri_plugin_autostart::init(
tauri_plugin_autostart::MacosLauncher::LaunchAgent,
None,
))
.plugin(tauri_plugin_shell::init())
.plugin(tauri_plugin_fs::init())
.plugin(dataans::init_dataans_plugin())
Expand Down
27 changes: 27 additions & 0 deletions dataans/src/backend/autostart.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// https://github.com/tauri-apps/plugins-workspace/blob/715a0477be8f6f77af0377f4eca2b649554446be/plugins/autostart/api-iife.js

use serde_wasm_bindgen::{from_value, to_value};

use crate::backend::{invoke, EmptyArgs};

pub async fn enable() -> bool {
let args = to_value(&EmptyArgs {}).expect("EmptyArgs serialization to JsValue should not fail.");
invoke("plugin:autostart|enable", args).await;

is_enabled().await
}

pub async fn disable() -> bool {
let args = to_value(&EmptyArgs {}).expect("EmptyArgs serialization to JsValue should not fail.");
invoke("plugin:autostart|disable", args).await;

is_enabled().await
}

pub async fn is_enabled() -> bool {
let args = to_value(&EmptyArgs {}).expect("EmptyArgs serialization to JsValue should not fail.");
let is_enabled = invoke("plugin:autostart|is_enabled", args).await;
trace!("Is autostart enabled: {:?}.", is_enabled);

from_value(is_enabled).expect("bool deserialization from JsValue should not fail.")
}
3 changes: 2 additions & 1 deletion dataans/src/backend/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pub mod autostart;
pub mod file;
pub mod notes;
pub mod spaces;
Expand Down Expand Up @@ -52,7 +53,7 @@ pub async fn load_theme(file_path: &Path) -> Theme {
}

#[derive(Serialize)]
struct EmptyArgs {}
pub struct EmptyArgs {}

pub async fn open_config_file() {
let args = to_value(&EmptyArgs {}).expect("EmptyArgs serialization to JsValue should not fail.");
Expand Down
21 changes: 21 additions & 0 deletions dataans/src/spaces/app_info/app_info_window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,22 @@ pub fn AppInfoWindow(#[prop(into)] close: Callback<(), ()>) -> impl IntoView {
let open_config_file_folder = move |_| spawn_local(open_config_file_folder());
let open_theme_file = move |theme: PathBuf| spawn_local(async move { open_theme_file(&theme).await });

let (is_autostart_enabled, set_autostart) = create_signal(false);

let enable_autostart = move |_| {
spawn_local(async move {
let flag = crate::backend::autostart::enable().await;
set_autostart.set(flag);
})
};

let disable_autostart = move |_| {
spawn_local(async move {
let flag = crate::backend::autostart::disable().await;
set_autostart.set(flag);
})
};

view! {
<div class="app-into-window">
<button
Expand All @@ -38,6 +54,11 @@ pub fn AppInfoWindow(#[prop(into)] close: Callback<(), ()>) -> impl IntoView {
>
<img alt="edit note" src="/public/icons/folder.png" />
</button>
{move || if is_autostart_enabled.get() {view! {
<button class="button_ok" on:click=disable_autostart title="Disable autostart">"Disable autostart"</button>
}} else {view! {
<button class="button_ok" on:click=enable_autostart title="Enable autostart">"Enable autostart"</button>
}}}
</div>
{move || {
let Config { key_bindings, appearance, app } = global_config.get();
Expand Down

0 comments on commit 1f240b9

Please sign in to comment.