It is currently February 23rd, 2024, 3:21 am

Handling Empty InputText

Tips and Tricks from the Rainmeter Community
RicardoTM
Posts: 124
Joined: December 28th, 2022, 9:30 pm
Location: México

Handling Empty InputText

Post by RicardoTM »

I was looking for an answer to this some time ago and didn't find anything, so here's my solution.

The problems:
-When we use the InputText plugin to allow the user to enter some value, it is possible that the user mistakenly/knowingly leaves it empty and hits enter. What happens is that the meter that displays that value will disappear, making any other meter which position is relative to that meter move.

-We just want to avoid the variable from being empty since that could negatively impact the functioning of other measures that use that variable in their formula.

Example: In the next code, we have a shape meter and 2 string meters, one called title and another subtitle. You can enter a Title and a subtitle by clicking them, typing and then hitting enter.

Code: Select all

[Rainmeter]
Update=-1
AccurateText=1

[Variables]
Title=Title
Subtitle=Subtitle

[TextStyle]
FontColor=255,255,255
AntiAlias=1
StringAlign=Center
X=([BG:W]/2)
Y=10R
W=100
DynamicVariables=1
LeftMouseUpAction=!CommandMeasure "m#CurrentSection#" "ExecuteBatch 1-2"

[mTitle]
Measure=Plugin
Plugin=InputText
StringAlign=Center
InputLimit=0
InputNumber=0
X=([Title:X])
Y=[Title:Y]
W=100
H=[Title:H]
FontSize=15
Command1=[!SetVariable Title "$UserInput$"][!WriteKeyValue Variables Title "[#CurrentSection#]"][!Update]
DefaultValue=#Title#

[mSubtitle]
Measure=Plugin
Plugin=InputText
StringAlign=Center
InputLimit=0
InputNumber=0
X=([Subtitle:X])
Y=[Subtitle:Y]
W=100
H=[Subtitle:H]
FontSize=10
Command1=[!SetVariable Subtitle "$UserInput$"][!WriteKeyValue Variables Subtitle "[#CurrentSection#]"][!Update]
DefaultValue=#Subtitle#

[BG]
Meter=Shape
Shape=Rectangle 0,0,100,70 | Fill Color 0,0,0

[Title]
MeterStyle=TextStyle
Meter=String
FontSize=15
Text=#Title#
Y=10

[Subtitle]
Meter=String
MeterStyle=TextStyle
FontSize=10
Text=#Subtitle#
This works great, but if you leave Title empty and hit enter, Tittle will disappear and Subtitle will move up.
ezgif.com-video-to-gif-converter (1).gif
Solution:
To avoid this we can use substitute.
We just add Substitute="":"DefaultValue" to the InputText measures. Not that easy tho, if you only do that, nothing will change, because we don't use the value of the measure to set the variable, only to write it, we use $UserInput$ to set the variable. It would only go back to the default value after a refresh. To fix it, we'll add another SetVariable bang like this:

Before:

Code: Select all

[!SetVariable Subtitle "$UserInput$"][!WriteKeyValue Variables Subtitle "[#CurrentSection#]"][!Update]
After:

Code: Select all

[!SetVariable Subtitle "$UserInput$"][!SetVariable Subtitle "[#CurrentSection#]"][!WriteKeyValue Variables Subtitle "[#CurrentSection#]"][!Update]
This will overwrite the $UserInput$ value with the value of the measure with our substitution (If we don't use $UserInput$ at all the Plugin won't work).

So now, after adding those changes to both measures our code looks like this:

Code: Select all

[Rainmeter]
Update=-1
AccurateText=1

[Variables]
Title=Title
Subtitle=Subtitle

[TextStyle]
FontColor=255,255,255
AntiAlias=1
StringAlign=Center
X=([BG:W]/2)
Y=10R
W=100
DynamicVariables=1
LeftMouseUpAction=!CommandMeasure "m#CurrentSection#" "ExecuteBatch 1-2"

[mTitle]
Measure=Plugin
Plugin=InputText
Substitute="":"Title"
StringAlign=Center
InputLimit=0
InputNumber=0
X=([Title:X])
Y=[Title:Y]
W=100
H=[Title:H]
FontSize=15
Command1=[!SetVariable Title "$UserInput$"][!SetVariable Title "[#CurrentSection#]"][!WriteKeyValue Variables Title "[#CurrentSection#]"][!Update]
DefaultValue=#Title#

