From a02881ee4139ba0142d74b3f820e7d4c26b77641 Mon Sep 17 00:00:00 2001 From: Simon Lydell Date: Sun, 9 Oct 2016 13:03:16 +0200 Subject: [PATCH] Make Caret browsing handling more robust Make sure that people cannot end up with Caret mode accidentally enabled. Fixes #821. --- documentation/options.md | 15 +++++++++++++++ extension/lib/commands.coffee | 5 ++++- extension/lib/defaults.coffee | 1 + extension/lib/main.coffee | 10 ++++++++++ extension/lib/modes.coffee | 15 ++++++++------- extension/lib/prefs.coffee | 1 + extension/lib/vimfx.coffee | 14 ++++++++++++++ 7 files changed, 53 insertions(+), 8 deletions(-) diff --git a/documentation/options.md b/documentation/options.md index cf6d22e..e474397 100644 --- a/documentation/options.md +++ b/documentation/options.md @@ -378,6 +378,21 @@ block of the text with lots of search matches and then continue going through matches with `n` after that block, without having to spam `n` lots and lots of times. +### `browsewithcaret` + +[Caret mode] uses [Firefox’s own Caret mode] under the hood. This means that +VimFx temporarily enables the Firefox option `accessibility.browsewithcaret` +when you are in VimFx’s Caret mode. When you leave Caret mode, that option is +turned off again. + +VimFx automatically syncs the `browsewithcaret` option with +`accessibility.browsewithcaret`. If you change the latter manually (such as by +using the Firefox default shortcut ``), VimFx’s option is changed as well, +so you shouldn’t really have to touch this option at all. + +[Caret mode]: commands.md#caret-mode +[Firefox’s own Caret mode]: http://kb.mozillazine.org/Accessibility_features_of_Firefox#Allow_text_to_be_selected_with_the_keyboard + ### `ignore_ctrl_alt` This option is enabled by default on Windows, and disabled otherwise. diff --git a/extension/lib/commands.coffee b/extension/lib/commands.coffee index 59700f3..483bc7e 100644 --- a/extension/lib/commands.coffee +++ b/extension/lib/commands.coffee @@ -961,7 +961,10 @@ commands.esc = ({vim}) -> vim.hideNotification() utils.blurActiveBrowserElement(vim) vim.window.gBrowser.getFindBar().close() - MarkerContainer.remove(vim.window) # Better safe than sorry. + + # Better safe than sorry. + MarkerContainer.remove(vim.window) + vim._parent.resetCaretBrowsing() # Calling `.hide()` when the toolbar is not open can destroy it for the rest # of the Firefox session. The code here is taken from the `.toggle()` method. diff --git a/extension/lib/defaults.coffee b/extension/lib/defaults.coffee index 1ebc9a3..6dcf7dd 100644 --- a/extension/lib/defaults.coffee +++ b/extension/lib/defaults.coffee @@ -176,6 +176,7 @@ advanced_options = 'prevent_target_blank': true 'counts_enabled': true 'find_from_top_of_viewport': true + 'browsewithcaret': false 'ignore_ctrl_alt': (Services.appinfo.OS == 'WINNT') 'prevent_autofocus_modes': 'normal' 'config_file_directory': '' diff --git a/extension/lib/main.coffee b/extension/lib/main.coffee index 605c930..42b99c7 100644 --- a/extension/lib/main.coffee +++ b/extension/lib/main.coffee @@ -96,7 +96,17 @@ module.exports = (data, reason) -> # their 'loadConfig' code manually. config.load(vimfx) vimfx.on('shutdown', -> messageManager.send('unloadConfig')) + + # Since VimFx has its own Caret mode, it doesn’t make much sense having + # Firefox’s Caret mode always own, so make sure that it is disabled (or + # enabled if the user has chosen to explicitly have it always on.) + vimfx.resetCaretBrowsing() + module.onShutdown(-> + # Make sure that users are not left with Firefox’s own Caret mode + # accidentally enabled. + vimfx.resetCaretBrowsing() + # Make sure to run the below lines in this order. The second line results in # removing all message listeners in frame scripts, including the one for # 'unloadConfig' (see above). diff --git a/extension/lib/modes.coffee b/extension/lib/modes.coffee index 8dfb67a..569677e 100644 --- a/extension/lib/modes.coffee +++ b/extension/lib/modes.coffee @@ -30,7 +30,6 @@ translate = require('./translate') utils = require('./utils') {FORWARD, BACKWARD} = SelectionManager -CARET_BROWSING_PREF = 'accessibility.browsewithcaret' # Helper to create modes in a DRY way. mode = (modeName, obj, commands = null) -> @@ -125,27 +124,29 @@ helper_move_caret = (method, direction, {vim, storage, count = 1}) -> mode('caret', { onEnter: ({vim, storage}, {select = false} = {}) -> storage.select = select - storage.caretBrowsingPref = prefs.root.get(CARET_BROWSING_PREF) - prefs.root.set(CARET_BROWSING_PREF, true) + vim._parent.resetCaretBrowsing(true) vim._run('enable_caret') listener = -> return unless newVim = vim._parent.getCurrentVim(vim.window) - prefs.root.set( - CARET_BROWSING_PREF, - if newVim.mode == 'caret' then true else storage.caretBrowsingPref + vim._parent.resetCaretBrowsing( + if newVim.mode == 'caret' then true else null ) vim._parent.on('TabSelect', listener) storage.removeListener = -> vim._parent.off('TabSelect', listener) onLeave: ({vim, storage}) -> - prefs.root.set(CARET_BROWSING_PREF, storage.caretBrowsingPref) + vim._parent.resetCaretBrowsing() vim._run('clear_selection') storage.removeListener?() storage.removeListener = null onInput: (args, match) -> args.vim.hideNotification() + + # In case the user turns Caret Browsing off while in Caret mode. + args.vim._parent.resetCaretBrowsing(true) + switch match.type when 'full' match.command.run(args) diff --git a/extension/lib/prefs.coffee b/extension/lib/prefs.coffee index 09d0352..28719ed 100644 --- a/extension/lib/prefs.coffee +++ b/extension/lib/prefs.coffee @@ -99,6 +99,7 @@ module.exports = { set: set.bind(null, branches.root.user) has: has.bind(null, branches.root.user) tmp: tmp.bind(null, branches.root.user) + observe: observe.bind(null, branches.root.user) default: { get: get.bind(null, branches.root.default) set: set.bind(null, branches.root.default) diff --git a/extension/lib/vimfx.coffee b/extension/lib/vimfx.coffee index dd5dbe5..ece99fe 100644 --- a/extension/lib/vimfx.coffee +++ b/extension/lib/vimfx.coffee @@ -27,6 +27,7 @@ prefs = require('./prefs') utils = require('./utils') Vim = require('./vim') +CARET_BROWSING_PREF = 'accessibility.browsewithcaret' DIGIT = /^\d$/ class VimFx extends utils.EventEmitter @@ -35,10 +36,12 @@ class VimFx extends utils.EventEmitter @vims = new WeakMap() @lastClosedVim = null @goToCommand = null + @skipObserveCaretBrowsing = false @ignoreKeyEventsUntilTime = 0 @skipCreateKeyTrees = false @createKeyTrees() @reset() + @observeCaretBrowsing() @on('modeChange', ({vim}) => @reset(vim.mode)) SPECIAL_KEYS: { @@ -78,6 +81,17 @@ class VimFx extends utils.EventEmitter @lastInputTime = 0 @count = '' + resetCaretBrowsing: (value = @options.browsewithcaret) -> + @skipObserveCaretBrowsing = true + prefs.root.set(CARET_BROWSING_PREF, value) + @skipObserveCaretBrowsing = false + + observeCaretBrowsing: -> + prefs.root.observe(CARET_BROWSING_PREF, => + return if @skipObserveCaretBrowsing + prefs.set('browsewithcaret', prefs.root.get(CARET_BROWSING_PREF)) + ) + createKeyTrees: -> return if @skipCreateKeyTrees {@keyTrees, @errors} = -- 2.39.3