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
use std::ops::{Deref, DerefMut};

use fltk::misc::InputChoice;
use fltk::prelude::*;

use super::{LayoutElement, LayoutWidgetWrapper, Size};

pub struct InputChoiceElement {
    widget: InputChoice,
}

impl LayoutWidgetWrapper<InputChoice> for InputChoiceElement {
    fn wrap(widget: InputChoice) -> Self {
        Self { widget }
    }
}

impl LayoutElement for InputChoiceElement {
    fn min_size(&self) -> Size {
        fltk::draw::set_font(self.widget.text_font(), self.widget.text_size());
        let text_height = fltk::draw::height();
        let text_width = self
            .widget
            .menu_button()
            .into_iter()
            .filter_map(|item| item.label())
            .map(|label| fltk::draw::measure(&label, true).0)
            .max()
            .unwrap_or_default();

        let frame = self.widget.frame();
        let frame_dx = frame.dx();
        let frame_dy = frame.dy();
        let frame_dw = frame.dw();
        let frame_dh = frame.dh();
        let frame_width = frame_dx + frame_dw;
        let frame_height = frame_dy + frame_dh;

        Size {
            width: 3 * frame_width + text_width + text_height,
            height: text_height + frame_height + 1,
        }
    }

    fn layout(&self, x: i32, y: i32, width: i32, height: i32) {
        self.widget.clone().resize(x, y, width, height);
    }
}

impl Deref for InputChoiceElement {
    type Target = InputChoice;
    fn deref(&self) -> &Self::Target {
        &self.widget
    }
}

impl DerefMut for InputChoiceElement {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.widget
    }
}