It is currently March 28th, 2024, 10:11 pm

Using !SetOption

Tips and Tricks from the Rainmeter Community
poiru
Developer
Posts: 2872
Joined: April 17th, 2009, 12:18 pm

Using !SetOption

Post by poiru »

Most of you reading this are undoubtedly familiar with !SetVariable and DynamicVariables. If not, you might want to check the DynamicVariables entry in the manual.

Many of you will also likely agree that !SetVariable is, quite frankly, ugly in many (if not most) cases.

Take a look at the following code.

Code: Select all

[Variables]
size=10

[SomeMeter]
Meter=STRING
...
FontSize=#size#
DynamicVariables=1
MouseOverAction=!SetVariable "size" "12"
MouseLeaveAction=!SetVariable "size" "10"
Now close your eyes, take a few deep breaths, and look at the code below.

Code: Select all

[SomeMeter]
Meter=STRING
...
FontSize=10
MouseOverAction=!SetOption "SomeMeter" "FontSize" "12"
MouseLeaveAction=!SetOption "SomeMeter" "FontSize" "10"
See what I did there? No variables. No need for DynamicVariables. !SetOption is not only more logical and easier to use than !SetVariable, it is also more efficient in most cases.

What is !SetOption?

In essence, it allows you to set an option (FontSize, Text, ImageName, ...) of a meter or measure on-the-fly.
Manual wrote:!SetOption [Meter] [Option] [Value] (Config)
!SetOption [Measure] [Option] [Value] (Config)
!SetOptionGroup [Group] [Option] [Value] (Config)
!SetOption cannot change..
- anything in [Rainmeter]
- anything in [Variables] (!SetVariable can though)
- anything in [Metadata] (but who cares)
- anything in a [MeterStyle] (unless it is also a meter or measure)
- anything in a Plugin measure
- the "type" of a meter or measure. (ie: Meter=String)
- the MeasureName setting of a meter
- the X or Y of a meter (use !MoveMeter)

Always prefer dedicated bangs (e.g. !DisableMeasure, !HideMeter, etc.) to changing the option (e.g. Disabled=1, Hidden=1) with !SetOption.

Examples:

Code: Select all

!SetOption "StringMeter" "Text" "Hello, world!"
!SetOption "CalcMeasure" "Formula" "40+2"
!SetOption "LineMeter" "LineWidth" "3"
!SetOption "ImageMeter" "ImageName" "image.png"
!SetOption "TimeMeasure" "Format" "%Y"
!SetOptionGroup "SomeGroup" "SolidColor" "255,255,255,255"
Other things:
  • Under-the-hood !SetOption force-enables DynamicVariables for the meter/measure/group in question for one update cycle after which DynamicVariables reverts to the original setting.

    Due to this design, Text and FontSize will be updated in the example below each time you hover over the meter.

    Code: Select all

    [SomeMeter]
    Meter=STRING
    ...
    FontSize=#size#
    Text="Hello!"
    MouseOverAction=!Execute [!SetVariable "size" "20"][!SetOption "SomeMeter" "Text" "Get off me!"]
  • The #CURRENTSECTION# variable is handy, especially when used with styles.

    Code: Select all

    [SomeStyle]
    LeftMouseUpAction=!SetOption "#CURRENTSECTION#" "SolidColor" "255,255,255"
    
    [SomeMeter]
    Meter=STRING
    MeterStyle=SomeStyle
  • When using a variable as the value, the actual value of the variable is assigned. Consider the following case:

    Code: Select all

    [Variables]
    normSize=10
    bigSize=15
    
    ...
    
    [SomeMeter]
    Meter=STRING
    FontSize=#normSize#
    MouseOverAction=!SetOption "SomeMeter" "FontSize" "#bigSize#"
    MouseLeaveAction=!SetOption "SomeMeter" "FontSize" "#normSize#"
    FontSize is set to the actual value of #bigSize# (i.e. 15) when the cursor is over [SomeMeter] and back to the actual value of #normSize# (i.e. 10) when the cursor leaves.

    This won't matter in most cases. If you use both !SetVariable and !SetOption on a given option, the following won't work:

    Code: Select all

    [SomeMeter]
    Meter=STRING
    FontSize=#normSize#
    MouseOverAction=!SetOption "SomeMeter" "FontSize" "#bigSize#"
    LeftMouseUpAction=!SetVariable "bigSize" "25"
    DynamicVariables=1
    When you hover over the meter, FontSize is set to the actual value of #bigSize#. Then, when you click on the meter, you change #bigSize# to 25. This won't have any effect on SomeMeter (unless you leave and enter) as FontSize is not set to #bigSize# but 25.

    To get around this, we've allowed escaping variables and measures ([*EscapedMeasure*] and #*EscapedVariable*#).

    Code: Select all

    MouseOverAction=!SetOption "SomeMeter" "FontSize" "#*bigSize*#"
    LeftMouseUpAction=!SetVariable "bigSize" "25"
    The chunk above, on the other hand, will set FontSize to the literal #bigSize# (as compared to the value of it) and allow the LeftMouseUpAction to work (without leaving and re-entering the meter).

    Of course, the easier way out is to avoid using a combination of !SetVariable/!SetOption on a given option.
  • Using !SetOption with an empty (i.e. "") value results in the default value for the option. The following, for example, results in FontColor being set to 0,0,0,255 (which is the default value for FontColor):

    Code: Select all

    !SetOption "SomeMeter" "FontColor" ""
