sketch-module-web-view icon indicating copy to clipboard operation
sketch-module-web-view copied to clipboard

Prevent close doesn't work

Open nadavkaner opened this issue 6 years ago • 4 comments

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 avatar Nov 30 '19 10:11 nadavkaner

@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 avatar Dec 11 '19 08:12 scuster1-chwy

@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.

mathieudutour avatar Dec 11 '19 09:12 mathieudutour

This is the behavior that I need - to be able to hide the window when clicking the close button.. Thanks for the help

nadavkaner avatar Dec 11 '19 12:12 nadavkaner

@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();
});

arvinxx avatar May 06 '21 10:05 arvinxx