From 68414e99c1b6716d5819020376472f3d26cb53df Mon Sep 17 00:00:00 2001 From: Keryan Didier Date: Fri, 31 May 2024 11:24:43 +0200 Subject: [PATCH] Targeted canvas placement --- src/ocamlCanvas.ml | 2 +- src/ocamlCanvas.mli | 7 +++++-- src/stubs/ml_canvas.c | 6 ++++-- src/stubs/ml_canvas.js | 16 +++++++++++----- 4 files changed, 21 insertions(+), 10 deletions(-) diff --git a/src/ocamlCanvas.ml b/src/ocamlCanvas.ml index 5458f3e9..023e9051 100644 --- a/src/ocamlCanvas.ml +++ b/src/ocamlCanvas.ml @@ -542,7 +542,7 @@ module V1 = struct external createOnscreen : ?autocommit:bool -> ?decorated:bool -> ?resizeable:bool -> ?minimize:bool -> ?maximize:bool -> ?close:bool -> ?title:string -> - ?pos:(int * int) -> size:(int * int) -> unit -> t + ?target:string -> ?pos:(int * int) -> size:(int * int) -> unit -> t = "ml_canvas_create_onscreen" "ml_canvas_create_onscreen_n" external createOffscreen : size:(int * int) -> unit -> t diff --git a/src/ocamlCanvas.mli b/src/ocamlCanvas.mli index e94e0ede..14dff782 100644 --- a/src/ocamlCanvas.mli +++ b/src/ocamlCanvas.mli @@ -766,9 +766,9 @@ module V1 : sig val createOnscreen : ?autocommit:bool -> ?decorated:bool -> ?resizeable:bool -> ?minimize:bool -> ?maximize:bool -> ?close:bool -> ?title:string -> - ?pos:(int * int) -> size:(int * int) -> unit -> t + ?target:string -> ?pos:(int * int) -> size:(int * int) -> unit -> t (** [createOnscreen ?autocommit ?decorated ?resizeable ?minimize - ?maximize ?close ?title ?pos ~size ()] creates a windowed + ?maximize ?close ?title ?target ?pos ~size()] creates a windowed canvas of size [size]. The window title and position can be specified using the optional arguments [title] and [pos]. The window decorations, which are active by default, can @@ -777,6 +777,9 @@ module V1 : sig The [decorated] argument has a higher priority: if set to false, all other decoration arguments will be ignored (considered to be false), and all decorations will be removed from the window. + The [target] option is relevant only for the Javascript backend. + It indicates the element id in which the canvas should be placed, + default to the html body. The [autocommit] option, which is active by default, indicates whether the canvas should be automatically presented after each frame event. See {!Canvas.commit} for more info on [autocommit]. diff --git a/src/stubs/ml_canvas.c b/src/stubs/ml_canvas.c index 8b060ce8..7352e6a4 100644 --- a/src/stubs/ml_canvas.c +++ b/src/stubs/ml_canvas.c @@ -673,12 +673,14 @@ ml_canvas_create_onscreen_n( value mlMaximize, /* bool, optional, default = true */ value mlClose, /* bool, optional, default = true */ value mlTitle, /* string, optional, default = "" */ + value mlTarget, /* string, optional */ value mlPos, /* (int * int), optional */ value mlSize, /* (int * int), non-optional */ value mlUnit) { CAMLparam5(mlAutocommit, mlDecorated, mlResizeable, mlMinimize, mlMaximize); - CAMLxparam5(mlClose, mlTitle, mlPos, mlSize, mlUnit); + CAMLxparam5(mlClose, mlTitle, mlTarget, mlPos, mlSize); + CAMLxparam1(mlUnit); CAMLlocal1(mlCanvas); _ml_canvas_ensure_initialized(); int32_t width = Int31_val_clip(Field(mlSize, 0)); @@ -708,7 +710,7 @@ ml_canvas_create_onscreen_n( CAMLreturn(mlCanvas); } -BYTECODE_STUB_10(ml_canvas_create_onscreen) +BYTECODE_STUB_11(ml_canvas_create_onscreen) CAMLprim value ml_canvas_create_offscreen( diff --git a/src/stubs/ml_canvas.js b/src/stubs/ml_canvas.js index 90eed615..416e7bbc 100644 --- a/src/stubs/ml_canvas.js +++ b/src/stubs/ml_canvas.js @@ -112,7 +112,7 @@ function _header_down_handler(e) { _move.target = e.target.canvas.frame; _move.prev_x = e.pageX; _move.prev_y = e.pageY; - document.body.insertBefore(_move.target, null); + e.target.canvas.target.insertBefore(_move.target, null); } return false; } @@ -123,7 +123,7 @@ function _header_down_handler(e) { function _surface_down_handler(e) { if (e.target !== null) { _focus = e.target.canvas; - document.body.insertBefore(e.target.canvas.frame, null); + e.target.canvas.target.insertBefore(e.target.canvas.frame, null); var evt = [EVENT_TAG.BUTTON_ACTION, [0, e.target.canvas, caml_int64_of_float(e.timeStamp * 1000.0), @@ -634,8 +634,8 @@ function _ml_canvas_decorate(header, minimize, //Requires: _ml_canvas_ensure_initialized, _ml_canvas_valid_canvas_size, _resize, _next_id, _header_down_handler //Requires: _surface_down_handler, _up_handler, _move_handler, _ml_canvas_decorate, Optional_bool_val, Optional_val //Requires: caml_invalid_argument -function ml_canvas_create_onscreen(autocmmit, decorated, resizeable, minimize, - maximize, close, title, pos, size) { +function ml_canvas_create_onscreen(autocommit, decorated, resizeable, minimize, + maximize, close, title, target, pos, size) { _ml_canvas_ensure_initialized(); @@ -656,11 +656,17 @@ function ml_canvas_create_onscreen(autocmmit, decorated, resizeable, minimize, var maximize = Optional_bool_val(maximize, true); var close = Optional_bool_val(close, true); var title = Optional_val(title, null); + var target = Optional_val(target, null); + target = document.getElementById(target); + if(target == null) { + target = document.body; + } var id = ++_next_id; var canvas = { name: title, + target: target, frame: null, header: null, surface: null, @@ -691,7 +697,7 @@ function ml_canvas_create_onscreen(autocmmit, decorated, resizeable, minimize, frame.oncontextmenu = function() { return false; } frame.canvas = canvas; canvas.frame = frame; - document.body.appendChild(frame); + target.appendChild(frame); var header = null; if (decorated === true) {