# Cancel window closing

**URL:** <https://forum.juce.com/t/cancel-window-closing/44759>\
**Category:** General JUCE discussion\
**Created:** [March 2, 2021, 10:21pm UTC](https://forum.juce.com/t/cancel-window-closing/44759 "2021-03-02T22:21:06Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![JojoTheMAN21](https://avatars.discourse-cdn.com/v4/letter/j/aca169/32.png) [@JojoTheMAN21](https://forum.juce.com/u/JojoTheMAN21)\
**Post date:** [March 2, 2021, 10:21pm UTC](https://forum.juce.com/t/cancel-window-closing/44759/1 "2021-03-02T22:21:06Z")

</div>

Is there a way to cancel or ignore a user’s close request? I’d like to delay a window closing if the user’s project is still saving.

I know about `NewWindow::closeButtonPressed` and `Component::userTriedToCloseWindow`, but neither of these functions allows for a way to completely cancel or ignore a close request. I can’t create a while loop in these functions either, because I don’t want the UI to freeze up.

For an example, C# WPF has a way to `Cancel` a window closing: [Window.Closing Event (System.Windows) | Microsoft Docs](https://docs.microsoft.com/en-us/dotnet/api/system.windows.window.closing?view=net-5.0#remarks)

---

<div class="post-metadata">

**Author:** ![daniel](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/daniel/32/790_2.png) [@daniel](https://forum.juce.com/u/daniel)\
**Post date:** [March 2, 2021, 10:37pm UTC](https://forum.juce.com/t/cancel-window-closing/44759/2 "2021-03-02T22:37:08Z")

</div>

It is handled in the JUCEApplication. There is a method [systemRequestedQuit()](https://docs.juce.com/develop/classJUCEApplication.html#a8e9b08200268216a791326635ec9321c)

> Called when the operating system is trying to close the application.
> 
> The default implementation of this method is to call [quit()](https://docs.juce.com/develop/classJUCEApplicationBase.html#a542e212605f0aa693f687d8798998615), but it may be overloaded to ignore the request or do some other special behaviour instead. For example, you might want to offer the user the chance to save their changes before quitting, and give them the chance to cancel.
> 
> […]

---

<div class="post-metadata">

**Author:** ![xenakios](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/xenakios/32/4047_2.png) [@xenakios](https://forum.juce.com/u/xenakios)\
**Post date:** [March 2, 2021, 10:40pm UTC](https://forum.juce.com/t/cancel-window-closing/44759/3 "2021-03-02T22:40:48Z")

</div>

Not going to have any use for plugin projects, though…? The original poster didn’t clarify if this is for a standalone application or for a plugin. If it’s a plugin, there’s no way ignore the user’s request to close the plugin window since that is owned by the host.

---

<div class="post-metadata">

**Author:** ![JojoTheMAN21](https://avatars.discourse-cdn.com/v4/letter/j/aca169/32.png) [@JojoTheMAN21](https://forum.juce.com/u/JojoTheMAN21)\
**Post date:** [March 2, 2021, 10:42pm UTC](https://forum.juce.com/t/cancel-window-closing/44759/4 "2021-03-02T22:42:25Z")

</div>

This is for a standalone application, so @daniel’s solution works great. Thanks guys!

---

<div class="post-metadata">

**Author:** ![JojoTheMAN21](https://avatars.discourse-cdn.com/v4/letter/j/aca169/32.png) [@JojoTheMAN21](https://forum.juce.com/u/JojoTheMAN21)\
**Post date:** [March 3, 2021, 2:50am UTC](https://forum.juce.com/t/cancel-window-closing/44759/5 "2021-03-03T02:50:07Z")

</div>

Just realized that `JUCEApplication::systemRequestedQuit()` might not work for my purposes. The window that I’m trying to cancel closing is the second window opened; meaning when the user clicks close on this window, the application itself isn’t quitting because there’s a window still open. So that function won’t be called.

Is there another method that can do what I’m trying to do? Or is it not possible as @xenakios suggested?

---

<div class="post-metadata">

**Author:** ![daniel](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/daniel/32/790_2.png) [@daniel](https://forum.juce.com/u/daniel)\
**Post date:** [March 3, 2021, 7:46am UTC](https://forum.juce.com/t/cancel-window-closing/44759/6 "2021-03-03T07:46:53Z")

</div>

If your window is a [juce::DocumentWindow](https://docs.juce.com/develop/classDocumentWindow.html#details) then you get a callback as well called [closeButtonPressed()](https://docs.juce.com/develop/classDocumentWindow.html#a71c46283a6fe63f2cad49d01997e16d5). This makes sense, because only DocumentWindows have the close/maximize/minimize buttons.

In this doc there is another valuable hint:

> (Note that the [DocumentWindow](https://docs.juce.com/develop/classDocumentWindow.html) class overrides [Component::userTriedToCloseWindow()](https://docs.juce.com/develop/classComponent.html#a543e70d5a3ff75cc3facb476f4cdb131) and redirects it to call this method, so any methods of closing the window that are caught by [userTriedToCloseWindow()](https://docs.juce.com/develop/classComponent.html#a543e70d5a3ff75cc3facb476f4cdb131) will also end up here).

that means, you could also look into that [userTriedToCloseWindow()](https://docs.juce.com/develop/classComponent.html#a543e70d5a3ff75cc3facb476f4cdb131) method, e.g. if you don’t use a DocumentWindow.

Hope that helps

---

<div class="post-metadata">

**Author:** ![JojoTheMAN21](https://avatars.discourse-cdn.com/v4/letter/j/aca169/32.png) [@JojoTheMAN21](https://forum.juce.com/u/JojoTheMAN21)\
**Post date:** [March 3, 2021, 11:05pm UTC](https://forum.juce.com/t/cancel-window-closing/44759/7 "2021-03-03T23:05:35Z")

</div>

Thanks, but as I said in my original post, `closeButtonPressed()` doesn’t have a way to cancel the closing of the `DocumentWindow`. The only way to delay closing the window is to essentially halt the UI thread while the saving task finishes, which I don’t want to do in this case because I want to show a loading screen or something.

I think I just need to go about this another way and make project saving a application-wide process, that way when `systemRequestedQuit()` is called I can “cancel” the application closing until the project is finished saving.

---

<div class="post-metadata">

**Author:** ![daniel](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/daniel/32/790_2.png) [@daniel](https://forum.juce.com/u/daniel)\
**Post date:** [March 3, 2021, 11:11pm UTC](https://forum.juce.com/t/cancel-window-closing/44759/8 "2021-03-03T23:11:48Z")

</div>

Sounds to me that the architecture could be improved by having the state separated from the window, so the state stays alive regardless if the window is open or not.

The problem you face is, if the same thread handles the saving and the closing, waiting doesn’t buy you time.

---

<div class="post-metadata">

**Author:** ![connorreviere](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/connorreviere/32/4890_2.png) [@connorreviere](https://forum.juce.com/u/connorreviere)\
**Post date:** [March 4, 2021, 2:15am UTC](https://forum.juce.com/t/cancel-window-closing/44759/9 "2021-03-04T02:15:13Z")

</div>

Regardless of any architectural concerns, I’m confused why `closeButtonPressed()` doesn’t do what you need. If you do nothing in your `closeButtonPressed()` method, the window shouldn’t close, so can’t you just return early if a save is in-progress to “cancel”? What does your implementation of that method look like?

For example, this class creates a window that does not close when the close button is pressed:

```
struct NonClosableWindow : juce::DocumentWindow
{
    NonClosableWindow()
        : juce::DocumentWindow ("NonClosableWindow", juce::Colours::grey,
                                juce::DocumentWindow::TitleBarButtons::allButtons)
    {
        setUsingNativeTitleBar (true);
        centreWithSize (800, 500);
        setVisible (true);
    }

    void closeButtonPressed() override
    {
    }
};
```

---

<div class="post-metadata">

**Author:** ![JojoTheMAN21](https://avatars.discourse-cdn.com/v4/letter/j/aca169/32.png) [@JojoTheMAN21](https://forum.juce.com/u/JojoTheMAN21)\
**Post date:** [March 4, 2021, 2:42am UTC](https://forum.juce.com/t/cancel-window-closing/44759/10 "2021-03-04T02:42:30Z")

</div>

Oh my gosh. 🤦‍♂️ That totally works! I had a class that overrides that but I thought it wasn’t able to cancel the close. I guess I’m crazy. 🙄

Thank you!!!
