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
use crate::svg::load_svg_bytes;
use crate::{
    include_labeled_bytes, Button, Color, ControlState, EdgeInsets, EventCtx, GfxCtx, MultiKey,
    Outcome, RewriteColor, ScreenDims, ScreenPt, Text, TextSpan, Widget, WidgetImpl, WidgetOutput,
};

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

impl Toggle {
    pub fn new_widget(enabled: bool, false_btn: Button, true_btn: Button) -> Widget {
        if enabled {
            Widget::new(Box::new(Toggle {
                enabled,
                btn: true_btn,
                other_btn: false_btn,
            }))
        } else {
            Widget::new(Box::new(Toggle {
                enabled,
                btn: false_btn,
                other_btn: true_btn,
            }))
        }
    }

    pub fn switch<MK: Into<Option<MultiKey>>>(
        ctx: &EventCtx,
        label: &str,
        hotkey: MK,
        enabled: bool,
    ) -> Widget {
        let mut buttons = ctx
            .style()
            .btn_plain
            .text(label)
            // we don't want the default coloring, because we do custom coloring below
            .image_color(RewriteColor::NoOp, ControlState::Default);

        if let Some(hotkey) = hotkey.into() {
            buttons = buttons.hotkey(hotkey);
        }

        let (off_batch, off_bounds) = {
            let (label, bytes) = include_labeled_bytes!("../../icons/switch_off.svg");
            let (batch, bounds) = load_svg_bytes(ctx.prerender, label, bytes).expect("invalid SVG");
            let batch = batch
                .color(RewriteColor::Change(Color::WHITE, ctx.style.btn_solid.bg))
                .color(RewriteColor::Change(Color::BLACK, ctx.style.btn_solid.fg));
            (batch, bounds)
        };
        let (on_batch, on_bounds) = {
            let (label, bytes) = include_labeled_bytes!("../../icons/switch_on.svg");
            let (batch, bounds) = load_svg_bytes(ctx.prerender, label, bytes).expect("invalid SVG");
            let batch = batch
                .color(RewriteColor::Change(Color::WHITE, ctx.style.btn_solid.bg))
                .color(RewriteColor::Change(Color::BLACK, ctx.style.btn_solid.fg));
            (batch, bounds)
        };

        let off_button = buttons
            .clone()
            .image_batch(off_batch, off_bounds)
            .build(ctx, label);

        let on_button = buttons.image_batch(on_batch, on_bounds).build(ctx, label);

        Toggle::new_widget(enabled, off_button, on_button).named(label)
    }

    pub fn checkbox<MK: Into<Option<MultiKey>>>(
        ctx: &EventCtx,
        label: &str,
        hotkey: MK,
        enabled: bool,
    ) -> Widget {
        let mut false_btn = ctx
            .style()
            .btn_plain
            .icon_bytes(include_labeled_bytes!("../../icons/checkbox_unchecked.svg"))
            .image_color(
                RewriteColor::Change(Color::BLACK, ctx.style().icon_fg),
                ControlState::Default,
            )
            .image_color(
                RewriteColor::Change(Color::BLACK, ctx.style().icon_fg),
                ControlState::Hovered,
            )
            .image_color(
                RewriteColor::Change(Color::BLACK, ctx.style().icon_fg),
                ControlState::Disabled,
            )
            .label_text(label);

        if let Some(hotkey) = hotkey.into() {
            false_btn = false_btn.hotkey(hotkey);
        }

        let true_btn = false_btn
            .clone()
            .image_bytes(include_labeled_bytes!("../../icons/checkbox_checked.svg"));

        Toggle::new_widget(
            enabled,
            false_btn.build(ctx, label),
            true_btn.build(ctx, label),
        )
        .named(label)
    }

    pub fn custom_checkbox<MK: Into<Option<MultiKey>>>(
        ctx: &EventCtx,
        action: &str,
        spans: Vec<TextSpan>,
        hotkey: MK,
        enabled: bool,
    ) -> Widget {
        let mut false_btn = ctx
            .style()
            .btn_plain
            .icon_bytes(include_labeled_bytes!("../../icons/checkbox_unchecked.svg"))
            .image_color(
                RewriteColor::Change(Color::BLACK, ctx.style().icon_fg),
                ControlState::Default,
            )
            .image_color(
                RewriteColor::Change(Color::BLACK, ctx.style().icon_fg),
                ControlState::Hovered,
            )
            .image_color(
                RewriteColor::Change(Color::BLACK, ctx.style().icon_fg),
                ControlState::Disabled,
            )
            .label_styled_text(Text::from_all(spans), ControlState::Default);

        if let Some(hotkey) = hotkey.into() {
            false_btn = false_btn.hotkey(hotkey);
        }

        let true_btn = false_btn
            .clone()
            .image_bytes(include_labeled_bytes!("../../icons/checkbox_checked.svg"));

        Toggle::new_widget(
            enabled,
            false_btn.build(ctx, action),
            true_btn.build(ctx, action),
        )
        .named(action)
    }

