Source file mosaic.ml

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
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
(** TEA (The Elm Architecture) runtime for mosaic. *)

open Mosaic_ui

(* External re-exports *)

module Ansi = Matrix.Ansi
module Border = Matrix.Grid.Border
module Event = Mosaic_ui.Event
module Canvas = Mosaic_ui.Canvas

(* Geometry type aliases *)

type 'a size = 'a Toffee.Geometry.Size.t = { width : 'a; height : 'a }

type 'a rect = 'a Toffee.Geometry.Rect.t = {
  left : 'a;
  right : 'a;
  top : 'a;
  bottom : 'a;
}

type 'a point = 'a Toffee.Geometry.Point.t = { x : 'a; y : 'a }
type 'a line = 'a Toffee.Geometry.Line.t = { start : 'a; end_ : 'a }
type dimension = Toffee.Style.Dimension.t
type length_percentage = Toffee.Style.Length_percentage.t
type length_percentage_auto = Toffee.Style.Length_percentage_auto.t
type span = Mosaic_ui.Text_buffer.span = { text : string; style : Ansi.Style.t }

(* Layout enum modules *)

module Display = Toffee.Style.Display
module Position = Toffee.Style.Position
module Box_sizing = Toffee.Style.Box_sizing
module Overflow = Toffee.Style.Overflow
module Text_align = Toffee.Style.Text_align
module Flex_direction = Toffee.Style.Flex_direction
module Flex_wrap = Toffee.Style.Flex_wrap
module Align = Toffee.Style.Align_items
module Justify = Toffee.Style.Align_content
module Grid_auto_flow = Toffee.Style.Grid_auto_flow

(* Grid module *)

module Grid = struct
  type template = Toffee.Style.Grid_template_component.t

  let fr = Toffee.Style.Grid_template_component.fr
  let length = Toffee.Style.Grid_template_component.length
  let percent = Toffee.Style.Grid_template_component.percent
  let auto = Toffee.Style.Grid_template_component.auto
  let min_content = Toffee.Style.Grid_template_component.min_content
  let max_content = Toffee.Style.Grid_template_component.max_content
  let fit_content = Toffee.Style.Grid_template_component.fit_content
  let minmax = Toffee.Style.Grid_template_component.minmax

  type placement = Toffee.Style.Grid_placement.t

  let line = Toffee.Style.Grid_placement.line
  let span = Toffee.Style.Grid_placement.span
  let auto_placement = Toffee.Style.Grid_placement.auto

  let line_range s e : placement Toffee.Geometry.Line.t =
    {
      start = Toffee.Style.Grid_placement.line s;
      end_ = Toffee.Style.Grid_placement.line e;
    }

  let span_range s n : placement Toffee.Geometry.Line.t =
    {
      start = Toffee.Style.Grid_placement.line s;
      end_ = Toffee.Style.Grid_placement.span n;
    }

  type track = Toffee.Style.Track_sizing_function.t

  let track_fr = Toffee.Style.Track_sizing_function.fr
  let track_length = Toffee.Style.Track_sizing_function.length
  let track_percent = Toffee.Style.Track_sizing_function.percent
  let track_auto = Toffee.Style.Track_sizing_function.auto
  let track_min_content = Toffee.Style.Track_sizing_function.min_content
  let track_max_content = Toffee.Style.Track_sizing_function.max_content

  type area = Toffee.Style.Grid_template_area.t
end

(* Narrowed widget modules *)

module Select = Mosaic_ui.Select
module Tab_select = Mosaic_ui.Tab_select
module Table = Mosaic_ui.Table
module Tree = Mosaic_ui.Tree
module Spinner = Mosaic_ui.Spinner
module Slider = Mosaic_ui.Slider
module Scroll_bar = Mosaic_ui.Scroll_bar
module Text_surface = Mosaic_ui.Text_surface
module Line_number = Mosaic_ui.Line_number
module Markdown = Mosaic_ui.Markdown
module Syntax_theme = Mosaic_ui.Syntax_theme

(* Internal modules for tests *)

module Reconciler = Reconciler

