A_ThisVariant

Propose new features and changes
guest3456
Posts: 3463
Joined: 09 Oct 2013, 10:31

A_ThisVariant

Post by guest3456 » 07 Feb 2024, 13:48

if the same hotkey key can be triggered by multiple #if conditions, would be nice to have an easy way to know which variant triggered the hotkey


lexikos
Posts: 9592
Joined: 30 Sep 2013, 04:07
Contact:

Re: A_ThisVariant

Post by lexikos » 15 Feb 2024, 23:17

What value is it supposed to have?

guest3456
Posts: 3463
Joined: 09 Oct 2013, 10:31

Re: A_ThisVariant

Post by guest3456 » 16 Feb 2024, 09:32

lexikos wrote:
15 Feb 2024, 23:17
What value is it supposed to have?
to allow you to know which variant triggered the hotkey

pseudocode example

Code: Select all

Hotkey, If, MouseIsOverNotepad()
Hotkey, F1, F1_Func(), on
Hotkey, If, FirefoxIsActive()
Hotkey, F1, F1_Func(), on

F1_Func()
{
	;currently need to loop through all #if conditions and re-test ourselves to determine which variant has triggered this func
	if MouseIsOverNotepad()
		msgbox mouseover variant
	else if FirefoxIsActive()
		msgbox winactive variant
}


Descolada
Posts: 1141
Joined: 23 Dec 2021, 02:30

Re: A_ThisVariant

Post by Descolada » 16 Feb 2024, 15:22

@guest3456, why not use

Code: Select all

HotIf MouseIsOverNotepad
Hotkey "F1", F1_Func.Bind("Notepad")
HotIf FirefoxIsActive
Hotkey "F1", F1_Func.Bind("Firefox")

F1_Func(ThisVariant) {
	if ThisVariant = "Notepad"
		msgbox "mouseover variant"
	else if ThisVariant = "Firefox"
		msgbox "winactive variant"
}
I modified the example for v2 because v1 isn't getting any new features.

User avatar
WarlordAkamu67
Posts: 220
Joined: 21 Mar 2023, 06:52

Re: A_ThisVariant

Post by WarlordAkamu67 » 26 Feb 2024, 07:11

There are a few ways to figure out which variant was pressed as with any code stuffs. The HotIf line as a string is one suggestion for the value, if the variable is made.

Code: Select all

HotIf MouseIsOverNotepad
Hotkey "F1", F1_Func() ; Says "MouseIsOverNotepad"
HotIf(FirefoxIsActive())
Hotkey "F1", F1_Func() ; Says "FirefoxIsActive()"
HotIf(FirefoxIsActive() && otherThings() || moreFunctions())
Hotkey "F1", F1_Func() ; Says "FirefoxIsActive() && otherThings() || moreFunctions()"

F1_Func() {
	MsgBox(A_ThisVariant) ; Where the display is the string of the HotIf
}

Descolada
Posts: 1141
Joined: 23 Dec 2021, 02:30

Re: A_ThisVariant

Post by Descolada » 29 Feb 2024, 02:26

@WarlordAkamu67
Suppose A_ThisVariant returned the string of the HotIf function call argument.

Code: Select all

HotIf MouseIsOverNotepad
This case is simple, as A_ThisVariant would be the string "MouseIsOverNotepad"

However, we can bind arguments to functions:

Code: Select all

HotIf MouseIsOverNotepad.Bind(1)
What should the string be now? "MouseIsOverNotepad.Bind(1)" or "MouseIsOverNotepad(1)" or ignore the argument and return "MouseIsOverNotepad"?

We can also assign functions to variables:

Code: Select all

f := MouseIsOverNotepad
HotIf f
Is A_ThisVariant now "MouseIsOverNotepad" or "f"?

Okay, suppose we said that only the *actual* function name should be returned ("MouseIsOverNotepad", not "f"). What should we do in the case of anonymous functions?

Code: Select all

HotIf (*) => MouseIsOverNotepad()
Would A_ThisVariant be "" or "(*) => MouseIsOverNotepad()"?

Then we have the case of #HotIf with multiple conditions, eg

Code: Select all

