Skip to content

Commit

Permalink
Update examples to Theme breaking changes
Browse files Browse the repository at this point in the history
  • Loading branch information
mitchmindtree committed Nov 15, 2015
1 parent 527093d commit 861726a
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 28 deletions.
4 changes: 2 additions & 2 deletions examples/canvas.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down
70 changes: 45 additions & 25 deletions examples/custom_widget.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -42,6 +43,8 @@ mod circular_button {
Theme,
UpdateArgs,
Widget,
WidgetKind,
Ui,
};


Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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<C: CharacterCache>(&self, theme: &Theme, _: &GlyphCache<C>) -> 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<C: CharacterCache>(&self, ui: &Ui<C>) -> Dimension {
// If no width was given via the `Sizeable` (a trait implemented for all widgets)
// methods, some default width must be chosen.
//
Expand All @@ -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<C: CharacterCache>(&self, ui: &Ui<C>) -> 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
Expand Down Expand Up @@ -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::<Self>(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::<Self>(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::<Self>(KIND).map(|default| {
default.style.maybe_label_font_size.unwrap_or(theme.font_size_medium)
})).unwrap_or(theme.font_size_medium)
}
Expand Down
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,13 +83,15 @@ 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};
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;


Expand Down
1 change: 0 additions & 1 deletion src/widget/button.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down

0 comments on commit 861726a

Please sign in to comment.