From 99bf0b6855d1d91d60a8c4050a0ea6c9a55bf81b Mon Sep 17 00:00:00 2001 From: MrDulfin Date: Mon, 26 May 2025 13:58:40 -0400 Subject: [PATCH] Added "Add to Queue" test button --- dmp-core/src/music_controller/connections.rs | 6 +-- src-tauri/src/commands.rs | 27 +++++++++--- src-tauri/src/lib.rs | 23 +++------- src/App.tsx | 46 ++++++++++++++++++-- 4 files changed, 74 insertions(+), 28 deletions(-) diff --git a/dmp-core/src/music_controller/connections.rs b/dmp-core/src/music_controller/connections.rs index 89dbfa8..40cd65e 100644 --- a/dmp-core/src/music_controller/connections.rs +++ b/dmp-core/src/music_controller/connections.rs @@ -1,15 +1,15 @@ use std::{ sync::{ - atomic::{AtomicBool, Ordering}, Arc, + atomic::{AtomicBool, Ordering}, }, thread::sleep, - time::{Duration, Instant, SystemTime, UNIX_EPOCH}, + time::{Duration, SystemTime, UNIX_EPOCH}, }; use chrono::TimeDelta; use crossbeam::select; -use crossbeam_channel::{bounded, unbounded, Receiver, Sender}; +use crossbeam_channel::{Receiver, Sender, bounded, unbounded}; use discord_presence::Client; use listenbrainz::ListenBrainz; use parking_lot::RwLock; diff --git a/src-tauri/src/commands.rs b/src-tauri/src/commands.rs index 7423940..eddc3c5 100644 --- a/src-tauri/src/commands.rs +++ b/src-tauri/src/commands.rs @@ -1,4 +1,3 @@ -use std::{fs::OpenOptions, io::Write}; use dmp_core::{ music_controller::{ connections::LastFMAuth, @@ -7,6 +6,7 @@ use dmp_core::{ }, music_storage::queue::{QueueItem, QueueItemType}, }; +use std::{fs::OpenOptions, io::Write}; use tauri::{AppHandle, Emitter, State, Wry}; use tempfile::TempDir; use uuid::Uuid; @@ -58,9 +58,9 @@ pub async fn display_album_art( temp_dir: State<'_, TempDir>, uuid: Uuid, ) -> Result<(), String> { - match ctrl_handle.lib_get_song(uuid.clone()).await.0.album_art(0) { + match ctrl_handle.lib_get_song(uuid).await.0.album_art(0) { Ok(art) => { - let mut art = art.unwrap(); + let art = art.unwrap(); let path = temp_dir.path().join(format!( "CoverArt_{uuid}.{}", file_format::FileFormat::from_bytes(&art).extension() @@ -74,7 +74,7 @@ pub async fn display_album_art( .read(true) .open(path.clone()) .unwrap(); - file.write_all(&mut art).unwrap(); + file.write_all(&art).unwrap(); } opener::open(path).unwrap(); } @@ -91,4 +91,21 @@ pub async fn last_fm_init_auth(ctrl_handle: State<'_, ControllerHandle>) -> Resu LastFMAuth::Session(None), ); Ok(()) -} \ No newline at end of file +} + +// #[tauri::command] +// pub async fn test_menu( +// ctrl_handle: State<'_, ControllerHandle>, +// app: AppHandle, +// window: Window, +// uuid: Uuid, +// ) -> Result<(), String> { +// let handle = app.app_handle(); +// let menu = MenuBuilder::new(handle) +// .item(&MenuItem::new(handle, "Add to Queue", true, None::<&str>).unwrap()) +// .build() +// .unwrap(); +// window.set_menu(menu).unwrap(); +// println!("Menu popup!"); +// Ok(()) +// } diff --git a/src-tauri/src/lib.rs b/src-tauri/src/lib.rs index 572b87e..2e60e65 100644 --- a/src-tauri/src/lib.rs +++ b/src-tauri/src/lib.rs @@ -4,12 +4,12 @@ use std::{ fs, path::PathBuf, sync::Arc, - thread::{scope, spawn, JoinHandle}, + thread::{scope, spawn}, time::Duration, }; use config::{close_window, get_config, open_config_window, save_config}; -use crossbeam::channel::{bounded, Receiver}; +use crossbeam::channel::bounded; use dmp_core::{ config::{Config, ConfigLibrary}, music_controller::{ @@ -19,7 +19,7 @@ use dmp_core::{ music_storage::library::{MusicLibrary, Song}, }; use parking_lot::RwLock; -use tauri::{http::Response, AppHandle, Emitter, Listener, Manager}; +use tauri::{http::Response, AppHandle, Emitter, Manager}; use uuid::Uuid; use wrappers::{_Song, stop}; @@ -69,6 +69,7 @@ pub fn run() { save_config, close_window, start_controller, + // test_menu, ]) .manage(tempfile::TempDir::new().unwrap()) .manage(sync_rx) @@ -128,19 +129,12 @@ fn start_controller(app: AppHandle) -> Result<(), String> { let mut config = init_get_config().unwrap(); let (lib_path, lib_uuid) = match config.libraries.get_default() { - Ok(library) => { - (library.path.clone(), library.uuid) - } - Err(_) => { - (create_new_library().unwrap(), Uuid::new_v4()) - } + Ok(library) => (library.path.clone(), library.uuid), + Err(_) => (create_new_library().unwrap(), Uuid::new_v4()), }; let scan_path = lib_path.parent().unwrap(); - println!( - "lib_path: {}\nscan_path:{scan_path:?}", - lib_path.display() - ); + println!("lib_path: {}\nscan_path:{scan_path:?}", lib_path.display()); let mut library = MusicLibrary::init(lib_path.clone(), lib_uuid).unwrap(); @@ -161,7 +155,6 @@ fn start_controller(app: AppHandle) -> Result<(), String> { library.save(lib_path.to_path_buf()).unwrap(); app.emit("library_loaded", ()).unwrap(); - let last_fm_session = config.connections.last_fm_session.clone(); let listenbrainz_token = config.connections.listenbrainz_token.clone(); @@ -260,7 +253,6 @@ fn init_get_config() -> Result { } } - fn create_new_library() -> Result { let dir = rfd::FileDialog::new() .set_title("Pick a library path") @@ -286,4 +278,3 @@ fn create_new_library() -> Result { Ok(path) } - diff --git a/src/App.tsx b/src/App.tsx index deb8369..7bb3fe7 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -1,4 +1,4 @@ -import React, { createRef, useEffect, useRef, useState } from "react"; +import React, { createRef, ReactEventHandler, useEffect, useRef, useState } from "react"; import { convertFileSrc, invoke } from "@tauri-apps/api/core"; import "./App.css"; import { Config, playbackInfo } from "./types"; @@ -6,6 +6,9 @@ import { Config, playbackInfo } from "./types"; // import { listen } from "@tauri-apps/api/event"; // import { fetch } from "@tauri-apps/plugin-http"; import { getCurrentWebviewWindow } from "@tauri-apps/api/webviewWindow"; +import { getCurrentWindow, LogicalPosition } from "@tauri-apps/api/window"; +import { Menu } from "@tauri-apps/api/menu"; +import { listen } from "@tauri-apps/api/event"; const appWindow = getCurrentWebviewWindow(); @@ -25,6 +28,7 @@ function App() { /> ); + useEffect(() => { const unlisten = appWindow.listen("now_playing_change", ({ payload, }) => { const displayArtwork = () => { @@ -240,6 +244,7 @@ function MainView({ lib_ref, viewName }: MainViewProps) { ) } + interface SongProps { location: any, playerLocation: string | {"Playlist" : any}, @@ -255,11 +260,43 @@ interface SongProps { function Song(props: SongProps) { // console.log(props.tags); + const add_to_queue_test = (_: string) => { + invoke('add_song_to_queue', { uuid: props.uuid, location: props.playerLocation }).then(() => {}); + } + + const songMenuPromise = Menu.new({ + items: [ + { id: "add_song_to_queue" + props.uuid, text: "Add to Queue", action: add_to_queue_test} + ] + }) + + async function clickHandler(event: React.MouseEvent) { + event.preventDefault(); + const menu = await songMenuPromise; + const pos = new LogicalPosition(event.clientX, event.clientY); + menu.popup(pos); + } + + // useEffect(() => { + // const unlistenPromise = listen("add_song_to_queue", (event) => { + // switch (event.payload) { + // default: + // console.log("Unimplemented application menu id:", event.payload); + // } + // }); + + // return () => { + // unlistenPromise.then((unlisten) => unlisten()); + // }; + // }, []); return( -
{ - invoke("play_now", { uuid: props.uuid, location: props.playerLocation }).then(() => {}) - }} className="song"> +
{ + invoke("play_now", { uuid: props.uuid, location: props.playerLocation }).then(() => {}) + }} + onContextMenu={clickHandler} + className="song">

{ props.tags.TrackArtist }

{ props.tags.TrackTitle }

{ props.tags.AlbumTitle }

@@ -271,6 +308,7 @@ function Song(props: SongProps) { ) } + interface PlayBarProps { playing: boolean, setPlaying: React.Dispatch>