Skip to content

Commit

Permalink
minor fix
Browse files Browse the repository at this point in the history
  • Loading branch information
tramhao committed May 21, 2022
1 parent 185d450 commit 460b147
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 73 deletions.
57 changes: 29 additions & 28 deletions src/ui/components/music_library.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -20,37 +20,52 @@ pub struct MusicLibrary {
}

impl MusicLibrary {
pub fn new(
tree: &Tree,
initial_node: Option<String>,
color_mapping: &StyleColorSymbol,
keys: &Keys,
) -> Self {
pub fn new(tree: &Tree, initial_node: Option<String>, config: &Termusic) -> Self {
// Preserve initial node if exists
let initial_node = match initial_node {
Some(id) if tree.root().query(&id).is_some() => id,
_ => tree.root().id().to_string(),
};
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(),
}
}

Expand Down Expand Up @@ -274,20 +289,13 @@ 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) {
if Some(AttrValue::Flag(true)) == f {
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
Expand All @@ -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) {
Expand Down
71 changes: 37 additions & 34 deletions src/ui/components/playlist.rs
Original file line number Diff line number Diff line change
@@ -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},
};
Expand Down Expand Up @@ -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)
Expand All @@ -63,7 +77,7 @@ impl Playlist {
.add_col(TextSpan::from("Empty"))
.build(),
),
keys: keys.clone(),
keys: config.keys.clone(),
}
}
}
Expand Down Expand Up @@ -161,10 +175,10 @@ impl Component<Msg, NoUserEvent> 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;
}
}

Expand All @@ -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());
}
}
Expand Down Expand Up @@ -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(&current_node);
if !p.exists() {
Expand Down Expand Up @@ -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<()> {
Expand Down Expand Up @@ -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();
}
Expand Down
13 changes: 2 additions & 11 deletions src/ui/model/view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down Expand Up @@ -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![])
Expand Down

0 comments on commit 460b147

Please sign in to comment.