It is currently March 28th, 2024, 4:05 pm

Toggle between Power Options and variable meter image

Get help with creating, editing & fixing problems with skins
soapyb
Posts: 5
Joined: December 3rd, 2017, 6:35 am

Toggle between Power Options and variable meter image

Post by soapyb »

Hey all

I'm fairly new to Rainmeter, and I have some experience coding so I've been trying to learn what I can to do this myself. I've spent a fair amount of time today trying to figure out how to do this but I'm not coming up with a proper solution. Hope you can help or point me in the right direction.

What I'd like to do, is have a clickable meter image that will toggle through power options using LeftMouseUpAction. I found a script that will change the meter image when I toggle "High Performance" and "Balanced" power options, but I have to toggle these options manually within Windows. Here's that bit of code:

Code: Select all

.
.
[Variables]
high=neon-mountains-bottom-power-HIGH.png
balance=neon-mountains-bottom-power-ON.png
Back=[MeasureActivePower]
...
[MeasureActivePower]
Measure=Registry
RegHKey=HKEY_LOCAL_MACHINE
RegKey=SYSTEM\ControlSet001\Control\Power\User\PowerSchemes
RegValue=ActivePowerScheme
Substitute="8c5e7fda-e8bf-4a96-9a85-a6e23a8c635c":"#high#","381b4222-f694-41f0-9685-ff5bb260df2e":"#balance#"
DynamicVariables=1
.
.
I know that I can use LeftMouseUpAction to set a single power option (say High Performance) on click, but is there any way to have a second option (Balanced) available? I've seen that conditionals like IF are only available for Measures; I tried coming up with a way to use that but I couldn't get anything to work. I was thinking to use a variable somehow, but I can't find documentation on how to use a variable with a !Execute command.

Additionally...would there be a way to toggle between THREE options? Ideally, I'd like to be able to toggle between High Performance -> Balanced -> Power Saver, but I'll settle for just two if I have to.

Thanks in advance.
User avatar
balala
Rainmeter Sage
Posts: 16110
Joined: October 11th, 2010, 6:27 pm
Location: Gheorgheni, Romania

Re: Toggle between Power Options and variable meter image

Post by balala »

soapyb wrote:I was thinking to use a variable somehow, but I can't find documentation on how to use a variable with a !Execute command.

