Files
aligned
app
arrayvec
as_slice
bare_metal
byteorder
cfg_if
cortex_m
cortex_m_rt
cstr_core
cty
druid
druid_shell
embedded_graphics
embedded_hal
generic_array
hash32
heapless
introsort
kurbo
libchip8
libm
log
memchr
mynewt
nb
num_traits
piet
piet_common
piet_embedded_graphics
r0
st7735_lcd
stable_deref_trait
typenum
unicode_segmentation
vcell
void
volatile_register
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
// Copyright 2018 The xi-editor Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//! A button widget.

use crate::{
    BaseState, BoxConstraints, Data, Env, Event, EventCtx, LayoutCtx, PaintCtx, Size, UpdateCtx,
    WidgetId, Widget,
};

use crate::kurbo::RoundedRect;
use crate::piet::{UnitPoint}; ////
////use crate::piet::{LinearGradient, UnitPoint};

////use crate::theme;
use crate::widget::{/* Align, */ Label, LabelText, /* SizedBox, */ WidgetType}; ////
use crate::{Point, RenderContext, WindowBox}; ////

/// A button with a text label.
#[derive(Clone)] ////
pub struct Button<T: Data + 'static + Default> { ////
////pub struct Button<T> {
    id: WidgetId, //// Unique Widget ID
    label: Label<T>,
    /////// A closure that will be invoked when the button is clicked.
    action: fn(&mut EventCtx<T>, &mut T, &Env), ////
    ////action: Box<dyn Fn(&mut EventCtx, &mut T, &Env)>,
}

impl<T: Data + 'static + Default> Button<T> { ////
////impl<T: Data + 'static> Button<T> {
    /// Create a new button. The closure provided will be called when the button
    /// is clicked.
    pub fn new(
        text: impl Into<LabelText<T>>,
        action: fn(&mut EventCtx<T>, &mut T, &Env), ////
        ////action: impl Fn(&mut EventCtx, &mut T, &Env) + 'static,
    ) -> Button<T> {
        Button {
            id: super::get_widget_id(), ////
            label: Label::aligned(text, UnitPoint::CENTER),
            action, ////
            ////action: Box::new(action),
        }
    }

    /* ////
        /// Create a new button with a fixed size.
        pub fn sized(
            text: impl Into<LabelText<T>>,
            action: impl Fn(&mut EventCtx, &mut T, &Env) + 'static,
            width: f64,
            height: f64,
        ) -> impl Widget<T> {
            Align::vertical(
                UnitPoint::CENTER,
                SizedBox::new(Button {
                    label: Label::aligned(text, UnitPoint::CENTER),
                    action: Box::new(action),
                })
                .width(width)
                .height(height),
            )
        }

        /// A function that can be passed to `Button::new`, for buttons with no action.
        ///
        /// # Examples
        ///
        /// ```
        /// use druid::widget::Button;
        ///
        /// let button = Button::<u32>::new("hello", Button::noop);
        /// ```
        pub fn noop(_: &mut EventCtx, _: &mut T, _: &Env) {}
    */ ////
}


impl<T: Data + 'static + Default> Widget<T> for Button<T> { ////
////impl<T: Data> Widget<T> for Button<T> {
    fn paint(&mut self, paint_ctx: &mut PaintCtx, base_state: &BaseState, data: &T, env: &Env) {
        ////let is_active = base_state.is_active();
        ////let is_hot = base_state.is_hot();

        let rounded_rect =
            RoundedRect::from_origin_size(Point::ORIGIN, base_state.size().to_vec2(), 4.);
        //let bg_color = crate::env::BUTTON_DARK; ////
        /* ////
            let bg_gradient = if is_active {
                LinearGradient::new(
                    UnitPoint::TOP,
                    UnitPoint::BOTTOM,
                    (env.get(theme::BUTTON_LIGHT), env.get(theme::BUTTON_DARK)),
                )
            } else {
                LinearGradient::new(
                    UnitPoint::TOP,
                    UnitPoint::BOTTOM,
                    (env.get(theme::BUTTON_DARK), env.get(theme::BUTTON_LIGHT)),
                )
            };
        */ ////

        let border_color = crate::env::BORDER; ////
        /* ////
            let border_color = if is_hot {
                env.get(theme::BORDER_LIGHT)
            } else {
                env.get(theme::BORDER)
            };
        */ ////

        // TODO paint_ctx.render_ctx.fill(rounded_rect, &bg_color); ////
        paint_ctx.render_ctx.stroke(rounded_rect, &border_color, 2.0); ////

        ////paint_ctx.stroke(rounded_rect, &border_color, 2.0);
        ////paint_ctx.render_ctx.fill(rounded_rect, &bg_gradient); ////
        ////paint_ctx.fill(rounded_rect, &bg_gradient);

        self.label.paint(paint_ctx, base_state, data, env);
    }

    fn layout(
        &mut self,
        layout_ctx: &mut LayoutCtx,
        bc: &BoxConstraints,
        data: &T,
        env: &Env,
    ) -> Size {
        bc.debug_check("Button");

        self.label.layout(layout_ctx, bc, data, env)
    }

    fn event(&mut self, ctx: &mut EventCtx<T>, event: &Event, data: &mut T, env: &Env) { ////
    ////fn event(&mut self, ctx: &mut EventCtx, event: &Event, data: &mut T, env: &Env) {
        match event {
            Event::MouseDown(_) => {
                ctx.set_active(true);
                ctx.invalidate();
            }
            Event::MouseUp(_) => {
                if ctx.is_active() {
                    ctx.set_active(false);
                    ctx.invalidate();
                    ////TODO: if ctx.is_hot() {
                        (self.action)(ctx, data, env);
                    ////}
                    //cortex_m::asm::bkpt(); ////
                }
            }
            Event::HotChanged(_) => {
                ctx.invalidate();
            }
            _ => (),
        }
    }

    fn update(&mut self, ctx: &mut UpdateCtx<T>, old_data: Option<&T>, data: &T, env: &Env) { ////
    ////fn update(&mut self, ctx: &mut UpdateCtx, old_data: Option<&T>, data: &T, env: &Env) {
        self.label.update(ctx, old_data, data, env)
    }

    fn to_type(self) -> WidgetType<T> { ////
        WidgetType::Button(self)
    }

    fn new_window(self) -> WindowBox<T> { ////
        WindowBox::new()
    }

    fn get_id(self) -> WidgetId { ////
        self.id
    }
}