mirror of
https://github.com/FliegendeWurst/cursive.git
synced 2024-11-23 17:35:00 +00:00
Add Cursive::set_theme
method
Allow setting a theme without having to load a toml file.
This commit is contained in:
parent
1052c0b74c
commit
9db3c87851
11
src/lib.rs
11
src/lib.rs
@ -148,6 +148,7 @@ impl Cursive {
|
|||||||
B::init();
|
B::init();
|
||||||
|
|
||||||
let theme = theme::load_default();
|
let theme = theme::load_default();
|
||||||
|
theme.activate();
|
||||||
// let theme = theme::load_theme("assets/style.toml").unwrap();
|
// let theme = theme::load_theme("assets/style.toml").unwrap();
|
||||||
|
|
||||||
let (tx, rx) = mpsc::channel();
|
let (tx, rx) = mpsc::channel();
|
||||||
@ -249,12 +250,18 @@ impl Cursive {
|
|||||||
&self.theme
|
&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.
|
/// Loads a theme from the given file.
|
||||||
///
|
///
|
||||||
/// `filename` must point to a valid toml file.
|
/// `filename` must point to a valid toml file.
|
||||||
pub fn load_theme_file<P: AsRef<Path>>(&mut self, filename: P)
|
pub fn load_theme_file<P: AsRef<Path>>(&mut self, filename: P)
|
||||||
-> Result<(), theme::Error> {
|
-> Result<(), theme::Error> {
|
||||||
self.theme = try!(theme::load_theme_file(filename));
|
self.set_theme(try!(theme::load_theme_file(filename)));
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -262,7 +269,7 @@ impl Cursive {
|
|||||||
///
|
///
|
||||||
/// Content must be valid toml.
|
/// Content must be valid toml.
|
||||||
pub fn load_theme(&mut self, content: &str) -> Result<(), theme::Error> {
|
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(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
13
src/theme.rs
13
src/theme.rs
@ -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
|
// Initialize each color with the backend
|
||||||
B::init_color_style(ColorStyle::Background,
|
B::init_color_style(ColorStyle::Background,
|
||||||
&self.colors.view,
|
&self.colors.view,
|
||||||
@ -314,8 +319,8 @@ pub struct Palette {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl Palette {
|
impl Palette {
|
||||||
|
/// Fills `self` with the colors from the given `table`.
|
||||||
fn load(&mut self, table: &toml::Table) {
|
fn load(&mut self, table: &toml::Table) {
|
||||||
|
|
||||||
load_color(&mut self.background, table.get("background"));
|
load_color(&mut self.background, table.get("background"));
|
||||||
load_color(&mut self.shadow, table.get("shadow"));
|
load_color(&mut self.shadow, table.get("shadow"));
|
||||||
load_color(&mut self.view, table.get("view"));
|
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 {
|
fn load_color(target: &mut Color, value: Option<&toml::Value>) -> bool {
|
||||||
if let Some(value) = value {
|
if let Some(value) = value {
|
||||||
match *value {
|
match *value {
|
||||||
@ -478,7 +484,6 @@ pub fn load_theme(content: &str) -> Result<Theme, Error> {
|
|||||||
|
|
||||||
let mut theme = Theme::default();
|
let mut theme = Theme::default();
|
||||||
theme.load(&table);
|
theme.load(&table);
|
||||||
theme.activate();
|
|
||||||
|
|
||||||
Ok(theme)
|
Ok(theme)
|
||||||
}
|
}
|
||||||
@ -486,7 +491,7 @@ pub fn load_theme(content: &str) -> Result<Theme, Error> {
|
|||||||
/// Loads the default theme, and returns its representation.
|
/// Loads the default theme, and returns its representation.
|
||||||
pub fn load_default() -> Theme {
|
pub fn load_default() -> Theme {
|
||||||
let theme = Theme::default();
|
let theme = Theme::default();
|
||||||
theme.activate();
|
|
||||||
theme
|
theme
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -78,11 +78,16 @@ impl Dialog {
|
|||||||
self.content = Box::new(view);
|
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.
|
/// Convenient method to create an infobox.
|
||||||
///
|
///
|
||||||
/// It will contain the given text and a `Ok` dismiss button.
|
/// It will contain the given text and a `Ok` dismiss button.
|
||||||
pub fn info<S: Into<String>>(text: S) -> Self {
|
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.
|
/// Adds a button to the dialog with the given label and callback.
|
||||||
|
Loading…
Reference in New Issue
Block a user