This repository was archived by the owner on Jun 23, 2021. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 155
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #73 from sentialx/menu
feat: add menu
- Loading branch information
Showing
48 changed files
with
941 additions
and
75 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
import { ipcMain } from 'electron'; | ||
|
||
import { promises } from 'fs'; | ||
import { EventEmitter } from 'events'; | ||
import { appWindows } from '.'; | ||
import { getPath } from '~/shared/utils/paths'; | ||
import { makeId } from '~/shared/utils/string'; | ||
|
||
export class Settings extends EventEmitter { | ||
public object = { dark: false }; | ||
|
||
private queue: string[] = []; | ||
|
||
private loaded = false; | ||
|
||
public constructor() { | ||
super(); | ||
|
||
ipcMain.on( | ||
'save-settings', | ||
(e, { settings }: { settings: string; incognito: boolean }) => { | ||
this.object = { ...this.object, ...JSON.parse(settings) }; | ||
|
||
for (const window of appWindows) { | ||
if (window.webContents.id !== e.sender.id) { | ||
window.webContents.send('update-settings', this.object); | ||
window.menu.webContents.send('update-settings', this.object); | ||
} | ||
} | ||
|
||
this.addToQueue(); | ||
}, | ||
); | ||
|
||
ipcMain.on('get-settings', e => { | ||
if (!this.loaded) { | ||
this.once('load', () => { | ||
e.returnValue = this.object; | ||
}); | ||
} else { | ||
e.returnValue = this.object; | ||
} | ||
}); | ||
|
||
this.load(); | ||
} | ||
|
||
private async load() { | ||
try { | ||
const file = await promises.readFile(getPath('settings.json'), 'utf8'); | ||
|
||
this.object = { | ||
...this.object, | ||
...JSON.parse(file), | ||
}; | ||
|
||
this.loaded = true; | ||
|
||
this.emit('load'); | ||
} catch (e) { | ||
this.loaded = true; | ||
this.emit('load'); | ||
} | ||
} | ||
|
||
private async save() { | ||
try { | ||
await promises.writeFile( | ||
getPath('settings.json'), | ||
JSON.stringify(this.object), | ||
); | ||
|
||
if (this.queue.length >= 3) { | ||
for (let i = this.queue.length - 1; i > 0; i--) { | ||
this.removeAllListeners(this.queue[i]); | ||
this.queue.splice(i, 1); | ||
} | ||
} else { | ||
this.queue.splice(0, 1); | ||
} | ||
|
||
if (this.queue[0]) { | ||
this.emit(this.queue[0]); | ||
} | ||
} catch (e) { | ||
console.error(e); | ||
} | ||
} | ||
|
||
public async addToQueue() { | ||
const id = makeId(32); | ||
|
||
this.queue.push(id); | ||
|
||
if (this.queue.length === 1) { | ||
this.save(); | ||
} else { | ||
this.once(id, () => { | ||
this.save(); | ||
}); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import { AppWindow } from './app'; | ||
import { TOOLBAR_HEIGHT } from '~/renderer/views/app/constants/design'; | ||
import { PopupWindow } from './popup'; | ||
import { ipcMain } from 'electron'; | ||
|
||
const WIDTH = 300; | ||
const HEIGHT = 146; | ||
|
||
export class MenuWindow extends PopupWindow { | ||
public visible: boolean = false; | ||
|
||
public constructor(appWindow: AppWindow) { | ||
super(appWindow, 'menu'); | ||
|
||
this.setBounds({ | ||
height: HEIGHT, | ||
width: WIDTH, | ||
} as any); | ||
|
||
ipcMain.on(`hide-${this.id}`, () => { | ||
this.hide(); | ||
}); | ||
|
||
this.on('blur', () => { | ||
this.hide(); | ||
}); | ||
} | ||
|
||
public show() { | ||
super.show(); | ||
this.rearrange(); | ||
this.visible = true; | ||
} | ||
|
||
public hide() { | ||
super.hide(); | ||
this.visible = false; | ||
} | ||
|
||
public toggle() { | ||
if (this.visible) this.hide(); | ||
else this.show(); | ||
} | ||
|
||
public rearrange() { | ||
const cBounds = this.appWindow.getContentBounds(); | ||
this.setBounds({ | ||
x: Math.round(cBounds.x + cBounds.width - WIDTH - 8), | ||
y: cBounds.y + TOOLBAR_HEIGHT, | ||
} as any); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { BrowserWindow, app, ipcMain } from 'electron'; | ||
import { join } from 'path'; | ||
import { AppWindow } from './app'; | ||
|
||
export class PopupWindow extends BrowserWindow { | ||
protected appWindow: AppWindow; | ||
|
||
public constructor(appWindow: AppWindow, name: string, devtools = false) { | ||
super({ | ||
frame: false, | ||
resizable: false, | ||
show: false, | ||
fullscreenable: false, | ||
webPreferences: { | ||
nodeIntegration: true, | ||
contextIsolation: false, | ||
}, | ||
skipTaskbar: true, | ||
backgroundColor: '#00ffffff', | ||
}); | ||
|
||
this.appWindow = appWindow; | ||
|
||
if (process.env.ENV === 'dev') { | ||
if (devtools) { | ||
this.webContents.openDevTools({ mode: 'detach' }); | ||
} | ||
this.loadURL(`http://localhost:4444/${name}.html`); | ||
} else { | ||
this.loadURL(join('file://', app.getAppPath(), `build/${name}.html`)); | ||
} | ||
|
||
ipcMain.on(`get-window-id-${this.id}`, e => { | ||
e.returnValue = this.appWindow.id; | ||
}); | ||
|
||
this.setParentWindow(this.appWindow); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import * as React from 'react'; | ||
|
||
import { colors } from '~/renderer/constants'; | ||
import { StyledSwitch, Thumb } from './styles'; | ||
import { observer } from 'mobx-react-lite'; | ||
|
||
interface Props { | ||
color?: string; | ||
onChange?: (value: boolean) => void; | ||
value?: boolean; | ||
} | ||
|
||
export const Switch = observer(({ color, value }: Props) => { | ||
return ( | ||
<StyledSwitch activated={value} color={color}> | ||
<Thumb activated={value} color={color} /> | ||
</StyledSwitch> | ||
); | ||
}); | ||
|
||
(Switch as any).defaultProps = { | ||
color: colors.blue['500'], | ||
}; |
Oops, something went wrong.