Kode / Kha

Ultra-portable, high performance, open source multimedia framework.
http://kha.tech
zlib License
1.49k stars 172 forks source link

Incorrect reported window size using Electron / Debug HTML5 #1348

Open doczi-dominik opened 3 years ago

doczi-dominik commented 3 years ago

Describe the bug kha.window.notifyOnResize provides incorrect width and height values when using Debug HTML5 / Electron

To Reproduce Steps to reproduce the behavior:

  1. Add a simple callback to kha.window.notifyOnResize that prints both width and height values.
  2. Run project using native and make note of the reported values.
  3. Run project using debug HTML5 and make note of the reported values.
  4. The two values do not match.

Expected behavior The output of Debug HTML5 should match that of the native application.

Screenshots image image

Execution Environment:

For native:

ScaleManager.hx:8: New window: 766 x 1059

Additional context Maybe it's a Linux specific issue?

doczi-dominik commented 3 years ago

I managed to narrow down the issue to DPI scaling.

On my setup, I change the Xft.dpi parameter for the X11 Server from the default 96 to a non-standard 80. Temporarily resetting it to 96 makes Electron display the correct window size and Kha reports it correctly, too.

I am 99% sure this is an Electron issue, however, if it can be universally fixed by e.g.: including the screen module and checking the scale factor in electron.js, I think it would benefit Kha / Khamake.

RobDangerous commented 3 years ago

Kha should always report the size in pixels, independent of dpi-settings. We're probably still missing something to make that work properly in html5, will have a look.

doczi-dominik commented 3 years ago

The plot thickens...

When launching from the terminal, the resolution is reported correctly. However, when launching from the VSCode build task, the resolution is incorrect! I have checked if it's related to VSCode zoom settings, and it is not. I will try to check what enviromental variables VSCode passes, maybe the issue lies there.

I think this issue is safe to close now as it probably has no relation to Kha, only to Electron/Linux/VSCode.

RobDangerous commented 3 years ago

Still want to have a look :) And GitHub should really add support for issue-priorities.

doczi-dominik commented 3 years ago

I found out that the issue only occurs when the debugger type is pwa-chrome. Using node, chrome or even pwa-node resolves the issue. However, debugging is made more difficult due to having to set the output capture source to standard output, which also captures Chromium logs.

doczi-dominik commented 3 years ago

I can confirm that the issue lied with Electron's automatic HiDPI scaling. Adding the command line flag --force-device-scale-factor=1 makes kha.System.windowWidth() / kha.System.windowHeight() behave normally, and it does not change the appearance of the display at all.

Relevant question: I want to add this command line switch permanently via electron.js. Is there a way to override electron.js like with index.html via the Assets directory or do I need to mess with khamake?

If it's the latter, I will probably create a PR that allows setting it to 1 via a khafile.js parameter like project.targetOptions.html5.electronDisableScaling.

RblSb commented 2 years ago

Currently "--force-device-scale-factor=1" makes electron window blurry on any device with dpi > 1.

image

If you try to remove that flag from launch.json, kha window resize logic stops working. Idk why resizing actually works in electron currently, there is no code like canvas.width = window.width * devicePixelRatio