(* TEA view: handlers return ['msg option]. *)
type 'msg t = 'msg option Vnode.t

let map (f : 'a -> 'b) (view : 'a t) : 'b t = Vnode.map (Option.map f) view

let compile ~(dispatch : 'msg -> unit) (view : 'msg t) : unit Vnode.t =
  Vnode.map (function Some msg -> dispatch msg | None -> ()) view

module Cmd = struct
  type 'msg t =
    | None
    | Batch of 'msg t list
    | Perform of (('msg -> unit) -> unit)
    | Quit
    | Set_title of string
    | Focus of string
    | Static_commit of 'msg option Vnode.t
    | Static_clear

  let none = None
  let batch cmds = Batch cmds
  let perform f = Perform f
  let quit = Quit
  let set_title title = Set_title title
  let focus id = Focus id
  let static_commit view = Static_commit view
  let static_clear = Static_clear

  let rec map (f : 'a -> 'b) (cmd : 'a t) : 'b t =
    match cmd with
    | None -> None
    | Batch cmds -> Batch (List.map (map f) cmds)
    | Perform g -> Perform (fun dispatch -> g (fun msg -> dispatch (f msg)))
    | Quit -> Quit
    | Set_title title -> Set_title title
    | Focus id -> Focus id
    | Static_commit view -> Static_commit (Vnode.map (Option.map f) view)
    | Static_clear -> Static_clear
end

module Sub = struct
  type 'msg t =
    | None
    | Batch of 'msg t list
    | Every of float * (unit -> 'msg)
    | On_tick of (dt:float -> 'msg)
    | On_key of (Event.key -> 'msg option)
    | On_key_all of (Event.key -> 'msg option)
    | On_mouse of (Event.mouse -> 'msg option)
    | On_mouse_all of (Event.mouse -> 'msg option)
    | On_paste of (Event.paste -> 'msg option)
    | On_paste_all of (Event.paste -> 'msg option)
    | On_resize of (width:int -> height:int -> 'msg)
    | On_focus of 'msg
    | On_blur of 'msg

  let none = None
  let batch subs = Batch subs
  let every interval f = Every (interval, f)
  let on_tick f = On_tick f
  let on_key f = On_key f
  let on_key_all f = On_key_all f
  let on_mouse f = On_mouse f
  let on_mouse_all f = On_mouse_all f
  let on_paste f = On_paste f
  let on_paste_all f = On_paste_all f
  let on_resize f = On_resize f
  let on_focus msg = On_focus msg
  let on_blur msg = On_blur msg

  let rec map (f : 'a -> 'b) (sub : 'a t) : 'b t =
    match sub with
    | None -> None
    | Batch subs -> Batch (List.map (map f) subs)
    | Every (interval, g) -> Every (interval, fun () -> f (g ()))
    | On_tick g -> On_tick (fun ~dt -> f (g ~dt))
    | On_key g -> On_key (fun ev -> Option.map f (g ev))
    | On_key_all g -> On_key_all (fun ev -> Option.map f (g ev))
    | On_mouse g -> On_mouse (fun ev -> Option.map f (g ev))
    | On_mouse_all g -> On_mouse_all (fun ev -> Option.map f (g ev))
    | On_paste g -> On_paste (fun ev -> Option.map f (g ev))
    | On_paste_all g -> On_paste_all (fun ev -> Option.map f (g ev))
    | On_resize g -> On_resize (fun ~width ~height -> f (g ~width ~height))
    | On_focus msg -> On_focus (f msg)
    | On_blur msg -> On_blur (f msg)
end

type ('model, 'msg) app = {
  init : unit -> 'model * 'msg Cmd.t;
  update : 'msg -> 'model -> 'model * 'msg Cmd.t;
  view : 'model -> 'msg t;
  subscriptions : 'model -> 'msg Sub.t;
}

type ('model, 'msg) runtime = {
  mutable model : 'model;
  mutable pending_msgs : 'msg list;
  mutable pending_focus : string list;
  app : ('model, 'msg) app;
  matrix_app : Matrix.app;
  process_perform : (unit -> unit) -> unit;
  renderer : Renderer.t;
  reconciler : Reconciler.t;
  mutable key_subs : (bool * (Event.key -> 'msg option)) list;
  mutable mouse_subs : (bool * (Event.mouse -> 'msg option)) list;
  mutable paste_subs : (bool * (Event.paste -> 'msg option)) list;
  mutable resize_sub : (width:int -> height:int -> 'msg) option;
  mutable tick_sub : (dt:float -> 'msg) option;
  mutable every_subs : (float * float * (unit -> 'msg)) list;
  mutable focus_sub : 'msg option;
  mutable blur_sub : 'msg option;
  mutable sub_live_active : bool;
}

let rec find_by_id_in (node : Renderable.t) (id : string) : Renderable.t option
    =
  if String.equal (Renderable.id node) id then Some node
  else
    List.find_map
      (fun child -> find_by_id_in child id)
      (Renderable.children node)

let find_by_id runtime id = find_by_id_in (Renderer.root runtime.renderer) id

let try_focus runtime id =
  match find_by_id runtime id with
  | Some node -> Renderer.focus runtime.renderer node
  | None -> false

let enqueue_focus runtime id =
  if
    not
      (List.exists
         (fun existing -> String.equal existing id)
         runtime.pending_focus)
  then runtime.pending_focus <- runtime.pending_focus @ [ id ]

let process_pending_focus runtime =
  let focused = ref false in
  let remaining =
    List.filter
      (fun id ->
        match find_by_id runtime id with
        | Some node ->
            let did_focus = Renderer.focus runtime.renderer node in
            if did_focus then focused := true;
            not did_focus
        | None -> true)
      runtime.pending_focus
  in
  runtime.pending_focus <- remaining;
  if !focused then Matrix.request_redraw runtime.matrix_app

let set_renderer_viewport (renderer : Renderer.t) ~width ~height =
  let root = Renderer.root renderer in
  let style =
    Renderable.style root
    |> Toffee.Style.set_display Toffee.Style.Display.Block
    |> Toffee.Style.set_width
         (Toffee.Style.Dimension.length (Float.of_int width))
    |> Toffee.Style.set_height
         (Toffee.Style.Dimension.length (Float.of_int height))
  in
  Renderable.set_style root style

let serialize_grid_rows (grid : Matrix.Grid.t) ~rows =
  if rows <= 0 then ""
  else
    let width = Matrix.Grid.width grid in
    if rows = Matrix.Grid.height grid then Matrix.Grid.to_ansi ~reset:false grid
    else
      let cropped =
        Matrix.Grid.create ~width ~height:rows
          ~glyph_pool:(Matrix.Grid.glyph_pool grid)
          ~width_method:(Matrix.Grid.width_method grid)
          ()
      in
      Matrix.Grid.blit_region ~src:grid ~dst:cropped ~src_x:0 ~src_y:0 ~width
        ~height:rows ~dst_x:0 ~dst_y:0;
      Matrix.Grid.to_ansi ~reset:false cropped

let render_static_view runtime (view : _ t) =
  let width, _ = Matrix.size runtime.matrix_app in
  let _, full_height = Matrix.full_size runtime.matrix_app in
  let width = max 1 width in
  let max_height = 100_000 in
  let rec render_with_height height =
    let renderer = Renderer.create () in
    let reconciler = Reconciler.create ~container:(Renderer.root renderer) in
    let vnode = compile ~dispatch:(fun _ -> ()) view in
    set_renderer_viewport renderer ~width ~height;
    Reconciler.render reconciler vnode;
    Renderer.render_frame renderer ~width ~height ~delta:0.;
    let grid = Matrix.Screen.grid (Renderer.screen renderer) in
    let used_rows = Matrix.Grid.active_height grid in
    if used_rows >= height && height < max_height then
      render_with_height (min (height * 2) max_height)
    else (serialize_grid_rows grid ~rows:used_rows, used_rows)
  in
  render_with_height (max 1 full_height)

let rec process_cmd runtime (cmd : _ Cmd.t) =
  match cmd with
  | Cmd.None -> ()
  | Cmd.Batch cmds -> List.iter (process_cmd runtime) cmds
  | Cmd.Perform f ->
      let dispatch msg =
        runtime.pending_msgs <- msg :: runtime.pending_msgs;
        Matrix.request_redraw runtime.matrix_app
      in
      runtime.process_perform (fun () -> f dispatch)
  | Cmd.Quit -> Matrix.stop runtime.matrix_app
  | Cmd.Set_title title ->
      let term = Matrix.terminal runtime.matrix_app in
      Matrix.Terminal.set_title term title
  | Cmd.Focus id ->
      if not (try_focus runtime id) then (
        enqueue_focus runtime id;
        Matrix.request_redraw runtime.matrix_app)
  | Cmd.Static_commit view ->
      let text, rows = render_static_view runtime view in
      Matrix.static_write runtime.matrix_app ~rows text
  | Cmd.Static_clear -> Matrix.static_clear runtime.matrix_app

let rec collect_subs runtime (sub : _ Sub.t) =
  match sub with
  | Sub.None -> ()
  | Sub.Batch subs -> List.iter (collect_subs runtime) subs
  | Sub.Every (interval, f) ->
      runtime.every_subs <- (interval, 0., f) :: runtime.every_subs
  | Sub.On_tick f -> runtime.tick_sub <- Some f
  | Sub.On_key f -> runtime.key_subs <- (false, f) :: runtime.key_subs
  | Sub.On_key_all f -> runtime.key_subs <- (true, f) :: runtime.key_subs
  | Sub.On_mouse f -> runtime.mouse_subs <- (false, f) :: runtime.mouse_subs
  | Sub.On_mouse_all f -> runtime.mouse_subs <- (true, f) :: runtime.mouse_subs
  | Sub.On_paste f -> runtime.paste_subs <- (false, f) :: runtime.paste_subs
  | Sub.On_paste_all f -> runtime.paste_subs <- (true, f) :: runtime.paste_subs
  | Sub.On_resize f -> runtime.resize_sub <- Some f
  | Sub.On_focus msg -> runtime.focus_sub <- Some msg
  | Sub.On_blur msg -> runtime.blur_sub <- Some msg

let update_subscriptions runtime =
  let prev_every = runtime.every_subs in
  runtime.key_subs <- [];
  runtime.mouse_subs <- [];
  runtime.paste_subs <- [];
  runtime.resize_sub <- None;
  runtime.tick_sub <- None;
  runtime.every_subs <- [];
  runtime.focus_sub <- None;
  runtime.blur_sub <- None;
  collect_subs runtime (runtime.app.subscriptions runtime.model);
  (* Preserve accumulated elapsed time for every_subs that were recollected with
     matching intervals. This prevents time resets when subscriptions are
     re-evaluated (e.g. after dispatch from handle_tick). *)
  runtime.every_subs <-
    List.map
      (fun (interval, _new_elapsed, f) ->
        let prev_elapsed =
          List.fold_left
            (fun acc (prev_iv, prev_el, _) ->
              if acc = 0. && Float.equal prev_iv interval then prev_el else acc)
            0. prev_every
        in
        (interval, prev_elapsed, f))
      runtime.every_subs;
  (* Track subscription-driven liveness: tick and every subscriptions
     require the render cadence to be running. *)
  let has_live_subs =
    runtime.tick_sub <> None || runtime.every_subs <> []
  in
  if has_live_subs && not runtime.sub_live_active then (
    runtime.sub_live_active <- true;
    Matrix.request_live runtime.matrix_app)
  else if (not has_live_subs) && runtime.sub_live_active then (
    runtime.sub_live_active <- false;
    Matrix.drop_live runtime.matrix_app)

let dispatch runtime msg =
  let model', cmd = runtime.app.update msg runtime.model in
  runtime.model <- model';
  process_cmd runtime cmd;
  update_subscriptions runtime

let process_pending_msgs runtime =
  while runtime.pending_msgs <> [] do
    let msgs = List.rev runtime.pending_msgs in
    runtime.pending_msgs <- [];
    List.iter (dispatch runtime) msgs
  done

let handle_key runtime (event : Event.key) =
  let consumed = Event.Key.default_prevented event in
  List.iter
    (fun (all_events, f) ->
      if all_events || not consumed then
        match f event with Some msg -> dispatch runtime msg | None -> ())
    runtime.key_subs

let handle_mouse runtime (event : Event.mouse) =
  let consumed = Event.Mouse.default_prevented event in
  List.iter
    (fun (all_events, f) ->
      if all_events || not consumed then
        match f event with Some msg -> dispatch runtime msg | None -> ())
    runtime.mouse_subs

let handle_paste runtime (event : Event.paste) =
  let consumed = Event.Paste.default_prevented event in
  List.iter
    (fun (all_events, f) ->
      if all_events || not consumed then
        match f event with Some msg -> dispatch runtime msg | None -> ())
    runtime.paste_subs

let handle_resize runtime ~width ~height =
  match runtime.resize_sub with
  | Some f -> dispatch runtime (f ~width ~height)
  | None -> ()

let handle_tick runtime ~dt =
  match runtime.tick_sub with
  | Some f ->
      runtime.pending_msgs <- f ~dt :: runtime.pending_msgs;
      Matrix.request_redraw runtime.matrix_app
  | None -> ()

let handle_every_subs runtime ~dt =
  runtime.every_subs <-
    List.map
      (fun (interval, elapsed, f) ->
        let new_elapsed = elapsed +. dt in
        if new_elapsed >= interval then begin
          runtime.pending_msgs <- f () :: runtime.pending_msgs;
          Matrix.request_redraw runtime.matrix_app;
          (interval, new_elapsed -. interval, f)
        end
        else (interval, new_elapsed, f))
      runtime.every_subs

let handle_input runtime (input : Matrix.Input.t) =
  match input with
  | Matrix.Input.Focus -> (
      match runtime.focus_sub with
      | Some msg -> dispatch runtime msg
      | None -> ())
  | Matrix.Input.Blur -> (
      match runtime.blur_sub with
      | Some msg -> dispatch runtime msg
      | None -> ())
  | Matrix.Input.Key key_event ->
      let ev = Renderer.dispatch_key runtime.renderer key_event in
      handle_key runtime ev
  | Matrix.Input.Mouse mouse_event ->
      Renderer.dispatch_mouse runtime.renderer mouse_event;
      let map_button = function
        | Matrix.Input.Mouse.Left -> Event.Mouse.Left
        | Matrix.Input.Mouse.Right -> Event.Mouse.Right
        | Matrix.Input.Mouse.Middle -> Event.Mouse.Middle
        | Matrix.Input.Mouse.Button n -> Event.Mouse.Button n
        | Matrix.Input.Mouse.Wheel_up | Matrix.Input.Mouse.Wheel_down
        | Matrix.Input.Mouse.Wheel_left | Matrix.Input.Mouse.Wheel_right ->
            Event.Mouse.Left
      in
      let ev =
        match mouse_event with
        | Matrix.Input.Mouse.Button_press (x, y, btn, mods) ->
            Event.Mouse.make ~x ~y ~modifiers:mods
              (Event.Mouse.Down { button = map_button btn })
        | Matrix.Input.Mouse.Button_release (x, y, btn, mods) ->
            Event.Mouse.make ~x ~y ~modifiers:mods
              (Event.Mouse.Up { button = map_button btn; is_dragging = false })
        | Matrix.Input.Mouse.Motion (x, y, state, mods) ->
            if state.left || state.middle || state.right then
              let button =
                if state.left then Event.Mouse.Left
                else if state.middle then Event.Mouse.Middle
                else Event.Mouse.Right
              in
              Event.Mouse.make ~x ~y ~modifiers:mods
                (Event.Mouse.Drag { button; is_dragging = true })
            else Event.Mouse.make ~x ~y ~modifiers:mods Event.Mouse.Move
      in
      handle_mouse runtime ev
  | Matrix.Input.Scroll (x, y, dir, delta, modifiers) ->
      let direction : Event.Mouse.scroll_direction =
        match dir with
        | Matrix.Input.Mouse.Scroll_up -> Scroll_up
        | Matrix.Input.Mouse.Scroll_down -> Scroll_down
        | Matrix.Input.Mouse.Scroll_left -> Scroll_left
        | Matrix.Input.Mouse.Scroll_right -> Scroll_right
      in
      Renderer.dispatch_scroll runtime.renderer ~x ~y ~direction ~delta
        ~modifiers;
      let ev =
        Event.Mouse.make ~x ~y ~modifiers
          (Event.Mouse.Scroll { direction; delta })
      in
      handle_mouse runtime ev
  | Matrix.Input.Paste text ->
      Renderer.dispatch_paste runtime.renderer text;
      let ev = Event.Paste.of_text text in
      handle_paste runtime ev
  | _ -> ()

let render runtime =
  process_pending_msgs runtime;
  let view = runtime.app.view runtime.model in
  let dispatch msg =
    runtime.pending_msgs <- msg :: runtime.pending_msgs;
    Matrix.request_redraw runtime.matrix_app
  in
  let vnode = compile ~dispatch view in
  Reconciler.render runtime.reconciler vnode

let run ?matrix
    ?(process_perform = fun thunk -> ignore (Thread.create thunk () : Thread.t))
    app =
  let matrix_app =
    match matrix with
    | Some matrix -> matrix
    | None ->
        Matrix.create ~target_fps:(Some 60.) ~cursor_visible:false
          ~start_idle:true ()
  in
  let model, init_cmd = app.init () in
  let base_grid = Matrix.grid matrix_app in
  let renderer_style =
    match Matrix.mode matrix_app with
    | `Primary ->
        Some
          (Toffee.Style.default
          |> Toffee.Style.set_width (Toffee.Style.Dimension.pct 100.)
          |> Toffee.Style.set_height Toffee.Style.Dimension.auto)
    | `Alt -> None
  in
  let renderer =
    Renderer.create ?style:renderer_style
      ~glyph_pool:(Matrix.Grid.glyph_pool base_grid)
      ~width_method:(Matrix.Grid.width_method base_grid)
      ()
  in
  let container = Renderer.root renderer in
  let reconciler = Reconciler.create ~container in
  let runtime =
    {
      model;
      pending_msgs = [];
      pending_focus = [];
      app;
      matrix_app;
      process_perform;
      renderer;
      reconciler;
      key_subs = [];
      mouse_subs = [];
      paste_subs = [];
      resize_sub = None;
      tick_sub = None;
      every_subs = [];
      focus_sub = None;
      blur_sub = None;
      sub_live_active = false;
    }
  in
  process_cmd runtime init_cmd;
  update_subscriptions runtime;

  (* Drive Matrix loop activity from Renderable.live_count: start the
     render cadence when any node needs continuous rendering, stop when
     none do. *)
  let root = Renderer.root renderer in
  let live_was_positive = ref false in
  Renderable.Private.set_on_live_count_change root
    (Some
       (fun _node ->
         let count = Renderable.Private.live_count root in
         let is_positive = count > 0 in
         if is_positive && not !live_was_positive then (
           live_was_positive := true;
           Matrix.request_live matrix_app)
         else if (not is_positive) && !live_was_positive then (
           live_was_positive := false;
           Matrix.drop_live matrix_app)));

  let frame_delta = ref 0. in
  let primary_required_rows (_app : Matrix.app) =
    match Matrix.mode matrix_app with
    | `Primary ->
        Some (max 1 (Renderable.height (Renderer.root runtime.renderer)))
    | `Alt -> None
  in

  Matrix.run runtime.matrix_app ~primary_required_rows
    ~on_frame:(fun _app ~dt ->
      handle_tick runtime ~dt;
      handle_every_subs runtime ~dt;
      process_pending_msgs runtime;
      frame_delta := dt)
    ~on_input:(fun _app input -> handle_input runtime input)
    ~on_resize:(fun _app ~cols ~rows ->
      handle_resize runtime ~width:cols ~height:rows)
    ~on_render:(fun _app ->
      render runtime;
      let width, height = Matrix.effective_size runtime.matrix_app in
      Renderer.render_frame runtime.renderer ~width ~height ~delta:!frame_delta;
      let renderer_screen = Renderer.screen runtime.renderer in
      let renderer_grid = Matrix.Screen.grid renderer_screen in
      let renderer_hits = Matrix.Screen.hit_grid renderer_screen in
      Matrix.Grid.blit ~src:renderer_grid ~dst:(Matrix.grid runtime.matrix_app);
      Matrix.Screen.Hit_grid.blit ~src:renderer_hits
        ~dst:(Matrix.hits runtime.matrix_app);
      let cursor = Matrix.Screen.cursor_info renderer_screen in
      (* Keep the terminal cursor hidden unless the focused renderable exposes
         an explicit cursor position. *)
      Matrix.set_cursor
        ~visible:(cursor.visible && cursor.has_position)
        runtime.matrix_app;
      Matrix.set_cursor_style runtime.matrix_app ~style:cursor.style
        ~blinking:cursor.blinking;
      if cursor.has_position then
        Matrix.set_cursor_position runtime.matrix_app ~row:cursor.row
          ~col:cursor.col;
      (match cursor.color with
      | Some (r, g, b) ->
          Matrix.set_cursor_color runtime.matrix_app
            ~r:(Float.of_int r /. 255.)
            ~g:(Float.of_int g /. 255.)
            ~b:(Float.of_int b /. 255.)
            ~a:1.
      | None -> ());
      ignore (Renderer.render runtime.renderer : string);
      process_pending_focus runtime)

let empty = Vnode.empty
let fragment = Vnode.fragment
let embed = Vnode.embed

let layout_style ?display ?box_sizing ?position ?overflow ?scrollbar_width
    ?text_align ?inset ?size ?min_size ?max_size ?aspect_ratio ?margin ?padding
    ?border_width ?gap ?align_items ?align_self ?align_content ?justify_items
    ?justify_self ?justify_content ?flex_direction ?flex_wrap ?flex_grow
    ?flex_shrink ?flex_basis ?grid_template_rows ?grid_template_columns
    ?grid_auto_rows ?grid_auto_columns ?grid_auto_flow ?grid_template_areas
    ?grid_template_column_names ?grid_template_row_names ?grid_row ?grid_column
    () =
  Toffee.Style.make ?display ?box_sizing ?position ?overflow ?scrollbar_width
    ?text_align ?inset ?size ?min_size ?max_size ?aspect_ratio ?margin ?padding
    ?border:border_width ?gap ?align_items ?align_self ?align_content
    ?justify_items ?justify_self ?justify_content ?flex_direction ?flex_wrap
    ?flex_grow ?flex_shrink ?flex_basis ?grid_template_rows
    ?grid_template_columns ?grid_auto_rows ?grid_auto_columns ?grid_auto_flow
    ?grid_template_areas ?grid_template_column_names ?grid_template_row_names
    ?grid_row ?grid_column ()

let box ?key ?id ?display ?box_sizing ?position ?overflow ?scrollbar_width
    ?text_align ?inset ?flex_direction ?flex_wrap ?justify_content ?align_items
    ?size ?min_size ?max_size ?aspect_ratio ?gap ?padding ?margin ?border_width
    ?align_self ?align_content ?justify_items ?justify_self ?flex_grow
    ?flex_shrink ?flex_basis ?grid_template_rows ?grid_template_columns
    ?grid_auto_rows ?grid_auto_columns ?grid_auto_flow ?grid_template_areas
    ?grid_template_column_names ?grid_template_row_names ?grid_row ?grid_column
    ?visible ?z_index ?opacity ?focusable ?autofocus ?buffered ?live ?ref
    ?on_mouse ?on_key ?on_paste ?border ?border_style ?border_sides
    ?border_color ?focused_border_color ?background ?fill ?title
    ?title_alignment children =
  let style =
    layout_style ?display ?box_sizing ?position ?overflow ?scrollbar_width
      ?text_align ?inset ?size ?min_size ?max_size ?aspect_ratio ?margin
      ?padding ?border_width ?gap ?align_items ?align_self ?align_content
      ?justify_items ?justify_self ?justify_content ?flex_direction ?flex_wrap
      ?flex_grow ?flex_shrink ?flex_basis ?grid_template_rows
      ?grid_template_columns ?grid_auto_rows ?grid_auto_columns ?grid_auto_flow
      ?grid_template_areas ?grid_template_column_names ?grid_template_row_names
      ?grid_row ?grid_column ()
  in
  Vnode.box ?key ?id ~style ?visible ?z_index ?opacity ?focusable ?autofocus
    ?buffered ?live ?ref ?on_mouse ?on_key ?on_paste ?border ?border_style
    ?border_sides ?border_color ?focused_border_color ?background ?fill ?title
    ?title_alignment children

let text ?key ?id ?display ?box_sizing ?position ?overflow ?scrollbar_width
    ?text_align ?inset ?flex_direction ?flex_wrap ?justify_content ?align_items
    ?size ?min_size ?max_size ?aspect_ratio ?gap ?padding ?margin ?border_width
    ?align_self ?align_content ?justify_items ?justify_self ?flex_grow
    ?flex_shrink ?flex_basis ?grid_template_rows ?grid_template_columns
    ?grid_auto_rows ?grid_auto_columns ?grid_auto_flow ?grid_template_areas
    ?grid_template_column_names ?grid_template_row_names ?grid_row ?grid_column
    ?style ?visible ?z_index ?opacity ?focusable ?autofocus ?buffered ?live ?ref
    ?on_mouse ?on_key ?on_paste ?text_style ?wrap ?selectable ?selection_bg
    ?selection_fg ?tab_width ?truncate content =
  let layout_style =
    layout_style ?display ?box_sizing ?position ?overflow ?scrollbar_width
      ?text_align ?inset ?size ?min_size ?max_size ?aspect_ratio ?margin
      ?padding ?border_width ?gap ?align_items ?align_self ?align_content
      ?justify_items ?justify_self ?justify_content ?flex_direction ?flex_wrap
      ?flex_grow ?flex_shrink ?flex_basis ?grid_template_rows
      ?grid_template_columns ?grid_auto_rows ?grid_auto_columns ?grid_auto_flow
      ?grid_template_areas ?grid_template_column_names ?grid_template_row_names
      ?grid_row ?grid_column ()
  in
  let text_style =
    match text_style with Some _ -> text_style | None -> style
  in
  Vnode.text ?key ?id ~style:layout_style ?visible ?z_index ?opacity ?focusable
    ?autofocus ?buffered ?live ?ref ?on_mouse ?on_key ?on_paste ?text_style
    ?wrap ?selectable ?selection_bg ?selection_fg ?tab_width ?truncate content

let slider ?key ?id ?display ?box_sizing ?position ?overflow ?scrollbar_width
    ?text_align ?inset ?flex_direction ?flex_wrap ?justify_content ?align_items
    ?size ?min_size ?max_size ?aspect_ratio ?gap ?padding ?margin ?border_width
    ?align_self ?align_content ?justify_items ?justify_self ?flex_grow
    ?flex_shrink ?flex_basis ?grid_template_rows ?grid_template_columns
    ?grid_auto_rows ?grid_auto_columns ?grid_auto_flow ?grid_template_areas
    ?grid_template_column_names ?grid_template_row_names ?grid_row ?grid_column
    ?visible ?z_index ?opacity ?focusable ?autofocus ?buffered ?live ?ref
    ?on_mouse ?on_key ?on_paste ?orientation ?value ?min ?max ?viewport_size
    ?track_color ?thumb_color ?on_value_change () =
  let style =
    layout_style ?display ?box_sizing ?position ?overflow ?scrollbar_width
      ?text_align ?inset ?size ?min_size ?max_size ?aspect_ratio ?margin
      ?padding ?border_width ?gap ?align_items ?align_self ?align_content
      ?justify_items ?justify_self ?justify_content ?flex_direction ?flex_wrap
      ?flex_grow ?flex_shrink ?flex_basis ?grid_template_rows
      ?grid_template_columns ?grid_auto_rows ?grid_auto_columns ?grid_auto_flow
      ?grid_template_areas ?grid_template_column_names ?grid_template_row_names
      ?grid_row ?grid_column ()
  in
  Vnode.slider ?key ?id ~style ?visible ?z_index ?opacity ?focusable ?autofocus
    ?buffered ?live ?ref ?on_mouse ?on_key ?on_paste ?orientation ?value ?min
    ?max ?viewport_size ?track_color ?thumb_color ?on_value_change ()

let input ?key ?id ?display ?box_sizing ?position ?overflow ?scrollbar_width
    ?text_align ?inset ?flex_direction ?flex_wrap ?justify_content ?align_items
    ?size ?min_size ?max_size ?aspect_ratio ?gap ?padding ?margin ?border_width
    ?align_self ?align_content ?justify_items ?justify_self ?flex_grow
    ?flex_shrink ?flex_basis ?grid_template_rows ?grid_template_columns
    ?grid_auto_rows ?grid_auto_columns ?grid_auto_flow ?grid_template_areas
    ?grid_template_column_names ?grid_template_row_names ?grid_row ?grid_column
    ?visible ?z_index ?opacity ?focusable ?autofocus ?buffered ?live ?ref
    ?on_mouse ?on_key ?on_paste ?value ?cursor ?selection ?placeholder
    ?max_length ?text_color ?background_color ?focused_text_color
    ?focused_background_color ?placeholder_color ?selection_color ?selection_fg
    ?cursor_style ?cursor_color ?cursor_blinking ?on_input ?on_change ?on_submit
    ?on_cursor () =
  let style =
    layout_style ?display ?box_sizing ?position ?overflow ?scrollbar_width
      ?text_align ?inset ?size ?min_size ?max_size ?aspect_ratio ?margin
      ?padding ?border_width ?gap ?align_items ?align_self ?align_content
      ?justify_items ?justify_self ?justify_content ?flex_direction ?flex_wrap
      ?flex_grow ?flex_shrink ?flex_basis ?grid_template_rows
      ?grid_template_columns ?grid_auto_rows ?grid_auto_columns ?grid_auto_flow
      ?grid_template_areas ?grid_template_column_names ?grid_template_row_names
      ?grid_row ?grid_column ()
  in
  Vnode.input ?key ?id ~style ?visible ?z_index ?opacity ?focusable ?autofocus
    ?buffered ?live ?ref ?on_mouse ?on_key ?on_paste ?value ?cursor ?selection
    ?placeholder ?max_length ?text_color ?background_color ?focused_text_color
    ?focused_background_color ?placeholder_color ?selection_color ?selection_fg
    ?cursor_style ?cursor_color ?cursor_blinking ?on_input ?on_change ?on_submit
    ?on_cursor ()

let select ?key ?id ?display ?box_sizing ?position ?overflow ?scrollbar_width
    ?text_align ?inset ?flex_direction ?flex_wrap ?justify_content ?align_items
    ?size ?min_size ?max_size ?aspect_ratio ?gap ?padding ?margin ?border_width
    ?align_self ?align_content ?justify_items ?justify_self ?flex_grow
    ?flex_shrink ?flex_basis ?grid_template_rows ?grid_template_columns
    ?grid_auto_rows ?grid_auto_columns ?grid_auto_flow ?grid_template_areas
    ?grid_template_column_names ?grid_template_row_names ?grid_row ?grid_column
    ?visible ?z_index ?opacity ?focusable ?autofocus ?buffered ?live ?ref
    ?on_mouse ?on_key ?on_paste ?selected_index ?background ?text_color
    ?focused_background ?focused_text_color ?selected_background
    ?selected_text_color ?description_color ?selected_description_color
    ?show_description ?show_scroll_indicator ?wrap_selection ?item_spacing
    ?fast_scroll_step ?on_change ?on_activate options =
  let style =
    layout_style ?display ?box_sizing ?position ?overflow ?scrollbar_width
      ?text_align ?inset ?size ?min_size ?max_size ?aspect_ratio ?margin
      ?padding ?border_width ?gap ?align_items ?align_self ?align_content
      ?justify_items ?justify_self ?justify_content ?flex_direction ?flex_wrap
      ?flex_grow ?flex_shrink ?flex_basis ?grid_template_rows
      ?grid_template_columns ?grid_auto_rows ?grid_auto_columns ?grid_auto_flow
      ?grid_template_areas ?grid_template_column_names ?grid_template_row_names
      ?grid_row ?grid_column ()
  in
  Vnode.select ?key ?id ~style ?visible ?z_index ?opacity ?focusable ?autofocus
    ?buffered ?live ?ref ?on_mouse ?on_key ?on_paste ~options ?selected_index
    ?background ?text_color ?focused_background ?focused_text_color
    ?selected_background ?selected_text_color ?description_color
    ?selected_description_color ?show_description ?show_scroll_indicator
    ?wrap_selection ?item_spacing ?fast_scroll_step ?on_change ?on_activate ()

let tab_select ?key ?id ?display ?box_sizing ?position ?overflow
    ?scrollbar_width ?text_align ?inset ?flex_direction ?flex_wrap
    ?justify_content ?align_items ?size ?min_size ?max_size ?aspect_ratio ?gap
    ?padding ?margin ?border_width ?align_self ?align_content ?justify_items
    ?justify_self ?flex_grow ?flex_shrink ?flex_basis ?grid_template_rows
    ?grid_template_columns ?grid_auto_rows ?grid_auto_columns ?grid_auto_flow
    ?grid_template_areas ?grid_template_column_names ?grid_template_row_names
    ?grid_row ?grid_column ?visible ?z_index ?opacity ?focusable ?autofocus
    ?buffered ?live ?ref ?on_mouse ?on_key ?on_paste ?selected ?tab_width
    ?background ?text_color ?focused_background ?focused_text_color
    ?selected_background ?selected_text_color ?description_color
    ?selected_description_color ?show_underline ?show_description
    ?show_scroll_arrows ?wrap_selection ?on_change ?on_activate options =
  let style =
    layout_style ?display ?box_sizing ?position ?overflow ?scrollbar_width
      ?text_align ?inset ?size ?min_size ?max_size ?aspect_ratio ?margin
      ?padding ?border_width ?gap ?align_items ?align_self ?align_content
      ?justify_items ?justify_self ?justify_content ?flex_direction ?flex_wrap
      ?flex_grow ?flex_shrink ?flex_basis ?grid_template_rows
      ?grid_template_columns ?grid_auto_rows ?grid_auto_columns ?grid_auto_flow
      ?grid_template_areas ?grid_template_column_names ?grid_template_row_names
      ?grid_row ?grid_column ()
  in
  Vnode.tab_select ?key ?id ~style ?visible ?z_index ?opacity ?focusable
    ?autofocus ?buffered ?live ?ref ?on_mouse ?on_key ?on_paste ~options
    ?selected ?tab_width ?background ?text_color ?focused_background
    ?focused_text_color ?selected_background ?selected_text_color
    ?description_color ?selected_description_color ?show_underline
    ?show_description ?show_scroll_arrows ?wrap_selection ?on_change
    ?on_activate ()

let canvas ?key ?id ?display ?box_sizing ?position ?overflow ?scrollbar_width
    ?text_align ?inset ?flex_direction ?flex_wrap ?justify_content ?align_items
    ?size ?min_size ?max_size ?aspect_ratio ?gap ?padding ?margin ?border_width
    ?align_self ?align_content ?justify_items ?justify_self ?flex_grow
    ?flex_shrink ?flex_basis ?grid_template_rows ?grid_template_columns
    ?grid_auto_rows ?grid_auto_columns ?grid_auto_flow ?grid_template_areas
    ?grid_template_column_names ?grid_template_row_names ?grid_row ?grid_column
    ?visible ?z_index ?opacity ?focusable ?autofocus ?buffered ?live ?ref
    ?on_mouse ?on_key ?on_paste ?respect_alpha draw =
  let style =
    layout_style ?display ?box_sizing ?position ?overflow ?scrollbar_width
      ?text_align ?inset ?size ?min_size ?max_size ?aspect_ratio ?margin
      ?padding ?border_width ?gap ?align_items ?align_self ?align_content
      ?justify_items ?justify_self ?justify_content ?flex_direction ?flex_wrap
      ?flex_grow ?flex_shrink ?flex_basis ?grid_template_rows
      ?grid_template_columns ?grid_auto_rows ?grid_auto_columns ?grid_auto_flow
      ?grid_template_areas ?grid_template_column_names ?grid_template_row_names
      ?grid_row ?grid_column ()
  in
  Vnode.canvas ?key ?id ~style ?visible ?z_index ?opacity ?focusable ?autofocus
    ?buffered ?live ?ref ?on_mouse ?on_key ?on_paste ?respect_alpha
    ~on_draw:draw ()

let spinner ?key ?id ?display ?box_sizing ?position ?overflow ?scrollbar_width
    ?text_align ?inset ?flex_direction ?flex_wrap ?justify_content ?align_items
    ?size ?min_size ?max_size ?aspect_ratio ?gap ?padding ?margin ?border_width
    ?align_self ?align_content ?justify_items ?justify_self ?flex_grow
    ?flex_shrink ?flex_basis ?grid_template_rows ?grid_template_columns
    ?grid_auto_rows ?grid_auto_columns ?grid_auto_flow ?grid_template_areas
    ?grid_template_column_names ?grid_template_row_names ?grid_row ?grid_column
    ?visible ?z_index ?opacity ?focusable ?autofocus ?buffered ?live ?ref
    ?on_mouse ?on_key ?on_paste ?frame_set ?color () =
  let style =
    layout_style ?display ?box_sizing ?position ?overflow ?scrollbar_width
      ?text_align ?inset ?size ?min_size ?max_size ?aspect_ratio ?margin
      ?padding ?border_width ?gap ?align_items ?align_self ?align_content
      ?justify_items ?justify_self ?justify_content ?flex_direction ?flex_wrap
      ?flex_grow ?flex_shrink ?flex_basis ?grid_template_rows
      ?grid_template_columns ?grid_auto_rows ?grid_auto_columns ?grid_auto_flow
      ?grid_template_areas ?grid_template_column_names ?grid_template_row_names
      ?grid_row ?grid_column ()
  in
  Vnode.spinner ?key ?id ~style ?visible ?z_index ?opacity ?focusable ?autofocus
    ?buffered ?live ?ref ?on_mouse ?on_key ?on_paste ?frame_set ?color ()

let progress_bar ?key ?id ?display ?box_sizing ?position ?overflow
    ?scrollbar_width ?text_align ?inset ?flex_direction ?flex_wrap
    ?justify_content ?align_items ?size ?min_size ?max_size ?aspect_ratio ?gap
    ?padding ?margin ?border_width ?align_self ?align_content ?justify_items
    ?justify_self ?flex_grow ?flex_shrink ?flex_basis ?grid_template_rows
    ?grid_template_columns ?grid_auto_rows ?grid_auto_columns ?grid_auto_flow
    ?grid_template_areas ?grid_template_column_names ?grid_template_row_names
    ?grid_row ?grid_column ?visible ?z_index ?opacity ?focusable ?autofocus
    ?buffered ?live ?ref ?on_mouse ?on_key ?on_paste ?value ?min ?max
    ?orientation ?filled_color ?empty_color () =
  let style =
    layout_style ?display ?box_sizing ?position ?overflow ?scrollbar_width
      ?text_align ?inset ?size ?min_size ?max_size ?aspect_ratio ?margin
      ?padding ?border_width ?gap ?align_items ?align_self ?align_content
      ?justify_items ?justify_self ?justify_content ?flex_direction ?flex_wrap
      ?flex_grow ?flex_shrink ?flex_basis ?grid_template_rows
      ?grid_template_columns ?grid_auto_rows ?grid_auto_columns ?grid_auto_flow
      ?grid_template_areas ?grid_template_column_names ?grid_template_row_names
      ?grid_row ?grid_column ()
  in
  Vnode.progress_bar ?key ?id ~style ?visible ?z_index ?opacity ?focusable
    ?autofocus ?buffered ?live ?ref ?on_mouse ?on_key ?on_paste ?value ?min ?max
    ?orientation ?filled_color ?empty_color ()

let scroll_bar ?key ?id ?display ?box_sizing ?position ?overflow
    ?scrollbar_width ?text_align ?inset ?flex_direction ?flex_wrap
    ?justify_content ?align_items ?size ?min_size ?max_size ?aspect_ratio ?gap
    ?padding ?margin ?border_width ?align_self ?align_content ?justify_items
    ?justify_self ?flex_grow ?flex_shrink ?flex_basis ?grid_template_rows
    ?grid_template_columns ?grid_auto_rows ?grid_auto_columns ?grid_auto_flow
    ?grid_template_areas ?grid_template_column_names ?grid_template_row_names
    ?grid_row ?grid_column ?visible ?z_index ?opacity ?focusable ?autofocus
    ?buffered ?live ?ref ?on_mouse ?on_key ?on_paste ?orientation ?show_arrows
    ?track_color ?thumb_color ?arrow_fg ?arrow_bg ?on_change () =
  let style =
    layout_style ?display ?box_sizing ?position ?overflow ?scrollbar_width
      ?text_align ?inset ?size ?min_size ?max_size ?aspect_ratio ?margin
      ?padding ?border_width ?gap ?align_items ?align_self ?align_content
      ?justify_items ?justify_self ?justify_content ?flex_direction ?flex_wrap
      ?flex_grow ?flex_shrink ?flex_basis ?grid_template_rows
      ?grid_template_columns ?grid_auto_rows ?grid_auto_columns ?grid_auto_flow
      ?grid_template_areas ?grid_template_column_names ?grid_template_row_names
      ?grid_row ?grid_column ()
  in
  Vnode.scroll_bar ?key ?id ~style ?visible ?z_index ?opacity ?focusable
    ?autofocus ?buffered ?live ?ref ?on_mouse ?on_key ?on_paste ?orientation
    ?show_arrows ?track_color ?thumb_color ?arrow_fg ?arrow_bg ?on_change ()

let scroll_box ?key ?id ?display ?box_sizing ?position ?overflow
    ?scrollbar_width ?text_align ?inset ?flex_direction ?flex_wrap
    ?justify_content ?align_items ?size ?min_size ?max_size ?aspect_ratio ?gap
    ?padding ?margin ?border_width ?align_self ?align_content ?justify_items
    ?justify_self ?flex_grow ?flex_shrink ?flex_basis ?grid_template_rows
    ?grid_template_columns ?grid_auto_rows ?grid_auto_columns ?grid_auto_flow
    ?grid_template_areas ?grid_template_column_names ?grid_template_row_names
    ?grid_row ?grid_column ?visible ?z_index ?opacity ?focusable ?autofocus
    ?buffered ?live ?ref ?on_mouse ?on_key ?on_paste ?scroll_x ?scroll_y
    ?sticky_scroll ?sticky_start ?background ?on_scroll children =
  let style =
    layout_style ?display ?box_sizing ?position ?overflow ?scrollbar_width
      ?text_align ?inset ?size ?min_size ?max_size ?aspect_ratio ?margin
      ?padding ?border_width ?gap ?align_items ?align_self ?align_content
      ?justify_items ?justify_self ?justify_content ?flex_direction ?flex_wrap
      ?flex_grow ?flex_shrink ?flex_basis ?grid_template_rows
      ?grid_template_columns ?grid_auto_rows ?grid_auto_columns ?grid_auto_flow
      ?grid_template_areas ?grid_template_column_names ?grid_template_row_names
      ?grid_row ?grid_column ()
  in
  Vnode.scroll_box ?key ?id ~style ?visible ?z_index ?opacity ?focusable
    ?autofocus ?buffered ?live ?ref ?on_mouse ?on_key ?on_paste ?scroll_x
    ?scroll_y ?sticky_scroll ?sticky_start ?background ?on_scroll children

let textarea ?key ?id ?display ?box_sizing ?position ?overflow ?scrollbar_width
    ?text_align ?inset ?flex_direction ?flex_wrap ?justify_content ?align_items
    ?size ?min_size ?max_size ?aspect_ratio ?gap ?padding ?margin ?border_width
    ?align_self ?align_content ?justify_items ?justify_self ?flex_grow
    ?flex_shrink ?flex_basis ?grid_template_rows ?grid_template_columns
    ?grid_auto_rows ?grid_auto_columns ?grid_auto_flow ?grid_template_areas
    ?grid_template_column_names ?grid_template_row_names ?grid_row ?grid_column
    ?visible ?z_index ?opacity ?focusable ?autofocus ?buffered ?live ?ref
    ?on_mouse ?on_key ?on_paste ?value ?cursor ?selection ?spans
    ?ghost_text ?ghost_text_color ?placeholder ?wrap ?text_color
    ?background_color ?focused_text_color ?focused_background_color
    ?placeholder_color ?selection_color ?selection_fg ?cursor_style
    ?cursor_color ?cursor_blinking ?on_input ?on_change ?on_submit ?on_cursor ()
    =
  let style =
    layout_style ?display ?box_sizing ?position ?overflow ?scrollbar_width
      ?text_align ?inset ?size ?min_size ?max_size ?aspect_ratio ?margin
      ?padding ?border_width ?gap ?align_items ?align_self ?align_content
      ?justify_items ?justify_self ?justify_content ?flex_direction ?flex_wrap
      ?flex_grow ?flex_shrink ?flex_basis ?grid_template_rows
      ?grid_template_columns ?grid_auto_rows ?grid_auto_columns ?grid_auto_flow
      ?grid_template_areas ?grid_template_column_names ?grid_template_row_names
      ?grid_row ?grid_column ()
  in
  Vnode.textarea ?key ?id ~style ?visible ?z_index ?opacity ?focusable
    ?autofocus ?buffered ?live ?ref ?on_mouse ?on_key ?on_paste ?value ?cursor
    ?selection ?spans ?ghost_text ?ghost_text_color ?placeholder ?wrap
    ?text_color ?background_color ?focused_text_color ?focused_background_color
    ?placeholder_color ?selection_color ?selection_fg ?cursor_style
    ?cursor_color ?cursor_blinking ?on_input ?on_change ?on_submit ?on_cursor ()

let code ?key ?id ?display ?box_sizing ?position ?overflow ?scrollbar_width
    ?text_align ?inset ?flex_direction ?flex_wrap ?justify_content ?align_items
    ?size ?min_size ?max_size ?aspect_ratio ?gap ?padding ?margin ?border_width
    ?align_self ?align_content ?justify_items ?justify_self ?flex_grow
    ?flex_shrink ?flex_basis ?grid_template_rows ?grid_template_columns
    ?grid_auto_rows ?grid_auto_columns ?grid_auto_flow ?grid_template_areas
    ?grid_template_column_names ?grid_template_row_names ?grid_row ?grid_column
    ?visible ?z_index ?opacity ?focusable ?autofocus ?buffered ?live ?ref
    ?on_mouse ?on_key ?on_paste ?spans ?text_style ?wrap ?tab_width
    ?selectable ?selection_bg ?selection_fg ?on_selection content =
  let style =
    layout_style ?display ?box_sizing ?position ?overflow ?scrollbar_width
      ?text_align ?inset ?size ?min_size ?max_size ?aspect_ratio ?margin
      ?padding ?border_width ?gap ?align_items ?align_self ?align_content
      ?justify_items ?justify_self ?justify_content ?flex_direction ?flex_wrap
      ?flex_grow ?flex_shrink ?flex_basis ?grid_template_rows
      ?grid_template_columns ?grid_auto_rows ?grid_auto_columns ?grid_auto_flow
      ?grid_template_areas ?grid_template_column_names ?grid_template_row_names
      ?grid_row ?grid_column ()
  in
  Vnode.code ?key ?id ~style ?visible ?z_index ?opacity ?focusable ?autofocus
    ?buffered ?live ?ref ?on_mouse ?on_key ?on_paste ?spans ?text_style
    ?wrap ?tab_width ?selectable ?selection_bg ?selection_fg ?on_selection
    content

let line_number ?key ?id ?display ?box_sizing ?position ?overflow
    ?scrollbar_width ?text_align ?inset ?flex_direction ?flex_wrap
    ?justify_content ?align_items ?size ?min_size ?max_size ?aspect_ratio ?gap
    ?padding ?margin ?border_width ?align_self ?align_content ?justify_items
    ?justify_self ?flex_grow ?flex_shrink ?flex_basis ?grid_template_rows
    ?grid_template_columns ?grid_auto_rows ?grid_auto_columns ?grid_auto_flow
    ?grid_template_areas ?grid_template_column_names ?grid_template_row_names
    ?grid_row ?grid_column ?visible ?z_index ?opacity ?focusable ?autofocus
    ?buffered ?live ?ref ?on_mouse ?on_key ?on_paste ?fg ?bg ?min_width
    ?padding_right ?show_line_numbers ?line_number_offset ?line_colors
    ?line_signs ?hidden_line_numbers child =
  let style =
    layout_style ?display ?box_sizing ?position ?overflow ?scrollbar_width
      ?text_align ?inset ?size ?min_size ?max_size ?aspect_ratio ?margin
      ?padding ?border_width ?gap ?align_items ?align_self ?align_content
      ?justify_items ?justify_self ?justify_content ?flex_direction ?flex_wrap
      ?flex_grow ?flex_shrink ?flex_basis ?grid_template_rows
      ?grid_template_columns ?grid_auto_rows ?grid_auto_columns ?grid_auto_flow
      ?grid_template_areas ?grid_template_column_names ?grid_template_row_names
      ?grid_row ?grid_column ()
  in
  Vnode.line_number ?key ?id ~style ?visible ?z_index ?opacity ?focusable
    ?autofocus ?buffered ?live ?ref ?on_mouse ?on_key ?on_paste ?fg ?bg
    ?min_width ?padding_right ?show_line_numbers ?line_number_offset
    ?line_colors ?line_signs ?hidden_line_numbers child

let markdown ?key ?id ?display ?box_sizing ?position ?overflow ?scrollbar_width
    ?text_align ?inset ?flex_direction ?flex_wrap ?justify_content ?align_items
    ?size ?min_size ?max_size ?aspect_ratio ?gap ?padding ?margin ?border_width
    ?align_self ?align_content ?justify_items ?justify_self ?flex_grow
    ?flex_shrink ?flex_basis ?grid_template_rows ?grid_template_columns
    ?grid_auto_rows ?grid_auto_columns ?grid_auto_flow ?grid_template_areas
    ?grid_template_column_names ?grid_template_row_names ?grid_row ?grid_column
    ?visible ?z_index ?opacity ?focusable ?autofocus ?buffered ?live ?ref
    ?on_mouse ?on_key ?on_paste ?md_style ?conceal ?streaming content =
  let style =
    layout_style ?display ?box_sizing ?position ?overflow ?scrollbar_width
      ?text_align ?inset ?size ?min_size ?max_size ?aspect_ratio ?margin
      ?padding ?border_width ?gap ?align_items ?align_self ?align_content
      ?justify_items ?justify_self ?justify_content ?flex_direction ?flex_wrap
      ?flex_grow ?flex_shrink ?flex_basis ?grid_template_rows
      ?grid_template_columns ?grid_auto_rows ?grid_auto_columns ?grid_auto_flow
      ?grid_template_areas ?grid_template_column_names ?grid_template_row_names
      ?grid_row ?grid_column ()
  in
  Vnode.markdown ?key ?id ~style ?visible ?z_index ?opacity ?focusable
    ?autofocus ?buffered ?live ?ref ?on_mouse ?on_key ?on_paste ?md_style
    ?conceal ?streaming content

let table ?key ?id ?display ?box_sizing ?position ?overflow ?scrollbar_width
    ?text_align ?inset ?flex_direction ?flex_wrap ?justify_content ?align_items
    ?size ?min_size ?max_size ?aspect_ratio ?gap ?padding ?margin ?border_width
    ?align_self ?align_content ?justify_items ?justify_self ?flex_grow
    ?flex_shrink ?flex_basis ?grid_template_rows ?grid_template_columns
    ?grid_auto_rows ?grid_auto_columns ?grid_auto_flow ?grid_template_areas
    ?grid_template_column_names ?grid_template_row_names ?grid_row ?grid_column
    ?visible ?z_index ?opacity ?focusable ?autofocus ?buffered ?live ?ref
    ?on_mouse ?on_key ?on_paste ?columns ?rows ?selected_row ?border
    ?border_style ?show_header ?show_column_separator ?show_row_separator
    ?cell_padding ?header_color ?header_background ?text_color ?background
    ?selected_text_color ?selected_background ?focused_selected_text_color
    ?focused_selected_background ?row_styles ?wrap_selection ?fast_scroll_step
    ?on_change ?on_activate () =
  let style =
    layout_style ?display ?box_sizing ?position ?overflow ?scrollbar_width
      ?text_align ?inset ?size ?min_size ?max_size ?aspect_ratio ?margin
      ?padding ?border_width ?gap ?align_items ?align_self ?align_content
      ?justify_items ?justify_self ?justify_content ?flex_direction ?flex_wrap
      ?flex_grow ?flex_shrink ?flex_basis ?grid_template_rows
      ?grid_template_columns ?grid_auto_rows ?grid_auto_columns ?grid_auto_flow
      ?grid_template_areas ?grid_template_column_names ?grid_template_row_names
      ?grid_row ?grid_column ()
  in
  Vnode.table ?key ?id ~style ?visible ?z_index ?opacity ?focusable ?autofocus
    ?buffered ?live ?ref ?on_mouse ?on_key ?on_paste ?columns ?rows
    ?selected_row ?border ?border_style ?show_header ?show_column_separator
    ?show_row_separator ?cell_padding ?header_color ?header_background
    ?text_color ?background ?selected_text_color ?selected_background
    ?focused_selected_text_color ?focused_selected_background ?row_styles
    ?wrap_selection ?fast_scroll_step ?on_change ?on_activate ()

let tree ?key ?id ?display ?box_sizing ?position ?overflow ?scrollbar_width
    ?text_align ?inset ?flex_direction ?flex_wrap ?justify_content ?align_items
    ?size ?min_size ?max_size ?aspect_ratio ?gap ?padding ?margin ?border_width
    ?align_self ?align_content ?justify_items ?justify_self ?flex_grow
    ?flex_shrink ?flex_basis ?grid_template_rows ?grid_template_columns
    ?grid_auto_rows ?grid_auto_columns ?grid_auto_flow ?grid_template_areas
    ?grid_template_column_names ?grid_template_row_names ?grid_row ?grid_column
    ?visible ?z_index ?opacity ?focusable ?autofocus ?buffered ?live ?ref
    ?on_mouse ?on_key ?on_paste ?items ?selected_index ?expand_depth
    ?indent_size ?show_guides ?guide_style ?expand_icon ?collapse_icon
    ?leaf_icon ?background ?text_color ?selected_background ?selected_text_color
    ?focused_selected_background ?focused_selected_text_color ?guide_color
    ?icon_color ?wrap_selection ?fast_scroll_step ?on_change ?on_activate
    ?on_expand () =
  let style =
    layout_style ?display ?box_sizing ?position ?overflow ?scrollbar_width
      ?text_align ?inset ?size ?min_size ?max_size ?aspect_ratio ?margin
      ?padding ?border_width ?gap ?align_items ?align_self ?align_content
      ?justify_items ?justify_self ?justify_content ?flex_direction ?flex_wrap
      ?flex_grow ?flex_shrink ?flex_basis ?grid_template_rows
      ?grid_template_columns ?grid_auto_rows ?grid_auto_columns ?grid_auto_flow
      ?grid_template_areas ?grid_template_column_names ?grid_template_row_names
      ?grid_row ?grid_column ()
  in
  Vnode.tree ?key ?id ~style ?visible ?z_index ?opacity ?focusable ?autofocus
    ?buffered ?live ?ref ?on_mouse ?on_key ?on_paste ?items ?selected_index
    ?expand_depth ?indent_size ?show_guides ?guide_style ?expand_icon
    ?collapse_icon ?leaf_icon ?background ?text_color ?selected_background
    ?selected_text_color ?focused_selected_background
    ?focused_selected_text_color ?guide_color ?icon_color ?wrap_selection
    ?fast_scroll_step ?on_change ?on_activate ?on_expand ()

let px = Vnode.px
let pct = Vnode.pct
let size = Vnode.size
let gap = Vnode.gap
let auto = Vnode.auto
let padding = Vnode.padding
let margin = Vnode.margin
let inset = Vnode.inset
let size_wh w h : dimension Toffee.Geometry.Size.t = { width = w; height = h }

let gap_xy x y : length_percentage Toffee.Geometry.Size.t =
  let x = Toffee.Style.Length_percentage.length (Float.of_int x) in
  let y = Toffee.Style.Length_percentage.length (Float.of_int y) in
  { width = x; height = y }

let padding_xy x y : length_percentage Toffee.Geometry.Rect.t =
  let x = Toffee.Style.Length_percentage.length (Float.of_int x) in
  let y = Toffee.Style.Length_percentage.length (Float.of_int y) in
  { left = x; right = x; top = y; bottom = y }

let padding_lrtb l r t b : length_percentage Toffee.Geometry.Rect.t =
  let f n = Toffee.Style.Length_percentage.length (Float.of_int n) in
  { left = f l; right = f r; top = f t; bottom = f b }

let margin_xy x y : length_percentage_auto Toffee.Geometry.Rect.t =
  let x = Toffee.Style.Length_percentage_auto.length (Float.of_int x) in
  let y = Toffee.Style.Length_percentage_auto.length (Float.of_int y) in
  { left = x; right = x; top = y; bottom = y }

let margin_lrtb l r t b : length_percentage_auto Toffee.Geometry.Rect.t =
  let f n = Toffee.Style.Length_percentage_auto.length (Float.of_int n) in
  { left = f l; right = f r; top = f t; bottom = f b }

let inset_lrtb l r t b : length_percentage_auto Toffee.Geometry.Rect.t =
  let f n = Toffee.Style.Length_percentage_auto.length (Float.of_int n) in
  { left = f l; right = f r; top = f t; bottom = f b }