1
0
mirror of https://github.com/bitwarden/browser.git synced 2024-09-16 02:27:00 +02:00

organization main into classes

This commit is contained in:
Kyle Spearrin 2018-02-08 13:10:13 -05:00
parent 825cc5fd37
commit 5e8ccd19c5
4 changed files with 212 additions and 103 deletions

View File

@ -1,50 +1,11 @@
import { app, BrowserWindow, dialog, ipcMain, screen } from 'electron';
import * as path from 'path';
import * as url from 'url';
/*
import { getPassword, setPassword, deletePassword } from 'keytar';
import { BrowserWindow } from 'electron';
const keytarService = 'bitwarden';
ipcMain.on('keytar', async (event: any, message: any) => {
try {
let val: string = null;
if (message.action && message.key) {
if (message.action === 'getPassword') {
val = await getPassword(keytarService, message.key);
} else if (message.action === 'setPassword' && message.value) {
await setPassword(keytarService, message.key, message.value);
} else if (message.action === 'deletePassword') {
await deletePassword(keytarService, message.key);
}
}
event.returnValue = val;
}
catch {
event.returnValue = null;
}
});
*/
import { MenuMain } from './main/menu.main';
import { MessagingMain } from './main/messaging.main';
import { WindowMain } from './main/window.main';
import { I18nService } from './services/i18n.service';
const i18nService = new I18nService('en', './locales/');
i18nService.init().then(() => { });
ipcMain.on('messagingService', async (event: any, message: any) => {
switch (message.command) {
case 'loggedIn':
break;
case 'logout':
break;
case 'syncCompleted':
console.log('sync completed!!');
break;
default:
break;
}
});
let win: BrowserWindow;
const args = process.argv.slice(1);
const watch = args.some((val) => val === '--watch');
const dev = args.some((val) => val === '--dev');
@ -53,65 +14,14 @@ if (watch) {
require('electron-reload')(__dirname, {});
}
function createWindow() {
const primaryScreenSize = screen.getPrimaryDisplay().workAreaSize;
const i18nService = new I18nService('en', './locales/');
i18nService.init().then(() => { });
// Create the browser window.
win = new BrowserWindow({
width: primaryScreenSize.width < 950 ? primaryScreenSize.width : 950,
height: primaryScreenSize.height < 600 ? primaryScreenSize.height : 600,
minWidth: 680,
minHeight: 500,
title: app.getName(),
darkTheme: true,
vibrancy: 'ultra-dark',
});
let win: BrowserWindow;
const messagingMain = new MessagingMain();
const menuMain = new MenuMain();
const windowMain = new WindowMain(win, dev);
// and load the index.html of the app.
win.loadURL(url.format({
protocol: 'file:',
pathname: path.join(__dirname, '/index.html'),
slashes: true,
}));
// Open the DevTools.
if (dev) {
win.webContents.openDevTools();
}
// Emitted when the window is closed.
win.on('closed', () => {
// Dereference the window object, usually you would store window
// in an array if your app supports multi windows, this is the time
// when you should delete the corresponding element.
win = null;
});
}
try {
// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs.
app.on('ready', createWindow);
// Quit when all windows are closed.
app.on('window-all-closed', () => {
// On OS X it is common for applications and their menu bar
// to stay active until the user quits explicitly with Cmd + Q
if (process.platform !== 'darwin') {
app.quit();
}
});
app.on('activate', () => {
// On OS X it's common to re-create a window in the app when the
// dock icon is clicked and there are no other windows open.
if (win === null) {
createWindow();
}
});
} catch (e) {
// Catch Error
// throw e;
}
messagingMain.init();
menuMain.init();
windowMain.init();

83
src/main/menu.main.ts Normal file
View File

