Skip to content

Commit

Permalink
Fix the emoji picker crash (#1255)
Browse files Browse the repository at this point in the history
  • Loading branch information
whitecrownclown authored Feb 4, 2020
1 parent c3b10c0 commit 7236196
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 21 deletions.
13 changes: 4 additions & 9 deletions source/browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import elementReady = require('element-ready');
import selectors from './browser/selectors';
import config from './config';
import {toggleVideoAutoplay} from './autoplay';
import {createConversationList} from './browser/conversation-list';
import {sendConversationList} from './browser/conversation-list';

const selectedConversationSelector = '._5l-3._1ht1._1ht2';
const preferencesSelector = '._10._4ebx.uiLayer._4-hy';
Expand Down Expand Up @@ -277,16 +277,11 @@ function setDarkMode(): void {
updateVibrancy();
}

async function setPrivateMode(): Promise<void> {
function setPrivateMode(): 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);
}
sendConversationList();
}
}

Expand Down Expand Up @@ -570,7 +565,7 @@ document.addEventListener('DOMContentLoaded', async () => {
setDarkMode();

// Activate Private Mode if it was set before quitting
await setPrivateMode();
setPrivateMode();

// Configure do not disturb
if (is.macos) {
Expand Down
19 changes: 7 additions & 12 deletions source/browser/conversation-list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,12 @@ function drawIcon(size: number, img?: HTMLImageElement): HTMLCanvasElement {
canvas.height = size + padding.top + padding.bottom;

const ctx = canvas.getContext('2d')!;
ctx.save();
ctx.beginPath();
ctx.arc((size / 2) + padding.left, (size / 2) + padding.top, (size / 2), 0, Math.PI * 2, true);
ctx.closePath();
ctx.clip();

ctx.drawImage(img, padding.left, padding.top, size, size);
ctx.restore();
} else {
canvas.width = 0;
canvas.height = 0;
Expand All @@ -46,7 +44,7 @@ async function urlToCanvas(url: string, size: number): Promise<HTMLCanvasElement
return new Promise(resolve => {
const img = new Image();

img.crossOrigin = 'anonymous';
img.setAttribute('crossorigin', 'anonymous');

img.addEventListener('load', () => {
resolve(drawIcon(size, img));
Expand All @@ -72,6 +70,7 @@ async function createIcons(el: HTMLElement, url: string): Promise<void> {
ctx.fillStyle = '#f42020';
ctx.beginPath();
ctx.ellipse(canvas.width - markerSize, markerSize, markerSize, markerSize, 0, 0, 2 * Math.PI);
ctx.closePath();
ctx.fill();

el.setAttribute(icon.unread, canvas.toDataURL());
Expand Down Expand Up @@ -132,7 +131,7 @@ async function createConversation(el: HTMLElement): Promise<Conversation> {
return conversation as Conversation;
}

export async function createConversationList(): Promise<Conversation[]> {
async function createConversationList(): Promise<Conversation[]> {
const list = await elementReady<HTMLElement>(selectors.conversationList, {
stopOnDomReady: false
});
Expand All @@ -144,15 +143,13 @@ export async function createConversationList(): Promise<Conversation[]> {

const items: HTMLElement[] = [...list.children] as HTMLElement[];

const conversations: Conversation[] = await Promise.all(
items.map(async (element: HTMLElement): Promise<Conversation> => createConversation(element))
);
const conversations: Conversation[] = await Promise.all(items.map(createConversation));

return conversations;
}

async function sendConversationList(): Promise<void> {
if (is.macos && config.get('privateMode')) {
export async function sendConversationList(): Promise<void> {
if (config.get('privateMode')) {
ipc.send('hide-touchbar-labels');
} else {
const conversationsToRender: Conversation[] = await createConversationList();
Expand All @@ -163,9 +160,7 @@ async function sendConversationList(): Promise<void> {
window.addEventListener('load', async () => {
const sidebar = document.querySelector<HTMLElement>('[role=navigation]');

if (sidebar) {
await sendConversationList();

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

conversationListObserver.observe(sidebar, {
Expand Down

0 comments on commit 7236196

Please sign in to comment.