当前位置:网站首页>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
边栏推荐
- Summary of several relationships of UML class diagram
- @Mapperscan and @ mapper
- Video understanding
- VeraCrypt文件硬盘加密使用教程
- An algorithm problem was encountered during the interview_ Find the mirrored word pairs in the dictionary
- Understanding various team patterns in scrum patterns
- Why is the hexadecimal printf output of C language sometimes with 0xff and sometimes not
- Lottery applet, mother no longer have to worry about who does the dishes (assign tasks), so easy
- Decompile and get the source code of any wechat applet - just read this (latest)
- MySQL syntax collation (3)
猜你喜欢
【webrtc】Add x264 encoder for CEF/Chromium
Decompile and get the source code of any wechat applet - just read this (latest)
JVM的类加载过程
Distinction between pointer array and array pointer
Virtual machine performance monitoring and fault handling tools
Pdf reference learning notes
Class loading process of JVM
Is meituan, a profit-making company with zero foundation, hungry? Coupon CPS applet (with source code)
php参考手册String(7.2千字)
The textarea cursor cannot be controlled by the keyboard due to antd dropdown + modal + textarea
随机推荐
5 minutes to achieve wechat cloud applet payment function (including source code)
Building googlenet neural network based on pytorch for flower recognition
山东大学软件学院项目实训-创新实训-网络安全靶场实验平台(七)
Possible root causes include a too low setting for -Xss and illegal cyclic inheritance dependencies
Hot reload debugging
Steps to build a deep learning environment GPU
An example of using JNI to directly access surface data
The textarea cursor cannot be controlled by the keyboard due to antd dropdown + modal + textarea
Command - sudo
How to select the third-party package of golang
Go recursively loops through folders
An idea of rendering pipeline based on FBO
Zero cost, zero foundation, build profitable film and television applet
Deep learning -- Summary of Feature Engineering
深度学习——特征工程小总结
Redis core technology and practice 1 - start with building a simple key value database simplekv
Is meituan, a profit-making company with zero foundation, hungry? Coupon CPS applet (with source code)
goroutine
Possible root causes include a too low setting for -Xss and illegal cyclic inheritance dependencies
Shanda Wangan shooting range experimental platform project - personal record (V)