简体   繁体   English

如何在 Electron 应用程序的两个 windows 上放置主题(暗/亮模式)?

[英]How I can put a theme (dark/light mode) on my two windows of Electron app?

Hello guys , I have a problem... I have created an Electron app with two windows (Home and Settings) on the Home Window I have a button where I can change the theme(Dark/Light) , I do this with css variables and a little Javascript script (In the html file the class define the theme). Hello guys , I have a problem... I have created an Electron app with two windows (Home and Settings) on the Home Window I have a button where I can change the theme(Dark/Light) , I do this with css variables和一点 Javascript 脚本(在 html 文件中 class 定义主题)。 My question is how I can apply my theme also on my settings window?我的问题是如何在我的设置 window 上应用我的主题? (Does I need to do a link between the two files?) I tried many things but without success:/ (我需要在两个文件之间建立链接吗?)我尝试了很多东西但没有成功:/


I post the main code to help you to understand my problem:我发布主要代码以帮助您理解我的问题:

Html: Html:

<html lang="en" class="theme-dark" id="fortheme">

Css: Css:

.theme-light {
  --color-primary: #dedad4;
  --color-secondary: #d7d3cb;
  --color-border: #c1beb9;
  --color-accent1: #d52015;
  --color-accent2: #2196f3;
  --color-accent3: #4caf50;
  --font-color: #070b0b;
}
.theme-dark {
  --color-primary: #21252b;
  --color-secondary: #282c34;
  --color-border: #3e4146;
  --color-accent1: #d52015;
  --color-accent2: #2196f3;
  --color-accent3: #4caf50;
  --font-color: #f8f4f4;
}

Javascript: Javascript:

//Change pictures (picture of the button) and theme
$('#light-btn').on({
    'click': function () {
        image = $("#light-image")
        if (image.attr("src") == "Images/Sun.png") {
            image.attr("src", "Images/Dark.png")
            setTheme('theme-light');
            $("img").css({ filter: "invert(100%)" })
        } else {
            image.attr("src", "Images/Sun.png")
            setTheme('theme-dark');
            $("img").css({ filter: "invert(0%)" })

        }

    }
});

Screenshot of the app to help you to understand:应用截图帮助你理解:

Light mode: (the settings window don't have theme applyed)灯光模式:(设置 window 没有应用主题) 光

Dark mode:黑暗模式: 黑暗的

Thanks a lot for your help !非常感谢你的帮助 !

There are probably more than a few ways to handle this.可能有不止几种方法可以处理这个问题。 Personally I would handle it in the main context so that the user preference can be saved and restored on subsequent launches.就我个人而言,我会在main上下文中处理它,以便在后续启动时可以保存和恢复用户偏好。

So the schema would be something like this:所以架构会是这样的:

While it is possible for windows to communicate with each other directly, this schema will give you more flexibility if you decide to add more windows or other "themes" in the future.虽然 windows 可以直接相互通信,但如果您决定在未来添加更多 windows 或其他“主题”,此架构将为您提供更大的灵活性。 It's a little more work but lets the windows be "dumb" with control residing in main .这是一个多一点的工作,但让 windows 变得“愚蠢”,控制位于main

. . . . . . but I could also be doing it wrong.但我也可能做错了。 ;-) ;-)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在使用 header 作为开关位置的同时将我的亮/暗模式主题包裹在 index.js 周围? - How can I wrap my light/dark mode theme around index.js while using my header as the switch location? 如何使用 localStorage 使浏览器记住亮/暗模式主题切换的类列表切换状态? - How can I use localStorage to make the browser remember a classlist toggle state for a light/dark mode theme switch? 如何保存 cookies 以进行暗模式和亮模式切换? - How can I save cookies for dark mode and light mode toggling? 如何在深色模式下显示浅色徽标? - How can I show light logo in dark mode? 如何强制更新我的反应代码中的组件以将深色主题切换为浅色主题 - How do I force Update my components in my react code to toggle dark theme to light theme 我的网站如何检测浏览器是否使用深色/浅色主题而不是操作系统是否使用主题 - How can my website detect if a browser is using a dark/light theme NOT if the OS is using a theme 如何在 vuetify 的浅色主题中使用自定义颜色启用深色模式? - How to enable dark mode with custom colors in light theme in vuetify? React:我怎样才能为明暗模式动态更改整个背景? Redux 持有我的黑暗模式 state - React: How can I dynamically change the entire background dynamically for light and dark mode? Redux holds my darkMode state 如何为深色/浅色主题提供两个版本的图像? - How to have two versions of images for dark/light theme? 如何使用 div 和 JS 在暗/亮模式之间切换? - How can I toggle between dark/light mode using a div & JS?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM