Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
hockyy committed Dec 18, 2022
0 parents commit 56be5d7
Show file tree
Hide file tree
Showing 23 changed files with 13,428 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
node_modules
*.log
.next
app
dist
40 changes: 40 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Nextron: Main",
"type": "node",
"request": "attach",
"protocol": "inspector",
"port": 9292,
"skipFiles": ["<node_internals>/**"],
"sourceMapPathOverrides": {
"webpack:///./~/*": "${workspaceFolder}/node_modules/*",
"webpack:///./*": "${workspaceFolder}/*",
"webpack:///*": "*"
}
},
{
"name": "Nextron: Renderer",
"type": "chrome",
"request": "attach",
"port": 5858,
"timeout": 10000,
"urlFilter": "http://localhost:*",
"webRoot": "${workspaceFolder}/app",
"sourceMapPathOverrides": {
"webpack:///./src/*": "${webRoot}/*"
}
}
],
"compounds": [
{
"name": "Nextron: All",
"preLaunchTask": "dev",
"configurations": ["Nextron: Main", "Nextron: Renderer"]
}
]
}
21 changes: 21 additions & 0 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"version": "2.0.0",
"tasks": [
{
"type": "npm",
"script": "dev",
"isBackground": true,
"problemMatcher": {
"owner": "custom",
"pattern": {
"regexp": ""
},
"background": {
"beginsPattern": "started server",
"endsPattern": "Debugger listening on"
}
},
"label": "dev"
}
]
}
38 changes: 38 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<p align="center"><img src="https://i.imgur.com/a9QWW0v.png"></p>

## Usage

### Create an App

```
# with npx
$ npx create-nextron-app my-app --example with-typescript-tailwindcss
# with yarn
$ yarn create nextron-app my-app --example with-typescript-tailwindcss
# with pnpx
$ pnpx create-nextron-app my-app --example with-typescript-tailwindcss
```

### Install Dependencies

```
$ cd my-app
# using yarn or npm
$ yarn (or `npm install`)
# using pnpm
$ pnpm install --shamefully-hoist
```

### Use it

```
# development mode
$ yarn dev (or `npm run dev` or `pnpm run dev`)
# production build
$ yarn build (or `npm run build` or `pnpm run build`)
```
12 changes: 12 additions & 0 deletions electron-builder.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
appId: com.example.nextron
productName: My Nextron App
copyright: Copyright © 2018 Yoshihide Shiono
directories:
output: dist
buildResources: resources
files:
- from: .
filter:
- package.json
- app
publish: null
32 changes: 32 additions & 0 deletions main/background.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { app } from 'electron';
import serve from 'electron-serve';
import { createWindow } from './helpers';

const isProd: boolean = process.env.NODE_ENV === 'production';

if (isProd) {
serve({ directory: 'app' });
} else {
app.setPath('userData', `${app.getPath('userData')} (development)`);
}

(async () => {
await app.whenReady();

const mainWindow = createWindow('main', {
width: 1000,
height: 600,
});

if (isProd) {
await mainWindow.loadURL('app://./home.html');
} else {
const port = process.argv[2];
await mainWindow.loadURL(`http://localhost:${port}/home`);
mainWindow.webContents.openDevTools();
}
})();

app.on('window-all-closed', () => {
app.quit();
});
84 changes: 84 additions & 0 deletions main/helpers/create-window.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import {
screen,
BrowserWindow,
BrowserWindowConstructorOptions,
} from 'electron';
import Store from 'electron-store';

export default (windowName: string, options: BrowserWindowConstructorOptions): BrowserWindow => {
const key = 'window-state';
const name = `window-state-${windowName}`;
const store = new Store({ name });
const defaultSize = {
width: options.width,
height: options.height,
};
let state = {};
let win;

const restore = () => store.get(key, defaultSize);

const getCurrentPosition = () => {
const position = win.getPosition();
const size = win.getSize();
return {
x: position[0],
y: position[1],
width: size[0],
height: size[1],
};
};

const windowWithinBounds = (windowState, bounds) => {
return (
windowState.x >= bounds.x &&
windowState.y >= bounds.y &&
windowState.x + windowState.width <= bounds.x + bounds.width &&
windowState.y + windowState.height <= bounds.y + bounds.height
);
};

const resetToDefaults = () => {
const bounds = screen.getPrimaryDisplay().bounds;
return Object.assign({}, defaultSize, {
x: (bounds.width - defaultSize.width) / 2,
y: (bounds.height - defaultSize.height) / 2,
});
};

const ensureVisibleOnSomeDisplay = windowState => {
const visible = screen.getAllDisplays().some(display => {
return windowWithinBounds(windowState, display.bounds);
});
if (!visible) {
// Window is partially or fully not visible now.
// Reset it to safe defaults.
return resetToDefaults();
}
return windowState;
};

const saveState = () => {
if (!win.isMinimized() && !win.isMaximized()) {
Object.assign(state, getCurrentPosition());
}
store.set(key, state);
};

state = ensureVisibleOnSomeDisplay(restore());

const browserOptions: BrowserWindowConstructorOptions = {
...options,
...state,
webPreferences: {
nodeIntegration: true,
contextIsolation: false,
...options.webPreferences,
},
};
win = new BrowserWindow(browserOptions);

win.on('close', saveState);

return win;
};
5 changes: 5 additions & 0 deletions main/helpers/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import createWindow from './create-window';

export {
createWindow,
};
Loading

0 comments on commit 56be5d7

Please sign in to comment.