From 460b1477b4615c8a0432389df2446c0747d2498b Mon Sep 17 00:00:00 2001 From: tramhao Date: Sat, 21 May 2022 15:54:36 +0800 Subject: [PATCH] minor fix --- src/ui/components/music_library.rs | 57 ++++++++++++------------ src/ui/components/playlist.rs | 71 ++++++++++++++++-------------- src/ui/model/view.rs | 13 +----- 3 files changed, 68 insertions(+), 73 deletions(-) diff --git a/src/ui/components/music_library.rs b/src/ui/components/music_library.rs index 1f3fc554..d6adeaa8 100644 --- a/src/ui/components/music_library.rs +++ b/src/ui/components/music_library.rs @@ -1,4 +1,4 @@ -use crate::config::{Keys, StyleColorSymbol}; +use crate::config::{Keys, Termusic}; use crate::ui::model::MAX_DEPTH; use crate::ui::{Id, LIMsg, Model, Msg, TEMsg, YSMsg}; use crate::utils::get_pin_yin; @@ -20,12 +20,7 @@ pub struct MusicLibrary { } impl MusicLibrary { - pub fn new( - tree: &Tree, - initial_node: Option, - color_mapping: &StyleColorSymbol, - keys: &Keys, - ) -> Self { + pub fn new(tree: &Tree, initial_node: Option, config: &Termusic) -> Self { // Preserve initial node if exists let initial_node = match initial_node { Some(id) if tree.root().query(&id).is_some() => id, @@ -33,24 +28,44 @@ impl MusicLibrary { }; Self { component: TreeView::default() - .background(color_mapping.library_background().unwrap_or(Color::Reset)) - .foreground(color_mapping.library_foreground().unwrap_or(Color::Magenta)) + .background( + config + .style_color_symbol + .library_background() + .unwrap_or(Color::Reset), + ) + .foreground( + config + .style_color_symbol + .library_foreground() + .unwrap_or(Color::Magenta), + ) .borders( Borders::default() - .color(color_mapping.library_border().unwrap_or(Color::Magenta)) + .color( + config + .style_color_symbol + .library_border() + .unwrap_or(Color::Magenta), + ) .modifiers(BorderType::Rounded), ) // .inactive(Style::default().fg(Color::Gray)) .indent_size(2) .scroll_step(6) .title("Library", Alignment::Left) - .highlighted_color(color_mapping.library_highlight().unwrap_or(Color::Yellow)) - .highlight_symbol(&color_mapping.library_highlight_symbol) + .highlighted_color( + config + .style_color_symbol + .library_highlight() + .unwrap_or(Color::Yellow), + ) + .highlight_symbol(&config.style_color_symbol.library_highlight_symbol) .preserve_state(true) // .highlight_symbol("🦄") .with_tree(tree.clone()) .initial_node(initial_node), - keys: keys.clone(), + keys: config.keys.clone(), } } @@ -274,7 +289,6 @@ impl Model { State::One(StateValue::String(id)) => Some(id), _ => None, }; - // Remount tree // keep focus let mut focus_library = false; if let Ok(f) = self.app.query(&Id::Library, Attribute::Focus) { @@ -282,12 +296,6 @@ impl Model { focus_library = true; } } - // let mut focus_playlist = false; - // if let Ok(f) = self.app.query(&Id::Playlist, Attribute::Focus) { - // if Some(AttrValue::Flag(true)) == f { - // focus_playlist = true; - // } - // } assert!(self.app.umount(&Id::Library).is_ok()); assert!(self @@ -297,21 +305,14 @@ impl Model { Box::new(MusicLibrary::new( &self.tree.clone(), current_node, - &self.config.style_color_symbol, - &self.config.keys, + &self.config, ),), Vec::new() ) .is_ok()); if focus_library { assert!(self.app.active(&Id::Library).is_ok()); - // return; } - // if focus_playlist { - // return; - // } - - // assert!(self.app.active(&Id::Library).is_ok()); } pub fn library_stepinto(&mut self, node_id: &str) { diff --git a/src/ui/components/playlist.rs b/src/ui/components/playlist.rs index 09b13c48..f2845f18 100644 --- a/src/ui/components/playlist.rs +++ b/src/ui/components/playlist.rs @@ -1,5 +1,5 @@ use crate::{ - config::{get_app_config_path, Keys, StyleColorSymbol}, + config::{get_app_config_path, Keys, Termusic}, track::Track, ui::{GSMsg, Id, Loop, Model, Msg, PLMsg}, }; @@ -32,24 +32,38 @@ pub struct Playlist { } impl Playlist { - pub fn new(color_mapping: &StyleColorSymbol, keys: &Keys) -> Self { + pub fn new(config: &Termusic) -> Self { Self { component: Table::default() .borders( - Borders::default() - .modifiers(BorderType::Rounded) - .color(color_mapping.playlist_border().unwrap_or(Color::Blue)), + Borders::default().modifiers(BorderType::Rounded).color( + config + .style_color_symbol + .playlist_border() + .unwrap_or(Color::Blue), + ), + ) + .background( + config + .style_color_symbol + .playlist_background() + .unwrap_or(Color::Reset), + ) + .foreground( + config + .style_color_symbol + .playlist_foreground() + .unwrap_or(Color::Yellow), ) - .background(color_mapping.playlist_background().unwrap_or(Color::Reset)) - .foreground(color_mapping.playlist_foreground().unwrap_or(Color::Yellow)) .title("Playlist", Alignment::Left) .scroll(true) .highlighted_color( - color_mapping + config + .style_color_symbol .playlist_highlight() .unwrap_or(Color::LightBlue), ) - .highlighted_str(&color_mapping.playlist_highlight_symbol) + .highlighted_str(&config.style_color_symbol.playlist_highlight_symbol) .rewind(false) .step(4) .row_height(1) @@ -63,7 +77,7 @@ impl Playlist { .add_col(TextSpan::from("Empty")) .build(), ), - keys: keys.clone(), + keys: config.keys.clone(), } } } @@ -161,10 +175,10 @@ impl Component for Playlist { impl Model { pub fn playlist_reload(&mut self) { // keep focus - let mut focus_playlist = false; + let mut focus = false; if let Ok(f) = self.app.query(&Id::Playlist, Attribute::Focus) { if Some(AttrValue::Flag(true)) == f { - focus_playlist = true; + focus = true; } } @@ -173,15 +187,12 @@ impl Model { .app .mount( Id::Playlist, - Box::new(Playlist::new( - &self.config.style_color_symbol, - &self.config.keys - )), + Box::new(Playlist::new(&self.config)), Vec::new() ) .is_ok()); self.playlist_sync(); - if focus_playlist { + if focus { assert!(self.app.active(&Id::Playlist).is_ok()); } } @@ -232,19 +243,16 @@ impl Model { if !filetype_supported(current_node) { return Ok(()); } - match Track::read_from_path(current_node) { - Ok(item) => { - if add_playlist_front { - self.playlist_items.push_front(item); - } else { - self.playlist_items.push_back(item); - } - self.playlist_sync(); - } - Err(e) => return Err(e), + let item = Track::read_from_path(current_node)?; + if add_playlist_front { + self.playlist_items.push_front(item); + } else { + self.playlist_items.push_back(item); } + self.playlist_sync(); Ok(()) } + pub fn playlist_add(&mut self, current_node: &str) { let p: &Path = Path::new(¤t_node); if !p.exists() { @@ -339,7 +347,6 @@ impl Model { pub fn playlist_empty(&mut self) { self.playlist_items.clear(); self.playlist_sync(); - // self.app.active(&Id::Library).ok(); } pub fn playlist_save(&mut self) -> Result<()> { @@ -393,12 +400,8 @@ impl Model { } pub fn playlist_update_library_delete(&mut self) { - self.playlist_items.retain(|x| { - x.file().map_or(false, |p| { - let path = Path::new(p); - path.exists() - }) - }); + self.playlist_items + .retain(|x| x.file().map_or(false, |p| Path::new(p).exists())); self.playlist_sync(); } diff --git a/src/ui/model/view.rs b/src/ui/model/view.rs index 8c3901ac..bda05ede 100644 --- a/src/ui/model/view.rs +++ b/src/ui/model/view.rs @@ -70,12 +70,7 @@ impl Model { assert!(app .mount( Id::Library, - Box::new(MusicLibrary::new( - tree, - None, - &config.style_color_symbol, - &config.keys - )), + Box::new(MusicLibrary::new(tree, None, config)), vec![] ) .is_ok()); @@ -114,11 +109,7 @@ impl Model { ) .is_ok()); assert!(app - .mount( - Id::Playlist, - Box::new(Playlist::new(&config.style_color_symbol, &config.keys)), - vec![] - ) + .mount(Id::Playlist, Box::new(Playlist::new(config)), vec![]) .is_ok()); assert!(app .mount(Id::Progress, Box::new(Progress::new(config)), vec![])