-
Notifications
You must be signed in to change notification settings - Fork 3.5k
[GLFW] Restore css scaling behavior (removed in 3.1.51) #22900
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 11 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
1067b21
restored css scaling behavior (removed in 3.1.51)
ypujante 6b5b856
fixed broken test
ypujante ae7511b
fixed space after if
ypujante 8a1b7ce
Merge branch 'main' into emscripten-22847
ypujante 98c8256
removed "incoming module API"
ypujante 0b2717d
fixed wrong constant name
ypujante b7210dc
added test for the CSS scaling feature
ypujante 1dbf09c
Merge remote-tracking branch 'yan-fork/main' into emscripten-22847
ypujante dd871a9
implemented proper behavior with HiDPI
ypujante 861fc4b
added documentation for HiDPI
ypujante c0c147f
Merge branch 'main' into emscripten-22847
ypujante a05693c
PR review updates
ypujante 06c5e82
Merge branch 'main' into emscripten-22847
ypujante 906f073
updated Changelog
ypujante File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,119 @@ | ||
| /* | ||
| * Copyright 2024 The Emscripten Authors. All rights reserved. | ||
| * Emscripten is available under two separate licenses, the MIT license and the | ||
| * University of Illinois/NCSA Open Source License. Both these licenses can be | ||
| * found in the LICENSE file. | ||
| */ | ||
|
|
||
| #include <GLFW/glfw3.h> | ||
| #include <assert.h> | ||
| #include <emscripten/html5.h> | ||
| #include <stdio.h> | ||
|
|
||
| /* | ||
| * History: | ||
| * - This test was added after the issue #22847 was reported (November 2024). | ||
| * - For historical reasons, the JavaScript GLFW implementation includes a feature: the ability to scale the canvas via CSS. | ||
| * - Note that this feature is not part of GLFW as GLFW does not offer any API to scale the window. | ||
| * - Being undocumented/untested, this feature was accidentally removed when HiDPI support was added (in 3.1.51 / December 2023). | ||
| * - This test was added to document this feature and ensure proper behavior. | ||
| * | ||
| * What does this feature do? | ||
| * - if there is a CSS rule that specifies the size of the canvas (ex: `#canvas { width: 100%;}`), then the canvas | ||
| * will be scaled to match this value. Note that from a GLFW point of view, the size of the window remains what | ||
| * gets specified in `glfwCreateWindow` and/or `glfwSetWindowSize` | ||
| * - only global CSS rules apply, as setting a CSS rule on the canvas itself is removed (ex: `<canvas style="width:100%;">` is ignored) | ||
| * | ||
| * In HiDPI mode, (`glfwWindowHint(GLFW_SCALE_TO_MONITOR, GLFW_TRUE)`), this feature cannot work and as a result is disabled (because the canvas | ||
| * is sized using `devicePixelRatio` as the factor and CSS is used to scale it at the desired size). | ||
| */ | ||
|
|
||
| /** | ||
| * As explained above, this function adds a global CSS rule instead of setting the CSS style directly on the canvas | ||
| * because it gets ignored otherwise. */ | ||
| EM_JS(void, addCSSScalingRule, (), { | ||
| const style = document.createElement('style'); | ||
| style.appendChild(document.createTextNode("#canvas { width: 700px; height: 500px; }")); | ||
| document.head.appendChild(style); | ||
| }) | ||
|
|
||
| /** | ||
| * Since it is unclear in which browser/resolution this test will run we compute the actual ratio used by the test. | ||
| * This has the neat effect that the test can be run manually on a HiDPI screen. | ||
| */ | ||
| EM_JS(double, getDevicePixelRatio, (), {return (typeof devicePixelRatio == 'number' && devicePixelRatio) || 1.0;}) | ||
|
|
||
| /** | ||
| * Checks window size and framebuffer size according to ratio */ | ||
| static void checkWindowSize(GLFWwindow *window, int expectedWidth, int expectedHeight, float ratio) { | ||
| // first check the window size | ||
| int w, h; | ||
| glfwGetWindowSize(window, &w, &h); | ||
| printf("windowSize => %d == %d && %d == %d\n", w, expectedWidth, h, expectedHeight); | ||
| assert(w == expectedWidth && h == expectedHeight); | ||
|
|
||
| // second check the frame buffer size | ||
| int fbw, fbh; | ||
| glfwGetFramebufferSize(window, &fbw, &fbh); | ||
| printf("framebufferSize => %d == %d && %d == %d\n", fbw, (int) (expectedWidth * ratio), fbh, (int) (expectedHeight * ratio)); | ||
| assert(fbw == (int) (expectedWidth * ratio) && fbh == (int) (expectedHeight * ratio)); | ||
| } | ||
|
|
||
| int main() { | ||
|
|
||
| assert(glfwInit() == GLFW_TRUE); | ||
|
|
||
| // Use Case #1 | ||
| // Create a window without HiDPI support and without CSS rule => expected sizes to match | ||
| { | ||
| printf("Use case #1\n"); | ||
| GLFWwindow* window = glfwCreateWindow(640, 480, "test_glfw3_css_scaling.c", NULL, NULL); | ||
| assert(window); | ||
| checkWindowSize(window, 640, 480, 1.0); | ||
| double w, h; | ||
| emscripten_get_element_css_size("#canvas", &w, &h); | ||
| printf("CSS Size=%.0fx%.0f\n", w, h); | ||
| assert(w == 640 && h == 480); | ||
| glfwDestroyWindow(window); | ||
| } | ||
ypujante marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| // Use Case #2 | ||
| // Create a window without HiDPI support, and with CSS rule => | ||
| // the window size should match the creation size, but the CSS size should match the rule. | ||
| { | ||
| printf("Use case #2\n"); | ||
| addCSSScalingRule(); | ||
| GLFWwindow* window = glfwCreateWindow(640, 480, "test_glfw3_css_scaling.c", NULL, NULL); | ||
| assert(window); | ||
| checkWindowSize(window, 640, 480, 1.0); | ||
| double w, h; | ||
| emscripten_get_element_css_size("#canvas", &w, &h); | ||
| printf("CSS Size=%.0fx%.0f\n", w, h); | ||
| assert(w == 700 && h == 500); // Rule is "#canvas { width: 700px; height: 500px; }" | ||
| glfwDestroyWindow(window); | ||
| } | ||
|
|
||
| // Use Case #3 | ||
| // Create a window with HiDPI support, and with CSS rule => | ||
| // the window size and framebuffer size should match the creation size (CSS rule is ignored) | ||
| { | ||
| printf("Use case #3\n"); | ||
| glfwWindowHint(GLFW_SCALE_TO_MONITOR, GLFW_TRUE); | ||
| GLFWwindow* window = glfwCreateWindow(640, 480, "test_glfw3_css_scaling.c", NULL, NULL); | ||
| assert(window); | ||
| double dpr = getDevicePixelRatio(); | ||
| printf("devicePixelRatio=%.0f\n", dpr); | ||
| checkWindowSize(window, 640, 480, dpr); | ||
| double w, h; | ||
| emscripten_get_element_css_size("#canvas", &w, &h); | ||
| printf("CSS Size=%.0fx%.0f\n", w, h); | ||
| assert(w == 640 && h == 480); | ||
| glfwDestroyWindow(window); | ||
| } | ||
|
|
||
| printf("All tests complete\n"); | ||
|
|
||
| glfwTerminate(); | ||
|
|
||
| return 0; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.