Added fully functional discord RPC support

- Ability to change discord RPC heading (description) and details (state)
- Ability to toggle on or the RPC without launcher restart.
This commit is contained in:
Soham Nandy 2023-01-18 22:36:24 +05:30
parent 16bcc1f2dc
commit f2dda02106
3 changed files with 110 additions and 31 deletions

View file

@ -58,11 +58,33 @@ Adw.PreferencesPage page {
Adw.ActionRow discord_rpc_row
{
title: "Discord RPC";
subtitle: "Show your friends your lack of bitches";
subtitle: "Discord RPC allows you to provide Discord the info that you are currently playing the game to let your friends know.";
Gtk.Switch discord_rpc_switch {
valign: center;
}
}
Adw.ActionRow discord_rpc_desc_row
{
title: "Discord RPC Heading";
subtitle: "Set a custom heading for the activity status!\n(Requires launcher restart, or disable and re-enable the RPC)";
Gtk.Entry discord_rpc_desc
{
valign:center;
}
}
Adw.ActionRow discord_rpc_state_row
{
title: "Discord RPC State";
subtitle: "Set a custom description for the activity status!\n(Requires launcher restart, or disable and re-enable the RPC)";
Gtk.Entry discord_rpc_state
{
valign:center;
}
}
Adw.ComboRow fsr_combo {
title: "FSR";
subtitle: "Upscales game to your monitor size. To use select lower\nresolution in the game's settings and press Alt+Enter";

View file

@ -77,23 +77,29 @@ fn main() {
DiscordIpcClient::new(config.game.enhancements.discord_rpc.app_id.as_str())
.expect("Failed to create client");
match client.connect() {
Ok(_) => {
println!("Client connected to Discord successfully.");
}
Err(_) => {
println!(
"Client failed to connect to Discord, Please try again or relaunch Discord."
);
}
};
let mut activity_set:bool = false;
let mut connected: bool = false;
let _thread = std::thread::spawn(move || loop {
let conf = lib::config::get().expect("Failed to load config");
// println!("activity_set: {:?} connected: {:?}",activity_set,connected);
if conf.game.enhancements.discord_rpc.enabled {
if !connected{
match client.connect() {
Ok(_) => {
println!("Client connected to Discord successfully.");connected=true;
}
Err(_) => {
println!(
"Client failed to connect to Discord, Please try again or relaunch Discord."
);
}
};
}
let act = activity::Activity::new();
let activity_state: Activity = if config.game.enhancements.discord_rpc.state != "" {
act.state(config.game.enhancements.discord_rpc.state.as_str())
act.state(conf.game.enhancements.discord_rpc.state.as_str())
.clone()
} else {
act
@ -101,33 +107,42 @@ fn main() {
let activity_details: Activity =
if config.game.enhancements.discord_rpc.description != "" {
activity_state
.state(config.game.enhancements.discord_rpc.description.as_str())
.details(conf.game.enhancements.discord_rpc.description.as_str())
.clone()
} else {
activity_state
};
let activity_li: Activity =
if config.game.enhancements.discord_rpc.large_image_key != "" {
activity_details
.state(
config.game.enhancements.discord_rpc.large_image_key.as_str(),
)
.clone()
if conf.game.enhancements.discord_rpc.large_image_key != "" {
activity_details.assets(activity::Assets::new().large_image(config.game.enhancements.discord_rpc.large_image_key.as_str())).clone()
} else {
activity_details
};
match client.set_activity(activity_li) {
Ok(_) => {println!("Client set activity successfully.");}
Err(_) => {println!("Client failed to set activity, Please try again or relaunch Discord.");}
};
std::thread::sleep(std::time::Duration::from_millis(10));
} else {
match client.clear_activity(){
Ok(_) => {println!("Client activity cleared successfully.");}
Err(_) => {println!("Failed to clear.");}
if !activity_set{
match client.set_activity(activity_li) {
Ok(_) => {println!("Client set activity successfully."); activity_set=true;}
Err(_) => {println!("Client failed to set activity, Please try again or relaunch Discord.");}
};
}
std::thread::sleep(std::time::Duration::from_millis(1000));
} else {
if activity_set{
match client.clear_activity(){
Ok(_) => {println!("Client activity cleared successfully.");connected=false;activity_set=false}
Err(_) => {println!("Failed to clear.");}
}
}
if connected{
match client.close(){
Ok(_) => {println!("Client connection closed.");connected=false;}
Err(_) => {println!("Failed to clear.");}
}
}
}
std::thread::sleep(std::time::Duration::from_millis(10));
std::thread::sleep(std::time::Duration::from_millis(1000));
});
// Apply CSS styles to the application
let provider = CssProvider::new();

View file

@ -51,6 +51,12 @@ pub struct AppWidgets {
pub discord_rpc_row: adw::ActionRow,
pub discord_rpc: gtk::Switch,
pub discord_rpc_state_row: adw::ActionRow,
pub discord_rpc_state: gtk::Entry,
pub discord_rpc_desc_row: adw::ActionRow,
pub discord_rpc_desc: gtk::Entry,
}
impl AppWidgets {
@ -86,7 +92,12 @@ impl AppWidgets {
fps_unlocker_window_mode_combo: get_object(&builder, "fps_unlocker_window_mode_combo")?,
fps_unlocker_priority_combo: get_object(&builder, "fps_unlocker_priority_combo")?,
discord_rpc: get_object(&builder,"discord_rpc_switch")?,
discord_rpc_row: get_object(&builder, "discord_rpc_row")?
discord_rpc_row: get_object(&builder, "discord_rpc_row")?,
discord_rpc_state: get_object(&builder, "discord_rpc_state")?,
discord_rpc_state_row: get_object(&builder, "discord_rpc_state_row")?,
discord_rpc_desc: get_object(&builder, "discord_rpc_desc")?,
discord_rpc_desc_row: get_object(&builder, "discord_rpc_desc_row")?,
};
// Set availale wine languages
@ -109,9 +120,12 @@ impl AppWidgets {
result.gamescope_row.set_sensitive(false);
result.gamescope_row.set_tooltip_text(Some("Gamescope is not installed"));
}
// result.discord_rpc_desc_row.set_sensitive(true);
// result.discord_rpc_state_row.set_sensitive(true);
result.discord_rpc_row.set_sensitive(true);
result.discord_rpc.set_sensitive(true);
Ok(result)
}
}
@ -237,6 +251,31 @@ impl App {
config::update(config);
}
});
self.widgets.discord_rpc_state.connect_changed(move |state|
{
if let Ok(mut config) = config::get()
{
let string = state.text().as_str().to_string();
std::thread::sleep(std::time::Duration::from_millis(10));
config.game.enhancements.discord_rpc.state = string;
println!("Updated string: {}",config.game.enhancements.discord_rpc.state);
config::update(config);
}
});
self.widgets.discord_rpc_desc.connect_changed(move |state|
{
if let Ok(mut config) = config::get()
{
let string = state.text().as_str().to_string();
std::thread::sleep(std::time::Duration::from_millis(10));
config.game.enhancements.discord_rpc.description = string;
println!("Updated string: {}",config.game.enhancements.discord_rpc.description);
config::update(config);
}
});
// Gamemode switching
self.widgets.gamemode_switcher.connect_state_notify(move |switch| {
if let Ok(mut config) = config::get() {
@ -279,11 +318,12 @@ impl App {
}
});
// FPS unlocker -> power saving swithing
self.widgets.fps_unlocker_power_saving_switcher.connect_state_notify(move |switch| {
if let Ok(mut config) = config::get() {
config.game.enhancements.fps_unlocker.config.power_saving = switch.state();
config::update(config);
}
});
@ -372,6 +412,8 @@ impl App {
// Discord RPC
self.widgets.discord_rpc.set_state(config.game.enhancements.discord_rpc.enabled);
self.widgets.discord_rpc_state.set_placeholder_text(Some(config.game.enhancements.discord_rpc.state.as_str()));
self.widgets.discord_rpc_desc.set_placeholder_text(Some(config.game.enhancements.discord_rpc.description.as_str()));
// Gamemode switching
self.widgets.gamemode_switcher.set_state(config.game.enhancements.gamemode);