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
// Copyright 2019 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 label widget.

////use std::marker::PhantomData;

use core::str::FromStr; ////
use crate::{
    BaseState, BoxConstraints, Data, Env, Event, EventCtx, LayoutCtx, PaintCtx, Size, UpdateCtx,
    Widget, 
    WidgetId, WidgetType, WindowBox, ////
};

use crate::kurbo::Rect;
use crate::piet::{
    FontBuilder, PietText, PietTextLayout, Text, TextLayout, TextLayoutBuilder, UnitPoint,
};

use crate::localization::LocalizedString;
////use crate::theme;
use crate::{Point, RenderContext};

type MaxLabel = heapless::consts::U20; //// Max length of label strings
type String = heapless::String::<MaxLabel>; ////

/// The text for the label; either a localized or a specific string.
#[derive(Clone)] ////
pub enum LabelText<T> {
    Localized(LocalizedString<T>),
    Specific(String),
}

/// A label that displays some text.
#[derive(Clone)] ////
pub struct Label<T> {
    id: WidgetId, //// Unique Widget ID
    text: LabelText<T>,
    align: UnitPoint,
}

/* ////
/// A label with dynamic text.
///
/// The provided closure is called on update, and its return
/// value is used as the text for the label.
pub struct DynLabel<T: Data> {
    label_closure: Box<dyn FnMut(&T, &Env) -> String>,
    phantom: PhantomData<T>,
}
*/ ////

impl<T: Data + 'static + Default> Label<T> { ////
////impl<T: Data> Label<T> {
    /// Discussion question: should this return Label or a wrapped
    /// widget (with WidgetPod)?
    pub fn new(text: impl Into<LabelText<T>>) -> Self {
        Label {
            id: super::get_widget_id(), ////
            text: text.into(),
            align: UnitPoint::LEFT,
        }
    }

    pub fn aligned(text: impl Into<LabelText<T>>, align: UnitPoint) -> Self {
        Label {
            id: super::get_widget_id(), ////
            text: text.into(),
            align,
        }
    }

    fn get_layout(&self, t: &mut PietText, _env: &Env) -> PietTextLayout {
        let font_name = crate::env::FONT_NAME; ////env.get(theme::FONT_NAME);
        let font_size = crate::env::TEXT_SIZE_NORMAL; ////env.get(theme::TEXT_SIZE_NORMAL);
        let text = self.text.display_text();
        // TODO: caching of both the format and the layout
        let font = t.new_font_by_name(font_name, font_size).build().expect("get layout fail"); //// .unwrap();
        t.new_text_layout(&font, text).build().expect("get layout fail") ///// .unwrap()
    }
}

impl<T: Data + 'static + Default> Widget<T> for Label<T> { ////
////impl<T: Data> Widget<T> for Label<T> {
    fn paint(&mut self, paint_ctx: &mut PaintCtx, base_state: &BaseState, _data: &T, env: &Env) {
        let font_size = crate::env::TEXT_SIZE_NORMAL; ////env.get(theme::TEXT_SIZE_NORMAL);

        let text_layout = self.get_layout(paint_ctx.render_ctx.text(), env); ////
        ////let text_layout = self.get_layout(paint_ctx.text(), env);

        // Find the origin for the text
        let mut origin = self.align.resolve(Rect::from_origin_size(
            Point::ORIGIN,
            Size::new(
                (base_state.size().width - text_layout.width()).max(0.0),
                base_state.size().height + (font_size * 1.2) / 2.,
            ),
        ));

        //Make sure we don't draw the text too low
        origin.y = origin.y.min(base_state.size().height);

        paint_ctx.render_ctx.draw_text(&text_layout, origin, &crate::env::LABEL_COLOR); ////
        ////paint_ctx.draw_text(&text_layout, origin, &env.get(theme::LABEL_COLOR));
    }

    fn layout(
        &mut self,
        layout_ctx: &mut LayoutCtx,
        bc: &BoxConstraints,
        data: &T,
        env: &Env,
    ) -> Size {
        bc.debug_check("Label");
        self.text.resolve(data, env);  ////  TODO: Should auto-resolve

        let font_size = crate::env::TEXT_SIZE_NORMAL; ////
        ////let font_size = env.get(theme::TEXT_SIZE_NORMAL);
        let text_layout = self.get_layout(layout_ctx.text(), env);
        // This magical 1.2 constant helps center the text vertically in the rect it's given
        bc.constrain((text_layout.width(), font_size * 1.2))
    }

    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) {}

    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(); ////
        if self.text.resolve(data, env) {
            ctx.invalidate();
        }
    }

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

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

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

