Skip to content

Commit

Permalink
feat(suite-desktop): open system settings using desktopApi
Browse files Browse the repository at this point in the history
  • Loading branch information
szymonlesisz committed Feb 14, 2025
1 parent 7413e2c commit 1077075
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 0 deletions.
2 changes: 2 additions & 0 deletions packages/suite-desktop-api/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ export interface InvokeChannels {
'connect-popup/ready': () => void;
'connect-popup/response': (response: ConnectPopupResponse) => void;
'system/get-system-information': () => InvokeResult<GetSystemInformationResponse>;
'system/open-settings': (settings: 'bluetooth') => InvokeResult;
}

type DesktopApiListener = ListenerMethod<RendererChannels>;
Expand Down Expand Up @@ -175,4 +176,5 @@ export interface DesktopApi {
connectPopupResponse: DesktopApiInvoke<'connect-popup/response'>;
//system
getSystemInformation: DesktopApiInvoke<'system/get-system-information'>;
openSystemSettings: DesktopApiInvoke<'system/open-settings'>;
}
1 change: 1 addition & 0 deletions packages/suite-desktop-api/src/factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,5 +183,6 @@ export const factory = <R extends StrictIpcRenderer<any, IpcRendererEvent>>(
connectPopupResponse: response => ipcRenderer.invoke('connect-popup/response', response),

getSystemInformation: () => ipcRenderer.invoke('system/get-system-information'),
openSystemSettings: settings => ipcRenderer.invoke('system/open-settings', settings),
};
};
2 changes: 2 additions & 0 deletions packages/suite-desktop-core/src/modules/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import * as requestInterceptor from './request-interceptor';
import * as shortcuts from './shortcuts';
import * as store from './store';
import * as systemInformation from './system-information';
import * as systemSettings from './system-settings';
import * as theme from './theme';
import * as tray from './tray';
import * as trezorConnect from './trezor-connect';
Expand Down Expand Up @@ -63,6 +64,7 @@ const MODULES: Module[] = [
autoStart,
bridge,
systemInformation,
systemSettings,
// Modules used only in dev/prod mode
...(isDevEnv ? [] : [csp, fileProtocol]),
];
Expand Down
55 changes: 55 additions & 0 deletions packages/suite-desktop-core/src/modules/system-settings.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import { exec } from 'child_process';

import { isLinux, isMacOs, isWindows } from '@trezor/env-utils';

import { ipcMain } from '../typed-electron';

import type { ModuleInit } from './index';

export const SERVICE_NAME = 'system-settings';

const openSettings = (cmd: string, env?: Record<string, string>) =>
new Promise<any>(resolve => {
exec(cmd, { env: { ...process.env, ...env } }, error => {
if (error) {
resolve({ success: false, error } as const);
} else {
resolve({ success: true } as const);
}
});
});

const openBluetoothSettings = () => {
if (isLinux()) {
// https://github.com/electron/electron/blob/ab2a4fd836d539194bc5cde5f0d665eddeb6a134/docs/api/environment-variables.md?plain=1#L190
// Electron modifies the value of XDG_CURRENT_DESKTOP
const xdg = process.env.ORIGINAL_XDG_CURRENT_DESKTOP || process.env.XDG_CURRENT_DESKTOP;
if (xdg?.includes('GNOME')) {
return openSettings('gnome-control-center bluetooth', {
XDG_CURRENT_DESKTOP: xdg,
});
} else if (xdg?.includes('KDE')) {
return openSettings('systemsettings5', {
XDG_CURRENT_DESKTOP: xdg,
});
}
}
if (isMacOs()) {
return openSettings('open "x-apple.systempreferences:com.apple.Bluetooth"');
}
if (isWindows()) {
return openSettings('start ms-settings:bluetooth');
}

return { success: false, error: 'Unsupported os' };
};

export const init: ModuleInit = () => {
ipcMain.handle('system/open-settings', (_, settings) => {
if (settings === 'bluetooth') {
return openBluetoothSettings();
}

return { success: false, error: `Unknown settings: ${settings}` };
});
};

0 comments on commit 1077075

Please sign in to comment.