Skip to content
This repository has been archived by the owner on Dec 23, 2018. It is now read-only.

Toggle last desktop #65

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
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
1 change: 1 addition & 0 deletions settings.ini
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ NextTenDesktops=
[KeyboardShortcutsIdentifiers]
PreviousDesktop=Left
NextDesktop=Right
LastActiveDesktop=C
Desktop1=1
Desktop2=2
Desktop3=3
Expand Down
49 changes: 48 additions & 1 deletion virtual-desktop-enhancer.ahk
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ global taskbarID=0
global previousDesktopNo=0
global doFocusAfterNextSwitch=0
global hasSwitchedDesktopsBefore=1
global lastActiveDesktopNumber := _GetCurrentDesktopNumber()
global lastDesktopChangeTime := A_TickCount
global timeForDesktopToBeActive=1000

initialDesktopNo := _GetCurrentDesktopNumber()

Expand All @@ -96,6 +99,7 @@ hkModifiersMoveAndSwitch := KeyboardShortcutsModifiersMoveWindowAndSwitchToDes
hkModifiersPlusTen := KeyboardShortcutsModifiersNextTenDesktops
hkIdentifierPrevious := KeyboardShortcutsIdentifiersPreviousDesktop
hkIdentifierNext := KeyboardShortcutsIdentifiersNextDesktop
hkIdentifierLastActive := KeyboardShortcutsIdentifiersLastActiveDesktop
hkComboPinWin := KeyboardShortcutsCombinationsPinWindow
hkComboUnpinWin := KeyboardShortcutsCombinationsUnpinWindow
hkComboTogglePinWin := KeyboardShortcutsCombinationsTogglePinWindow
Expand Down Expand Up @@ -174,12 +178,15 @@ if (!(GeneralUseNativePrevNextDesktopSwitchingIfConflicting && _IsPrevNextDeskto
if (!(GeneralUseNativePrevNextDesktopSwitchingIfConflicting && _IsPrevNextDesktopSwitchingKeyboardShortcutConflicting(hkModifiersSwitch, hkIdentifierNext))) {
setUpHotkeyWithOneSetOfModifiersAndIdentifier(hkModifiersSwitch, hkIdentifierNext, "OnShiftRightPress", "[KeyboardShortcutsModifiers] SwitchDesktop, [KeyboardShortcutsIdentifiers] NextDesktop")
}
setUpHotkeyWithOneSetOfModifiersAndIdentifier(hkModifiersSwitch, hkIdentifierLastActive, "OnShiftLastActivePress", "[KeyboardShortcutsModifiers] SwitchDesktop, [KeyboardShortcutsIdentifiers] LastActiveDesktop")

setUpHotkeyWithOneSetOfModifiersAndIdentifier(hkModifiersMove, hkIdentifierPrevious, "OnMoveLeftPress", "[KeyboardShortcutsModifiers] MoveWindowToDesktop, [KeyboardShortcutsIdentifiers] PreviousDesktop")
setUpHotkeyWithOneSetOfModifiersAndIdentifier(hkModifiersMove, hkIdentifierNext, "OnMoveRightPress", "[KeyboardShortcutsModifiers] MoveWindowToDesktop, [KeyboardShortcutsIdentifiers] NextDesktop")
setUpHotkeyWithOneSetOfModifiersAndIdentifier(hkModifiersMove, hkIdentifierLastActive, "OnMoveLastActivePress", "[KeyboardShortcutsModifiers] MoveWindowToDesktop, [KeyboardShortcutsIdentifiers] LastActiveDesktop")

setUpHotkeyWithOneSetOfModifiersAndIdentifier(hkModifiersMoveAndSwitch, hkIdentifierPrevious, "OnMoveAndShiftLeftPress", "[KeyboardShortcutsModifiers] MoveWindowAndSwitchToDesktop, [KeyboardShortcutsIdentifiers] PreviousDesktop")
setUpHotkeyWithOneSetOfModifiersAndIdentifier(hkModifiersMoveAndSwitch, hkIdentifierNext, "OnMoveAndShiftRightPress", "[KeyboardShortcutsModifiers] MoveWindowAndSwitchToDesktop, [KeyboardShortcutsIdentifiers] NextDesktop")
setUpHotkeyWithOneSetOfModifiersAndIdentifier(hkModifiersMoveAndSwitch, hkIdentifierLastActive, "OnMoveAndShiftLastActivePress", "[KeyboardShortcutsModifiers] MoveWindowAndSwitchToDesktop, [KeyboardShortcutsIdentifiers] LastActiveDesktop")

setUpHotkeyWithCombo(hkComboPinWin, "OnPinWindowPress", "[KeyboardShortcutsCombinations] PinWindow")
setUpHotkeyWithCombo(hkComboUnpinWin, "OnUnpinWindowPress", "[KeyboardShortcutsCombinations] UnpinWindow")
Expand Down Expand Up @@ -232,6 +239,19 @@ OnShiftRightPress() {
SwitchToDesktop(_GetNextDesktopNumber())
}

OnShiftLastActivePress() {
; Shift to the last active desktop.

; Prepare to switch desktop by saving the target desktop.
targetDesktopNumber := lastActiveDesktopNumber
; Save the current desktop as the last active desktop.
; This lets the user rapidly switch back and forth between the two last active desktops
; without waiting timeForDesktopToBeActive milliseconds.
lastActiveDesktopNumber := _getCurrentDesktopNumber()
lastActiveDesktopChangeTime := A_TickCount
SwitchToDesktop(targetDesktopNumber)
}

OnMoveLeftPress() {
MoveToDesktop(_GetPreviousDesktopNumber())
}
Expand All @@ -240,6 +260,12 @@ OnMoveRightPress() {
MoveToDesktop(_GetNextDesktopNumber())
}

OnMoveLastActivePress() {
; Move the window to the last active desktop.

MoveToDesktop(lastActiveDesktopNumber)
}

OnMoveAndShiftLeftPress() {
MoveAndSwitchToDesktop(_GetPreviousDesktopNumber())
}
Expand All @@ -248,6 +274,19 @@ OnMoveAndShiftRightPress() {
MoveAndSwitchToDesktop(_GetNextDesktopNumber())
}

OnMoveAndShiftLastActivePress() {
; Move the current window to the last active desktop and shift to it.

; Prepare to switch desktop by saving the target desktop.
targetDesktopNumber := lastActiveDesktopNumber
; Save the current desktop as the last active desktop.
; This lets the user rapidly switch back and forth between the two last active desktops
; without waiting timeForDesktopToBeActive milliseconds.
lastActiveDesktopNumber := _getCurrentDesktopNumber()
lastActiveDesktopChangeTime := A_TickCount
MoveAndSwitchToDesktop(targetDesktopNumber)
}

OnTaskbarScrollUp() {
if (_IsCursorHoveringTaskbar()) {
OnShiftLeftPress()
Expand Down Expand Up @@ -433,7 +472,15 @@ _MoveCurrentWindowToDesktop(n:=1) {
DllCall(MoveWindowToDesktopNumberProc, UInt, activeHwnd, UInt, n-1)
}

_ChangeDesktop(n:=1) {
_ChangeDesktop(n:=1) {
; If this desktop was active for more than 1000ms save it as the last active desktop.
timeSinceLastDesktopChange := A_TickCount - lastDesktopChangeTime
lastDesktopChangeTime := A_TickCount
if (timeSinceLastDesktopChange > timeForDesktopToBeActive) {
lastActiveDesktopNumber := _getCurrentDesktopNumber()
}

; Change desktop.
if (n == 0) {
n := 10
}
Expand Down