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
use crate::{
    Btn, Button, Color, EventCtx, GeomBatch, GfxCtx, Line, MultiKey, Outcome, RewriteColor,
    ScreenDims, ScreenPt, Text, TextExt, TextSpan, Widget, WidgetImpl, WidgetOutput,
};

pub struct Checkbox {
    pub(crate) enabled: bool,
    btn: Button,
    other_btn: Button,
}

impl Checkbox {
    // TODO Not typesafe! Gotta pass a button. Also, make sure to give an ID.
    pub fn new(enabled: bool, false_btn: Widget, true_btn: Widget) -> Widget {
        if enabled {
            Widget::new(Box::new(Checkbox {
                enabled,
                btn: true_btn.take_btn(),
                other_btn: false_btn.take_btn(),
            }))
        } else {
            Widget::new(Box::new(Checkbox {
                enabled,
                btn: false_btn.take_btn(),
                other_btn: true_btn.take_btn(),
            }))
        }
    }

    pub fn switch<I: Into<String>>(
        ctx: &EventCtx,
        label: I,
        hotkey: Option<MultiKey>,
        enabled: bool,
    ) -> Widget {
        let label = label.into();
        let (off, hitbox) = Widget::row(vec![
            GeomBatch::load_svg(ctx.prerender, "system/assets/tools/toggle_off.svg")
                .batch()
                .centered_vert(),
            label.clone().batch_text(ctx),
        ])
        .to_geom(ctx, None);
        let (on, _) = Widget::row(vec![
            GeomBatch::load_svg(ctx.prerender, "system/assets/tools/toggle_on.svg")
                .batch()
                .centered_vert(),
            label.clone().batch_text(ctx),
        ])
        .to_geom(ctx, None);

        Checkbox::new(
            enabled,
            Btn::custom(
                off.clone(),
                off.color(RewriteColor::Change(
                    Color::hex("#F2F2F2"),
                    ctx.style().hovering_color,
                )),
                hitbox.clone(),
            )
            .build(ctx, &label, hotkey.clone()),
            Btn::custom(
                on.clone(),
                on.color(RewriteColor::Change(
                    Color::hex("#F2F2F2"),
                    ctx.style().hovering_color,
                )),
                hitbox,
            )
            .build(ctx, &label, hotkey),
        )
        .named(label)
    }

    pub fn checkbox<I: Into<String>>(
        ctx: &EventCtx,
        label: I,
        hotkey: Option<MultiKey>,
        enabled: bool,
    ) -> Widget {
        let label = label.into();
        Checkbox::new(
            enabled,
            Btn::text_fg(format!("[ ] {}", label)).build(ctx, &label, hotkey.clone()),
            Btn::text_fg(format!("[X] {}", label)).build(ctx, &label, hotkey),
        )
        .outline(ctx.style().outline_thickness, ctx.style().outline_color)
        .named(label)
    }

    pub fn custom_checkbox<I: Into<String>>(
        ctx: &EventCtx,
        label: I,
        spans: Vec<TextSpan>,
        hotkey: Option<MultiKey>,
        enabled: bool,
    ) -> Widget {
        let label = label.into();
        let mut off = vec![Line("[ ] ")];
        let mut on = vec![Line("[X] ")];
        off.extend(spans.clone());
        on.extend(spans);

        Checkbox::new(
            enabled,
            Btn::txt(&label, Text::from_all(off)).build_def(ctx, hotkey.clone()),
            Btn::txt(&label, Text::from_all(on)).build_def(ctx, hotkey),
        )
        .outline(ctx.style().outline_thickness, ctx.style().outline_color)
        .named(label)
    }

    pub fn colored(ctx: &EventCtx, label: &str, color: Color, enabled: bool) -> Widget {
        let (off, hitbox) = Widget::row(vec![
            GeomBatch::load_svg(ctx.prerender, "system/assets/tools/checkbox.svg")
                .color(RewriteColor::ChangeAll(color.alpha(0.3)))
                .batch()
                .centered_vert(),
            label.batch_text(ctx),
        ])
        .to_geom(ctx, None);
        let (on, _) = Widget::row(vec![
            GeomBatch::load_svg(ctx.prerender, "system/assets/tools/checkbox.svg")
                .color(RewriteColor::Change(Color::BLACK, color))
                .batch()
                .centered_vert(),
            label.batch_text(ctx),
        ])
        .to_geom(ctx, None);

        Checkbox::new(
            enabled,
            Btn::custom(
                off.clone(),
                off.color(RewriteColor::Change(
                    Color::WHITE,
                    ctx.style().hovering_color,
                )),
                hitbox.clone(),
            )
            .build(ctx, label, None),
            Btn::custom(
                on.clone(),
                on.color(RewriteColor::Change(
                    Color::WHITE,
                    ctx.style().hovering_color,
                )),
                hitbox,
            )
            .build(ctx, label, None),
        )
        .named(label)
    }

    // TODO These should actually be radio buttons
    pub fn toggle<I: Into<String>>(
        ctx: &EventCtx,
        label: I,
        left_label: I,
        right_label: I,
        hotkey: Option<MultiKey>,
        enabled: bool,
    ) -> Widget {
        let left_label = left_label.into();
        let right_label = right_label.into();
        Widget::row(vec![
            left_label.clone().draw_text(ctx),
            Checkbox::new(
                enabled,
                Btn::svg_def("system/assets/tools/toggle_right.svg").build(
                    ctx,
                    left_label,
                    hotkey.clone(),
                ),
                Btn::svg_def("system/assets/tools/toggle_left.svg").build(
                    ctx,
                    right_label.clone(),
                    hotkey,
                ),
            )
            .named(label),
            right_label.draw_text(ctx),
        ])
    }
}

impl WidgetImpl for Checkbox {
    fn get_dims(&self) -> ScreenDims {
        self.btn.get_dims()
    }

    fn set_pos(&mut self, top_left: ScreenPt) {
        self.btn.set_pos(top_left);
    }

    fn event(&mut self, ctx: &mut EventCtx, output: &mut WidgetOutput) {
        self.btn.event(ctx, output);
        if let Outcome::Clicked(_) = output.outcome {
            output.outcome = Outcome::Changed;
            std::mem::swap(&mut self.btn, &mut self.other_btn);
            self.btn.set_pos(self.other_btn.top_left);
            self.enabled = !self.enabled;
            output.redo_layout = true;
        }
    }

    fn draw(&self, g: &mut GfxCtx) {
        self.btn.draw(g);
    }
}