当前位置:网站首页>Electron入门教程4 —— 切换应用的主题
Electron入门教程4 —— 切换应用的主题
2022-04-23 19:41:00 【害恶细君】
从这一期开始,我会陆续地总结桌面应用常用的一些功能案例,之后有类似需求举一反三即可。这节学习一下如何切换应用的主题。
如果您想要手动在亮/暗模式之间切换,您可以通过在nativeTheme
模块的themeSource
属性中设置所需的模式来做到这一点。此属性的值将传播到您的渲染进程。任何与prefers-color-scheme
相关的CSS规则都将相应地更新。
先上代码,之后再分析:
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>主题切换</title>
<link rel="stylesheet" type="text/css" href="./style.css">
</head>
<body>
<p>当前主题为:<strong id="theme-source">系统主题</strong></p>
<button id="toggle">切换为深色模式</button>
<button id="reset-to-system">切换系统主题</button>
<script src='./index.js'></script>
</body>
</html>
preload.js
const {
contextBridge, ipcRenderer } = require('electron')
contextBridge.exposeInMainWorld('darkMode', {
toggle: () => ipcRenderer.invoke('dark-mode:toggle'),
system: () => ipcRenderer.invoke('dark-mode:system')
})
index.js
document.getElementById('toggle').addEventListener('click', async () => {
const isDarkMode = await window.darkMode.toggle()
document.getElementById('theme-source').innerHTML = isDarkMode ? '深色主题' : '浅色主题'
document.getElementById('toggle').innerHTML= isDarkMode ? '切换为浅色主题' : '切换为深色主题'
})
document.getElementById('reset-to-system').addEventListener('click', async () => {
await window.darkMode.system()
document.getElementById('theme-source').innerHTML = '系统主题'
})
style.css
@media (prefers-color-scheme: dark) {
body {
background: #333; color: white; }
}
@media (prefers-color-scheme: light) {
body {
background: #ddd; color: black; }
}
main.js
const {
app, BrowserWindow, ipcMain, nativeTheme } = require('electron')
const path = require('path')
function createWindow () {
const win = new BrowserWindow({
width: 800,
height: 600,
webPreferences: {
preload: path.join(__dirname, 'preload.js')
}
})
win.loadFile('index.html')
ipcMain.handle('dark-mode:toggle', () => {
if (nativeTheme.shouldUseDarkColors) {
nativeTheme.themeSource = 'light'
} else {
nativeTheme.themeSource = 'dark'
}
return nativeTheme.shouldUseDarkColors
})
ipcMain.handle('dark-mode:system', () => {
nativeTheme.themeSource = 'system'
})
}
app.whenReady().then(() => {
createWindow()
app.on('activate', () => {
if (BrowserWindow.getAllWindows().length === 0) {
createWindow()
}
})
})
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') {
app.quit()
}
})
运行效果如下(这个GIF有点慢,别介意):
CSS文件使用@media
媒体查询的prefers-color-scheme
来设置< body >
元素背景和文本颜色。然后就是用到了上一节教程用到的进程通信的知识。在main.js主进程里面通过nativeTheme.themeSource
来设置主题。nativeTheme.shouldUseDarkColors
返回一个布尔值,表示操作系统/Chromium当前是否启用了暗模式 , 如果你想修改这个值,你应该使用themeSource
。 nativeTheme.themeSource
可以有三个属性值:system
, light
和dark
。它用于覆盖和取代Chromium选择在内部主题使用的值。
版权声明
本文为[害恶细君]所创,转载请带上原文链接,感谢
https://blog.csdn.net/weixin_50216991/article/details/124264658
边栏推荐
- A brief explanation of golang's keyword "competence"
- Pit encountered using camera x_ When onpause, the camera is not released, resulting in a black screen when it comes back
- 深度学习环境搭建步骤—gpu
- Zero cost, zero foundation, build profitable film and television applet
- IIS数据转换问题16bit转24bit
- Speex Wiener filter and rewriting of hypergeometric distribution
- Main differences between go and PHP
- Lpc1768 optimization comparison of delay time and different levels
- The flyer realizes page Jump through routing routes
- RuntimeError: Providing a bool or integral fill value without setting the optional `dtype` or `out`
猜你喜欢
基于pytorch搭建GoogleNet神经网络用于花类识别
山东大学软件学院项目实训-创新实训-网络安全靶场实验平台(七)
Kubernetes入门到精通-KtConnect(全称Kubernetes Toolkit Connect)是一款基于Kubernetes环境用于提高本地测试联调效率的小工具。
Common SQL commands
Shanda Wangan shooting range experimental platform project - personal record (IV)
Build intelligent garbage classification applet based on Zero
2021-2022-2 ACM training team weekly Programming Competition (8) problem solution
Possible root causes include a too low setting for -Xss and illegal cyclic inheritance dependencies
【文本分类案例】(4) RNN、LSTM 电影评价倾向分类,附TensorFlow完整代码
山东大学软件学院项目实训-创新实训-网络安全靶场实验平台(六)
随机推荐
The flyer realizes page Jump through routing routes
Kubernetes getting started to proficient - install openelb on kubernetes
Zero cost, zero foundation, build profitable film and television applet
ESP8266-入门第一篇
@MapperScan与@Mapper
[H264] hevc H264 parsing and frame rate setting of the old version of libvlc
Distinction between pointer array and array pointer
指针数组与数组指针的区分
Garbage collector and memory allocation strategy
Kibana reports an error server is not ready yet. Possible causes
Scrum Patterns之理解各种团队模式
Strange passion
MySQL数据库 - 单表查询(三)
SQL of contention for system time plus time in ocrale database
【h264】libvlc 老版本的 hevc h264 解析,帧率设定
考试系统进入试卷优化思路
Why is PostgreSQL about to surpass SQL Server?
Openharmony open source developer growth plan, looking for new open source forces that change the world!
MySQL syntax collation (2)
Go recursively loops through folders