Add ThemedView

This commit is contained in:
Alexandre Bury 2021-02-10 09:31:12 -08:00
parent e2f5806deb
commit 8e34bc132a
3 changed files with 112 additions and 35 deletions

View File

@ -93,43 +93,47 @@ mod slider_view;
mod stack_view; mod stack_view;
mod text_area; mod text_area;
mod text_view; mod text_view;
mod themed_view;
mod tracked_view; mod tracked_view;
pub use self::boxed_view::BoxedView; pub use self::{
pub use self::button::Button; boxed_view::BoxedView,
pub use self::canvas::Canvas; button::Button,
pub use self::checkbox::Checkbox; canvas::Canvas,
pub use self::circular_focus::CircularFocus; checkbox::Checkbox,
pub use self::debug_view::DebugView; circular_focus::CircularFocus,
pub use self::dialog::{Dialog, DialogFocus}; debug_view::DebugView,
pub use self::dummy::DummyView; dialog::{Dialog, DialogFocus},
pub use self::edit_view::EditView; dummy::DummyView,
pub use self::enableable_view::EnableableView; edit_view::EditView,
pub use self::fixed_layout::FixedLayout; enableable_view::EnableableView,
pub use self::hideable_view::HideableView; fixed_layout::FixedLayout,
pub use self::last_size_view::LastSizeView; hideable_view::HideableView,
pub use self::layer::Layer; last_size_view::LastSizeView,
pub use self::linear_layout::LinearLayout; layer::Layer,
pub use self::list_view::{ListChild, ListView}; linear_layout::LinearLayout,
pub use self::menu_popup::MenuPopup; list_view::{ListChild, ListView},
pub use self::menubar::Menubar; menu_popup::MenuPopup,
pub use self::named_view::{NamedView, ViewRef}; menubar::Menubar,
pub use self::on_event_view::OnEventView; named_view::{NamedView, ViewRef},
pub use self::on_layout_view::OnLayoutView; on_event_view::OnEventView,
pub use self::padded_view::PaddedView; on_layout_view::OnLayoutView,
pub use self::panel::Panel; padded_view::PaddedView,
pub use self::progress_bar::ProgressBar; panel::Panel,
pub use self::radio::{RadioButton, RadioGroup}; progress_bar::ProgressBar,
pub use self::resized_view::ResizedView; radio::{RadioButton, RadioGroup},
pub use self::screens_view::ScreensView; resized_view::ResizedView,
pub use self::scroll_view::ScrollView; screens_view::ScreensView,
pub use self::select_view::SelectView; scroll_view::ScrollView,
pub use self::shadow_view::ShadowView; select_view::SelectView,
pub use self::slider_view::SliderView; shadow_view::ShadowView,
pub use self::stack_view::{LayerPosition, StackView}; slider_view::SliderView,
pub use self::text_area::TextArea; stack_view::{LayerPosition, StackView},
pub use self::text_view::{TextContent, TextContentRef, TextView}; text_area::TextArea,
pub use self::tracked_view::TrackedView; text_view::{TextContent, TextContentRef, TextView},
themed_view::ThemedView,
tracked_view::TrackedView,
};
/// Same as [`LastSizeView`](self::LastSizeView). /// Same as [`LastSizeView`](self::LastSizeView).
#[deprecated(note = "`SizedView` is being renamed to `LastSizeView`")] #[deprecated(note = "`SizedView` is being renamed to `LastSizeView`")]

View File

@ -0,0 +1,39 @@
use crate::view::{View, ViewWrapper};
/// Applies a theme to the wrapped view.
pub struct ThemedView<T> {
theme: crate::theme::Theme,
view: T,
}
impl<T> ThemedView<T> {
/// 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<T: View> ViewWrapper for ThemedView<T> {
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);
},
);
}
}

View File

@ -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"),
),
));
}