Use ViewExt.title() in TabView

Instead of storing a separate copy of the view title.

Additionally, rename `ViewExt.set_title()` to `ViewExt.with_title()` as it
consumes `self` and returns ownership.
This commit is contained in:
Henrik Friedrichsen
2022-08-19 20:42:46 +02:00
parent 5db79d5b2e
commit 38dfba1db5
11 changed files with 39 additions and 40 deletions

View File

@@ -277,7 +277,7 @@ impl ListItem for Album {
queue.clone(), queue.clone(),
library.clone(), library.clone(),
) )
.set_title(format!("Similar to Album \"{}\"", self.title)) .with_title(&format!("Similar to Album \"{}\"", self.title))
.into_boxed_view_ext() .into_boxed_view_ext()
}) })
} }

View File

@@ -191,7 +191,7 @@ impl ListItem for Artist {
queue.clone(), queue.clone(),
library.clone(), library.clone(),
) )
.set_title(format!("Similar to Artist \"{}\"", self.name,)) .with_title(&format!("Similar to Artist \"{}\"", self.name))
.into_boxed_view_ext() .into_boxed_view_ext()
}) })
} }

View File

@@ -312,7 +312,7 @@ impl ListItem for Playlist {
queue.clone(), queue.clone(),
library.clone(), library.clone(),
) )
.set_title(format!("Similar to Tracks in \"{}\"", self.name,)) .with_title(&format!("Similar to Tracks in \"{}\"", self.name))
.into_boxed_view_ext() .into_boxed_view_ext()
}) })
} }

View File

