-
Notifications
You must be signed in to change notification settings - Fork 69
Expand file tree
/
Copy pathMain.elm
More file actions
executable file
·807 lines (616 loc) · 19.4 KB
/
Main.elm
File metadata and controls
executable file
·807 lines (616 loc) · 19.4 KB
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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
module Debugger.Main exposing
( cornerView
, getUserModel
, initialWindowHeight
, initialWindowWidth
, popoutView
, wrapInit
, wrapSubs
, wrapUpdate
)
import Bitwise
import Debugger.Expando as Expando exposing (Expando)
import Debugger.History as History exposing (History)
import Debugger.Metadata as Metadata exposing (Metadata)
import Debugger.Overlay as Overlay
import Debugger.Report as Report
import Dict exposing (Dict)
import Elm.Kernel.Debugger
import Html exposing (..)
import Html.Attributes as A exposing (..)
import Html.Events exposing (on, onClick, onInput, onMouseDown, onMouseUp)
import Html.Lazy exposing (lazy)
import Json.Decode as Decode exposing (Decoder)
import Json.Encode as Encode
import Task exposing (Task)
import VirtualDom as V
-- CONSTANTS
minimumPanelSize : Int
minimumPanelSize =
150
initialWindowWidth : Int
initialWindowWidth =
900
initialWindowHeight : Int
initialWindowHeight =
420
-- SUBSCRIPTIONS
wrapSubs : (model -> Sub msg) -> Model model msg -> Sub (Msg msg)
wrapSubs subscriptions model =
Sub.map UserMsg (subscriptions (getLatestModel model.state))
-- MODEL
type alias Model model msg =
{ history : History model msg
, state : State model msg
, expandoModel : Expando
, expandoMsg : Expando
, metadata : Result Metadata.Error Metadata
, overlay : Overlay.State
, popout : Popout
, layout : Layout
}
type Popout
= Popout Popout
type Layout
= Vertical DragStatus Float Float
| Horizontal DragStatus Float Float
type DragStatus
= Static
| Moving
getUserModel : Model model msg -> model
getUserModel model =
getCurrentModel model.state
-- STATE
type State model msg
= Running model
| Paused Int model model msg (History model msg)
getLatestModel : State model msg -> model
getLatestModel state =
case state of
Running model ->
model
Paused _ _ model _ _ ->
model
getCurrentModel : State model msg -> model
getCurrentModel state =
case state of
Running model ->
model
Paused _ model _ _ _ ->
model
isPaused : State model msg -> Bool
isPaused state =
case state of
Running _ ->
False
Paused _ _ _ _ _ ->
True
cachedHistory : Model model msg -> History model msg
cachedHistory model =
case model.state of
Running _ ->
model.history
Paused _ _ _ _ history ->
history
-- INIT
wrapInit : Encode.Value -> Popout -> (flags -> ( model, Cmd msg )) -> flags -> ( Model model msg, Cmd (Msg msg) )
wrapInit metadata popout init flags =
let
(userModel, userCommands) = init flags
in
( { history = History.empty userModel
, state = Running userModel
, expandoModel = Expando.init userModel
, expandoMsg = Expando.init ()
, metadata = Metadata.decode metadata
, overlay = Overlay.none
, popout = popout
, layout = Horizontal Static 0.3 0.5
}
, Cmd.map UserMsg userCommands
)
-- UPDATE
type Msg msg
= NoOp
| UserMsg msg
| TweakExpandoMsg Expando.Msg
| TweakExpandoModel Expando.Msg
| Resume
| Jump Int
| SliderJump Int
| Open
| Up
| Down
| Import
| Export
| Upload String
| OverlayMsg Overlay.Msg
--
| SwapLayout
| DragStart
| Drag DragInfo
| DragEnd
type alias UserUpdate model msg =
msg -> model -> ( model, Cmd msg )
wrapUpdate : UserUpdate model msg -> Msg msg -> Model model msg -> ( Model model msg, Cmd (Msg msg) )
wrapUpdate update msg model =
case msg of
NoOp ->
( model, Cmd.none )
UserMsg userMsg ->
let
userModel = getLatestModel model.state
newHistory = History.add userMsg userModel model.history
( newUserModel, userCmds ) = update userMsg userModel
commands = Cmd.map UserMsg userCmds
in
case model.state of
Running _ ->
( { model
| history = newHistory
, state = Running newUserModel
, expandoModel = Expando.merge newUserModel model.expandoModel
, expandoMsg = Expando.merge userMsg model.expandoMsg
}
, Cmd.batch [ commands, scroll model.popout ]
)
Paused index indexModel _ _ history ->
( { model
| history = newHistory
, state = Paused index indexModel newUserModel userMsg history
}
, commands
)
TweakExpandoMsg eMsg ->
( { model | expandoMsg = Expando.update eMsg model.expandoMsg }
, Cmd.none
)
TweakExpandoModel eMsg ->
( { model | expandoModel = Expando.update eMsg model.expandoModel }
, Cmd.none
)
Resume ->
case model.state of
Running _ ->
( model, Cmd.none )
Paused _ _ userModel userMsg _ ->
( { model
| state = Running userModel
, expandoMsg = Expando.merge userMsg model.expandoMsg
, expandoModel = Expando.merge userModel model.expandoModel
}
, scroll model.popout
)
Jump index ->
( jumpUpdate update index model
, Cmd.none
)
SliderJump index ->
( jumpUpdate update index model
, scrollTo (History.idForMessageIndex index) model.popout
)
Open ->
( model
, Task.perform (always NoOp) (Elm.Kernel.Debugger.open model.popout)
)
Up ->
case model.state of
Running _ ->
( model, Cmd.none )
Paused i _ _ _ history ->
let
targetIndex = i + 1
in
if targetIndex < History.size history then
wrapUpdate update (SliderJump targetIndex) model
else
wrapUpdate update Resume model
Down ->
case model.state of
Running _ ->
wrapUpdate update (Jump (History.size model.history - 1)) model
Paused index _ _ _ _ ->
if index > 0 then
wrapUpdate update (SliderJump (index - 1)) model
else
( model, Cmd.none )
Import ->
withGoodMetadata model <|
\_ -> ( model, upload model.popout )
Export ->
withGoodMetadata model <|
\metadata -> ( model, download metadata model.history )
Upload jsonString ->
withGoodMetadata model <|
\metadata ->
case Overlay.assessImport metadata jsonString of
Err newOverlay ->
( { model | overlay = newOverlay }, Cmd.none )
Ok rawHistory ->
loadNewHistory rawHistory update model
OverlayMsg overlayMsg ->
case Overlay.close overlayMsg model.overlay of
Nothing ->
( { model | overlay = Overlay.none }, Cmd.none )
Just rawHistory ->
loadNewHistory rawHistory update model
SwapLayout ->
( { model | layout = swapLayout model.layout }, Cmd.none )
DragStart ->
( { model | layout = setDragStatus Moving model.layout }, Cmd.none )
Drag info ->
( { model | layout = drag info model.layout }, Cmd.none )
DragEnd ->
( { model | layout = setDragStatus Static model.layout }, Cmd.none )
jumpUpdate : UserUpdate model msg -> Int -> Model model msg -> Model model msg
jumpUpdate update index model =
let
history =
cachedHistory model
currentMsg =
History.getRecentMsg history
currentModel =
getLatestModel model.state
( indexModel, indexMsg ) =
History.get update index history
in
{ model
| state = Paused index indexModel currentModel currentMsg history
, expandoModel = Expando.merge indexModel model.expandoModel
, expandoMsg = Expando.merge indexMsg model.expandoMsg
}
-- LAYOUT HELPERS
swapLayout : Layout -> Layout
swapLayout layout =
case layout of
Horizontal s x y -> Vertical s x y
Vertical s x y -> Horizontal s x y
setDragStatus : DragStatus -> Layout -> Layout
setDragStatus status layout =
case layout of
Horizontal _ x y -> Horizontal status x y
Vertical _ x y -> Vertical status x y
drag : DragInfo -> Layout -> Layout
drag info layout =
case layout of
Horizontal status _ y ->
Horizontal status (info.x / info.width) y
Vertical status x _ ->
Vertical status x (info.y / info.height)
-- COMMANDS
scroll : Popout -> Cmd (Msg msg)
scroll popout =
Task.perform (always NoOp) (Elm.Kernel.Debugger.scroll popout)
scrollTo : String -> Popout -> Cmd (Msg msg)
scrollTo id popout =
Task.perform (always NoOp) (Elm.Kernel.Debugger.scrollTo id popout)
upload : Popout -> Cmd (Msg msg)
upload popout =
Task.perform Upload (Elm.Kernel.Debugger.upload popout)
download : Metadata -> History model msg -> Cmd (Msg msg)
download metadata history =
let
historyLength = History.size history
in
Task.perform (\_ -> NoOp) <| Elm.Kernel.Debugger.download historyLength <|
Elm.Kernel.Json.unwrap <|
Encode.object
[ ( "metadata", Metadata.encode metadata )
, ( "history", History.encode history )
]
-- UPDATE OVERLAY
withGoodMetadata :
Model model msg
-> (Metadata -> ( Model model msg, Cmd (Msg msg) ))
-> ( Model model msg, Cmd (Msg msg) )
withGoodMetadata model func =
case model.metadata of
Ok metadata ->
func metadata
Err error ->
( { model | overlay = Overlay.badMetadata error }
, Cmd.none
)
loadNewHistory : Encode.Value -> UserUpdate model msg -> Model model msg -> ( Model model msg, Cmd (Msg msg) )
loadNewHistory rawHistory update model =
let
initialUserModel =
History.getInitialModel model.history
pureUserUpdate msg userModel =
Tuple.first (update msg userModel)
decoder =
History.decoder initialUserModel pureUserUpdate
in
case Decode.decodeValue decoder rawHistory of
Err _ ->
( { model | overlay = Overlay.corruptImport }
, Cmd.none
)
Ok ( latestUserModel, newHistory ) ->
( { model
| history = newHistory
, state = Running latestUserModel
, expandoModel = Expando.init latestUserModel
, expandoMsg = Expando.init (History.getRecentMsg newHistory)
, overlay = Overlay.none
}
, Cmd.none
)
-- CORNER VIEW
cornerView : Model model msg -> Html (Msg msg)
cornerView model =
Overlay.view
{ resume = Resume
, open = Open
, importHistory = Import
, exportHistory = Export
, wrap = OverlayMsg
}
(isPaused model.state)
(Elm.Kernel.Debugger.isOpen model.popout)
(History.size model.history)
model.overlay
toBlockerType : Model model msg -> Overlay.BlockerType
toBlockerType model =
Overlay.toBlockerType (isPaused model.state) model.overlay
-- BIG DEBUG VIEW
popoutView : Model model msg -> Html (Msg msg)
popoutView model =
let
maybeIndex =
case model.state of
Running _ -> Nothing
Paused index _ _ _ _ -> Just index
historyToRender = cachedHistory model
in
node "body"
(
toDragListeners model.layout
++
[ style "margin" "0"
, style "padding" "0"
, style "width" "100%"
, style "height" "100%"
, style "font-family" "monospace"
, style "display" "flex"
, style "background-color" "white"
, style "flex-direction" (toFlexDirection model.layout)
]
)
[ viewHistory maybeIndex historyToRender model.layout
, viewDragZone model.layout
, viewExpando model.expandoMsg model.expandoModel model.layout
]
toFlexDirection : Layout -> String
toFlexDirection layout =
case layout of
Horizontal _ _ _ -> "row"
Vertical _ _ _ -> "column-reverse"
-- DRAG LISTENERS
toDragListeners : Layout -> List (Attribute (Msg msg))
toDragListeners layout =
case getDragStatus layout of
Static -> []
Moving -> [ onMouseMove, onMouseUp DragEnd ]
getDragStatus : Layout -> DragStatus
getDragStatus layout =
case layout of
Horizontal status _ _ -> status
Vertical status _ _ -> status
type alias DragInfo =
{ x : Float
, y : Float
, down : Bool
, width : Float
, height : Float
}
onMouseMove : Attribute (Msg msg)
onMouseMove =
on "mousemove" <| Decode.map Drag <|
Decode.map5 DragInfo
(Decode.field "pageX" Decode.float)
(Decode.field "pageY" Decode.float)
(Decode.field "buttons" (Decode.map (\v -> v == 1) Decode.int))
(decodeDimension "innerWidth")
(decodeDimension "innerHeight")
decodeDimension : String -> Decoder Float
decodeDimension field =
Decode.at [ "currentTarget", "ownerDocument", "defaultView", field ] Decode.float
-- VIEW DRAG ZONE
viewDragZone : Layout -> Html (Msg msg)
viewDragZone layout =
case layout of
Horizontal _ x _ ->
div
[ style "position" "absolute"
, style "top" "0"
, style "left" (toPercent x)
, style "margin-left" "-5px"
, style "width" "10px"
, style "height" "100%"
, style "cursor" "col-resize"
, onMouseDown DragStart
]
[]
Vertical _ _ y ->
div
[ style "position" "absolute"
, style "top" (toPercent y)
, style "left" "0"
, style "margin-top" "-5px"
, style "width" "100%"
, style "height" "10px"
, style "cursor" "row-resize"
, onMouseDown DragStart
]
[]
-- LAYOUT HELPERS
toPercent : Float -> String
toPercent fraction =
String.fromFloat (100 * fraction) ++ "%"
toMouseBlocker : Layout -> String
toMouseBlocker layout =
case getDragStatus layout of
Static -> "auto"
Moving -> "none"
-- VIEW HISTORY
viewHistory : Maybe Int -> History model msg -> Layout -> Html (Msg msg)
viewHistory maybeIndex history layout =
let
(w,h) = toHistoryPercents layout
block = toMouseBlocker layout
in
div
[ style "width" w
, style "height" h
, style "display" "flex"
, style "flex-direction" "column"
, style "color" "#DDDDDD"
, style "background-color" "rgb(61, 61, 61)"
, style "pointer-events" block
, style "user-select" block
]
[ viewHistorySlider history maybeIndex
, Html.map Jump (History.view maybeIndex history)
, lazy viewHistoryOptions layout
]
toHistoryPercents : Layout -> (String, String)
toHistoryPercents layout =
case layout of
Horizontal _ x _ -> ( toPercent x, "100%" )
Vertical _ _ y -> ( "100%", toPercent (1 - y) )
viewHistorySlider : History model msg -> Maybe Int -> Html (Msg msg)
viewHistorySlider history maybeIndex =
let
lastIndex = History.size history - 1
selectedIndex = Maybe.withDefault lastIndex maybeIndex
in
div
[ style "display" "flex"
, style "flex-direction" "row"
, style "align-items" "center"
, style "width" "100%"
, style "height" "36px"
, style "background-color" "rgb(50, 50, 50)"
]
[ lazy viewPlayButton (isPlaying maybeIndex)
, input
[ type_ "range"
, style "width" "calc(100% - 56px)"
, style "height" "36px"
, style "margin" "0 10px"
, A.min "0"
, A.max (String.fromInt lastIndex)
, value (String.fromInt selectedIndex)
, onInput (String.toInt >> Maybe.withDefault lastIndex >> SliderJump)
]
[]
]
viewPlayButton : Bool -> Html (Msg msg)
viewPlayButton playing =
button
[ style "background" "#1293D8"
, style "border" "none"
, style "color" "white"
, style "cursor" "pointer"
, style "width" "36px"
, style "height" "36px"
, onClick Resume
]
[ if playing
then icon "M2 2h4v12h-4v-12z M10 2h4v12h-4v-12z"
else icon "M2 2l12 7l-12 7z"
]
isPlaying : Maybe Int -> Bool
isPlaying maybeIndex =
case maybeIndex of
Nothing -> True
Just _ -> False
viewHistoryOptions : Layout -> Html (Msg msg)
viewHistoryOptions layout =
div
[ style "width" "100%"
, style "height" "36px"
, style "display" "flex"
, style "flex-direction" "row"
, style "align-items" "center"
, style "justify-content" "space-between"
, style "background-color" "rgb(50, 50, 50)"
]
[ viewHistoryButton "Swap Layout" SwapLayout (toHistoryIcon layout)
, div
[ style "display" "flex"
, style "flex-direction" "row"
, style "align-items" "center"
, style "justify-content" "space-between"
]
[ viewHistoryButton "Import" Import "M5 1a1 1 0 0 1 0 2h-2a1 1 0 0 0-1 1v8a1 1 0 0 0 1 1h10a1 1 0 0 0 1-1a1 1 0 0 1 2 0a3 3 0 0 1-3 3h-10a3 3 0 0 1-3-3v-8a3 3 0 0 1 3-3z M10 2a1 1 0 0 0 -2 0v6a1 1 0 0 0 1 1h6a1 1 0 0 0 0-2h-3.586l4.293-4.293a1 1 0 0 0-1.414-1.414l-4.293 4.293z"
, viewHistoryButton "Export" Export "M5 1a1 1 0 0 1 0 2h-2a1 1 0 0 0-1 1v8a1 1 0 0 0 1 1h10a1 1 0 0 0 1-1 a1 1 0 0 1 2 0a3 3 0 0 1-3 3h-10a3 3 0 0 1-3-3v-8a3 3 0 0 1 3-3z M9 3a1 1 0 1 1 0-2h6a1 1 0 0 1 1 1v6a1 1 0 1 1-2 0v-3.586l-5.293 5.293 a1 1 0 0 1-1.414-1.414l5.293 -5.293z"
]
]
viewHistoryButton : String -> msg -> String -> Html msg
viewHistoryButton label msg path =
button
[ style "display" "flex"
, style "flex-direction" "row"
, style "align-items" "center"
, style "background" "none"
, style "border" "none"
, style "color" "inherit"
, style "cursor" "pointer"
, onClick msg
]
[ icon path
, span [ style "padding-left" "6px" ] [ text label ]
]
icon : String -> Html msg
icon path =
V.nodeNS "http://www.w3.org/2000/svg" "svg"
[ V.attribute "viewBox" "0 0 16 16"
, V.attribute "xmlns" "http://www.w3.org/2000/svg"
, V.attribute "fill" "currentColor"
, V.attribute "width" "16px"
, V.attribute "height" "16px"
]
[ V.nodeNS "http://www.w3.org/2000/svg" "path"
[ V.attribute "d" path ]
[]
]
toHistoryIcon : Layout -> String
toHistoryIcon layout =
case layout of
Horizontal _ _ _ -> "M13 1a3 3 0 0 1 3 3v8a3 3 0 0 1-3 3h-10a3 3 0 0 1-3-3v-8a3 3 0 0 1 3-3z M13 3h-10a1 1 0 0 0-1 1v5h12v-5a1 1 0 0 0-1-1z M14 10h-12v2a1 1 0 0 0 1 1h10a1 1 0 0 0 1-1z"
Vertical _ _ _ -> "M0 4a3 3 0 0 1 3-3h10a3 3 0 0 1 3 3v8a3 3 0 0 1-3 3h-10a3 3 0 0 1-3-3z M2 4v8a1 1 0 0 0 1 1h2v-10h-2a1 1 0 0 0-1 1z M6 3v10h7a1 1 0 0 0 1-1v-8a1 1 0 0 0-1-1z"
-- VIEW EXPANDO
viewExpando : Expando -> Expando -> Layout -> Html (Msg msg)
viewExpando expandoMsg expandoModel layout =
let
(w,h) = toExpandoPercents layout
block = toMouseBlocker layout
in
div
[ style "display" "block"
, style "width" ("calc(" ++ w ++ " - 4em)")
, style "height" ("calc(" ++ h ++ " - 4em)")
, style "padding" "2em"
, style "margin" "0"
, style "overflow" "auto"
, style "pointer-events" block
, style "-webkit-user-select" block
, style "-moz-user-select" block
, style "-ms-user-select" block
, style "user-select" block
]
[ div [ style "color" "#ccc", style "padding" "0 0 1em 0" ] [ text "-- MESSAGE" ]
, Html.map TweakExpandoMsg <| Expando.view Nothing expandoMsg
, div [ style "color" "#ccc", style "padding" "1em 0" ] [ text "-- MODEL" ]
, Html.map TweakExpandoModel <| Expando.view Nothing expandoModel
]
toExpandoPercents : Layout -> (String, String)
toExpandoPercents layout =
case layout of
Horizontal _ x _ -> ( toPercent (1 - x), "100%" )
Vertical _ _ y -> ( "100%", toPercent y )
-- No case/let/if in parentheses. Always new top-level function.
-- No case/let/if within a let. Always new top-level function.
-- Replace "creating variable to use in different branches" with "same function call in different branches"