It is currently March 28th, 2024, 10:40 am

LoadSkin.lua

Discuss the use of Lua in Script measures.
Post Reply
User avatar
raiguard
Posts: 660
Joined: June 25th, 2015, 7:02 pm
Location: The Sky, USA
Contact:

LoadSkin.lua

Post by raiguard »

This script loads / unloads the specified skin or config, and sets parameters for toggle buttons related to those skins.

Usage
To use this script, copy it into your suite and create a measure for it. The following parameters may be added to the script measure:

ToggleOn - The text or image path of the 'on' state of the button. If not included in the measure, it will default to the [#toggleOn] variable.
ToggleOff - The text or image path of the 'off' state of the button. If not included in the measure, it will default to the [#toggleOff] variable.
ToggleGroup - The meter group name that the toggle buttons belong to. If not included in the measure, it will default to 'SkinToggles'.

The toggle buttons can be images or string meters. Depending on which type of toggle you use, things will be slightly different.

For either kind of button, DynamicVariables=1 is required. You will also need to include all the toggle buttons in the group specified in the script measure for them to update properly. It is recommended to create a MeterStyle for the toggle buttons, so you can keep things consistent.

ToggleSkin() is used through a !CommandMeasure bang in the MouseAction of your toggle button. Make certain that you use nested syntax for any variables, and surround the config and skin arguments with single quotes.

GetIcon() is used through inline LUA on the ImageName or Text argument of your toggle button. Make certain that any variables used as arguments use the nested syntax.

Function Descriptions:
ToggleSkin(config, skin, variant, state)
config - the config of the skin you want to toggle, omitting the root config (e.g. 'ToggledSkin' rather than 'LoadSkin\ToggledSkin').
skin (optional, default nil) - the name of the skin INI you wish to toggle (including the extension). If there is only one skin in the config, this value is unnecessary.
variant (optional, default -1) - the skin INI's placement in the list of variants accessed through Rainmeter's interface. If you only have one skin variant, this value is unnecessary.
state (optional, default nil) - the state you wish to toggle to. If set to true, the function will always try to load the skin. If set to false, it will always try to unload the skin.

GetIcon(config, variant)
config - the name of the config that the toggle button will be triggering.
variant (optional, default 1) - the placement in the variants list of the skin you wish to toggle.

Example Skin
LoadSkin2.gif
Attached is an example skin I created to demonstrate the uses of this script. The LoadSkin.lua source code is also available below:

Code: Select all

[Rainmeter]
MiddleMouseUpAction=[!Refresh]
AccurateText=1

[Variables]
colorButtonPress=230,30,30

[MeasureScript]
Measure=Script
ScriptFile=#@#Scripts\LoadSkin.lua
ToggleOn=[\x5a]
ToggleOff=[\x56]

[StyleLabelString]
FontFace=Roboto
FontColor=240,240,240
FontSize=10
AntiAlias=1
X=3R
Y=-1r

[StyleToggleButton]
FontFace=ElegantIcons
FontColor=138,210,250
FontSize=12
AntiAlias=1
X=5
Y=4R
DynamicVariables=1
Group=SkinToggles

[MeterBackground]
Meter=Shape
Shape=Rectangle 0,0,120,102 | Fill Color 15,15,15 | StrokeWidth 0

[MeterToggleConfig]
Meter=String
MeterStyle=StyleToggleButton
Y=5
Text=[&MeasureScript:GetIcon('ToggledSkin')]
LeftMouseUpAction=[!CommandMeasure MeasureScript "ToggleSkin('ToggledSkin')"]

[MeterToggleConfigLabelString]
Meter=String
MeterStyle=StyleLabelString
Text="Toggle Config"

[MeterToggleSkin1]
Meter=String
MeterStyle=StyleToggleButton
Text=[&MeasureScript:GetIcon('ToggledSkin', 1)]
LeftMouseUpAction=[!CommandMeasure MeasureScript "ToggleSkin('ToggledSkin', 'Variant1.ini', 1)"]

[MeterToggleSkin1LabelString]
Meter=String
MeterStyle=StyleLabelString
Text="Toggle Skin 1"

[MeterToggleSkin2]
Meter=String
MeterStyle=StyleToggleButton
Text=[&MeasureScript:GetIcon('ToggledSkin', 2)]
LeftMouseUpAction=[!CommandMeasure MeasureScript "ToggleSkin('ToggledSkin', 'Variant2.ini', 2)"]

[MeterToggleSkin2LabelString]
Meter=String
MeterStyle=StyleLabelString
Text="Toggle Skin 2"

[MeterLoadSkin2]
Meter=String
MeterStyle=StyleLabelString
X=5
Y=3R
Text="Load Skin 2"
LeftMouseUpAction=[!CommandMeasure MeasureScript "ToggleSkin('ToggledSkin', 'Variant2.ini', 2, true)"]
MouseOverAction=[!SetOption MeterLoadSkin2 FontColor "#colorButtonPress#"][!UpdateMeter MeterLoadSkin2][!Redraw]
MouseLeaveAction=[!SetOption MeterLoadSkin2 FontColor ""][!UpdateMeter MeterLoadSkin2][!Redraw]

[MeterUnloadSkin2]
Meter=String
MeterStyle=StyleLabelString
X=5
Y=3R
Text="Unload Skin 2"
LeftMouseUpAction=[!CommandMeasure MeasureScript "ToggleSkin('ToggledSkin', 'Variant2.ini', 2, false)"]
MouseOverAction=[!SetOption MeterUnloadSkin2 FontColor "#colorButtonPress#"][!UpdateMeter MeterUnloadSkin2][!Redraw]
MouseLeaveAction=[!SetOption MeterUnloadSkin2 FontColor ""][!UpdateMeter MeterUnloadSkin2][!Redraw]
LoadSkin.lua:

Code: Select all

-- LoadSkin.lua
-- raiguard
-- v1.2.0
--
--
-- CHANGELOG:
-- v1.2.0 - 2017-12-27
--  - Added ability to specifically load or unload skins, rather than always toggling them
-- v1.1.0 - 2017-12-7
--  - Consolidated LoadConfig() into LoadSkin()
-- v1.0.0 - 2017-10-2
--  - Initial release
--
-- ------------------------------
--
-- This script loads / unlaods the specified skin or config, and sets parameters for toggle
-- buttons related to those skins.
--
--
-- INSTRUCTIONS FOR USE:
-- Copy this file and paste it into your own suite, then create a Rainmeter script
-- measure pointing to this file, like so:
--
-- [MeasureLoadSkinScript]
-- Measure=Script
-- ScriptFile=#@#Scripts\LoadSkin.lua
-- ToggleOn=#@#Images\toggle-on.png
-- ToggleOff=#@#Images\toggle-off.png
-- ToggleGroup=ToggleButtons
-- 
-- The 'ToggleOn' and 'ToggleOff' parameters are for the toggle buttons. If you are using
-- images, these will be the image paths for the buttons' respective on and off states. If
-- you are using strings, these will be the 'on' and 'off' strings that will show on the
-- buttons. If you do not include these parameters, the script will default to using what's
-- contained in '#toggleOn#' and '#toggleOff#' variables.
--
-- The 'ToggleGroup' parameter specifies the group that the toggle button meters belong to.
-- If you do not include this option, it will default to 'SkinToggles'.
--
--
-- A toggle button meter should look something like this:
--
-- [MeterToggleSkin]
-- Meter=Image
-- ImageName=[&MeasureLoadSkinScript:GetIcon('ToggledSkin')]
-- X=5
-- Y=5
-- W=31
-- H=20
-- LeftMouseUpAction=[!CommandMeasure MeasureLoadSkinScript "ToggleSkin('ToggledSkin')"]
-- DynamicVariables=1
-- Group=SkinToggles
--
-- The toggle buttons get their parameters via inline LUA, which requires that
-- 'DynamicVariables=1' must be set on all the buttons. The buttons must also belong to the
-- 'SkinToggles' group, unless otherwise specified in the script measure.
--
-- Please note that if you load or unload a skin without using the toggle buttons, the
-- buttons will not update until one of the buttons is clicked or the skin is refreshed.
--
-- ------------------------------

debug = false

function Initialize()

	filePath = SKIN:GetVariable('SETTINGSPATH') .. 'Rainmeter.ini'
	rootConfig = SKIN:GetVariable('ROOTCONFIG') .. '\\'
	iniTable = ReadIni(filePath)

	toggleOn = SELF:GetOption('ToggleOn', '[#toggleOn]')
	toggleOff = SELF:GetOption('ToggleOff', '[#toggleOff]')
	toggleGroup = SELF:GetOption('ToggleGroup', 'SkinToggles')

end

function Update() end

-- Toggles the specified skin.
function ToggleSkin(config, skin, variant, state)
	-- CONFIG: The name of the config you wish to toggle, omitting the root config
	-- SKIN (optional): The file name of the skin you wish to toggle
	-- VARIANT (optional): The skin file's numeric location in the list of variants
	-- STATE (optional): The state you wish to toggle to
	config = rootConfig .. config
	if variant == nil then variant = -1 end
	local activeState = 0
	if iniTable[config] ~= nil then activeState = tonumber(iniTable[config]['Active']) end

	if skin == nil then
		if activeState > 0 then
			SKIN:Bang('!DeactivateConfig', config)
		else
			SKIN:Bang('!ActivateConfig', config)
		end
	else
		if state == true then SKIN:Bang('!ActivateConfig', config, skin)
		elseif state == false then SKIN:Bang('!DeactivateConfig', config, skin)
		elseif activeState > 0 and activeState ~= variant then SKIN:Bang('!ActivateConfig', config, skin)
		else SKIN:Bang('!ToggleConfig', config, skin) end
	end

	iniTable = ReadIni(filePath)

	SKIN:Bang('!UpdateMeterGroup', toggleGroup)
	SKIN:Bang('!Redraw')
end

-- Returns whether or not the specified skin or variant is loaded.
function GetIcon(config, variant)

	config = rootConfig .. config
	if variant == nil then variant = -1 end
	local state = 0
	if iniTable[config] ~= nil then state = tonumber(iniTable[config]['Active']) end

	if state == variant then return toggleOn
	elseif state > 0 and variant == -1 then return toggleOn
	else return toggleOff end

end

-- parses a INI formatted text file into a 'Table[Section][Key] = Value' table
function ReadIni(inputfile)
	local file = assert(io.open(inputfile, 'r'), 'Unable to open ' .. inputfile)
	local tbl, section = {}
  local num = 0
	for line in file:lines() do
		num = num + 1
		if not line:match('^%s;') then
			local key, command = line:match('^([^=]+)=(.+)')
			if line:match('^%s-%[.+') then
				section = line:match('^%s-%[([^%]]+)')
        		-- LogHelper(section, 'Debug')
				if not tbl[section] then tbl[section] = {} end
			elseif key and command and section then
        		-- LogHelper(key .. '=' .. command, 'Debug')
				tbl[section][key:match('(%S*)%s*$')] = command:match('^s*(.-)%s*$')
			elseif #line > 0 and section and not key or command then
				LogHelper(num .. ': Invalid property or value.', Error)
			end
		end
	end
	if not section then print('No sections found in ' .. inputfile) end
	file:close()
	return tbl
end

-- function to make logging messages less cluttered
function LogHelper(message, type)

  	if debug == true then
    	SKIN:Bang("!Log", message, type)
  	elseif type ~= 'Debug' then
  		SKIN:Bang("!Log", message, type)
	end

end
Attachments
LoadSkin_1.2.0.rmskin
(105.72 KiB) Downloaded 113 times
Post Reply