refactor: differentiate between screens and views

This commit is contained in:
Henrik Friedrichsen
2021-01-09 23:23:57 +01:00
parent 18dc6c6bf8
commit 254c7a3856
2 changed files with 22 additions and 17 deletions

View File

@@ -264,12 +264,12 @@ fn main() {
); );
let mut layout = ui::layout::Layout::new(status, &event_manager, theme) let mut layout = ui::layout::Layout::new(status, &event_manager, theme)
.view("search", search.with_name("search"), "Search") .screen("search", search.with_name("search"), "Search")
.view("library", libraryview.with_name("library"), "Library") .screen("library", libraryview.with_name("library"), "Library")
.view("queue", queueview, "Queue"); .screen("queue", queueview, "Queue");
// initial view is library // initial screen is library
layout.set_view("library"); layout.set_screen("library");
cursive.add_global_callback(':', move |s| { cursive.add_global_callback(':', move |s| {
if s.find_name::<ContextMenu>("contextmenu").is_none() { if s.find_name::<ContextMenu>("contextmenu").is_none() {

View File

@@ -23,7 +23,7 @@ struct Screen {
} }
pub struct Layout { pub struct Layout {
views: HashMap<String, Screen>, screens: HashMap<String, Screen>,
stack: Vec<Screen>, stack: Vec<Screen>,
statusbar: Box<dyn View>, statusbar: Box<dyn View>,
focus: Option<String>, focus: Option<String>,
@@ -45,7 +45,7 @@ impl Layout {
); );
Layout { Layout {
views: HashMap::new(), screens: HashMap::new(),
stack: Vec::new(), stack: Vec::new(),
statusbar: status.as_boxed_view(), statusbar: status.as_boxed_view(),
focus: None, focus: None,
@@ -74,22 +74,27 @@ impl Layout {
} }
} }
pub fn add_view<S: Into<String>, T: IntoBoxedViewExt>(&mut self, id: S, view: T, title: S) { pub fn add_screen<S: Into<String>, T: IntoBoxedViewExt>(&mut self, id: S, view: T, title: S) {
let s = id.into(); let s = id.into();
let screen = Screen { let screen = Screen {
title: title.into(), title: title.into(),
view: view.as_boxed_view_ext(), view: view.as_boxed_view_ext(),
}; };
self.views.insert(s.clone(), screen); self.screens.insert(s.clone(), screen);
self.focus = Some(s); self.focus = Some(s);
} }
pub fn view<S: Into<String>, T: IntoBoxedViewExt>(mut self, id: S, view: T, title: S) -> Self { pub fn screen<S: Into<String>, T: IntoBoxedViewExt>(
(&mut self).add_view(id, view, title); mut self,
id: S,
view: T,
title: S,
) -> Self {
(&mut self).add_screen(id, view, title);
self self
} }
pub fn set_view<S: Into<String>>(&mut self, id: S) { pub fn set_screen<S: Into<String>>(&mut self, id: S) {
let s = id.into(); let s = id.into();
self.focus = Some(s); self.focus = Some(s);
self.cmdline_focus = false; self.cmdline_focus = false;
@@ -138,7 +143,7 @@ impl Layout {
} }
if let Some(id) = self.focus.as_ref() { if let Some(id) = self.focus.as_ref() {
self.views.get(id) self.screens.get(id)
} else { } else {
None None
} }
@@ -150,7 +155,7 @@ impl Layout {
} }
if let Some(id) = self.focus.as_ref() { if let Some(id) = self.focus.as_ref() {
self.views.get_mut(id) self.screens.get_mut(id)
} else { } else {
None None
} }
@@ -293,9 +298,9 @@ impl ViewExt for Layout {
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> {
match cmd { match cmd {
Command::Focus(view) => { Command::Focus(view) => {
if self.views.keys().any(|k| k == view) { if self.screens.keys().any(|k| k == view) {
self.set_view(view.clone()); self.set_screen(view.clone());
let screen = self.views.get_mut(view).unwrap(); let screen = self.screens.get_mut(view).unwrap();
screen.view.on_command(s, cmd)?; screen.view.on_command(s, cmd)?;
} }