#HotIf FirefoxIsActive() && otherThings()
but suppose that in another part of my code I used

Code: Select all

#HotIf FirefoxIsActive() and otherThings()
These are equivalent, but should A_ThisVariant return different strings?

As you see this would get complex and convoluted. Just use BoundFuncs as I showed in my example...

lexikos
Posts: 9592
Joined: 30 Sep 2013, 04:07
Contact:

Re: A_ThisVariant

Post by lexikos » 29 Feb 2024, 19:26

guest3456 wrote:
16 Feb 2024, 09:32
lexikos wrote:
15 Feb 2024, 23:17
What value is it supposed to have?
to allow you to know which variant triggered the hotkey
I mean "What value is the variable supposed to return?", not "What is the worth of this feature?"

For v1, which I will not be adding features to anyway, criteria is a combination of multiple parameters.

Descolada wrote:
29 Feb 2024, 02:26
Suppose A_ThisVariant returned the string of the HotIf function call argument.
Only #HotIf has a string (it also has an anonymous function). HotIf only has a function object. A function such as HotIf only knows what parameters it was given, not the source code of the expression which provided the values.
These are equivalent, but should A_ThisVariant return different strings?
If it returns strings, it must return the appropriate string for the current variant. A variant which was created under a && b cannot be referenced by setting the criteria to a and b. A case such as below would create two separate variants, though one would never execute unless the other was disabled.

Code: Select all

#HotIf a && b
x::MsgBox 1
#HotIf a and b
x::MsgBox 2
The same would apply to expressions which differ only by case or spacing, even if they have the same result.

User avatar
WarlordAkamu67
Posts: 220
Joined: 21 Mar 2023, 06:52

Re: A_ThisVariant

Post by WarlordAkamu67 » 01 Mar 2024, 06:21

I kind of pictured it in my head like trimming the error window code, so whatever that is displayed there. So I guess it would "MouseIsOverNotepad.Bind(1)" and "f"? I don't have any great examples of a use case, but that is what I think of for a potential value, if assigned.
variant.png
variant.png (36.95 KiB) Viewed 258 times

lexikos
Posts: 9592
Joined: 30 Sep 2013, 04:07
Contact:

Re: A_ThisVariant

Post by lexikos » 02 Mar 2024, 20:13

#HotIf is a directive, and as such its parameter is literal source code. The directive creates an anonymous function and sets it as the current hotkey criterion. Since the script has no way to refer to this function directly, it also stores the expression source code for use with the HotIf function and to merge exact duplicates. #HotIf is allowed only in global scope, so two lines with an exactly identical expression would behave the same even if they weren't implicitly merged.

The HotIf function takes a function object. A unique function object may not directly correspond to literal source code, and even if it did, the same snippet of source code can have different interpretations depending on scope. Unlike #HotIf, HotIf can be used in local scope.

More importantly, functions receive only the values of their parameters. Attempting to self-introspect to determine the source code snippet corresponding to the parameter would be slow and error-prone. Error dialogs only show the line that called the function. A line can contain multiple calls to HotIf. The same source code can correspond to many different unique function objects, even if there's only one call to HotIf in the entire script.

guest3456
Posts: 3463
Joined: 09 Oct 2013, 10:31

Re: A_ThisVariant

Post by guest3456 » 03 Mar 2024, 22:12

Descolada wrote:
16 Feb 2024, 15:22
@guest3456, why not use

Code: Select all

HotIf MouseIsOverNotepad
Hotkey "F1", F1_Func.Bind("Notepad")
HotIf FirefoxIsActive
Hotkey "F1", F1_Func.Bind("Firefox")

F1_Func(ThisVariant) {
	if ThisVariant = "Notepad"
		msgbox "mouseover variant"
	else if ThisVariant = "Firefox"
		msgbox "winactive variant"
}
I modified the example for v2 because v1 isn't getting any new features.
thanks. this should work

lexikos wrote:
29 Feb 2024, 19:26
I mean "What value is the variable supposed to return?", not "What is the worth of this feature?"
ahh gotcha. not sure what the value of the variable should be, given the difficulties already mentioned. i'm fine using the .Bind method


Post Reply

Return to “Wish List”