Skip to content

Commit

Permalink
Make the Touch Bar respect private mode (#1138)
Browse files Browse the repository at this point in the history
  • Loading branch information
jyoo980 authored and sindresorhus committed Oct 31, 2019
1 parent 2b2be20 commit ea8015e
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 11 deletions.
15 changes: 12 additions & 3 deletions source/browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -283,8 +283,17 @@ function setDarkMode(): void {
updateVibrancy();
}

function setPrivateMode(): void {
async function setPrivateMode(): Promise<void> {
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 {
Expand Down Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion source/browser/conversation-list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ async function createConversation(el: HTMLElement): Promise<Conversation> {
return conversation as Conversation;
}

async function createConversationList(): Promise<Conversation[]> {
export async function createConversationList(): Promise<Conversation[]> {
const list = await elementReady<HTMLElement>(selectors.conversationList, {
stopOnDomReady: false
});
Expand Down
3 changes: 3 additions & 0 deletions source/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import * as path from 'path';

export const caprineIconPath = path.join(__dirname, '..', 'static', 'Icon.png');
3 changes: 2 additions & 1 deletion source/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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'),
Expand Down
3 changes: 2 additions & 1 deletion source/menu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Menu> {
const newConversationItem: MenuItemConstructorOptions = {
Expand Down Expand Up @@ -571,7 +572,7 @@ ${debugInfo()}`;
type: 'separator'
},
aboutMenuItem({
icon: path.join(__dirname, '..', 'static', 'Icon.png'),
icon: caprineIconPath,
text: 'Created by Sindre Sorhus'
})
);
Expand Down
25 changes: 20 additions & 5 deletions source/touch-bar.ts
Original file line number Diff line number Diff line change
@@ -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',
Expand All @@ -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]);
});

0 comments on commit ea8015e

Please sign in to comment.