From 861726a4c7b03b3af918047470fc9ed83b76c55c Mon Sep 17 00:00:00 2001 From: mitchmindtree Date: Sun, 15 Nov 2015 17:41:34 +1100 Subject: [PATCH] Update examples to Theme breaking changes --- examples/canvas.rs | 4 +-- examples/custom_widget.rs | 70 +++++++++++++++++++++++++-------------- src/lib.rs | 2 ++ src/widget/button.rs | 1 - 4 files changed, 49 insertions(+), 28 deletions(-) diff --git a/examples/canvas.rs b/examples/canvas.rs index 98e784d1f..1eef565b6 100644 --- a/examples/canvas.rs +++ b/examples/canvas.rs @@ -76,7 +76,7 @@ fn draw_ui(ui: &mut Ui, c: Context, g: &mut G2d) { Tabs::new(&[(TAB_FOO, "FOO"), (TAB_BAR, "BAR"), (TAB_BAZ, "BAZ")]) - .dim(ui.widget_size(MIDDLE_COLUMN)) + .dim_of(MIDDLE_COLUMN) .color(blue()) .label_color(white()) .middle_of(MIDDLE_COLUMN) @@ -99,7 +99,7 @@ fn draw_ui(ui: &mut Ui, c: Context, g: &mut G2d) { Label::new("Bar!").color(white()).font_size(36).middle_of(TAB_BAR).set(BAR_LABEL, ui); Label::new("BAZ!").color(white()).font_size(36).middle_of(TAB_BAZ).set(BAZ_LABEL, ui); - let footer_dim = ui.widget_size(FOOTER); + let footer_dim = ui.dim_of(FOOTER).unwrap(); WidgetMatrix::new(COLS, ROWS) .dimensions(footer_dim[0], footer_dim[1] * 2.0) .mid_top_of(FOOTER) diff --git a/examples/custom_widget.rs b/examples/custom_widget.rs index 4d9554461..29d49b0ea 100644 --- a/examples/custom_widget.rs +++ b/examples/custom_widget.rs @@ -26,14 +26,15 @@ extern crate vecmath; /// The module in which we'll implement our own custom circular button. mod circular_button { use conrod::{ + default_dimension, CharacterCache, Color, Colorable, CommonBuilder, + Dimension, DrawArgs, Element, FontSize, - GlyphCache, Labelable, Dimensions, Mouse, @@ -42,6 +43,8 @@ mod circular_button { Theme, UpdateArgs, Widget, + WidgetKind, + Ui, }; @@ -85,6 +88,9 @@ mod circular_button { interaction: Interaction, } + /// A `&'static str` that can be used to uniquely identify our widget type. + pub const KIND: WidgetKind = "CircularButton"; + impl State { /// Alter the widget color depending on the state. fn color(&self, color: Color) -> Color { @@ -195,19 +201,36 @@ mod circular_button { /// The Style struct that we defined above. type Style = Style; - fn common(&self) -> &CommonBuilder { &self.common } - fn common_mut(&mut self) -> &mut CommonBuilder { &mut self.common } - fn unique_kind(&self) -> &'static str { "CircularButton" } + fn common(&self) -> &CommonBuilder { + &self.common + } + + fn common_mut(&mut self) -> &mut CommonBuilder { + &mut self.common + } + + fn unique_kind(&self) -> &'static str { + KIND + } + fn init_state(&self) -> State { - State { maybe_label: None, interaction: Interaction::Normal } + State { + maybe_label: None, + interaction: Interaction::Normal, + } } - fn style(&self) -> Style { self.style.clone() } - /// Default width of the widget. This method is optional. The Widget trait - /// provides a default implementation that always returns zero. - fn default_width(&self, theme: &Theme, _: &GlyphCache) -> Scalar { - const DEFAULT_WIDTH: Scalar = 64.0; + fn style(&self) -> Style { + self.style.clone() + } + /// Default width of the widget. + /// + /// This method is optional. + /// + /// The default implementation is the same as below, but unwraps to an absolute scalar of + /// `0.0` instead of `64.0`. + fn default_x_dimension(&self, ui: &Ui) -> Dimension { // If no width was given via the `Sizeable` (a trait implemented for all widgets) // methods, some default width must be chosen. // @@ -216,20 +239,17 @@ mod circular_button { // // Most commonly, defaults are to be retrieved from the `Theme`, however in some cases // some other logic may need to be considered. - theme.maybe_button.as_ref().map(|default| { - default.common.maybe_width.unwrap_or(DEFAULT_WIDTH) - }).unwrap_or(DEFAULT_WIDTH) + default_dimension(self, ui).unwrap_or(Dimension::Absolute(64.0)) } - /// Default width of the widget. This method is optional. The Widget trait - /// provides a default implementation that always returns zero. - fn default_height(&self, theme: &Theme) -> Scalar { - const DEFAULT_HEIGHT: Scalar = 64.0; - - // See default_width for comments on this logic. - theme.maybe_button.as_ref().map(|default| { - default.common.maybe_height.unwrap_or(DEFAULT_HEIGHT) - }).unwrap_or(DEFAULT_HEIGHT) + /// Default height of the widget. + /// + /// This method is optional. + /// + /// The default implementation is the same as below, but unwraps to an absolute scalar of + /// `0.0` instead of `64.0`. + fn default_y_dimension(&self, ui: &Ui) -> Dimension { + default_dimension(self, ui).unwrap_or(Dimension::Absolute(64.0)) } /// Update the state of the button. The state may or may not have changed since @@ -385,21 +405,21 @@ mod circular_button { /// Get the Color for an Element. pub fn color(&self, theme: &Theme) -> Color { - self.maybe_color.or(theme.maybe_button.as_ref().map(|default| { + self.maybe_color.or(theme.widget_style::(KIND).map(|default| { default.style.maybe_color.unwrap_or(theme.shape_color) })).unwrap_or(theme.shape_color) } /// Get the label Color for an Element. pub fn label_color(&self, theme: &Theme) -> Color { - self.maybe_label_color.or(theme.maybe_button.as_ref().map(|default| { + self.maybe_label_color.or(theme.widget_style::(KIND).map(|default| { default.style.maybe_label_color.unwrap_or(theme.label_color) })).unwrap_or(theme.label_color) } /// Get the label font size for an Element. pub fn label_font_size(&self, theme: &Theme) -> FontSize { - self.maybe_label_font_size.or(theme.maybe_button.as_ref().map(|default| { + self.maybe_label_font_size.or(theme.widget_style::(KIND).map(|default| { default.style.maybe_label_font_size.unwrap_or(theme.font_size_medium) })).unwrap_or(theme.font_size_medium) } diff --git a/src/lib.rs b/src/lib.rs index 99473bf2e..b413e1c10 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -83,6 +83,7 @@ pub use position::{Corner, Depth, Direction, Dimension, Dimensions, Horizontal, pub use position::Matrix as PositionMatrix; pub use theme::{Align, Theme}; pub use ui::{GlyphCache, Ui, UserInput}; +pub use widget::default_dimension; pub use widget::{drag, scroll}; pub use widget::{CommonBuilder, CommonState, DrawArgs, Floating, MaybeParent, UiCell, UpdateArgs, Widget}; @@ -90,6 +91,7 @@ pub use widget::{KidArea, KidAreaArgs}; pub use widget::CommonState as WidgetCommonState; pub use widget::Id as WidgetId; pub use widget::Index as WidgetIndex; +pub use widget::Kind as WidgetKind; pub use widget::State as WidgetState; diff --git a/src/widget/button.rs b/src/widget/button.rs index b84bb8387..3e904fbf9 100644 --- a/src/widget/button.rs +++ b/src/widget/button.rs @@ -1,7 +1,6 @@ use {Label, NodeIndex, FramedRectangle, Ui}; use color::{Color, Colorable}; -use elmesque::Element; use frame::Frameable; use graphics::character::CharacterCache; use label::{FontSize, Labelable};