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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
// 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 widget that arranges its children in a one-dimensional array.

use crate::kurbo::{Point, Rect, Size}; ////

use crate::{
    BaseState, BoxConstraints, Data, Env, Event, EventCtx, LayoutCtx, PaintCtx, UpdateCtx, Widget,
    WidgetPod,
    Window, WindowType, WindowBox, widget::{WidgetBox, WidgetId, WidgetType}, ////
};

/// A builder for a row widget that can contain flex children.
///
/// The actual widget is implemented by [`Flex`], but this builder
/// is provided for convenience.
///
/// [`Flex`]: struct.Flex.html
pub struct Row;
/// A builder for a column widget that can contain flex children.
///
/// The actual widget is implemented by [`Flex`], but this builder
/// is provided for convenience.
///
/// [`Flex`]: struct.Flex.html
pub struct Column;

type MaxWidgets = heapless::consts::U8; //// Max widgets per container
type Vec<T> = heapless::Vec::<T, MaxWidgets>;

/// A container with either horizontal or vertical layout.
#[derive(Clone)] ////
pub struct Flex<T: Data + 'static + Default> { ////
////pub struct Flex<T: Data> {
    id: WidgetId, //// Unique Widget ID
    direction: Axis,
    children: Vec<ChildWidget<T>>, ////
    ////children: Vec<ChildWidget<T>>,
}

#[derive(Clone)] ////
struct ChildWidget<T: Data + 'static + Default> { ////
////struct ChildWidget<T: Data> {
    widget: WidgetPod<T, WidgetBox<T>>, ////
    ////widget: WidgetPod<T, Box<dyn Widget<T>>>,
    params: Params,
}

#[derive(Clone, Copy)] ////
pub enum Axis {
    Horizontal,
    Vertical,
}

#[derive(Copy, Clone, Default)]
struct Params {
    flex: f64,
}

impl Axis {
    fn major(&self, coords: Size) -> f64 {
        match *self {
            Axis::Horizontal => coords.width,
            Axis::Vertical => coords.height,
        }
    }

    fn minor(&self, coords: Size) -> f64 {
        match *self {
            Axis::Horizontal => coords.height,
            Axis::Vertical => coords.width,
        }
    }

    fn pack(&self, major: f64, minor: f64) -> (f64, f64) {
        match *self {
            Axis::Horizontal => (major, minor),
            Axis::Vertical => (minor, major),
        }
    }
}

impl Row {
    /// Create a new row widget.
    ///
    /// The child widgets are laid out horizontally, from left to right.
    pub fn new<T: Data + 'static + Default>() -> Flex<T> { ////
    ////pub fn new<T: Data>() -> Flex<T> {
        Flex {
            id: super::get_widget_id(), ////
            direction: Axis::Horizontal,

            children: Vec::new(),
        }
    }
}

impl Column {
    /// Create a new row widget.
    ///
    /// The child widgets are laid out vertically, from top to bottom.
    pub fn new<T: Data + 'static + Default>() -> Flex<T> { ////
    ////pub fn new<T: Data>() -> Flex<T> {
        Flex {
            id: super::get_widget_id(), ////
            direction: Axis::Vertical,

            children: Vec::new(),
        }
    }
}

impl<T: Data + 'static + Default> Flex<T> { ////
////impl<T: Data> Flex<T> {
    /// Add a child widget.
    ///
    /// If `flex` is zero, then the child is non-flex. It is given the same
    /// constraints on the "minor axis" as its parent, but unconstrained on the
    /// "major axis".
    ///
    /// If `flex` is non-zero, then all the space left over after layout of
    /// the non-flex children is divided up, in proportion to the `flex` value,
    /// among the flex children.
    pub fn add_child<W: Widget<T> + Clone>(&mut self, child: W, flex: f64) { ////
    ////pub fn add_child(&mut self, child: impl Widget<T> + 'static, flex: f64) {
        let params = Params { flex };
        let child = ChildWidget {
            widget: WidgetPod::new( ////
                WidgetBox::<T>::new(child)
            ),
            ////widget: WidgetPod::new(child).boxed(),
            params,
        };
        self.children.push(child)
            .expect("add child fail"); ////
        ////self.children.push(child);
    }
}

