diff --git a/cursive-core/src/views/mod.rs b/cursive-core/src/views/mod.rs index 5ff2366..e257d24 100644 --- a/cursive-core/src/views/mod.rs +++ b/cursive-core/src/views/mod.rs @@ -93,43 +93,47 @@ mod slider_view; mod stack_view; mod text_area; mod text_view; +mod themed_view; mod tracked_view; -pub use self::boxed_view::BoxedView; -pub use self::button::Button; -pub use self::canvas::Canvas; -pub use self::checkbox::Checkbox; -pub use self::circular_focus::CircularFocus; -pub use self::debug_view::DebugView; -pub use self::dialog::{Dialog, DialogFocus}; -pub use self::dummy::DummyView; -pub use self::edit_view::EditView; -pub use self::enableable_view::EnableableView; -pub use self::fixed_layout::FixedLayout; -pub use self::hideable_view::HideableView; -pub use self::last_size_view::LastSizeView; -pub use self::layer::Layer; -pub use self::linear_layout::LinearLayout; -pub use self::list_view::{ListChild, ListView}; -pub use self::menu_popup::MenuPopup; -pub use self::menubar::Menubar; -pub use self::named_view::{NamedView, ViewRef}; -pub use self::on_event_view::OnEventView; -pub use self::on_layout_view::OnLayoutView; -pub use self::padded_view::PaddedView; -pub use self::panel::Panel; -pub use self::progress_bar::ProgressBar; -pub use self::radio::{RadioButton, RadioGroup}; -pub use self::resized_view::ResizedView; -pub use self::screens_view::ScreensView; -pub use self::scroll_view::ScrollView; -pub use self::select_view::SelectView; -pub use self::shadow_view::ShadowView; -pub use self::slider_view::SliderView; -pub use self::stack_view::{LayerPosition, StackView}; -pub use self::text_area::TextArea; -pub use self::text_view::{TextContent, TextContentRef, TextView}; -pub use self::tracked_view::TrackedView; +pub use self::{ + boxed_view::BoxedView, + button::Button, + canvas::Canvas, + checkbox::Checkbox, + circular_focus::CircularFocus, + debug_view::DebugView, + dialog::{Dialog, DialogFocus}, + dummy::DummyView, + edit_view::EditView, + enableable_view::EnableableView, + fixed_layout::FixedLayout, + hideable_view::HideableView, + last_size_view::LastSizeView, + layer::Layer, + linear_layout::LinearLayout, + list_view::{ListChild, ListView}, + menu_popup::MenuPopup, + menubar::Menubar, + named_view::{NamedView, ViewRef}, + on_event_view::OnEventView, + on_layout_view::OnLayoutView, + padded_view::PaddedView, + panel::Panel, + progress_bar::ProgressBar, + radio::{RadioButton, RadioGroup}, + resized_view::ResizedView, + screens_view::ScreensView, + scroll_view::ScrollView, + select_view::SelectView, + shadow_view::ShadowView, + slider_view::SliderView, + stack_view::{LayerPosition, StackView}, + text_area::TextArea, + text_view::{TextContent, TextContentRef, TextView}, + themed_view::ThemedView, + tracked_view::TrackedView, +}; /// Same as [`LastSizeView`](self::LastSizeView). #[deprecated(note = "`SizedView` is being renamed to `LastSizeView`")] diff --git a/cursive-core/src/views/themed_view.rs b/cursive-core/src/views/themed_view.rs new file mode 100644 index 0000000..9822f75 --- /dev/null +++ b/cursive-core/src/views/themed_view.rs @@ -0,0 +1,39 @@ +use crate::view::{View, ViewWrapper}; + +/// Applies a theme to the wrapped view. +pub struct ThemedView { + theme: crate::theme::Theme, + view: T, +} + +impl ThemedView { + /// Wrap the given view with a theme. + pub fn new(theme: crate::theme::Theme, view: T) -> Self { + ThemedView { theme, view } + } + + /// Retrieve the wrapped theme. + pub fn get_theme(&self) -> &crate::theme::Theme { + &self.theme + } + + /// Sets a new theme for the wrapped view. + pub fn set_theme(&mut self, theme: crate::theme::Theme) { + self.theme = theme; + } + + inner_getters!(self.view: T); +} + +impl ViewWrapper for ThemedView { + wrap_impl!(self.view: T); + + fn wrap_draw(&self, printer: &crate::Printer) { + printer.theme(&self.theme).with_style( + crate::theme::ColorStyle::primary(), + |printer| { + self.view.draw(printer); + }, + ); + } +} diff --git a/examples/src/bin/themed_view.rs b/examples/src/bin/themed_view.rs new file mode 100644 index 0000000..cccee1d --- /dev/null +++ b/examples/src/bin/themed_view.rs @@ -0,0 +1,34 @@ +use cursive::{self, theme, views, With}; + +fn main() { + let mut cursive = cursive::default(); + + cursive.add_layer( + views::Dialog::text("Open a themed dialog?") + .button("Open", show_dialog) + .button("Quit", |s| s.quit()), + ); + + cursive.run(); +} + +fn show_dialog(s: &mut cursive::Cursive) { + // Let's build a green theme + let theme = s.current_theme().clone().with(|theme| { + theme.palette[theme::PaletteColor::View] = + theme::Color::Dark(theme::BaseColor::Black); + theme.palette[theme::PaletteColor::Primary] = + theme::Color::Light(theme::BaseColor::Green); + theme.palette[theme::PaletteColor::TitlePrimary] = + theme::Color::Light(theme::BaseColor::Green); + theme.palette[theme::PaletteColor::Highlight] = + theme::Color::Dark(theme::BaseColor::Green); + }); + + s.add_layer(views::ThemedView::new( + theme, + views::Layer::new( + views::Dialog::info("Colors!").title("Themed Dialog"), + ), + )); +}