Skip to content

Commit d074072

Browse files
xudafengmoshangqi
andauthored
feat: add windows-titlebar (#11)
Co-authored-by: moshangqi <zengwanpeng@qq.com>
1 parent f6edaa6 commit d074072

File tree

5 files changed

+116
-0
lines changed

5 files changed

+116
-0
lines changed

src/app.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ export default class App {
1616
require('./updator')(this);
1717
} else if (action === 'electron-webview-schedule') {
1818
require('./webview-schedule')(this);
19+
} else if (action === 'electron-windows-titlebar') {
20+
require('./windows-titlebar')(this);
1921
}
2022
});
2123
}

src/windows-titlebar/index.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
'use strict';
2+
3+
const url = require('url');
4+
const path = require('path');
5+
const _ = require('lodash');
6+
const { ipcMain } = require('electron');
7+
const windowTitleBar = require('electron-windows-titlebar');
8+
9+
module.exports = (app: any) => {
10+
const windowUrl = url.format({
11+
pathname: path.join(__dirname, 'renderer', 'main.html'),
12+
protocol: 'file:',
13+
});
14+
const loadingUrl = url.format({
15+
pathname: path.join(__dirname, 'renderer', 'loading.html'),
16+
protocol: 'file:',
17+
});
18+
19+
const window = app.windowManager.create({
20+
name: Date.now(),
21+
loadingView: {
22+
url: loadingUrl,
23+
},
24+
browserWindow: {
25+
webPreferences: {
26+
nodeIntegration: true,
27+
webSecurity: true,
28+
webviewTag: true,
29+
preload: path.join(__dirname, 'renderer', 'preload.js'),
30+
},
31+
},
32+
});
33+
const hwnd = window?.getNativeWindowHandle();
34+
ipcMain.on('switch-theme-mode', (_, params) => {
35+
const { dark } = params;
36+
if (hwnd) {
37+
dark ? windowTitleBar.switchDarkMode(hwnd) : windowTitleBar.switchLightMode(hwnd);
38+
}
39+
});
40+
window.loadURL(windowUrl);
41+
};
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="UTF-8">
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
6+
<title>Loading</title>
7+
<style>
8+
::-webkit-scrollbar {
9+
display: none;
10+
}
11+
* {
12+
overflow: hidden;
13+
box-sizing: border-box;
14+
}
15+
html,
16+
body {
17+
background-color: white;
18+
width: 100%;
19+
height: 100%;
20+
margin: 0;
21+
padding: 0;
22+
}
23+
.wrapper {
24+
text-align: center;
25+
padding-top: 100px;
26+
}
27+
</style>
28+
</head>
29+
<body>
30+
<div class="wrapper">
31+
<div class="loading">
32+
loading...
33+
</div>
34+
</div>
35+
</body>
36+
</html>
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="UTF-8">
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
6+
<title>Addon Demo</title>
7+
</head>
8+
<body>
9+
<h1>Titlebar Addon Demo</h1>
10+
<p>Print handle of current window <span id="electron-window"></span></p>
11+
<button id="switch-dark-mode">switch dark mode</button>
12+
<button id="switch-light-mode">switch light mode</button>
13+
</body>
14+
</html>
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
'use strict';
2+
3+
const { contextBridge, ipcRenderer } = require('electron');
4+
5+
contextBridge.exposeInMainWorld('electron', {
6+
ipcRenderer: {
7+
send: (channel, ...args) => ipcRenderer.send(channel, ...args),
8+
},
9+
});
10+
11+
window.addEventListener('DOMContentLoaded', () => {
12+
document.getElementById('switch-dark-mode').addEventListener('click', () => {
13+
ipcRenderer.send('switch-theme-mode', {
14+
dark: true,
15+
});
16+
});
17+
18+
document.getElementById('switch-light-mode').addEventListener('click', () => {
19+
ipcRenderer.send('switch-theme-mode', {
20+
dark: false,
21+
});
22+
});
23+
});

0 commit comments

Comments
 (0)