Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
177 changes: 74 additions & 103 deletions GUI/GUI.ahk
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
; Created by Asger Juul Brunshøj

; Note: Save with encoding UTF-8 with BOM if possible.
; I had issues with special characters like in ¯\_(ツ)_/¯ that wouldn't work otherwise.
; Notepad will save UTF-8 files with BOM automatically (even though it does not say so).
; Some editors however save without BOM, and then special characters look messed up in the AHK GUI.
#SingleInstance Force
SetWorkingDir %A_ScriptDir%

;-------------------------------------------------------------------------------
; AUTO EXECUTE
Expand All @@ -24,34 +20,20 @@ gui_autoexecute:
cPurple := "c" . "b294bb"

gui_control_options := "xm w220 " . cForeground . " -E0x200"
; -E0x200 removes border around Edit controls

; Initialize variable to keep track of the state of the GUI
gui_state = closed

; Initialize search_urls as a variable set to zero
search_urls := 0
return

;-------------------------------------------------------------------------------
; LAUNCH GUI
;-------------------------------------------------------------------------------
CapsLock & Space::
gui_spawn:
if gui_state != closed
{
; If the GUI is already open, close it.
gui_destroy()
return
}

gui_state = main

Gui, Margin, 16, 16
Gui, Color, 1d1f21, 282a2e
Gui, +AlwaysOnTop -SysMenu +ToolWindow -caption +Border
Gui, Font, s11, Segoe UI
Gui, Add, Text, %gui_control_options% vgui_main_title, ¯\_(ツ)_/¯
Gui, Add, Text, %gui_control_options% vgui_main_title, What you want? Baby, I got it.
Gui, Font, s10, Segoe UI
Gui, Add, Edit, %gui_control_options% vPedersen gFindus
Gui, Show,, myGUI
Expand All @@ -60,43 +42,19 @@ gui_spawn:
;-------------------------------------------------------------------------------
; GUI FUNCTIONS AND SUBROUTINES
;-------------------------------------------------------------------------------
; Automatically triggered on Escape key:
GuiEscape:
gui_destroy()
return

; The callback function when the text changes in the input field.
Findus:
Gui, Submit, NoHide
#Include %A_ScriptDir%\GUI\UserCommands.ahk
return

;
; gui_destroy: Destroy the GUI after use.
;
#WinActivateForce
gui_destroy() {
global gui_state
global gui_search_title

gui_state = closed
; Forget search title variable so the next search does not re-use it
; in case the next search does not set its own:
gui_search_title =

; Clear the tooltip
Gosub, gui_tooltip_clear

; Hide GUI
Gui, Destroy

; Bring focus back to another window found on the desktop
Gui, 2:Destroy ; Close help dialog
Gui, Destroy ; Close main GUI
WinActivate
}