impl<T: Data + 'static + Default> LabelText<T> { ////
////impl<T: Data> LabelText<T> {
    /// The text that should be displayed. This ensures that localized
    /// strings are up to date.
    pub fn display_text(&self) -> &str {
        match self {
            LabelText::Specific(s) => s.as_str(),
            LabelText::Localized(s) => s.localized_str(),
        }
    }

    /// Update the localization, if necessary.
    ///
    /// Returns `true` if the string has changed.
    pub fn resolve(&mut self, data: &T, env: &Env) -> bool {
        match self {
            LabelText::Specific(_) => false,
            LabelText::Localized(s) => s.resolve(data, env),
        }
    }
}

/* ////
    impl<T: Data> DynLabel<T> {
        pub fn new(label_closure: impl FnMut(&T, &Env) -> String + 'static) -> DynLabel<T> {
            DynLabel {
                label_closure: Box::new(label_closure),
                phantom: Default::default(),
            }
        }

        fn get_layout(&mut self, t: &mut PietText, env: &Env, data: &T) -> PietTextLayout {
            let text = (self.label_closure)(data, env);

            let font_name = env.get(theme::FONT_NAME);
            let font_size = env.get(theme::TEXT_SIZE_NORMAL);

            // TODO: caching of both the format and the layout
            let font = t.new_font_by_name(font_name, font_size).build().unwrap();
            t.new_text_layout(&font, &text).build().unwrap()
        }
    }

    impl<T: Data> Widget<T> for DynLabel<T> {
        fn paint(&mut self, paint_ctx: &mut PaintCtx, base_state: &BaseState, data: &T, env: &Env) {
            let font_size = env.get(theme::TEXT_SIZE_NORMAL);

            let align = UnitPoint::LEFT;
            let origin = align.resolve(Rect::from_origin_size(
                Point::ORIGIN,
                Size::new(
                    base_state.size().width,
                    base_state.size().height + (font_size * 1.2) / 2.,
                ),
            ));

            let text_layout = self.get_layout(paint_ctx.text(), env, data);
            paint_ctx.draw_text(&text_layout, origin, &env.get(theme::LABEL_COLOR));
        }

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

            let font_size = env.get(theme::TEXT_SIZE_NORMAL);
            let text_layout = self.get_layout(layout_ctx.text(), env, data);
            // This magical 1.2 constant helps center the text vertically in the rect it's given
            bc.constrain(Size::new(text_layout.width(), font_size * 1.2))
        }

        fn event(&mut self, _ctx: &mut EventCtx, _event: &Event, _data: &mut T, _env: &Env) {}

        fn update(&mut self, ctx: &mut UpdateCtx, _old_data: Option<&T>, _data: &T, _env: &Env) {
            ctx.invalidate();
        }
    }
*/ ////

impl<T> From<String> for LabelText<T> {
    fn from(src: String) -> LabelText<T> {
        LabelText::Specific(src)
    }
}

impl<T> From<&str> for LabelText<T> {
    fn from(src: &str) -> LabelText<T> {
        LabelText::Specific(String::from_str(src).expect("label text fail")) ////
        ////LabelText::Specific(src.to_string())
    }
}

impl<T> From<LocalizedString<T>> for LabelText<T> {
    fn from(src: LocalizedString<T>) -> LabelText<T> {
        LabelText::Localized(src)
    }
}