@ -0,0 +1,83 @@
import { app, Menu, MenuItemConstructorOptions } from 'electron';
export class MenuMain {
constructor() { }
init() {
const template: MenuItemConstructorOptions[] = [
{
label: 'Edit',
submenu: [
{ role: 'undo' },
{ role: 'redo' },
{ type: 'separator' },
{ role: 'cut' },
{ role: 'copy' },
{ role: 'paste' },
{ role: 'pasteandmatchstyle' },
{ role: 'delete' },
{ role: 'selectall' }
]
},
{
label: 'View',
submenu: [
{ role: 'reload' },
{ role: 'forcereload' },
{ role: 'toggledevtools' },
{ type: 'separator' },
{ role: 'resetzoom' },
{ role: 'zoomin' },
{ role: 'zoomout' },
{ type: 'separator' },
{ role: 'togglefullscreen' }
]
},
{
role: 'window',
submenu: [
{ role: 'minimize' },
{ role: 'close' }
]
},
{
role: 'help',
submenu: [
{
label: 'Learn More',
click() { require('electron').shell.openExternal('https://electronjs.org') }
}
]
}
];
if (process.platform === 'darwin') {
template.unshift({
label: app.getName(),
submenu: [
{ role: 'about' },
{ type: 'separator' },
{ role: 'services', submenu: [] },
{ type: 'separator' },
{ role: 'hide' },
{ role: 'hideothers' },
{ role: 'unhide' },
{ type: 'separator' },
{ role: 'quit' }
]
})
// Window menu
template[3].submenu = [
{ role: 'close' },
{ role: 'minimize' },
{ role: 'zoom' },
{ type: 'separator' },
{ role: 'front' }
]
};
const menu = Menu.buildFromTemplate(template);
Menu.setApplicationMenu(menu);
}
}

View File

@ -0,0 +1,44 @@
import { app, ipcMain } from 'electron';
// import { getPassword, setPassword, deletePassword } from 'keytar';
const KeytarService = 'bitwarden';
export class MessagingMain {
init() {
ipcMain.on('messagingService', async (event: any, message: any) => {
switch (message.command) {
case 'loggedIn':
break;
case 'logout':
break;
case 'syncCompleted':
console.log('sync completed!!');
break;
default:
break;
}
});
/*
ipcMain.on('keytar', async (event: any, message: any) => {
try {
let val: string = null;
if (message.action && message.key) {
if (message.action === 'getPassword') {
val = await getPassword(KeytarService, message.key);
} else if (message.action === 'setPassword' && message.value) {
await setPassword(KeytarService, message.key, message.value);
} else if (message.action === 'deletePassword') {
await deletePassword(KeytarService, message.key);
}
}
event.returnValue = val;
}
catch {
event.returnValue = null;
}
});
*/
}
}

72
src/main/window.main.ts Normal file
View File

@ -0,0 +1,72 @@
import { app, BrowserWindow, screen } from 'electron';
import * as path from 'path';
import * as url from 'url';
export class WindowMain {
constructor(private win: BrowserWindow, private dev: boolean) { }
init() {
try {
// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs.
app.on('ready', () => this.createWindow());
// Quit when all windows are closed.
app.on('window-all-closed', () => {
// On OS X it is common for applications and their menu bar
// to stay active until the user quits explicitly with Cmd + Q
if (process.platform !== 'darwin') {
app.quit();
}
});
app.on('activate', () => {
// On OS X it's common to re-create a window in the app when the
// dock icon is clicked and there are no other windows open.
if (this.win === null) {
this.createWindow();
}
});
} catch (e) {
// Catch Error
// throw e;
}
}
private createWindow() {
const primaryScreenSize = screen.getPrimaryDisplay().workAreaSize;
// Create the browser window.
this.win = new BrowserWindow({
width: primaryScreenSize.width < 950 ? primaryScreenSize.width : 950,
height: primaryScreenSize.height < 600 ? primaryScreenSize.height : 600,
minWidth: 680,
minHeight: 500,
title: app.getName(),
darkTheme: true,
vibrancy: 'ultra-dark',
});
// and load the index.html of the app.
this.win.loadURL(url.format({
protocol: 'file:',
pathname: path.join(__dirname, '/index.html'),
slashes: true,
}));
// Open the DevTools.
if (this.dev) {
this.win.webContents.openDevTools();
}
// Emitted when the window is closed.
this.win.on('closed', () => {
// Dereference the window object, usually you would store window
// in an array if your app supports multi windows, this is the time
// when you should delete the corresponding element.
this.win = null;
});
}
}