Commit 5ddaa5ca authored by Guilherme Gazzo's avatar Guilherme Gazzo
Browse files

window storage

parent d9a8cf9e
......@@ -3,6 +3,14 @@ import { rendererAppName, rendererAppPort } from './constants';
import { environment } from '../environments/environment';
import { join } from 'path';
import { format } from 'url';
import { ElectronWindowState } from '@rocket.chat.desktop/electron-window-state';
// const WindowStateManager = require('electron-window-state-manager');
// const mainWindowState = new WindowStateManager('mainWindow', {
// defaultWidth: 1024,
// defaultHeight: 768
// });
import { IpcStateMain } from '@rocket.chat.desktop/ipc-state';
......@@ -10,9 +18,9 @@ const state = new IpcStateMain<{
servers: [string, boolean, boolean];
}>('servers', new Map(Object.entries({'servers': [] })) as any);
setInterval(() => {
state.set('servers', [JSON.stringify(process.env), App.isDevelopmentMode(), process.env.WEBPACK_DEV_SERVER === 'true']);
}, 1000);
// setInterval(() => {
// state.set('servers', [JSON.stringify(process.env), App.isDevelopmentMode(), process.env.WEBPACK_DEV_SERVER === 'true']);
// }, 1000);
export default class App {
// Keep a global reference of the window object, if you don't, the window will
......@@ -89,10 +97,19 @@ export default class App {
const width = Math.min(1280, workAreaSize.width || 1280);
const height = Math.min(720, workAreaSize.height || 720);
const windowState = new ElectronWindowState({
width,
height,
});
// Create the browser window.
App.mainWindow = new BrowserWindow({
width: width,
height: height,
width: windowState.width,
height: windowState.height,
x: windowState.x,
y: windowState.y,
show: false,
titleBarStyle: 'hidden',
webPreferences: {
......@@ -102,8 +119,12 @@ export default class App {
preload: join(__dirname, 'preload.js'),
},
});
if (windowState.maximized) {
App.mainWindow.maximize();
}
App.mainWindow.setMenu(null);
App.mainWindow.center();
// if main window is ready to show, close the splash window and show the main window
App.mainWindow.once('ready-to-show', () => {
......@@ -116,13 +137,24 @@ export default class App {
// App.onRedirect(event, url);
// });
// Emitted when the window is closed.
App.mainWindow.on('close', () => {
// Dereference the window object, usually you would store windows
// in an array if your app supports multi windows, this is the time
// when you should delete the corresponding element.
windowState.save(App.mainWindow);
});
App.mainWindow.on('closed', () => {
// Dereference the window object, usually you would store windows
// in an array if your app supports multi windows, this is the time
// when you should delete the corresponding element.
App.mainWindow = null;
});
}
private static loadMainWindow() {
......
......@@ -42,39 +42,41 @@ const SidebarLink = forwardRef((props, ref) => (
export function App() {
return (
<HashRouter>
<SafeDragZone>
<Sidebar isVisible>
<SidebarGroup>
{ process.platform === 'darwin' && <Box h='x16' width='full' /> }
<Link component={SidebarLink} { ...{ icon: 'plus' } as any } to='/add'/>
</SidebarGroup>
<SidebarGroup>
<Link component={SidebarLink} { ...{ icon:'cog'} as any} to='/settings' />
<Link component={SidebarLink} { ...{ icon:'download'} as any} to='/downloads'/>
</SidebarGroup>
</Sidebar>
<Box is='webview' flexGrow={1} src='https://open.rocket.chat' elevation={"1"} />
<Switch>
<Route exact path="/" component={Home}/>
<Route exact path="/settings" component={Settings}/>
<Route exact path="/downloads" component={Downloads}/>
</Switch>
</SafeDragZone>
<SafeDragZone>
<Sidebar isVisible>
<SidebarGroup>
{ process.platform === 'darwin' && <Box h='x16' width='full' /> }
<Link component={SidebarLink} { ...{ icon: 'plus' } as any } to='/add'/>
</SidebarGroup>
<SidebarGroup>
<Link component={SidebarLink} { ...{ icon:'cog'} as any} to='/settings' />
<Link component={SidebarLink} { ...{ icon:'download'} as any} to='/downloads'/>
</SidebarGroup>
</Sidebar>
<Box display='flex' flexGrow={1} elevation={"1"}>
<Box is='webview' flexGrow={1} src='https://open.rocket.chat' />
<Switch>
<Route exact path="/" component={Home}/>
<Route exact path="/settings" component={Settings}/>
<Route exact path="/downloads" component={Downloads}/>
</Switch>
</Box>
</SafeDragZone>
</HashRouter>
);
}
function Home() {
return <h2>Home</h2>;
return <Box position='fixed' width='100%' height='100%'>Home</Box>;
}
function Settings() {
return <h2>Settings</h2>;
return <Box position='fixed' width='100%' height='100%'>Settings</Box>;
}
function Downloads() {
return <h2>Downloads</h2>;
return <Box position='fixed' width='100%' height='100%'>Downloads</Box>;
}
export default App;
{
"presets": [["@nrwl/web/babel", { "useBuiltIns": "usage" }]]
}
{
"extends": ["../../.eslintrc.json"],
"ignorePatterns": ["!**/*"],
"overrides": [
{
"files": ["*.ts", "*.tsx", "*.js", "*.jsx"],
"rules": {}
},
{
"files": ["*.ts", "*.tsx"],
"rules": {}
},
{
"files": ["*.js", "*.jsx"],
"rules": {}
}
]
}
# electron-window-state
This library was generated with [Nx](https://nx.dev).
## Running unit tests
Run `nx test electron-window-state` to execute the unit tests via [Jest](https://jestjs.io).
module.exports = {
displayName: 'electron-window-state',
preset: '../../jest.preset.js',
globals: {
'ts-jest': {
tsconfig: '<rootDir>/tsconfig.spec.json',
},
},
testEnvironment: 'node',
transform: {
'^.+\\.[tj]sx?$': 'ts-jest',
},
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx'],
coverageDirectory: '../../coverage/libs/electron-window-state',
};
export * from './lib/electron-window-state';
import { electronWindowState } from './electron-window-state';
describe('electronWindowState', () => {
it('should work', () => {
expect(electronWindowState()).toEqual('electron-window-state');
});
});
import electron from 'electron';
import path from "path";
import jetpack from 'fs-jetpack';
const WINDOWS_STATE_FILE = path.join(electron.app.getPath('appData'), electron.app.getName(), 'window-states.json');
export type ElectronWindowStateOptions = {
width: number;
height: number;
x?: number;
y?: number;
maximized?: false;
} | {
width: number;
height: number;
maximized: true;
}
export class ElectronWindowState {
get width() { return this.options.width; }
get height() { return this.options.height; }
get x(): number | undefined { return this.options.maximized === true ? undefined : this.options.x; }
get y(): number | undefined { return this.options.maximized === true ? undefined : this.options.y; }
get maximized() { return this.options.maximized; }
private readonly options: ElectronWindowStateOptions;
private readonly name: string;
constructor(options: ElectronWindowStateOptions, name = 'default') {
const storedState = this.getStoredState();
this.options = { ...options, ...storedState?.[name] };
this.name = name;
}
getStoredState(): { [key: string]: ElectronWindowStateOptions } {
return jetpack.read(WINDOWS_STATE_FILE, 'json') || {};
}
get(): ElectronWindowStateOptions {
return this.options;
}
private getOptions(window: Electron.BrowserWindow): ElectronWindowStateOptions{
const bounds = window.getBounds();
if(!window.isMaximized()) {
return {
width: bounds.width,
height: bounds.height,
x: bounds.x,
y: bounds.y,
maximized: false,
}
}
return {
width: bounds.width,
height: bounds.height,
maximized: true,
}
}
save(window: Electron.BrowserWindow) {
if (window.isFullScreen()){
return
}
const options = this.getStoredState();
const state = this.getOptions(window);
jetpack.write(WINDOWS_STATE_FILE, { ...options, [this.name]: state }, { atomic: true });
}
}
{
"extends": "../../tsconfig.base.json",
"files": [],
"include": [],
"references": [
{
"path": "./tsconfig.lib.json"
},
{
"path": "./tsconfig.spec.json"
}
]
}
{
"extends": "./tsconfig.json",
"compilerOptions": {
"module": "commonjs",
"outDir": "../../dist/out-tsc",
"declaration": true,
"types": ["node"]
},
"exclude": ["**/*.spec.ts"],
"include": ["**/*.ts"]
}
{
"extends": "./tsconfig.json",
"compilerOptions": {
"outDir": "../../dist/out-tsc",
"module": "commonjs",
"types": ["jest", "node"]
},
"include": [
"**/*.spec.ts",
"**/*.spec.tsx",
"**/*.spec.js",
"**/*.spec.jsx",
"**/*.d.ts"
]
}
......@@ -39,6 +39,9 @@
"electron-store": {
"tags": []
},
"electron-window-state": {
"tags": []
},
"ipc-state": {
"tags": []
},
......
......@@ -19,6 +19,9 @@
"@rocket.chat.desktop/electron-store": [
"libs/electron-store/src/index.ts"
],
"@rocket.chat.desktop/electron-window-state": [
"libs/electron-window-state/src/index.ts"
],
"@rocket.chat.desktop/ipc-state": ["libs/ipc-state/src/index.ts"],
"@rocket.chat.desktop/ui-desktop-components": [
"libs/ui-desktop-components/src/index.ts"
......
......@@ -89,6 +89,27 @@
}
}
},
"electron-window-state": {
"root": "libs/electron-window-state",
"sourceRoot": "libs/electron-window-state/src",
"projectType": "library",
"targets": {
"lint": {
"executor": "@nrwl/linter:eslint",
"options": {
"lintFilePatterns": ["libs/electron-window-state/**/*.ts"]
}
},
"test": {
"executor": "@nrwl/jest:jest",
"outputs": ["coverage/libs/electron-window-state"],
"options": {
"jestConfig": "libs/electron-window-state/jest.config.js",
"passWithNoTests": true
}
}
}
},
"ipc-state": {
"root": "libs/ipc-state",
"sourceRoot": "libs/ipc-state/src",
......
......@@ -4235,6 +4235,11 @@ app-root-dir@^1.0.2:
resolved "https://registry.yarnpkg.com/app-root-dir/-/app-root-dir-1.0.2.tgz#38187ec2dea7577fff033ffcb12172692ff6e118"
integrity sha1-OBh+wt6nV3//Az/8sSFyaS/24Rg=
 
app-root-path@^1.3.0:
version "1.4.0"
resolved "https://registry.yarnpkg.com/app-root-path/-/app-root-path-1.4.0.tgz#6335d865c9640d0fad99004e5a79232238e92dfa"
integrity sha1-YzXYZclkDQ+tmQBOWnkjIjjpLfo=
aproba@^1.0.3, aproba@^1.1.1:
version "1.2.0"
resolved "https://registry.yarnpkg.com/aproba/-/aproba-1.2.0.tgz#6802e6264efd18c790a1b0d517f0f2627bf2c94a"
......@@ -7019,6 +7024,14 @@ electron-to-chromium@^1.3.564, electron-to-chromium@^1.3.811:
resolved "https://registry.yarnpkg.com/electron-to-chromium/-/electron-to-chromium-1.3.818.tgz#32ed024fa8316e5d469c96eecbea7d2463d80085"
integrity sha512-c/Z9gIr+jDZAR9q+mn40hEc1NharBT+8ejkarjbCDnBNFviI6hvcC5j2ezkAXru//bTnQp5n6iPi0JA83Tla1Q==
 
electron-window-state-manager@^0.3.2:
version "0.3.2"
resolved "https://registry.yarnpkg.com/electron-window-state-manager/-/electron-window-state-manager-0.3.2.tgz#a552ea35470297c999c744d81abb2a8c8a51770e"
integrity sha1-pVLqNUcCl8mZx0TYGrsqjIpRdw4=
dependencies:
app-root-path "^1.3.0"
fs-jetpack "^0.9.2"
electron@^13.2.2:
version "13.2.2"
resolved "https://registry.yarnpkg.com/electron/-/electron-13.2.2.tgz#332d91891d0db4f9a1d22d4d0bc3b500e59dc051"
......@@ -8234,6 +8247,24 @@ fs-extra@^9.0.0, fs-extra@^9.0.1, fs-extra@^9.1.0:
jsonfile "^6.0.1"
universalify "^2.0.0"
 
fs-jetpack@^0.9.2:
version "0.9.2"
resolved "https://registry.yarnpkg.com/fs-jetpack/-/fs-jetpack-0.9.2.tgz#93b414dbd07801863d64c81a76adc03d850c9b56"
integrity sha1-k7QU29B4AYY9ZMgadq3APYUMm1Y=
dependencies:
minimatch "^3.0.2"
mkdirp "^0.5.1"
q "^1.0.1"
rimraf "^2.2.8"
fs-jetpack@^4.1.1:
version "4.1.1"
resolved "https://registry.yarnpkg.com/fs-jetpack/-/fs-jetpack-4.1.1.tgz#83bd16f2c282eb05e4e6f62be4c5fe4f767c179c"
integrity sha512-BSZ+f6VjrMInpA6neNnUhQNFPPdf3M+I8v8M9dBRrbmExd8GNRbTJIq1tjNh86FQ4a+EoMtPcp1oemwY5ghGBw==
dependencies:
minimatch "^3.0.2"
rimraf "^2.6.3"
fs-minipass@^2.0.0, fs-minipass@^2.1.0:
version "2.1.0"
resolved "https://registry.yarnpkg.com/fs-minipass/-/fs-minipass-2.1.0.tgz#7f5036fdbf12c63c169190cbe4199c852271f9fb"
......@@ -13253,7 +13284,7 @@ pupa@^2.1.1:
dependencies:
escape-goat "^2.0.0"
 
q@^1.1.2:
q@^1.0.1, q@^1.1.2:
version "1.5.1"
resolved "https://registry.yarnpkg.com/q/-/q-1.5.1.tgz#7e32f75b41381291d04611f1bf14109ac00651d7"
integrity sha1-fjL3W0E4EpHQRhHxvxQQmsAGUdc=
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment