Skip to content

Commit

Permalink
Fix disabling hardware acceleration
Browse files Browse the repository at this point in the history
  • Loading branch information
imolorhe committed Nov 16, 2024
1 parent cac29ef commit 78eb35d
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 16 deletions.
2 changes: 1 addition & 1 deletion packages/altair-core/src/plugin/v3/parent-engine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ export class PluginParentEngine {
const htmlClasses = Array.from(document.documentElement.classList);
// Get the styles that are applicable to the current theme of the page
// Doesn't work crossorigin cases. e.g. when loading from CDN. Fallback to theme instead.
const styles = getCssStyles(htmlClasses);
const styles = getCssStyles(htmlClasses).filter((s) => s.trim().length > 0);

return { styleUrls, styles, htmlClasses, theme: this.opts?.theme };
});
Expand Down
26 changes: 17 additions & 9 deletions packages/altair-electron/src/app/menu.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { app, Menu, MenuItemConstructorOptions, shell } from 'electron';
import { ActionManager } from './actions';
import { getStartupOption, setStartupOption } from '../utils/startup';
import { restartApp } from '../utils';

export class MenuManager {
constructor(private actionManager: ActionManager) {
Expand Down Expand Up @@ -104,15 +106,21 @@ export class MenuManager {
submenu: [
{ role: 'minimize' },
{ role: 'close' },
{
label: 'Disable hardware acceleration (beta)',
click: () => {
app.commandLine.appendSwitch('ignore-gpu-blacklist');
app.commandLine.appendSwitch('disable-gpu');
app.commandLine.appendSwitch('disable-gpu-compositing');
app.disableHardwareAcceleration();
},
},
getStartupOption('DISABLE_HARDWARE_ACCELERATION')
? {
label: 'Enable hardware acceleration (beta)',
click: () => {
setStartupOption('DISABLE_HARDWARE_ACCELERATION', false);
restartApp(app);
},
}
: {
label: 'Disable hardware acceleration (beta)',
click: () => {
setStartupOption('DISABLE_HARDWARE_ACCELERATION', true);
restartApp(app);
},
},
...(isMac
? ([
{ role: 'minimize' },
Expand Down
2 changes: 2 additions & 0 deletions packages/altair-electron/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ import unhandled from 'electron-unhandled';
import * as Sentry from '@sentry/electron';
import { ElectronApp } from './app';
import { app } from 'electron';
import { configureAppOnStartup } from './utils/startup';

Sentry.init({
dsn: 'https://1b08762f991476e3115e1ab7d12e6682@o4506180788879360.ingest.sentry.io/4506198594813952',
release: app.getVersion(),
});
configureAppOnStartup(app);
new ElectronApp();
unhandled({ showDialog: false });
4 changes: 3 additions & 1 deletion packages/altair-electron/src/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,6 @@ export class InMemoryStore {
}
}

export class PersistentStore extends ElectronStore {}
export class PersistentStore<
T extends Record<string, any> = Record<string, unknown>,
> extends ElectronStore<T> {}
15 changes: 10 additions & 5 deletions packages/altair-electron/src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import { readdir } from 'fs';
import fs from 'fs';
import { join } from 'path';
import { ipcMain } from 'electron';
import { App, ipcMain } from 'electron';
import { ALTAIR_CUSTOM_PROTOCOL } from '@altairgraphql/electron-interop';

export const getDirectoriesInDirectory = (path: string) => {
Expand All @@ -12,16 +12,16 @@ export const getDirectoriesInDirectory = (path: string) => {
? reject(err)
: resolve(
dirents
.filter(dirent => dirent.isDirectory())
.map(dirent => dirent.name)
.filter((dirent) => dirent.isDirectory())
.map((dirent) => dirent.name)
)
);
});
};

export const deleteFolderRecursive = (path: string) => {
if (fs.existsSync(path)) {
fs.readdirSync(path).forEach(file => {
fs.readdirSync(path).forEach((file) => {
const curPath = join(path, file);
if (fs.lstatSync(curPath).isDirectory()) {
// recurse
Expand Down Expand Up @@ -62,5 +62,10 @@ export const handleWithCustomErrors = (
// We don't know the exact position of the URL is in argv. Chromium might inject its own arguments
// into argv. See https://www.electronjs.org/docs/latest/api/app#event-second-instance.
export function findCustomProtocolUrlInArgv(argv: string[]) {
return argv.find(arg => arg.startsWith(`${ALTAIR_CUSTOM_PROTOCOL}://`));
return argv.find((arg) => arg.startsWith(`${ALTAIR_CUSTOM_PROTOCOL}://`));
}

export function restartApp(app: App) {
app.relaunch();
app.exit();
}
32 changes: 32 additions & 0 deletions packages/altair-electron/src/utils/startup.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { App } from 'electron';
import { PersistentStore } from '../store';

interface StartupOptionsStore {
DISABLE_HARDWARE_ACCELERATION: boolean;
}
const startupOptionsStore = new PersistentStore<StartupOptionsStore>({
defaults: {
DISABLE_HARDWARE_ACCELERATION: false,
},
});
function disableHardwareAcceleration(app: App) {
app.commandLine.appendSwitch('ignore-gpu-blacklist');
app.commandLine.appendSwitch('disable-gpu');
app.commandLine.appendSwitch('disable-gpu-compositing');
app.disableHardwareAcceleration();
}
export function getStartupOption<T extends keyof StartupOptionsStore>(option: T) {
return startupOptionsStore.get(option);
}
export function setStartupOption<T extends keyof StartupOptionsStore>(
option: T,
value: StartupOptionsStore[T]
) {
startupOptionsStore.set(option, value);
}

export function configureAppOnStartup(app: App) {
if (startupOptionsStore.get('DISABLE_HARDWARE_ACCELERATION')) {
disableHardwareAcceleration(app);
}
}

0 comments on commit 78eb35d

Please sign in to comment.