impl<T: Data + 'static + Default> Widget<T> for Flex<T> { ////
////impl<T: Data> Widget<T> for Flex<T> {
    fn paint(&mut self, paint_ctx: &mut PaintCtx, _base_state: &BaseState, data: &T, env: &Env) {
        for child in &mut self.children {
            child.widget.paint_with_offset(paint_ctx, data, env);
        }
    }

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

        // Measure non-flex children.
        let mut total_non_flex = 0.0;
        let mut minor = self.direction.minor(bc.min());
        for child in &mut self.children {
            if child.params.flex == 0.0 {
                let child_bc = match self.direction {
                    Axis::Horizontal => BoxConstraints::new(
                        Size::new(0.0, bc.min().height),
                        Size::new(core::f64::INFINITY, bc.max.height), ////
                        ////Size::new(std::f64::INFINITY, bc.max.height),
                    ),
                    Axis::Vertical => BoxConstraints::new(
                        Size::new(bc.min().width, 0.0),
                        Size::new(bc.max().width, core::f64::INFINITY), ////
                        ////Size::new(bc.max().width, std::f64::INFINITY),
                    ),
                };
                let child_size = child.widget.layout(layout_ctx, &child_bc, data, env);
                minor = minor.max(self.direction.minor(child_size));
                total_non_flex += self.direction.major(child_size);
                // Stash size.
                let rect = Rect::from_origin_size(Point::ORIGIN, child_size);
                child.widget.set_layout_rect(rect);
            }
        }

        let total_major = self.direction.major(bc.max());
        let remaining = (total_major - total_non_flex).max(0.0);
        let flex_sum: f64 = self.children.iter().map(|child| child.params.flex).sum();

        // Measure flex children.
        for child in &mut self.children {
            if child.params.flex != 0.0 {
                let major = remaining * child.params.flex / flex_sum;

                let min_major = if major.is_infinite() { 0.0 } else { major };

                let child_bc = match self.direction {
                    Axis::Horizontal => BoxConstraints::new(
                        Size::new(min_major, bc.min().height),
                        Size::new(major, bc.max().height),
                    ),
                    Axis::Vertical => BoxConstraints::new(
                        Size::new(bc.min().width, min_major),
                        Size::new(bc.max().width, major),
                    ),
                };
                let child_size = child.widget.layout(layout_ctx, &child_bc, data, env);
                minor = minor.max(self.direction.minor(child_size));
                // Stash size.
                let rect = Rect::from_origin_size(Point::ORIGIN, child_size);
                child.widget.set_layout_rect(rect);
            }
        }

        // Finalize layout, assigning positions to each child.
        let mut major = 0.0;
        for child in &mut self.children {
            // top-align, could do center etc. based on child height
            let rect = child.widget.get_layout_rect();
            let pos: Point = self.direction.pack(major, 0.0).into();
            child.widget.set_layout_rect(rect.with_origin(pos));
            major += self.direction.major(rect.size());
        }

        if flex_sum > 0.0 && total_major.is_infinite() {
            ////log::warn!("A child of Flex is flex, but Flex is unbounded.")
        }

        if flex_sum > 0.0 {
            major = total_major;
        }

        // TODO: should be able to make this `into`
        let (width, height) = self.direction.pack(major, minor);
        Size::new(width, height)
    }

    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) {
        for child in &mut self.children {
            child.widget.event(ctx, event, data, env);
        }
    }

    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) {
        //cortex_m::asm::bkpt(); ////
        for child in &mut self.children {
            child.widget.update(ctx, data, env);
        }
    }

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

    fn new_window(self) -> WindowBox<T> { ////
        let window = Window::new(self);
        let window_box = WindowBox(
            WindowType::Flex(window),
            //PhantomData,
        );
        window_box
    }

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

/// Implement formatted output for ChildWidget
impl<T: Data + Default> core::fmt::Debug for ChildWidget<T> { ////
    fn fmt(&self, _fmt: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result {
        //  TODO
        Ok(())
    }
}