Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
Moeweb647252 committed Sep 22, 2024
1 parent 7c6d9ab commit 3afc152
Show file tree
Hide file tree
Showing 8 changed files with 121 additions and 32 deletions.
45 changes: 45 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -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}"
}
]
}
3 changes: 0 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,3 @@ lto = "thin"
panic = "abort"
codegen-units = 1
opt-level = "s"

[profile.dev]
debug = 0
39 changes: 27 additions & 12 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -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,
)
}
}
}
14 changes: 7 additions & 7 deletions src/utils.rs → src/font.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,22 @@ fn load_font(config: &Config) -> Option<FontData> {
// .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())
.build(),
) {
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) {
Expand Down
8 changes: 4 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)
}
6 changes: 5 additions & 1 deletion src/ui/mod.rs
Original file line number Diff line number Diff line change
@@ -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},
Expand All @@ -20,6 +23,7 @@ pub struct DesktopLyricApp {
pub players: Vec<Player>,
pub event_sender: SyncSender<Event>,
pub config: Config,
pub config_path: PathBuf,
}

impl App for DesktopLyricApp {
Expand Down
7 changes: 4 additions & 3 deletions src/ui/run.rs
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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,
Expand Down
31 changes: 29 additions & 2 deletions src/ui/settings.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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();
}
}
});
});
}
Expand Down

0 comments on commit 3afc152

Please sign in to comment.