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
mod button;
pub mod layout;
mod placeholder;
mod text;
mod text_input;

use crate::widget_manager::WidgetBox;
use crate::{SizeConstraints, SystemEvent};
pub use button::Button;
use druid_shell::kurbo::{Point, Size};
use druid_shell::piet;
use druid_shell::Region;
pub use placeholder::Placeholder;
use std::any::Any;
use std::cell::RefCell;
use std::fmt::{Debug, Formatter};
pub use text::Text;
pub use text_input::TextInput;

///
pub type WidgetId = usize;

/// A command to a widget.
pub enum WidgetCommand {
    /// Append the child widget.
    AppendChild(WidgetBox),
    /// Remove the widget's children.
    RemoveAllChildren,
    /// Remove the child widget.
    RemoveChild(WidgetId),
    /// Enables/disables debug rendering mode.
    SetDebugRendering(bool),
    /// Gives/removes focus to the widget.
    SetHasFocus(bool),
    /// Enables/disables the widget.
    SetIsDisabled(bool),
    /// Hides/shows the widget.
    SetIsHidden(bool),
    /// Sets the given value to the widget.
    SetValue(Box<dyn Any>),
}

impl Debug for WidgetCommand {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        match self {
            WidgetCommand::AppendChild(widget_box) => {
                write!(
                    f,
                    "AppendChild({:?})",
                    RefCell::borrow(widget_box).widget_id()
                )
            }
            WidgetCommand::RemoveAllChildren => {
                write!(f, "{:?}", self)
            }
            WidgetCommand::RemoveChild(_) => {
                write!(f, "{:?}", self)
            }
            WidgetCommand::SetDebugRendering(_) => {
                write!(f, "{:?}", self)
            }
            WidgetCommand::SetHasFocus(_) => {
                write!(f, "{:?}", self)
            }
            WidgetCommand::SetIsDisabled(_) => {
                write!(f, "{:?}", self)
            }
            WidgetCommand::SetIsHidden(_) => {
                write!(f, "{:?}", self)
            }
            WidgetCommand::SetValue(_) => {
                write!(f, "{:?}", self)
            }
        }
    }
}

// =================================================================================================

///
#[derive(Debug)]
pub enum WidgetError {
    CommandNotHandled(WidgetId, WidgetCommand),
    NoSuchWidget(WidgetId),
}

/// An event generated by a widget.
#[derive(Debug)]
pub enum WidgetEvent {
    Clicked(WidgetId),
    GotFocus(WidgetId),
    LostFocus(WidgetId),
    ValueChanged(WidgetId, Box<dyn Any>),
}

///
pub trait Widget {
    ///
    fn apply_size_constraints(&mut self, size_constraints: SizeConstraints) -> Size;

    ///
    fn handle_command(&mut self, widget_command: WidgetCommand) -> Result<(), WidgetError>;

    ///
    fn handle_event(&mut self, system_event: &SystemEvent, widget_events: &mut Vec<WidgetEvent>);

    ///
    fn paint(&self, piet: &mut piet::Piet, region: &Region) -> Result<(), piet::Error>;

    ///
    fn set_origin(&mut self, origin: Point);

    ///
    fn widget_id(&self) -> &WidgetId;
}