package miaou-core

  1. Overview
  2. Docs
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_widgets_input/validated_textbox_widget.ml.html

Source file validated_textbox_widget.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
(*****************************************************************************)
(*                                                                           *)
(* SPDX-License-Identifier: MIT                                              *)
(* Copyright (c) 2025 Nomadic Labs <contact@nomadic-labs.com>                *)
(* Copyright (c) 2026 Mathias Bourgoin <mathias.bourgoin@atacama.tech>       *)
(*                                                                           *)
(*****************************************************************************)
open Miaou_widgets_display.Widgets

type 'a validation_result = Valid of 'a | Invalid of string

type 'a validator = string -> 'a validation_result

type 'a t = {
  textbox : Textbox_widget.t;
  validator : 'a validator;
  validation_state : 'a validation_result;
  (* Debounce support *)
  debounce_ms : int;
  last_input_time : float;
  pending_validation : bool;
}

let default_debounce_ms = 250

let create ?title ?(width = 60) ?(initial = "") ?(placeholder = None)
    ?(debounce_ms = default_debounce_ms) ~validator () =
  let textbox = Textbox_widget.create ?title ~width ~initial ~placeholder () in
  let validation_state = validator initial in
  {
    textbox;
    validator;
    validation_state;
    debounce_ms;
    last_input_time = 0.;
    pending_validation = false;
  }

let open_centered ?title ?(width = 60) ?(initial = "") ?(placeholder = None)
    ?(debounce_ms = default_debounce_ms) ~validator () =
  create ?title ~width ~initial ~placeholder ~debounce_ms ~validator ()

let run_validation t =
  let current_value = Textbox_widget.value t.textbox in
  let validation_state = t.validator current_value in
  {t with validation_state; pending_validation = false}

(* Check if debounce period has elapsed and run validation if needed.
   Call this before render to update the validation state. *)
let tick t =
  if not t.pending_validation then t
  else
    let now = Unix.gettimeofday () in
    let elapsed_ms = (now -. t.last_input_time) *. 1000. in
    if elapsed_ms >= float_of_int t.debounce_ms then run_validation t else t

let render t ~focus =
  let base_render = Textbox_widget.render t.textbox ~focus in
  match t.validation_state with
  | Valid _ -> base_render
  | Invalid error_msg ->
      let error_display =
        if visible_chars_count error_msg > 60 then
          let idx = visible_byte_index_of_pos error_msg 57 in
          String.sub error_msg 0 idx ^ "..."
        else error_msg
      in
      (* Add red coloring to indicate error and show error message below *)
      let colored_base = red base_render in
      colored_base ^ "\n" ^ red ("⚠ " ^ error_display)

let handle_key t ~key =
  (* First, check if any pending validation should run now *)
  let t = tick t in
  let updated_textbox = Textbox_widget.handle_key t.textbox ~key in
  let text_changed =
    Textbox_widget.value updated_textbox <> Textbox_widget.value t.textbox
  in
  if text_changed then
    (* Text changed: mark validation as pending, record timestamp *)
    let now = Unix.gettimeofday () in
    if t.debounce_ms <= 0 then
      (* No debounce: validate immediately *)
      run_validation {t with textbox = updated_textbox}
    else (
      (* Debounce enabled: defer validation and request a re-render *)
      Miaou_helpers.Render_notify.request_render () ;
      {
        t with
        textbox = updated_textbox;
        last_input_time = now;
        pending_validation = true;
      })
  else
    (* No text change (e.g., cursor movement): no validation needed *)
    {t with textbox = updated_textbox}

let on_key t ~key =
  let t' = handle_key t ~key in
  (* Determine result based on key (mirrors Textbox_widget.on_key logic) *)
  let handled =
    match key with
    | "Backspace" | "Delete" | "Left" | "Right" | "Home" | "End" | "Esc"
    | "Escape" ->
        true
    | _ when String.length key = 1 -> true (* printable char *)
    | _ -> false
  in
  (t', Miaou_interfaces.Key_event.of_bool handled)

let is_cancelled t = Textbox_widget.is_cancelled t.textbox

let reset_cancelled t =
  let reset_textbox = Textbox_widget.reset_cancelled t.textbox in
  {t with textbox = reset_textbox}

let value t = Textbox_widget.value t.textbox

let validation_result t = t.validation_state

let is_valid t =
  match t.validation_state with Valid _ -> true | Invalid _ -> false

let get_validated_value t =
  match t.validation_state with Valid v -> Some v | Invalid _ -> None

let get_error_message t =
  match t.validation_state with Valid _ -> None | Invalid msg -> Some msg

let width t = Textbox_widget.width t.textbox

let with_width t width =
  let textbox = Textbox_widget.with_width t.textbox width in
  {t with textbox}

(* Force immediate validation, useful before form submission *)
let flush_validation t = if t.pending_validation then run_validation t else t

(* Check if there's a pending validation that hasn't run yet *)
let has_pending_validation t = t.pending_validation

let () =
  Miaou_registry.register
    ~name:"validated_textbox"
    ~mli:[%blob "validated_textbox_widget.mli"]
    ()