setWindowButtonVisibility() slow?

I am trying to create traffic lights with a custom inset in my Mac app, as it seems like there’s currently no good way of doing so in Electron

My solution was to make my window frameless to hide default traffic lights (and I also want to hide the titlebar), and then simply recreate the traffic lights in CSS and inject the Javascript.

In fullscreen mode, I still want to preserve the default traffic lights in the fullscreen titlebar (while hiding my custom ones), so I use win.setWindowButtonVisibility(true) when the fullscreen button is clicked. Upon exiting fullscreen, I want to once again hide the default traffic lights in the titlebar and show my custom inset ones, so I call win.setWindowButtonVisibility(false). However, there seems to be a delay when I do this, as I get a quick glimpse of the native traffic lights before they disappear:

My current logic:

win.on('leave-full-screen', () => {
    win.setWindowButtonVisibility(false);
    webContents.executeJavaScript(showTrafficLights());
});

Is this a result of setWindowButtonVisibility(false) being slow? Or am I approaching this incorrectly?

Solved. My workaround is to just directly listen to the NSWindow notification and set it earlier:

systemPreferences.subscribeLocalNotification(
    'NSWindowWillExitFullScreenNotification',
    () => {
        win.setWindowButtonVisibility(false);
        webContents.executeJavaScript(showTrafficLights());
    },
);