summaryrefslogtreecommitdiff
path: root/src/options.rs
blob: c3812941266afa39ce354d5a8d1c81745de8dc92 (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
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
use std::collections::BTreeMap;

#[allow(unused_imports)]
use iced::widget::{Button, Column, Container, Image, Row, Svg, Text};
use iced::{Padding, Rotation};

use crate::values::ValueFromStr;

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

impl<Message> ApplyOptions for Button<'_, 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::value_from_str(padding).unwrap();
            button = button.padding(padding);
        }

        button
    }
}

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

        if let Some(padding) = options.get("padding").expect("padding key") {
            let padding = Padding::value_from_str(padding).unwrap();
            column = column.padding(padding);
        }

        column
    }
}

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

        if let Some(padding) = options.get("padding").expect("padding key") {
            let padding = Padding::value_from_str(padding).unwrap();
            row = row.padding(padding);
        }

        row
    }
}

impl<Handle> ApplyOptions for Image<Handle> {
    fn apply_options(self, options: BTreeMap<String, Option<String>>) -> Self {
        let mut image = self;

        if let Some(rotation) = options.get("rotation").expect("rotation key") {
            let rotation = Rotation::value_from_str(rotation).unwrap();
            image = image.rotation(rotation);
        }

        image
    }
}

impl ApplyOptions for Svg<'_> {
    fn apply_options(self, options: BTreeMap<String, Option<String>>) -> Self {
        let mut svg = self;

        if let Some(rotation) = options.get("rotation").expect("rotation key") {
            let rotation = Rotation::value_from_str(rotation).unwrap();
            svg = svg.rotation(rotation);
        }

        svg
    }
}