Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

function: toggling a setting and the corresponding controls


  • Please log in to reply
9 replies to this topic
Ace_NoOne
  • Members
  • 299 posts
  • Last active: May 02 2008 08:19 AM
  • Joined: 10 Oct 2005
Hi guys,

I just wrote a very simple function to toggle a setting and the corresponding menu item (e.g. you might have a "shut down after finished" item in the tray menu, which would toggle the shutDown variable as well as check/uncheck the menu item):
; toggle setting and corresponding menu item
toggleSetting(ByRef var, menuName, menuItem)
{
	If (var = True)
	{
		var := False
		Menu, %menuName%, Uncheck, %menuItem% 
	}
	Else
	{
		var := True
		Menu, %menuName%, Check, %menuItem%
	}
}
Not sure if anyone will ever need this - but I certainly will, so storing it here is the safest way to make sure it won't get lost... :p

Example script:
Menu, Tray, NoStandard
Menu, Tray, Add, Shut down, toggleShutdown

Loop
{
	If (shutDown = True)
	{
		MsgBox, I'll shut you down!
		ExitApp ; "Shutdown, 9" deemed unsafe for the purpose of this demo
	}
	Sleep, 1000
}

toggleShutdown:
	toggleSetting(shutDown, "Tray", "Shut down")
Return

toggleSetting(ByRef var, menuName, menuItem)
{
	If (var = True)
	{
		var := False
		Menu, %menuName%, Uncheck, %menuItem% 
	}
	Else
	{
		var := True
		Menu, %menuName%, Check, %menuItem%
	}
}

UPDATE: See below for improved version.

PhiLho
  • Moderators
  • 6850 posts
  • Last active: Jan 02 2012 10:09 PM
  • Joined: 27 Dec 2005
Just for the fun, I tried to improve your function. I has the same functionnality (or inverted? Seemed logical to check when var is 1) and won't improve speed or anything. That's just an alternative.
#Persistent
;~ Menu, Tray, NoStandard
Menu, Tray, Add, Shut down, toggleShutdown

toggleShutdown:
   toggleSetting(shutDown, "Tray", "Shut down")
Return

toggleSetting(ByRef var, menuName, menuItem)
{
	local check, check1, check0
	check1 = Check
	check0 = Uncheck
	check := check%var%
	Menu, %menuName%, %check%, %menuItem%
	var := not var
}

Posted Image vPhiLho := RegExReplace("Philippe Lhoste", "^(\w{3})\w*\s+\b(\w{3})\w*$", "$1$2")

polyethene
  • Members
  • 5519 posts
  • Last active: May 17 2015 06:39 AM
  • Joined: 26 Oct 2012
Quite good, but it can be simplified:
toggleSetting(ByRef var, Name, Item) {

	var := !var

	IfEqual, var, 0, SetEnv, ch, Un

	Menu, %Name%, %ch%Check, %Item%

}

autohotkey.com/net Site Manager

 

Contact me by email (polyethene at autohotkey.net) or message tidbit


Ace_NoOne
  • Members
  • 299 posts
  • Last active: May 02 2008 08:19 AM
  • Joined: 10 Oct 2005
Very clever, Titan! :shock:

I'd prefer a more readable version though:
toggleSetting(ByRef var, menuName, menuItem)
{
	var := !var
	If var = 0
		prefix = Un
	Menu, %menuName%, %prefix%Check, %menuItem%
}
(just replaced IfEqual and SetEnv with their respective alternatives)

PhiLho
  • Moderators
  • 6850 posts
  • Last active: Jan 02 2012 10:09 PM
  • Joined: 27 Dec 2005
I prefer to avoid SetEnv too...
Indeed, Titan's solution, using the syntaxical particularities of this parameter, is clever!
Posted Image vPhiLho := RegExReplace("Philippe Lhoste", "^(\w{3})\w*\s+\b(\w{3})\w*$", "$1$2")

polyethene
  • Members
  • 5519 posts
  • Last active: May 17 2015 06:39 AM
  • Joined: 26 Oct 2012
IfEqual/SetEnv is a little trick I learnt from one of Laszlo's scripts - I find it readable :)

Thanks anyway.

autohotkey.com/net Site Manager

 

Contact me by email (polyethene at autohotkey.net) or message tidbit


Ace_NoOne
  • Members
  • 299 posts
  • Last active: May 02 2008 08:19 AM
  • Joined: 10 Oct 2005

IfEqual/SetEnv is a little trick I learnt from one of Laszlo's scripts - I find it readable :)

A "trick"? It's just an alternative way of expressing the same operation, isn't it? Or is there any advantage regarding performance?

Anyways, I've altered the function to also support checkboxes:
; toggle setting and corresponding menu item and checkbox
toggleSetting(ByRef var, menuName, menuItem, checkBoxName) {
	; toggle variable
	var := !var
	; toggle menu item
	If (menuName != "" AND menuItem != "")
	{
		If var = 0
			prefix = Un
		Menu, %menuName%, %prefix%Check, %menuItem%
	}
	; toggle checkbox
	If (checkBoxName != "")
		GuiControl ,, %checkBoxName%, %var% 
}


Laszlo
  • Moderators
  • 4713 posts
  • Last active: Mar 31 2012 03:17 AM
  • Joined: 14 Feb 2005

A "trick"? It's just an alternative way of expressing the same operation

It is a trick to cram more instructions in a single line.
IfEqual a,, IfEqual b,, IfEqual c,, MsgBox All Empty
Not that AND did not work, just to show, that AHK sometimes allows many commands in one line.

Ace_NoOne
  • Members
  • 299 posts
  • Last active: May 02 2008 08:19 AM
  • Joined: 10 Oct 2005
Why would you wanna cram so many instructions into a single line? The human brain has a very limited amount of RAM (or cache, rather?), so this makes the code a lot harder to read IMHO. "One command per line", that's pretty much what I was told (by my very competent CS teacher back at school as well as by experience).
I do have to admit though that it sometimes does make sense to only use a single line for a particular set of commands, that is if they form an inseperable "logical block" (can't think of any examples though).

Anyways, this is more or less a question of personal preference, and we're getting way off-topic again...

polyethene
  • Members
  • 5519 posts
  • Last active: May 17 2015 06:39 AM
  • Joined: 26 Oct 2012

Why would you wanna cram so many instructions into a single line?

People read horizontally (ok the Chinese are an exception) so cramming more code into a single line improves readability. I find that stacking one or two words/phrases on each line and indenting them is less easy to read, less logical to read rather. The white space fakes an illusion of clarity when in fact it just means you have to change your focus more often to know what's going on. It really depends what you prefer though, left-brainers like myself (and Laszlo?) would prefer to have more code on one line whereas you and PhiLho might prefer a more spaced out image.

autohotkey.com/net Site Manager

 

Contact me by email (polyethene at autohotkey.net) or message tidbit