-
-
Notifications
You must be signed in to change notification settings - Fork 71
/
Copy pathProgram.fs
314 lines (234 loc) · 8.37 KB
/
Program.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
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
module Elmish.WPF.Samples.SubModelSeq.Program
open System
open Serilog
open Serilog.Extensions.Logging
open Elmish.WPF
type InOutMsg<'a, 'b> =
| InMsg of 'a
| OutMsg of 'b
module Option =
let set a = Option.map (fun _ -> a)
module Func =
let flip f b a = f a b
module FuncOption =
let inputIfNone f a = a |> f |> Option.defaultValue a
let map (f: 'b -> 'c) (mb: 'a -> 'b option) =
mb >> Option.map f
let bind (f: 'b -> 'a -> 'c) (mb: 'a -> 'b option) a =
mb a |> Option.bind (fun b -> Some(f b a))
let map get set f a =
a |> get |> f |> Func.flip set a
module List =
let swap i j =
List.permute
(function
| a when a = i -> j
| a when a = j -> i
| a -> a)
let swapWithNext i = swap i (i + 1)
let swapWithPrev i = swap i (i - 1)
let cons head tail = head :: tail
let mapFirst p f input =
let rec mapFirstRec reverseFront back =
match back with
| [] ->
(*
* Conceptually, the correct value to return is
* reverseFront |> List.rev
* but this is the same as
* input
* so returning that instead.
*)
input
| a :: ma ->
if p a then
(reverseFront |> List.rev) @ (f a :: ma)
else
mapFirstRec (a :: reverseFront) ma
mapFirstRec [] input
[<AutoOpen>]
module Identifiable =
type Identifiable<'a> =
{ Id: Guid
Value: 'a }
module Identifiable =
let getId m = m.Id
let get m = m.Value
let set v m = { m with Value = v }
let map f = f |> map get set
[<AutoOpen>]
module Counter =
type Counter =
{ Count: int
StepSize: int }
type CounterMsg =
| Increment
| Decrement
| SetStepSize of int
| Reset
module Counter =
let init =
{ Count = 0
StepSize = 1 }
let canReset = (<>) init
let update msg m =
match msg with
| Increment -> { m with Count = m.Count + m.StepSize }
| Decrement -> { m with Count = m.Count - m.StepSize }
| SetStepSize x -> { m with StepSize = x }
| Reset -> init
let bindings () : Binding<Counter, CounterMsg> list = [
"CounterValue" |> Binding.oneWay (fun m -> m.Count)
"Increment" |> Binding.cmd Increment
"Decrement" |> Binding.cmd Decrement
"StepSize" |> Binding.twoWay(
(fun m -> float m.StepSize),
int >> SetStepSize)
"Reset" |> Binding.cmdIf(Reset, canReset)
]
[<AutoOpen>]
module RoseTree =
type RoseTree<'model> =
{ Data: 'model
Children: RoseTree<'model> list }
type RoseTreeMsg<'a, 'msg> =
| BranchMsg of 'a * RoseTreeMsg<'a, 'msg>
| LeafMsg of 'msg
module RoseTree =
let create data children =
{ Data = data
Children = children }
let createLeaf a = create a []
let getData t = t.Data
let setData (d: 'a) (t: RoseTree<'a>) = { t with Data = d }
let mapData f = map getData setData f
let getChildren t = t.Children
let setChildren c t = { t with Children = c }
let mapChildren f = map getChildren setChildren f
let addSubtree t = t |> List.cons |> mapChildren
let addChildData a = a |> createLeaf |> addSubtree
let update p (f: 'msg -> RoseTree<'model> -> RoseTree<'model>) =
let rec updateRec = function
| BranchMsg (a, msg) -> msg |> updateRec |> List.mapFirst (p a) |> mapChildren
| LeafMsg msg -> msg |> f
updateRec
module App =
type Model =
{ SomeGlobalState: bool
DummyRoot: RoseTree<Identifiable<Counter>> }
type SubtreeMsg =
| CounterMsg of CounterMsg
| AddChild
| Remove of Guid
| MoveUp of Guid
| MoveDown of Guid
type SubtreeOutMsg =
| OutRemove
| OutMoveUp
| OutMoveDown
type Msg =
| ToggleGlobalState
| SubtreeMsg of RoseTreeMsg<Guid, SubtreeMsg>
let getSomeGlobalState m = m.SomeGlobalState
let setSomeGlobalState v m = { m with SomeGlobalState = v }
let mapSomeGlobalState f = f |> map getSomeGlobalState setSomeGlobalState
let getDummyRoot m = m.DummyRoot
let setDummyRoot v m = { m with DummyRoot = v }
let mapDummyRoot f = f |> map getDummyRoot setDummyRoot
let createNewIdentifiableCounter () =
{ Id = Guid.NewGuid ()
Value = Counter.init }
let createNewLeaf () =
createNewIdentifiableCounter ()
|> RoseTree.createLeaf
let init () =
let dummyRootData = createNewIdentifiableCounter () // Placeholder data to satisfy type system. User never sees this.
{ SomeGlobalState = false
DummyRoot =
createNewLeaf ()
|> List.singleton
|> RoseTree.create dummyRootData }
let hasId id t = t.Data.Id = id
let swapCounters swap nId =
nId
|> hasId
|> List.tryFindIndex
|> FuncOption.bind swap
|> FuncOption.inputIfNone
let updateSubtree = function
| CounterMsg msg -> msg |> Counter.update |> Identifiable.map |> RoseTree.mapData
| AddChild -> createNewLeaf () |> List.cons |> RoseTree.mapChildren
| Remove cId -> cId |> hasId >> not |> List.filter |> RoseTree.mapChildren
| MoveUp cId -> cId |> swapCounters List.swapWithPrev |> RoseTree.mapChildren
| MoveDown cId -> cId |> swapCounters List.swapWithNext |> RoseTree.mapChildren
let update = function
| ToggleGlobalState -> mapSomeGlobalState not
| SubtreeMsg msg -> msg |> RoseTree.update hasId updateSubtree |> mapDummyRoot
let mapOutMsg = function
| OutRemove -> Remove
| OutMoveUp -> MoveUp
| OutMoveDown -> MoveDown
module Bindings =
open App
type SelfWithParent<'a> =
{ Self: 'a
Parent: 'a }
let moveUpMsg (_, { Parent = p; Self = s }) =
match p.Children |> List.tryHead with
| Some c when c.Data.Id <> s.Data.Id ->
OutMoveUp |> Some
| _ -> None
let moveDownMsg (_, { Parent = p; Self = s }) =
match p.Children |> List.tryLast with
| Some c when c.Data.Id <> s.Data.Id ->
OutMoveDown |> Some
| _ -> None
let rec subtreeBindings () : Binding<Model * SelfWithParent<RoseTree<Identifiable<Counter>>>, InOutMsg<RoseTreeMsg<Guid, SubtreeMsg>, SubtreeOutMsg>> list =
let counterBindings =
Counter.bindings ()
|> Bindings.mapModel (fun (_, { Self = s }) -> s.Data.Value)
|> Bindings.mapMsg (CounterMsg >> LeafMsg)
let inMsgBindings =
[ "CounterIdText" |> Binding.oneWay(fun (_, { Self = s }) -> s.Data.Id)
"AddChild" |> Binding.cmd(AddChild |> LeafMsg)
"GlobalState" |> Binding.oneWay(fun (m, _) -> m.SomeGlobalState)
"ChildCounters"
|> Binding.subModelSeq (subtreeBindings, (fun (_, { Self = c }) -> c.Data.Id))
|> Binding.mapModel (fun (m, { Self = p }) -> p.Children |> Seq.map (fun c -> m, { Self = c; Parent = p }))
|> Binding.mapMsg (fun (cId, inOutMsg) ->
match inOutMsg with
| InMsg msg -> (cId, msg) |> BranchMsg
| OutMsg msg -> cId |> mapOutMsg msg |> LeafMsg)
] @ counterBindings
|> Bindings.mapMsg InMsg
let outMsgBindings =
[ "Remove" |> Binding.cmd OutRemove
"MoveUp" |> Binding.cmdIf moveUpMsg
"MoveDown" |> Binding.cmdIf moveDownMsg
] |> Bindings.mapMsg OutMsg
outMsgBindings @ inMsgBindings
let rootBindings () : Binding<Model, Msg> list = [
"Counters"
|> Binding.subModelSeq (subtreeBindings, (fun (_, { Self = c }) -> c.Data.Id))
|> Binding.mapModel (fun m -> m.DummyRoot.Children |> Seq.map (fun c -> m, { Self = c; Parent = m.DummyRoot }))
|> Binding.mapMsg (fun (cId, inOutMsg) ->
match inOutMsg with
| InMsg msg -> (cId, msg) |> BranchMsg
| OutMsg msg -> cId |> mapOutMsg msg |> LeafMsg
|> SubtreeMsg)
"ToggleGlobalState" |> Binding.cmd ToggleGlobalState
"AddCounter" |> Binding.cmd (AddChild |> LeafMsg |> SubtreeMsg)
]
let counterDesignVm = ViewModel.designInstance Counter.init (Counter.bindings ())
let mainDesignVm = ViewModel.designInstance (App.init ()) (Bindings.rootBindings ())
let main window =
let logger =
LoggerConfiguration()
.MinimumLevel.Override("Elmish.WPF.Update", Events.LogEventLevel.Verbose)
.MinimumLevel.Override("Elmish.WPF.Bindings", Events.LogEventLevel.Verbose)
.MinimumLevel.Override("Elmish.WPF.Performance", Events.LogEventLevel.Verbose)
.WriteTo.Console()
.CreateLogger()
WpfProgram.mkSimple App.init App.update Bindings.rootBindings
|> WpfProgram.withLogger (new SerilogLoggerFactory(logger))
|> WpfProgram.startElmishLoop window