当前位置:网站首页>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
边栏推荐
- 图书管理数据库系统设计
- Mysql database - basic operation of database and table (II)
- Deep learning -- Summary of Feature Engineering
- Inject Autowired fields into ordinary beans
- Data analysis learning directory
- MySQL practical skills
- Steps to build a deep learning environment GPU
- No, some people can't do the National Day avatar applet (you can open the traffic master and earn pocket money)
- Shanda Wangan shooting range experimental platform project - personal record (V)
- C学习完结
猜你喜欢

山大网安靶场实验平台项目—个人记录(四)

山东大学软件学院项目实训-创新实训-网络安全靶场实验平台(七)

Pdf reference learning notes

ESP8266-入门第一篇

First experience of using fluent canvas

Is meituan, a profit-making company with zero foundation, hungry? Coupon CPS applet (with source code)

Comment créer un pass BEP - 20 sur la chaîne BNB

MySQL syntax collation (3)

Intuitive understanding of the essence of two-dimensional rotation

PHP reference manual string (7.2000 words)
随机推荐
MySQL lock
Understanding various team patterns in scrum patterns
Is meituan, a profit-making company with zero foundation, hungry? Coupon CPS applet (with source code)
Golang timer
2021-2022-2 ACM training team weekly Programming Competition (8) problem solution
图书管理数据库系统设计
The usage of slice and the difference between slice and array
MySQL syntax collation (3)
uIP1.0 主动发送的问题理解
First experience of using fluent canvas
Go three ways to copy files
JVM的类加载过程
Possible root causes include a too low setting for -Xss and illegal cyclic inheritance dependencies
Speex Wiener filter and rewriting of hypergeometric distribution
Go recursively loops through folders
An example of using JNI to directly access surface data
Using oes texture + glsurfaceview + JNI to realize player picture processing based on OpenGL es
Project training of Software College of Shandong University - Innovation Training - network security shooting range experimental platform (V)
Web Security
IIS data conversion problem: 16bit to 24bit