]> git.gir.st - VimFx.git/blob - extension/lib/main.coffee
Make Caret browsing handling more robust
[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 button = require('./button')
25 config = require('./config')
26 defaults = require('./defaults')
27 help = require('./help')
28 {applyMigrations} = require('./legacy')
29 MarkerContainer = require('./marker-container')
30 messageManager = require('./message-manager')
31 migrations = require('./migrations')
32 modes = require('./modes')
33 options = require('./options')
34 {parsePref} = require('./parse-prefs')
35 prefs = require('./prefs')
36 UIEventManager = require('./events')
37 utils = require('./utils')
38 VimFx = require('./vimfx')
39 # @if TESTS
40 test = require('../test/index')
41 # @endif
42
43 {AddonManager} = Cu.import('resource://gre/modules/AddonManager.jsm', {})
44
45 module.exports = (data, reason) ->
46 # Set default prefs and apply migrations as early as possible.
47 prefs.default.init()
48 applyMigrations(migrations)
49
50 parsedOptions = {}
51 for pref of defaults.all_options
52 parsedOptions[pref] = parsePref(pref)
53 vimfx = new VimFx(modes, parsedOptions)
54 vimfx.id = data.id
55 vimfx.version = data.version
56 AddonManager.getAddonByID(vimfx.id, (info) -> vimfx.info = info)
57
58 utils.loadCss("#{ADDON_PATH}/skin/style.css")
59
60 options.observe(vimfx)
61
62 prefs.observe('', (pref) ->
63 if pref.startsWith('mode.') or pref.startsWith('custom.')
64 vimfx.createKeyTrees()
65 else if pref of defaults.all_options
66 value = parsePref(pref)
67 vimfx.options[pref] = value
68 )
69
70 button.injectButton(vimfx)
71
72 setWindowAttribute = (window, name, value) ->
73 window.document.documentElement.setAttribute("vimfx-#{name}", value)
74
75 onModeDisplayChange = (data) ->
76 window = data.vim?.window ? data.event.originalTarget.ownerGlobal
77
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 vimfx.on('modeChange', onModeDisplayChange)
86 vimfx.on('TabSelect', onModeDisplayChange)
87
88 vimfx.on('focusTypeChange', ({vim}) ->
89 setWindowAttribute(vim.window, 'focus-type', vim.focusType)
90 )
91
92 # `config.load` sends a 'loadConfig' message to all frame scripts, but it is
93 # intenionally run _before_ the frame scripts are loaded. Even if it is run
94 # after the frame scripts have been `messageManager.load`ed, we cannot know
95 # when it is ready to receive messages. Instead, the frame scripts trigger
96 # their 'loadConfig' code manually.
97 config.load(vimfx)
98 vimfx.on('shutdown', -> messageManager.send('unloadConfig'))
99
100 # Since VimFx has its own Caret mode, it doesn’t make much sense having
101 # Firefox’s Caret mode always own, so make sure that it is disabled (or
102 # enabled if the user has chosen to explicitly have it always on.)
103 vimfx.resetCaretBrowsing()
104
105 module.onShutdown(->
106 # Make sure that users are not left with Firefox’s own Caret mode
107 # accidentally enabled.
108 vimfx.resetCaretBrowsing()
109
110 # Make sure to run the below lines in this order. The second line results in
111 # removing all message listeners in frame scripts, including the one for
112 # 'unloadConfig' (see above).
113 vimfx.emit('shutdown')
114 messageManager.send('shutdown')
115 )
116
117 windows = new WeakSet()
118 messageManager.listen('tabCreated', (data, callback, browser) ->
119 # Frame scripts are run in more places than we need. Tell those not to do
120 # anything.
121 unless browser.getAttribute('messagemanagergroup') == 'browsers'
122 callback(false)
123 return
124
125 window = browser.ownerGlobal
126 vimfx.addVim(browser)
127
128 unless windows.has(window)
129 windows.add(window)
130 eventManager = new UIEventManager(vimfx, window)
131 eventManager.addListeners(vimfx, window)
132 setWindowAttribute(window, 'mode', 'normal')
133 setWindowAttribute(window, 'focus-type', 'none')
134 module.onShutdown(->
135 MarkerContainer.remove(window)
136 help.removeHelp(window)
137 )
138
139 callback(true)
140 )
141
142 # For tabs not visited yet since a session restore (“pending” tabs), Firefox
143 # seems to not load the frame script immediately, but instead remember the URI
144 # and load it when the user eventually visits that tab. If VimFx is updated
145 # during that time this means that the below URI is saved several times, and
146 # will be loaded that many times. Therefore the URI is changed with each
147 # build, causing remembered URIs to point to non-existent files.
148 messageManager.load("#{ADDON_PATH}/content/bootstrap-frame-#{BUILD_TIME}.js")
149
150 # @if TESTS
151 runTests = true
152 messageManager.listen('runTests', (data, callback) ->
153 # Running the regular tests inside this callback means that there will be a
154 # `window` available for tests, if they need one.
155 test(vimfx) if runTests
156 callback(runTests)
157 runTests = false
158 )
159 # @endif
Imprint / Impressum