Prevent close doesn't work
I can't prevent from the window to close from the window red close button, I want to prevent the close and instead just hide the window. this is the code I'm trying:
browserWindow.on('close', (event) => {
event.preventDefault();
});
The event 'close' being fire but the window still being close.
@nadavkaner you can't do it this way because the native window is closing before this event actually fires. If you want to "hide" the window, I would recommend just disabling the close button and only allowing the user to minimize it (the plugin is not it's own application, and this is an anti-pattern). You can achieve this by simply passing an option: closable: false
const options = {
identifier: webviewIdentifier,,
closable: false
}
After doing this you should still provide a way for the user to terminate your plugin without terminating Sketch as a whole.
If this helped you solve your issue, please consider closing this ticket.
@scuster1-chwy has a good workaround, although the reason why event.preventDefault(); doesn't work is because of how CocoaScript (on which plugins are built) works: the delegate we create should be able to return a BOOL but CocoaScript can only work with id.
This is the behavior that I need - to be able to hide the window when clicking the close button.. Thanks for the help
@nadavkaner There is a way to hack the function
// get the close button
const closeButton = browserWindow._panel.standardWindowButton(NSWindowCloseButton);
// replace the implement
closeButton.setCOSJSTargetFunction(() => {
xxx.hide();
});