Bug 1708422: part 3) Add some documentation to `mozInlineSpellChecker::CheckWordsAndA...
[gecko.git] / toolkit / content / globalOverlay.js
blob2cc0804e7b70ce87d0b006e2b5d80893f91370a5
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2  * License, v. 2.0. If a copy of the MPL was not distributed with this
3  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 function closeWindow(aClose, aPromptFunction) {
6   let { AppConstants } = ChromeUtils.import(
7     "resource://gre/modules/AppConstants.jsm"
8   );
10   // Closing the last window doesn't quit the application on OS X.
11   if (AppConstants.platform != "macosx") {
12     var windowCount = 0;
13     for (let w of Services.wm.getEnumerator(null)) {
14       if (w.closed) {
15         continue;
16       }
17       if (++windowCount == 2) {
18         break;
19       }
20     }
22     // If we're down to the last window and someone tries to shut down, check to make sure we can!
23     if (windowCount == 1 && !canQuitApplication("lastwindow")) {
24       return false;
25     }
26     if (
27       windowCount != 1 &&
28       typeof aPromptFunction == "function" &&
29       !aPromptFunction()
30     ) {
31       return false;
32     }
33   } else if (typeof aPromptFunction == "function" && !aPromptFunction()) {
34     return false;
35   }
37   if (aClose) {
38     window.close();
39     return window.closed;
40   }
42   return true;
45 function canQuitApplication(aData) {
46   try {
47     var cancelQuit = Cc["@mozilla.org/supports-PRBool;1"].createInstance(
48       Ci.nsISupportsPRBool
49     );
50     Services.obs.notifyObservers(
51       cancelQuit,
52       "quit-application-requested",
53       aData || null
54     );
56     // Something aborted the quit process.
57     if (cancelQuit.data) {
58       return false;
59     }
60   } catch (ex) {}
61   return true;
64 function goQuitApplication() {
65   if (!canQuitApplication()) {
66     return false;
67   }
69   Services.startup.quit(Ci.nsIAppStartup.eAttemptQuit);
70   return true;
74 // Command Updater functions
76 function goUpdateCommand(aCommand) {
77   try {
78     var controller = top.document.commandDispatcher.getControllerForCommand(
79       aCommand
80     );
82     var enabled = false;
83     if (controller) {
84       enabled = controller.isCommandEnabled(aCommand);
85     }
87     goSetCommandEnabled(aCommand, enabled);
88   } catch (e) {
89     Cu.reportError(
90       "An error occurred updating the " + aCommand + " command: " + e
91     );
92   }
95 function goDoCommand(aCommand) {
96   try {
97     var controller = top.document.commandDispatcher.getControllerForCommand(
98       aCommand
99     );
100     if (controller && controller.isCommandEnabled(aCommand)) {
101       controller.doCommand(aCommand);
102     }
103   } catch (e) {
104     Cu.reportError(
105       "An error occurred executing the " + aCommand + " command: " + e
106     );
107   }
110 function goSetCommandEnabled(aID, aEnabled) {
111   var node = document.getElementById(aID);
113   if (node) {
114     if (aEnabled) {
115       node.removeAttribute("disabled");
116     } else {
117       node.setAttribute("disabled", "true");
118     }
119   }