Repository navigation
Description
Continuation of #146683
Current status of investigation
The upstream Electron issue is at electron/electron#30760.
The issue was introduced in Electron v14.0.0-beta.5 and the commit that introduced the issue is electron/electron@172ac25.
The PR electron/electron#35189 fixes the issue where frameless resizable windows show Windows 7 frames during startup, but there's a larger issue that is still unresolved even with that PR. I also searched through the thousands of Chromium commits associated with that Electron commit to see whether they had anything to do with the issue, but I didn't find anything relevant even after reverting several suspicious commits.Minimal repro
const { app, BrowserWindow } = require('electron') function createWindow() { new BrowserWindow({ width: 800, height: 600, frame: false }) } app.whenReady().then(() => { createWindow() }) app.on('window-all-closed', function () { app.quit() })Running
<path-to-electron-exe> <path-to-js-file-above>with Electron v14.0.0-beta.5 or newer on Windows 10 reproduces the issue.Going through the Chromium code with the PR fix for frameless resizable windows
The PR electron/electron#35189 fixes the issue for frameless resizable windows by calling
set_can_resize()at an earlier stage of program initialization. Assume the window is frameless and resizable. Then, the program setscan_resizetotrueearlier in widget_delegate.cc, and the following occurs:
- widget_hwnd_utils.cc:68 is not run, meaning
WS_THICKFRAMEis not removed from the style set in Line 62. This style parameter is passed in to aCreateWindowExcall at window_impl.cc:213.- hwnd_message_handler.cc:1029 is not run. Instead, the
ifblock above is run, which adds aWS_THICKFRAMEstyle to the style passed into theSetWindowLongcall.- Somehow, because of these
WS_THICKFRAMEstyles being passed in, after the window is shown, we get a singleWM_DWMNCRENDERINGCHANGEDmessage, and noWM_NCPAINTmessages after. NC in this case refers to non-client area, which is the frame. DWM stands for desktop windows manager.After adding a long
setTimeoutof 30 seconds to thecreateWindowcall above, I was able to use Spy++ to log messages to that window and see the following:<000157> 0000000000170B9C R WM_WINDOWPOSCHANGING <000158> 0000000000170B9C S WM_GETICON nType:ICON_BIG <000159> 0000000000170B9C R WM_GETICON hicon:00000000 <000160> 0000000000170B9C P WM_DWMNCRENDERINGCHANGED fRenderingEnabled:True <000161> 0000000000170B9C S WM_GETICON nType:ICON_SMALL2 <000162> 0000000000170B9C R WM_GETICON hicon:00000000 <000163> 0000000000170B9C P message:0x0400 [User-defined:WM_USER+0] wParam:00000003 lParam:00000000 <000164> 0000000000170B9C S WM_GETICON nType:ICON_SMALL <000165> 0000000000170B9C R WM_GETICON hicon:00000000 <000166> 0000000000170B9C P message:0xC112 [Registered:"TaskbarButtonCreated"] wParam:00000000 lParam:00000000 <000167> 0000000000170B9C S WM_GETMINMAXINFO lpmmi:0000002354FFE8F0 <000168> 0000000000170B9C R WM_GETMINMAXINFO lpmmi:0000002354FFE8F0 <000169> 0000000000170B9C P WM_PAINT hdc:00000000I have also attached full logs to the end of this comment.
The PR doesn't fix the case of frameless non-resizable windows
For frameless windows that cannot resize, even if we call
set_can_resize()earlier, because the program would pass infalse, for theCreateWindowExandSetWindowLongcalls above, theWS_THICKFRAMEstyle would not be in the styles sent to the calls. As a result, what I see in Spy++ isWM_DWMNCRENDERINGCHANGEDbeing toggled three times, and severalWM_WINDOWPOSCHANGINGcalls that result inWM_NCPAINTmessages being emitted. Using Spy++, I confirmed that theWM_WINDOWPOSCHANGINGcalls have the flagSWP_FRAMECHANGEDset.<000174> 0000000000130AAA P WM_DWMNCRENDERINGCHANGED fRenderingEnabled:True <000175> 0000000000130AAA S WM_GETICON nType:ICON_SMALL2 <000176> 0000000000130AAA R WM_GETICON hicon:00000000 <000177> 0000000000130AAA P WM_DWMNCRENDERINGCHANGED fRenderingEnabled:False <000178> 0000000000130AAA S WM_WINDOWPOSCHANGING lpwp:000000E709BFE280 <000179> 0000000000130AAA R WM_WINDOWPOSCHANGING <000180> 0000000000130AAA S WM_NCCALCSIZE fCalcValidRects:True lpncsp:000000E709BFE250 <000181> 0000000000130AAA R WM_NCCALCSIZE fuValidRect:0000 lpncsp:000000E709BFE250 <000182> 0000000000130AAA S WM_NCPAINT hrgn:00000001 <000183> 0000000000130AAA R WM_NCPAINT <000184> 0000000000130AAA S WM_ERASEBKGND hdc:5B01182B <000185> 0000000000130AAA R WM_ERASEBKGND fErased:True <000186> 0000000000130AAA S WM_WINDOWPOSCHANGED lpwp:000000E709BFE280 <000187> 0000000000130AAA S WM_WINDOWPOSCHANGING lpwp:000000E709BFDB20 <000188> 0000000000130AAA R WM_WINDOWPOSCHANGING <000189> 0000000000130AAA S WM_NCCALCSIZE fCalcValidRects:True lpncsp:000000E709BFDAF0 <000190> 0000000000130AAA R WM_NCCALCSIZE fuValidRect:0000 lpncsp:000000E709BFDAF0 <000191> 0000000000130AAA S WM_NCPAINT hrgn:00000001 <000192> 0000000000130AAA R WM_NCPAINT <000193> 0000000000130AAA S WM_ERASEBKGND hdc:FFFFFFFF87011810 <000194> 0000000000130AAA R WM_ERASEBKGND fErased:True <000195> 0000000000130AAA S WM_WINDOWPOSCHANGED lpwp:000000E709BFDB20 <000196> 0000000000130AAA R WM_WINDOWPOSCHANGED <000197> 0000000000130AAA R WM_WINDOWPOSCHANGED <000198> 0000000000130AAA S WM_GETICON nType:ICON_SMALL <000199> 0000000000130AAA R WM_GETICON hicon:00000000 <000200> 0000000000130AAA P message:0x0400 [User-defined:WM_USER+0] wParam:00000003 lParam:00000000 <000201> 0000000000130AAA P WM_DWMNCRENDERINGCHANGED fRenderingEnabled:True <000202> 0000000000130AAA S WM_WINDOWPOSCHANGING lpwp:000000E709BFE280 <000203> 0000000000130AAA R WM_WINDOWPOSCHANGING <000204> 0000000000130AAA S WM_NCCALCSIZE fCalcValidRects:True lpncsp:000000E709BFE250 <000205> 0000000000130AAA R WM_NCCALCSIZE fuValidRect:0000 lpncsp:000000E709BFE250 <000206> 0000000000130AAA S WM_NCPAINT hrgn:00000001 <000207> 0000000000130AAA R WM_NCPAINT <000208> 0000000000130AAA S WM_ERASEBKGND hdc:010118D7 <000209> 0000000000130AAA R WM_ERASEBKGND fErased:True <000210> 0000000000130AAA S WM_WINDOWPOSCHANGED lpwp:000000E709BFE280 <000211> 0000000000130AAA S WM_WINDOWPOSCHANGING lpwp:000000E709BFDB20 <000212> 0000000000130AAA R WM_WINDOWPOSCHANGING <000213> 0000000000130AAA S WM_NCCALCSIZE fCalcValidRects:True lpncsp:000000E709BFDAF0 <000214> 0000000000130AAA R WM_NCCALCSIZE fuValidRect:0000 lpncsp:000000E709BFDAF0 <000215> 0000000000130AAA S WM_NCPAINT hrgn:00000001 <000216> 0000000000130AAA R WM_NCPAINT <000217> 0000000000130AAA S WM_ERASEBKGND hdc:5B01182B <000218> 0000000000130AAA R WM_ERASEBKGND fErased:True <000219> 0000000000130AAA S WM_WINDOWPOSCHANGED lpwp:000000E709BFDB20 <000220> 0000000000130AAA R WM_WINDOWPOSCHANGED <000221> 0000000000130AAA R WM_WINDOWPOSCHANGEDMore information
I have searched the Chromium and Electron codebases for
WS_THICKFRAMEandSWP_FRAMECHANGED(as well asSWP_DRAWFRAME) to see why the frame might be rendering more often whenWS_THICKFRAMEis not passed in. However, I haven't found anything within the codebase itself.Searching Windows documentation, I found the following:
WS_THICKFRAMEcausesWM_GETMINMAXINFOto be sent. Reference: https://docs.microsoft.com/en-us/windows/win32/winmsg/wm-windowposchanging. However, I'm not sure whether this could explain the difference in behaviour between resizable and non-resizable windows.SetWindowPoscan pass inSWP_FRAMECHANGEDorSWP_DRAWFRAMEto sendNC_CALCSIZEto the window, which is why I searched for those two constants within the codebase. Reference: https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-setwindowpos. Also, Chromium handlesNC_CALCSIZEby returningWVR_REDRAWat hwnd_message_handler.cc:2353, which I assume causes theWM_NCPAINTmessage to occur.Also, Electron calls
SetWindowLongat native_window_views.cc:350. I noticed that if I don't callSetWindowLongthere, the Windows 7 frame doesn't show up, but then the Electron window doesn't render with the proper style (see electron/electron#32981). I also noticed that when the Windows 7 frame shows up, it has the style of whatever was passed into that specificSetWindowLongcall. For example, if I addWS_SYSMENUto the call, then for a second I see a Windows 7 frame withWS_SYSMENUapplied.I also tried creating a minimal project in VS to reproduce the issue but wasn't successful. The frame that rendered seemed to have a Windows 10 style rather than a Windows 7 one.
I also tried registering a listener in
hwnd_message_handler.ccfor theWM_DWMNCRENDERINGCHANGEDmessages, and it did seem to get hit, though I couldn't do anything with it. There's also the mysterious ScopedRedrawLock, but I wasn't able to apply it to resolve the issue.Attached log files
A text file showing the good case where WM_DWMNCRENDERINGCHANGED is only called once. The window is frameless and resizable.
A text file showing the bad case where WM_DWMNCRENDERINGCHANGED is called three times. The window is frameless and not resizable.
Activity
deepak1556 commentedon Aug 13, 2022
rzhao271 commentedon Aug 15, 2022
In Visual Studio, I got an older-looking frame to show up by calling
SetWindowLongwith certain styles before callingShowWindow.The older-looking frame kept showing up until I added
WS_CAPTION. Even addingWS_BORDERdidn't help.I also noticed that
0x60a0000included theWS_SYSMENUstyle. Based on https://docs.microsoft.com/en-us/windows/win32/winmsg/window-styles, when theWS_SYSMENUstyle is specified, theWS_CAPTIONstyle must also be specified. However, addingWS_CAPTIONto the style didn't fix the issue in Electron.rzhao271 commentedon Aug 17, 2022
Robo (@deepak1556) thought it was strange that the styles kept changing, whereas I just took the styles changing for granted other than the
0x06e0000style going to0x06a0000(which is whereWS_THICKFRAMEis removed from the styling due to resizable being false).I annotated an example of the styles changing below:
It turns out the
WS_CAPTIONstyle is removed from the Window in HWNDMessageHandler::OnCreate, just for Electron to add it back later. Even though the style is applied early, it turns out that commenting out that if statement seems to fix the issue for both resizable and non-resizable frameless windows, even without settingcan_resizetotrueearlier.That if statement arises due to https://codereview.chromium.org/9372053/, with a comment that states that "[The if statement] removes the unsightly close button visible in the frame during startup and resizing". I still have to check whether commenting out that code causes that glitch to show up again, considering Electron has its own style that it applies to the window anyway. For non-resizable windows, I haven't seen any regression. I still have to test the change for resizable windows, though. The code review also links to images, but those links are dead, so I'm unable to see what the glitchy behaviour looked like.
Edit: we hit that if statement in the first place due to
remove_standard_framebeing set. Some code pointers:Setting
remove_standard_framein Electron https://github.com/electron/electron/blob/1c0f1180995df357e199dcb99e6cb434f6daa32e/shell/browser/native_window_views.cc#L248Initializing
remove_standard_frame_in Chromium https://source.chromium.org/chromium/chromium/src/+/main:ui/views/widget/desktop_aura/desktop_window_tree_host_win.cc;l=187Referencing
remove_standard_frame_https://source.chromium.org/chromium/chromium/src/+/main:ui/views/widget/desktop_aura/desktop_window_tree_host_win.cc;l=763rzhao271 commentedon Aug 18, 2022
Upstream: electron/electron#30024
rzhao271 commentedon Aug 31, 2022
TODO:
Verification steps if needed:
Note that #146683 already gets rid of the Windows 7 frame showing up during launch, but this issue tracks an alternative solution for Electron that is simpler and applicable to a larger class of frameless windows.
deepak1556 commentedon Dec 13, 2022
This is now addressed.