]> git.gir.st - VimFx.git/blob - packages/utils.coffee
Added extension icon, and renamed the toolbar button icons to icon16*.png
[VimFx.git] / packages / utils.coffee
1 { interfaces: Ci, classes: Cc } = Components
2
3 HTMLInputElement = Ci.nsIDOMHTMLInputElement
4 HTMLTextAreaElement = Ci.nsIDOMHTMLTextAreaElement
5 HTMLSelectElement = Ci.nsIDOMHTMLSelectElement
6 XULDocument = Ci.nsIDOMXULDocument
7 XULElement = Ci.nsIDOMXULElement
8 HTMLDocument = Ci.nsIDOMHTMLDocument
9 HTMLElement = Ci.nsIDOMHTMLElement
10 Window = Ci.nsIDOMWindow
11 ChromeWindow = Ci.nsIDOMChromeWindow
12 KeyboardEvent = Ci.nsIDOMKeyEvent
13
14 _sss = Cc["@mozilla.org/content/style-sheet-service;1"].getService(Ci.nsIStyleSheetService)
15 _clip = Cc["@mozilla.org/widget/clipboard;1"].getService(Ci.nsIClipboard)
16
17 { getPref } = require 'prefs'
18
19 class Bucket
20 constructor: (@idFunc, @newFunc) ->
21 @bucket = {}
22
23 get: (obj) ->
24 id = @idFunc obj
25 if container = @bucket[id]
26 return container
27 else
28 return @bucket[id] = @newFunc obj
29
30 forget: (obj) ->
31 delete @bucket[id] if id = @idFunc obj
32
33 isRootWindow = (window) ->
34 window.location == "chrome://browser/content/browser.xul"
35
36 # Returns the `window` from the currently active tab.
37 getCurrentTabWindow = (event) ->
38 if window = getEventWindow event
39 if rootWindow = getRootWindow window
40 return rootWindow.gBrowser.selectedTab.linkedBrowser.contentWindow
41
42 # Returns the window associated with the event
43 getEventWindow = (event) ->
44 if event.originalTarget instanceof Window
45 return event.originalTarget
46 else
47 doc = event.originalTarget.ownerDocument or event.originalTarget
48 if doc instanceof HTMLDocument or doc instanceof XULDocument
49 return doc.defaultView
50
51 getEventRootWindow = (event) ->
52 if window = getEventWindow event
53 return getRootWindow window
54
55 getEventTabBrowser = (event) ->
56 cw.gBrowser if cw = getEventRootWindow event
57
58 getRootWindow = (window) ->
59 return window.QueryInterface(Ci.nsIInterfaceRequestor)
60 .getInterface(Ci.nsIWebNavigation)
61 .QueryInterface(Ci.nsIDocShellTreeItem)
62 .rootTreeItem
63 .QueryInterface(Ci.nsIInterfaceRequestor)
64 .getInterface(Window);
65
66 isElementEditable = (element) ->
67 return element.isContentEditable or \
68 element instanceof HTMLInputElement or \
69 element instanceof HTMLTextAreaElement or \
70 element instanceof HTMLSelectElement
71
72 getWindowId = (window) ->
73 return window.QueryInterface(Components.interfaces.nsIInterfaceRequestor)
74 .getInterface(Components.interfaces.nsIDOMWindowUtils)
75 .outerWindowID
76
77 getSessionStore = ->
78 Cc["@mozilla.org/browser/sessionstore;1"].getService(Ci.nsISessionStore);
79
80 # Function that returns a URI to the css file that's part of the extension
81 cssUri = do () ->
82 (name) ->
83 baseURI = Services.io.newURI __SCRIPT_URI_SPEC__, null, null
84 uri = Services.io.newURI "resources/#{ name }.css", null, baseURI
85 return uri
86
87 # Loads the css identified by the name in the StyleSheetService as User Stylesheet
88 # The stylesheet is then appended to every document, but it can be overwritten by
89 # any user css
90 loadCss = (name) ->
91 uri = cssUri(name)
92 _sss.loadAndRegisterSheet(uri, _sss.AGENT_SHEET)
93
94 unload ->
95 _sss.unregisterSheet(uri, _sss.AGENT_SHEET)
96
97 # Processes the keyboard event and extracts string representation
98 # of the key *without modifiers*.
99 # Only processes that can be handled by the extension
100 #
101 # Currently we handle letters, Escape and Tab keys
102 keyboardEventChar = (keyboardEvent) ->
103
104 # Ignore the key if Meta of Alt are pressed
105 if keyboardEvent.metaKey or keyboardEvent.altKey
106 return
107
108 key = keyboardEvent.keyCode
109 char = String.fromCharCode(key)
110 HINTCHARS = getPref 'hint_chars'
111
112 if key >= KeyboardEvent.DOM_VK_A and key <= KeyboardEvent.DOM_VK_Z
113 if keyboardEvent.shiftKey
114 return char.toUpperCase()
115 else
116 return char.toLowerCase()
117 # Allow additional chars from the hint chars list
118 else if HINTCHARS.search(char) > -1
119 return char
120 else
121 switch keyboardEvent.keyCode
122 when KeyboardEvent.DOM_VK_ESCAPE
123 return 'Esc'
124 when KeyboardEvent.DOM_VK_BACK_SPACE
125 return 'Backspace'
126
127 # Extracts string representation of the KeyboardEvent and adds
128 # relevant modifiers (_ctrl_, _alt_, _meta_) in case they were pressed
129 keyboardEventKeyString = (keyboardEvent) ->
130 char = keyboardEventChar keyboardEvent
131
132 if char and keyboardEvent.ctrlKey
133 char = "c-#{ char }"
134
135 return char
136
137 # Simulate mouse click with full chain of event
138 # Copied from Vimium codebase
139 simulateClick = (element, modifiers) ->
140 document = element.ownerDocument
141 window = document.defaultView
142 modifiers ||= {}
143
144 eventSequence = ["mouseover", "mousedown", "mouseup", "click"]
145 for event in eventSequence
146 mouseEvent = document.createEvent("MouseEvents")
147 mouseEvent.initMouseEvent(event, true, true, window, 1, 0, 0, 0, 0, modifiers.ctrlKey, false, false,
148 modifiers.metaKey, 0, null)
149 # Debugging note: Firefox will not execute the element's default action if we dispatch this click event,
150 # but Webkit will. Dispatching a click on an input box does not seem to focus it; we do that separately
151 element.dispatchEvent(mouseEvent)
152
153 # Write a string into system clipboard
154 writeToClipboard = (text) ->
155 str = Cc["@mozilla.org/supports-string;1"].createInstance(Ci.nsISupportsString);
156 str.data = text
157
158 trans = Cc["@mozilla.org/widget/transferable;1"].createInstance(Ci.nsITransferable);
159 trans.addDataFlavor("text/unicode");
160 trans.setTransferData("text/unicode", str, text.length * 2);
161
162 _clip.setData trans, null, Ci.nsIClipboard.kGlobalClipboard
163 #
164 # Write a string into system clipboard
165 readFromClipboard = () ->
166 trans = Cc["@mozilla.org/widget/transferable;1"].createInstance(Ci.nsITransferable);
167 trans.addDataFlavor("text/unicode");
168
169 _clip.getData trans, Ci.nsIClipboard.kGlobalClipboard
170
171 str = {}
172 strLength = {}
173
174 trans.getTransferData("text/unicode", str, strLength)
175
176 if str
177 str = str.value.QueryInterface(Ci.nsISupportsString);
178 return str.data.substring 0, strLength.value / 2
179
180 return undefined
181
182 # Executes function `func` and mearues how much time it took
183 timeIt = (func, msg) ->
184 start = new Date().getTime()
185 result = func()
186 end = new Date().getTime()
187
188 console.log msg, end - start
189 return result
190
191 exports.Bucket = Bucket
192 exports.isRootWindow = isRootWindow
193 exports.getCurrentTabWindow = getCurrentTabWindow
194 exports.getEventWindow = getEventWindow
195 exports.getEventRootWindow = getEventRootWindow
196 exports.getEventTabBrowser = getEventTabBrowser
197
198 exports.getWindowId = getWindowId
199 exports.getRootWindow = getRootWindow
200 exports.isElementEditable = isElementEditable
201 exports.getSessionStore = getSessionStore
202
203 exports.loadCss = loadCss
204
205 exports.keyboardEventKeyString = keyboardEventKeyString
206 exports.simulateClick = simulateClick
207 exports.readFromClipboard = readFromClipboard
208 exports.writeToClipboard = writeToClipboard
209 exports.timeIt = timeIt
Imprint / Impressum