diff --git a/lib/brrer/brrer.ml b/lib/brrer/brrer.ml index ffec73c..ac069b4 100644 --- a/lib/brrer/brrer.ml +++ b/lib/brrer/brrer.ml @@ -69,3 +69,5 @@ module Brr_canvas = Brr_canvas module Fut = Fut module Intersection_observer = Intersection_observer module Mutation_observer = Mutation_observer +module Resize_observer = Resize_observer +module Dom_rect_read_only = Dom_rect_read_only diff --git a/lib/brrer/dom_rect_read_only.ml b/lib/brrer/dom_rect_read_only.ml new file mode 100644 index 0000000..792be94 --- /dev/null +++ b/lib/brrer/dom_rect_read_only.ml @@ -0,0 +1,12 @@ +type t = Jv.t + +external of_jv : Jv.t -> t = "%identity" + +let x t = Jv.get t "x" |> Jv.to_int +let y t = Jv.get t "y" |> Jv.to_int +let width t = Jv.get t "width" |> Jv.to_int +let height t = Jv.get t "height" |> Jv.to_int +let top t = Jv.get t "top" |> Jv.to_int +let right t = Jv.get t "right" |> Jv.to_int +let bottom t = Jv.get t "bottom" |> Jv.to_int +let left t = Jv.get t "left" |> Jv.to_int diff --git a/lib/brrer/dom_rect_read_only.mli b/lib/brrer/dom_rect_read_only.mli new file mode 100644 index 0000000..8264cbe --- /dev/null +++ b/lib/brrer/dom_rect_read_only.mli @@ -0,0 +1,15 @@ +(** Bindings to DOMRectReadOnly + + https://developer.mozilla.org/en-US/docs/Web/API/DOMRectReadOnly *) + +type t + +val of_jv : Jv.t -> t +val x : t -> int +val y : t -> int +val width : t -> int +val height : t -> int +val top : t -> int +val right : t -> int +val bottom : t -> int +val left : t -> int diff --git a/lib/brrer/resize_observer.ml b/lib/brrer/resize_observer.ml new file mode 100644 index 0000000..357302b --- /dev/null +++ b/lib/brrer/resize_observer.ml @@ -0,0 +1,63 @@ +open Brr + +type t = Jv.t + +external of_jv : Jv.t -> t = "%identity" + +module Entry = struct + type t = Jv.t + + external of_jv : Jv.t -> t = "%identity" + + type box_size = { block_size : int; inline_size : int } + + let box_size_of_jv jv = + { + block_size = Jv.get jv "blockSize" |> Jv.to_int; + inline_size = Jv.get jv "inlineSize" |> Jv.to_int; + } + + let border_box_size t = Jv.get t "borderBoxSize" |> Jv.to_array box_size_of_jv + + let content_box_size t = + Jv.get t "contentBoxSize" |> Jv.to_array box_size_of_jv + + let content_rect t = Jv.get t "contentRect" |> Dom_rect_read_only.of_jv + + let device_pixel_content_box_size t = + Jv.get t "devicePixelContentBoxSize" |> Jv.to_array box_size_of_jv + + let target t = Jv.get t "target" |> El.of_jv +end + +type box = Content_box | Border_box | Device_pixel_content_box + +let string_of_box = function + | Content_box -> "content-box" + | Border_box -> "border-box" + | Device_pixel_content_box -> "device-pixel-content-box" + +let disconnect t = ignore @@ Jv.call t "disconnect" [||] + +let observe t ?box target = + let args = + let target = El.to_jv target in + match box with + | None -> [| target |] + | Some b -> + let box = Jv.obj [| ("box", Jv.of_string (string_of_box b)) |] in + [| target; box |] + in + Jv.call t "observe" args |> ignore + +let unobserve t target = + let target = El.to_jv target in + ignore @@ Jv.call t "disconnect" [| target |] + +let create ~callback = + let callback entries observer = + let entries = Jv.to_list Entry.of_jv entries in + callback entries (of_jv observer) + in + let args = [| Jv.repr callback |] in + Jv.new' (Jv.get Jv.global "ResizeObserver") args diff --git a/lib/brrer/resize_observer.mli b/lib/brrer/resize_observer.mli new file mode 100644 index 0000000..c62b050 --- /dev/null +++ b/lib/brrer/resize_observer.mli @@ -0,0 +1,29 @@ +(** Bindings to the ResizeObserver API + @see https://developer.mozilla.org/en-US/docs/Web/API/ResizeObserver +*) + +type t + +val of_jv : Jv.t -> t + +module Entry : sig + type t + + val of_jv : Jv.t -> t + + type box_size = { block_size : int; inline_size : int } + + val box_size_of_jv : t -> box_size + val border_box_size : t -> box_size array + val content_box_size : t -> box_size array + val content_rect : t -> Dom_rect_read_only.t + val device_pixel_content_box_size : t -> box_size array + val target : t -> Brr.El.t +end + +type box = Content_box | Border_box | Device_pixel_content_box + +val disconnect : t -> unit +val observe : t -> ?box:box -> Brr.El.t -> unit +val unobserve : t -> Brr.El.t -> unit +val create : callback:(Entry.t list -> t -> unit) -> t