Additionally...would there be a way to toggle between THREE options? Ideally, I'd like to be able to toggle between High Performance -> Balanced -> Power Saver, but I'll settle for just two if I have to.
Good idea to use a variable, this is exactly what you need.
Let's start by defining one in the [Variables] section. Let's name it Power (obviously you can use any name you'd like):

Code: Select all

[Variables]
Power=0
Furtherly you need a Calc measure, where you can add the IfConditions, to set the appropriate power scheme, accordingly to the value of the Power variable:

Code: Select all

[MeasurePower]
Measure=Calc
Formula=#Power#
IfCondition=(#CURRENTSECTION#=0)
IfTrueAction=ADD HERE THE COMMAND TO SET THE HIGH PERFORMANCE POWER SCHEME
IfCondition2=(#CURRENTSECTION#=1)
IfTrueAction2=ADD HERE THE COMMAND TO SET THE HIGH BALANCED SCHEME
IfCondition3=(#CURRENTSECTION#=2)
IfTrueAction3=ADD HERE THE COMMAND TO SET THE POWER SAVER POWER SCHEME
DynamicVariables=1
And you need just one more action: setting the LeftMouseUpAction option of the Image meter, to change the value of the Power variable. Add the following option to the appropriate Image meter: LeftMouseUpAction=[!Setvariable Power "((#Power#+1)%3)"][!UpdateMeasure "MeasurePower"][!UpdateMeter #CURRENTSECTION#]. This option will set the value of the Power variable from 0 to 1, from 1 to 2, then from 2 to 0 again.
Obviously with this method you can set so many schemes as many you want, just have to modify the %3 section of the formula and add the appropriate IfConditionX / IfTrueActionX options. Eg for %4, you can set up to 4 power schemes and so on.

About how can you set the appropriate power scheme: I think you have to set the appropriate power scheme using the PowerCfg command, which sets certain power schemes by their "identifier" (probably this isn't the best name, but I don't know how they could be named). Eg the identifier of the High Performance scheme is 8c5e7fda-e8bf-4a96-9a85-a6e23a8c635c, while the identifier of the Balanced scheme is 381b4222-f694-41f0-9685-ff5bb260df2e (you also have used them in your code). To set them , try to modify the following options of the [MeasurePower] measure (leave the not-posted options as they are):

Code: Select all

[MeasurePower]
...
IfTrueAction=[POWERCFG /SETACTIVE 8c5e7fda-e8bf-4a96-9a85-a6e23a8c635c]
...
IfTrueAction2=[POWERCFG /SETACTIVE 381b4222-f694-41f0-9685-ff5bb260df2e]
...
IfTrueAction3=[POWERCFG /SETACTIVE a1841308-3541-4fab-bc81-f71556f20b4a]
You have to check the last identifier, I'm not sure if I posted the right one.

Please let me know if you could handle all these. If not, please post the whole code you have so far and I'll add the needed options, to make it to work properly.
soapyb
Posts: 5
Joined: December 3rd, 2017, 6:35 am

Re: Toggle between Power Options and variable meter image

Post by soapyb »

Hey balala, thanks for the reply!

The power option "identifiers" you listed are correct.

I did what you suggested but I still can't click on the image to cycle through power modes. I had it working last night before I posted on here, but that was when I had it as a single action, so it's not really helpful in this situation. I was really trying to do this myself but it doesn't seem to be panning out, I'm sure it's something really easy that I'm overlooking. Here's my full code if you can get the time to look at it:

Code: Select all

[Rainmeter]
Update=1000


[Variables]
high=neon-mountains-bottom-power-HIGH.png
balance=neon-mountains-bottom-power-ON.png
saver=neon-mountains-bottom-power-saver.png
Back=[MeasureActivePower]
Power=0

[MeasureActivePower]
Measure=Registry
RegHKey=HKEY_LOCAL_MACHINE
RegKey=SYSTEM\ControlSet001\Control\Power\User\PowerSchemes
RegValue=ActivePowerScheme
Substitute="8c5e7fda-e8bf-4a96-9a85-a6e23a8c635c":"#high#","381b4222-f694-41f0-9685-ff5bb260df2e":"#balance#","a1841308-3541-4fab-bc81-f71556f20b4a":"#saver#"
DynamicVariables=1

[MeasurePower]
Measure=Calc
Formula=#Power#
IfCondition=(#CURRENTSECTION#=0)
IfTrueAction=[POWERCFG /SETACTIVE 8c5e7fda-e8bf-4a96-9a85-a6e23a8c635c]
IfCondition2=(#CURRENTSECTION#=1)
IfTrueAction2=[POWERCFG /SETACTIVE 381b4222-f694-41f0-9685-ff5bb260df2e]
IfCondition3=(#CURRENTSECTION#=2)
IfTrueAction3=[POWERCFG /SETACTIVE a1841308-3541-4fab-bc81-f71556f20b4a]
DynamicVariables=1


[BatteryMeter]
Meter=Image
ImageName=#Back#
W=1920
H=495
LeftMouseUpAction=[!Setvariable Power "((#Power#+1)%3)"][!UpdateMeasure "MeasurePower"][!UpdateMeter #CURRENTSECTION#]
DynamicVariables=1
User avatar
balala
Rainmeter Sage
Posts: 16110
Joined: October 11th, 2010, 6:27 pm
Location: Gheorgheni, Romania

Re: Toggle between Power Options and variable meter image

Post by balala »

soapyb wrote:I did what you suggested but I still can't click on the image to cycle through power modes.
Are you sure? Because the LeftMouseUpAction set to the [BatteryMeter] of the posted code should work properly and it even does for me. I don't see anything what could make it to not work.
The only thing which is weird (but this can't cause the image meter to not work), is the Back=[MeasureActivePower] variable definition, within the [Variables] section. A such kind of definition usually would require to set the dynamic variables, but they are not supported into the [Variables] section. But you don't even have to use a such variable:

Code: Select all

[Rainmeter]
Update=1000

[Variables]
high=neon-mountains-bottom-power-HIGH.png
balance=neon-mountains-bottom-power-ON.png
saver=neon-mountains-bottom-power-saver.png
Power=0

[MeasureActivePower]
Measure=Registry
RegHKey=HKEY_LOCAL_MACHINE
RegKey=SYSTEM\ControlSet001\Control\Power\User\PowerSchemes
RegValue=ActivePowerScheme
Substitute="8c5e7fda-e8bf-4a96-9a85-a6e23a8c635c":"#high#","381b4222-f694-41f0-9685-ff5bb260df2e":"#balance#","a1841308-3541-4fab-bc81-f71556f20b4a":"#saver#"
DynamicVariables=1

[MeasurePower]
Measure=Calc
Formula=#Power#
IfCondition=(#CURRENTSECTION#=0)
IfTrueAction=[POWERCFG /SETACTIVE 8c5e7fda-e8bf-4a96-9a85-a6e23a8c635c]
IfCondition2=(#CURRENTSECTION#=1)
IfTrueAction2=[POWERCFG /SETACTIVE 381b4222-f694-41f0-9685-ff5bb260df2e]
IfCondition3=(#CURRENTSECTION#=2)
IfTrueAction3=[POWERCFG /SETACTIVE a1841308-3541-4fab-bc81-f71556f20b4a]
DynamicVariables=1

[BatteryMeter]
Meter=Image
MeasureName=MeasureActivePower
W=1920
H=495
LeftMouseUpAction=[!Setvariable Power "((#Power#+1)%3)"][!UpdateMeasure "MeasurePower"][!UpdateMeter #CURRENTSECTION#]
DynamicVariables=1
Here I completely removed the Back variable and replaced the ImageName option of the [BatteryMeter] meter with a MeasureName option. Since the [MeasureActivePower] measure returns an image name (through the Substitute option), the image meter will use the name of the image, returned by this measure.
If it still doesn't work (properly), please pack the config and upload it, to can check.
User avatar
jsmorley
Developer
Posts: 22628
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: Toggle between Power Options and variable meter image

Post by jsmorley »

Just as a note:

You can use [SectionVariables] in the definition of variable values.

[Variables]
SomeVar=[SomeMeasure]

While it is true that getting the current value of [SomeMeasure] will require DynamicVariables, and that is not supported in the [Variables] section, that's fine. DynamicVariables=1 would be required where you "use" the variable #SomeVar#. There is no need for it in [Variables].

The value of the variable SomeVar is not a section variable, it is not a reference to SomeMeasure, and it is not the current value of SomeMeasure. All variables are just strings. The value of #SomeVar# is simply the text "[SomeMeasure]", and will only be treated as a section variable and resolved for the current value when it is used in some meter or measure that has DynamicVariables=1 on it.

For example:

[Variables]
Var1=5
Var2=(#Var1# + 1)

The value of Var1 is "5". The value of Var2 is not "6". It is literally "(#Var1# + 1)" as a string. However, if you use the variable #Var2# in a meter or measure option that supports formulas, (most do) the result of #Var2# will be seen as the number 6.

[MeasureCalc]
Measure=Calc
Formula=#Var2#

So the long and the short of it is that DynamicVariables=1 is not supported in [Variables] because it is both unneeded and pointless there. Variables are never in any way resolved in the [Variables] section, where they are defined. They are resolved in the meters or measures in which they are used.
soapyb
Posts: 5
Joined: December 3rd, 2017, 6:35 am

Re: Toggle between Power Options and variable meter image

Post by soapyb »

balala wrote:Here I completely removed the Back variable and replaced the ImageName option of the [BatteryMeter] meter with a MeasureName option.
Initially I edited my code to do what you said here, and it still did not work. As far as I could tell, it looked exactly like the code you gave me, and it didn't work. After that, I copied your code and replaced mine, and it worked perfectly. I have no idea what I had different that wasn't letting it work but it works like a charm now! Exactly how I wanted!! Thanks a ton!! When I get some time to record the transitions I'll do it so you can see the result!

Quick follow up question, not a big issue...but I notice that every time I switch power modes, a powercfg window briefly pops up. Of course this is necessary, but is there any way to hide the window somehow?
User avatar
balala
Rainmeter Sage
Posts: 16110
Joined: October 11th, 2010, 6:27 pm
Location: Gheorgheni, Romania

Re: Toggle between Power Options and variable meter image

Post by balala »

soapyb wrote:Quick follow up question, not a big issue...but I notice that every time I switch power modes, a powercfg window briefly pops up. Of course this is necessary, but is there any way to hide the window somehow?
Probably using the RunCommand plugin, which can run hidden such commands. I'll take a look a bit later and hope will come back with a code modification, to hide that window pop up.
User avatar
balala
Rainmeter Sage
Posts: 16110
Joined: October 11th, 2010, 6:27 pm
Location: Gheorgheni, Romania

Re: Toggle between Power Options and variable meter image

Post by balala »

Here is the solution: add the following measure to your code:

Code: Select all

[MeasureRun]
Measure=Plugin
Plugin=RunCommand
State=Hide
and replace the IfTrueAction options of the [MeasurePower] measure as it follows:

Code: Select all

[MeasurePower]
...
IfTrueAction=[!SetOption MeasureRun Parameter "POWERCFG /SETACTIVE 8c5e7fda-e8bf-4a96-9a85-a6e23a8c635c"][!CommandMeasure "MeasureRun" "Run"]
...
IfTrueAction2=[!SetOption MeasureRun Parameter "POWERCFG /SETACTIVE 381b4222-f694-41f0-9685-ff5bb260df2e"][!CommandMeasure "MeasureRun" "Run"]
...
IfTrueAction3=[!SetOption MeasureRun Parameter "POWERCFG /SETACTIVE a1841308-3541-4fab-bc81-f71556f20b4a"][!CommandMeasure "MeasureRun" "Run"]
Each of these IfTrueActions sets the appropriate Parameter option for the [MeasureRun] measure, then runs it. Accordingly the plugin sets hidden the appropriate power scheme.
soapyb
Posts: 5
Joined: December 3rd, 2017, 6:35 am

Re: Toggle between Power Options and variable meter image

Post by soapyb »

That worked! Balala, you're a saint. Thanks for all the help!

Here's the final product in case you were curious:

https://imgur.com/a/pX2X1
User avatar
balala
Rainmeter Sage
Posts: 16110
Joined: October 11th, 2010, 6:27 pm
Location: Gheorgheni, Romania

Re: Toggle between Power Options and variable meter image

Post by balala »

Saint? Isn't this too much?
But I'm glad if I helped.
Post Reply