init dark mode

This commit is contained in:
layyback 2023-03-26 19:35:07 +08:00
parent 958e20fef9
commit 9e2b6f52a4
4 changed files with 53 additions and 9 deletions

View File

@ -1,7 +1,7 @@
import commonConst from "@/common/utils/commonConst"; import commonConst from "@/common/utils/commonConst";
export default { export default {
version: 2, version: 4,
perf: { perf: {
shortCut: { shortCut: {
showAndHidden: "Option+R", showAndHidden: "Option+R",
@ -14,6 +14,7 @@ export default {
// 是否失焦隐藏。默认在dev环境不隐藏在打包后隐藏。 // 是否失焦隐藏。默认在dev环境不隐藏在打包后隐藏。
hideOnBlur: commonConst.production(), hideOnBlur: commonConst.production(),
autoPast: false, autoPast: false,
darkMode: false
}, },
local: { local: {
search: true, search: true,

View File

@ -3,7 +3,7 @@ import path from "path";
import commonConst from "../../common/utils/commonConst"; import commonConst from "../../common/utils/commonConst";
import { PLUGIN_INSTALL_DIR as baseDir } from "@/common/constans/main"; import { PLUGIN_INSTALL_DIR as baseDir } from "@/common/constans/main";
const getRelativePath = (indexPath) => { const getRelativePath = indexPath => {
return commonConst.windows() return commonConst.windows()
? indexPath.replace("file://", "") ? indexPath.replace("file://", "")
: indexPath.replace("file:", ""); : indexPath.replace("file:", "");
@ -38,6 +38,7 @@ export default () => {
const createView = (plugin, window: BrowserWindow) => { const createView = (plugin, window: BrowserWindow) => {
let pluginIndexPath = plugin.tplPath || plugin.indexPath; let pluginIndexPath = plugin.tplPath || plugin.indexPath;
let darkMode;
if (!pluginIndexPath) { if (!pluginIndexPath) {
const pluginPath = path.resolve(baseDir, "node_modules", plugin.name); const pluginPath = path.resolve(baseDir, "node_modules", plugin.name);
pluginIndexPath = `file://${path.join(pluginPath, "./", plugin.main)}`; pluginIndexPath = `file://${path.join(pluginPath, "./", plugin.main)}`;
@ -56,8 +57,8 @@ export default () => {
devTools: true, devTools: true,
webviewTag: true, webviewTag: true,
preload, preload,
session: ses, session: ses
}, }
}); });
window.setBrowserView(view); window.setBrowserView(view);
view.webContents.loadURL(pluginIndexPath); view.webContents.loadURL(pluginIndexPath);
@ -67,13 +68,18 @@ export default () => {
view.setAutoResize({ width: true }); view.setAutoResize({ width: true });
executeHooks("PluginEnter", plugin.ext); executeHooks("PluginEnter", plugin.ext);
executeHooks("PluginReady", plugin.ext); executeHooks("PluginReady", plugin.ext);
darkMode = global.OP_CONFIG.get().perf.common.darkMode;
darkMode &&
view.webContents.executeJavaScript(
`document.body.classList.add("dark");window.rubick.theme="dark"`
);
window.webContents.executeJavaScript(`window.pluginLoaded()`); window.webContents.executeJavaScript(`window.pluginLoaded()`);
}); });
// 修复请求跨域问题 // 修复请求跨域问题
view.webContents.session.webRequest.onBeforeSendHeaders( view.webContents.session.webRequest.onBeforeSendHeaders(
(details, callback) => { (details, callback) => {
callback({ callback({
requestHeaders: { referer: "*", ...details.requestHeaders }, requestHeaders: { referer: "*", ...details.requestHeaders }
}); });
} }
); );
@ -83,8 +89,8 @@ export default () => {
callback({ callback({
responseHeaders: { responseHeaders: {
"Access-Control-Allow-Origin": ["*"], "Access-Control-Allow-Origin": ["*"],
...details.responseHeaders, ...details.responseHeaders
}, }
}); });
} }
); );
@ -116,6 +122,6 @@ export default () => {
init, init,
getView, getView,
removeView, removeView,
executeHooks, executeHooks
}; };
}; };

View File

@ -1,4 +1,12 @@
import { globalShortcut, BrowserWindow, screen, ipcMain, app } from "electron"; import {
globalShortcut,
nativeTheme,
BrowserWindow,
BrowserView,
screen,
ipcMain,
app
} from "electron";
const registerHotKey = (mainWindow: BrowserWindow): void => { const registerHotKey = (mainWindow: BrowserWindow): void => {
// 设置开机启动 // 设置开机启动
@ -9,9 +17,36 @@ const registerHotKey = (mainWindow: BrowserWindow): void => {
openAsHidden: true openAsHidden: true
}); });
}; };
// 设置暗黑模式
const setDarkMode = () => {
const config = global.OP_CONFIG.get();
const isDark = config.perf.common.darkMode;
if (isDark) {
nativeTheme.themeSource = "dark";
mainWindow.webContents.executeJavaScript(
`document.body.classList.add("dark");window.rubick.theme="dark"`
);
mainWindow.getBrowserViews().forEach((view: BrowserView) => {
view.webContents.executeJavaScript(
`document.body.classList.add("dark");window.rubick.theme="dark"`
);
});
} else {
nativeTheme.themeSource = "light";
mainWindow.webContents.executeJavaScript(
`document.body.classList.remove("dark");window.rubick.theme="light"`
);
mainWindow.getBrowserViews().forEach((view: BrowserView) => {
view.webContents.executeJavaScript(
`document.body.classList.remove("dark");window.rubick.theme="light"`
);
});
}
};
const init = () => { const init = () => {
setAutoLogin(); setAutoLogin();
setDarkMode();
const config = global.OP_CONFIG.get(); const config = global.OP_CONFIG.get();
globalShortcut.unregisterAll(); globalShortcut.unregisterAll();
// 注册偏好快捷键 // 注册偏好快捷键

View File

@ -119,6 +119,7 @@ const clearSearchValue = () => {
</script> </script>
<style lang="less"> <style lang="less">
@import "./assets/var.less";
.drag-bar { .drag-bar {
-webkit-app-region: drag; -webkit-app-region: drag;
width: 100%; width: 100%;
@ -131,6 +132,7 @@ const clearSearchValue = () => {
#components-layout { #components-layout {
height: 100vh; height: 100vh;
overflow: hidden; overflow: hidden;
background: var(--color-body-bg);
::-webkit-scrollbar { ::-webkit-scrollbar {
width: 0; width: 0;
} }