This repository has been archived by the owner on Jan 1, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Util.fs
125 lines (114 loc) · 6.17 KB
/
Util.fs
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
module internal Util
open Fabulous
open Fabulous.XamarinForms
open Xamarin.Forms
let inline tryFind<'t> name (map: Map<string, obj>) : Option<'t> =
map
|> Map.tryFind name
|> Option.map unbox<'t>
let inline isDefined prop =
match prop with
| (name, Some value) -> Some(name, value)
| _ -> None
let inline applyGridSettings (element: ViewElement) (map: Map<string, obj>) : ViewElement =
[ Keys.Row, tryFind Keys.Row map
Keys.Column, tryFind Keys.Column map
Keys.RowSpan, tryFind Keys.RowSpan map
Keys.ColumnSpan, tryFind Keys.ColumnSpan map ]
|> List.choose isDefined
|> List.fold (fun (elem: ViewElement) (propName, propValue) ->
match propName with
| Keys.Row -> elem.Row(int propValue)
| Keys.Column -> elem.Column(int propValue)
| Keys.RowSpan -> elem.RowSpan(int propValue)
| Keys.ColumnSpan -> elem.ColumnSpan(int propValue)
| _ -> elem) element
let inline applyAbsoluteLayoutSettings (element: ViewElement) (props: Map<string, obj>) : ViewElement =
[ Keys.AbsoluteLayoutFlags, tryFind Keys.AbsoluteLayoutFlags props
Keys.AbsoluteLayoutBounds, tryFind Keys.AbsoluteLayoutBounds props ]
|> List.choose isDefined
|> List.fold (fun (el: ViewElement) (propName, propValue) ->
match propName with
| Keys.AbsoluteLayoutFlags -> el.LayoutFlags (unbox<AbsoluteLayoutFlags> propValue)
| Keys.AbsoluteLayoutBounds -> el.LayoutBounds (unbox<Rectangle> propValue)
| _ -> el) element
let inline applyRelativeLayoutConstraints (element: ViewElement) (props: Map<string, obj>) : ViewElement =
[ Keys.WidthConstraint, tryFind Keys.WidthConstraint props
Keys.HeightConstraint, tryFind Keys.HeightConstraint props
Keys.XConstraint, tryFind Keys.XConstraint props
Keys.YConstraint, tryFind Keys.YConstraint props ]
|> List.choose isDefined
|> List.fold (fun (el: ViewElement) (propName, propValue) ->
match propName with
| Keys.WidthConstraint -> el.WidthConstraint (unbox propValue)
| Keys.HeightConstraint -> el.HeightConstraint (unbox propValue)
| Keys.XConstraint -> el.XConstraint (unbox propValue)
| Keys.YConstraint -> el.YConstraint (unbox propValue)
| _ -> el) element
let inline applyFlexLayoutSettings (element: ViewElement) (props: Map<string, obj>) : ViewElement =
[ Keys.Basis, tryFind Keys.Basis props
Keys.Order, tryFind Keys.Order props
Keys.Grow, tryFind Keys.Grow props
Keys.Shrink, tryFind Keys.Shrink props
Keys.FlexLayoutDirection, tryFind Keys.FlexLayoutDirection props
Keys.AlignSelf, tryFind Keys.AlignSelf props]
|> List.choose isDefined
|> List.fold (fun (el: ViewElement) (propName, propValue) ->
match propName with
| Keys.Basis -> el.Basis (unbox<FlexBasis> propValue)
| Keys.Order -> el.Order (int propValue)
| Keys.Grow -> el.Grow(double propValue)
| Keys.Shrink -> el.Shrink (single propValue)
| Keys.FlexLayoutDirection -> el.FlexLayoutDirection(unbox<FlexDirection> propValue)
| Keys.AlignSelf -> el.AlignSelf (unbox<FlexAlignSelf> propValue)
| _ -> el) element
let inline applyMarginSettings (map: Map<string, obj>) : Thickness =
let initialMargin =
Map.tryFind Keys.Margin map
|> Option.map unbox<Thickness>
|> Option.defaultValue (Thickness(0.0))
[ Keys.MarginLeft, tryFind Keys.MarginLeft map
Keys.MarginRight, tryFind Keys.MarginRight map
Keys.MarginTop, tryFind Keys.MarginTop map
Keys.MarginBottom, tryFind Keys.MarginBottom map ]
|> List.choose isDefined
|> List.fold (fun (current: Thickness) (propName, propValue : obj) ->
match propName with
| Keys.MarginLeft ->
let marginLeft = unbox<float> propValue
Thickness(marginLeft, current.Top, current.Right, current.Bottom)
| Keys.MarginRight ->
let marginRight = unbox<float> propValue
Thickness(current.Left, current.Top, marginRight, current.Bottom)
| Keys.MarginTop ->
let marginTop = unbox<float> propValue
Thickness(current.Left, marginTop, current.Right, current.Bottom)
| Keys.MarginBottom ->
let marginBottom = unbox<float> propValue
Thickness(current.Left, current.Top, current.Right, marginBottom)
| _ -> current) initialMargin
let inline applyPaddingSettings (map: Map<string, obj>) : Thickness =
let initialPadding =
Map.tryFind Keys.Padding map
|> Option.map unbox<Thickness>
|> Option.defaultValue (Thickness(0.0))
[ Keys.PaddingLeft, tryFind Keys.PaddingLeft map
Keys.PaddingRight, tryFind Keys.PaddingRight map
Keys.PaddingTop, tryFind Keys.PaddingTop map
Keys.PaddingBottom, tryFind Keys.PaddingBottom map ]
|> List.choose isDefined
|> List.fold (fun (current: Thickness) (propName, propValue : obj) ->
match propName with
| Keys.PaddingLeft ->
let paddingLeft = unbox<float> propValue
Thickness(paddingLeft, current.Top, current.Right, current.Bottom)
| Keys.PaddingRight ->
let paddingRight = unbox<float> propValue
Thickness(current.Left, current.Top, paddingRight, current.Bottom)
| Keys.PaddingTop ->
let paddingTop = unbox<float> propValue
Thickness(current.Left, paddingTop, current.Right, current.Bottom)
| Keys.PaddingBottom ->
let paddingBottom = unbox<float> propValue
Thickness(current.Left, current.Top, current.Right, paddingBottom)
| _ -> current) initialPadding