gui_change_title(message,color = "") {
; If parameter color is omitted, the message is assumed to be an error
; message, and given the color red.
If color =
{
global cRed
Expand All @@ -105,19 +63,16 @@ gui_change_title(message,color = "") {
GuiControl,, gui_main_title, %message%
Gui, Font, s11 %color%
GuiControl, Font, gui_main_title
Gui, Font, s10 cffffff ; reset
Gui, Font, s10 cffffff
}

;-------------------------------------------------------------------------------
; SEARCH ENGINES
;-------------------------------------------------------------------------------
;
; gui_search_add_elements: Add GUI controls to allow typing of a search query.
;
gui_search_add_elements:
Gui, Add, Text, %gui_control_options% %cYellow%, %gui_search_title%
Gui, Add, Edit, %gui_control_options% %cYellow% vgui_SearchEdit -WantReturn
Gui, Add, Button, x-10 y-10 w1 h1 +default ggui_SearchEnter ; hidden button
Gui, Add, Button, x-10 y-10 w1 h1 +default ggui_SearchEnter
GuiControl, Disable, Pedersen
Gui, Show, AutoSize
return
Expand All @@ -127,14 +82,8 @@ gui_search(url) {
if gui_state != search
{
gui_state = search
; if gui_state is "main", then we are coming from the main window and
; GUI elements for the search field have not yet been added.
Gosub, gui_search_add_elements
}

; Assign the url to a variable.
; The variables will have names search_url1, search_url2, ...

search_urls := search_urls + 1
search_url%search_urls% := url
}
Expand All @@ -153,60 +102,82 @@ gui_SearchEnter:

;-------------------------------------------------------------------------------
; TOOLTIP
; The tooltip shows all defined commands, along with a description of what
; each command does. It gets the description from the comments in UserCommands.ahk.
; The code was improved and fixed for Windows 10 with the help of schmimae.
;-------------------------------------------------------------------------------
gui_tooltip_clear:
ToolTip
return

gui_commandlibrary:
; hidden GUI used to pass font options to tooltip:
CoordMode, Tooltip, Screen ; To make sure the tooltip coordinates is displayed according to the screen and not active window
Gui, 2:Font,s10, Lucida Console
Gui, 2:Add, Text, HwndhwndStatic

tooltiptext =
maxpadding = 0
StringCaseSense, Off ; Matching to both if/If in the IfInString command below
Loop, read, %A_ScriptDir%/GUI/UserCommands.ahk
SysGet, Mon1, Monitor
xOffset := Mon1Left + 50
yOffset := Mon1Top + 50

tooltiptext := ""
currentCategory := ""

Loop, read, %A_ScriptDir%\GUI\UserCommands.ahk
{
; search for the string If Pedersen =, but search for each word individually because spacing between words might not be consistent. (might be improved with regex)
If Substr(A_LoopReadLine, 1, 1) != ";" ; Do not display commented commands
{
If A_LoopReadLine contains if
{
IfInString, A_LoopReadLine, Pedersen
IfInString, A_LoopReadLine, =
{
StringGetPos, setpos, A_LoopReadLine,=
StringTrimLeft, trimmed, A_LoopReadLine, setpos+1 ; trim everything that comes before the = sign
StringReplace, trimmed, trimmed, `%A_Space`%,{space}, All
tooltiptext .= trimmed
tooltiptext .= "`n"

; The following is used to correct padding:
StringGetPos, commentpos, trimmed,`;
if (maxpadding < commentpos)
maxpadding := commentpos
}
}
if InStr(A_LoopReadLine, ";;;") {
currentCategory := RegExReplace(A_LoopReadLine, "[;]")
currentCategory := Trim(currentCategory)
tooltiptext .= "`n=== " . currentCategory . " ===`n`n"
continue
}

if InStr(A_LoopReadLine, "Pedersen =") {
RegExMatch(A_LoopReadLine, "Pedersen = (\w+).*?; (.*)", match)
if (match1 && match2)
tooltiptext .= match1 . "`t`t" . match2 . "`n"
}
}
tooltiptextpadded =
Loop, Parse, tooltiptext,`n
{
line = %A_LoopField%
StringGetPos, commentpos, line, `;
spaces_to_insert := maxpadding - commentpos
Loop, %spaces_to_insert%
{
StringReplace, line, line,`;,%A_Space%`;

Gui, 2:Destroy
Gui, 2:+AlwaysOnTop -Caption +ToolWindow
Gui, 2:Font, s11, Consolas
Gui, 2:Add, Edit, vCommandList ReadOnly VScroll w400 h600 -WantReturn, %tooltiptext%

Gui, 2:Show, x%xOffset% y%yOffset%

ControlSend, Edit1, ^{Home}, A
ControlFocus, Edit1, myGUI

return

;-------------------------------------------------------------------------------
; HOTKEYS AND COMMAND HANDLING
;-------------------------------------------------------------------------------
CapsLock & Space::
if (gui_state = "closed") {
Gosub, gui_spawn
}
return

#If WinExist("ahk_class AutoHotkeyGUI")
CapsLock & Space::
if (gui_state != "closed") {
Gui, 2:Destroy ; Close help dialog
gui_destroy() ; Close command line
return
}
Gosub, gui_spawn
return

#If

#If WinActive("myGUI")
^Backspace::
SendInput, ^+{Left}{BS}
#If

Findus:
Gui, Submit, NoHide
if (Pedersen) { ; Only proceed if there's input
oldPedersen := Pedersen ; Store the current value
#Include %A_ScriptDir%\GUI\UserCommands.ahk
if (oldPedersen != Pedersen && Pedersen != "?") { ; Close only if a command was executed AND it's not the ? command
Gui, 2:Destroy ; Close help dialog first
gui_destroy() ; Then close command line
}
tooltiptextpadded .= line
tooltiptextpadded .= "`n"
}
Sort, tooltiptextpadded
ToolTip %tooltiptextpadded%, 3, 3, 1
return
#If
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,3 +110,11 @@ The url is now passed as a parameter instead:
gui_search("https://www.youtube.com/results?search_query=REPLACEME")

Additionally, the tooltip was revived for Windows 10 and improved with the help of Github user schmimae.

### Addition to the script
#### February 5, 2025
In this version:
the tooltip is scrollable so that all inputs are visible
the inputs displayed in the tooltip are organized by category (based on the UserCommands.ahk file)
adapted the formatting of the tooltip for better readability
you can now delete entire words in the UI using ctrl + backspace