summaryrefslogtreecommitdiff
path: root/src/options.rs
blob: 9514dcb96cfe7b11d49f8c27d14dbd928fda0b47 (plain)
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
use std::collections::BTreeMap;
use std::str::FromStr;

use iced::Padding;
use iced::widget::{Button, Column, Container, Image, Row, Svg, Text};

pub trait ApplyOptions {
    fn apply_options(self, options: BTreeMap<String, Option<String>>) -> Self;
}

impl<'a, Message> ApplyOptions for Button<'a, Message> {
    fn apply_options(self, options: BTreeMap<String, Option<String>>) -> Self {
        let mut button = self;

        if let Some(padding) = options.get("padding").expect("padding key") {
            let padding: Padding = padding
                .strip_prefix('[')
                .and_then(|s| s.strip_suffix(']'))
                .and_then(|s| {
                    Some(
                        s.split(',')
                            .map(|n| f32::from_str(n).unwrap())
                            .collect::<Vec<_>>(),
                    )
                })
                .and_then(|s| {
                    if s.len() == 4 {
                        Some(Padding {
                            top: s[0],
                            right: s[1],
                            bottom: s[2],
                            left: s[3],
                        })
                    } else {
                        None
                    }
                })
                .unwrap();
            button = button.padding(padding);
        }

        button
    }
}