@@ -295,7 +295,7 @@ impl ListItem for Track {
queue.clone(), queue.clone(),
library.clone(), library.clone(),
) )
.set_title(format!( .with_title(&format!(
"Similar to \"{} - {}\"", "Similar to \"{} - {}\"",
self.artists.join(", "), self.artists.join(", "),
self.title self.title

View File

@@ -40,17 +40,16 @@ impl AlbumView {
let tabs = TabView::new() let tabs = TabView::new()
.tab( .tab(
"tracks", "tracks",
"Tracks",
ListView::new( ListView::new(
Arc::new(RwLock::new(tracks)), Arc::new(RwLock::new(tracks)),
queue.clone(), queue.clone(),
library.clone(), library.clone(),
), )
.with_title("Tracks"),
) )
.tab( .tab(
"artists", "artists",
"Artists", ListView::new(Arc::new(RwLock::new(artists)), queue, library).with_title("Artists"),
ListView::new(Arc::new(RwLock::new(artists)), queue, library),
); );
Self { album, tabs } Self { album, tabs }

View File

@@ -68,28 +68,26 @@ impl ArtistView {
tabs.add_tab( tabs.add_tab(
"tracks", "tracks",
"Saved Tracks",
ListView::new( ListView::new(
Arc::new(RwLock::new(tracks)), Arc::new(RwLock::new(tracks)),
queue.clone(), queue.clone(),
library.clone(), library.clone(),
), )
.with_title("Saved Tracks"),
); );
} }
tabs.add_tab( tabs.add_tab(
"top_tracks", "top_tracks",
"Top 10", ListView::new(top_tracks, queue.clone(), library.clone()).with_title("Top 10"),
ListView::new(top_tracks, queue.clone(), library.clone()),
); );
tabs.add_tab("albums", "Albums", albums_view); tabs.add_tab("albums", albums_view.with_title("Albums"));
tabs.add_tab("singles", "Singles", singles_view); tabs.add_tab("singles", singles_view.with_title("Singles"));
tabs.add_tab( tabs.add_tab(
"related", "related",
"Related Artists", ListView::new(related, queue, library).with_title("Related Artists"),
ListView::new(related, queue, library),
); );
Self { Self {

View File

@@ -33,28 +33,27 @@ impl LibraryView {
match tab { match tab {
LibraryTab::Tracks => tabview.add_tab( LibraryTab::Tracks => tabview.add_tab(
"tracks", "tracks",
"Tracks", ListView::new(library.tracks.clone(), queue.clone(), library.clone())
ListView::new(library.tracks.clone(), queue.clone(), library.clone()), .with_title("Tracks"),
), ),
LibraryTab::Albums => tabview.add_tab( LibraryTab::Albums => tabview.add_tab(
"albums", "albums",
"Albums", ListView::new(library.albums.clone(), queue.clone(), library.clone())
ListView::new(library.albums.clone(), queue.clone(), library.clone()), .with_title("Albums"),
), ),
LibraryTab::Artists => tabview.add_tab( LibraryTab::Artists => tabview.add_tab(
"artists", "artists",
"Artists", ListView::new(library.artists.clone(), queue.clone(), library.clone())
ListView::new(library.artists.clone(), queue.clone(), library.clone()), .with_title("Artists"),
), ),
LibraryTab::Playlists => tabview.add_tab( LibraryTab::Playlists => tabview.add_tab(
"playlists", "playlists",
"Playlists",
PlaylistsView::new(queue.clone(), library.clone()), PlaylistsView::new(queue.clone(), library.clone()),
), ),
LibraryTab::Podcasts => tabview.add_tab( LibraryTab::Podcasts => tabview.add_tab(
"podcasts", "podcasts",
"Podcasts", ListView::new(library.shows.clone(), queue.clone(), library.clone())
ListView::new(library.shows.clone(), queue.clone(), library.clone()), .with_title("Podcasts"),
), ),
} }
} }

View File

@@ -75,8 +75,8 @@ impl<I: ListItem> ListView<I> {
} }
} }
pub fn set_title(mut self, title: String) -> Self { pub fn with_title(mut self, title: &str) -> Self {
self.title = title; self.title = title.to_string();
self self
} }

View File

@@ -53,6 +53,10 @@ impl ViewWrapper for PlaylistsView {
} }
impl ViewExt for PlaylistsView { impl ViewExt for PlaylistsView {
fn title(&self) -> String {
"Playlists".to_string()
}
fn on_command(&mut self, s: &mut Cursive, cmd: &Command) -> Result<CommandResult, String> { fn on_command(&mut self, s: &mut Cursive, cmd: &Command) -> Result<CommandResult, String> {
if let Command::Delete = cmd { if let Command::Delete = cmd {
if let Some(dialog) = self.delete_dialog() { if let Some(dialog) = self.delete_dialog() {

View File

@@ -72,12 +72,12 @@ impl SearchResultsView {
let pagination_episodes = list_episodes.get_pagination().clone(); let pagination_episodes = list_episodes.get_pagination().clone();
let tabs = TabView::new() let tabs = TabView::new()
.tab("tracks", "Tracks", list_tracks) .tab("tracks", list_tracks.with_title("Tracks"))
.tab("albums", "Albums", list_albums) .tab("albums", list_albums.with_title("Albums"))
.tab("artists", "Artists", list_artists) .tab("artists", list_artists.with_title("Artists"))
.tab("playlists", "Playlists", list_playlists) .tab("playlists", list_playlists.with_title("Playlists"))
.tab("shows", "Podcasts", list_shows) .tab("shows", list_shows.with_title("Podcasts"))
.tab("episodes", "Podcast Episodes", list_episodes); .tab("episodes", list_episodes.with_title("Podcast Episodes"));
let mut view = SearchResultsView { let mut view = SearchResultsView {
search_term, search_term,

View File

@@ -13,7 +13,6 @@ use crate::commands::CommandResult;
use crate::traits::{IntoBoxedViewExt, ViewExt}; use crate::traits::{IntoBoxedViewExt, ViewExt};
pub struct Tab { pub struct Tab {
title: String,
view: Box<dyn ViewExt>, view: Box<dyn ViewExt>,
} }
@@ -34,17 +33,16 @@ impl TabView {
} }
} }
pub fn add_tab<S: Into<String>, V: IntoBoxedViewExt>(&mut self, id: S, title: S, view: V) { pub fn add_tab<S: Into<String>, V: IntoBoxedViewExt>(&mut self, id: S, view: V) {
let tab = Tab { let tab = Tab {
title: title.into(),
view: view.into_boxed_view_ext(), view: view.into_boxed_view_ext(),
}; };
self.tabs.push(tab); self.tabs.push(tab);
self.ids.insert(id.into(), self.tabs.len() - 1); self.ids.insert(id.into(), self.tabs.len() - 1);
} }
pub fn tab<S: Into<String>, V: IntoBoxedViewExt>(mut self, id: S, title: S, view: V) -> Self { pub fn tab<S: Into<String>, V: IntoBoxedViewExt>(mut self, id: S, view: V) -> Self {
self.add_tab(id, title, view); self.add_tab(id, view);
self self
} }
@@ -81,11 +79,12 @@ impl View for TabView {
width += printer.size.x % self.tabs.len(); width += printer.size.x % self.tabs.len();
} }
let offset = HAlign::Center.get_offset(tab.title.width(), width); let title = tab.view.title();
let offset = HAlign::Center.get_offset(title.width(), width);
printer.with_color(style, |printer| { printer.with_color(style, |printer| {
printer.print_hline((i * tabwidth, 0), width, " "); printer.print_hline((i * tabwidth, 0), width, " ");
printer.print((i * tabwidth + offset, 0), &tab.title); printer.print((i * tabwidth + offset, 0), &title);
}); });
} }