dotfiles/win/ahk/Spotlight.ahk

172 lines
4.7 KiB
AutoHotkey
Raw Normal View History

Spotlight() {
global
2021-08-04 16:47:38 +00:00
SPOTLIGHT_FONT_NAME := "Sarasa Mono SC"
SPOTLIGHT_ITEM_FONT_STYLE := "s14 ceeeeee"
SPOTLIGHT_SELECTED_ITEM_FONT_STYLE := "s14 c007acc"
2021-08-04 16:47:38 +00:00
SPOTLIGHT_ITEMS := 10
SPOTLIGHT_SELECTED_INDEX := 1
SPOTLIGHT_SELECTED_ITEM := null
; window position
GetCursorMonGeometry(x, y, w, h)
ww := w * 0.5
wh := h * 0.3
wx := x + w / 2 - ww / 2
wy := y + h / 2 - wh / 2
2021-08-04 16:47:38 +00:00
Gui, New, +Theme -Border +AlwaysOnTop -0xc00000, Spotlight
Gui, Font, %SPOTLIGHT_ITEM_FONT_STYLE%, %SPOTLIGHT_FONT_NAME%
Gui, Color, 252526
; search box
2021-08-04 16:47:38 +00:00
userInputX := 10
userInputY := 10
userInputW := ww - 20
userInputH := 30
Gui, Add, Edit, x%userInputX% y%userInputY% w%userInputW% h%userInputH% c252525 gOnChange vUserInputEdit
; ok button
Gui, Add, Button, Default Hidden gOnEnter, Ok
; result items
Loop %SPOTLIGHT_ITEMS% {
ItemEdit%A_Index% := null
2021-08-04 16:47:38 +00:00
Gui, Add, Text, w%userInputW% vItemEdit%A_Index%
}
2021-08-04 16:47:38 +00:00
; parse keyword
SpotlightProcessUserInput()
; show window on center
Gui, Show, X %wx% Y %wy% W %ww% H %wh%
Return
OnChange:
2021-08-04 16:47:38 +00:00
local userInput := ""
GuiControlGet, userInput, , UserInputEdit
SpotlightProcessUserInput(userInput)
Return
OnEnter:
2021-08-04 16:47:38 +00:00
SpotlightProcessUserInput(userInput, 1)
Gui, Destroy
Return
GuiEscape:
Gui, Destroy
Return
}
2021-08-04 16:47:38 +00:00
SpotlightProcessUserInput(userInput := "", stage := 0) {
global SPOTLIGHT_SELECTED_INDEX := 1
mode := ""
idx := 0
; extract "<mode-sign> " as mode, current only '>' as command mode
idx := RegExMatch(userInput, ">")
if (idx) {
mode := SubStr(userInput, 1, 1)
userInput := SubStr(userInput, 2)
}
2021-08-04 16:47:38 +00:00
; extract " <number>" as selected index
idx := RegExMatch(userInput, " \d")
if (idx) {
2021-08-04 16:47:38 +00:00
SPOTLIGHT_SELECTED_INDEX := SubStr(userInput, idx+1)
userInput := SubStr(userInput, 1, idx-1)
}
LogDebug("SPOTLIGHT >>> mode {1} input {2} stage {3}", mode, userInput, stage)
; enter mode subroutine
if (mode = ">") {
SpotlightRunCommand(userInput, stage)
} else {
SpotlightSwitchWindow(userInput, stage)
}
}
SpotlightUpdateItems(items) {
global
LogDebug("SPOTLIGHT >>> update ui items, count {1}, first text {2}", items.Length(), items[1][1])
; make sure items within limit
while (items.Length() > SPOTLIGHT_ITEMS) {
items.Pop()
}
2021-08-04 16:47:38 +00:00
; update first n lines
for itemIndex, item in items {
itemText := item[1]
if (itemIndex = SPOTLIGHT_SELECTED_INDEX) {
Gui, Font, %SPOTLIGHT_SELECTED_ITEM_FONT_STYLE%, %SPOTLIGHT_FONT_NAME%
itemText := itemText . " <<< "
SPOTLIGHT_SELECTED_ITEM := item
} else {
Gui, Font, %SPOTLIGHT_ITEM_FONT_STYLE%, %SPOTLIGHT_FONT_NAME%
}
2021-08-04 16:47:38 +00:00
lineText := Format("{1} {2}", itemIndex, itemText)
LogDebug("SPOTLIGHT >>> line {1}", lineText)
GuiControl, Font, ItemEdit%itemIndex%
GuiControl, , ItemEdit%itemIndex%, %lineText%
}
2021-08-04 16:47:38 +00:00
; clear contents of rest lines
While itemIndex <= SPOTLIGHT_ITEMS {
itemIndex := itemIndex + 1
GuiControl, , ItemEdit%itemIndex%
}
}
2021-08-04 16:47:38 +00:00
SpotlightRunCommand(command, stage) {
funcName := Format("cmd_{1}", command)
if (IsFunc(funcName)) {
%funcName%()
}
}
SpotlightSwitchWindow(keyword, stage) {
global SPOTLIGHT_SELECTED_ITEM
LogDebug("SPOTLIGHT >>> switch window mode, keyword {1}, stage {2}", keyword, stage)
if (stage = 1) {
winId := SPOTLIGHT_SELECTED_ITEM[2]
LogDebug("SPOTLIGHT >>> switch to window {1} item length {2}", winId, SPOTLIGHT_SELECTED_ITEM.Length())
WinActivate, ahk_id %winId%
SetCursorToCenterOfActiveWin()
return
}
items := Array()
WinGet windows, List
Loop %windows% {
2021-08-04 16:47:38 +00:00
winId := windows%A_Index%
if (A_DefaultGui = winId) {
LogDebug("SPOTLIGHT >>> skip default gui: {2}", winId, A_DefaultGui)
Continue
}
WinGetTitle winTitle, ahk_id %winId%
if (!winTitle) {
Continue
}
2021-08-04 16:47:38 +00:00
WinGet procName, ProcessName, ahk_id %winId%
itemText := Format("{1:-20} {2}", procName, winTitle)
if (itemText and !InStr(itemText, keyword)) {
Continue
}
items.Push(Array(itemText, winId))
}
SpotlightUpdateItems(items)
}
cmd_wt() {
2021-08-05 02:31:46 +00:00
TIMEZONES := { "North American": -3, "Hezheng Yin": -7, "Camille": +2 }
2021-08-04 16:47:38 +00:00
items := Array()
for zone, offset in TIMEZONES {
dt = %A_NowUTC%
dt += offset, Hours
2021-08-05 02:31:46 +00:00
FormatTime, timeText, %dt%, dd ddd HH:mm
2021-08-04 16:47:38 +00:00
items.Push(Array(Format("{1:-20} {2}", zone, timeText)))
}
2021-08-04 16:47:38 +00:00
SpotlightUpdateItems(items)
}