]> git.gir.st - VimFx.git/blob - extension/lib/main.coffee
Merge pull request #615 from akhodakivskiy/rework-notifications
[VimFx.git] / extension / lib / main.coffee
1 ###
2 # Copyright Anton Khodakivskiy 2012, 2013, 2014.
3 # Copyright Simon Lydell 2013, 2014, 2015.
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 statusPanel = require('./status-panel')
34 utils = require('./utils')
35 VimFx = require('./vimfx')
36 test = try require('../test/index')
37
38 Cu.import('resource://gre/modules/AddonManager.jsm')
39
40 module.exports = (data, reason) ->
41 parsedOptions = {}
42 for pref of defaults.all_options
43 parsedOptions[pref] = parsePref(pref)
44 vimfx = new VimFx(modes, parsedOptions)
45 vimfx.id = data.id
46 vimfx.version = data.version
47 AddonManager.getAddonByID(vimfx.id, (info) -> vimfx.info = info)
48
49 utils.loadCss('style')
50
51 options.observe(vimfx)
52
53 skipCreateKeyTrees = false
54 prefs.observe('', (pref) ->
55 if pref.startsWith('mode.') or pref.startsWith('custom.')
56 vimfx.createKeyTrees() unless skipCreateKeyTrees
57 else if pref of defaults.all_options
58 value = parsePref(pref)
59 vimfx.options[pref] = value
60 )
61
62 button.injectButton(vimfx)
63
64 onModeDisplayChange = (vimOrEvent) ->
65 window = vimOrEvent.window ? vimOrEvent.originalTarget.ownerGlobal
66
67 # The 'modeChange' event provides the `vim` object that changed mode, but it
68 # might not be the current `vim` anymore, so always get the current one.
69 return unless vim = vimfx.getCurrentVim(window)
70
71 window.document.documentElement.setAttribute('vimfx-mode', vim.mode)
72 vimfx.emit('modeDisplayChange', vim)
73
74 vimfx.on('modeChange', onModeDisplayChange)
75 vimfx.on('TabSelect', onModeDisplayChange)
76
77 # Setup the public API. See public.coffee for more information. This is done
78 # _after_ the prefs observing setup, so that option prefs get validated and
79 # used when calling `vimfx.set()`.
80 apiUrl = "#{data.resourceURI.spec}lib/public.js"
81 prefs.set('api_url', apiUrl)
82 publicScope = Cu.import(apiUrl, {})
83 api = createAPI(vimfx)
84 publicScope._invokeCallback = (callback) ->
85 # Calling `vimfx.createKeyTrees()` after each `vimfx.set()` that modifies a
86 # shortcut is absolutely redundant and may make Firefox start slower. Do it
87 # once instead.
88 skipCreateKeyTrees = true
89 callback(api)
90 skipCreateKeyTrees = false
91 vimfx.createKeyTrees()
92 module.onShutdown(-> publicScope._invokeCallbacks = null)
93
94 # Pass the API to add-ons that loaded before VimFx, either because they just
95 # happened to do so when Firefox started, or because VimFx was updated (or
96 # disabled and then enabled) in the middle of the session. Because of the
97 # latter case, `Cu.unload(apiUrl)` is not called on shutdown. Otherwise you’d
98 # have to either restart Firefox, or disable and enable every add-on using the
99 # API in order for them to take effect again. (`_callbacks` should always
100 # exist, but it’s better to be safe than sorry.)
101 if publicScope._callbacks?.length > 0
102 publicScope._invokeCallback((api) ->
103 callback(api) for callback in publicScope._callbacks
104 return
105 )
106
107 test?(vimfx)
108
109 windows = new WeakSet()
110 messageManager.listen('tabCreated', (data, {target: browser}) ->
111 # Frame script are run in more places than we need. Tell those not to do
112 # anything.
113 group = browser.getAttribute('messagemanagergroup')
114 return false unless group == 'browsers'
115
116 window = browser.ownerGlobal
117 vimfx.addVim(browser)
118
119 unless windows.has(window)
120 windows.add(window)
121 eventManager = new UIEventManager(vimfx, window)
122 eventManager.addListeners(vimfx, window)
123 window.document.documentElement.setAttribute('vimfx-mode', 'normal')
124 statusPanel.injectStatusPanel(browser, vimfx)
125
126 return [__SCRIPT_URI_SPEC__, MULTI_PROCESS_ENABLED]
127 )
128
129 messageManager.load('bootstrap')
Imprint / Impressum