]> git.gir.st - VimFx.git/blob - extension/lib/main.coffee
Merge branch 'master' into develop
[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 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 # @if TESTS
38 test = require('../test/index')
39 # @endif
40
41 {AddonManager} = Cu.import('resource://gre/modules/AddonManager.jsm', {})
42
43 module.exports = (data, reason) ->
44 # Set default prefs and apply migrations as early as possible.
45 prefs.default.init()
46 applyMigrations(migrations)
47
48 parsedOptions = {}
49 for pref of defaults.all_options
50 parsedOptions[pref] = parsePref(pref)
51 vimfx = new VimFx(modes, parsedOptions)
52 vimfx.id = data.id
53 vimfx.version = data.version
54 AddonManager.getAddonByID(vimfx.id, (info) -> vimfx.info = info)
55
56 utils.loadCss("#{ADDON_PATH}/skin/style.css")
57
58 options.observe(vimfx)
59
60 prefs.observe('', (pref) ->
61 if pref.startsWith('mode.') or pref.startsWith('custom.')
62 vimfx.createKeyTrees()
63 else if pref of defaults.all_options
64 value = parsePref(pref)
65 vimfx.options[pref] = value
66 )
67
68 button.injectButton(vimfx)
69
70 setWindowAttribute = (window, name, value) ->
71 window.document.documentElement.setAttribute("vimfx-#{name}", value)
72
73 onModeDisplayChange = (data) ->
74 window = data.vim?.window ? data.event.originalTarget.ownerGlobal
75
76 # The 'modeChange' event provides the `vim` object that changed mode, but
77 # it might not be the current `vim` anymore so always get the current one.
78 return unless vim = vimfx.getCurrentVim(window)
79
80 setWindowAttribute(window, 'mode', vim.mode)
81 vimfx.emit('modeDisplayChange', {vim})
82
83 vimfx.on('modeChange', onModeDisplayChange)
84 vimfx.on('TabSelect', onModeDisplayChange)
85
86 vimfx.on('focusTypeChange', ({vim}) ->
87 setWindowAttribute(vim.window, 'focus-type', vim.focusType)
88 )
89
90 # `config.load` sends a 'loadConfig' message to all frame scripts, but it is
91 # intenionally run _before_ the frame scripts are loaded. Even if it is run
92 # after the frame scripts have been `messageManager.load`ed, we cannot know
93 # when it is ready to receive messages. Instead, the frame scripts trigger
94 # their 'loadConfig' code manually.
95 config.load(vimfx)
96 vimfx.on('shutdown', -> messageManager.send('unloadConfig'))
97 module.onShutdown(->
98 # Make sure to run the below lines in this order. The second line results in
99 # removing all message listeners in frame scripts, including the one for
100 # 'unloadConfig' (see above).
101 vimfx.emit('shutdown')
102 messageManager.send('shutdown')
103 )
104
105 windows = new WeakSet()
106 messageManager.listen('tabCreated', (data, callback, browser) ->
107 # Frame scripts are run in more places than we need. Tell those not to do
108 # anything.
109 unless browser.getAttribute('messagemanagergroup') == 'browsers'
110 callback(false)
111 return
112
113 window = browser.ownerGlobal
114 vimfx.addVim(browser)
115
116 unless windows.has(window)
117 windows.add(window)
118 eventManager = new UIEventManager(vimfx, window)
119 eventManager.addListeners(vimfx, window)
120 setWindowAttribute(window, 'mode', 'normal')
121 setWindowAttribute(window, 'focus-type', 'none')
122
123 callback(true)
124 )
125
126 messageManager.load("#{ADDON_PATH}/content/bootstrap.js")
127
128 # @if TESTS
129 test(vimfx)
130 runFrameTests = true
131 messageManager.listen('runTests', (data, callback) ->
132 callback(runFrameTests)
133 runFrameTests = false
134 )
135 # @endif
Imprint / Impressum