-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
explore a unified attribute interface
- Loading branch information
1 parent
1ce151b
commit 33c4438
Showing
8 changed files
with
113 additions
and
76 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
module StringSet = Set.Make (struct | ||
type t = string | ||
|
||
let compare a b = | ||
let a = String.lowercase_ascii a in | ||
let b = String.lowercase_ascii b in | ||
String.compare a b | ||
end) | ||
|
||
let attributes_to_skip = StringSet.of_list [ "xmlns"; "xmlns:xlink" ] | ||
|
||
module Event_handler = struct | ||
type t = | ||
| Mouse of (Brr.Ev.Mouse.t Brr.Ev.t -> unit) | ||
| Keyboard of (Brr.Ev.Keyboard.t Brr.Ev.t -> unit) | ||
| Raw of (Jv.t -> unit) | ||
end | ||
|
||
type t = | ||
| Attr of string * Jv.t | ||
| Event of | ||
{ on : string | ||
; handler : Event_handler.t | ||
} | ||
|
||
type attrs = | ||
{ attr : (string * Jv.t) list | ||
; event : (string * Jv.t) list | ||
} | ||
|
||
let make_attr k f x = Attr (k, f x) | ||
let bool k v = make_attr k Jv.of_bool v | ||
let int k v = make_attr k Jv.of_int v | ||
let float k v = make_attr k Jv.of_float v | ||
let string k v = make_attr k Jv.of_string v | ||
let click handler = Event { on = "click"; handler = Event_handler.Mouse handler } | ||
let mouse_event on handler = Event { on; handler = Event_handler.Mouse handler } | ||
let keyboard_event on handler = Event { on; handler = Event_handler.Keyboard handler } | ||
let unsafe_event on handler = Event { on; handler = Event_handler.Raw handler } | ||
|
||
let attrs_to_jv xs = | ||
let obj = | ||
List.fold_left | ||
(fun acc attr -> | ||
match attr with | ||
| Attr (k, v) -> | ||
if StringSet.mem k attributes_to_skip | ||
then acc | ||
else { acc with attr = (k, v) :: acc.attr } | ||
| Event { on; handler } -> { acc with event = (on, Jv.repr handler) :: acc.event }) | ||
{ attr = []; event = [] } | ||
xs | ||
in | ||
Jv.obj | ||
[| "attrs", Jv.obj (Array.of_list obj.attr); "on", Jv.obj (Array.of_list obj.event) |] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
type t | ||
|
||
val bool : string -> bool -> t | ||
val int : string -> int -> t | ||
val float : string -> float -> t | ||
val string : string -> string -> t | ||
val click : (Brr.Ev.Mouse.t Brr.Ev.t -> unit) -> t | ||
val mouse_event : string -> (Brr.Ev.Mouse.t Brr.Ev.t -> unit) -> t | ||
val keyboard_event : string -> (Brr.Ev.Keyboard.t Brr.Ev.t -> unit) -> t | ||
val unsafe_event : string -> (Jv.t -> unit) -> t | ||
val attrs_to_jv : t list -> Jv.t |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters