#Discord-RPC & Tauri

37 messages · Page 1 of 1 (latest)

foggy sable
#

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)

#

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

young garnet
#

Can you provide more context? Where is this loop located?

foggy sable
#
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

young garnet
#

Okay, It's going to be a long explanation, so give me a moment

foggy sable
#

sure

young garnet
#

What crate are you using for Discord IPC right now?

foggy sable
#

discord-rpc-client = "0.3.0"

young garnet
# foggy sable sure

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(...);
}
GitHub

An abstraction build on top of discord-rich-presence that makes possible to use it in a more declarative way - GitHub - KPidS/declarative-discord-rich-presence: An abstraction build on top of disco...

foggy sable
#

thanks

#

I will look into it

#

@young garnet can u show me how to set an activity with your thing

young garnet
foggy sable
#

can u rewrite the tauri command function please

young garnet
#
#[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

foggy sable
#

and the ... ?

#

i should let it like that ?

#

i assume not

young garnet
#
#[tauri::command]
fn set_activity(discord_ipc_client: State<'_, DeclarativeDiscordIpcClient>) {
    discord_ipc_client.set_activity(
      activity::Activity::new()
        .state("foo")
        .details("bar")
    );
}
foggy sable
young garnet
#
.setup(|app| {
  // ...
  Ok(()) // Add this at the end of closure
})
foggy sable
#

now on the tauri command

young garnet
#
use tauri::State
foggy sable
#

Finally

young garnet
#

🎉 🎉 🎉

foggy sable
#

thx a lot

foggy sable
#

Oof