From 3afc152ad88be0a281c251dd55fe5b77a3a7d866 Mon Sep 17 00:00:00 2001 From: Misaka Date: Sun, 22 Sep 2024 22:11:59 +0800 Subject: [PATCH] Update --- .vscode/launch.json | 45 +++++++++++++++++++++++++++++++++++++++ Cargo.toml | 3 --- src/config.rs | 39 ++++++++++++++++++++++----------- src/{utils.rs => font.rs} | 14 ++++++------ src/main.rs | 8 +++---- src/ui/mod.rs | 6 +++++- src/ui/run.rs | 7 +++--- src/ui/settings.rs | 31 +++++++++++++++++++++++++-- 8 files changed, 121 insertions(+), 32 deletions(-) create mode 100644 .vscode/launch.json rename src/{utils.rs => font.rs} (81%) diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..8af67c4 --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,45 @@ +{ + // 使用 IntelliSense 了解相关属性。 + // 悬停以查看现有属性的描述。 + // 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + { + "type": "lldb", + "request": "launch", + "name": "Debug executable 'desktop_lyric'", + "cargo": { + "args": [ + "build", + "--bin=desktop_lyric", + "--package=desktop_lyric" + ], + "filter": { + "name": "desktop_lyric", + "kind": "bin" + } + }, + "args": [], + "cwd": "${workspaceFolder}" + }, + { + "type": "lldb", + "request": "launch", + "name": "Debug unit tests in executable 'desktop_lyric'", + "cargo": { + "args": [ + "test", + "--no-run", + "--bin=desktop_lyric", + "--package=desktop_lyric" + ], + "filter": { + "name": "desktop_lyric", + "kind": "bin" + } + }, + "args": [], + "cwd": "${workspaceFolder}" + } + ] +} \ No newline at end of file diff --git a/Cargo.toml b/Cargo.toml index 19a7e01..eb16586 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -28,6 +28,3 @@ lto = "thin" panic = "abort" codegen-units = 1 opt-level = "s" - -[profile.dev] -debug = 0 diff --git a/src/config.rs b/src/config.rs index 0ffce7b..d88cf57 100644 --- a/src/config.rs +++ b/src/config.rs @@ -3,6 +3,7 @@ use log::info; use serde::de::Visitor; use serde::{Deserialize, Serialize, Serializer}; use std::fs::read_to_string; +use std::path::PathBuf; use std::str::FromStr; static DEFAULT_CONFIG: &'static str = include_str!("../config.yaml"); @@ -72,24 +73,38 @@ impl Config { serde_yaml::from_str(&content).unwrap() } - pub fn init() -> Self { + pub fn init() -> (Self, PathBuf) { if let Some(home_dir) = dirs::home_dir() { let path = home_dir .join(".config") .join("desktop_lyric") .join("config.yaml"); - if !path.exists() { - std::fs::create_dir_all(&path.parent().unwrap()).unwrap(); - std::fs::write(path, DEFAULT_CONFIG.as_bytes()).unwrap(); - info!("Using default config file"); - serde_yaml::from_str(DEFAULT_CONFIG).unwrap() - } else { - info!("Using config file: {}", path.to_string_lossy()); - serde_yaml::from_str(read_to_string(path).unwrap().as_str()).unwrap() - } + ( + if !path.exists() { + std::fs::create_dir_all(&path.parent().unwrap()).unwrap(); + std::fs::write(&path, DEFAULT_CONFIG.as_bytes()).unwrap(); + info!("Using default config file"); + serde_yaml::from_str(DEFAULT_CONFIG).unwrap() + } else { + info!("Using config file: {}", path.to_string_lossy()); + serde_yaml::from_str(read_to_string(&path).unwrap().as_str()).unwrap() + }, + path, + ) } else { - info!("Using default config file"); - serde_yaml::from_str(DEFAULT_CONFIG).unwrap() + let path: PathBuf = "./config.yaml".into(); + ( + if !path.exists() { + std::fs::create_dir_all(&path.parent().unwrap()).unwrap(); + std::fs::write(&path, DEFAULT_CONFIG.as_bytes()).unwrap(); + info!("Using default config file"); + serde_yaml::from_str(DEFAULT_CONFIG).unwrap() + } else { + info!("Using config file: {}", path.to_string_lossy()); + serde_yaml::from_str(read_to_string(&path).unwrap().as_str()).unwrap() + }, + path, + ) } } } diff --git a/src/utils.rs b/src/font.rs similarity index 81% rename from src/utils.rs rename to src/font.rs index 879edaa..30a1869 100644 --- a/src/utils.rs +++ b/src/font.rs @@ -9,7 +9,8 @@ fn load_font(config: &Config) -> Option { // .ttf and .otf files supported. return Some(FontData::from_owned(font_data)); } - } else if let Some(font_name) = config.font_name.clone() { + } + if let Some(font_name) = config.font_name.clone() { if let Some(font) = font_loader::system_fonts::get( &FontPropertyBuilder::new() .family(font_name.as_str()) @@ -17,14 +18,13 @@ fn load_font(config: &Config) -> Option { ) { return Some(FontData::from_owned(font.0)); } + } + + if let Some(font) = font_loader::system_fonts::get(&FontPropertyBuilder::new().build()) { + return Some(FontData::from_owned(font.0)); } else { - if let Some(font) = font_loader::system_fonts::get(&FontPropertyBuilder::new().build()) { - return Some(FontData::from_owned(font.0)); - } else { - return None; - } + return None; } - None } pub fn setup_custom_fonts(ctx: &egui::Context, config: &Config) { diff --git a/src/main.rs b/src/main.rs index 68df404..42eab8c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -6,11 +6,11 @@ use log::info; use ui::DesktopLyricApp; mod config; +mod font; mod fuo; mod lyric; mod serve; mod ui; -mod utils; #[derive(clap::Parser)] struct Args { @@ -21,11 +21,11 @@ struct Args { fn main() -> eframe::Result { env_logger::init(); // Log to stderr (if you run with `RUST_LOG=debug`). let args = Args::parse(); - let config = if let Some(path) = args.config { + let (config, config_path) = if let Some(path) = args.config { info!("Using config file: {}", path); - Config::from_file(path.as_str()) + (Config::from_file(path.as_str()), path.into()) } else { Config::init() }; - DesktopLyricApp::run(config) + DesktopLyricApp::run(config, config_path) } diff --git a/src/ui/mod.rs b/src/ui/mod.rs index 17a432d..b51cea1 100644 --- a/src/ui/mod.rs +++ b/src/ui/mod.rs @@ -1,4 +1,7 @@ -use std::sync::{mpsc::SyncSender, Arc}; +use std::{ + path::PathBuf, + sync::{mpsc::SyncSender, Arc}, +}; use eframe::{ egui::{self, mutex::RwLock, ViewportId}, @@ -20,6 +23,7 @@ pub struct DesktopLyricApp { pub players: Vec, pub event_sender: SyncSender, pub config: Config, + pub config_path: PathBuf, } impl App for DesktopLyricApp { diff --git a/src/ui/run.rs b/src/ui/run.rs index 8bee823..17643b6 100644 --- a/src/ui/run.rs +++ b/src/ui/run.rs @@ -1,13 +1,13 @@ -use std::sync::mpsc::sync_channel; +use std::{path::PathBuf, sync::mpsc::sync_channel}; use eframe::egui::ViewportBuilder; -use crate::{config::Config, serve::serve, utils::setup_custom_fonts}; +use crate::{config::Config, font::setup_custom_fonts, serve::serve}; use super::DesktopLyricApp; impl DesktopLyricApp { - pub fn run(config: Config) -> eframe::Result { + pub fn run(config: Config, config_path: PathBuf) -> eframe::Result { let options = eframe::NativeOptions { viewport: ViewportBuilder::default() .with_decorations(false) // Hide the OS-specific "chrome" around the window @@ -29,6 +29,7 @@ impl DesktopLyricApp { setup_custom_fonts(&cc.egui_ctx, &config); Ok(Box::new(DesktopLyricApp { config, + config_path, current_lyric: lock, drag_mode: true, settings_viewport_id: None, diff --git a/src/ui/settings.rs b/src/ui/settings.rs index 72ee5b4..492b61a 100644 --- a/src/ui/settings.rs +++ b/src/ui/settings.rs @@ -1,7 +1,9 @@ +use std::fs::write; + use eframe::egui::{CentralPanel, ComboBox, Context, Slider, ViewportBuilder, ViewportId}; use log::info; -use crate::serve::Event; +use crate::{font::setup_custom_fonts, serve::Event}; use super::DesktopLyricApp; @@ -35,7 +37,7 @@ impl DesktopLyricApp { let mut player_name = self.config.player_name.clone(); ui.horizontal(|ui| { ui.label("Player"); - ComboBox::from_label("") + ComboBox::from_id_source("player_combo_box") .selected_text(&self.config.player_name) .show_ui(ui, |ui| { for player in self.players.iter() { @@ -63,6 +65,31 @@ impl DesktopLyricApp { .changed() .then(|| self.event_sender.send(Event::ToggleFuzzy).ok()); }); + let mut font_name = self.config.font_name.clone().unwrap_or("".to_owned()); + ui.horizontal(|ui| { + ui.label("Font"); + ComboBox::from_id_source("font_combo_box") + .selected_text(&self.config.font_name.clone().unwrap_or("".to_owned())) + .show_ui(ui, |ui| { + for font in font_loader::system_fonts::query_all() { + ui.selectable_value(&mut font_name, font.clone(), font.as_str()); + } + }); + }); + if let Some(config_font_name) = &self.config.font_name { + if font_name != config_font_name.as_str() { + info!("Font changed to {}", font_name); + self.config.font_name = Some(font_name.clone()); + setup_custom_fonts(ctx, &self.config); + } + } else { + self.config.font_name = Some(font_name.clone()); + } + if ui.button("Save").clicked() { + if let Ok(data) = serde_yaml::to_string(&self.config) { + write(&self.config_path, data.as_bytes()).ok(); + } + } }); }); }