Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion data/warn.html → data/warnclosewindow.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
</head>

<body>
Press Control-Q or Control-Shift-W again to quit.
Press Control-Shift-W again to close the window.
</body>

</html>
2 changes: 1 addition & 1 deletion data/warnmac.html → data/warnclosewindowmac.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
</head>

<body>
Press &#8984;-Q or &#8984;-Shift-W again to quit.
Press &#8984;-Shift-W again to close the window.
</body>

</html>
20 changes: 20 additions & 0 deletions data/warnquit.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<!DOCTYPE html>
<html>

<head>
<meta charset="utf-8">
<style type="text/css" media="all">
body {
margin: 10px;
font-family: sans-serif;
padding: 10px;
text-align: center;
}
</style>
</head>

<body>
Press Control-Q again to quit.
</body>

</html>
20 changes: 20 additions & 0 deletions data/warnquitmac.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<!DOCTYPE html>
<html>

<head>
<meta charset="utf-8">
<style type="text/css" media="all">
body {
margin: 10px;
font-family: sans-serif;
padding: 10px;
text-align: center;
}
</style>
</head>

<body>
Press &#8984;-Q again to quit.
</body>

</html>
99 changes: 62 additions & 37 deletions lib/main.js
Original file line number Diff line number Diff line change
@@ -1,57 +1,82 @@
// Define keyboard shortcuts for showing and hiding a custom panel.
var { Cc, Ci } = require("chrome");
var { Hotkey } = require("hotkeys");
var panel = require("panel");
var timers = require("timers");
var data = require("self").data;
var runtime = require("runtime");
var { Ci, Cu } = require("chrome");
var { Hotkey } = require("sdk/hotkeys");
var panel = require("sdk/panel");
var timers = require("sdk/timers");
var data = require("sdk/self").data;
var runtime = require("sdk/system/runtime");
var osString = runtime.OS;
var osWarnFile;
var osWarnQuitFile;
var osWarnCloseWindowFile;
var lastActiveWindow;

Cu.import("resource://gre/modules/Services.jsm");

// Load the right warning
if (osString === "Darwin") {
osWarnFile = "warnmac.html";
} else {
osWarnFile = "warn.html";
osWarnQuitFile = "warnquitmac.html";
osWarnCloseWindowFile = "warnclosewindowmac.html";
} else {
osQuitWarnFile = "warnquit.html";
osCloseWindowFile = "warnclosewindow.html";
}

// Create the panel to show the warning
var myPanel = panel.Panel({
width: 300,
height: 80,
contentURL: data.url(osWarnFile),
var warnCloseWindowPanel = panel.Panel({
width: 300,
height: 80,
contentURL: data.url(osWarnCloseWindowFile)
});

var warnQuitPanel = panel.Panel({
width: 300,
height: 80,
contentURL: data.url(osWarnQuitFile)
});

// Display the warning
function showPanel() {
myPanel.show();
function showWarnPanel(panel) {
panel.show();
timers.setTimeout(function() {
myPanel.hide();
panel.hide();
}, 2000);
}

var pressButton = function() {
// We're already showing the warning
if (myPanel.isShowing) {
myPanel.hide();
Cc['@mozilla.org/toolkit/app-startup;1']
.getService(Ci.nsIAppStartup)
.quit(Ci.nsIAppStartup.eAttemptQuit)
function attemptQuit() {
warnCloseWindowPanel.hide();
if (warnQuitPanel.isShowing) {
// We're already showing the warning
warnQuitPanel.hide();
Services.startup.quit(Ci.nsIAppStartup.eAttemptQuit);
} else {
// Show the warning since we didn't quit yet
showWarnPanel(warnQuitPanel);
}
}

function attemptCloseWindow() {
warnQuitPanel.hide();
var activeWindow = Services.wm.getMostRecentWindow(null);
var warningDisplayed = (
warnCloseWindowPanel.isShowing &&
activeWindow === lastActiveWindow);
if (warningDisplayed) {
warnCloseWindowPanel.hide();
activeWindow.close();
} else {
showWarnPanel(warnCloseWindowPanel);
lastActiveWindow = activeWindow;
}
// Show the warning since we didn't quit yet
showPanel();
};
}

// Handle the keypress
var AccelQ = Hotkey({
combo: "accel-q",
onPress: function() {
pressButton();
}
combo: "accel-q",
onPress: function() {
attemptQuit();
}
});
var AccelShiftW = Hotkey({
combo: "accel-shift-w",
onPress: function() {
pressButton();
}
combo: "accel-shift-w",
onPress: function() {
attemptCloseWindow();
}
});
6 changes: 3 additions & 3 deletions test/test-main.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ exports.test_test_run = function(test) {
};

exports.test_id = function(test) {
test.assert(require("self").id.length > 0);
test.assert(require("sdk/self").id.length > 0);
};

exports.test_url = function(test) {
require("request").Request({
require("sdk/request").Request({
url: "http://www.mozilla.org/",
onComplete: function(response) {
test.assertEqual(response.statusText, "OK");
Expand All @@ -20,7 +20,7 @@ exports.test_url = function(test) {
};

exports.test_open_tab = function(test) {
const tabs = require("tabs");
const tabs = require("sdk/tabs");
tabs.open({
url: "http://www.mozilla.org/",
onReady: function(tab) {
Expand Down