Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent close doesn't work #136

Open
nadavkaner opened this issue Nov 30, 2019 · 4 comments
Open

Prevent close doesn't work #136

nadavkaner opened this issue Nov 30, 2019 · 4 comments

Comments

@nadavkaner
Copy link

nadavkaner commented Nov 30, 2019

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.

@scuster1-chwy
Copy link

scuster1-chwy commented Dec 11, 2019

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

@mathieudutour
Copy link
Member

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

@nadavkaner
Copy link
Author

nadavkaner commented Dec 11, 2019

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

@arvinxx
Copy link

arvinxx commented May 6, 2021

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants