Skip to content

Commit

Permalink
refactor: discord rpc which now shows as listening
Browse files Browse the repository at this point in the history
  • Loading branch information
0PandaDEV committed Jan 2, 2025
1 parent 23b285e commit 8f8902e
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 71 deletions.
54 changes: 30 additions & 24 deletions app.vue
Original file line number Diff line number Diff line change
Expand Up @@ -6,83 +6,89 @@
</template>

<script lang="ts" setup>
import { register, unregister, isRegistered } from '@tauri-apps/plugin-global-shortcut';
import initializeSettings from '~/plugins/settings';
import {
register,
unregister,
isRegistered,
} from "@tauri-apps/plugin-global-shortcut";
import initializeSettings from "~/plugins/settings";
import { invoke } from "@tauri-apps/api/core";
const { $player, $settings } = useNuxtApp();
onMounted(async () => {
await initializeSettings(useNuxtApp())
await initializeSettings(useNuxtApp());
document.addEventListener('keydown', handleKeyDown);
document.addEventListener('focusin', updateFocus);
document.addEventListener('focusout', updateFocus);
document.addEventListener("keydown", handleKeyDown);
document.addEventListener("focusin", updateFocus);
document.addEventListener("focusout", updateFocus);
if (await isRegistered("MediaPlayPause")) {
await unregister("MediaPlayPause")
await unregister("MediaPlayPause");
}
await register('MediaPlayPause', (event) => {
await register("MediaPlayPause", (event) => {
if (event.state === "Pressed") {
$player.playPause()
$player.playPause();
}
});
if (await isRegistered("MediaTrackNext")) {
await unregister("MediaTrackNext")
await unregister("MediaTrackNext");
}
await register('MediaTrackNext', (event) => {
await register("MediaTrackNext", (event) => {
if (event.state === "Pressed") {
$player.skip()
$player.skip();
}
});
if (await isRegistered("MediaTrackPrevious")) {
await unregister("MediaTrackPrevious")
await unregister("MediaTrackPrevious");
}
await register('MediaTrackPrevious', (event) => {
await register("MediaTrackPrevious", (event) => {
if (event.state === "Pressed") {
$player.rewind();
}
});
});
onUnmounted(async () => {
document.removeEventListener('keydown', handleKeyDown);
document.removeEventListener('focusin', updateFocus);
document.removeEventListener('focusout', updateFocus);
document.removeEventListener("keydown", handleKeyDown);
document.removeEventListener("focusin", updateFocus);
document.removeEventListener("focusout", updateFocus);
if (await isRegistered("MediaPlayPause")) {
await unregister("MediaPlayPause")
await unregister("MediaPlayPause");
}
if (await isRegistered("MediaTrackNext")) {
await unregister("MediaTrackNext")
await unregister("MediaTrackNext");
}
if (await isRegistered("MediaTrackPrevious")) {
await unregister("MediaTrackPrevious")
await unregister("MediaTrackPrevious");
}
});
const isTextInputFocused = ref(false);
function handleKeyDown(event: KeyboardEvent) {
if (event.code === 'Space' && !isTextInputFocused.value) {
if (event.code === "Space" && !isTextInputFocused.value) {
$player.playPause();
event.preventDefault();
}
}
function updateFocus() {
const activeElement = document.activeElement;
isTextInputFocused.value = activeElement instanceof HTMLInputElement ||
isTextInputFocused.value =
activeElement instanceof HTMLInputElement ||
activeElement instanceof HTMLTextAreaElement;
}
</script>

<style lang="scss">
@use '~/assets/styles/global';
</style>
@use "~/assets/styles/global";
</style>
1 change: 0 additions & 1 deletion plugins/player.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,6 @@ export default defineNuxtPlugin((nuxtApp) => {
await invoke('update_activity', {
details: `by ${this.currentSong.value?.artist}`,
largeImage: 'https://api.vleer.app/thumbnail?id=' + this.currentSong.value?.id,
largeImageText: this.currentSong.value?.title,
state: this.currentSong.value?.title,
youtubeUrl: 'https://www.youtube.com/watch?v=' + this.currentSong.value?.id
})
Expand Down
64 changes: 25 additions & 39 deletions src-tauri/Cargo.lock

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

4 changes: 2 additions & 2 deletions src-tauri/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ tauri-build = { version = "2.0.3", features = [] }

[dependencies]
anyhow = "1.0.95"
discord-ipc-rp = "0.1.1"
discord-rich-presence = "0.2.5"
lazy_static = "1.5.0"
regex = "1.11.1"
reqwest = "0.12.11"
reqwest = "0.12.12"
serde = "1.0.217"
serde_json = "1.0.134"
sqlx = { version = "0.8.2", features = [
Expand Down
16 changes: 11 additions & 5 deletions src-tauri/src/api/discord_rpc.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
use discord_ipc_rp::{activity, DiscordIpc, DiscordIpcClient};
use discord_rich_presence::{activity, DiscordIpc, DiscordIpcClient};
use lazy_static::lazy_static;
use std::env;
use std::sync::Mutex;
use std::thread;
use std::time::{SystemTime, UNIX_EPOCH};

lazy_static! {
static ref DRPC_CLIENT: Mutex<Option<DiscordIpcClient>> =
Mutex::new(Some(DiscordIpcClient::new("1194990403963858984")));
Mutex::new(Some(DiscordIpcClient::new("1194990403963858984").unwrap()));
}

fn is_discord_rpc_disabled() -> bool {
Expand Down Expand Up @@ -53,7 +54,6 @@ pub fn update_activity(
state: String,
details: String,
large_image: String,
large_image_text: String,
youtube_url: Option<String>,
) -> Result<(), String> {
if is_discord_rpc_disabled() {
Expand All @@ -64,14 +64,20 @@ pub fn update_activity(
let drpc = DRPC_CLIENT.lock().map_err(|e| e.to_string());
if let Ok(mut drpc) = drpc {
if let Some(ref mut client) = *drpc {
let start_timestamp = SystemTime::now()
.duration_since(UNIX_EPOCH)
.expect("Time went backwards")
.as_secs() as i64;

let mut activity_builder = activity::Activity::new()
.state(&state)
.details(&details)
.assets(
activity::Assets::new()
.large_image(&large_image)
.large_text(&large_image_text),
);
)
.timestamps(activity::Timestamps::new().start(start_timestamp))
.activity_type(activity::ActivityType::Listening);

if let Some(ref url) = youtube_url {
let youtube_button = activity::Button::new("YouTube", url);
Expand Down

0 comments on commit 8f8902e

Please sign in to comment.