当前位置:网站首页>Introduction to electron Tutorial 4 - switching application topics
Introduction to electron Tutorial 4 - switching application topics
2022-04-23 19:45:00 【Harm the evil king】
From this issue on , I will continue to summarize some common function cases of desktop applications , Then there are similar needs, just draw inferences from one instance . In this section, learn how to switch the theme of the application .
If you want to manually turn on / Switching between dark modes , You can use the nativeTheme
Modular themeSource
Property to do this . The value of this attribute is propagated to your rendering process . Anything to do with prefers-color-scheme
dependent CSS The rules will be updated accordingly .
On the first code , And we'll analyze it later :
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title> Topic switching </title>
<link rel="stylesheet" type="text/css" href="./style.css">
</head>
<body>
<p> The current topic is :<strong id="theme-source"> System theme </strong></p>
<button id="toggle"> Switch to dark mode </button>
<button id="reset-to-system"> Switch system theme </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 ? ' Dark theme ' : ' Light color theme '
document.getElementById('toggle').innerHTML= isDarkMode ? ' Switch to light theme ' : ' Switch to dark theme '
})
document.getElementById('reset-to-system').addEventListener('click', async () => {
await window.darkMode.system()
document.getElementById('theme-source').innerHTML = ' System theme '
})
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()
}
})
The operation effect is as follows ( This GIF It's a little slow , Don't you mind? ):
CSS Files use @media
The media inquires about prefers-color-scheme
To set up < body >
Element background and text color . Then we use the knowledge of process communication used in the previous tutorial . stay main.js In the main process nativeTheme.themeSource
To set the theme .nativeTheme.shouldUseDarkColors
Returns a Boolean value , Represents the operating system /Chromium Whether dark mode is currently enabled , If you want to change this value , You should use themeSource
. nativeTheme.themeSource
You can have three attribute values :system
, light
and dark
. It is used to cover and replace Chromium Select the value to use in the internal theme .
版权声明
本文为[Harm the evil king]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231940533324.html
边栏推荐
- Kubernetes introduction to mastery - ktconnect (full name: kubernetes toolkit connect) is a small tool based on kubernetes environment to improve the efficiency of local test joint debugging.
- Go three ways to copy files
- How about Bohai futures. Is it safe to open futures accounts?
- Summary of several relationships of UML class diagram
- Data analysis learning directory
- 对普通bean进行Autowired字段注入
- Lottery applet, mother no longer have to worry about who does the dishes (assign tasks), so easy
- Audio editing generation software
- Esp8266 - beginner level Chapter 1
- Speculation on the way to realize the smooth drag preview of video editing software
猜你喜欢
@MapperScan与@Mapper
Garbage collector and memory allocation strategy
山东大学软件学院项目实训-创新实训-网络安全靶场实验平台(七)
An idea of rendering pipeline based on FBO
【webrtc】Add x264 encoder for CEF/Chromium
5 minutes to achieve wechat cloud applet payment function (including source code)
The textarea cursor cannot be controlled by the keyboard due to antd dropdown + modal + textarea
Deep learning -- Summary of Feature Engineering
MySQL syntax collation (5) -- functions, stored procedures and triggers
Project training of Software College of Shandong University - Innovation Training - network security shooting range experimental platform (VII)
随机推荐
Electron入门教程3 ——进程通信
LPC1768 关于延时Delay时间与不同等级的优化对比
[H264] hevc H264 parsing and frame rate setting of the old version of libvlc
First experience of using fluent canvas
Possible root causes include a too low setting for -Xss and illegal cyclic inheritance dependencies
对普通bean进行Autowired字段注入
Openharmony open source developer growth plan, looking for new open source forces that change the world!
Kubernetes getting started to proficient - install openelb on kubernetes
[report] Microsoft: application of deep learning methods in speech enhancement
MySQL数据库 - 数据库和表的基本操作(二)
OpenHarmony开源开发者成长计划,寻找改变世界的开源新生力!
An idea of rendering pipeline based on FBO
DevOps集成-Jenkins 服务的环境变量和构建工具 Tools
Application of DCT transform
MySQL syntax collation (3)
Easy mock local deployment (you need to experience three times in a crowded time. Li Zao will do the same as me. Love is like a festival mock)
Electron入门教程4 —— 切换应用的主题
MySQL数据库 - 单表查询(二)
Why is the hexadecimal printf output of C language sometimes with 0xff and sometimes not
A simple (redisson based) distributed synchronization tool class encapsulation