[mSubtitle]
Measure=Plugin
Plugin=InputText
Substitute="":"Subtitle"
StringAlign=Center
InputLimit=0
InputNumber=0
X=([Subtitle:X])
Y=[Subtitle:Y]
W=100
H=[Subtitle:H]
FontSize=10
Command1=[!SetVariable Subtitle "$UserInput$"][!SetVariable Subtitle "[#CurrentSection#]"][!WriteKeyValue Variables Subtitle "[#CurrentSection#]"][!Update]
DefaultValue=#Subtitle#

[BG]
Meter=Shape
Shape=Rectangle 0,0,100,70 | Fill Color 0,0,0

[Title]
MeterStyle=TextStyle
Meter=String
FontSize=15
Text=#Title#
Y=10

[Subtitle]
Meter=String
MeterStyle=TextStyle
FontSize=10
Text=#Subtitle#
And done, now when we leave them empty, the values return to our set default values, in this case, "Title" and "Subtitle".
ezgif.com-video-to-gif-converter (2).gif
Any other workaround is welcomed.
You do not have the required permissions to view the files attached to this post.
Last edited by RicardoTM on February 2nd, 2024, 10:49 pm, edited 2 times in total.
User avatar
balala
Rainmeter Sage
Posts: 16007
Joined: October 11th, 2010, 6:27 pm
Location: Gheorgheni, Romania

Re: Handling Empty InputText

Post by balala »

RicardoTM wrote: February 2nd, 2024, 12:10 am Any other workaround is welcomed.
I think the problem of moving upward the subtitle can easier be fixed by simply use relative positioning of the subtitle meter using the r parameter, instead of R. If you replace the Y=10R option of the [TextStyle] style section by for instance Y=30r you get the same behavior by a much simpler way, I think.
Just note one more thing related to the code: using the [!Update] and [!Redraw] bangs together makes not too much sense (they are used together in the Command1 option of the [mTitle] measure), because whenever a skin is updated (and doesn't matter if it is updated "normally", prescribed by the Update option of the [Rainmeter] section, or by a [!Update] bang) it is automatically redrawn as well, so no need for an extra redrawing (done in this case by the [!Redraw] bang). In such cases you should use only [!Update] and no [!Redraw].
RicardoTM
Posts: 124
Joined: December 28th, 2022, 9:30 pm
Location: México

Re: Handling Empty InputText

Post by RicardoTM »

balala wrote: February 2nd, 2024, 7:39 pm I think the problem of moving upward the subtitle can easier be fixed by simply use relative positioning of the subtitle meter using the r parameter, instead of R. If you replace the Y=10R option of the [TextStyle] style section by for instance Y=30r you get the same behavior by a much simpler way, I think.
Thanks, by the way, doing only that, the meter will disappear anyway after leaving it empty and thus the user won't be able to enter any value anymore unless modifying the variable manually on the code. A workaround for that, of course, is making sure you also set H and W on the meter.

To me, avoiding the value from being blank is what matters, especially when the value is not just a Text meter but a variable value that goes into a formula afterwards and can't be empty. I think I'll add another example to cover that when I have some spare time.
balala wrote: February 2nd, 2024, 7:39 pm Just note one more thing related to the code: using the [!Update] and [!Redraw] bangs together makes not too much sense (they are used together in the Command1 option of the [mTitle] measure), because whenever a skin is updated (and doesn't matter if it is updated "normally", prescribed by the Update option of the [Rainmeter] section, or by a [!Update] bang) it is automatically redrawn as well, so no need for an extra redrawing (done in this case by the [!Redraw] bang). In such cases you should use only [!Update] and no [!Redraw].
Noted, thank you for the correction. I've updated the code.
User avatar
balala
Rainmeter Sage
Posts: 16007
Joined: October 11th, 2010, 6:27 pm
Location: Gheorgheni, Romania

Re: Handling Empty InputText

Post by balala »

RicardoTM wrote: February 2nd, 2024, 11:09 pm Thanks, by the way, doing only that, the meter will disappear anyway after leaving it empty and thus the user won't be able to enter any value anymore unless modifying the variable manually on the code. A workaround for that, of course, is making sure you also set H and W on the meter.

To me, avoiding the value from being blank is what matters, especially when the value is not just a Text meter but a variable value that goes into a formula afterwards and can't be empty. I think I'll add another example to cover that when I have some spare time.
Sorry, it seems I misinterpreted little bit your post. :great:
RicardoTM wrote: February 2nd, 2024, 11:09 pm Noted, thank you for the correction. I've updated the code.
You're welcome.