Skip to content

Commit 709d05a

Browse files
committed
[0729] 使用社区版 GLFW && 修复 WASM 版本光标不闪烁的问题
1 parent c980b20 commit 709d05a

5 files changed

Lines changed: 48 additions & 9 deletions

File tree

bin/wasm_server.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,28 +10,35 @@
1010
#if not os.path.exists(html) and os.path.exists(js):
1111
with open(html, "w", encoding="utf-8") as f:
1212
f.write("""<!doctype html>
13+
<!doctype html>
1314
<html>
1415
<head>
1516
<meta charset="utf-8">
1617
<title>stem</title>
1718
<style>
18-
html, body, canvas {
19+
html, body {
1920
margin: 0;
2021
width: 100%;
2122
height: 100%;
2223
overflow: hidden;
24+
background-color: #9f9f9f;
25+
}
26+
#main-canvas {
27+
display: block;
28+
width: 100%;
29+
height: 100%;
30+
outline: none;
2331
}
2432
</style>
2533
</head>
2634
<body>
27-
<canvas id="canvas"></canvas>
28-
35+
<canvas id="main-canvas" tabindex="1"></canvas>
36+
<!-- tabindex="1" 允许 main-canvas 被获得 focus -->
2937
<script>
3038
var Module = {
31-
canvas: document.getElementById('canvas')
39+
canvas: document.getElementById("main-canvas")
3240
};
3341
</script>
34-
3542
<script src="stem.js"></script>
3643
</body>
3744
</html>

devel/0729.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# [0729] 使用社区版 GLFW && 修复 WASM 版本光标不闪烁的问题
2+
3+
## What
4+
- 更改了编译器选项,使用社区维护的 C++ 版 GLFW
5+
- 修改 html 适配社区版 GLFW
6+
- 修改获取窗口大小的逻辑,resize相关逻辑,适配社区版 GLFW
7+
- 修复 WASM 版本光标不闪烁的问题
8+
9+
## Why
10+
- 社区版支持的 GLFW API 更多,具体参考(https://github.com/pongasoft/emscripten-glfw/blob/master/docs/Usage.md)
11+
- 改用社区版后,构建产物大小与先前没有区别
12+
- 社区版对窗口大小的处理逻辑与 Emscripten 提供的 JS 版本不同,因此修改相关逻辑做适配
13+
- 社区版实现了 `glfwSetWindowFocusCallback` API,因此虽然没有代码修改,但是解决了 WASM 版本光标不闪烁的问题

src/Plugins/ImGui/im_gui.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -460,6 +460,15 @@ gui_root_extents (SI& width, SI& height) {
460460
// QGuiApplication::primaryScreen()->size()
461461
int w= 1920, h= 1080;
462462
if (s_glfw_initialized) {
463+
#ifdef __EMSCRIPTEN__
464+
// Browsers do not expose a meaningful primary monitor to GLFW.
465+
// Use the current canvas CSS size as the root extent instead.
466+
double css_w, css_h;
467+
emscripten_get_element_css_size ("#main-canvas", &css_w, &css_h);
468+
width = (SI) css_w * PIXEL;
469+
height= (SI) css_h * PIXEL;
470+
return;
471+
#else
463472
GLFWmonitor* monitor= glfwGetPrimaryMonitor ();
464473
const GLFWvidmode* mode = monitor ? glfwGetVideoMode (monitor) : nullptr;
465474
if (mode) {

src/Plugins/ImGui/im_tm_widget.cpp

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737

3838
#ifdef __EMSCRIPTEN__
3939
#include <emscripten.h>
40+
#include <GLFW/emscripten_glfw3.h>
4041
#endif
4142

4243
#include <cmath>
@@ -233,14 +234,23 @@ im_tm_widget_rep::im_tm_widget_rep (int mask, command _quit)
233234

234235
float main_scale=
235236
ImGui_ImplGlfw_GetContentScaleForMonitor (glfwGetPrimaryMonitor ());
236-
const int win_w = 800; // size for init
237-
const int win_h = 600;
237+
SI win_w, win_h;
238+
gui_root_extents(win_w, win_h);
239+
win_w = win_w / PIXEL;
240+
win_h = win_h / PIXEL;
238241
GLFWmonitor* monitor= glfwGetPrimaryMonitor ();
239242
int wa_x= 0, wa_y= 0, wa_w= win_w, wa_h= win_h;
240243
if (monitor != nullptr)
241244
glfwGetMonitorWorkarea (monitor, &wa_x, &wa_y, &wa_w, &wa_h);
245+
#ifdef __EMSCRIPTEN__
246+
glfwWindowHint (GLFW_SCALE_FRAMEBUFFER, GLFW_TRUE); // Enable retina
247+
emscripten_glfw_set_next_window_canvas_selector("#main-canvas");
248+
#endif
242249
window= glfwCreateWindow ((int) win_w * main_scale, (int) win_h * main_scale,
243250
"Mogan (ImGui)", nullptr, nullptr);
251+
#ifdef __EMSCRIPTEN__
252+
emscripten_glfw_make_canvas_resizable(window, "window", nullptr); // emscripten 3.1.56 specific
253+
#endif
244254
if (window == nullptr) {
245255
return;
246256
}

xmake/targets/stem.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,9 +101,9 @@ target("stem") do
101101

102102
if is_plat("wasm") then
103103
add_cxxflags("-O3")
104-
add_cxxflags("-sUSE_GLFW=3")
104+
add_cxxflags("--use-port=contrib.glfw3")
105105
add_ldflags("-O3")
106-
add_ldflags("-sUSE_GLFW=3")
106+
add_ldflags("--use-port=contrib.glfw3")
107107
add_ldflags("-sINITIAL_MEMORY=512MB")
108108
add_ldflags("-sALLOW_MEMORY_GROWTH=1")
109109
add_ldflags("-sSTACK_SIZE=32MB", {force = true})

0 commit comments

Comments
 (0)