package miaou-core
sectionYPositions = computeSectionYPositions($el), 10)"
x-init="setTimeout(() => sectionYPositions = computeSectionYPositions($el), 10)"
>
Miaou core/widgets (no drivers, no SDL)
Install
dune-project
Dependency
Authors
Maintainers
Sources
v0.5.2.tar.gz
md5=60a3b9f181f24572a06a9492532bfdda
sha512=fcc35a275066be2900e6201782faf47503076fa4640f08cf78067835a6f447b74613009e55b2ac799adb7ca46f1bffa261fc5971753f2cc3c6bef327511c7ef6
doc/src/miaou-core.core/modal_manager.ml.html
Source file modal_manager.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(*****************************************************************************) (* *) (* SPDX-License-Identifier: MIT *) (* Copyright (c) 2025 Nomadic Labs <contact@nomadic-labs.com> *) (* *) (*****************************************************************************) [@@@warning "-32-34-37-69"] module Logger_capability = Miaou_interfaces.Logger_capability type outcome = [`Commit | `Cancel] type max_width_spec = Miaou_internals.Modal_snapshot.max_width_spec = | Fixed of int | Ratio of float | Clamped of {ratio : float; min : int; max : int} type ui = { title : string; left : int option; max_width : max_width_spec option; dim_background : bool; } let resolve_max_width = Miaou_internals.Modal_snapshot.resolve_max_width type frame = | Frame : { p : (module Tui_page.PAGE_SIG with type state = 's); mutable st : 's Navigation.t; ui : ui; commit_on : string list; cancel_on : string list; on_close : 's Navigation.t -> outcome -> unit; } -> frame let stack : frame list ref = ref [] let has_active () = !stack <> [] let clear () = stack := [] ; Miaou_internals.Modal_snapshot.clear_rendered_position () let debug_enabled = lazy (let get_env var = match Miaou_interfaces.System.get () with | Some sys -> sys.get_env_var var | None -> Sys.getenv_opt var in match get_env "MIAOU_TUI_DEBUG_MODAL" with | Some ("1" | "true" | "TRUE" | "yes" | "YES") -> true | _ -> false) let dprintf fmt = if Lazy.force debug_enabled then Printf.eprintf fmt else Printf.ifprintf Stdlib.stdout fmt (* Expose a mutable location for the current terminal geometry. The driver should update this on each render (and on resize events) so modal view thunks receive the correct size. *) let current_size = ref {LTerm_geom.rows = 24; cols = 80} let set_current_size rows cols = current_size := {LTerm_geom.rows; cols} let current_cols () = !current_size.LTerm_geom.cols let get_current_size () = (!current_size.LTerm_geom.rows, !current_size.LTerm_geom.cols) let push (type s) (module P : Tui_page.PAGE_SIG with type state = s) ~(init : s Navigation.t) ~ui ~commit_on ~cancel_on ~on_close = (* Replace any existing frame with the same title to avoid duplicate overlays. *) dprintf "[DEBUG] Modal_manager.push: title='%s', current_stack_size=%d\n%!" ui.title (List.length !stack) ; List.iteri (fun i (Frame r) -> dprintf "[DEBUG] [%d] existing: '%s'\n%!" i r.ui.title) !stack ; stack := List.filter (fun (Frame r) -> r.ui.title <> ui.title) !stack ; let fr = Frame {p = (module P); st = init; ui; commit_on; cancel_on; on_close} in stack := !stack @ [fr] ; dprintf "[DEBUG] Modal_manager.push: after push, stack_size=%d\n%!" (List.length !stack) ; (* Also log to Logger capability if available *) match Logger_capability.get () with | Some logger -> logger.logf Debug (Printf.sprintf "PUSH: title='%s' stack_size=%d" ui.title (List.length !stack)) | None -> () let pop_top () = match List.rev !stack with | [] -> () | Frame _ :: rest_rev -> stack := List.rev rest_rev ; (* Clear rendered position if stack is now empty *) if !stack = [] then Miaou_internals.Modal_snapshot.clear_rendered_position () let handle_key key = match List.rev !stack with | [] -> () | Frame ({commit_on; cancel_on; on_close; _} as r) :: _ -> let module P = (val r.p : Tui_page.PAGE_SIG with type state = _) in let term_size = !current_size in let max_width_opt = match r.ui.max_width with | None -> None | Some spec -> resolve_max_width spec ~cols:term_size.LTerm_geom.cols in let geom = Miaou_internals.Modal_utils.compute_modal_geometry ~cols:term_size.LTerm_geom.cols ~rows:term_size.LTerm_geom.rows ~left_opt:r.ui.left ~max_width_opt in let size = {LTerm_geom.rows = geom.max_content_h; cols = geom.content_width} in (* Translate mouse coordinates using the position stored during render. The modal_renderer stores the content top-left after computing the actual modal position based on content size. *) let key = match Miaou_internals.Modal_snapshot.get_rendered_position () with | Some (content_top, content_left) -> Miaou_helpers.Mouse.translate_key ~row_offset:content_top ~col_offset:content_left key | None -> (* Fallback: no position stored, don't translate *) key in (* Let the page handle the key first so it can update its state based on the key (e.g. move cursor). After the page updated the state we evaluate commit/cancel semantics so commits reflect the new state. *) r.st <- P.handle_key r.st key ~size ; if List.exists (( = ) key) cancel_on then ( let st = r.st in pop_top () ; on_close st `Cancel) else if List.exists (( = ) key) commit_on then ( let st = r.st in pop_top () ; on_close st `Commit) else () let render_overlay ~base = (* render_overlay is intentionally not implemented here; use modal renderer in the executable. The internal library provides a snapshot accessor via Miaou.Internal.Modal_snapshot.set_provider which we register below. *) let _ = base in None (* Register provider for internal snapshot accessor so the internal lib can call back into this module without requiring the internal lib to depend on the public lib. *) let () = let provider () = List.map (fun (Frame r) -> let title = r.ui.title in let left = r.ui.left in let max_width = r.ui.max_width in let dim = r.ui.dim_background in let view_thunk (size : LTerm_geom.size) = let module P = (val r.p : Tui_page.PAGE_SIG with type state = _) in P.view r.st ~focus:true ~size in (title, left, max_width, dim, view_thunk)) !stack in try Miaou_internals.Modal_snapshot.set_provider provider with _ -> () (* Helper: allow a modal to mark that the next key which closed it should be considered consumed and not propagated by the driver. This is useful for short-lived informational modals that must not let Enter/Esc hit the underlying page. *) let consume_next_key_flag = ref false let set_consume_next_key () = consume_next_key_flag := true let take_consume_next_key () = let v = !consume_next_key_flag in consume_next_key_flag := false ; v (* Pending navigation from modal on_close callbacks. Modal callbacks can call set_pending_navigation to request navigation without needing access to the parent page's pstate. The driver checks this after modal close and applies it to the pstate. *) let : Navigation.nav option ref = ref None let = pending_navigation_ref := Some nav let () = let v = !pending_navigation_ref in pending_navigation_ref := None ; v let top_ui_opt () = match List.rev !stack with [] -> None | Frame r :: _ -> Some r.ui (* Expose the top modal title when present to allow targeted auto-dismissal. *) let top_title_opt () = match top_ui_opt () with Some ui -> Some ui.title | None -> None (* Duplicate current_size/set_current_size declaration removed; top-level definitions near the top of this module are used instead. *) let close_top (outcome : outcome) = match List.rev !stack with | [] -> () | Frame r :: _ -> (* Pop first so handlers that push new frames are respected. *) let st = r.st in pop_top () ; r.on_close st outcome let push_default (type s) (module P : Tui_page.PAGE_SIG with type state = s) ~init ~ui ~on_close = (* Default keys commonly used by modals. *) let commit_on = ["Enter"] in let cancel_on = ["Esc"] in push (module P) ~init ~ui ~commit_on ~cancel_on ~on_close (* Higher-level convenience helpers. These are non-blocking wrappers that push appropriate modal pages and deliver results via callbacks. They use `push_default` internally to get the Enter/Esc key behavior. *) let alert (type s) (module P : Tui_page.PAGE_SIG with type state = s) ~init ?(title = "") ?left ?max_width ?(dim_background = true) () = let ui = {title; left; max_width; dim_background} in push_default (module P) ~init ~ui ~on_close:(fun (_ : s Navigation.t) -> function `Commit | `Cancel -> ()) let confirm (type s) (module P : Tui_page.PAGE_SIG with type state = s) ~init ?(title = "Confirm") ?left ?max_width ?(dim_background = true) ~on_result () = let ui = {title; left; max_width; dim_background} in push_default (module P) ~init ~ui ~on_close:(fun _st -> function | `Commit -> on_result true | `Cancel -> on_result false) let confirm_with_extract (type s) (module P : Tui_page.PAGE_SIG with type state = s) ~init ?(title = "Confirm") ?left ?max_width ?(dim_background = true) ~extract ~on_result () = let ui = {title; left; max_width; dim_background} in push_default (module P) ~init ~ui ~on_close:(fun st -> function | `Commit -> on_result (extract st) | `Cancel -> on_result None) let prompt (type s) (module P : Tui_page.PAGE_SIG with type state = s) ~init ?(title = "Prompt") ?left ?max_width ?(dim_background = true) ~extract ~on_result () = let ui = {title; left; max_width; dim_background} in push_default (module P) ~init ~ui ~on_close:(fun st -> function | `Commit -> on_result (extract st) | `Cancel -> on_result None)
sectionYPositions = computeSectionYPositions($el), 10)"
x-init="setTimeout(() => sectionYPositions = computeSectionYPositions($el), 10)"
>