chore(release): v0.1.5 - Linux ksni tray, 1:1 sidebar collapse button, zero-overhead Linux RAM tuning

This commit is contained in:
marwy 2026-11-27 18:35:49 +03:00
parent f063382410
commit ee65d23b2a
9 changed files with 684 additions and 48 deletions

View file

@ -451,22 +451,267 @@ mod imp {
mod imp {
use super::NativeEvent;
use crate::api::models::Track;
use crossbeam_channel::{unbounded, Receiver, Sender};
use ksni::blocking::{Handle, TrayMethods};
use std::io::{BufRead, BufReader};
use std::process::{Command, Stdio};
use std::sync::Arc;
use winit::window::Window;
const OAUTH_TOKEN_PREFIX: &str = "YM_OAUTH_TOKEN:";
pub struct NativeDesktop;
struct LinuxTray {
tx: Sender<NativeEvent>,
icons: Arc<Vec<ksni::Icon>>,
is_ru: bool,
window_visible: bool,
is_playing: bool,
track_summary: String,
}
impl ksni::Tray for LinuxTray {
fn id(&self) -> String {
"yandex-music-rs".into()
}
fn category(&self) -> ksni::Category {
ksni::Category::ApplicationStatus
}
fn title(&self) -> String {
if self.is_ru {
"Яндекс Музыка".into()
} else {
"Yandex Music".into()
}
}
fn icon_name(&self) -> String {
"yandex-music-rs".into()
}
fn icon_pixmap(&self) -> Vec<ksni::Icon> {
(*self.icons).clone()
}
fn tool_tip(&self) -> ksni::ToolTip {
ksni::ToolTip {
icon_name: "yandex-music-rs".into(),
icon_pixmap: Vec::new(),
title: self.title(),
description: self.track_summary.clone(),
}
}
fn activate(&mut self, _x: i32, _y: i32) {
let _ = self.tx.send(NativeEvent::ToggleWindow);
}
fn menu(&self) -> Vec<ksni::MenuItem<Self>> {
use ksni::menu::*;
let toggle_label = if self.window_visible {
if self.is_ru { "Скрыть" } else { "Hide" }
} else if self.is_ru {
"Показать"
} else {
"Show"
};
let play_label = if self.is_playing {
if self.is_ru { "Пауза" } else { "Pause" }
} else if self.is_ru {
"Играть"
} else {
"Play"
};
let ver_label = if self.is_ru {
format!("Яндекс Музыка {}", env!("CARGO_PKG_VERSION"))
} else {
format!("Yandex Music {}", env!("CARGO_PKG_VERSION"))
};
vec![
StandardItem {
label: toggle_label.into(),
activate: Box::new(|this: &mut Self| {
let _ = this.tx.send(NativeEvent::ToggleWindow);
}),
..Default::default()
}
.into(),
StandardItem {
label: if self.is_ru {
"Проверить обновления".into()
} else {
"Check updates".into()
},
activate: Box::new(|this: &mut Self| {
let _ = this.tx.send(NativeEvent::CheckUpdates);
}),
..Default::default()
}
.into(),
MenuItem::Separator,
StandardItem {
label: play_label.into(),
activate: Box::new(|this: &mut Self| {
let _ = this.tx.send(NativeEvent::TogglePlay);
}),
..Default::default()
}
.into(),
StandardItem {
label: if self.is_ru {
"Предыдущий трек".into()
} else {
"Previous song".into()
},
activate: Box::new(|this: &mut Self| {
let _ = this.tx.send(NativeEvent::Prev);
}),
..Default::default()
}
.into(),
StandardItem {
label: if self.is_ru {
"Следующий трек".into()
} else {
"Next song".into()
},
activate: Box::new(|this: &mut Self| {
let _ = this.tx.send(NativeEvent::Next);
}),
..Default::default()
}
.into(),
MenuItem::Separator,
SubMenu {
label: if self.is_ru {
"О приложении".into()
} else {
"About the app".into()
},
submenu: vec![StandardItem {
label: ver_label,
enabled: false,
..Default::default()
}
.into()],
..Default::default()
}
.into(),
MenuItem::Separator,
StandardItem {
label: if self.is_ru {
"Закрыть приложение".into()
} else {
"Close app".into()
},
activate: Box::new(|this: &mut Self| {
let _ = this.tx.send(NativeEvent::QuitApp);
}),
..Default::default()
}
.into(),
]
}
}
fn decode_ksni_icon(png_bytes: &[u8]) -> Option<ksni::Icon> {
let img = image::load_from_memory(png_bytes).ok()?.to_rgba8();
let (w, h) = img.dimensions();
let mut data = img.into_raw();
for px in data.chunks_exact_mut(4) {
px.rotate_right(1); // RGBA -> ARGB32 network byte order (SNI spec)
}
Some(ksni::Icon {
width: w as i32,
height: h as i32,
data,
})
}
pub struct NativeDesktop {
handle: Option<Handle<LinuxTray>>,
rx: Receiver<NativeEvent>,
last_playing: Option<bool>,
last_track_id: Option<String>,
}
impl Drop for NativeDesktop {
fn drop(&mut self) {
if let Some(ref handle) = self.handle {
handle.shutdown();
}
}
}
impl NativeDesktop {
pub fn new(_window: &Window, _is_ru: bool) -> Self {
Self
pub fn new(_window: &Window, is_ru: bool) -> Self {
let (tx, rx) = unbounded();
let mut pixmaps = Vec::with_capacity(2);
if let Some(ic32) = decode_ksni_icon(include_bytes!("../../assets/icons/tray_32.png")) {
pixmaps.push(ic32);
}
if let Some(ic48) = decode_ksni_icon(include_bytes!("../../assets/icons/app_48.png")) {
pixmaps.push(ic48);
}
let tray = LinuxTray {
tx,
icons: Arc::new(pixmaps),
is_ru,
window_visible: true,
is_playing: false,
track_summary: String::new(),
};
let handle = tray.assume_sni_available(true).spawn().ok();
Self {
handle,
rx,
last_playing: None,
last_track_id: None,
}
}
pub fn set_window_visible_label(&self, _visible: bool) {}
pub fn set_window_visible_label(&self, visible: bool) {
if let Some(ref handle) = self.handle {
handle.update(move |t| {
t.window_visible = visible;
});
}
}
pub fn poll_events(&self) -> Vec<NativeEvent> {
Vec::new()
let mut out = Vec::new();
while let Ok(ev) = self.rx.try_recv() {
out.push(ev);
}
out
}
pub fn sync_playback(&mut self, track: Option<&Track>, is_playing: bool, _pos_ms: u64) {
let track_id = track.map(|t| t.id.as_str());
if self.last_playing == Some(is_playing) && self.last_track_id.as_deref() == track_id {
return;
}
self.last_playing = Some(is_playing);
self.last_track_id = track_id.map(String::from);
let summary = track
.map(|tr| {
let artists = tr.artists_string();
if artists.is_empty() {
tr.title.clone()
} else {
format!("{} — {}", artists, tr.title)
}
})
.unwrap_or_default();
if let Some(ref handle) = self.handle {
handle.update(move |t| {
t.is_playing = is_playing;
t.track_summary = summary;
});
}
}
pub fn sync_playback(&mut self, _track: Option<&Track>, _is_playing: bool, _pos_ms: u64) {}
}
pub fn load_window_icon() -> Option<winit::window::Icon> {