Discord RPC: fixed connection / disconnection on game launch

This commit is contained in:
Observer KRypt0n_ 2023-01-28 13:30:14 +02:00
parent ab27e072f4
commit 64921ea3cb
No known key found for this signature in database
GPG key ID: 844DA47BA25FE1E2
3 changed files with 20 additions and 12 deletions

View file

@ -17,8 +17,8 @@ impl Default for DiscordRpc {
app_id: 901534333360304168,
enabled: false,
title: String::from("of Teyvat"),
subtitle: String::from("Researching the world"),
title: String::from("Researching the world"),
subtitle: String::from("of Teyvat"),
image: String::from("gi-icon")
}
}

View file

@ -42,11 +42,13 @@ impl DiscordRpc {
let mut client = DiscordIpcClient::new(&config.app_id.to_string())
.expect("Failed to register discord ipc client");
let mut connected = false;
while let Ok(update) = receiver.recv() {
match update {
RpcUpdates::Connect => {
if !config.enabled {
config.enabled = true;
if !connected {
connected = true;
client.connect().expect("Failed to connect to discord");
@ -56,8 +58,8 @@ impl DiscordRpc {
}
RpcUpdates::Disconnect => {
if config.enabled {
config.enabled = false;
if connected {
connected = false;
client.close().expect("Failed to disconnect from discord");
}
@ -68,14 +70,16 @@ impl DiscordRpc {
config.subtitle = subtitle;
config.image = image;
if config.enabled {
if connected {
client.set_activity(Self::get_activity(&config))
.expect("Failed to update discord rpc activity");
}
}
RpcUpdates::ClearActivity => {
client.clear_activity().expect("Failed to clear discord rpc activity");
if connected {
client.clear_activity().expect("Failed to clear discord rpc activity");
}
}
}
}
@ -86,8 +90,8 @@ impl DiscordRpc {
pub fn get_activity(config: &DiscordRpcConfig) -> Activity {
Activity::new()
.state(&config.title)
.details(&config.subtitle)
.details(&config.title)
.state(&config.subtitle)
.assets(Assets::new().large_image(&config.image))
}

View file

@ -358,7 +358,9 @@ impl App {
else {
std::thread::sleep(std::time::Duration::from_secs(2));
this.widgets.preferences_stack.enhancements_page.discord_rpc.update(RpcUpdates::Connect);
if config.launcher.discord_rpc.enabled {
this.widgets.preferences_stack.enhancements_page.discord_rpc.update(RpcUpdates::Connect);
}
loop {
std::thread::sleep(std::time::Duration::from_secs(3));
@ -375,7 +377,9 @@ impl App {
}
}
this.widgets.preferences_stack.enhancements_page.discord_rpc.update(RpcUpdates::Disconnect);
if config.launcher.discord_rpc.enabled {
this.widgets.preferences_stack.enhancements_page.discord_rpc.update(RpcUpdates::Disconnect);
}
this.widgets.window.show();
}