User avatar
~Faradey~
Posts: 366
Joined: November 12th, 2009, 4:47 pm
Location: Ukraine

Re: Using !SetOption

Post by ~Faradey~ »

Awesome news as allways! :thumbup:

i think there is a typo in code

Code: Select all

[SomeMeter]
Meter=STRING
...
FontSize=#size#
Text="Hello!"
MouseOverAction=!Execute [!SetVariable "size" "20"][!SetOption "SomeMeter" "Text" "Get off me!"]
MouseOverAction=!Execute [!SetVariable "size" "20"] should be !SetOption i suppose
or i'm wrong? sorry is so ;-)

So we can set almost everything? even ImageCrop and ColorMatrix?
User avatar
Kaelri
Developer
Posts: 1721
Joined: July 25th, 2009, 4:47 am

Re: Using !SetOption

Post by Kaelri »

~Faradey~ wrote:Awesome news as allways! :thumbup:

i think there is a typo in code

Code: Select all

[SomeMeter]
Meter=STRING
...
FontSize=#size#
Text="Hello!"
MouseOverAction=!Execute [!SetVariable "size" "20"][!SetOption "SomeMeter" "Text" "Get off me!"]
MouseOverAction=!Execute [!SetVariable "size" "20"] should be !SetOption i suppose
or i'm wrong? sorry is so ;-)
That's not an error. The example is to demonstrate that !SetOption temporarily makes a meter or measure "dynamic," so it will be affected by dynamic actions (such as !SetVariable) for one update cycle only. Not something you would typically have to worry about, but could be useful in some cases.
So we can set almost everything? even ImageCrop and ColorMatrix?
Yep. :)
User avatar
~Faradey~
Posts: 366
Joined: November 12th, 2009, 4:47 pm
Location: Ukraine

Re: Using !SetOption

Post by ~Faradey~ »

Kaelri wrote:That's not an error. The example is to demonstrate that !SetOption temporarily makes a meter or measure "dynamic," so it will be affected by dynamic actions (such as !SetVariable) for one update cycle only. Not something you would typically have to worry about, but could be useful in some cases.
Thank you for explanation :thumbup:
Now i understand, no need to add "DynamicVariables=1" in this case and there is no errors in code! :bow:
User avatar
GHOST®
Posts: 55
Joined: March 11th, 2011, 6:33 pm
Location: Garden City, MI

Re: Using !SetOption

Post by GHOST® »

::Suffering a serious Brain Fart::

!SetOption is temporary modification of variables, yes??

!WriteKeyValue is still needed to permanately change a variable??
"Do you want to be healed, now? Or would you prefer to bleed to death so I can try my hand at resurrection?"
User avatar
Kaelri
Developer
Posts: 1721
Joined: July 25th, 2009, 4:47 am

Re: Using !SetOption

Post by Kaelri »

GHOST® wrote:::Suffering a serious Brain Fart::

