Source file layout_options.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
open! Core
open! Import
open Bonsai_web
open Bonsai.Let_syntax

module Style_config = struct
  type t =
    { presets_attr : A.t
    ; panel_attr : A.t
    ; wrapper_attr : A.t
    ; render_icon : [ `Content_config | `Hidden | `Shown ] -> N.t
    }
  [@@deriving fields ~getters]
end

let default_style =
  { Style_config.presets_attr =
      [%css
        {|
          display: flex;
          flex-direction: column;
          align-items: stretch;

          & .title {
            font-weight: bold;
          }

          & div > *:hover {
            cursor: pointer;
            user-select: none;
          }
        |}]
  ; panel_attr = [%css "padding-left: 0.4em;"]
  ; wrapper_attr =
      [%css
        {|
          padding: 0.2em;
          padding-left: 0;
          cursor: pointer;
        |}]
  ; render_icon =
      (function
        | `Content_config -> Codicons.svg ~color:(`Hex "1c2127") Codicons.Gear
        | `Hidden -> Codicons.svg ~color:(`Hex "1c2127") Codicons.Eye_closed
        | `Shown -> Codicons.svg ~color:(`Hex "1c2127") Codicons.Eye)
  }
;;

let child_config_buttons ~open_config_editor ~config ~inject (local_ graph) =
  match%sub config with
  | { Bonsai_web_panel_config.config = Content content; _ } ->
    (match open_config_editor with
     | Some open_config_editor ->
       let%arr config_edit_effect =
         open_config_editor
           ~update:
             (let%arr inject in
              fun content -> inject (Logic.Action.Set_content content))
           content
           graph
       in
       [ `Content_config, A.on_click (fun _ -> config_edit_effect) ]
     | None -> Bonsai.return [])
  | _ -> Bonsai.return []
;;

let child_config_panel_type ~config =
  match config with
  | { Bonsai_web_panel_config.config = Content _; _ } -> "Untitled panel"
  | { Bonsai_web_panel_config.config = Tabbed _; _ } -> "Untitled tab stack"
  | { Bonsai_web_panel_config.config = Horizontal_fixed _; _ } ->
    "Untitled horizontal stack"
  | { Bonsai_web_panel_config.config = Vertical_fixed _; _ }
  | { Bonsai_web_panel_config.config = Vertical_variable _; _ } ->
    "Untitled vertical stack"
;;

let single_panel_row ~style_config ~config ~title =
  let%arr { Style_config.panel_attr; render_icon; _ } = style_config
  and title
  and config in
  fun ~buttons ->
    let title =
      Option.value_map
        title
        ~default:(`Unnamed, child_config_panel_type ~config)
        ~f:(Tuple2.create `Named)
    in
    let title =
      let italics =
        match fst title with
        | `Named -> A.empty
        | `Unnamed -> [%css {|font-style: italic;|}]
      in
      let title = Vdom.Node.text (snd title) in
      {%html|
        <div style="flex-grow: 1; padding-right: 1em" %{italics}>
          %{title}
        </div>
      |}
    in
    let buttons =
      List.map
        ~f:(fun (icon, on_click) ->
          let icon = render_icon icon in
          let icon =
            match icon with
            | N.Widget _ -> N.div [ icon ]
            | _ -> icon
          in
          match icon with
          | N.Element e ->
            N.Element (N.Element.map_attrs ~f:(fun a -> A.many [ on_click; a ]) e)
          | _ -> icon)
        buttons
    in
    {%html|
      <div
        %{panel_attr}
        style="display: flex; flex-direction: row; justify-content: space-between"
      >
        %{title}
        <div style="display: flex; flex-direction: row; flex: 0 0 auto">
          *{buttons}
        </div>
      </div>
    |}
    |> Option.return
;;

let panel_layout_options ~open_config_editor ~style_config ~recurse state (local_ graph)
  : N.t list Bonsai.t
  =
  let%sub ~config, ~inject, ~child_layout = state in
  let layout =
    let%arr child_layout in
    let%map.Option _, layout = child_layout in
    layout
  in
  match%sub config with
  | { Bonsai_web_panel_config.config = Content _; _ } -> Bonsai.return []
  | { Bonsai_web_panel_config.config =
        Horizontal_fixed _ | Vertical_variable _ | Vertical_fixed _
    ; _
    } ->
    let child_layouts =
      let%arr config in
      Bonsai_web_panel_config.child_float_layouts config |> Option.value_exn
    in
    let child_configs =
      let%arr config in
      Bonsai_web_panel_config.child_configs config |> Option.value_exn
    in
    let children_with_ids =
      let%arr child_configs and child_layouts in
      let children =
        Bonsai_web_panel_config.create_children child_configs child_layouts
      in
      List.mapi children ~f:(fun index (config, layout) ->
        let panel_id = Bonsai_web_panel_config.panel_id config in
        panel_id, (index, config, layout))
      |> Map.of_alist_exn (module Bonsai_web_panel_config.Panel_id)
    in
    Bonsai.assoc
      (module Bonsai_web_panel_config.Panel_id)
      children_with_ids
      ~f:(fun _panel_id child_data (local_ graph) ->
        let%sub child_index, config, layout = child_data in
        let child_inject =
          let%arr inject and config in
          let panel_id = Bonsai_web_panel_config.panel_id config in
          fun action -> inject (Logic.Action.Update_child_config (~panel_id, ~action))
        in
        let state =
          let%arr config and layout and child_inject and child_index in
          ~config, ~inject:child_inject, ~child_layout:(Some (child_index, layout))
        in
        let hide_button =
          let%arr layout and inject and child_index and child_layouts and config in
          let panel_id = Bonsai_web_panel_config.panel_id config in
          if (not layout.hidden)
             && List.for_alli child_layouts ~f:(fun i other_layout ->
               if i = child_index then true else other_layout.hidden)
          then []
          else
            [ ( (if layout.hidden then `Hidden else `Shown)
              , A.on_click (fun _ ->
                  Effect.Many
                    [ Effect.Stop_propagation
                    ; inject
                        (Logic.Action.Set_hidden (~panel_id, ~hidden:(not layout.hidden)))
                    ]) )
            ]
        in
        let child_buttons =
          let%arr child_buttons =
            child_config_buttons ~open_config_editor ~config ~inject:child_inject graph
          and hide_button in
          child_buttons @ hide_button
        in
        let title =
          let%arr layout in
          Bonsai_web_panel_config.Child_layout.title layout
        in
        let%arr single_panel_row = single_panel_row ~style_config ~title ~config
        and child_buttons
        and child_children = recurse state graph in
        {%html|
          <div>
            ?{single_panel_row ~buttons:child_buttons}
            <div style="padding-left: 1em">*{child_children}</div>
          </div>
        |})
      graph
    >>| Map.data
  | { Bonsai_web_panel_config.config = Tabbed { tabs; _ }; _ } ->
    let tabs_with_ids =
      let%arr tabs in
      Nonempty_list.to_list tabs
      |> List.mapi ~f:(fun tab_index (config, title) ->
        let panel_id = Bonsai_web_panel_config.panel_id config in
        panel_id, (tab_index, config, title))
      |> Map.of_alist_exn (module Bonsai_web_panel_config.Panel_id)
    in
    Bonsai.assoc
      (module Bonsai_web_panel_config.Panel_id)
      tabs_with_ids
      ~f:(fun _panel_id tab_data (local_ graph) ->
        let%sub tab_index, config, title = tab_data in
        let child_inject =
          let%arr inject and config in
          let panel_id = Bonsai_web_panel_config.panel_id config in
          fun action -> inject (Logic.Action.Update_child_config (~panel_id, ~action))
        in
        let child_layout =
          let%arr tab_index and layout in
          let%bind.Option layout in
          (tab_index, layout) |> Option.return
        in
        let state =
          let%arr config and inject and child_layout in
          ~config, ~inject, ~child_layout
        in
        let%arr single_panel_row =
          single_panel_row ~style_config ~title:(title >>| Option.some) ~config
        and child_children = recurse state graph
        and buttons =
          child_config_buttons ~open_config_editor ~config ~inject:child_inject graph
        in
        {%html|
          <div>
            ?{single_panel_row ~buttons}
            <div style="padding-left: 1em">*{child_children}</div>
          </div>
        |})
      graph
    >>| Map.data
;;

let component
  ?(style_config = Bonsai.return default_style)
  ?presets
  ?open_config_editor
  ~logic
  (local_ graph)
  =
  let%sub { Logic.inject; config; set_config } = logic in
  let preset_chooser =
    let%arr { presets_attr; _ } = style_config
    and presets = Bonsai.transpose_opt presets
    and set_config in
    let%map.Option presets in
    let on_click config =
      A.on_click (fun _ -> Effect.Many [ Effect.Stop_propagation; set_config config ])
    in
    let presets =
      Map.to_alist presets
      |> List.map ~f:(fun (name, config) ->
        {%html|<div %{on_click config}>%{name#String}</div>|})
    in
    {%html|
      <div %{presets_attr}>
        <span class="title">Presets</span>
        <div>*{presets}</div>
        <hr style="width: 100%" />
      </div>
    |}
  in
  let state =
    let%arr config and inject in
    ~config, ~inject, ~child_layout:None
  in
  let panel_layout_options =
    Bonsai.fix ~f:(panel_layout_options ~style_config ~open_config_editor) state graph
  in
  let%arr preset_chooser and panel_layout_options in
  {%html|<div>?{preset_chooser} *{panel_layout_options}</div>|}
;;