Skip to content

Commit

Permalink
Allow to remove config and save windows only on request
Browse files Browse the repository at this point in the history
  • Loading branch information
albireox committed Dec 9, 2022
1 parent 7f78bc4 commit 278b362
Show file tree
Hide file tree
Showing 11 changed files with 283 additions and 64 deletions.
8 changes: 6 additions & 2 deletions src/main/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ export default function loadEvents() {
} else if (mode === 'merge') {
const def = config[val as keyof typeof config] ?? {};
const user = store.get(val);
event.returnValue = Object.assign(def, user);
event.returnValue = { ...def, ...user };
} else {
event.returnValue = undefined;
}
Expand All @@ -100,6 +100,9 @@ export default function loadEvents() {
ipcMain.handle('store:delete', async (event, key) => {
store.delete(key);
});
ipcMain.handle('store:clear', async () => {
store.clear();
});
ipcMain.handle(
'store:subscribe',
async (event, property, channel: string) => {
Expand Down Expand Up @@ -145,7 +148,8 @@ export default function loadEvents() {
ipcMain.handle(
'dialog:show-message-box',
async (event, options: MessageBoxOptions) => {
await dialog.showMessageBox(options);
const result = await dialog.showMessageBox(options);
return result;
}
);
ipcMain.handle(
Expand Down
55 changes: 36 additions & 19 deletions src/main/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,10 @@ if (isDebug) {
}

function saveWindows() {
return store.get('interface.saveWindows', true);
const save = store.get('interface.saveWindows', true);
const onlyOnRequest = store.get('interface.saveOnlyOnRequest', false);

return save && !onlyOnRequest;
}

const installExtensions = async () => {
Expand Down Expand Up @@ -101,14 +104,13 @@ export async function createWindow(windowName: WindowNames) {
let windowParams: WindowParams =
config.windows[windowName.startsWith('log') ? 'log' : windowName] ?? {};

if (saveWindows()) {
const savedWindowParams: WindowParams = store.get(`windows.${name}`) || {};
windowParams = Object.assign(windowParams, savedWindowParams);
}

const savedWindowParams: WindowParams = store.get(`windows.${name}`) || {};
windowParams = { ...windowParams, ...savedWindowParams };
console.log(name, windowParams);
const newWindow = new BrowserWindow({
show: false,
icon: getAssetPath('icon.png'),
title: name,
titleBarStyle: 'hidden',
backgroundColor: nativeTheme.shouldUseDarkColors ? '#37393E' : '#FFFFFF',
...windowParams,
Expand Down Expand Up @@ -136,33 +138,50 @@ export async function createWindow(windowName: WindowNames) {
newWindow.show();
}

const openWindows: string[] = store.get('windows.openWindows', ['main']);
if (!openWindows.includes(name)) openWindows.push(name);
store.set('windows.openWindows', openWindows);
if (saveWindows()) {
const openWindows: string[] = store.get('windows.openWindows', ['main']);
if (!openWindows.includes(name)) openWindows.push(name);
store.set('windows.openWindows', openWindows);
}

// Force devtools to not show up on start.
// newWindow.webContents.closeDevTools();

// This won't show anything on the window itself, but that way we can
// generate a list of window name to BrowserWindow anywhere.
newWindow.setTitle(name);
});

// newWindow.on('did-finish-load', () => {
// newWindow.setTitle(name);
// });

newWindow.on('resized', () => {
if (!saveWindows()) return;

const size = newWindow.getSize();
store.set(`windows.${name}.width`, size[0]);
store.set(`windows.${name}.height`, size[1]);
});

newWindow.on('moved', () => {
if (!saveWindows()) return;

const position = newWindow.getPosition();
store.set(`windows.${name}.x`, position[0]);
store.set(`windows.${name}.y`, position[1]);
});

newWindow.on('closed', () => {
windows.delete(name);
const openWindows: string[] = store.get('windows.openWindows', []);
const newOpenWindows = openWindows.filter((value) => {
return value === 'main' || value !== name;
});
setTimeout(() => store.set('windows.openWindows', newOpenWindows), 3000);

if (saveWindows()) {
const openWindows: string[] = store.get('windows.openWindows', []);
const newOpenWindows = openWindows.filter((value) => {
return value === 'main' || value !== name;
});
setTimeout(() => store.set('windows.openWindows', newOpenWindows), 3000);
}
});

if (name === 'main') {
Expand Down Expand Up @@ -199,11 +218,9 @@ app.on('window-all-closed', () => {
app
.whenReady()
.then(() => {
let openWindows: WindowNames[] = ['main'];
if (saveWindows()) {
openWindows = store.get('windows.openWindows', ['main']);
}

const openWindows: WindowNames[] = store.get('windows.openWindows', [
'main',
]);
openWindows.map((key) => createWindow(key));

app.on('activate', () => {
Expand Down
6 changes: 6 additions & 0 deletions src/main/menu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
shell,
} from 'electron';
import { createWindow } from './main';
import { saveWindows } from './util';

interface DarwinMenuItemConstructorOptions extends MenuItemConstructorOptions {
selector?: string;
Expand Down Expand Up @@ -70,6 +71,11 @@ export default class MenuBuilder {
click: () => createWindow('preferences'),
},
{ type: 'separator' },
{
label: 'Save window positions',
click: saveWindows,
},
{ type: 'separator' },
{ label: 'Services', submenu: [] },
{ type: 'separator' },
{
Expand Down
10 changes: 8 additions & 2 deletions src/main/preload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
ipcRenderer,
IpcRendererEvent,
MessageBoxOptions,
MessageBoxReturnValue,
shell,
} from 'electron';
import Command from './tron/command';
Expand Down Expand Up @@ -122,6 +123,9 @@ const ElectronAPI = {
delete(key: string) {
return ipcRenderer.invoke('store:delete', key);
},
clear() {
return ipcRenderer.invoke('store:clear');
},
subscribe(property: string, channel: string) {
return ipcRenderer.invoke('store:subscribe', property, channel);
},
Expand Down Expand Up @@ -149,9 +153,11 @@ const ElectronAPI = {
},
},
dialog: {
showMessageBox: async (options: MessageBoxOptions) =>
showMessageBox: async (
options: MessageBoxOptions
): Promise<MessageBoxReturnValue> =>
ipcRenderer.invoke('dialog:show-message-box', options),
showErrorBox: async (title: string, content: string) =>
showErrorBox: async (title: string, content: string): Promise<void> =>
ipcRenderer.invoke('dialog:show-error-box', title, content),
},
};
Expand Down
3 changes: 2 additions & 1 deletion src/main/store/user.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
},
"interface": {
"mode": "system",
"saveWindows": true
"saveWindows": true,
"saveOnlyOnRequest": false
},
"guider": {
"xpaset": "/usr/local/bin/xpaset",
Expand Down
31 changes: 31 additions & 0 deletions src/main/util.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
/* eslint import/prefer-default-export: off */
import { BrowserWindow } from 'electron';
import path from 'path';
import { URL } from 'url';
import store from './store';
import { WindowParams } from './types';

export function resolveHtmlPath(htmlFileName: string) {
if (process.env.NODE_ENV === 'development') {
Expand All @@ -23,6 +26,34 @@ function getRandomInt(min: number, max: number) {
return Math.floor(Math.random() * (max - min)) + min;
}

export function saveWindows() {
const openWindows = BrowserWindow.getAllWindows();

const windows: { [key: string]: WindowParams } = {};

openWindows.forEach((win) => {
const name = win.getTitle();

const size = win.getSize();
const position = win.getPosition();

windows[name] = {
x: position[0],
y: position[1],
width: size[0],
height: size[1],
};
});

store.set('windows', windows);

const openWindowsNames = openWindows.map((win) => win.getTitle());
if (!openWindowsNames.includes('main')) {
openWindowsNames.push('main');
}
store.set('windows.openWindows', openWindowsNames);
}

export function generateName() {
const name1 = [
'abandoned',
Expand Down
5 changes: 5 additions & 0 deletions src/renderer/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ export default function App() {
action: {
boxBackground: prefersDarkMode ? '#40444B' : '#EBEDEF',
},
error: {
dark: '#d32f2f',
light: '#e57373',
main: '#CC4B41',
},
},
typography: {
fontSize: 12,
Expand Down
12 changes: 12 additions & 0 deletions src/renderer/Components/IOSwitch.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ const IOSSwitch = styled((props: SwitchProps) => (
opacity: 1,
border: 0,
},
'&.Mui-disabled + .MuiSwitch-track': {
opacity: 0.5,
},
'&:hover': {
backgroundColor: theme.palette.mode === 'light' && 'white',
},
Expand All @@ -55,6 +58,15 @@ const IOSSwitch = styled((props: SwitchProps) => (
backgroundColor: theme.palette.action.active,
},
},
'&.Mui-disabled .MuiSwitch-thumb': {
color:
theme.palette.mode === 'light'
? theme.palette.grey[100]
: theme.palette.grey[600],
},
'&.Mui-disabled + .MuiSwitch-track': {
opacity: theme.palette.mode === 'light' ? 0.7 : 0.3,
},
'& .MuiSwitch-track': {
borderRadius: 26 / 2,
backgroundColor: theme.palette.action.disabledBackground,
Expand Down
7 changes: 5 additions & 2 deletions src/renderer/Preferences/Components/Switch.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@ import { useStore } from 'renderer/hooks';

export interface SwitchProps {
param: string;
disabled?: boolean;
}

export default function Switch(props: SwitchProps) {
const { param } = props;
const { param, disabled = false } = props;

const [value, setValue] = useStore<boolean>(param);

Expand All @@ -27,5 +28,7 @@ export default function Switch(props: SwitchProps) {
setValue(checked);
};

return <IOSSwitch checked={value} onChange={handleChange} />;
return (
<IOSSwitch disabled={disabled} checked={value} onChange={handleChange} />
);
}
Loading

0 comments on commit 278b362

Please sign in to comment.