Add Cursive::set_theme method

Allow setting a theme without having to load a toml file.
This commit is contained in:
Alexandre Bury 2016-08-04 00:11:16 -07:00
parent 1052c0b74c
commit 9db3c87851
3 changed files with 24 additions and 7 deletions

View File

@ -148,6 +148,7 @@ impl Cursive {
B::init();
let theme = theme::load_default();
theme.activate();
// let theme = theme::load_theme("assets/style.toml").unwrap();
let (tx, rx) = mpsc::channel();
@ -249,12 +250,18 @@ impl Cursive {
&self.theme
}
/// Sets the current theme.
pub fn set_theme(&mut self, theme: theme::Theme) {
self.theme = theme;
self.theme.activate();
}
/// Loads a theme from the given file.
///
/// `filename` must point to a valid toml file.
pub fn load_theme_file<P: AsRef<Path>>(&mut self, filename: P)
-> Result<(), theme::Error> {
self.theme = try!(theme::load_theme_file(filename));
self.set_theme(try!(theme::load_theme_file(filename)));
Ok(())
}
@ -262,7 +269,7 @@ impl Cursive {
///
/// Content must be valid toml.
pub fn load_theme(&mut self, content: &str) -> Result<(), theme::Error> {
self.theme = try!(theme::load_theme(content));
self.set_theme(try!(theme::load_theme(content)));
Ok(())
}

View File

@ -227,7 +227,12 @@ impl Theme {
}
}
fn activate(&self) {
/// Sets a theme as active.
///
/// **Don't use this directly.** Uses [`Cursive::set_theme`] instead.
///
/// [`Cursive::set_theme`]: ../struct.Cursive.html#method.set_theme
pub fn activate(&self) {
// Initialize each color with the backend
B::init_color_style(ColorStyle::Background,
&self.colors.view,
@ -314,8 +319,8 @@ pub struct Palette {
}
impl Palette {
/// Fills `self` with the colors from the given `table`.
fn load(&mut self, table: &toml::Table) {
load_color(&mut self.background, table.get("background"));
load_color(&mut self.shadow, table.get("shadow"));
load_color(&mut self.view, table.get("view"));
@ -330,6 +335,7 @@ impl Palette {
}
}
/// Parses `value` and fills `target` if it's a valid color.
fn load_color(target: &mut Color, value: Option<&toml::Value>) -> bool {
if let Some(value) = value {
match *value {
@ -478,7 +484,6 @@ pub fn load_theme(content: &str) -> Result<Theme, Error> {
let mut theme = Theme::default();
theme.load(&table);
theme.activate();
Ok(theme)
}
@ -486,7 +491,7 @@ pub fn load_theme(content: &str) -> Result<Theme, Error> {
/// Loads the default theme, and returns its representation.
pub fn load_default() -> Theme {
let theme = Theme::default();
theme.activate();
theme
}

View File

@ -78,11 +78,16 @@ impl Dialog {
self.content = Box::new(view);
}
/// Convenient method to create a dialog with a simple text content.
pub fn text<S: Into<String>>(text: S) -> Self {
Self::new(TextView::new(text))
}
/// Convenient method to create an infobox.
///
/// It will contain the given text and a `Ok` dismiss button.
pub fn info<S: Into<String>>(text: S) -> Self {
Self::new(TextView::new(text)).dismiss_button("Ok")
Dialog::text(text).dismiss_button("Ok")
}
/// Adds a button to the dialog with the given label and callback.