added tray | removed console at start

This commit is contained in:
AmokDev 2025-03-21 19:29:43 +03:00
parent ccc6603f88
commit 20f73be4c1
8 changed files with 128 additions and 4 deletions

1
.gitignore vendored
View file

@ -1,5 +1,6 @@
# custom
.env
app/data.json
# ---> Rust
# Generated by Cargo

View file

@ -1,11 +1,15 @@
[package]
name = "DiscordRPC-HDRezcord-APP"
version = "0.1.0"
version = "0.1.2"
edition = "2021"
[dependencies]
actix-web = "4"
image = "0.25.5"
open = "5.3.2"
rust-discord-rpc = { git = "https://git.amok.dev/AmokDev/rust-discord-rpc", version = "0.3.3" }
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
tokio = { version = "1", features = ["macros", "rt-multi-thread"] }
tray-icon = "0.20.0"
winit = "0.30.9"

View file

@ -1 +1 @@
{"episode":0,"season":0,"timestamps":[0, 0],"name":"name","image_url":"image_url"}
{"episode":0,"season":0,"timestamps":[1,1],"name":"Полетта","image_url":"https://statichdrezka.ac/i/2014/1/25/ze3dffce6f572bx77h96o.jpg"}

BIN
app/src/icons/32.ico Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 184 KiB

View file

@ -1,12 +1,18 @@
#![windows_subsystem = "windows"]
mod network;
mod discord;
mod utils;
use network::api::start_api;
use discord::rpc::start_presence;
use utils::tray::init_tray;
#[actix_web::main]
async fn main() -> std::io::Result<()> {
std::thread::spawn(|| {
init_tray();
});
let _ = tokio::join!(start_api(), start_presence());
Ok(())
}

View file

@ -3,7 +3,7 @@ use crate::utils::json_database::{save_to_json, PostData};
#[post("/set_presence")]
async fn set_presence(data: web::Json<PostData>) -> HttpResponse {
// println!("{:?}", data);
println!("{:?}", data);
let _ = save_to_json(&data);
HttpResponse::Ok().finish()
}

View file

@ -1 +1,2 @@
pub mod json_database;
pub mod json_database;
pub mod tray;

112
app/src/utils/tray.rs Normal file
View file

@ -0,0 +1,112 @@
use tray_icon::{
menu::{Menu, MenuEvent, MenuId, MenuItem},
TrayIcon, TrayIconBuilder, TrayIconEvent,
};
use winit::{
application::ApplicationHandler,
event_loop::EventLoop, platform::windows::EventLoopBuilderExtWindows,
};
use std::process;
#[derive(Debug)]
enum UserEvent {
MenuEvent(tray_icon::menu::MenuEvent),
}
struct Application {
tray_icon: Option<TrayIcon>,
}
impl Application {
fn new() -> Application {
Application { tray_icon: None }
}
fn new_tray_icon() -> TrayIcon {
let path = concat!(env!("CARGO_MANIFEST_DIR"), "/src/icons/32.ico");
let icon = load_icon(std::path::Path::new(path));
TrayIconBuilder::new()
.with_menu(Box::new(Self::new_tray_menu()))
.with_tooltip("HDRezcord")
.with_icon(icon)
.build()
.unwrap()
}
fn new_tray_menu() -> Menu {
let menu = Menu::new();
let _ = menu.append(&MenuItem::new("GitHub", true, None));
let _ = menu.append(&MenuItem::new("Exit", true, None));
menu
}
}
impl ApplicationHandler<UserEvent> for Application {
fn resumed(&mut self, _event_loop: &winit::event_loop::ActiveEventLoop) {}
fn window_event(
&mut self,
_event_loop: &winit::event_loop::ActiveEventLoop,
_window_id: winit::window::WindowId,
_event: winit::event::WindowEvent,
) {
}
fn new_events(
&mut self,
_event_loop: &winit::event_loop::ActiveEventLoop,
cause: winit::event::StartCause,
) {
if winit::event::StartCause::Init == cause {
#[cfg(not(target_os = "linux"))]
{
self.tray_icon = Some(Self::new_tray_icon());
}
}
}
fn user_event(&mut self, _event_loop: &winit::event_loop::ActiveEventLoop, event: UserEvent) {
// println!("{event:?}");
match event {
UserEvent::MenuEvent(menu_event) => {
// println!("{:?}", menu_event.id);
if menu_event.id == MenuId("1001".to_string()) {
let _ = open::that("https://git.amok.dev/AmokDev/HDRezcord");
} else if menu_event.id == MenuId("1002".to_string()) {
process::exit(0);
}
}
}
}
}
pub fn init_tray() {
let event_loop = EventLoop::<UserEvent>::with_user_event().with_any_thread(true).build().unwrap();
let proxy = event_loop.create_proxy();
MenuEvent::set_event_handler(Some(move |event| {
let _ = proxy.send_event(UserEvent::MenuEvent(event));
}));
let mut app = Application::new();
let _menu_channel = MenuEvent::receiver();
let _tray_channel = TrayIconEvent::receiver();
if let Err(err) = event_loop.run_app(&mut app) {
println!("Error: {:?}", err);
}
}
fn load_icon(path: &std::path::Path) -> tray_icon::Icon {
let (icon_rgba, icon_width, icon_height) = {
let image = image::open(path)
.expect("Failed to open icon path")
.into_rgba8();
let (width, height) = image.dimensions();
let rgba = image.into_raw();
(rgba, width, height)
};
tray_icon::Icon::from_rgba(icon_rgba, icon_width, icon_height).expect("Failed to open icon")
}