当前位置:网站首页>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
边栏推荐
- Design of warehouse management database system
- MySQL数据库 - 数据库和表的基本操作(二)
- 基于pytorch搭建GoogleNet神经网络用于花类识别
- Application of DCT transform
- Kubernetes entry to mastery - bare metal loadbalance 80 443 port exposure precautions
- Speculation on the way to realize the smooth drag preview of video editing software
- 数据库查询 - 选课系统
- Esp8266 - beginner level Chapter 1
- 如何在BNB链上创建BEP-20通证
- Summary of several relationships of UML class diagram
猜你喜欢
如何在BNB链上创建BEP-20通证
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.
如何在BNB鏈上創建BEP-20通證
Application of DCT transform
MySQL syntax collation (2)
C6748 software simulation and hardware test - with detailed FFT hardware measurement time
An algorithm problem was encountered during the interview_ Find the mirrored word pairs in the dictionary
【webrtc】Add x264 encoder for CEF/Chromium
MySQL syntax collation (5) -- functions, stored procedures and triggers
Zero base to build profit taking away CPS platform official account
随机推荐
MySQL syntax collation (5) -- functions, stored procedures and triggers
【2022】将3D目标检测看作序列预测-Point2Seq: Detecting 3D Objects as Sequences
Design of library management database system
The usage of slice and the difference between slice and array
An example of using JNI to directly access surface data
How to create bep-20 pass on BNB chain
RuntimeError: Providing a bool or integral fill value without setting the optional `dtype` or `out`
Use test of FFT and IFFT library functions of TI DSP
@MapperScan与@Mapper
山东大学软件学院项目实训-创新实训-网络安全靶场实验平台(八)
渤海期货这家公司怎么样。期货开户办理安全?
Scrum Patterns之理解各种团队模式
图书管理数据库系统设计
A brief explanation of golang's keyword "competence"
Understanding various team patterns in scrum patterns
How to use go code to compile Pb generated by proto file with protoc Compiler Go file
仓库管理数据库系统设计
uIP1.0 主动发送的问题理解
TI DSP的 FFT与IFFT库函数的使用测试
Unity创建超写实三维场景的一般步骤