From 26b9464f51eb8b31412ff6ac83f991912ed8db8f Mon Sep 17 00:00:00 2001 From: Tim Marston Date: Tue, 31 Jan 2023 16:22:20 +0000 Subject: [PATCH] gg: setup ctx.window.user_data and ctx.user_data on ctx.run(), instead of in gg.new_context, to allow for embedding gg.Context in `ui` (#17169) --- examples/gg/cursor.v | 5 +- .../modules/sim/anim/app.v | 2 +- vlib/gg/gg.c.v | 55 ++++++++++--------- vlib/gg/gg.js.v | 8 ++- 4 files changed, 38 insertions(+), 32 deletions(-) diff --git a/examples/gg/cursor.v b/examples/gg/cursor.v index c883e9b04..7a3812bf1 100644 --- a/examples/gg/cursor.v +++ b/examples/gg/cursor.v @@ -5,12 +5,13 @@ import gx import sokol.sapp fn main() { - gg.new_context( + mut ctx := gg.new_context( bg_color: gx.white window_title: 'Cursor' frame_fn: frame init_fn: init - ).run() + ) + ctx.run() } fn init(mut ctx gg.Context) { diff --git a/examples/pendulum-simulation/modules/sim/anim/app.v b/examples/pendulum-simulation/modules/sim/anim/app.v index d393c0b4e..4262c8687 100644 --- a/examples/pendulum-simulation/modules/sim/anim/app.v +++ b/examples/pendulum-simulation/modules/sim/anim/app.v @@ -13,7 +13,7 @@ struct Pixel { color gx.Color } -struct App { +pub struct App { pub: args simargs.ParallelArgs request_chan chan &sim.SimRequest diff --git a/vlib/gg/gg.c.v b/vlib/gg/gg.c.v index 53035e56c..41c7dcde5 100644 --- a/vlib/gg/gg.c.v +++ b/vlib/gg/gg.c.v @@ -458,39 +458,42 @@ pub fn new_context(cfg Config) &Context { ft: 0 ui_mode: cfg.ui_mode native_rendering: cfg.native_rendering - } - if cfg.user_data == unsafe { nil } { - ctx.user_data = ctx + window: sapp.Desc{ + init_userdata_cb: gg_init_sokol_window + frame_userdata_cb: gg_frame_fn + event_userdata_cb: gg_event_fn + fail_userdata_cb: gg_fail_fn + cleanup_userdata_cb: gg_cleanup_fn + window_title: &char(cfg.window_title.str) + html5_canvas_name: &char(cfg.window_title.str) + width: cfg.width + height: cfg.height + sample_count: cfg.sample_count + high_dpi: true + fullscreen: cfg.fullscreen + __v_native_render: cfg.native_rendering + // drag&drop + enable_dragndrop: cfg.enable_dragndrop + max_dropped_files: cfg.max_dropped_files + max_dropped_file_path_length: cfg.max_dropped_file_path_length + swap_interval: cfg.swap_interval + } } ctx.set_bg_color(cfg.bg_color) // C.printf('new_context() %p\n', cfg.user_data) - window := sapp.Desc{ - user_data: ctx - init_userdata_cb: gg_init_sokol_window - frame_userdata_cb: gg_frame_fn - event_userdata_cb: gg_event_fn - fail_userdata_cb: gg_fail_fn - cleanup_userdata_cb: gg_cleanup_fn - window_title: &char(cfg.window_title.str) - html5_canvas_name: &char(cfg.window_title.str) - width: cfg.width - height: cfg.height - sample_count: cfg.sample_count - high_dpi: true - fullscreen: cfg.fullscreen - __v_native_render: cfg.native_rendering - // drag&drop - enable_dragndrop: cfg.enable_dragndrop - max_dropped_files: cfg.max_dropped_files - max_dropped_file_path_length: cfg.max_dropped_file_path_length - swap_interval: cfg.swap_interval - } - ctx.window = window return ctx } // run starts the main loop of the context. -pub fn (ctx &Context) run() { +pub fn (mut ctx Context) run() { + // set context late, in case it changed (e.g., due to embedding) + ctx.window = sapp.Desc{ + ...ctx.window + user_data: ctx + } + if ctx.user_data == unsafe { nil } { + ctx.user_data = ctx + } sapp.run(&ctx.window) } diff --git a/vlib/gg/gg.js.v b/vlib/gg/gg.js.v index fef344976..4ca9366b8 100644 --- a/vlib/gg/gg.js.v +++ b/vlib/gg/gg.js.v @@ -328,9 +328,6 @@ pub fn new_context(cfg Config) &Context { sz.height = g.height sz.width = g.width g.config = cfg - if isnil(cfg.user_data) { - g.user_data = g - } g.window = dom.window() document := dom.document canvas_elem := document.getElementById(cfg.canvas.str) or { @@ -453,6 +450,11 @@ pub fn new_context(cfg Config) &Context { } pub fn (mut ctx Context) run() { + // set context late, in case it changed (e.g., due to embedding) + if isnil(ctx.user_data) { + ctx.user_data = ctx + } + gg_animation_frame_fn(mut ctx) } -- 2.30.2