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