Skip to content

Commit

Permalink
feat(CapsLockX): virtual desktop for win11
Browse files Browse the repository at this point in the history
virtual desktop for win11
  • Loading branch information
snomiao committed May 19, 2024
1 parent e09a951 commit 75f2cde
Show file tree
Hide file tree
Showing 5 changed files with 4,057 additions and 2,965 deletions.
21 changes: 10 additions & 11 deletions DevTools/modulesTips.mjs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import fs from "fs";
import "sno-utils";
import { 表键筛 } from "sno-utils";
import { filter } from "rambda";
const 全部提取 = (s, p) => {
return (s.match(RegExp(p, p.flags)) || []).map((e) => {
console.log(e);
Expand All @@ -13,8 +12,8 @@ const 表按键排序 = (表) =>
Object.fromEntries(
Object.entries().sort(
([fn0, hk0], [fn1, hk1]) =>
hk0.length - hk1.length || hk0.localeCompare(hk1)
)
hk0.length - hk1.length || hk0.localeCompare(hk1),
),
);

// const 默认辅助键序列 = "#^+!".split('')
Expand Down Expand Up @@ -45,7 +44,7 @@ const 热键列提取 = (文件内容) => {
// const 函数列 = 全部提取(文件内容, /^(\S+)\(\)\s*?{/);
const 条件列 = 全部提取(
文件内容,
/(?<=\n|^)(#if\w*)[ \t]*(.*)\s*([\s\S]*?)(?:$|(?=#if))/gi
/(?<=\n|^)(#if\w*)[ \t]*(.*)\s*([\s\S]*?)(?:$|(?=#if))/gi,
);
const 热键列 = 条件列.flatMap(([判断类型, 条件, code]) => {
条件 = 条件.trim().replace(/;.*/, "");
Expand All @@ -67,7 +66,7 @@ const 热键列提取 = (文件内容) => {
...hkde
.filter(([hk]) => !hk.match(/Up$/))
.map(([hk, de]) => [de, hkp(hk)]),
])
]),
);
if (!Object.entries(指令热键表).length) {
return [];
Expand All @@ -91,15 +90,15 @@ const 条件热键对表列 = (
.map(async (文件名) => {
const 文件内容 = await fs.promises.readFile(
`${ModulesPath}/${文件名}`,
"utf8"
"utf8",
);
return 热键列提取(文件内容);
})
}),
)
).flat();
const 条件热键表 = 热键合并表(条件热键对表列);
const 函数条件热键表 = 表键筛(() => .match(/^\S+\(\)$/))(条件热键表);
const 非函数条件热键表 = 表键筛(() => !.match(/^\S+\(\)$/))(条件热键表);
const 函数条件热键表 = filter(() => .match(/^\S+\(\)$/))(条件热键表);
const 非函数条件热键表 = filter(() => !.match(/^\S+\(\)$/))(条件热键表);
console.log(JSON.stringify(非函数条件热键表, null, 4));

const QuickTipsUpdate = async (条件热键表) => {
Expand Down Expand Up @@ -141,7 +140,7 @@ const QuickTipsUpdate = async (条件热键表) => {
.replace(/^\uFEFF/, "")
.replace(
/^QuickTips\(\)\s*?{[\s\S]*?^}/gim,
`QuickTips(){\n${QuickTips}\n}`
`QuickTips(){\n${QuickTips}\n}`,
);
// console.log(dst);
await fs.promises.writeFile(QuickTipsAHK, dst);
Expand Down
56 changes: 43 additions & 13 deletions Modules/CLX-VirtualDesktop.ahk
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
; ref project when updated to win11:
; [MScholtes/VirtualDesktop: C# command line tool to manage virtual desktops in Windows 10]( https://github.com/MScholtes/VirtualDesktop )
;
; 2024-05-19 - [VirtualDesktop/VirtualDesktop11.cs at master · MScholtes/VirtualDesktop]( https://github.com/MScholtes/VirtualDesktop/blob/master/VirtualDesktop11.cs )
;

if (!CapsLockX) {
ExitApp
Expand Down Expand Up @@ -251,7 +253,7 @@ SwitchToDesktop(idx)
if (SwitchToDesktopByInternalAPI(idx)) {
; ok
} else if (SwitchToDesktopByHotkey(idx)) {
Tooltip, WARN SwitchToDesktopByHotkey %idx%
; Tooltip, WARN SwitchToDesktopByHotkey %idx%
} else {
Tooltip, WARN SwitchToDesktop FAILED
}
Expand Down Expand Up @@ -321,30 +323,55 @@ IsWindowOnCurrentVirtualDesktop(hWnd)
SwitchToDesktopByInternalAPI(idx)
{
succ := 0
IServiceProvider := ComObjCreate("{C2F03A33-21F5-47FA-B4BB-156362A2F239}", "{6D5140C1-7436-11CE-8034-00AA006009FA}")
IVirtualDesktopManagerInternal_Win11 := ComObjQuery(IServiceProvider, "{C5E0CDCA-7B6E-41B2-9FC4-D93975CC467B}", "{B2F925B9-5A0F-4D2E-9F4D-2B1507593C10}")
IVirtualDesktopManagerInternal_Win10 := ComObjQuery(IServiceProvider, "{C5E0CDCA-7B6E-41B2-9FC4-D93975CC467B}", "{F31574D6-B682-4CDC-BD56-1827860ABEC6}")
CLSID_ImmersiveShell := "{C2F03A33-21F5-47FA-B4BB-156362A2F239}"
CLSID_IServiceProvider10 := "{6D5140C1-7436-11CE-8034-00AA006009FA}"
CLSID_IVirtualDesktop_Win10 := "{FF72FFDD-BE7E-43FC-9C03-AD81681E88E4}"
CLSID_IVirtualDesktop_Win11 :="{536D3495-B208-4CC9-AE26-DE8111275BF8}"
CLSID_IVirtualDesktop_Win12 :="{3F07F4BE-B107-441A-AF0F-39D82529072C}"
CLSID_IVirtualDesktopManager := "{A5CD92FF-29BE-454C-8D04-D82879FB3F1B}"
CLSID_IVirtualDesktopManagerInternal_Win10 := "{F31574D6-B682-4CDC-BD56-1827860ABEC6}"
CLSID_IVirtualDesktopManagerInternal_Win11 := "{B2F925B9-5A0F-4D2E-9F4D-2B1507593C10}"
CLSID_IVirtualDesktopManagerInternal_Win12 := "{53F5CA0B-158F-4124-900C-057158060B27}"
CLSID_VirtualDesktopManager := "{AA509086-5CA9-4C25-8F95-589D3C07B48A}"
CLSID_VirtualDesktopManagerInternal := "{C5E0CDCA-7B6E-41B2-9FC4-D93975CC467B}"
CLSID_VirtualDesktopPinnedApps := "{B5A399E7-1C87-46B8-88E9-FC5747B171BD}"

IServiceProvider := ComObjCreate(CLSID_ImmersiveShell, CLSID_IServiceProvider10)
IVirtualDesktopManagerInternal_Win12 := ComObjQuery(IServiceProvider, CLSID_VirtualDesktopManagerInternal, CLSID_IVirtualDesktopManagerInternal_Win12)
IVirtualDesktopManagerInternal_Win11 := ComObjQuery(IServiceProvider, CLSID_VirtualDesktopManagerInternal, CLSID_IVirtualDesktopManagerInternal_Win11)
IVirtualDesktopManagerInternal_Win10 := ComObjQuery(IServiceProvider, CLSID_VirtualDesktopManagerInternal, CLSID_IVirtualDesktopManagerInternal_Win10)
win12 := !!IVirtualDesktopManagerInternal_Win12
win11 := !!IVirtualDesktopManagerInternal_Win11
win10 := !!IVirtualDesktopManagerInternal_Win10
IVirtualDesktopManagerInternal := !!IVirtualDesktopManagerInternal_Win11 ? IVirtualDesktopManagerInternal_Win11 : IVirtualDesktopManagerInternal_Win10
; tooltip win %win11% %win10%
_:= win12 && (IVirtualDesktopManagerInternal := IVirtualDesktopManagerInternal_Win12)
_:= win11 && (IVirtualDesktopManagerInternal := IVirtualDesktopManagerInternal_Win11)
_:= win12 && (IVirtualDesktopManagerInternal := IVirtualDesktopManagerInternal_Win12)

ToolTip win %win12% %win11% %win10%

ObjRelease(IServiceProvider)
if (IVirtualDesktopManagerInternal) {
; tooltip %idx%
GetCount := vtable(IVirtualDesktopManagerInternal, 3)
GetDesktops := vtable(IVirtualDesktopManagerInternal, 7)
SwitchDesktop := vtable(IVirtualDesktopManagerInternal, 9)

; TrayTip, , % IVirtualDesktopManagerInternal

pDesktopIObjectArray := 0
_ := win10 && DllCall(GetDesktops, "Ptr", IVirtualDesktopManagerInternal, "Ptr*", pDesktopIObjectArray)
_ := win12 && DllCall(GetDesktops, "Ptr", IVirtualDesktopManagerInternal, "Ptr*", pDesktopIObjectArray)
_ := win11 && DllCall(GetDesktops, "Ptr", IVirtualDesktopManagerInternal, "Ptr", 0, "Ptr*", pDesktopIObjectArray)
; Tooltip %pDesktopIObjectArray%
_ := win10 && DllCall(GetDesktops, "Ptr", IVirtualDesktopManagerInternal, "Ptr*", pDesktopIObjectArray)

; Tooltip % pDesktopIObjectArray
if (pDesktopIObjectArray) {
GetDesktopCount := vtable(pDesktopIObjectArray, 3)
GetDesktopAt := vtable(pDesktopIObjectArray, 4)
_ := win10 && DllCall(GetDesktopCount, "Ptr", IVirtualDesktopManagerInternal, "UInt*", DesktopCount)
_ := win12 && DllCall(GetDesktopCount, "Ptr", IVirtualDesktopManagerInternal, "UInt*", DesktopCount)
_ := win11 && DllCall(GetDesktopCount, "Ptr", IVirtualDesktopManagerInternal, "Ptr", 0, "UInt*", DesktopCount)
; tooltip 切换到桌面 %idx% / %DesktopCount%
_ := win10 && DllCall(GetDesktopCount, "Ptr", IVirtualDesktopManagerInternal, "UInt*", DesktopCount)

TrayTip, CapsLockX, % t("切换到桌面: ") . idx . "/" . DesktopCount
; if idx-th desktop doesn't exists then create a new desktop
if (idx > DesktopCount) {
diff := idx - DesktopCount
Expand All @@ -360,13 +387,16 @@ SwitchToDesktopByInternalAPI(idx)
SendEvent ^#{F4}
}
}
_ := win10 && GetGUIDFromString(IID_IVirtualDesktop, "{FF72FFDD-BE7E-43FC-9C03-AD81681E88E4}")
_ := win11 && GetGUIDFromString(IID_IVirtualDesktop, "{536D3495-B208-4CC9-AE26-DE8111275BF8}")
_ := win12 && GetGUIDFromString(IID_IVirtualDesktop, CLSID_IVirtualDesktop_Win12)
_ := win11 && GetGUIDFromString(IID_IVirtualDesktop, CLSID_IVirtualDesktop_Win11)
_ := win10 && GetGUIDFromString(IID_IVirtualDesktop, CLSID_IVirtualDesktop_Win10)
DllCall(GetDesktopAt, "Ptr", pDesktopIObjectArray, "UInt", idx - 1, "Ptr", &IID_IVirtualDesktop, "Ptr*", VirtualDesktop)
ObjRelease(pDesktopIObjectArray)
ToolTip, % "clx" . VirtualDesktop
if (VirtualDesktop) {
_ := win10 && DllCall(SwitchDesktop, "Ptr", IVirtualDesktopManagerInternal, "Ptr", VirtualDesktop)
_ := win12 && DllCall(SwitchDesktop, "Ptr", IVirtualDesktopManagerInternal, "Ptr", VirtualDesktop)
_ := win11 && DllCall(SwitchDesktop, "Ptr", IVirtualDesktopManagerInternal, "Ptr", 0, "Ptr", VirtualDesktop)
_ := win10 && DllCall(SwitchDesktop, "Ptr", IVirtualDesktopManagerInternal, "Ptr", VirtualDesktop)
ObjRelease(VirtualDesktop)
succ := idx
}
Expand Down
Loading

0 comments on commit 75f2cde

Please sign in to comment.