1
0
mirror of https://github.com/bitwarden/browser.git synced 2024-11-07 09:31:31 +01: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 { BrowserWindow } from 'electron';
import * as path from 'path';
import * as url from 'url';
/*
import { getPassword, setPassword, deletePassword } from 'keytar';
const keytarService = 'bitwarden'; import { MenuMain } from './main/menu.main';
ipcMain.on('keytar', async (event: any, message: any) => { import { MessagingMain } from './main/messaging.main';
try { import { WindowMain } from './main/window.main';
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 { I18nService } from './services/i18n.service'; 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 args = process.argv.slice(1);
const watch = args.some((val) => val === '--watch'); const watch = args.some((val) => val === '--watch');
const dev = args.some((val) => val === '--dev'); const dev = args.some((val) => val === '--dev');
@ -53,65 +14,14 @@ if (watch) {
require('electron-reload')(__dirname, {}); require('electron-reload')(__dirname, {});
} }
function createWindow() { const i18nService = new I18nService('en', './locales/');
const primaryScreenSize = screen.getPrimaryDisplay().workAreaSize; i18nService.init().then(() => { });
// Create the browser window. let win: BrowserWindow;
win = new BrowserWindow({ const messagingMain = new MessagingMain();
width: primaryScreenSize.width < 950 ? primaryScreenSize.width : 950, const menuMain = new MenuMain();
height: primaryScreenSize.height < 600 ? primaryScreenSize.height : 600, const windowMain = new WindowMain(win, dev);
minWidth: 680,
minHeight: 500,
title: app.getName(),
darkTheme: true,
vibrancy: 'ultra-dark',
});
// and load the index.html of the app. messagingMain.init();
win.loadURL(url.format({ menuMain.init();
protocol: 'file:', windowMain.init();
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;
}

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;
});
}
}