From c5c08783d2b8454336b6cff2134c8636e889e1c3 Mon Sep 17 00:00:00 2001 From: Shin'ya Ueoka Date: Sun, 6 May 2018 11:12:52 +0900 Subject: OperationComponent --- src/background/index.js | 3 +++ 1 file changed, 3 insertions(+) (limited to 'src/background/index.js') diff --git a/src/background/index.js b/src/background/index.js index ff27796..4f6b23f 100644 --- a/src/background/index.js +++ b/src/background/index.js @@ -1,6 +1,7 @@ import * as settingActions from 'background/actions/setting'; import messages from 'shared/messages'; import BackgroundComponent from 'background/components/background'; +import OperationComponent from 'background/components/operation'; import reducers from 'background/reducers'; import { createStore } from 'shared/store'; import * as versions from 'shared/versions'; @@ -16,6 +17,8 @@ const store = createStore(reducers, (e, sender) => { }); // eslint-disable-next-line no-unused-vars const backgroundComponent = new BackgroundComponent(store); +// eslint-disable-next-line no-unused-vars +const operationComponent = new OperationComponent(store); store.dispatch(settingActions.load()); -- cgit v1.2.3 From 2c366ac3b1d7114869567845c955238e96056565 Mon Sep 17 00:00:00 2001 From: Shin'ya Ueoka Date: Sun, 6 May 2018 16:11:40 +0900 Subject: Previous selected tab as redux --- src/background/actions/index.js | 3 +++ src/background/actions/tab.js | 11 ++++++++++- src/background/components/operation.js | 7 ++++++- src/background/components/tab.js | 17 +++++++++++++++++ src/background/index.js | 3 +++ src/background/reducers/index.js | 3 +++ src/background/reducers/tab.js | 19 +++++++++++++++++++ src/background/shared/tabs.js | 16 +++------------- 8 files changed, 64 insertions(+), 15 deletions(-) create mode 100644 src/background/components/tab.js create mode 100644 src/background/reducers/tab.js (limited to 'src/background/index.js') diff --git a/src/background/actions/index.js b/src/background/actions/index.js index 2bdaaf2..3833389 100644 --- a/src/background/actions/index.js +++ b/src/background/actions/index.js @@ -5,4 +5,7 @@ export default { // Find FIND_SET_KEYWORD: 'find.set.keyword', + + // Tab + TAB_SELECTED: 'tab.selected', }; diff --git a/src/background/actions/tab.js b/src/background/actions/tab.js index 3c642fd..0d439fd 100644 --- a/src/background/actions/tab.js +++ b/src/background/actions/tab.js @@ -1,3 +1,5 @@ +import actions from './index'; + const openNewTab = (url, openerTabId, background = false, adjacent = false) => { if (adjacent) { return browser.tabs.query({ @@ -18,4 +20,11 @@ const openToTab = (url, tab) => { return browser.tabs.update(tab.id, { url: url }); }; -export { openNewTab, openToTab }; +const selected = (tabId) => { + return { + type: actions.TAB_SELECTED, + tabId, + }; +}; + +export { openNewTab, openToTab, selected }; diff --git a/src/background/components/operation.js b/src/background/components/operation.js index b9581c9..9a0b4e1 100644 --- a/src/background/components/operation.js +++ b/src/background/components/operation.js @@ -30,6 +30,8 @@ export default class BackgroundComponent { // eslint-disable-next-line complexity exec(operation, tab) { + let tabState = this.store.getState().tab; + switch (operation.type) { case operations.TAB_CLOSE: return tabs.closeTab(tab.id); @@ -46,7 +48,10 @@ export default class BackgroundComponent { case operations.TAB_LAST: return tabs.selectLastTab(); case operations.TAB_PREV_SEL: - return tabs.selectPrevSelTab(); + if (tabState.previousSelected > 0) { + return tabs.selectTab(tabState.previousSelected); + } + break; case operations.TAB_RELOAD: return tabs.reload(tab, operation.cache); case operations.TAB_PIN: diff --git a/src/background/components/tab.js b/src/background/components/tab.js new file mode 100644 index 0000000..b273546 --- /dev/null +++ b/src/background/components/tab.js @@ -0,0 +1,17 @@ +import * as tabActions from '../actions/tab'; + +export default class TabComponent { + constructor(store) { + this.store = store; + + browser.tabs.onActivated.addListener((info) => { + return browser.tabs.query({ currentWindow: true }).then(() => { + return this.onTabActivated(info); + }); + }); + } + + onTabActivated(info) { + return this.store.dispatch(tabActions.selected(info.tabId)); + } +} diff --git a/src/background/index.js b/src/background/index.js index 4f6b23f..be042ce 100644 --- a/src/background/index.js +++ b/src/background/index.js @@ -2,6 +2,7 @@ import * as settingActions from 'background/actions/setting'; import messages from 'shared/messages'; import BackgroundComponent from 'background/components/background'; import OperationComponent from 'background/components/operation'; +import TabComponent from 'background/components/tab'; import reducers from 'background/reducers'; import { createStore } from 'shared/store'; import * as versions from 'shared/versions'; @@ -19,6 +20,8 @@ const store = createStore(reducers, (e, sender) => { const backgroundComponent = new BackgroundComponent(store); // eslint-disable-next-line no-unused-vars const operationComponent = new OperationComponent(store); +// eslint-disable-next-line no-unused-vars +const tabComponent = new TabComponent(store); store.dispatch(settingActions.load()); diff --git a/src/background/reducers/index.js b/src/background/reducers/index.js index 63ff0f8..5729f0a 100644 --- a/src/background/reducers/index.js +++ b/src/background/reducers/index.js @@ -1,15 +1,18 @@ import settingReducer from './setting'; import findReducer from './find'; +import tabReducer from './tab'; // Make setting reducer instead of re-use const defaultState = { setting: settingReducer(undefined, {}), find: findReducer(undefined, {}), + tab: tabReducer(undefined, {}), }; export default function reducer(state = defaultState, action = {}) { return Object.assign({}, state, { setting: settingReducer(state.setting, action), find: findReducer(state.find, action), + tab: tabReducer(state.tab, action), }); } diff --git a/src/background/reducers/tab.js b/src/background/reducers/tab.js new file mode 100644 index 0000000..e0cdf32 --- /dev/null +++ b/src/background/reducers/tab.js @@ -0,0 +1,19 @@ +import actions from 'background/actions'; + +const defaultState = { + previousSelected: -1, + currentSelected: -1, +}; + +export default function reducer(state = defaultState, action = {}) { + switch (action.type) { + case actions.TAB_SELECTED: + return { + previousSelected: state.currentSelected, + currentSelected: action.tabId, + }; + default: + return state; + } +} + diff --git a/src/background/shared/tabs.js b/src/background/shared/tabs.js index 277afb2..f1dcc73 100644 --- a/src/background/shared/tabs.js +++ b/src/background/shared/tabs.js @@ -1,13 +1,3 @@ -let prevSelTab = 1; -let currSelTab = 1; - -browser.tabs.onActivated.addListener((activeInfo) => { - return browser.tabs.query({ currentWindow: true }).then(() => { - prevSelTab = currSelTab; - currSelTab = activeInfo.tabId; - }); -}); - const closeTab = (id) => { return browser.tabs.get(id).then((tab) => { if (!tab.pinned) { @@ -102,8 +92,8 @@ const selectLastTab = () => { }); }; -const selectPrevSelTab = () => { - return browser.tabs.update(prevSelTab, { active: true }); +const selectTab = (id) => { + return browser.tabs.update(id, { active: true }); }; const reload = (current, cache) => { @@ -131,6 +121,6 @@ const duplicate = (id) => { export { closeTab, closeTabForce, reopenTab, selectAt, selectByKeyword, selectPrevTab, selectNextTab, selectFirstTab, - selectLastTab, selectPrevSelTab, reload, updateTabPinned, + selectLastTab, selectTab, reload, updateTabPinned, toggleTabPinned, duplicate }; -- cgit v1.2.3 From 129aae38df1a17a5850898d5832bb8112ead3cbb Mon Sep 17 00:00:00 2001 From: Shin'ya Ueoka Date: Sun, 6 May 2018 22:30:04 +0900 Subject: Indicator shows the add-on enabled --- manifest.json | 6 +++++ resources/disabled_32x32.png | Bin 0 -> 1426 bytes resources/enabled_32x32.png | Bin 0 -> 1504 bytes src/background/components/indicator.js | 38 ++++++++++++++++++++++++++++ src/background/index.js | 8 +++--- src/background/shared/indicators.js | 13 ++++++++++ src/content/components/common/index.js | 14 ++++++++++ src/content/components/top-content/index.js | 7 +++++ src/shared/messages.js | 3 +++ 9 files changed, 86 insertions(+), 3 deletions(-) create mode 100644 resources/disabled_32x32.png create mode 100644 resources/enabled_32x32.png create mode 100644 src/background/components/indicator.js create mode 100644 src/background/shared/indicators.js (limited to 'src/background/index.js') diff --git a/manifest.json b/manifest.json index 4c43b64..39d14f4 100644 --- a/manifest.json +++ b/manifest.json @@ -40,5 +40,11 @@ ], "options_ui": { "page": "build/settings.html" + }, + "browser_action": { + "default_icon": { + "32": "resources/enabled_32x32.png" + }, + "default_title": "Vim Vixen" } } diff --git a/resources/disabled_32x32.png b/resources/disabled_32x32.png new file mode 100644 index 0000000..d3b88db Binary files /dev/null and b/resources/disabled_32x32.png differ diff --git a/resources/enabled_32x32.png b/resources/enabled_32x32.png new file mode 100644 index 0000000..eaf2862 Binary files /dev/null and b/resources/enabled_32x32.png differ diff --git a/src/background/components/indicator.js b/src/background/components/indicator.js new file mode 100644 index 0000000..ccdcc74 --- /dev/null +++ b/src/background/components/indicator.js @@ -0,0 +1,38 @@ +import * as indicators from '../shared/indicators'; +import messages from 'shared/messages'; + +export default class IndicatorComponent { + constructor(store) { + this.store = store; + + messages.onMessage(this.onMessage.bind(this)); + + browser.tabs.onActivated.addListener((info) => { + return browser.tabs.query({ currentWindow: true }).then(() => { + return this.onTabActivated(info); + }); + }); + } + + onTabActivated(info) { + return browser.tabs.sendMessage(info.tabId, { + type: messages.ADDON_ENABLED_QUERY, + }).then((resp) => { + return this.updateIndicator(resp.enabled); + }); + } + + onMessage(message) { + switch (message.type) { + case messages.ADDON_ENABLED_RESPONSE: + return this.updateIndicator(message.enabled); + } + } + + updateIndicator(enabled) { + if (enabled) { + return indicators.enable(); + } + return indicators.disable(); + } +} diff --git a/src/background/index.js b/src/background/index.js index be042ce..3f1013c 100644 --- a/src/background/index.js +++ b/src/background/index.js @@ -3,6 +3,7 @@ import messages from 'shared/messages'; import BackgroundComponent from 'background/components/background'; import OperationComponent from 'background/components/operation'; import TabComponent from 'background/components/tab'; +import IndicatorComponent from 'background/components/indicator'; import reducers from 'background/reducers'; import { createStore } from 'shared/store'; import * as versions from 'shared/versions'; @@ -16,12 +17,13 @@ const store = createStore(reducers, (e, sender) => { }); } }); -// eslint-disable-next-line no-unused-vars + +/* eslint-disable no-unused-vars */ const backgroundComponent = new BackgroundComponent(store); -// eslint-disable-next-line no-unused-vars const operationComponent = new OperationComponent(store); -// eslint-disable-next-line no-unused-vars const tabComponent = new TabComponent(store); +const indicatorComponent = new IndicatorComponent(store); +/* eslint-enable no-unused-vars */ store.dispatch(settingActions.load()); diff --git a/src/background/shared/indicators.js b/src/background/shared/indicators.js new file mode 100644 index 0000000..74002c4 --- /dev/null +++ b/src/background/shared/indicators.js @@ -0,0 +1,13 @@ +const enable = () => { + return browser.browserAction.setIcon({ + path: 'resources/enabled_32x32.png', + }); +}; + +const disable = () => { + return browser.browserAction.setIcon({ + path: 'resources/disabled_32x32.png', + }); +}; + +export { enable, disable }; diff --git a/src/content/components/common/index.js b/src/content/components/common/index.js index 565632c..c5d1df7 100644 --- a/src/content/components/common/index.js +++ b/src/content/components/common/index.js @@ -14,10 +14,12 @@ export default class Common { input.onKey(key => keymapper.key(key)); this.store = store; + this.prevEnabled = this.store.getState().addon.enabled; this.reloadSettings(); messages.onMessage(this.onMessage.bind(this)); + store.subscribe(() => this.update()); } onMessage(message) { @@ -27,6 +29,18 @@ export default class Common { } } + update() { + let enabled = this.store.getState().addon.enabled; + if (enabled !== this.prevEnabled) { + this.prevEnabled = enabled; + + browser.runtime.sendMessage({ + type: messages.ADDON_ENABLED_RESPONSE, + enabled, + }); + } + } + reloadSettings() { browser.runtime.sendMessage({ type: messages.SETTINGS_QUERY, diff --git a/src/content/components/top-content/index.js b/src/content/components/top-content/index.js index cf21ec4..cb9e160 100644 --- a/src/content/components/top-content/index.js +++ b/src/content/components/top-content/index.js @@ -48,11 +48,18 @@ export default class TopContent { } onMessage(message) { + let addonState = this.store.getState().addon; + switch (message.type) { case messages.CONSOLE_UNFOCUS: this.win.focus(); consoleFrames.blur(window.document); return Promise.resolve(); + case messages.ADDON_ENABLED_QUERY: + return Promise.resolve({ + type: messages.ADDON_ENABLED_RESPONSE, + enabled: addonState.enabled, + }); } } } diff --git a/src/shared/messages.js b/src/shared/messages.js index a404658..2a286bf 100644 --- a/src/shared/messages.js +++ b/src/shared/messages.js @@ -48,6 +48,9 @@ export default { FIND_GET_KEYWORD: 'find.get.keyword', FIND_SET_KEYWORD: 'find.set.keyword', + ADDON_ENABLED_QUERY: 'addon.enabled.query', + ADDON_ENABLED_RESPONSE: 'addon.enabled.response', + OPEN_URL: 'open.url', SETTINGS_RELOAD: 'settings.reload', -- cgit v1.2.3