summaryrefslogtreecommitdiff
path: root/src/options.rs
blob: 2ebdd5f3626422344170f2ed912e903017346cd8 (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
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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
use std::collections::BTreeMap;
use std::str::FromStr;

use iced::widget::grid::Sizing;
use iced::widget::text::LineHeight;
#[allow(unused_imports)]
use iced::widget::{Button, Column, Container, Grid, Image, Row, Svg, Text};
use iced::{Alignment, ContentFit, Length, Padding, Pixels, Rotation};

use crate::values::Value;

pub trait ApplyOptions {
    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(width) = options.get("width").expect("width key") {
            let width = Length::from_str(width).unwrap();
            button = button.width(width);
        }

        if let Some(height) = options.get("height").expect("height key") {
            let height = Length::from_str(height).unwrap();
            button = button.height(height);
        }

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

        if let Some(clip) = options.get("clip").expect("clip key") {
            let clip = bool::from_str(clip).unwrap();
            button = button.clip(clip);
        }

        button
    }
}

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

        if let Some(size) = options.get("size").expect("size key") {
            let size = Pixels::from_str(size).unwrap();
            text = text.size(size);
        }

        if let Some(line_height) =
            options.get("line_height").expect("line_height key")
        {
            let line_height = LineHeight::from_str(line_height).unwrap();
            text = text.line_height(line_height);
        }

        if let Some(width) = options.get("width").expect("width key") {
            let width = Length::from_str(width).unwrap();
            text = text.width(width);
        }

        if let Some(height) = options.get("height").expect("height key") {
            let height = Length::from_str(height).unwrap();
            text = text.height(height);
        }

        if let Some(align_x) = options.get("align_x").expect("align_x key") {
            let align_x = Alignment::from_str(align_x).unwrap();
            text = text.align_x(align_x);
        }

        if let Some(align_y) = options.get("align_y").expect("align_y key") {
            let align_y = Alignment::from_str(align_y).unwrap();
            text = text.align_y(align_y);
        }

        text
    }
}

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

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

        if let Some(width) = options.get("width").expect("width key") {
            let width = Length::from_str(width).unwrap();
            container = container.width(width);
        }

        if let Some(height) = options.get("height").expect("height key") {
            let height = Length::from_str(height).unwrap();
            container = container.height(height);
        }

        if let Some(max_width) =
            options.get("max_width").expect("max_width key")
        {
            let max_width = Pixels::from_str(max_width).unwrap();
            container = container.max_width(max_width);
        }

        if let Some(max_height) =
            options.get("max_height").expect("max_height key")
        {
            let max_height = Pixels::from_str(max_height).unwrap();
            container = container.max_height(max_height);
        }

        if let Some(center_x) = options.get("center_x").expect("center_x key") {
            let center_x = Length::from_str(center_x).unwrap();
            container = container.center_x(center_x);
        }

        if let Some(center_y) = options.get("center_y").expect("center_y key") {
            let center_y = Length::from_str(center_y).unwrap();
            container = container.center_y(center_y);
        }

        if let Some(center) = options.get("center").expect("center key") {
            let center = Length::from_str(center).unwrap();
            container = container.center(center);
        }

        if let Some(align_left) =
            options.get("align_left").expect("align_left key")
        {
            let align_left = Length::from_str(align_left).unwrap();
            container = container.align_left(align_left);
        }

        if let Some(align_right) =
            options.get("align_right").expect("align_right key")
        {
            let align_right = Length::from_str(align_right).unwrap();
            container = container.align_right(align_right);
        }

        if let Some(align_top) =
            options.get("align_top").expect("align_top key")
        {
            let align_top = Length::from_str(align_top).unwrap();
            container = container.align_top(align_top);
        }

        if let Some(align_bottom) =
            options.get("align_bottom").expect("align_bottom key")
        {
            let align_bottom = Length::from_str(align_bottom).unwrap();
            container = container.align_bottom(align_bottom);
        }

        if let Some(align_x) = options.get("align_x").expect("align_x key") {
            let align_x = Alignment::from_str(align_x).unwrap();
            container = container.align_x(align_x);
        }

        if let Some(align_y) = options.get("align_y").expect("align_y key") {
            let align_y = Alignment::from_str(align_y).unwrap();
            container = container.align_y(align_y);
        }

        if let Some(clip) = options.get("clip").expect("clip key") {
            let clip = bool::from_str(clip).unwrap();
            container = container.clip(clip);
        }

        container
    }
}

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

        if let Some(spacing) = options.get("spacing").expect("spacing key") {
            let spacing = Pixels::from_str(spacing).unwrap();
            column = column.spacing(spacing);
        }

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

        if let Some(width) = options.get("width").expect("width key") {
            let width = Length::from_str(width).unwrap();
            column = column.width(width);
        }

        if let Some(height) = options.get("height").expect("height key") {
            let height = Length::from_str(height).unwrap();
            column = column.height(height);
        }

        if let Some(max_width) =
            options.get("max_width").expect("max_width key")
        {
            let max_width = Pixels::from_str(max_width).unwrap();
            column = column.max_width(max_width);
        }

        if let Some(align_x) = options.get("align_x").expect("align_x key") {
            let align_x = Alignment::from_str(align_x).unwrap();
            column = column.align_x(align_x);
        }

        if let Some(clip) = options.get("clip").expect("clip key") {
            let clip = bool::from_str(clip).unwrap();
            column = column.clip(clip);
        }

        column
    }
}

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

        if let Some(spacing) = options.get("spacing").expect("spacing key") {
            let spacing = Pixels::from_str(spacing).unwrap();
            row = row.spacing(spacing);
        }

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

        if let Some(width) = options.get("width").expect("width key") {
            let width = Length::from_str(width).unwrap();
            row = row.width(width);
        }

        if let Some(height) = options.get("height").expect("height key") {
            let height = Length::from_str(height).unwrap();
            row = row.height(height);
        }

        if let Some(align_y) = options.get("align_y").expect("align_y key") {
            let align_y = Alignment::from_str(align_y).unwrap();
            row = row.align_y(align_y);
        }

        if let Some(clip) = options.get("clip").expect("clip key") {
            let clip = bool::from_str(clip).unwrap();
            row = row.clip(clip);
        }

        row
    }
}

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

        if let Some(spacing) = options.get("spacing").expect("spacing key") {
            let spacing = f32::from_str(spacing).unwrap();
            grid = grid.spacing(spacing);
        }

        if let Some(width) = options.get("width").expect("width key") {
            let width = Pixels::from_str(width).unwrap();
            grid = grid.width(width);
        }

        if let Some(height) = options.get("height").expect("height key") {
            let height = Sizing::from_str(height).unwrap();
            grid = grid.height(height);
        }

        if let Some(columns) = options.get("columns").expect("columns key") {
            let columns = usize::from_str(columns).unwrap();
            grid = grid.columns(columns);
        }

        if let Some(fluid) = options.get("fluid").expect("fluid key") {
            let fluid = Pixels::from_str(fluid).unwrap();
            grid = grid.fluid(fluid);
        }

        grid
    }
}

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

        if let Some(width) = options.get("width").expect("width key") {
            let width = Length::from_str(width).unwrap();
            image = image.width(width);
        }

        if let Some(height) = options.get("height").expect("height key") {
            let height = Length::from_str(height).unwrap();
            image = image.height(height);
        }

        if let Some(content_fit) =
            options.get("content_fit").expect("content_fit key")
        {
            let content_fit = ContentFit::from_str(content_fit).unwrap();
            image = image.content_fit(content_fit);
        }

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

        if let Some(opacity) = options.get("opacity").expect("opacity key") {
            let opacity = f32::from_str(opacity).unwrap();
            image = image.opacity(opacity);
        }

        if let Some(scale) = options.get("scale").expect("scale key") {
            let scale = f32::from_str(scale).unwrap();
            image = image.scale(scale);
        }

        image
    }
}

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

        if let Some(width) = options.get("width").expect("width key") {
            let width = Length::from_str(width).unwrap();
            svg = svg.width(width);
        }

        if let Some(height) = options.get("height").expect("height key") {
            let height = Length::from_str(height).unwrap();
            svg = svg.height(height);
        }

        if let Some(content_fit) =
            options.get("content_fit").expect("content_fit key")
        {
            let content_fit = ContentFit::from_str(content_fit).unwrap();
            svg = svg.content_fit(content_fit);
        }

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

        if let Some(opacity) = options.get("opacity").expect("opacity key") {
            let opacity = f32::from_str(opacity).unwrap();
            svg = svg.opacity(opacity);
        }

        svg
    }
}