]> git.gir.st - VimFx.git/blob - extension/lib/main.coffee
Optimize the API
[VimFx.git] / extension / lib / main.coffee
1 ###
2 # Copyright Anton Khodakivskiy 2012, 2013, 2014.
3 # Copyright Simon Lydell 2013, 2014.
4 #
5 # This file is part of VimFx.
6 #
7 # VimFx is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # VimFx is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with VimFx. If not, see <http://www.gnu.org/licenses/>.
19 ###
20
21 # This file pulls in all the different parts of VimFx, initializes them, and
22 # stiches them together.
23
24 createAPI = require('./api')
25 button = require('./button')
26 defaults = require('./defaults')
27 UIEventManager = require('./events')
28 messageManager = require('./message-manager')
29 modes = require('./modes')
30 options = require('./options')
31 parsePref = require('./parse-prefs')
32 prefs = require('./prefs')
33 utils = require('./utils')
34 VimFx = require('./vimfx')
35 test = try require('../test/index')
36
37 Cu.import('resource://gre/modules/AddonManager.jsm')
38
39 module.exports = (data, reason) ->
40 parsedOptions = {}
41 for pref of defaults.all_options
42 parsedOptions[pref] = parsePref(pref)
43 vimfx = new VimFx(modes, parsedOptions)
44 vimfx.id = data.id
45 vimfx.version = data.version
46 AddonManager.getAddonByID(vimfx.id, (info) -> vimfx.info = info)
47
48 utils.loadCss('style')
49
50 options.observe(vimfx)
51
52 skipCreateKeyTrees = false
53 prefs.observe('', (pref) ->
54 if pref.startsWith('mode.') or pref.startsWith('custom.')
55 vimfx.createKeyTrees() unless skipCreateKeyTrees
56 else if pref of defaults.all_options
57 value = parsePref(pref)
58 vimfx.options[pref] = value
59 )
60
61 button.injectButton(vimfx)
62
63 # Setup the public API. See public.coffee for more information. This is done
64 # _after_ the prefs observing setup, so that option prefs get validated and
65 # used when calling `vimfx.set()`.
66 apiUrl = "#{ data.resourceURI.spec }lib/public.js"
67 prefs.set('api_url', apiUrl)
68 publicScope = Cu.import(apiUrl, {})
69 api = createAPI(vimfx)
70 publicScope._invokeCallback = (callback) ->
71 # Calling `vimfx.createKeyTrees()` after each `vimfx.set()` that modifies a
72 # shortcut is absolutely redundant and may make Firefox start slower. Do it
73 # once instead.
74 skipCreateKeyTrees = true
75 callback(api)
76 skipCreateKeyTrees = false
77 vimfx.createKeyTrees()
78 module.onShutdown(-> publicScope._invokeCallbacks = null)
79
80 # Pass the API to add-ons that loaded before VimFx, either because they just
81 # happened to do so when Firefox started, or because VimFx was updated (or
82 # disabled and then enabled) in the middle of the session. Because of the
83 # latter case, `Cu.unload(apiUrl)` is not called on shutdown. Otherwise you’d
84 # have to either restart Firefox, or disable and enable every add-on using the
85 # API in order for them to take effect again.
86 if publicScope._callbacks.length > 0
87 publicScope._invokeCallback((api) ->
88 callback(api) for callback in publicScope._callbacks
89 return
90 )
91
92 test?(vimfx)
93
94 windows = new WeakSet()
95 messageManager.listen('tabCreated', (data, { target }) ->
96 # Frame script are run in more places than we need. Tell those not to do
97 # anything.
98 return false unless target.getAttribute('messagemanagergroup') == 'browsers'
99
100 window = target.ownerGlobal
101 vimfx.addVim(target)
102
103 unless windows.has(window)
104 windows.add(window)
105 eventManager = new UIEventManager(vimfx, window)
106 eventManager.addListeners(vimfx, window)
107
108 return [__SCRIPT_URI_SPEC__, MULTI_PROCESS_ENABLED]
109 )
110
111 messageManager.load('bootstrap')
Imprint / Impressum