!SetOption is temporary modification of variables, yes??

!WriteKeyValue is still needed to permanately change a variable??
Correct. !SetOption is analogous to !HideMeter, !MoveMeter, or !DisableMeasure - it affects keys directly and non-permanently.
User avatar
jsmorley
Developer
Posts: 22628
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: Using !SetOption

Post by jsmorley »

GHOST® wrote:::Suffering a serious Brain Fart::

!SetOption is temporary modification of variables, yes??

!WriteKeyValue is still needed to permanately change a variable??
!SetOption is not about "variables" as such. It is a way to (yes temporarily) change the "settings", like FontColor= or Format= or ImageName= on meters and measures. If you want to permanently change ANY Key=Value in Rainmeter, you do indeed need to use !WriteKeyValue and refresh the skin.
User avatar
jsmorley
Developer
Posts: 22628
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: Using !SetOption

Post by jsmorley »

Just a "tip" for this new stuff:

[Variables]
Txt.Clr=255,255,25,255

[Style]
FontColor=#Txt.Clr#

[mDay1]
Meter=String
MeterStyle=Style
FontSize=15
Text=Hello
MouseOverAction=!SetOption mDay1 FontColor 255,255,255,255
1) MouseLeaveAction=!SetOption mDay1 FontColor #*Txt.Clr*#
2) MouseLeaveAction=!SetOption mDay1 FontColor ""

The meter will be yellow, driven by the MeterStyle.. Then SetOption will change it to white on MouseOver, by ADDING a new FontColor=255,255,255,255 to the meter in memory. With the first MouseLeaveAction example, it will change that new FontColor setting to the #Txt.Clr# variable, by using the new "escape" chars "*" to send that variable as a literal. In the second MouseLeaveAction example, it just REMOVES the FontColor setting from the meter, which means it reverts to using the FontColor found in the MeterStyle. The results are identical in this example, but one or the other may be more or less appropriate in other cases.


Be careful though, as:

[Variables]
Txt.Clr=255,255,25,255

[mDay1]
Meter=String
1) FontColor=255,255,25,255
2) FontColor=#Txt.Clr#
FontSize=15
Text=Hello
MouseOverAction=!SetOption mDay1 FontColor 255,255,255,255
MouseLeaveAction=!SetOption mDay1 FontColor ""

Will in both cases leave the color as 0,0,0,255, as that is the "default" color of a String meter. Remember that using "" with !SetOption REMOVES the setting from the meter entirely.

Note: We are looking at a !ResetOption bang for a future beta, which if possible will add some additional flexibility.
User avatar
GHOST®
Posts: 55
Joined: March 11th, 2011, 6:33 pm
Location: Garden City, MI

Re: Using !SetOption

Post by GHOST® »

jsmorley wrote: !SetOption is not about "variables" as such. It is a way to (yes temporarily) change the "settings", like FontColor= or Format= or ImageName= on meters and measures. If you want to permanently change ANY Key=Value in Rainmeter, you do indeed need to use !WriteKeyValue and refresh the skin.
Thanks for the clarification. Now to add this new feature and further modify my ever in progress skins yet again. I can C how this new feature will streamline some if not a bunch of things I've been trying to do. :op
"Do you want to be healed, now? Or would you prefer to bleed to death so I can try my hand at resurrection?"
User avatar
jsmorley
Developer
Posts: 22628
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: Using !SetOption

Post by jsmorley »

Mostly it is just a bunch more efficient and clean. If you look at this:

[Variables]
24Hour=""

[MeasureHour]
Measure=Time
Format=%H
IfAboveValue=11
IfAboveAction=!SetVariable 24Hour PM
IfBelowValue=12
IfBelowAction=!SetVariable 24Hour AM

[MeterOne]
Meter=String
Text=#24Hour#
DynamicVariables=1

It can be replaced with:

[MeasureHour]
Measure=Time
Format=%H
IfAboveValue=11
IfAboveAction=!SetOption MeterOne Text PM
IfBelowValue=12
IfBelowAction=!SetOption MeterOne Text AM

[MeterOne]
Meter=String

Less code, no variables to mess with, and no DyanmicVariables turned on.

I know there are ways to get this directly from the Time format, but it's just an example...