Hello I'm incorporating discord-rpc inside my tauri app and i have a little problem .
So I have this snippet rust loop { if let Err(why) = drpc.set_activity(|a| { a.details("Kamyroll") .state("Watching anime") .assets(|a| { a.large_image("cover") .large_text("Kamyroll") .small_image("crunchyroll") .small_text("Watching anime") }) }) { println!("Failed to set presence: {}", why); } }
and I wanted to know how can I implement it within tauri without a loop (or maybe with a loop but that implements well)
#Discord-RPC & Tauri
37 messages · Page 1 of 1 (latest)
and the idea is that i want to have information about the tauri window and evaluate in rust
with the loop in gives me that
Can you provide more context? Where is this loop located?
fn main() {
if let Err(ref e) = install_schema() {
println!("error: {}", e);
}
let mut drpc = Client::new(1037127420324610138);
drpc.start();
loop {
if let Err(why) = drpc.set_activity(|a| {
a.details("On home page")
.state("Watching anime")
.assets(|a| {
a.large_image("cover")
.large_text("Kamyroll")
.small_image("crunchyroll")
.small_text("Watching anime")
})
}) {
println!("Failed to set presence: {}", why);
}
}
let quit = CustomMenuItem::new("quit".to_string(), "Quit");
let hide = CustomMenuItem::new("hide".to_string(), "Hide");
let show = CustomMenuItem::new("show".to_string(),"Show");
let tray_menu = SystemTrayMenu::new()
.add_item(hide)
.add_native_item(SystemTrayMenuItem::Separator)
.add_item(quit)
.add_native_item(SystemTrayMenuItem::Separator)
.add_item(show);
tauri::Builder::default()
.system_tray(SystemTray::new().with_menu(tray_menu))
.on_system_tray_event(|app, event| match event {
[...]
})
.setup(|_app| {
[...]
})
/* .plugin(tauri_plugin_window_state::Builder::default().build()) */
.invoke_handler(tauri::generate_handler![close_splashscreen/* , download_file */])
.run(tauri::generate_context!())
.expect("failed to run app");
}```
for now it's like that
the loop is at the beginning
but when I put at the end it doesn't work anymore
and ofc when i put at the beginning my window doesn't even open
Okay, It's going to be a long explanation, so give me a moment
sure
What crate are you using for Discord IPC right now?
discord-rpc-client = "0.3.0"
When you start a loop like that, your tell your current thread repeats this loop.
Hence the warning "any code following this expression is unreachable".
The main funciton is busy processing the loop. Because it's an endless loop – the main function never gets to run the code that starts your app.
To fix this issue: you need to run both processes in parallel.
The most basic example to run some task in parallel would be:
thread::spawn(|| {
// Some task
});
// Some other task
However, there is no need to set the activity in the loop. You can set it once and it will persist until your Discord IPC client stops, or you set it to a new value.
So just remove the loop and this will fix the current version:
fn main() {
let mut drpc = DiscordIpcClient::new(1037127420324610138);
drpc.start();
drpc.set_activity(|a| {
// Activity
}).unwrap();
// Tauri builder, Etc.
}
But your goal is probably to change the activity dynamicly based on what is happening in your app. This is when it gets tricky.
Even if you send your Discord IPC client in a separate thread – there is no way to communicate with it and update the state.
When you need to share state between multiple threads (e.g. Tauri commands), you need to wrap your type in Arc<Mutex<YourType>>.
Basic explanation of Arc<Mutex<YourType>> is that it makes it possible to share values between threads. It makes sure that only one thread is able to access it at the same time.
You can research it on your own, because it very usefull. However, I've made a wrapper for another Discord IPC library that uses Arc<Mutex<_>> under the hood, so it's much easier to use that.
https://github.com/KPidS/declarative-discord-rich-presence
declarative-discord-rich-presence = { git = "https://github.com/KPidS/declarative-discord-rich-presence" }
You need to add the library as a git dependency, because it's not on crates.io.
.setup(|app| {
let discord_ipc_client = DeclarativeDiscordIpcClient::new("771124766517755954");
discord_ipc_client.enable();
discord_ipc_client.set_activity(...);
// Add client to the Tauri managed state so you can access it through commands or AppHandle
app.manage(discord_ipc_client);
})
#[tauri::command]
fn set_activity(discord_ipc_client: State<'_, DeclarativeDiscordIpcClient> {
discord_ipc_client.set_activity(...);
}
thanks
I will look into it
@young garnet can u show me how to set an activity with your thing
It's the same as the underlying crate: https://crates.io/crates/discord-rich-presence/
.set_activity(activity::Activity::new()
.state("foo")
.details("bar")
)
https://docs.rs/discord-rich-presence/0.2.3/discord_rich_presence/activity/struct.Activity.html
A struct representing a Discord rich presence activity
can u rewrite the tauri command function please
#[tauri::command]
fn set_activity(discord_ipc_client: State<'_, DeclarativeDiscordIpcClient>) {
discord_ipc_client.set_activity(...);
}
It was missing a ) but besides that I think it should be ok
#[tauri::command]
fn set_activity(discord_ipc_client: State<'_, DeclarativeDiscordIpcClient>) {
discord_ipc_client.set_activity(
activity::Activity::new()
.state("foo")
.details("bar")
);
}
.setup(|app| {
// ...
Ok(()) // Add this at the end of closure
})
use tauri::State
🎉 🎉 🎉
thx a lot
Oof