Skip to content
This repository has been archived by the owner on Feb 26, 2022. It is now read-only.

Commit

Permalink
Merge pull request #1612 from erikvold/962286
Browse files Browse the repository at this point in the history
Bug 962286 making sdk/window/utils isFocused export a dispatcher which now works for high level windows r=@ZER0
  • Loading branch information
erikvold committed Oct 2, 2014
2 parents 974d800 + 14a5932 commit 95a2f25
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 12 deletions.
5 changes: 3 additions & 2 deletions lib/sdk/window/browser.js
Expand Up @@ -11,7 +11,7 @@ const { getWindowTitle } = require('./utils');
const unload = require('../system/unload');
const { EventTarget } = require('../event/target');
const { isPrivate } = require('../private-browsing/utils');
const { isWindowPrivate } = require('../window/utils');
const { isWindowPrivate, isFocused } = require('../window/utils');
const { viewFor } = require('../view/core');

const ERR_FENNEC_MSG = 'This method is not yet supported by Fennec, consider using require("sdk/tabs") instead';
Expand Down Expand Up @@ -43,5 +43,6 @@ exports.BrowserWindow = BrowserWindow;

const getWindowView = window => windowNS(window).window;

isPrivate.define(BrowserWindow, window => isWindowPrivate(windowNS(this).window));
viewFor.define(BrowserWindow, getWindowView);
isPrivate.define(BrowserWindow, (window) => isWindowPrivate(viewFor(window).window));
isFocused.define(BrowserWindow, (window) => isFocused(viewFor(window).window));
6 changes: 4 additions & 2 deletions lib/sdk/window/utils.js
Expand Up @@ -10,6 +10,7 @@ module.metadata = {
const { Cc, Ci } = require('chrome');
const array = require('../util/array');
const { defer } = require('sdk/core/promise');
const { dispatcher } = require("../util/dispatcher");

const windowWatcher = Cc['@mozilla.org/embedcomp/window-watcher;1'].
getService(Ci.nsIWindowWatcher);
Expand Down Expand Up @@ -216,7 +217,8 @@ function onFocus(window) {
}
exports.onFocus = onFocus;

function isFocused(window) {
let isFocused = dispatcher("window-isFocused");
isFocused.when(x => x instanceof Ci.nsIDOMWindow, (window) => {
const FM = Cc["@mozilla.org/focus-manager;1"].
getService(Ci.nsIFocusManager);

Expand All @@ -231,7 +233,7 @@ function isFocused(window) {
}

return (focusedChildWindow === childTargetWindow);
}
});
exports.isFocused = isFocused;

/**
Expand Down
12 changes: 5 additions & 7 deletions lib/sdk/windows/firefox.js
Expand Up @@ -10,7 +10,7 @@ const { Cc, Ci, Cr } = require('chrome'),
{ WindowTabs, WindowTabTracker } = require('./tabs-firefox'),
{ WindowDom } = require('./dom'),
{ WindowLoader } = require('./loader'),
{ isBrowser, getWindowDocShell,
{ isBrowser, getWindowDocShell, isFocused,
windows: windowIterator, isWindowPrivate } = require('../window/utils'),
{ Options } = require('../tabs/common'),
apiUtils = require('../deprecated/api-utils'),
Expand Down Expand Up @@ -79,9 +79,7 @@ const BrowserWindowTrait = Trait.compose(
this._load();

windowNS(this._public).window = this._window;

isPrivate.implement(this._public, window => isWindowPrivate(getChromeWindow(window)));
viewFor.implement(this._public, getChromeWindow);
viewFor.implement(this._public, (w) => windowNS(w).window);

return this;
},
Expand Down Expand Up @@ -265,8 +263,8 @@ const browserWindows = Trait.resolve({ toString: null }).compose(
}).resolve({ toString: null })
)();

function getChromeWindow(window) {
return windowNS(window).window;
}
const isBrowserWindow = (x) => x instanceof BrowserWindow;
isPrivate.when(isBrowserWindow, (w) => isWindowPrivate(viewFor(w)));
isFocused.when(isBrowserWindow, (w) => isFocused(viewFor(w)));

exports.browserWindows = browserWindows;
4 changes: 4 additions & 0 deletions test/test-windows-common.js
Expand Up @@ -5,6 +5,7 @@

const { Loader } = require('sdk/test/loader');
const { browserWindows } = require('sdk/windows');
const { isFocused } = require('sdk/window/utils');
const { viewFor } = require('sdk/view/core');
const { modelFor } = require('sdk/model/core');
const { Ci } = require("chrome");
Expand All @@ -31,6 +32,9 @@ exports.testBrowserWindowsIterator = function(assert) {
}
};

exports.testActiveWindowIsFocused = function(assert) {
assert.ok(isFocused(browserWindows.activeWindow), 'the active window is focused');
}

exports.testWindowTabsObject_alt = function(assert, done) {
let window = browserWindows.activeWindow;
Expand Down
18 changes: 17 additions & 1 deletion test/windows/test-firefox-windows.js
Expand Up @@ -6,7 +6,7 @@
const { Cc, Ci } = require('chrome');
const { setTimeout } = require('sdk/timers');
const { Loader } = require('sdk/test/loader');
const { onFocus, getMostRecentWindow, windows, isBrowser, getWindowTitle } = require('sdk/window/utils');
const { onFocus, getMostRecentWindow, windows, isBrowser, getWindowTitle, isFocused } = require('sdk/window/utils');
const { open, close, focus } = require('sdk/window/helpers');
const { browserWindows } = require("sdk/windows");
const tabs = require("sdk/tabs");
Expand Down Expand Up @@ -42,6 +42,22 @@ exports.testOpenAndCloseWindow = function(assert, done) {
});
};

exports.testNeWindowIsFocused = function(assert, done) {
let mainWindow = browserWindows.activeWindow;

browserWindows.open({
url: "about:blank",
onOpen: function(window) {
focus(viewFor(window)).then((window) => {
assert.ok(isFocused(window), 'the new window is focused');
assert.ok(isFocused(browserWindows.activeWindow), 'the active window is focused');
assert.ok(!isFocused(mainWindow), 'the main window is not focused');
close(window).then(done).catch(assert.fail);
})
}
});
}

exports.testOpenRelativePathWindow = function(assert, done) {
assert.equal(browserWindows.length, 1, "Only one window open");

Expand Down

0 comments on commit 95a2f25

Please sign in to comment.