Skip to content

Commit

Permalink
Update Electron
Browse files Browse the repository at this point in the history
  • Loading branch information
richrace committed May 22, 2023
1 parent a5de284 commit 22d40a4
Show file tree
Hide file tree
Showing 9 changed files with 566 additions and 337 deletions.
55 changes: 29 additions & 26 deletions index.html
Original file line number Diff line number Diff line change
@@ -1,31 +1,34 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<link rel="stylesheet" href="./vendor/photon/css/photon.min.css">
<link rel="stylesheet" href="./assets/index.css" media="screen" charset="utf-8">
<script charset="utf-8">require('./index')</script>
<title>Arctis Monitor</title>
</head>
<body>
<div class="header-arrow"></div>
<div class="window">
<header class="toolbar toolbar-header">
<h1 class="title">Arctis Monitor</h1>
</header>

<div class="window-content">
<div class="devices">
</div>
</div>
<head>
<meta charset="utf-8">
<link rel="stylesheet" href="./vendor/photon/css/photon.min.css">
<link rel="stylesheet" href="./assets/index.css" media="screen" charset="utf-8">
<title>Arctis Monitor</title>
</head>

<body>
<div class="header-arrow"></div>
<div class="window">
<header class="toolbar toolbar-header">
<h1 class="title">Arctis Monitor</h1>
</header>

<footer class="toolbar toolbar-footer">
<div class="toolbar-actions">
<button id="quit" class="btn btn-default pull-right">
Quit
</button>
</div>
</footer>
<div class="window-content">
<div id="devices" class="devices">
</div>
</div>
</body>
</html>

<footer class="toolbar toolbar-footer">
<div class="toolbar-actions">
<button id="quit" class="btn btn-default pull-right">
Quit
</button>
</div>
</footer>
</div>
<script charset="utf-8" src="./src/renderer.js"></script>
</body>

</html>
117 changes: 0 additions & 117 deletions index.js

This file was deleted.

101 changes: 38 additions & 63 deletions main.js
Original file line number Diff line number Diff line change
@@ -1,58 +1,12 @@
const {
app, BrowserWindow, ipcMain, Tray,
// eslint-disable-next-line import/no-extraneous-dependencies
} = require('electron');
const path = require('path');
const { app, BrowserWindow, ipcMain } = require("electron");
const path = require("path");
const { createTray } = require("./src/tray");
const { updateStatus, checkWindowSize } = require("./src/arctis_view");

const assetsDirectory = path.join(__dirname, 'assets');

let tray;
let window;

const getWindowPosition = () => {
const windowBounds = window.getBounds();
const trayBounds = tray.getBounds();

// Center window horizontally below the tray icon
const x = Math.round(trayBounds.x + trayBounds.width / 2 - windowBounds.width / 2);

// Position window 4 pixels vertically below the tray icon
const y = Math.round(trayBounds.y + trayBounds.height + 4);

return { x, y };
};

const showWindow = () => {
const position = getWindowPosition();
window.setPosition(position.x, position.y, false);
window.show();
window.focus();
};

const toggleWindow = () => {
if (window.isVisible()) {
window.hide();
} else {
showWindow();
}
};

const createTray = () => {
tray = new Tray(path.join(assetsDirectory, 'headphones.png'));
tray.on('right-click', toggleWindow);
tray.on('double-click', toggleWindow);
tray.on('click', (event) => {
toggleWindow();

// Show devtools when command clicked
if (window.isVisible() && process.defaultApp && event.metaKey) {
window.openDevTools({ mode: 'detach' });
}
});
};
let mainWindow;

const createWindow = () => {
window = new BrowserWindow({
mainWindow = new BrowserWindow({
show: false,
frame: false,
width: 300,
Expand All @@ -70,31 +24,52 @@ const createWindow = () => {
backgroundThrottling: false,
nodeIntegration: true,
enableRemoteModule: true,

preload: path.join(__dirname, "src/preload.js"),
},
});
window.loadURL(`file://${path.join(__dirname, 'index.html')}`);
mainWindow.loadFile(path.join(__dirname, "index.html"));

// Hide the window when it loses focus
window.on('blur', () => {
if (!window.webContents.isDevToolsOpened()) {
window.hide();
mainWindow.on("blur", () => {
if (!mainWindow.webContents.isDevToolsOpened()) {
mainWindow.hide();
}
});

return mainWindow;
};

// Don't show the app in the doc
app.dock.hide();

app.on('ready', () => {
createTray();
function handleQuit() {
app.quit();
}

function updateView() {
const status = updateStatus();
mainWindow.webContents.send("handle-update", status.html);
checkWindowSize(mainWindow, status.numberOfDevices);
}

function initialize() {
updateView();
}

app.whenReady().then(() => {
ipcMain.on("quit", handleQuit);
ipcMain.on("init", initialize);

createWindow();
});
createTray(mainWindow);

// Quit the app when the window is closed
app.on('window-all-closed', () => {
app.quit();
// Refresh 5 minutes
const time = 5 * 60 * 1000;
setInterval(updateView, time);
});

ipcMain.on('quit-from-tray', () => {
// Quit the app when the window is closed
app.on("window-all-closed", () => {
app.quit();
});
Loading

0 comments on commit 22d40a4

Please sign in to comment.