From 129490f4b4d5d555edec0dc4b8591e9b5b8e3f1a Mon Sep 17 00:00:00 2001 From: rhysd Date: Wed, 19 Apr 2017 17:07:31 +0900 Subject: [PATCH] add shortcuts to switch to next/previous account --- README.md | 59 ++++++++++++++++++++++++++++++++++++---- main/account_switcher.ts | 42 +++++++++++++++++++++++++++- renderer/index.ts | 6 ++++ renderer/ipc.ts | 7 ++++- typings/ipc.d.ts | 8 ++++-- 5 files changed, 112 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 5010773..306ab6a 100644 --- a/README.md +++ b/README.md @@ -98,12 +98,14 @@ You need to write up this config at first. Object whose key is a key sequence and whose value is an action name. -| Action Name | Description | Default Key | -|--------------------|---------------------------------|-------------| -| `scroll-down` | Scroll down window | `j` | -| `scroll-up` | Scroll up window | `k` | -| `scroll-top` | Scroll up to top of window | `i` | +| Action Name | Description | Default Key | +|-----------------|---------------------------------|-------------| +| `scroll-down` | Scroll down window | `j` | +| `scroll-up` | Scroll up window | `k` | +| `scroll-top` | Scroll up to top of window | `i` | | `scroll-bottom` | Scroll down to bottom of window | `m` | +| `next-account` | Switch to next account | N/A | +| `prev-account` | Switch to previous account | N/A | If an action name starts with `/`, it will navigate to the path. For example, if you set `"/web/timelines/home"` to some key shortcut and you input the key, @@ -111,6 +113,51 @@ browser will navigate page to `https://{your host}/web/timelines/home`. By default, some key shortcuts for tab items are set in addition to above table. +
+ Example of configuration +

+{
+  "hot_key": "F8",
+  "icon_color": "black",
+  "always_on_top": false,
+  "normal_window": false,
+  "zoom_factor": 0.9,
+  "accounts": [
+    {
+      "name": "Linda_pp",
+      "host": "mstdn.jp",
+      "default_page": "/web/timelines/home"
+    },
+    {
+      "name": "inudog",
+      "host": "mastodon.social",
+      "default_page": "/web/timelines/home"
+    }
+  ],
+  "keymaps": {
+    "1": "/web/statuses/new",
+    "2": "/web/timelines/home",
+    "3": "/web/notifications",
+    "4": "/web/timelines/public/local",
+    "5": "/web/timelines/public",
+    "6": "/web/getting-started",
+    "ctrl+1": "/web/statuses/new",
+    "ctrl+2": "/web/timelines/home",
+    "ctrl+3": "/web/notifications",
+    "ctrl+4": "/web/timelines/public/local",
+    "ctrl+5": "/web/timelines/public",
+    "ctrl+6": "/web/getting-started",
+    "j": "scroll-down",
+    "k": "scroll-up",
+    "i": "scroll-top",
+    "m": "scroll-bottom",
+    "n": "next-account",
+    "p": "prev-account"
+  }
+}
+
+
+ ## Multi account If you set multiple accounts to `accounts` array in `config.json`, `Accounts` menu item will appear in application menu. @@ -118,7 +165,7 @@ If you set multiple accounts to `accounts` array in `config.json`, `Accounts` me ![multi account menu item](https://github.com/rhysd/ss/blob/master/Mstdn/multi-account.png?raw=true) It will show the list of your account. Check mark is added for current user. -When you click menu item of non-current user, application window will be rectreated and switch page to the account. +When you click menu item of non-current user, application window will be recreated and switch page to the account. [Mastodon]: https://github.com/tootsuite/mastodon [npm]: https://www.npmjs.com/package/mstdn diff --git a/main/account_switcher.ts b/main/account_switcher.ts index b5d5d23..6620609 100644 --- a/main/account_switcher.ts +++ b/main/account_switcher.ts @@ -1,5 +1,5 @@ import {EventEmitter} from 'events'; -import {Menu, MenuItem} from 'electron'; +import {Menu, MenuItem, ipcMain as ipc} from 'electron'; import log from './log'; import {Account} from './config'; @@ -49,6 +49,9 @@ export default class AccountSwitcher extends EventEmitter { menu.insert(menu.items.length - 1, item); Menu.setApplicationMenu(menu); } + + ipc.on('mstdn:next-account' as IpcChannelFromRenderer, this.switchToNext); + ipc.on('mstdn:prev-account' as IpcChannelFromRenderer, this.switchToPrev); } switchTo(account: Account) { @@ -60,4 +63,41 @@ export default class AccountSwitcher extends EventEmitter { this.emit('switch', account, this.current); this.current = account; } + + getAccountIndex(account: Account) { + for (let i = 0; i < this.accounts.length; ++i) { + const a = this.accounts[i]; + if (a.name === account.name && a.host === account.host) { + return i; + } + } + return -1; + } + + switchToNext = () => { + if (this.accounts.length <= 1) { + log.debug('Skip switching account: Only one account is registered'); + return; + } + let idx = this.getAccountIndex(this.current) + 1; + if (idx >= this.accounts.length) { + idx = 0; + } + log.debug('Switch to next account'); + this.switchTo(this.accounts[idx]); + } + + switchToPrev = () => { + if (this.accounts.length <= 1) { + log.debug('Skip switching account: Only one account is registered'); + return; + } + let idx = this.getAccountIndex(this.current); + if (idx <= 0) { + idx = this.accounts.length; + } + --idx; + log.debug('Switch to prev account'); + this.switchTo(this.accounts[idx]); + } } diff --git a/renderer/index.ts b/renderer/index.ts index 0fa9ba0..45aa95c 100644 --- a/renderer/index.ts +++ b/renderer/index.ts @@ -42,6 +42,12 @@ const ShortcutActions = { 'scroll-up': () => { scrollable().scrollTop -= window.innerHeight / 3; }, + 'next-account': () => { + Ipc.send('mstdn:next-account'); + }, + 'prev-account': () => { + Ipc.send('mstdn:prev-account'); + }, } as {[action: string]: () => void}; function setupKeybinds(keybinds: {[key: string]: string}, host: string) { diff --git a/renderer/ipc.ts b/renderer/ipc.ts index fa8b050..a36fbc8 100644 --- a/renderer/ipc.ts +++ b/renderer/ipc.ts @@ -3,9 +3,14 @@ import r from './require'; const electron = r('electron'); const ipc = electron.ipcRenderer; -export function on(channel: IpcChannel, callback: (...args: any[]) => void) { +export function on(channel: IpcChannelFromMain, callback: (...args: any[]) => void) { ipc.on(channel, (_, ...args: any[]) => { log.info('IPC: Received from:', channel, args); callback(...args); }); } + +export function send(channel: IpcChannelFromRenderer, ...args: any[]) { + log.info('IPC: Send:', channel, args); + ipc.send(channel, ...args); +} diff --git a/typings/ipc.d.ts b/typings/ipc.d.ts index 2312831..ed679b6 100644 --- a/typings/ipc.d.ts +++ b/typings/ipc.d.ts @@ -1,4 +1,8 @@ -type IpcChannel +type IpcChannelFromMain = 'mstdn:config' - | 'mstdn:change-account' +; + +type IpcChannelFromRenderer + = 'mstdn:next-account' + | 'mstdn:prev-account' ;