1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

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)

This commit is contained in:
Tim Marston 2023-01-31 16:22:20 +00:00 committed by GitHub
parent a932a8b1ea
commit 26b9464f51
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 38 additions and 32 deletions

View File

@ -5,12 +5,13 @@ import gx
import sokol.sapp import sokol.sapp
fn main() { fn main() {
gg.new_context( mut ctx := gg.new_context(
bg_color: gx.white bg_color: gx.white
window_title: 'Cursor' window_title: 'Cursor'
frame_fn: frame frame_fn: frame
init_fn: init init_fn: init
).run() )
ctx.run()
} }
fn init(mut ctx gg.Context) { fn init(mut ctx gg.Context) {

View File

@ -13,7 +13,7 @@ struct Pixel {
color gx.Color color gx.Color
} }
struct App { pub struct App {
pub: pub:
args simargs.ParallelArgs args simargs.ParallelArgs
request_chan chan &sim.SimRequest request_chan chan &sim.SimRequest

View File

@ -458,39 +458,42 @@ pub fn new_context(cfg Config) &Context {
ft: 0 ft: 0
ui_mode: cfg.ui_mode ui_mode: cfg.ui_mode
native_rendering: cfg.native_rendering native_rendering: cfg.native_rendering
} window: sapp.Desc{
if cfg.user_data == unsafe { nil } { init_userdata_cb: gg_init_sokol_window
ctx.user_data = ctx 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) ctx.set_bg_color(cfg.bg_color)
// C.printf('new_context() %p\n', cfg.user_data) // 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 return ctx
} }
// run starts the main loop of the context. // 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) sapp.run(&ctx.window)
} }

View File

@ -328,9 +328,6 @@ pub fn new_context(cfg Config) &Context {
sz.height = g.height sz.height = g.height
sz.width = g.width sz.width = g.width
g.config = cfg g.config = cfg
if isnil(cfg.user_data) {
g.user_data = g
}
g.window = dom.window() g.window = dom.window()
document := dom.document document := dom.document
canvas_elem := document.getElementById(cfg.canvas.str) or { 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() { 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) gg_animation_frame_fn(mut ctx)
} }