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

Respect "Do Not Disturb" on macOS #760

Merged
merged 11 commits into from
Oct 8, 2019
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
81 changes: 81 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"publish-snap": "del dist && tsc && electron-builder --linux && snapcraft push --release=stable dist/*.snap"
},
"dependencies": {
"@sindresorhus/do-not-disturb": "^1.1.0",
"electron-context-menu": "^0.15.0",
"electron-debug": "^3.0.1",
"electron-dl": "^1.14.0",
Expand Down
1 change: 1 addition & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ Caprine is an unofficial and privacy-focused Facebook Messenger app with many us
- Silent auto-updates
- Custom text size
- Emoji style setting
- Respects Do Not Disturb\*

\*macOS only

Expand Down
55 changes: 48 additions & 7 deletions source/browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import './browser/conversation-list'; // eslint-disable-line import/no-unassigne

const selectedConversationSelector = '._5l-3._1ht1._1ht2';
const preferencesSelector = '._10._4ebx.uiLayer._4-hy';
const messengerSoundsSelector = `${preferencesSelector} ._374d ._6bkz`;

async function withMenu(
menuButtonElement: HTMLElement,
Expand Down Expand Up @@ -55,7 +56,9 @@ function selectMenuItem(itemNumber: number): void {
`.uiLayer:not(.hidden_elem) ._54nq._2i-c._558b._2n_z li:nth-child(${itemNumber}) a`
)!;

selector.click();
if (selector) {
selector.click();
}
}

async function selectOtherListViews(itemNumber: number): Promise<void> {
Expand Down Expand Up @@ -183,10 +186,8 @@ function setSidebarVisibility(): void {
ipc.send('set-sidebar-visibility');
}

ipc.on('toggle-mute-notifications', async (_event: ElectronEvent, defaultStatus: boolean) => {
const preferencesAreOpen = isPreferencesOpen();

if (!preferencesAreOpen) {
async function openHiddenPreferences(): Promise<boolean> {
if (!isPreferencesOpen()) {
const style = document.createElement('style');
// Hide both the backdrop and the preferences dialog
style.textContent = `${preferencesSelector} ._3ixn, ${preferencesSelector} ._59s7 { opacity: 0 !important }`;
Expand All @@ -196,7 +197,31 @@ ipc.on('toggle-mute-notifications', async (_event: ElectronEvent, defaultStatus:

// Will clean up itself after the preferences are closed
document.querySelector<HTMLElement>(preferencesSelector)!.append(style);

return true;
}

return false;
}

ipc.on(
'toggle-sounds',
async (_event: ElectronEvent, checked: boolean): Promise<void> => {
const shouldClosePreferences = await openHiddenPreferences();

const soundsCheckbox = document.querySelector<HTMLInputElement>(messengerSoundsSelector)!;
if (typeof checked === 'undefined' || checked !== soundsCheckbox.checked) {
soundsCheckbox.click();
}

if (shouldClosePreferences) {
closePreferences();
}
}
);

ipc.on('toggle-mute-notifications', async (_event: ElectronEvent, defaultStatus: boolean) => {
const shouldClosePreferences = await openHiddenPreferences();

const notificationCheckbox = document.querySelector<HTMLInputElement>(
selectors.notificationCheckbox
Expand All @@ -213,7 +238,7 @@ ipc.on('toggle-mute-notifications', async (_event: ElectronEvent, defaultStatus:

ipc.send('mute-notifications-toggled', !notificationCheckbox.checked);

if (!preferencesAreOpen) {
if (shouldClosePreferences) {
closePreferences();
}
});
Expand Down Expand Up @@ -274,6 +299,17 @@ function updateVibrancy(): void {
ipc.send('set-vibrancy');
}

async function updateDoNotDisturb(): Promise<void> {
const shouldClosePreferences = await openHiddenPreferences();
const soundsCheckbox = document.querySelector<HTMLInputElement>(messengerSoundsSelector)!;

if (shouldClosePreferences) {
closePreferences();
}

ipc.send('update-dnd-mode', soundsCheckbox.checked);
}

function renderOverlayIcon(messageCount: number): HTMLCanvasElement {
const canvas = document.createElement('canvas');
canvas.height = 128;
Expand Down Expand Up @@ -482,7 +518,7 @@ function insertionListener(event: AnimationEvent): void {
document.addEventListener('animationstart', insertionListener, false);

// Inject a global style node to maintain custom appearance after conversation change or startup
document.addEventListener('DOMContentLoaded', () => {
document.addEventListener('DOMContentLoaded', async () => {
const style = document.createElement('style');
style.id = 'zoomFactor';
document.body.append(style);
Expand All @@ -503,6 +539,11 @@ document.addEventListener('DOMContentLoaded', () => {
// Activate Private Mode if it was set before quitting
setPrivateMode();

// Configure do not disturb
if (is.macos) {
await updateDoNotDisturb();
}

// Prevent flash of white on startup when in dark mode
// TODO: find a CSS-only solution
if (!is.macos && config.get('darkMode')) {
Expand Down
1 change: 1 addition & 0 deletions source/do-not-disturb.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
declare module '@sindresorhus/do-not-disturb';
24 changes: 22 additions & 2 deletions source/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import electronLocalshortcut = require('electron-localshortcut');
import electronDebug = require('electron-debug');
import {is, darkMode} from 'electron-util';
import {bestFacebookLocaleFor} from 'facebook-locales';
import doNotDisturb = require('@sindresorhus/do-not-disturb');
import updateAppMenu from './menu';
import config from './config';
import tray from './tray';
Expand Down Expand Up @@ -69,6 +70,7 @@ let mainWindow: BrowserWindow;
let isQuitting = false;
let prevMessageCount = 0;
let dockMenu: Menu;
let isDNDEnabled = false;

if (!app.requestSingleInstanceLock()) {
app.quit();
Expand Down Expand Up @@ -97,11 +99,16 @@ function updateBadge(conversations: Conversation[]): void {
const messageCount = getMessageCount(conversations);

if (is.macos || is.linux) {
if (config.get('showUnreadBadge')) {
if (config.get('showUnreadBadge') && !isDNDEnabled) {
app.setBadgeCount(messageCount);
}

if (is.macos && config.get('bounceDockOnMessage') && prevMessageCount !== messageCount) {
if (
is.macos &&
!isDNDEnabled &&
config.get('bounceDockOnMessage') &&
prevMessageCount !== messageCount
) {
app.dock.bounce('informational');
prevMessageCount = messageCount;
}
Expand Down Expand Up @@ -395,6 +402,19 @@ function createMainWindow(): BrowserWindow {
mainWindow.show();
}

if (is.macos) {
ipcMain.on('update-dnd-mode', async (_event: ElectronEvent, initialSoundsValue) => {
doNotDisturb.on('change', (doNotDisturb: boolean) => {
isDNDEnabled = doNotDisturb;
webContents.send('toggle-sounds', isDNDEnabled ? false : initialSoundsValue);
});

isDNDEnabled = await doNotDisturb.isEnabled();

webContents.send('toggle-sounds', isDNDEnabled ? false : initialSoundsValue);
});
}

webContents.send('toggle-mute-notifications', config.get('notificationsMuted'));
webContents.send('toggle-message-buttons', config.get('showMessageButtons'));

Expand Down