    pub fn colored_checkbox(ctx: &EventCtx, label: &str, color: Color, enabled: bool) -> Widget {
        let buttons = ctx.style().btn_plain.btn().label_text(label).padding(4.0);

        let false_btn = buttons
            .clone()
            .image_bytes(include_labeled_bytes!(
                "../../icons/checkbox_no_border_unchecked.svg"
            ))
            .image_color(
                RewriteColor::Change(Color::BLACK, color.alpha(0.3)),
                ControlState::Default,
            );

        let true_btn = buttons
            .image_bytes(include_labeled_bytes!(
                "../../icons/checkbox_no_border_checked.svg"
            ))
            .image_color(
                RewriteColor::Change(Color::BLACK, color),
                ControlState::Default,
            );

        Toggle::new_widget(
            enabled,
            false_btn.build(ctx, label),
            true_btn.build(ctx, label),
        )
        .named(label)
    }

    // TODO These should actually be radio buttons
    pub fn choice<MK: Into<Option<MultiKey>>>(
        ctx: &EventCtx,
        action: &str,
        left_label: &str,
        right_label: &str,
        hotkey: MK,
        enabled: bool,
    ) -> Widget {
        let mut toggle_left_button = ctx
            .style()
            .btn_plain
            .btn()
            .image_dims(ScreenDims::new(40.0, 40.0))
            .padding(4)
            // we don't want the default coloring, because we do custom coloring below
            .image_color(RewriteColor::NoOp, ControlState::Default);

        if let Some(hotkey) = hotkey.into() {
            toggle_left_button = toggle_left_button.hotkey(hotkey);
        }

        let (left_batch, left_bounds) = {
            let (label, bytes) = include_labeled_bytes!("../../icons/toggle_left.svg");
            let (batch, bounds) = load_svg_bytes(ctx.prerender, label, bytes).expect("invalid SVG");
            let batch = batch
                .color(RewriteColor::Change(Color::WHITE, ctx.style.btn_solid.bg))
                .color(RewriteColor::Change(Color::BLACK, ctx.style.btn_solid.fg));
            (batch, bounds)
        };
        let (right_batch, right_bounds) = {
            let (label, bytes) = include_labeled_bytes!("../../icons/toggle_right.svg");
            let (batch, bounds) = load_svg_bytes(ctx.prerender, label, bytes).expect("invalid SVG");
            let batch = batch
                .color(RewriteColor::Change(Color::WHITE, ctx.style.btn_solid.bg))
                .color(RewriteColor::Change(Color::BLACK, ctx.style.btn_solid.fg));
            (batch, bounds)
        };

        let toggle_right_button = toggle_left_button
            .clone()
            .image_batch(right_batch, right_bounds);

        let toggle_left_button = toggle_left_button
            .clone()
            .image_batch(left_batch, left_bounds);

        let left_text_button = ctx
            .style()
            .btn_plain
            .text(left_label)
            // Cheat vertical padding to align with switch
            .padding(EdgeInsets {
                left: 2.0,
                right: 2.0,
                top: 8.0,
                bottom: 14.0,
            })
            // TODO: make these clickable. Currently they would explode due to re-use of an action
            .disabled(true)
            .label_color(ctx.style().btn_outline.fg, ControlState::Disabled)
            .bg_color(Color::CLEAR, ControlState::Disabled);
        let right_text_button = left_text_button.clone().label_text(right_label);
        Widget::row(vec![
            left_text_button.build_def(ctx).centered_vert(),
            Toggle::new_widget(
                enabled,
                toggle_right_button.build(ctx, right_label),
                toggle_left_button.build(ctx, left_label),
            )
            .named(action)
            .centered_vert(),
            right_text_button.build_def(ctx).centered_vert(),
        ])
    }
}

impl WidgetImpl for Toggle {
    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 {
            // Both buttons have the same label
            output.outcome = Outcome::Changed(self.btn.action.clone());
            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);
    }
}