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

Commit

Permalink
First ideas about toggling between last desktops.
Browse files Browse the repository at this point in the history
This is a rapid jot down of the first ideas about enabling a hotkey
to toggle between the current desktop and the last one.

I am not really shure this is the most elegant way to do this, still,
it seems to work ;)
  • Loading branch information
Giorgio Bonvicini authored and Giorgio Bonvicini committed May 16, 2017
1 parent 6ee9b98 commit 39cbb15
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
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
LastDesktop=C
Desktop1=1
Desktop2=2
Desktop3=3
Expand Down
40 changes: 40 additions & 0 deletions 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 lastDesktopNumber := _GetCurrentDesktopNumber()
global lastDesktopChangeTime := A_TickCount
global timeForDesktopToBeActive := 1000

initialDesktopNo := _GetCurrentDesktopNumber()

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

setUpHotkeyWithOneSetOfModifiersAndIdentifier(hkModifiersMove, hkIdentifierPrevious, "OnMoveLeftPress", "[KeyboardShortcutsModifiers] MoveWindowToDesktop, [KeyboardShortcutsIdentifiers] PreviousDesktop")
setUpHotkeyWithOneSetOfModifiersAndIdentifier(hkModifiersMove, hkIdentifierNext, "OnMoveRightPress", "[KeyboardShortcutsModifiers] MoveWindowToDesktop, [KeyboardShortcutsIdentifiers] NextDesktop")
setUpHotkeyWithOneSetOfModifiersAndIdentifier(hkModifiersMove, hkIdentifierLast, "OnMoveLastPress", "[KeyboardShortcutsModifiers] MoveWindowToDesktop, [KeyboardShortcutsIdentifiers] LastDesktop")

setUpHotkeyWithOneSetOfModifiersAndIdentifier(hkModifiersMoveAndSwitch, hkIdentifierPrevious, "OnMoveAndShiftLeftPress", "[KeyboardShortcutsModifiers] MoveWindowAndSwitchToDesktop, [KeyboardShortcutsIdentifiers] PreviousDesktop")
setUpHotkeyWithOneSetOfModifiersAndIdentifier(hkModifiersMoveAndSwitch, hkIdentifierNext, "OnMoveAndShiftRightPress", "[KeyboardShortcutsModifiers] MoveWindowAndSwitchToDesktop, [KeyboardShortcutsIdentifiers] NextDesktop")
setUpHotkeyWithOneSetOfModifiersAndIdentifier(hkModifiersMoveAndSwitch, hkIdentifierLast, "OnMoveAndShiftLastPress", "[KeyboardShortcutsModifiers] MoveWindowAndSwitchToDesktop, [KeyboardShortcutsIdentifiers] NextDesktop")

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

OnShiftLastPress() {
global lastDesktopNumber, lastDesktopChangeTime
tempDesktopNumber := _getCurrentDesktopNumber()
SwitchToDesktop(lastDesktopNumber)
lastDesktopNumber := tempDesktopNumber
}

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

OnMoveLastPress() {
global lastDesktopNumber, lastDesktopChangeTime
tempDesktopNumber := _getCurrentDesktopNumber()
MoveToDesktop(lastDesktopNumber)
lastDesktopNumber := tempDesktopNumber
}

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

OnMoveAndShiftLastPress() {
global lastDesktopNumber, lastDesktopChangeTime
tempDesktopNumber := _getCurrentDesktopNumber()
MoveAndSwitchToDesktop(lastDesktopNumber)
lastDesktopNumber := tempDesktopNumber
}

OnTaskbarScrollUp() {
if (_IsCursorHoveringTaskbar()) {
OnShiftLeftPress()
Expand Down Expand Up @@ -434,6 +464,16 @@ _MoveCurrentWindowToDesktop(n:=1) {
}

_ChangeDesktop(n:=1) {

; If this desktop was active for more than 1000ms mark it as the last used desktop.
global lastDesktopChangeTime, lastDesktopNumber, timeForDesktopToBeActive
timeSinceLastDesktopChange := A_TickCount - lastDesktopChangeTime
lastDesktopChangeTime := A_TickCount
if (timeSinceLastDesktopChange > timeForDesktopToBeActive && n != lastDesktopNumber) {
lastDesktopNumber := _getCurrentDesktopNumber()
}

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

6 comments on commit 39cbb15

@GioBonvi
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sdias Trying to implement what was asked in #40 I came up with this.

I tried to implement it so that if a user skips rapidly (waiting less than 1 sec on each desktop) from desktop 1 to 5 (for example) he then can use the hotkey Switch/MoveWindowTo/MoveWindowToAndSwitch + C to jump between desktop 1 and 5.

Do you think this is a good way to implement it? To be honest it looks a little bit clumsy to me...

@sdias
Copy link
Owner

@sdias sdias commented on 39cbb15 May 17, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@GioBonvi I like the 1 second thing, but not in all cases, since it would mean you couldn't quickly go back and forth.
In terms of the implementation, I think there are better, more centralized ways of achieving it. I'll have a try at this next weekend.
Thanks for your work though.

@GioBonvi
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it would mean you couldn't quickly go back and forth.

I don't know if I understand correctly what you mean, but if you press Win + Ctrl + C rapidly multiple times it does indeed rapidly switch back and forth between two desktops,

The 1 second dealy is only used to skip "transitional" desktops and just consider the starting dekstop and the one the users lands on.

@sdias
Copy link
Owner

@sdias sdias commented on 39cbb15 May 17, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@GioBonvi Oh sorry, nevermind my comment then. I need to have a closer look at the code. Sorry if my last comment was dismissive, your code looks great, it just seems there's a fair bit of duplication that could be avoided.

@GioBonvi
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No offense at all! I am the first to recognize that it is far from optimal ;)
It really needs to be analyzed and refactored: I just replicated 1:1 all the calls to NextDesktop and changed them to LastDesktop.

@GioBonvi
Copy link
Collaborator

@GioBonvi GioBonvi commented on 39cbb15 Jun 13, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sdias
I had completely forgot about this and @temporaer brought it back to my attention.

As far as I can tell this works as intended, but it should be tested on more systems: I had some trouble implementing the hooks for the hotkeys and ended up copying from the NextDesktop calls, so you should probably double check this and optimize were needed as you are more familiar with the structure of the code.

This is how I implemented it: everytime the user switches to a new dekstop the time (millisecnods after program startup) is saved into a variable: when the next switch happens the time is checked again. If more than one second has passed since the last switch the current desktop is considered "the last active desktop" and is saved in a variable.
The user can jump back to the last active desktop with a hotkey.

This is better illustrated by an example:

Program start -> Desktop 1 -> lastDesktopChangeTime = 0

User waits 10 seconds

Hotkey: Switch to Desktop 2
now - lastDesktopChangeTimeSave > 1 second
lastDesktopNumber=1
go to Desktop 2

User waits 0.1 seconds

Hotkey: Switch to Desktop 3
now - lastDesktopChangeTimeSave < 1 second
nothing changes: Desktop 1 still is the last active desktop
go to Desktop 3

Hotkey: Go to last active desktop
go to Desktop 1
lastDesktopNumber=3

Please sign in to comment.