| 1 |
open Notty |
| 2 |
open Lwd_utils |
| 3 |
|
| 4 |
module Focus : |
| 5 |
sig |
| 6 |
type var = int Lwd.var |
| 7 |
type handle |
| 8 |
val make : unit -> handle |
| 9 |
val request : handle -> unit |
| 10 |
val request_var : var -> unit |
| 11 |
val release : handle -> unit |
| 12 |
|
| 13 |
type status = |
| 14 |
| Empty |
| 15 |
| Handle of int * var |
| 16 |
| Conflict of int |
| 17 |
|
| 18 |
val empty : status |
| 19 |
(*val is_empty : status -> bool*) |
| 20 |
val status : handle -> status Lwd.t |
| 21 |
val has_focus : status -> bool |
| 22 |
val merge : status -> status -> status |
| 23 |
end = struct |
| 24 |
|
| 25 |
type var = int Lwd.var |
| 26 |
|
| 27 |
type status = |
| 28 |
| Empty |
| 29 |
| Handle of int * var |
| 30 |
| Conflict of int |
| 31 |
|
| 32 |
type handle = var * status Lwd.t |
| 33 |
|
| 34 |
let make () = |
| 35 |
let v = Lwd.var 0 in |
| 36 |
(v, Lwd.map ~f:(fun i -> Handle (i, v)) (Lwd.get v)) |
| 37 |
|
| 38 |
let empty : status = Empty |
| 39 |
|
| 40 |
let status (h : handle) : status Lwd.t = snd h |
| 41 |
|
| 42 |
let has_focus = function |
| 43 |
| Empty -> false |
| 44 |
| Handle (i, _) | Conflict i -> i > 0 |
| 45 |
|
| 46 |
let clock = ref 0 |
| 47 |
|
| 48 |
let request_var (v : var) = |
| 49 |
incr clock; |
| 50 |
Lwd.set v !clock |
| 51 |
|
| 52 |
let request (v, _ : handle) = request_var v |
| 53 |
let release (v, _ : handle) = incr clock; Lwd.set v 0 |
| 54 |
|
| 55 |
let merge s1 s2 : status = match s1, s2 with |
| 56 |
| Empty, x | x, Empty -> x |
| 57 |
| _, Handle (0, _) -> s1 |
| 58 |
| Handle (0, _), _ -> s2 |
| 59 |
| Handle (i1, _), Handle (i2, _) when i1 = i2 -> s1 |
| 60 |
| (Handle (i1, _) | Conflict i1), Conflict i2 when i1 < i2 -> s2 |
| 61 |
| (Handle (i1, _) | Conflict i1), Handle (i2, _) when i1 < i2 -> |
| 62 |
Conflict i2 |
| 63 |
| Conflict _, (Handle (_, _) | Conflict _) -> s1 |
| 64 |
| Handle (i1, _), (Handle (_, _) | Conflict _) -> Conflict i1 |
| 65 |
end |
| 66 |
|
| 67 |
module Gravity : |
| 68 |
sig |
| 69 |
type direction = [ |
| 70 |
| `Negative |
| 71 |
| `Neutral |
| 72 |
| `Positive |
| 73 |
] |
| 74 |
val pp_direction : Format.formatter -> direction -> unit |
| 75 |
type t |
| 76 |
val pp : Format.formatter -> t -> unit |
| 77 |
val make : h:direction -> v:direction -> t |
| 78 |
val default : t |
| 79 |
val h : t -> direction |
| 80 |
val v : t -> direction |
| 81 |
|
| 82 |
type t2 |
| 83 |
val pair : t -> t -> t2 |
| 84 |
val p1 : t2 -> t |
| 85 |
val p2 : t2 -> t |
| 86 |
end = |
| 87 |
struct |
| 88 |
type direction = [ `Negative | `Neutral | `Positive ] |
| 89 |
type t = int |
| 90 |
type t2 = int |
| 91 |
|
| 92 |
let default = 0 |
| 93 |
|
| 94 |
let pack = function |
| 95 |
| `Negative -> 0 |
| 96 |
| `Neutral -> 1 |
| 97 |
| `Positive -> 2 |
| 98 |
|
| 99 |
let unpack = function |
| 100 |
| 0 -> `Negative |
| 101 |
| 1 -> `Neutral |
| 102 |
| _ -> `Positive |
| 103 |
|
| 104 |
let make ~h ~v = |
| 105 |
(pack h lsl 2) lor pack v |
| 106 |
|
| 107 |
let h x = unpack (x lsr 2) |
| 108 |
let v x = unpack (x land 3) |
| 109 |
|
| 110 |
let pp_direction ppf dir = |
| 111 |
let text = match dir with |
| 112 |
| `Negative -> "`Negative" |
| 113 |
| `Neutral -> "`Neutral" |
| 114 |
| `Positive -> "`Positive" |
| 115 |
in |
| 116 |
Format.pp_print_string ppf text |
| 117 |
|
| 118 |
let pp ppf g = |
| 119 |
Format.fprintf ppf "{ h = %a; v = %a }" pp_direction (h g) pp_direction (v g) |
| 120 |
|
| 121 |
let pair t1 t2 = |
| 122 |
(t1 lsl 4) lor t2 |
| 123 |
|
| 124 |
let p1 t = (t lsr 4) land 15 |
| 125 |
let p2 t = t land 15 |
| 126 |
end |
| 127 |
type gravity = Gravity.t |
| 128 |
|
| 129 |
module Interval : sig |
| 130 |
type t = private int |
| 131 |
val make : int -> int -> t |
| 132 |
val shift : t -> int -> t |
| 133 |
val fst : t -> int |
| 134 |
val snd : t -> int |
| 135 |
(*val size : t -> int*) |
| 136 |
val zero : t |
| 137 |
end = struct |
| 138 |
type t = int |
| 139 |
|
| 140 |
let half = Sys.word_size lsr 1 |
| 141 |
let mask = (1 lsl half) - 1 |
| 142 |
|
| 143 |
let make x y = |
| 144 |
let size = y - x in |
| 145 |
(*assert (size >= 0);*) |
| 146 |
(x lsl half) lor (size land mask) |
| 147 |
|
| 148 |
let shift t d = |
| 149 |
t + d lsl half |
| 150 |
|
| 151 |
let fst t = t asr half |
| 152 |
let size t = t land mask |
| 153 |
let snd t = fst t + size t |
| 154 |
|
| 155 |
let zero = 0 |
| 156 |
end |
| 157 |
|
| 158 |
module Ui = |
| 159 |
struct |
| 160 |
type may_handle = [ `Unhandled | `Handled ] |
| 161 |
|
| 162 |
type mouse_handler = x:int -> y:int -> Unescape.button -> [ |
| 163 |
| `Unhandled |
| 164 |
| `Handled |
| 165 |
| `Grab of (x:int -> y:int -> unit) * (x:int -> y:int -> unit) |
| 166 |
] |
| 167 |
|
| 168 |
type semantic_key = [ |
| 169 |
(* Clipboard *) |
| 170 |
| `Copy |
| 171 |
| `Paste |
| 172 |
(* Focus management *) |
| 173 |
| `Focus of [`Next | `Prev | `Left | `Right | `Up | `Down] |
| 174 |
] |
| 175 |
|
| 176 |
type key = [ |
| 177 |
| Unescape.special | `Uchar of Uchar.t | `ASCII of char | semantic_key |
| 178 |
] * Unescape.mods |
| 179 |
|
| 180 |
type mouse = Unescape.mouse |
| 181 |
|
| 182 |
type event = [ `Key of key | `Mouse of mouse | `Paste of Unescape.paste ] |
| 183 |
|
| 184 |
type layout_spec = { w : int; h : int; sw : int; sh : int } |
| 185 |
|
| 186 |
let pp_layout_spec ppf { w; h; sw; sh } = |
| 187 |
Format.fprintf ppf "{ w = %d; h = %d; sw = %d; sh = %d }" w h sw sh |
| 188 |
|
| 189 |
type flags = int |
| 190 |
let flags_none = 0 |
| 191 |
let flag_transient_sensor = 1 |
| 192 |
let flag_permanent_sensor = 2 |
| 193 |
|
| 194 |
type size_sensor = w:int -> h:int -> unit |
| 195 |
type frame_sensor = x:int -> y:int -> w:int -> h:int -> unit -> unit |
| 196 |
|
| 197 |
type t = { |
| 198 |
w : int; sw : int; |
| 199 |
h : int; sh : int; |
| 200 |
mutable desc : desc; |
| 201 |
focus : Focus.status; |
| 202 |
mutable flags : flags; |
| 203 |
mutable sensor_cache : (int * int * int * int) option; |
| 204 |
mutable cache : cache; |
| 205 |
} |
| 206 |
and cache = { |
| 207 |
vx : Interval.t; vy : Interval.t; |
| 208 |
image : image; |
| 209 |
} |
| 210 |
and desc = |
| 211 |
| Atom of image |
| 212 |
| Size_sensor of t * size_sensor |
| 213 |
| Transient_sensor of t * frame_sensor |
| 214 |
| Permanent_sensor of t * frame_sensor |
| 215 |
| Resize of t * Gravity.t2 * A.t |
| 216 |
| Mouse_handler of t * mouse_handler |
| 217 |
| Focus_area of t * (key -> may_handle) |
| 218 |
| Shift_area of t * int * int |
| 219 |
| Event_filter of t * ([`Key of key | `Mouse of mouse] -> may_handle) |
| 220 |
| X of t * t |
| 221 |
| Y of t * t |
| 222 |
| Z of t * t |
| 223 |
|
| 224 |
|
| 225 |
let layout_spec t : layout_spec = |
| 226 |
{ w = t.w; h = t.h; sw = t.sw; sh = t.sh } |
| 227 |
let layout_width t = t.w |
| 228 |
let layout_stretch_width t = t.sw |
| 229 |
let layout_height t = t.h |
| 230 |
let layout_stretch_height t = t.sh |
| 231 |
|
| 232 |
let cache : cache = |
| 233 |
{ vx = Interval.zero; vy = Interval.zero; image = I.empty } |
| 234 |
|
| 235 |
let empty : t = |
| 236 |
{ w = 0; sw = 0; h = 0; sh = 0; flags = flags_none; |
| 237 |
focus = Focus.empty; desc = Atom I.empty; |
| 238 |
sensor_cache = None; cache } |
| 239 |
|
| 240 |
let atom img : t = |
| 241 |
{ w = I.width img; sw = 0; |
| 242 |
h = I.height img; sh = 0; |
| 243 |
focus = Focus.empty; flags = flags_none; |
| 244 |
desc = Atom img; |
| 245 |
sensor_cache = None; cache; } |
| 246 |
|
| 247 |
let space_1_0 = atom (I.void 1 0) |
| 248 |
let space_0_1 = atom (I.void 0 1) |
| 249 |
let space_1_1 = atom (I.void 1 1) |
| 250 |
|
| 251 |
let space x y = |
| 252 |
match x, y with |
| 253 |
| 0, 0 -> empty |
| 254 |
| 1, 0 -> space_1_0 |
| 255 |
| 0, 1 -> space_0_1 |
| 256 |
| 1, 1 -> space_1_1 |
| 257 |
| _ -> atom (I.void x y) |
| 258 |
|
| 259 |
let mouse_area f t : t = |
| 260 |
{ t with desc = Mouse_handler (t, f) } |
| 261 |
|
| 262 |
let keyboard_area ?focus f t : t = |
| 263 |
let focus = match focus with |
| 264 |
| None -> t.focus |
| 265 |
| Some focus -> Focus.merge focus t.focus |
| 266 |
in |
| 267 |
{ t with desc = Focus_area (t, f); focus } |
| 268 |
|
| 269 |
let shift_area x y t : t = |
| 270 |
{ t with desc = Shift_area (t, x, y) } |
| 271 |
|
| 272 |
let size_sensor handler t : t = |
| 273 |
{ t with desc = Size_sensor (t, handler) } |
| 274 |
|
| 275 |
let transient_sensor frame_sensor t = |
| 276 |
{ t with desc = Transient_sensor (t, frame_sensor); |
| 277 |
flags = t.flags lor flag_transient_sensor } |
| 278 |
|
| 279 |
let permanent_sensor frame_sensor t = |
| 280 |
{ t with desc = Permanent_sensor (t, frame_sensor); |
| 281 |
flags = t.flags lor flag_permanent_sensor } |
| 282 |
|
| 283 |
let prepare_gravity = function |
| 284 |
| None, None -> Gravity.(pair default default) |
| 285 |
| Some g, None | None, Some g -> Gravity.(pair g g) |
| 286 |
| Some pad, Some crop -> Gravity.(pair pad crop) |
| 287 |
|
| 288 |
let resize ?w ?h ?sw ?sh ?pad ?crop ?(bg=A.empty) t : t = |
| 289 |
let g = prepare_gravity (pad, crop) in |
| 290 |
match (w, t.w), (h, t.h), (sw, t.sw), (sh, t.sh) with |
| 291 |
| (Some w, _ | None, w), (Some h, _ | None, h), |
| 292 |
(Some sw, _ | None, sw), (Some sh, _ | None, sh) -> |
| 293 |
{t with w; h; sw; sh; desc = Resize (t, g, bg)} |
| 294 |
|
| 295 |
let resize_to ({w; h; sw; sh} : layout_spec) ?pad ?crop ?(bg=A.empty) t : t = |
| 296 |
let g = prepare_gravity (pad, crop) in |
| 297 |
{t with w; h; sw; sh; desc = Resize (t, g, bg)} |
| 298 |
|
| 299 |
let event_filter ?focus f t : t = |
| 300 |
let focus = match focus with |
| 301 |
| None -> t.focus |
| 302 |
| Some focus -> focus |
| 303 |
in |
| 304 |
{ t with desc = Event_filter (t, f); focus } |
| 305 |
|
| 306 |
let join_x a b = { |
| 307 |
w = (a.w + b.w); sw = (a.sw + b.sw); |
| 308 |
h = (maxi a.h b.h); sh = (maxi a.sh b.sh); |
| 309 |
flags = a.flags lor b.flags; |
| 310 |
focus = Focus.merge a.focus b.focus; desc = X (a, b); |
| 311 |
sensor_cache = None; cache |
| 312 |
} |
| 313 |
|
| 314 |
let join_y a b = { |
| 315 |
w = (maxi a.w b.w); sw = (maxi a.sw b.sw); |
| 316 |
h = (a.h + b.h); sh = (a.sh + b.sh); |
| 317 |
flags = a.flags lor b.flags; |
| 318 |
focus = Focus.merge a.focus b.focus; desc = Y (a, b); |
| 319 |
sensor_cache = None; cache; |
| 320 |
} |
| 321 |
|
| 322 |
let join_z a b = { |
| 323 |
w = (maxi a.w b.w); sw = (maxi a.sw b.sw); |
| 324 |
h = (maxi a.h b.h); sh = (maxi a.sh b.sh); |
| 325 |
flags = a.flags lor b.flags; |
| 326 |
focus = Focus.merge a.focus b.focus; desc = Z (a, b); |
| 327 |
sensor_cache = None; cache; |
| 328 |
} |
| 329 |
|
| 330 |
let pack_x = (empty, join_x) |
| 331 |
let pack_y = (empty, join_y) |
| 332 |
let pack_z = (empty, join_z) |
| 333 |
|
| 334 |
let hcat xs = Lwd_utils.reduce pack_x xs |
| 335 |
let vcat xs = Lwd_utils.reduce pack_y xs |
| 336 |
let zcat xs = Lwd_utils.reduce pack_z xs |
| 337 |
|
| 338 |
let has_focus t = Focus.has_focus t.focus |
| 339 |
|
| 340 |
let rec pp ppf t = |
| 341 |
Format.fprintf ppf |
| 342 |
"@[<hov>{@ w = %d;@ h = %d;@ sw = %d;@ sh = %d;@ desc = @[%a@];@ }@]" |
| 343 |
t.w t.h t.sw t.sh pp_desc t.desc |
| 344 |
|
| 345 |
and pp_desc ppf = function |
| 346 |
| Atom _ -> Format.fprintf ppf "Atom _" |
| 347 |
| Size_sensor (desc, _) -> |
| 348 |
Format.fprintf ppf "Size_sensor (@[%a,@ _@])" pp desc |
| 349 |
| Transient_sensor (desc, _) -> |
| 350 |
Format.fprintf ppf "Transient_sensor (@[%a,@ _@])" pp desc |
| 351 |
| Permanent_sensor (desc, _) -> |
| 352 |
Format.fprintf ppf "Permanent_sensor (@[%a,@ _@])" pp desc |
| 353 |
| Resize (desc, gravity, _bg) -> |
| 354 |
Format.fprintf ppf "Resize (@[%a,@ %a,@ %a@])" pp desc |
| 355 |
Gravity.pp (Gravity.p1 gravity) |
| 356 |
Gravity.pp (Gravity.p2 gravity) |
| 357 |
| Mouse_handler (n, _) -> |
| 358 |
Format.fprintf ppf "Mouse_handler (@[%a,@ _@])" pp n |
| 359 |
| Focus_area (n, _) -> |
| 360 |
Format.fprintf ppf "Focus_area (@[%a,@ _@])" pp n |
| 361 |
| Shift_area (n, _, _) -> |
| 362 |
Format.fprintf ppf "Shift_area (@[%a,@ _@])" pp n |
| 363 |
| Event_filter (n, _) -> |
| 364 |
Format.fprintf ppf "Event_filter (@[%a,@ _@])" pp n |
| 365 |
| X (a, b) -> Format.fprintf ppf "X (@[%a,@ %a@])" pp a pp b |
| 366 |
| Y (a, b) -> Format.fprintf ppf "Y (@[%a,@ %a@])" pp a pp b |
| 367 |
| Z (a, b) -> Format.fprintf ppf "Z (@[%a,@ %a@])" pp a pp b |
| 368 |
|
| 369 |
let iter f ui = match ui.desc with |
| 370 |
| Atom _ -> () |
| 371 |
| Size_sensor (u, _) | Transient_sensor (u, _) | Permanent_sensor (u, _) |
| 372 |
| Resize (u, _, _) | Mouse_handler (u, _) |
| 373 |
| Focus_area (u, _) | Shift_area (u, _, _) | Event_filter (u, _) |
| 374 |
-> f u |
| 375 |
| X (u1, u2) | Y (u1, u2) | Z (u1, u2) -> f u1; f u2 |
| 376 |
end |
| 377 |
type ui = Ui.t |
| 378 |
|
| 379 |
module Renderer = |
| 380 |
struct |
| 381 |
open Ui |
| 382 |
|
| 383 |
type size = int * int |
| 384 |
|
| 385 |
type grab_function = (x:int -> y:int -> unit) * (x:int -> y:int -> unit) |
| 386 |
type t = { |
| 387 |
mutable size : size; |
| 388 |
mutable view : ui; |
| 389 |
mutable mouse_grab : grab_function option; |
| 390 |
} |
| 391 |
|
| 392 |
let make () = { |
| 393 |
mouse_grab = None; |
| 394 |
size = (0, 0); |
| 395 |
view = Ui.empty; |
| 396 |
} |
| 397 |
|
| 398 |
let size t = t.size |
| 399 |
|
| 400 |
let solve_focus ui i = |
| 401 |
let rec aux ui = |
| 402 |
match ui.focus with |
| 403 |
| Focus.Empty | Focus.Handle (0, _) -> () |
| 404 |
| Focus.Handle (i', _) when i = i' -> () |
| 405 |
| Focus.Handle (_, v) -> Lwd.set v 0 |
| 406 |
| Focus.Conflict _ -> Ui.iter aux ui |
| 407 |
in |
| 408 |
aux ui |
| 409 |
|
| 410 |
let split ~a ~sa ~b ~sb total = |
| 411 |
let stretch = sa + sb in |
| 412 |
let flex = total - a - b in |
| 413 |
if stretch > 0 && flex > 0 then |
| 414 |
let ratio = |
| 415 |
if sa > sb then |
| 416 |
flex * sa / stretch |
| 417 |
else |
| 418 |
flex - flex * sb / stretch |
| 419 |
in |
| 420 |
(a + ratio, b + flex - ratio) |
| 421 |
else |
| 422 |
(a, b) |
| 423 |
|
| 424 |
let pack ~fixed ~stretch total g1 g2 = |
| 425 |
let flex = total - fixed in |
| 426 |
if stretch > 0 && flex > 0 then |
| 427 |
(0, total) |
| 428 |
else |
| 429 |
let gravity = if flex >= 0 then g1 else g2 in |
| 430 |
match gravity with |
| 431 |
| `Negative -> (0, fixed) |
| 432 |
| `Neutral -> (flex / 2, fixed) |
| 433 |
| `Positive -> (flex, fixed) |
| 434 |
|
| 435 |
let has_transient_sensor flags = flags land flag_transient_sensor <> 0 |
| 436 |
let has_permanent_sensor flags = flags land flag_permanent_sensor <> 0 |
| 437 |
|
| 438 |
let rec update_sensors ox oy sw sh ui = |
| 439 |
if has_transient_sensor ui.flags || ( |
| 440 |
has_permanent_sensor ui.flags && |
| 441 |
match ui.sensor_cache with |
| 442 |
| None -> true |
| 443 |
| Some (ox', oy', sw', sh') -> |
| 444 |
not (ox = ox' && oy = oy' && sw = sw' && sh = sh') |
| 445 |
) |
| 446 |
then ( |
| 447 |
ui.flags <- ui.flags land lnot flag_transient_sensor; |
| 448 |
if has_permanent_sensor ui.flags then |
| 449 |
ui.sensor_cache <- Some (ox, oy, sw, sh); |
| 450 |
match ui.desc with |
| 451 |
| Atom _ -> () |
| 452 |
| Size_sensor (t, _) | Mouse_handler (t, _) |
| 453 |
| Focus_area (t, _) | Event_filter (t, _) -> |
| 454 |
update_sensors ox oy sw sh t |
| 455 |
| Transient_sensor (t, sensor) -> |
| 456 |
ui.desc <- t.desc; |
| 457 |
let sensor = sensor ~x:ox ~y:oy ~w:sw ~h:sh in |
| 458 |
update_sensors ox oy sw sh t; |
| 459 |
sensor () |
| 460 |
| Permanent_sensor (t, sensor) -> |
| 461 |
let sensor = sensor ~x:ox ~y:oy ~w:sw ~h:sh in |
| 462 |
update_sensors ox oy sw sh t; |
| 463 |
sensor () |
| 464 |
| Resize (t, g, _) -> |
| 465 |
let open Gravity in |
| 466 |
let dx, rw = pack ~fixed:t.w ~stretch:t.sw sw (h (p1 g)) (h (p2 g)) in |
| 467 |
let dy, rh = pack ~fixed:t.h ~stretch:t.sh sh (v (p1 g)) (v (p2 g)) in |
| 468 |
update_sensors (ox + dx) (oy + dy) rw rh t |
| 469 |
| Shift_area (t, sx, sy) -> |
| 470 |
update_sensors (ox - sx) (oy - sy) sw sh t |
| 471 |
| X (a, b) -> |
| 472 |
let aw, bw = split ~a:a.w ~sa:a.sw ~b:b.w ~sb:b.sw sw in |
| 473 |
update_sensors ox oy aw sh a; |
| 474 |
update_sensors (ox + aw) oy bw sh b |
| 475 |
| Y (a, b) -> |
| 476 |
let ah, bh = split ~a:a.h ~sa:a.sh ~b:b.h ~sb:b.sh sh in |
| 477 |
update_sensors ox oy sw ah a; |
| 478 |
update_sensors ox (oy + ah) sw bh b |
| 479 |
| Z (a, b) -> |
| 480 |
update_sensors ox oy sw sh a; |
| 481 |
update_sensors ox oy sw sh b |
| 482 |
) |
| 483 |
|
| 484 |
let update_focus ui = |
| 485 |
match ui.focus with |
| 486 |
| Focus.Empty | Focus.Handle _ -> () |
| 487 |
| Focus.Conflict i -> solve_focus ui i |
| 488 |
|
| 489 |
let update t size ui = |
| 490 |
t.size <- size; |
| 491 |
t.view <- ui; |
| 492 |
update_sensors 0 0 (fst size) (snd size) ui; |
| 493 |
update_focus ui |
| 494 |
|
| 495 |
let dispatch_mouse st x y btn w h t = |
| 496 |
let handle ox oy f = |
| 497 |
match f ~x:(x - ox) ~y:(y - oy) btn with |
| 498 |
| `Unhandled -> false |
| 499 |
| `Handled -> true |
| 500 |
| `Grab f -> st.mouse_grab <- Some f; true |
| 501 |
in |
| 502 |
let rec aux ox oy sw sh t = |
| 503 |
match t.desc with |
| 504 |
| Atom _ -> false |
| 505 |
| X (a, b) -> |
| 506 |
let aw, bw = split ~a:a.w ~sa:a.sw ~b:b.w ~sb:b.sw sw in |
| 507 |
if x - ox < aw |
| 508 |
then aux ox oy aw sh a |
| 509 |
else aux (ox + aw) oy bw sh b |
| 510 |
| Y (a, b) -> |
| 511 |
let ah, bh = split ~a:a.h ~sa:a.sh ~b:b.h ~sb:b.sh sh in |
| 512 |
if y - oy < ah |
| 513 |
then aux ox oy sw ah a |
| 514 |
else aux ox (oy + ah) sw bh b |
| 515 |
| Z (a, b) -> |
| 516 |
aux ox oy sw sh b || aux ox oy sw sh a |
| 517 |
| Mouse_handler (t, f) -> |
| 518 |
let _offsetx, rw = pack ~fixed:t.w ~stretch:t.sw sw `Negative `Negative |
| 519 |
and _offsety, rh = pack ~fixed:t.h ~stretch:t.sh sh `Negative `Negative |
| 520 |
in |
| 521 |
assert (_offsetx = 0 && _offsety = 0); |
| 522 |
(x - ox >= 0 && x - ox <= rw && y - oy >= 0 && y - oy <= rh) && |
| 523 |
(aux ox oy sw sh t || handle ox oy f) |
| 524 |
| Size_sensor (desc, _) |
| 525 |
| Transient_sensor (desc, _) | Permanent_sensor (desc, _) |
| 526 |
| Focus_area (desc, _) -> |
| 527 |
aux ox oy sw sh desc |
| 528 |
| Shift_area (desc, sx, sy) -> |
| 529 |
aux (ox - sx) (oy - sy) sw sh desc |
| 530 |
| Resize (t, g, _bg) -> |
| 531 |
let open Gravity in |
| 532 |
let dx, rw = pack ~fixed:t.w ~stretch:t.sw sw (h (p1 g)) (h (p2 g)) in |
| 533 |
let dy, rh = pack ~fixed:t.h ~stretch:t.sh sh (v (p1 g)) (v (p2 g)) in |
| 534 |
aux (ox + dx) (oy + dy) rw rh t |
| 535 |
| Event_filter (n, f) -> |
| 536 |
begin match f (`Mouse (`Press btn, (x, y), [])) with |
| 537 |
| `Handled -> true |
| 538 |
| `Unhandled -> aux ox oy sw sh n |
| 539 |
end |
| 540 |
in |
| 541 |
aux 0 0 w h t |
| 542 |
|
| 543 |
let release_grab st x y = |
| 544 |
match st.mouse_grab with |
| 545 |
| None -> () |
| 546 |
| Some (_, release) -> |
| 547 |
st.mouse_grab <- None; |
| 548 |
release ~x ~y |
| 549 |
|
| 550 |
let dispatch_mouse t (event, (x, y), _mods) = |
| 551 |
if |
| 552 |
match event with |
| 553 |
| `Press btn -> |
| 554 |
release_grab t x y; |
| 555 |
let w, h = t.size in |
| 556 |
dispatch_mouse t x y btn w h t.view |
| 557 |
| `Drag -> |
| 558 |
begin match t.mouse_grab with |
| 559 |
| None -> false |
| 560 |
| Some (drag, _) -> drag ~x ~y; true |
| 561 |
end |
| 562 |
| `Release -> |
| 563 |
release_grab t x y; true |
| 564 |
then `Handled |
| 565 |
else `Unhandled |
| 566 |
|
| 567 |
let resize_canvas rw rh image = |
| 568 |
let w = I.width image in |
| 569 |
let h = I.height image in |
| 570 |
if w <> rw || h <> rh |
| 571 |
then I.pad ~r:(rw - w) ~b:(rh - h) image |
| 572 |
else image |
| 573 |
|
| 574 |
let resize_canvas2 ox oy rw rh image = |
| 575 |
let w = I.width image in |
| 576 |
let h = I.height image in |
| 577 |
I.pad ~l:ox ~t:oy ~r:(rw - w - ox) ~b:(rh - h - oy) image |
| 578 |
|
| 579 |
let same_size w h image = |
| 580 |
w = I.width image && |
| 581 |
h = I.height image |
| 582 |
|
| 583 |
let rec render_node vx1 vy1 vx2 vy2 sw sh t : cache = |
| 584 |
if |
| 585 |
let cache = t.cache in |
| 586 |
vx1 >= Interval.fst cache.vx && vy1 >= Interval.fst cache.vy && |
| 587 |
vx2 <= Interval.snd cache.vx && vy2 <= Interval.snd cache.vy && |
| 588 |
same_size sw sh cache.image |
| 589 |
then t.cache |
| 590 |
else if vx2 < 0 || vy2 < 0 || sw < vx1 || sh < vy1 then |
| 591 |
let vx = Interval.make vx1 vx2 and vy = Interval.make vy1 vy2 in |
| 592 |
{ vx; vy; image = I.void sw sh } |
| 593 |
else |
| 594 |
let cache = match t.desc with |
| 595 |
| Atom image -> |
| 596 |
{ vx = Interval.make 0 sw; |
| 597 |
vy = Interval.make 0 sh; |
| 598 |
image = resize_canvas sw sh image } |
| 599 |
| Size_sensor (desc, handler) -> |
| 600 |
handler ~w:sw ~h:sh; |
| 601 |
render_node vx1 vy1 vx2 vy2 sw sh desc |
| 602 |
| Transient_sensor (desc, _) | Permanent_sensor (desc, _) -> |
| 603 |
render_node vx1 vy1 vx2 vy2 sw sh desc |
| 604 |
| Focus_area (desc, _) | Mouse_handler (desc, _) -> |
| 605 |
render_node vx1 vy1 vx2 vy2 sw sh desc |
| 606 |
| Shift_area (t', sx, sy) -> |
| 607 |
let cache = render_node |
| 608 |
(vx1 + sx) (vy1 + sy) (vx2 + sx) (vy2 + sy) (sx + sw) (sy + sh) t' |
| 609 |
in |
| 610 |
let vx = Interval.make vx1 vx2 and vy = Interval.make vy1 vy2 in |
| 611 |
let image = resize_canvas sw sh (I.crop ~l:sx ~t:sy cache.image) in |
| 612 |
{ vx; vy; image } |
| 613 |
| X (a, b) -> |
| 614 |
let aw, bw = split ~a:a.w ~sa:a.sw ~b:b.w ~sb:b.sw sw in |
| 615 |
let ca = render_node vx1 vy1 vx2 vy2 aw sh a in |
| 616 |
let cb = render_node (vx1 - aw) vy1 (vx2 - aw) vy2 bw sh b in |
| 617 |
let vx = Interval.make |
| 618 |
(maxi (Interval.fst ca.vx) (Interval.fst cb.vx + aw)) |
| 619 |
(mini (Interval.snd ca.vx) (Interval.snd cb.vx + aw)) |
| 620 |
and vy = Interval.make |
| 621 |
(maxi (Interval.fst ca.vy) (Interval.fst cb.vy)) |
| 622 |
(mini (Interval.snd ca.vy) (Interval.snd cb.vy)) |
| 623 |
and image = resize_canvas sw sh (I.(<|>) ca.image cb.image) in |
| 624 |
{ vx; vy; image } |
| 625 |
| Y (a, b) -> |
| 626 |
let ah, bh = split ~a:a.h ~sa:a.sh ~b:b.h ~sb:b.sh sh in |
| 627 |
let ca = render_node vx1 vy1 vx2 vy2 sw ah a in |
| 628 |
let cb = render_node vx1 (vy1 - ah) vx2 (vy2 - ah) sw bh b in |
| 629 |
let vx = Interval.make |
| 630 |
(maxi (Interval.fst ca.vx) (Interval.fst cb.vx)) |
| 631 |
(mini (Interval.snd ca.vx) (Interval.snd cb.vx)) |
| 632 |
and vy = Interval.make |
| 633 |
(maxi (Interval.fst ca.vy) (Interval.fst cb.vy + ah)) |
| 634 |
(mini (Interval.snd ca.vy) (Interval.snd cb.vy + ah)) |
| 635 |
and image = resize_canvas sw sh (I.(<->) ca.image cb.image) in |
| 636 |
{ vx; vy; image } |
| 637 |
| Z (a, b) -> |
| 638 |
let ca = render_node vx1 vy1 vx2 vy2 sw sh a in |
| 639 |
let cb = render_node vx1 vy1 vx2 vy2 sw sh b in |
| 640 |
let vx = Interval.make |
| 641 |
(maxi (Interval.fst ca.vx) (Interval.fst cb.vx)) |
| 642 |
(mini (Interval.snd ca.vx) (Interval.snd cb.vx)) |
| 643 |
and vy = Interval.make |
| 644 |
(maxi (Interval.fst ca.vy) (Interval.fst cb.vy)) |
| 645 |
(mini (Interval.snd ca.vy) (Interval.snd cb.vy)) |
| 646 |
and image = resize_canvas sw sh (I.(</>) cb.image ca.image) in |
| 647 |
{ vx; vy; image } |
| 648 |
| Resize (t, g, bg) -> |
| 649 |
let open Gravity in |
| 650 |
let dx, rw = pack ~fixed:t.w ~stretch:t.sw sw (h (p1 g)) (h (p2 g)) in |
| 651 |
let dy, rh = pack ~fixed:t.h ~stretch:t.sh sh (v (p1 g)) (v (p2 g)) in |
| 652 |
let c = |
| 653 |
render_node (vx1 - dx) (vy1 - dy) (vx2 - dx) (vy2 - dy) rw rh t |
| 654 |
in |
| 655 |
let image = resize_canvas2 dx dy sw sh c.image in |
| 656 |
let image = |
| 657 |
if bg != A.empty then |
| 658 |
I.(image </> char bg ' ' sw sh) |
| 659 |
else |
| 660 |
image |
| 661 |
in |
| 662 |
let vx = Interval.shift c.vx dx in |
| 663 |
let vy = Interval.shift c.vy dy in |
| 664 |
{ vx; vy; image } |
| 665 |
| Event_filter (t, _f) -> |
| 666 |
render_node vx1 vy1 vx2 vy2 sw sh t |
| 667 |
in |
| 668 |
t.cache <- cache; |
| 669 |
cache |
| 670 |
|
| 671 |
let image {size = (w, h); view; _} = |
| 672 |
(render_node 0 0 w h w h view).image |
| 673 |
|
| 674 |
let dispatch_raw_key st key = |
| 675 |
let rec iter (st: ui list) : [> `Unhandled] = |
| 676 |
match st with |
| 677 |
| [] -> `Unhandled |
| 678 |
| ui :: tl -> |
| 679 |
begin match ui.desc with |
| 680 |
| Atom _ -> iter tl |
| 681 |
| X (a, b) | Y (a, b) | Z (a, b) -> |
| 682 |
(* Try left/top most branch first *) |
| 683 |
let st' = |
| 684 |
if Focus.has_focus b.focus |
| 685 |
then b :: tl |
| 686 |
else a :: b :: tl |
| 687 |
in |
| 688 |
iter st' |
| 689 |
| Focus_area (t, f) -> |
| 690 |
begin match iter [t] with |
| 691 |
| `Handled -> `Handled |
| 692 |
| `Unhandled -> |
| 693 |
match f key with |
| 694 |
| `Handled -> `Handled |
| 695 |
| `Unhandled -> iter tl |
| 696 |
end |
| 697 |
| Mouse_handler (t, _) | Size_sensor (t, _) |
| 698 |
| Transient_sensor (t, _) | Permanent_sensor (t, _) |
| 699 |
| Shift_area (t, _, _) | Resize (t, _, _) -> |
| 700 |
iter (t :: tl) |
| 701 |
| Event_filter (t, f) -> |
| 702 |
begin match f (`Key key) with |
| 703 |
| `Unhandled -> iter (t :: tl) |
| 704 |
| `Handled -> `Handled |
| 705 |
end |
| 706 |
end |
| 707 |
in |
| 708 |
iter [st.view] |
| 709 |
|
| 710 |
exception Acquired_focus |
| 711 |
|
| 712 |
let grab_focus ui = |
| 713 |
let rec aux ui = |
| 714 |
match ui.focus with |
| 715 |
| Focus.Empty -> () |
| 716 |
| Focus.Handle (_, v) -> Focus.request_var v; raise Acquired_focus |
| 717 |
| Focus.Conflict _ -> iter aux ui |
| 718 |
in |
| 719 |
try aux ui; false with Acquired_focus -> true |
| 720 |
|
| 721 |
let rec dispatch_focus t dir = |
| 722 |
match t.desc with |
| 723 |
| Atom _ -> false |
| 724 |
| Mouse_handler (t, _) | Size_sensor (t, _) |
| 725 |
| Transient_sensor (t, _) | Permanent_sensor (t, _) |
| 726 |
| Shift_area (t, _, _) | Resize (t, _, _) | Event_filter (t, _) -> |
| 727 |
dispatch_focus t dir |
| 728 |
| Focus_area (t', _) -> |
| 729 |
if Focus.has_focus t'.focus then |
| 730 |
dispatch_focus t' dir || grab_focus t |
| 731 |
else if Focus.has_focus t.focus then |
| 732 |
false |
| 733 |
else |
| 734 |
grab_focus t |
| 735 |
| X (a, b) -> |
| 736 |
begin if Focus.has_focus a.focus then |
| 737 |
dispatch_focus a dir || |
| 738 |
(match dir with |
| 739 |
| `Next | `Right -> dispatch_focus b dir |
| 740 |
| _ -> false |
| 741 |
) |
| 742 |
else if Focus.has_focus b.focus then |
| 743 |
dispatch_focus b dir || |
| 744 |
(match dir with |
| 745 |
| `Prev | `Left -> dispatch_focus a dir |
| 746 |
| _ -> false |
| 747 |
) |
| 748 |
else |
| 749 |
match dir with |
| 750 |
| `Prev | `Left | `Up -> dispatch_focus b dir || dispatch_focus a dir |
| 751 |
| `Next | `Down | `Right -> dispatch_focus a dir || dispatch_focus b dir |
| 752 |
end |
| 753 |
| Y (a, b) -> |
| 754 |
begin if Focus.has_focus a.focus then |
| 755 |
dispatch_focus a dir || |
| 756 |
(match dir with |
| 757 |
| `Next | `Down -> dispatch_focus b dir |
| 758 |
| _ -> false |
| 759 |
) |
| 760 |
else if Focus.has_focus b.focus then |
| 761 |
dispatch_focus b dir || |
| 762 |
(match dir with |
| 763 |
| `Prev | `Up -> dispatch_focus a dir |
| 764 |
| _ -> false |
| 765 |
) |
| 766 |
else match dir with |
| 767 |
| `Prev | `Up -> dispatch_focus b dir || dispatch_focus a dir |
| 768 |
| `Next | `Left | `Down | `Right -> dispatch_focus a dir || dispatch_focus b dir |
| 769 |
end |
| 770 |
| Z (a, b) -> |
| 771 |
if Focus.has_focus a.focus then |
| 772 |
dispatch_focus a dir |
| 773 |
else |
| 774 |
dispatch_focus b dir || dispatch_focus a dir |
| 775 |
|
| 776 |
let rec dispatch_key st key = |
| 777 |
match dispatch_raw_key st key, key with |
| 778 |
| `Handled, _ -> `Handled |
| 779 |
| `Unhandled, (`Arrow dir, [`Meta]) -> |
| 780 |
let dir : [`Down | `Left | `Right | `Up] :> |
| 781 |
[`Down | `Left | `Right | `Up | `Next | `Prev] = dir in |
| 782 |
dispatch_key st (`Focus dir, [`Meta]) |
| 783 |
| `Unhandled, (`Tab, mods) -> |
| 784 |
let dir = if List.mem `Shift mods then `Prev else `Next in |
| 785 |
dispatch_key st (`Focus dir, mods) |
| 786 |
| `Unhandled, (`Focus dir, _) -> |
| 787 |
if dispatch_focus st.view dir then `Handled else `Unhandled |
| 788 |
| `Unhandled, _ -> `Unhandled |
| 789 |
|
| 790 |
let dispatch_event t = function |
| 791 |
| `Key key -> dispatch_key t key |
| 792 |
| `Mouse mouse -> dispatch_mouse t mouse |
| 793 |
| `Paste _ -> `Unhandled |
| 794 |
end |
| 795 |
|
| 796 |
module Ui_loop = |
| 797 |
struct |
| 798 |
open Notty_unix |
| 799 |
|
| 800 |
(* FIXME Uses of [quick_sample] and [quick_release] should be replaced by |
| 801 |
[sample] and [release] with the appropriate release management. *) |
| 802 |
|
| 803 |
let step ?(process_event=true) ?(timeout=(-1.0)) ~renderer term root = |
| 804 |
let size = Term.size term in |
| 805 |
let image = |
| 806 |
let rec stabilize () = |
| 807 |
let tree = Lwd.quick_sample root in |
| 808 |
Renderer.update renderer size tree; |
| 809 |
let image = Renderer.image renderer in |
| 810 |
if Lwd.is_damaged root |
| 811 |
then stabilize () |
| 812 |
else image |
| 813 |
in |
| 814 |
stabilize () |
| 815 |
in |
| 816 |
Term.image term image; |
| 817 |
if process_event then |
| 818 |
let i, _ = Term.fds term in |
| 819 |
let has_event = |
| 820 |
let rec select () = |
| 821 |
match Unix.select [i] [] [i] timeout with |
| 822 |
| [], [], [] -> false |
| 823 |
| _ -> true |
| 824 |
| exception (Unix.Unix_error (Unix.EINTR, _, _)) -> select () |
| 825 |
in |
| 826 |
select () |
| 827 |
in |
| 828 |
if has_event then |
| 829 |
match Term.event term with |
| 830 |
| `End -> () |
| 831 |
| `Resize _ -> () |
| 832 |
| #Unescape.event as event -> |
| 833 |
let event = (event : Unescape.event :> Ui.event) in |
| 834 |
ignore (Renderer.dispatch_event renderer event : [`Handled | `Unhandled]) |
| 835 |
|
| 836 |
let run_with_term term ?tick_period ?(tick=ignore) ~renderer quit t = |
| 837 |
let quit = Lwd.observe (Lwd.get quit) in |
| 838 |
let root = Lwd.observe t in |
| 839 |
let rec loop () = |
| 840 |
let quit = Lwd.quick_sample quit in |
| 841 |
if not quit then ( |
| 842 |
step ~process_event:true ?timeout:tick_period ~renderer term root; |
| 843 |
tick (); |
| 844 |
loop () |
| 845 |
) |
| 846 |
in |
| 847 |
loop (); |
| 848 |
ignore (Lwd.quick_release root); |
| 849 |
ignore (Lwd.quick_release quit) |
| 850 |
|
| 851 |
let run ?tick_period ?tick ?term ?(renderer=Renderer.make ()) |
| 852 |
?quit ?(quit_on_escape=true) ?(quit_on_ctrl_q=true) t = |
| 853 |
let quit = match quit with |
| 854 |
| Some quit -> quit |
| 855 |
| None -> Lwd.var false |
| 856 |
in |
| 857 |
let t = Lwd.map t ~f:(Ui.event_filter (function |
| 858 |
| `Key (`ASCII 'Q', [`Ctrl]) when quit_on_ctrl_q -> |
| 859 |
Lwd.set quit true; `Handled |
| 860 |
| `Key (`Escape, []) when quit_on_escape -> |
| 861 |
Lwd.set quit true; `Handled |
| 862 |
| _ -> `Unhandled |
| 863 |
)) |
| 864 |
in |
| 865 |
match term with |
| 866 |
| Some term -> run_with_term term ?tick_period ?tick ~renderer quit t |
| 867 |
| None -> |
| 868 |
let term = Term.create () in |
| 869 |
run_with_term term ?tick_period ?tick ~renderer quit t; |
| 870 |
Term.release term |
| 871 |
|
| 872 |
end |