Skip to content

Commit

Permalink
Targeted canvas placement
Browse files Browse the repository at this point in the history
  • Loading branch information
Keryan-dev committed May 31, 2024
1 parent e33ce2b commit 68414e9
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 10 deletions.
2 changes: 1 addition & 1 deletion src/ocamlCanvas.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 5 additions & 2 deletions src/ocamlCanvas.mli
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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].
Expand Down
6 changes: 4 additions & 2 deletions src/stubs/ml_canvas.c
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down Expand Up @@ -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(
Expand Down
16 changes: 11 additions & 5 deletions src/stubs/ml_canvas.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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),
Expand Down Expand Up @@ -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();

Expand All @@ -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,
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 68414e9

Please sign in to comment.