dumbeau / AutoHotPie

Radial menus in Windows, aka PIE MENYOOS!
MIT License
367 stars 21 forks source link

Add support for user-defined custom context sensitivity #143

Closed mmikeww closed 9 months ago

mmikeww commented 9 months ago

Why

Many users have requested the ability to have pie menus fire only when certain window titles (not window exes) are active, or many other different requirements. This pull requests allows the user to write a custom AHK function for whatever requirement they have. If the function returns true, then the context sensitivity is applied.

Implementation

The appProfiles[index].ahkHandles setting inside the AHPSettings.json file contains the context that will be applied to that profile. Currently, if that value is ahk_group regApps then that means the Default Profile is triggered and the menu will show whenever none of the other profile applications match. If that value is appname.exe then the menu will only be shown when that executable is active.

We add a new option for the appProfiles[index].ahkHandles setting, and that is any string that ends in Func such as "myFunc". If that is found, then AutoHotPie will search for that function name, and use that function to determine the context sensitivity for that profile.

Since the function needs to be written in AHK and defined by the user, it requires the user to either

  1. have the toggle turned on to "Use open source AHK to run the pie menus" or
  2. use the exported standalone ahk files

If two custom contexts both could match, the profile created first will take precedence. That is, the one higher up in the profile dropdown list

Examples

For any user who wants to test the examples below, here are the necessary steps currently (if/when this PR code gets merged, these steps will change and probably best to check the main README):

  1. Open AutoHotPie, click on the global Settings button in the upper left
  2. Make sure the setting for "Use open source AutoHotkey pie menus" is turned ON
  3. Create a new profile
  4. Choose any focused application, it doesn't matter
  5. Add a pie menu to this new profile
  6. Save and run
  7. Right click the AHK icon in the system tray near the clock, and click Exit
  8. Open your AHP settings .json file. It is located in this location: C:\Users\USERNAME\AppData\Roaming\AutoHotPie\AHPSettings.json
  9. CTRL+F to search for the new profile name you just created
  10. On the next line, edit the ahkHandles section and change the app.exe to "MyContextFunc". Here's what the before and after should look like: image
  11. Save the file
  12. Download this development branch here: https://github.com/mmikeww/AutoHotPie/archive/7d77326e9dc33c6ebe33e4291d80b958cc33e9d4.zip
  13. Unzip, and open the folder AutoHotPie\src\
  14. Right click the PieMenu.ahk file and choose 'Edit Script'
  15. Scroll to the bottom of the file, and paste one of the functions below
  16. Save the file and close it.
  17. Double click that PieMenu.ahk and test

Only show menu if a specific window title is active (Issue #95)

MyContextFunc()
{
    ; does window title contain "Untitled - Notepad" ?
    return WinActive("Untitled - Notepad")

    ; or

    ; is window title EXACTLY "Untitled - Notepad", and a notepad.exe window?
    ; oldMatchMode := A_TitleMatchMode
    ; SetTitleMatchMode, 3
    ; result := WinActive("Untitled - Notepad ahk_exe notepad.exe")
    ; SetTitleMatchMode, %oldMatchMode%
    ; return result 
}

Only show menu if the pixel at screen coordinate is a certain color (Issue #140)

MyContextFunc()
{
    ; is color at 1623x358 black ?
    PixelGetColor, color, 1623, 358
    if (color = 0x000000)
        return true
    return false
}

Only show menu if mouse is within a certain screen area (Issue #53)

MyContextFunc()
{
    ; is mouse within the top left quadrant of the screen?
    MouseGetPos, mx, my
    if (mx >= 0) && (mx <= A_ScreenWidth/2) && (my >= 0) && (my <= A_ScreenHeight/2)
        return true
    return false
}

Only show menu if certain apps are NOT active (Issue #85)

MyContextFunc()
{
    if !WinActive("ahk_exe notepad.exe") && !WinActive("ahk_exe mspaint.exe")
        return true
    return false
}

Since the possibilities are endless, if users have other requirements, they can simply go to the AutoHotkey forums, and request help in building a function to meet their specific requirements.

mmikeww commented 9 months ago

Adding to the javascript menu builder app

This pull request just implements the functionality on the AHK side. That gives the freedom to merge the code and allow users to manually add this if they so choose by simply following the instructions above, without needing to wait for the tedious, unenviable work of coding the UI.

I've created a potential mockup of what the UI could look like. Obviously change how you see fit:

image

When a new profile is created with custom context, the javascript builder could do something like:

  1. strip all spaces from the profile name
  2. write to the bottom of PieMenu.ahk any empty function definition:
    
    profilenameFunc()
    {

}


3. toggle on the setting "Use open source AHK to run the pie menus"
4. run notepad to open up PieMenu.ahk and send ctrl+end to scroll to bottom
dumbeau commented 9 months ago

This one may need some different UI treatment, just because once you're using any custom code, you have to switch over to using the AHK version and not the compiled version.

There are some piddly issues with how the settings UI tracks profiles, this may need a little more love but the foundation is there. I should be able to add the corresponding UI functionality and put up a new build this weekend.

Thank you so much adding this!

mmikeww commented 9 months ago

This one may need some different UI treatment, just because once you're using any custom code, you have to switch over to using the AHK version and not the compiled version.

Is it not sufficient to use the toggle for "Use open source AHK to run the pie menus"? That won't work?