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 dark mode not being set on startup #1569

Merged
merged 3 commits into from
Mar 8, 2021
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
32 changes: 26 additions & 6 deletions source/browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@ async function toggleSounds({isNewDesign, checked}: IToggleSounds): Promise<void
}

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

Expand All @@ -333,7 +333,7 @@ ipc.answerMain('toggle-mute-notifications', async ({isNewDesign, defaultStatus}:
}

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

return !isNewDesign && !notificationCheckbox.checked;
Expand Down Expand Up @@ -383,6 +383,24 @@ function setDarkMode(): void {
updateVibrancy();
}

async function observeDarkMode(): Promise<void> {
const observer = new MutationObserver((records: MutationRecord[]) => {
// Find records that had class attribute changed
const classRecords = records.filter(record => record.type === 'attributes' && record.attributeName === 'class');
// Check if dark mode classes exists
const isDark = classRecords.map(record => {
const {classList} = (record.target as HTMLElement);
return classList.contains('dark-mode') && classList.contains('__fb-dark-mode');
}).includes(true);
// If config and class list don't match, update class list
if (api.nativeTheme.shouldUseDarkColors !== isDark) {
setDarkMode();
}
});

observer.observe(document.documentElement, {attributes: true, attributeFilter: ['class']});
}

function setPrivateMode(isNewDesign: boolean): void {
document.documentElement.classList.toggle('private-mode', config.get('privateMode'));

Expand Down Expand Up @@ -437,7 +455,7 @@ async function updateDoNotDisturb(isNewDesign: boolean): Promise<void> {
}

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

Expand Down Expand Up @@ -662,10 +680,10 @@ function isPreferencesOpen(isNewDesign: boolean): boolean {
Boolean(document.querySelector<HTMLElement>('._3quh._30yy._2t_._5ixy'));
}

function closePreferences(isNewDesign: boolean): void {
async function closePreferences(isNewDesign: boolean): Promise<void> {
if (isNewDesign) {
const closeButton = document.querySelector<HTMLElement>('[aria-label=Preferences] [aria-label=Close]')!;
closeButton.click();
const closeButton = await elementReady<HTMLElement>('[aria-label=Preferences] [aria-label=Close]', {stopOnDomReady: false});
closeButton?.click();

// Wait for the preferences window to be closed, then remove the class from the document
const preferencesOverlayObserver = new MutationObserver(records => {
Expand Down Expand Up @@ -768,6 +786,8 @@ document.addEventListener('DOMContentLoaded', async () => {

// Activate Dark Mode if it was set before quitting
setDarkMode();
// Observe for dark mode changes
observeDarkMode();

// Activate Private Mode if it was set before quitting
setPrivateMode(newDesign);
Expand Down
2 changes: 1 addition & 1 deletion source/menu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ Press Command/Ctrl+R in Caprine to see your changes.
type: 'checkbox',
checked: config.get('notificationsMuted'),
click() {
sendAction('toggle-mute-notifications');
sendAction('toggle-mute-notifications', {isNewDesign});
}
},
{
Expand Down