]> git.gir.st - VimFx.git/blob - packages/window-utils.coffee
Event handling changed to accumulate keys during keydown, and execute them during...
[VimFx.git] / packages / window-utils.coffee
1 { classes: Cc, interfaces: Ci, utils: Cu } = Components
2
3 { getWindowId } = require 'utils'
4
5 tools = {}
6 Cu.import "resource://gre/modules/Services.jsm", tools
7
8 ww = tools.Services.ww
9
10 # Will run `callback` funcion with `window` passed as argument
11 # when the window finishes loading. The function is run
12 # synchronously if the window has finished loading.
13 runOnWindowLoad = (callback, window) ->
14 if window.document.readyState == 'complete'
15 callback window
16 else
17 onLoad = ->
18 window.removeEventListener 'load', arguments.callee, true
19 callback(window)
20
21 window.addEventListener 'load', onLoad, true
22
23 # Applies `runOnWindowLoad` to all windows currently opened
24 # passing it the `callback` argument
25 applyToWindows = (callback) ->
26 winEnum = ww.getWindowEnumerator()
27 while winEnum.hasMoreElements()
28 window = winEnum.getNext().QueryInterface(Ci.nsIDOMWindow)
29 runOnWindowLoad callback, window
30
31 # Checks if the window is a top level window, typically
32 # an instance of ChromeWindow. I'm kind of confused with the
33 # terms
34 isBrowserWindow = (window) ->
35 return window.document.documentElement.getAttribute("windowtype") == "navigator:browser"
36
37 # Class whose instanced passed to WindowWatcher service
38 # will receive notifications when new top level windows
39 # are opened/closed. It won't be notified about windows
40 # that were open when observer has been registered.
41 class WindowObserver
42 constructor: (@delegate) ->
43
44 observe: (subject, topic, data) ->
45 window = subject.QueryInterface(Ci.nsIDOMWindow)
46 switch topic
47 when 'domwindowopened'
48 runOnWindowLoad @delegate.track, window
49 when 'domwindowclosed'
50 runOnWindowLoad @delegate.untrack, window
51
52 # Class that will track all top level windows
53 # Each time a window is opened/closed the `track` and `untrack`
54 # methods will be called on the window
55 class WindowTracker
56
57 constructor: (@delegate) ->
58 @observer = new WindowObserver @delegate
59
60 start: ->
61 # We have to run the track on all windows that have already
62 # been opened
63 applyToWindows @delegate.track
64 ww.registerNotification @observer
65
66 stop: ->
67 # And run untack as well
68 ww.unregisterNotification @observer
69 applyToWindows @delegate.untrack
70
71 exports.runOnWindowLoad = runOnWindowLoad
72 exports.applyToWindows = applyToWindows
73 exports.WindowTracker = WindowTracker
74 exports.isBrowserWindow = isBrowserWindow
Imprint / Impressum