From ea8015ec99a30cb283dc641433d95991f4a9b2be Mon Sep 17 00:00:00 2001 From: James Yoo Date: Thu, 31 Oct 2019 01:06:49 -0700 Subject: [PATCH] Make the Touch Bar respect private mode (#1138) --- source/browser.ts | 15 ++++++++++++--- source/browser/conversation-list.ts | 2 +- source/constants.ts | 3 +++ source/index.ts | 3 ++- source/menu.ts | 3 ++- source/touch-bar.ts | 25 ++++++++++++++++++++----- 6 files changed, 40 insertions(+), 11 deletions(-) create mode 100644 source/constants.ts diff --git a/source/browser.ts b/source/browser.ts index fc6d968ed..9ca2e323d 100644 --- a/source/browser.ts +++ b/source/browser.ts @@ -6,7 +6,7 @@ import selectors from './browser/selectors'; import config from './config'; import {toggleVideoAutoplay} from './autoplay'; -import './browser/conversation-list'; // eslint-disable-line import/no-unassigned-import +import {createConversationList} from './browser/conversation-list'; const selectedConversationSelector = '._5l-3._1ht1._1ht2'; const preferencesSelector = '._10._4ebx.uiLayer._4-hy'; @@ -283,8 +283,17 @@ function setDarkMode(): void { updateVibrancy(); } -function setPrivateMode(): void { +async function setPrivateMode(): Promise { document.documentElement.classList.toggle('private-mode', config.get('privateMode')); + + if (is.macos) { + if (config.get('privateMode')) { + ipc.send('hide-touchbar-labels'); + } else { + const conversationsToRender: Conversation[] = await createConversationList(); + ipc.send('conversations', conversationsToRender); + } + } } function updateVibrancy(): void { @@ -544,7 +553,7 @@ document.addEventListener('DOMContentLoaded', async () => { setDarkMode(); // Activate Private Mode if it was set before quitting - setPrivateMode(); + await setPrivateMode(); // Configure do not disturb if (is.macos) { diff --git a/source/browser/conversation-list.ts b/source/browser/conversation-list.ts index d71d8d530..adc74d5b1 100644 --- a/source/browser/conversation-list.ts +++ b/source/browser/conversation-list.ts @@ -130,7 +130,7 @@ async function createConversation(el: HTMLElement): Promise { return conversation as Conversation; } -async function createConversationList(): Promise { +export async function createConversationList(): Promise { const list = await elementReady(selectors.conversationList, { stopOnDomReady: false }); diff --git a/source/constants.ts b/source/constants.ts new file mode 100644 index 000000000..173252610 --- /dev/null +++ b/source/constants.ts @@ -0,0 +1,3 @@ +import * as path from 'path'; + +export const caprineIconPath = path.join(__dirname, '..', 'static', 'Icon.png'); diff --git a/source/index.ts b/source/index.ts index 73d6c03b7..99f376b99 100644 --- a/source/index.ts +++ b/source/index.ts @@ -33,6 +33,7 @@ import {process as processEmojiUrl} from './emoji'; import ensureOnline from './ensure-online'; import './touch-bar'; // eslint-disable-line import/no-unassigned-import import {setUpMenuBarMode} from './menu-bar-mode'; +import {caprineIconPath} from './constants'; ipcMain.setMaxListeners(100); @@ -258,7 +259,7 @@ function createMainWindow(): BrowserWindow { y: lastWindowState.y, width: lastWindowState.width, height: lastWindowState.height, - icon: is.linux ? path.join(__dirname, '..', 'static', 'Icon.png') : undefined, + icon: is.linux ? caprineIconPath : undefined, minWidth: 400, minHeight: 200, alwaysOnTop: config.get('alwaysOnTop'), diff --git a/source/menu.ts b/source/menu.ts index 03f6f3c5d..ef6ca5a94 100644 --- a/source/menu.ts +++ b/source/menu.ts @@ -13,6 +13,7 @@ import config from './config'; import {sendAction, showRestartDialog} from './util'; import {generateSubmenu as generateEmojiSubmenu} from './emoji'; import {toggleMenuBarMode} from './menu-bar-mode'; +import {caprineIconPath} from './constants'; export default async function updateMenu(): Promise { const newConversationItem: MenuItemConstructorOptions = { @@ -571,7 +572,7 @@ ${debugInfo()}`; type: 'separator' }, aboutMenuItem({ - icon: path.join(__dirname, '..', 'static', 'Icon.png'), + icon: caprineIconPath, text: 'Created by Sindre Sorhus' }) ); diff --git a/source/touch-bar.ts b/source/touch-bar.ts index 28e146a6b..aa064d7af 100644 --- a/source/touch-bar.ts +++ b/source/touch-bar.ts @@ -1,12 +1,20 @@ import {TouchBar, ipcMain as ipc, nativeImage, Event as ElectronEvent} from 'electron'; import {sendAction, getWindow} from './util'; +import {caprineIconPath} from './constants'; const {TouchBarButton} = TouchBar; +const MAX_VISIBLE_LENGTH = 25; + +function setTouchBar(items: Electron.TouchBarButton[]): void { + const touchBar = new TouchBar({items}); + const win = getWindow(); + win.setTouchBar(touchBar); +} ipc.on('conversations', (_event: ElectronEvent, conversations: Conversation[]) => { const items = conversations.map(({label, selected, icon}, index: number) => { return new TouchBarButton({ - label: label.length > 25 ? label.slice(0, 25) + '…' : label, + label: label.length > MAX_VISIBLE_LENGTH ? label.slice(0, MAX_VISIBLE_LENGTH) + '…' : label, backgroundColor: selected ? '#0084ff' : undefined, icon: nativeImage.createFromDataURL(icon), iconPosition: 'left', @@ -15,9 +23,16 @@ ipc.on('conversations', (_event: ElectronEvent, conversations: Conversation[]) = } }); }); + setTouchBar(items); +}); - const touchBar = new TouchBar({items}); - const win = getWindow(); - - win.setTouchBar(touchBar); +ipc.on('hide-touchbar-labels', (_event: ElectronEvent) => { + const privateModeLabel = new TouchBarButton({ + label: 'Private mode enabled', + backgroundColor: undefined, + icon: nativeImage.createFromPath(caprineIconPath), + iconPosition: 'left', + click: undefined + }); + setTouchBar([privateModeLabel]); });