It is currently March 29th, 2024, 3:37 pm

Auto Changing Theme

Get help with creating, editing & fixing problems with skins
erenaa
Posts: 3
Joined: April 20th, 2021, 9:52 am

Auto Changing Theme

Post by erenaa »

I found a skin on the web. It gives me the option to change the theme dark or light. But I want it auto changes. I know windows' theme setting stored in "HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Themes\Personalize", but I cannot find a solution using registry measure.

Skin's theme.ini file:

Code: Select all

[Rainmeter]
ContextTitle=Light Theme
ContextAction=[!WriteKeyValue Variables BackgroundThemeName "Light" #VariablesFilePath#][!WriteKeyValue Variables ForegroundThemeName "Dark" #VariablesFilePath#][!WriteKeyValue Variables BackgroundColorName "White" #VariablesFilePath#][!WriteKeyValue Variables ForegroundColorName "Black" #VariablesFilePath#][!RefreshApp]
ContextTitle2=Dark Theme
ContextAction2=[!WriteKeyValue Variables BackgroundThemeName "Dark" #VariablesFilePath#][!WriteKeyValue Variables ForegroundThemeName "Light" #VariablesFilePath#][!WriteKeyValue Variables BackgroundColorName "Black" #VariablesFilePath#][!WriteKeyValue Variables ForegroundColorName "White" #VariablesFilePath#][!RefreshApp]


[Variables]
VariablesFilePath=""#@#variables.ini""
I tried to find a way to do it;
1-Tried Getting registry data -> I did
2-Tried Changing Dark-Light-Black-White -> I can't do that


PS: Sorry for my bad English
Last edited by balala on April 20th, 2021, 11:27 am, edited 1 time in total.
Reason: Please use <code> tags, not <Snippet>, whenever are posting code snippets. It's the </> button.
User avatar
SilverAzide
Rainmeter Sage
Posts: 2588
Joined: March 23rd, 2015, 5:26 pm

Re: Auto Changing Theme

Post by SilverAzide »

erenaa wrote: April 20th, 2021, 10:13 am I found a skin on the web. It gives me the option to change the theme dark or light. But I want it auto changes. I know windows' theme setting stored in "HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Themes\Personalize", but I cannot find a solution using registry measure.

I tried to find a way to do it;
1-Tried Getting registry data -> I did
2-Tried Changing Dark-Light-Black-White -> I can't do that

PS: Sorry for my bad English
Are you trying to change the skin's theme based on what the Windows light/dark theme setting is? Or are you trying to switch Windows themes using a Rainmeter skin?
Gadgets Wiki GitHub More Gadgets...
erenaa
Posts: 3
Joined: April 20th, 2021, 9:52 am

Re: Auto Changing Theme

Post by erenaa »

SilverAzide wrote: April 20th, 2021, 12:40 pm Are you trying to change the skin's theme based on what the Windows light/dark theme setting is? Or are you trying to switch Windows themes using a Rainmeter skin?
Changing skin's theme to windows' theme


Windows' theme will not change
User avatar
SilverAzide
Rainmeter Sage
Posts: 2588
Joined: March 23rd, 2015, 5:26 pm

Re: Auto Changing Theme

Post by SilverAzide »

erenaa wrote: April 20th, 2021, 12:48 pm Changing skin's theme to windows' theme

Windows' theme will not change
OK, it is possible to change the Windows theme too, but it should be fairly straightforward to have the skin change automatically. The code you posted is incomplete, but the basic idea is to read the registry setting you found and add "conditions" that will react to the value to adjust the skin accordingly.

You say you were able to read the registry to get the theme setting value. You can add code to that measure to react to the result. For example:

Code: Select all

; add a measure that will read the existing theme and convert the text value into a number you can use in logical expressions
[MeasureIsSkinLightTheme]
Measure=String
String=#BackgroundThemeName#
Substitute="Light":"1","Dark":"0"
UpdateDivider=-1

[MeasureIsWindowsLightTheme]
;
;... your existing code to read the value from HKCU\SOFTWARE\Microsoft\Windows\CurrentVersion\Themes\Personalize\AppsUseLightTheme or SystemUsesLightTheme (whichever you want to check), then add the following:
;
; if windows is set to light and skin is set to dark, switch skin to light
IfCondition=([MeasureIsWindowsLightTheme] = 1) && ([MeasureIsSkinLightTheme] = 0)
IfTrueAction=[!WriteKeyValue Variables BackgroundThemeName "Light" #VariablesFilePath#][!WriteKeyValue Variables ForegroundThemeName "Dark" #VariablesFilePath#][!WriteKeyValue Variables BackgroundColorName "White" #VariablesFilePath#][!WriteKeyValue Variables ForegroundColorName "Black" #VariablesFilePath#][!Refresh]
; if windows is set to dark and skin is set to light, switch skin to dark
IfCondition2=([MeasureIsWindowsLightTheme] = 0) && ([MeasureIsSkinLightTheme] = 1)
IfTrueAction2=[!WriteKeyValue Variables BackgroundThemeName "Dark" #VariablesFilePath#][!WriteKeyValue Variables ForegroundThemeName "Light" #VariablesFilePath#][!WriteKeyValue Variables BackgroundColorName "Black" #VariablesFilePath#][!WriteKeyValue Variables ForegroundColorName "White" #VariablesFilePath#][!Refresh]
DynamicVariables=1
Don't use [!RefreshApp] in your skin like in your snippet, that restarts Rainmeter and there is no need to do that. With a little extra effort, you can eliminate the [!Refresh] and have the skin switch themes on the fly without refreshing completely, but you can try that once you get the above working. Don't forget to remove the context menu items, as they will not work anymore.

I didn't try the above code, so you might need to fix things, but the basic logic is there...
Gadgets Wiki GitHub More Gadgets...
erenaa
Posts: 3
Joined: April 20th, 2021, 9:52 am

Re: Auto Changing Theme

Post by erenaa »

SilverAzide wrote: April 20th, 2021, 1:58 pm OK, it is possible to change the Windows theme too, but it should be fairly straightforward to have the skin change automatically. The code you posted is incomplete, but the basic idea is to read the registry setting you found and add "conditions" that will react to the value to adjust the skin accordingly.

You say you were able to read the registry to get the theme setting value. You can add code to that measure to react to the result. For example:

Code: Select all

; add a measure that will read the existing theme and convert the text value into a number you can use in logical expressions
[MeasureIsSkinLightTheme]
Measure=String
String=#BackgroundThemeName#
Substitute="Light":"1","Dark":"0"
UpdateDivider=-1

[MeasureIsWindowsLightTheme]
;
;... your existing code to read the value from HKCU\SOFTWARE\Microsoft\Windows\CurrentVersion\Themes\Personalize\AppsUseLightTheme or SystemUsesLightTheme (whichever you want to check), then add the following:
;
; if windows is set to light and skin is set to dark, switch skin to light
IfCondition=([MeasureIsWindowsLightTheme] = 1) && ([MeasureIsSkinLightTheme] = 0)
IfTrueAction=[!WriteKeyValue Variables BackgroundThemeName "Light" #VariablesFilePath#][!WriteKeyValue Variables ForegroundThemeName "Dark" #VariablesFilePath#][!WriteKeyValue Variables BackgroundColorName "White" #VariablesFilePath#][!WriteKeyValue Variables ForegroundColorName "Black" #VariablesFilePath#][!Refresh]
; if windows is set to dark and skin is set to light, switch skin to dark
IfCondition2=([MeasureIsWindowsLightTheme] = 0) && ([MeasureIsSkinLightTheme] = 1)
IfTrueAction2=[!WriteKeyValue Variables BackgroundThemeName "Dark" #VariablesFilePath#][!WriteKeyValue Variables ForegroundThemeName "Light" #VariablesFilePath#][!WriteKeyValue Variables BackgroundColorName "Black" #VariablesFilePath#][!WriteKeyValue Variables ForegroundColorName "White" #VariablesFilePath#][!Refresh]
DynamicVariables=1
Don't use [!RefreshApp] in your skin like in your snippet, that restarts Rainmeter and there is no need to do that. With a little extra effort, you can eliminate the [!Refresh] and have the skin switch themes on the fly without refreshing completely, but you can try that once you get the above working. Don't forget to remove the context menu items, as they will not work anymore.

I didn't try the above code, so you might need to fix things, but the basic logic is there...
Thanks A LOT.
I spent my 2 hr for this. Wish I wrote to the forum before trying to fix it.
User avatar
balala
Rainmeter Sage
Posts: 16110
Joined: October 11th, 2010, 6:27 pm
Location: Gheorgheni, Romania

Re: Auto Changing Theme

Post by balala »

Even if not exactly on subject of this topic, but there is something which has to be tell.
SilverAzide probably found the solution and there is nothing wrong with his solution. However what am I concerned about is the missing of quotations around the #VariablesFilePath# variable, within the !WriteKeyValue bangs. I'd definitely add those quotes, replacing for instance the first !WriteKeyValue bang of the IfTrueAction option of the [MeasureIsWindowsLightTheme] measure (originally has been [!WriteKeyValue Variables BackgroundThemeName "Light" #VariablesFilePath#]) with this: [!WriteKeyValue Variables BackgroundThemeName "Light" "#VariablesFilePath#"]. Why those quotes are so important is that they are not, unless the path contains spaces. If there are no spaces, everythin gis ok, but if there is a space (or more), the writing to the appropriate file will fail. Quoting the path avoid this inconvenient: no matter if there is / are space(s), the writing will take place.
The explanation of why this is going on, is that if there is no space, the whole path is accepted to be the parameter (the path of the file where the value has to be written). But if there is one single space (and especially if there are more), Rainmeter will want to write to the file indicated by the the path starting from the beginning of the fourth parameter, until the first space contained inside. But this is not the whole path, it's just part of it. Quotes indicat Rainmeter which is the whole path.
Even if in this case there are no spaces and the writing is working well, it seems to be a good habit to get used to use those quotes, to never forget them. Doesn't hurt, but in some circumstances, they can avoid headache.