Skip to content

Commit

Permalink
don't show switcher if we already dismissed it
Browse files Browse the repository at this point in the history
  • Loading branch information
andydotxyz committed Oct 15, 2023
1 parent b6aa829 commit 2d4de82
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 24 deletions.
20 changes: 12 additions & 8 deletions internal/ui/switcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,12 @@ func (s *Switcher) HideCancel() {
}()
}

func showAppSwitcherAt(off int, wins []fynedesk.Window, prov fynedesk.ApplicationProvider) *Switcher {
// Show the app switcher, it would then be hidden with HideApply or HideCancel.
func (s *Switcher) Show() {
s.win.Show()
}

func newAppSwitcherAt(off int, wins []fynedesk.Window, prov fynedesk.ApplicationProvider) *Switcher {
s := &Switcher{provider: prov}
s.icons = s.loadIcons(wins)
if len(s.icons) <= 1 { // don't actually show if only 1 is visible
Expand All @@ -235,20 +240,19 @@ func showAppSwitcherAt(off int, wins []fynedesk.Window, prov fynedesk.Applicatio
off = len(s.icons) + off // plus a negative is minus
}
s.win.Canvas().Focus(s.icons[off].(*switchIcon))
s.win.Show()
return s
}

// ShowAppSwitcher shows the application Switcher to change windows.
// NewAppSwitcher creates the application Switcher to change windows.
// The most recently used not-top window will be selected by default.
// If the Switcher was already visible then it will select the next window in order.
func ShowAppSwitcher(wins []fynedesk.Window, prov fynedesk.ApplicationProvider) *Switcher {
return showAppSwitcherAt(1, wins, prov)
func NewAppSwitcher(wins []fynedesk.Window, prov fynedesk.ApplicationProvider) *Switcher {
return newAppSwitcherAt(1, wins, prov)
}

// ShowAppSwitcherReverse shows the application Switcher to change windows.
// NewAppSwitcherReverse creates the application Switcher to change windows.
// The least recently used window will be selected by default.
// If the Switcher was already visible then it will select the last window in order.
func ShowAppSwitcherReverse(wins []fynedesk.Window, prov fynedesk.ApplicationProvider) *Switcher {
return showAppSwitcherAt(-1, wins, prov)
func NewAppSwitcherReverse(wins []fynedesk.Window, prov fynedesk.ApplicationProvider) *Switcher {
return newAppSwitcherAt(-1, wins, prov)
}
16 changes: 10 additions & 6 deletions internal/ui/switcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,23 +22,25 @@ func testWindows() []fynedesk.Window {

func TestShowAppSwitcher(t *testing.T) {
wins := testWindows()
s := ShowAppSwitcher(wins, test.NewAppProvider())
s := NewAppSwitcher(wins, test.NewAppProvider())
s.Show()

assert.NotNil(t, s.win)
assert.Equal(t, 1, s.currentIndex())
}

func TestShowAppSwitcherReverse(t *testing.T) {
wins := testWindows()
s := ShowAppSwitcherReverse(wins, test.NewAppProvider())
s := NewAppSwitcherReverse(wins, test.NewAppProvider())
s.Show()

assert.NotNil(t, s.win)
assert.Equal(t, len(wins)-1, s.currentIndex())
}

func TestSwitcher_Next(t *testing.T) {
wins := testWindows()
s := ShowAppSwitcher(wins, test.NewAppProvider())
s := NewAppSwitcher(wins, test.NewAppProvider())

current := s.currentIndex()
s.Next()
Expand All @@ -51,7 +53,7 @@ func TestSwitcher_Next(t *testing.T) {

func TestSwitcher_Previous(t *testing.T) {
wins := testWindows()
s := ShowAppSwitcher(wins, test.NewAppProvider())
s := NewAppSwitcher(wins, test.NewAppProvider())

current := s.currentIndex()
s.Previous()
Expand All @@ -64,15 +66,17 @@ func TestSwitcher_Previous(t *testing.T) {

func TestSwitcher_HideApply(t *testing.T) {
wins := testWindows()
s := ShowAppSwitcher(wins, test.NewAppProvider())
s := NewAppSwitcher(wins, test.NewAppProvider())
s.Show()

s.HideApply()
assert.True(t, wins[s.currentIndex()].(*test.Window).TopWindow())
}

func TestSwitcher_HideCancel(t *testing.T) {
wins := testWindows()
s := ShowAppSwitcher(wins, test.NewAppProvider())
s := NewAppSwitcher(wins, test.NewAppProvider())
s.Show()

s.HideCancel()
assert.False(t, wins[s.currentIndex()].(*test.Window).TopWindow())
Expand Down
50 changes: 40 additions & 10 deletions internal/x11/wm/switcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,34 +4,51 @@
package wm

import (
"time"

"github.com/BurntSushi/xgb/xproto"

"fyshos.com/fynedesk"
"fyshos.com/fynedesk/internal/ui"
)

// switcherInstance and the methods below manage the X to ui.Switcher bindings.
// This is needed due to the way that releasing Super is only reported if we grab the whole keyboard.
// Therefore the UI cannot handle keyboard input - so we add it here instead.
var switcherInstance *ui.Switcher
var (
// switcherInstance and the methods below manage the X to ui.Switcher bindings.
// This is needed due to the way that releasing Super is only reported if we grab the whole keyboard.
// Therefore the UI cannot handle keyboard input - so we add it here instead.
switcherInstance *ui.Switcher

// ignoreSwitcher helps us track where key pres+lift is faster than window show
ignoreSwitcher bool
)

func (x *x11WM) applyAppSwitcher() {
if switcherInstance == nil {
return
ignoreSwitcher = true
go func() {
time.Sleep(time.Second / 4)
ignoreSwitcher = false
}()
} else {
go switcherInstance.HideApply()
}

go switcherInstance.HideApply()
xproto.UngrabKeyboard(x.x.Conn(), xproto.TimeCurrentTime)
windowClientListStackingUpdate(x)
switcherInstance = nil
}

func (x *x11WM) cancelAppSwitcher() {
if switcherInstance == nil {
return
ignoreSwitcher = true
go func() {
time.Sleep(time.Second / 4)
ignoreSwitcher = false
}()
} else {
go switcherInstance.HideCancel()
}

go switcherInstance.HideCancel()
xproto.UngrabKeyboard(x.x.Conn(), xproto.TimeCurrentTime)
switcherInstance = nil
}
Expand Down Expand Up @@ -70,9 +87,22 @@ func (x *x11WM) showOrSelectAppSwitcher(reverse bool) {

go func() {
if reverse {
switcherInstance = ui.ShowAppSwitcherReverse(x.Windows(), fynedesk.Instance().IconProvider())
win := ui.NewAppSwitcherReverse(x.Windows(), fynedesk.Instance().IconProvider())

if ignoreSwitcher {
ignoreSwitcher = false
} else {
switcherInstance = win
win.Show()
}
} else {
switcherInstance = ui.ShowAppSwitcher(x.Windows(), fynedesk.Instance().IconProvider())
win := ui.NewAppSwitcher(x.Windows(), fynedesk.Instance().IconProvider())
if ignoreSwitcher {
ignoreSwitcher = false
} else {
switcherInstance = win
win.Show()
}
}
}()
}

0 comments on commit 2d4de82

Please sign in to comment.