Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix badge notification bug when private mode is enabled #1260

Merged
merged 5 commits into from
Feb 5, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 3 additions & 10 deletions source/browser/conversation-list.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
import {ipcRenderer as ipc} from 'electron';
import elementReady = require('element-ready');

import {is} from 'electron-util';
import config from '../config';
import selectors from './selectors';

const icon = {
Expand Down Expand Up @@ -149,18 +146,14 @@ async function createConversationList(): Promise<Conversation[]> {
}

export async function sendConversationList(): Promise<void> {
if (config.get('privateMode')) {
ipc.send('hide-touchbar-labels');
} else {
const conversationsToRender: Conversation[] = await createConversationList();
ipc.send('conversations', conversationsToRender);
}
const conversationsToRender: Conversation[] = await createConversationList();
ipc.send('conversations', conversationsToRender);
}

window.addEventListener('load', async () => {
const sidebar = document.querySelector<HTMLElement>('[role=navigation]');

if (sidebar && is.macos) {
if (sidebar) {
const conversationListObserver = new MutationObserver(sendConversationList);

conversationListObserver.observe(sidebar, {
Expand Down
51 changes: 30 additions & 21 deletions source/touch-bar.ts
Original file line number Diff line number Diff line change
@@ -1,38 +1,47 @@
import {TouchBar, ipcMain as ipc, nativeImage, Event as ElectronEvent} from 'electron';
import config from './config';
import {sendAction, getWindow} from './util';
import {caprineIconPath} from './constants';

const {TouchBarButton} = TouchBar;
const MAX_VISIBLE_LENGTH = 25;
const privateModeTouchBarLabel: Electron.TouchBarButton = new TouchBarButton({
label: 'Private mode enabled',
icon: nativeImage.createFromPath(caprineIconPath),
iconPosition: 'left'
});

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 > MAX_VISIBLE_LENGTH ? label.slice(0, MAX_VISIBLE_LENGTH) + '…' : label,
backgroundColor: selected ? '#0084ff' : undefined,
icon: nativeImage.createFromDataURL(icon),
iconPosition: 'left',
click: () => {
sendAction('jump-to-conversation', index + 1);
}
});
});
setTouchBar(items);
});
function createLabel(label: string): string {
if (label.length > MAX_VISIBLE_LENGTH) {
// If the label is too long, we'll render a truncated one with "…" appended
return `${label.slice(0, MAX_VISIBLE_LENGTH)}…`;
}

ipc.on('hide-touchbar-labels', (_event: ElectronEvent) => {
const privateModeLabel = new TouchBarButton({
label: 'Private mode enabled',
backgroundColor: undefined,
icon: nativeImage.createFromPath(caprineIconPath),
return label;
}

function createTouchBarButton({label, selected, icon}: Conversation, index: number): Electron.TouchBarButton {
return new TouchBarButton({
label: createLabel(label),
backgroundColor: selected ? '#0084ff' : undefined,
icon: nativeImage.createFromDataURL(icon),
iconPosition: 'left',
click: undefined
click: () => {
sendAction('jump-to-conversation', index + 1);
}
});
setTouchBar([privateModeLabel]);
}

ipc.on('conversations', (_event: ElectronEvent, conversations: Conversation[]) => {
if (config.get('privateMode')) {
setTouchBar([privateModeTouchBarLabel]);
} else {
setTouchBar(conversations.map(createTouchBarButton));
}
});