Merge branch 'master' of gitee.com:klesh/dotfiles

This commit is contained in:
Klesh Wong 2021-07-23 18:47:44 +08:00
commit cec93b201e
5 changed files with 256 additions and 208 deletions

View File

@ -29,13 +29,29 @@ PasteAsUnixPath() {
PasteAs(convertFuncName) {
%convertFuncName%()
Send ^v
Paste()
Sleep 500
}
Copy() {
if WinActive("ahk_exe WindowsTerminal.exe") {
Send, ^+c
} else {
Send, ^c
}
}
Paste() {
if WinActive("ahk_exe WindowsTerminal.exe") {
Send, ^+v
} else {
Send, ^v
}
}
CopyAndFetch() {
Clipboard =
Send, ^c ; simulate Ctrl+C (=selection in clipboard)
Copy()
ClipWait, 0, 1 ; wait until clipboard contains data
selection = %Clipboard% ; save the content of the clipboard
return selection
@ -58,3 +74,8 @@ LoadAlternativeToClipboard() {
global ALTERNATIVE_CLIPBOARD
Clipboard = %ALTERNATIVE_CLIPBOARD%
}
CopyClipboardToAlternative() {
global ALTERNATIVE_CLIPBOARD
ALTERNATIVE_CLIPBOARD = %ClipBoardAll%
}

View File

@ -1,161 +0,0 @@
;------------------------------
;
; Function: WinGetPosEx
;
; Original author: jballi (https://autohotkey.com/boards/viewtopic.php?t=3392)
;
; Update author: RiseUp
;
; Description:
;
; Gets the position, size, and offset of a window. See the *Remarks* section
; for more information.
;
; Parameters:
;
; hWindow - Handle to the window.
;
; X, Y, Width, Height - Output variables. [Optional] If defined, these
; variables contain the coordinates of the window relative to the
; upper-left corner of the screen (X and Y), and the Width and Height of
; the window.
;
; Offset_Left, Offset_Top, Offset_Right, Offset_Bottom - Output variables.
; [Optional] Offset, in pixels, of the actual position of the window
; versus the position of the window as reported by GetWindowRect. If
; moving the window to specific coordinates, add these offset values to
; the appropriate coordinate (X and/or Y) to reflect the true size of the
; window.
;
; Returns:
;
; If successful, the address of a RECTPlus structure is returned. The first
; 16 bytes contains a RECT structure that contains the dimensions of the
; bounding rectangle of the specified window. The dimensions are given in
; screen coordinates that are relative to the upper-left corner of the screen.
; The next 16 bytes contain the offsets (4-byte integer for each of left,
; top, right, and bottom offsets).
;
; Also if successful (and if defined), the output variables (X, Y, Width,
; Height, Offset_Left, Offset_Top, Offset_Right, and Offset_Bottom) are
; updated. See the *Parameters* section for more more information.
;
; If not successful, FALSE is returned.
;
; Requirement:
;
; Windows 2000+
;
; Remarks, Observations, and Changes:
;
; * Starting with Windows Vista, Microsoft includes the Desktop Window Manager
; (DWM) along with Aero-based themes that use DWM. Aero themes provide new
; features like a translucent glass design with subtle window animations.
; Unfortunately, the DWM doesn't always conform to the OS rules for size and
; positioning of windows. If using an Aero theme, many of the windows are
; actually larger than reported by Windows when using standard commands (Ex:
; WinGetPos, GetWindowRect, etc.) and because of that, are not positioned
; correctly when using standard commands (Ex: gui Show, WinMove, etc.). This
; function was created to 1) identify the true position and size of all
; windows regardless of the window attributes, desktop theme, or version of
; Windows and to 2) identify the appropriate offset that is needed to position
; the window if the window is a different size than reported.
;
; * The true size, position, and offset of a window cannot be determined until
; the window has been rendered. See the example script for an example of how
; to use this function to position a new window.
;
; * 20150906: The "dwmapi\DwmGetWindowAttribute" function can return odd errors
; if DWM is not enabled. One error I've discovered is a return code of
; 0x80070006 with a last error code of 6, i.e. ERROR_INVALID_HANDLE or "The
; handle is invalid." To keep the function operational during this types of
; conditions, the function has been modified to assume that all unexpected
; return codes mean that DWM is not available and continue to process without
; it. When DWM is a possibility (i.e. Vista+), a developer-friendly messsage
; will be dumped to the debugger when these errors occur.
;
; * 20171126: (RiseUp) Changed function to return 4 offset values instead of 2.
; Windows 10 has different offsets for the top versus the bottom of a window,
; so this function no longer assumes a symmetrical offset border around a
; given window.
;
; Credit:
;
; Idea and some code from *KaFu* (AutoIt forum)
;
;-------------------------------------------------------------------------------
WinGetPosEx(hWindow,ByRef X="",ByRef Y="",ByRef Width="",ByRef Height=""
,ByRef Offset_Left="",ByRef Offset_Top=""
,ByRef Offset_Right="",ByRef Offset_Bottom="")
{
Static Dummy5693
,RECTPlus
,S_OK:=0x0
,DWMWA_EXTENDED_FRAME_BOUNDS:=9
;-- Workaround for AutoHotkey Basic
PtrType:=(A_PtrSize=8) ? "Ptr":"UInt"
;-- Get the window's dimensions
; Note: Only the first 16 bytes of the RECTPlus structure are used by the
; DwmGetWindowAttribute and GetWindowRect functions.
VarSetCapacity(RECTPlus,32,0)
DWMRC:=DllCall("dwmapi\DwmGetWindowAttribute"
,PtrType,hWindow ;-- hwnd
,"UInt",DWMWA_EXTENDED_FRAME_BOUNDS ;-- dwAttribute
,PtrType,&RECTPlus ;-- pvAttribute
,"UInt",16) ;-- cbAttribute
if (DWMRC<>S_OK)
{
if ErrorLevel in -3,-4 ;-- Dll or function not found (older than Vista)
{
;-- Do nothing else (for now)
}
else
outputdebug,
(ltrim join`s
Function: %A_ThisFunc% -
Unknown error calling "dwmapi\DwmGetWindowAttribute".
RC=%DWMRC%,
ErrorLevel=%ErrorLevel%,
A_LastError=%A_LastError%.
"GetWindowRect" used instead.
)
;-- Collect the position and size from "GetWindowRect"
DllCall("GetWindowRect",PtrType,hWindow,PtrType,&RECTPlus)
}
;-- Populate the output variables
X:=Left := NumGet(RECTPlus,0,"Int")
Y:=Top := NumGet(RECTPlus,4,"Int")
Right := NumGet(RECTPlus,8,"Int")
Bottom := NumGet(RECTPlus,12,"Int")
Width := Right-Left
Height := Bottom-Top
Offset_Left := 0
Offset_Top := 0
Offset_Right := 0
Offset_Bottom := 0
;-- If DWM is not used (older than Vista or DWM not enabled), we're done
if (DWMRC<>S_OK)
Return &RECTPlus
;-- Collect dimensions via GetWindowRect
VarSetCapacity(RECT,16,0)
DllCall("GetWindowRect",PtrType,hWindow,PtrType,&RECT)
GWR_Left := NumGet(RECT,0,"Int")
GWR_Top := NumGet(RECT,4,"Int")
GWR_Right := NumGet(RECT,8,"Int")
GWR_Bottom := NumGet(RECT,12,"Int")
;-- Calculate offsets and update output variables
NumPut(Offset_Left := Left - GWR_Left,RECTPlus,16,"Int")
NumPut(Offset_Top := Top - GWR_Top ,RECTPlus,20,"Int")
NumPut(Offset_Right := GWR_Right - Right ,RECTPlus,24,"Int")
NumPut(Offset_Bottom := GWR_Bottom - Bottom ,RECTPlus,28,"Int")
Return &RECTPlus
}

View File

@ -32,7 +32,7 @@
InitWindowManager() {
LogDebug("InitWindowManager")
global RATIO := 0.618
; global RATIO := 0.618
global RATIO := 0.382
global ID_SEEN := Object()
global ARRANGEMENT := Object()
@ -64,19 +64,32 @@ FocusWinByPos(x, y) {
GetCursorMonGeometry(ByRef x, ByRef y, ByRef w, ByRef h) {
MouseGetPos, MouseX, MouseY
GetMonGeometryByPos(MouseX, MouseY, x, y, w, h)
}
GetActiveWindowMonGeometry(ByRef x, ByRef y, ByRef w, ByRef h) {
WinGetPos, wx, wy, ww, wh, A
GetMonGeometryByPos(wx + ww / 2, wy + wh / 2, x, y, w, h)
}
GetMonGeometryByPos(px, py, ByRef x, ByRef y, ByRef w, ByRef h) {
SysGet, mc, MonitorCount
; find current monitor
mi := 0
mi := 1
loop {
SysGet, mon, MonitorWorkArea, %mi%
if (monLeft < MouseX and monRight > MouseX) {
x := monLeft
y := monTop
w := monRight - monLeft
h := monBottom - monTop
return
}
if (mi > mc) {
break
}
SysGet, mon, MonitorWorkArea, %mi%
if (monLeft < px and monRight > px and monTop < py and monBottom > py) {
x := monLeft
y := monTop
w := monRight - monLeft
h := monBottom - monTop
return
}
mi := mi + 1
}
LogDebug("unable to find monitor for pos {1}, {2}", px, py)
}
FocusWinByDirection(direction) {
@ -89,6 +102,91 @@ FocusWinByDirection(direction) {
FocusWinByPos(x + w * wf, y + h * hf)
}
GetActiveWindowMargins(hwnd, monX, monY, monW, monH, ByRef l, ByRef t, ByRef r, ByRef b) {
Static Dummy5693
,RECTPlus
,S_OK:=0x0
,DWMWA_EXTENDED_FRAME_BOUNDS:=9
;-- Workaround for AutoHotkey Basic
PtrType:=(A_PtrSize=8) ? "Ptr":"UInt"
;-- Get the window's dimensions (excluding shadows)
; Note: Only the first 16 bytes of the RECTPlus structure are used by the
; DwmGetWindowAttribute and GetWindowRect functions.
VarSetCapacity(RECTPlus,32,0)
DWMRC:=DllCall("dwmapi\DwmGetWindowAttribute"
,PtrType,hwnd ;-- hwnd
,"UInt",DWMWA_EXTENDED_FRAME_BOUNDS ;-- dwAttribute
,PtrType,&RECTPlus ;-- pvAttribute
,"UInt",16) ;-- cbAttribute
if (DWMRC<>S_OK)
{
if ErrorLevel in -3,-4 ;-- Dll or function not found (older than Vista)
{
;-- Do nothing else (for now)
}
else
{
outputdebug,
(ltrim join`s
Function: %A_ThisFunc% -
Unknown error calling "dwmapi\DwmGetWindowAttribute".
RC=%DWMRC%,
ErrorLevel=%ErrorLevel%,
A_LastError=%A_LastError%.
"GetWindowRect" used instead.
)
}
;-- Collect the position and size from "GetWindowRect"
DllCall("GetWindowRect",PtrType,hWindow,PtrType,&RECTPlus)
}
;-- Populate the output variables
x1 := NumGet(RECTPlus,0,"Int")
y1 := NumGet(RECTPlus,4,"Int")
x2 := NumGet(RECTPlus,8,"Int")
y2 := NumGet(RECTPlus,12,"Int")
WinGetPos, winX, winY, winW, winH, A
;-- Convert to scaled unit
scale := Round((x2 - x1) / winW*10)/10
x1 := (x1 - monX) / scale + monX
y1 := (y1 - monY) / scale + monY
x2 := (x2 - monX) / scale + monX
y2 := (y2 - monY) / scale + monY
w := (x2 - x1)
h := (y2 - y1)
l := x1 - winX
t := y1 - winY
r := winX+winW-x2
b := winY+winH-y2
LogDebug("active window margins: {1} {2} {3} {4}", l, t, r, b)
}
ToggleActiveWinMaximum() {
WinGet, isMax, MinMax, A
if (isMax) {
WinRestore, A
} else {
WinMaximize, A
}
}
ArrangeActiveWindow(method) {
if (method = "monocle") {
WinGet, isMax, MinMax, A
if (not isMax) {
WinMaximize, A
}
} else {
MoveActiveWinByDirection(method)
}
SaveActiveWindowDirection(method)
}
MoveActiveWinByDirection(direction) {
WinGet, isMax, MinMax, A
if (isMax) {
@ -96,31 +194,105 @@ MoveActiveWinByDirection(direction) {
}
global RATIO
global PADDING
GetCursorMonGeometry(x, y, w, h)
activeWinId := WinExist("A")
WinGetPosEx(activeWinId, wx, wy, ww, wh, l, t, r, b)
GetActiveWindowMonGeometry(x, y, w, h)
GetActiveWindowMargins(activeWinId, x, y, w, h, l, t, r, b)
LogDebug("monitor geometry x: {1}, y: {2}, w: {3}, h: {4}", x, y, w, h)
; left
wx := x
wy := y
ww := floor(w * RATIO)
wh := h
LogDebug("left geometry: x: {1}, y: {2}, w: {3}, h: {4}", wx, wy, ww, wh)
; right
if (direction = "right") {
wx := ww + floor(PADDING / 2)
wx := wx + ww + floor(PADDING / 2)
ww := w - ww
LogDebug("right geometry: x: {1}, y: {2}, w: {3}, h: {4}", wx, wy, ww, wh)
} else {
wx := wx + PADDING
}
; adjust for aero margins
wx := wx - l
wy := wy - t
ww := ww + l + r
wh := wh + t + b
; padding
ww := ww - floor(PADDING * 1.5)
wy := wy + PADDING
wh := wh - PADDING * 2
WinMove, A,, wx - l, wy - t, ww + l + r, wh + t + b
LogDebug(Format("move {1} to {2}", activeWinId, direction))
SaveActiveWindowDirection(direction)
WinMove, A,, wx, wy, ww, wh
LogDebug("move win to x: {1}, y: {2}, w: {3}, h: {4}", wx, wy, ww, wh)
}
GetCursorNearestMonitor(direction, ByRef ml, ByRef mt, ByRef mr, ByRef mb) {
MouseGetPos x, y
if (direction = "right") { ; start at right most, and search for nearest monitor
ml := 10000
mt := 10000
mr := 10000
mb := 10000
} else {
ml := -10000
mt := -10000
mr := -10000
mb := -10000
}
SysGet, mc, MonitorCount
mi := 1
loop {
if (mi > mc) {
break
}
SysGet, mon, MonitorWorkArea, %mi%
if (direction = "right") {
if (monLeft > x and monLeft < ml) {
ml := monLeft
mt := monTop
mr := monRight
mb := monBottom
}
} else {
if (monRight < x and monRight > mr) {
ml := monLeft
mt := monTop
mr := monRight
mb := monBottom
}
}
mi := mi + 1
}
return Abs(ml) != 10000
}
MoveCursorToMonitor(direction) {
if (GetCursorNearestMonitor(direction, ml, mt, mr, mb)) {
SetCursorPos((ml+mr)/2, (mt + mb)/2)
FocusWinUnderCursor()
}
}
MoveWindowToMonitor(direction) {
WinRestore, A
if (GetCursorNearestMonitor(direction, ml, mt, mr, mb)) {
mw := mr - ml
mh := mb - mt
LogDebug("move win to mon size: {1}, {2}, {3}, {4}, {5}, {6}", ml, mt, mr, mb, mw, mh)
ww := mw * 0.5
wh := mh * 0.5
wx := ml + ww / 2
wy := mt + wh / 2
LogDebug("move win to mon: {1}, {2}, {3}, {4}", wx, wy, ww, wh)
WinMove, A,, wx, wy, ww, wh
ArrangeActiveWindowFromStorage()
SetCursorToCenterOfActiveWin()
}
}
SaveArrangement() {
LogDebug("SaveArrangement start")
global ARRANGEMENT
global ARRANGEMENT_PATH
LogDebug("SaveArrangement to {1} start", ARRANGEMENT_PATH)
file := FileOpen(ARRANGEMENT_PATH, "w")
file.Write(JSON.Dump(ARRANGEMENT,, 2))
file.Close()
@ -130,7 +302,7 @@ SaveArrangement() {
LoadArrangement() {
global ARRANGEMENT
global ARRANGEMENT_PATH
LogDebug("LoadArrangement start " .ARRANGEMENT_PATH)
LogDebug("LoadArrangement start {1}", ARRANGEMENT_PATH)
try {
FileRead, temp, %ARRANGEMENT_PATH%
ARRANGEMENT := JSON.Load(temp)
@ -155,7 +327,8 @@ LoadArrangement() {
GetActiveWindowPath() {
WinGet processPath, ProcessPath, A
WinGetClass windowClass, A
return processPath . ":" . windowClass
GetActiveWindowMonGeometry(x, y, w, h)
return Format("{1}_{2}\{3}\{4}", w, h, processPath, windowClass)
}
IsActiveWindowSeen() {
@ -231,26 +404,22 @@ ActiveWinInfo() {
return Format("{1}:{2}[{3}]{4}", processPath, klass, id, title)
}
ArrangeActiveWindowFromStorage() {
global ARRANGEMENT
windowPath := GetActiveWindowPath()
if ARRANGEMENT["windows"].HasKey(windowPath) {
ArrangeActiveWindow(ARRANGEMENT["windows"][windowPath])
}
}
AdjustNewWindow() {
seen := IsActiveWindowSeen()
arrangable := IsActiveWindowArrangable()
wininfo := ActiveWinInfo()
if not seen {
LogDebug(Format("win: {1}, seen: {2}, arrangable: {3}", wininfo, seen, arrangable))
LogDebug("win: {1}, seen: {2}, arrangable: {3}", wininfo, seen, arrangable)
}
if not seen and arrangable {
windowPath := GetActiveWindowPath()
if ARRANGEMENT["windows"].HasKey(windowPath) {
MoveActiveWinByDirection(ARRANGEMENT["windows"][windowPath])
}
}
}
ToggleActiveWinMaximum() {
WinGet, isMax, MinMax, A
if (isMax) {
WinRestore, A
} else {
WinMaximize, A
ArrangeActiveWindowFromStorage()
}
}

View File

@ -0,0 +1,2 @@
schtasks /create /tn WSLClockSync /tr "wsl.exe sudo hwclock -s" /sc onevent /ec system /mo "*[System[Provider[@Name='Microsoft-Windows-Kernel-General'] and (EventID=1)]]"
Set-ScheduledTask WSLClockSync -Settings (New-ScheduledTaskSettingsSet -AllowStartIfOnBatteries)

View File

@ -7,32 +7,44 @@ CoordMode, Mouse, Screen ; mouse coordinates relative to the screen
; =========================
; DEBUGGING
; =========================
global DEBUGGING := true
global DEBUGGING := False
ToggleDebugging() {
global DEBUGGING
DEBUGGING := not DEBUGGING
}
LogDebug(msg) {
LogDebug(params*) {
global DEBUGGING
if (not DEBUGGING) {
return
}
FormatTIme, now, , MM-dd HH:mm:ss
log := FileOpen("d:\win.ahk.log", "a")
log.WriteLine(Format("[{1}] {2}", now, msg))
log.WriteLine(Format("[{1}] {2}", now, Format(params*)))
log.Close()
}
SetDisableLockWorkstationRegKeyValue(value) {
RegWrite, REG_DWORD, HKEY_CURRENT_USER, Software\Microsoft\Windows\CurrentVersion\Policies\System, DisableLockWorkstation, %value%
}
LockWorkStation() {
SetDisableLockWorkstationRegKeyValue( 0 )
; Lock
DllCall( "User32\LockWorkStation" )
; Disable locking again
SetDisableLockWorkstationRegKeyValue( 1 )
}
; =========================
; LIBS
; =========================
InitWindowManager()
InitClipboardManager()
SetDisableLockWorkstationRegKeyValue(1) ; in order to remap win+l
#Include, ahk\JSON.ahk
#Include, ahk\WinGetPosEx.ahk
#Include, ahk\WindowManager.ahk
#Include, ahk\ClipboardManager.ahk
@ -58,32 +70,37 @@ InitClipboardManager()
; Win + \ => Toggle mute
#\::Send {Volume_Mute}
; Win + backspace => Lock
#BS::#l
#BS::LockWorkStation()
; WINDOW MANAGER
; Win + f => Toggle window maximum
#f:: ToggleActiveWinMaximum()
; Win + j => Focus right window
#j:: FocusWinByDirection("right")
; Win + k => Focus left window
#k:: FocusWinByDirection("left")
; Win + f => Move active window as monocle
#f::ArrangeActiveWindow("monocle")
; Win + Shift + j => Move active window to right side
#+j::MoveActiveWinByDirection("right")
#+j::ArrangeActiveWindow("right")
; Win + Shift + k => Move active window to left side
#+k::MoveActiveWinByDirection("left")
#+k::ArrangeActiveWindow("left")
; Win + Shift + b => Blacklist active window so it won't be arranged when launched
#+b::BlacklistArrangementForActiveWindow()
; Win + Shift + b => Whitelist active window so it always be arranged when launched
#+w::WhitelistArrangementForActiveWindow()
; Win + Shift + i => Remove active window from Blacklist/Whitelist
#+i::IgnoreArrangementForActiveWindow()
#+g::IgnoreArrangementForActiveWindow()
; Win + Shift + d => Toggle debug logging
#+d::ToggleDebugging()
#u::MoveCursorToMonitor("left")
#i::MoveCursorToMonitor("right")
#+u::MoveWindowToMonitor("left")
#+i::MoveWindowToMonitor("right")
; CLIPBOARD MANAGER
#c::AlternativeCopy()
#+c::CopyClipboardToAlternative()
#v::AlternativePaste()
@ -109,4 +126,4 @@ InitClipboardManager()
; Send {~}
; SetCapsLockState % !GetKeyState("CapsLock", "T")
; Return
; Capslock::return
; Capslock::return