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

Change Window Theme with Pulsar Theme #545

Merged
merged 4 commits into from
Apr 19, 2024
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
4 changes: 4 additions & 0 deletions src/application-delegate.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ module.exports = class ApplicationDelegate {
return ipcRenderer.send('open', params);
}

setWindowTheme(params) {
return ipcRenderer.send('setWindowTheme', params);
}

pickFolder(callback) {
const responseChannel = 'atom-pick-folder-response';
ipcRenderer.on(responseChannel, function(event, path) {
Expand Down
3 changes: 2 additions & 1 deletion src/atom-environment.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,8 @@ class AtomEnvironment {
config: this.config,
styleManager: this.styles,
notificationManager: this.notifications,
viewRegistry: this.views
viewRegistry: this.views,
applicationDelegate: this.applicationDelegate
});

/** @type {MenuManager} */
Expand Down
5 changes: 5 additions & 0 deletions src/config-schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,11 @@ const configSchema = {
description: 'Add the current tab title to the Pulsar Window title.',
type: 'boolean',
default: true
},
syncWindowThemeWithPulsarTheme: {
description: 'When changing the theme within Pulsar also change the theme of the window on the operating system.',
type: 'boolean',
default: false
}
}
},
Expand Down
11 changes: 10 additions & 1 deletion src/main-process/atom-application.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ const {
dialog,
ipcMain,
shell,
screen
screen,
nativeTheme
} = require('electron');
const { CompositeDisposable, Disposable } = require('event-kit');
const crypto = require('crypto');
Expand Down Expand Up @@ -806,6 +807,14 @@ module.exports = class AtomApplication extends EventEmitter {
})
);

this.disposable.add(
ipcHelpers.on(ipcMain, 'setWindowTheme', (event, options) => {
if (options && typeof options === 'string') {
nativeTheme.themeSource = options;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

}
})
);

// A request from the associated render process to open a set of paths using the standard window location logic.
// Used for application:reopen-project.
this.disposable.add(
Expand Down
22 changes: 21 additions & 1 deletion src/theme-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const { Emitter, CompositeDisposable } = require('event-kit');
const { File } = require('pathwatcher');
const fs = require('fs-plus');
const LessCompileCache = require('./less-compile-cache');
const Color = require('./color');

// Extended: Handles loading and activating available themes.
//
Expand All @@ -16,8 +17,10 @@ module.exports = class ThemeManager {
config,
styleManager,
notificationManager,
viewRegistry
viewRegistry,
applicationDelegate
}) {
this.applicationDelegate = applicationDelegate;
this.packageManager = packageManager;
this.config = config;
this.styleManager = styleManager;
Expand Down Expand Up @@ -372,6 +375,20 @@ On linux there are currently problems with watch sizes. See
return this.styleSheetDisposablesBySourcePath[path];
}

refreshWindowTheme() {
let bgColor = Color.parse(getComputedStyle(document.documentElement).backgroundColor);

let luminosity = 0.2126 * bgColor.red + 0.7152 * bgColor.green + 0.0722 * bgColor.blue;
// ^^ Luminosity per ITU-R BT.709
Comment on lines +381 to +382
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if (luminosity < 40) {
// Considered Dark
this.applicationDelegate.setWindowTheme("dark");
} else {
// Considered Bright
this.applicationDelegate.setWindowTheme("light");
}
}

activateThemes() {
return new Promise(resolve => {
// @config.observe runs the callback once, then on subsequent changes.
Expand All @@ -396,6 +413,9 @@ On linux there are currently problems with watch sizes. See
this.refreshLessCache(); // Update cache again now that @getActiveThemes() is populated
this.loadUserStylesheet();
this.reloadBaseStylesheets();
if (this.config.get("editor.syncWindowThemeWithPulsarTheme")) {
this.refreshWindowTheme();
}
this.initialLoadComplete = true;
this.emitter.emit('did-change-active-themes');
resolve();
Expand Down
Loading