conrod_core/widget/primitive/shape/
rectangle.rs1use super::Style;
7use widget;
8use widget::triangles::Triangle;
9use {Color, Colorable, Dimensions, Point, Rect, Sizeable, Widget};
10
11#[derive(Copy, Clone, Debug, WidgetCommon_)]
13pub struct Rectangle {
14 #[conrod(common_builder)]
16 pub common: widget::CommonBuilder,
17 pub style: Style,
19}
20
21#[derive(Copy, Clone, Debug, PartialEq)]
23pub struct State {
24 kind: Kind,
25}
26
27#[derive(Copy, Clone, Debug, PartialEq)]
29pub enum Kind {
30 Outline,
32 Fill,
34}
35
36impl Rectangle {
37 pub fn styled(dim: Dimensions, style: Style) -> Self {
39 Rectangle {
40 common: widget::CommonBuilder::default(),
41 style: style,
42 }
43 .wh(dim)
44 }
45
46 pub fn fill(dim: Dimensions) -> Self {
48 Rectangle::styled(dim, Style::fill())
49 }
50
51 pub fn fill_with(dim: Dimensions, color: Color) -> Self {
53 Rectangle::styled(dim, Style::fill_with(color))
54 }
55
56 pub fn outline(dim: Dimensions) -> Self {
58 Rectangle::styled(dim, Style::outline())
59 }
60
61 pub fn outline_styled(dim: Dimensions, line_style: widget::line::Style) -> Self {
63 Rectangle::styled(dim, Style::outline_styled(line_style))
64 }
65}
66
67impl Widget for Rectangle {
68 type State = State;
69 type Style = Style;
70 type Event = ();
71
72 fn init_state(&self, _: widget::id::Generator) -> Self::State {
73 State { kind: Kind::Fill }
74 }
75
76 fn style(&self) -> Self::Style {
77 self.style.clone()
78 }
79
80 fn update(self, args: widget::UpdateArgs<Self>) -> Self::Event {
82 let widget::UpdateArgs { state, style, .. } = args;
83
84 let kind = match *style {
85 Style::Fill(_) => Kind::Fill,
86 Style::Outline(_) => Kind::Outline,
87 };
88
89 if state.kind != kind {
90 state.update(|state| state.kind = kind);
91 }
92 }
93}
94
95impl Colorable for Rectangle {
96 fn color(mut self, color: Color) -> Self {
97 self.style.set_color(color);
98 self
99 }
100}
101
102pub fn triangles(rect: Rect) -> (Triangle<Point>, Triangle<Point>) {
104 let (l, r, b, t) = rect.l_r_b_t();
105 let quad = [[l, t], [r, t], [r, b], [l, b]];
106 widget::triangles::from_quad(quad)
107}