1
0
mirror of https://github.com/joshuarubin/go-sway synced 2024-11-22 21:11:59 +01:00

use interface instead of struct for Subscribe

Signed-off-by: Joshua Rubin <me@jawa.dev>
This commit is contained in:
Joshua Rubin 2019-04-14 00:35:40 -06:00
parent 441d8a7846
commit 5f1901e5f7
No known key found for this signature in database
GPG Key ID: 673103A1CC175722
3 changed files with 86 additions and 107 deletions

@ -39,44 +39,44 @@ func run() error {
} }
defer client.Close() defer client.Close()
fh := focusHandler(client)
n, err := client.GetTree(ctx) n, err := client.GetTree(ctx)
if err != nil { if err != nil {
return err return err
} }
fh(ctx, n.FocusedNode()) processFocus(ctx, client, n.FocusedNode())
h := sway.EventHandler{
Window: func(ctx context.Context, e sway.WindowEvent) {
if e.Change != "focus" {
return
}
fh(ctx, e.Container.FocusedNode())
},
}
lifecycle.GoErr(ctx, func() error { lifecycle.GoErr(ctx, func() error {
return sway.Subscribe(ctx, h, sway.EventTypeWindow) return sway.Subscribe(ctx, handler{client: client}, sway.EventTypeWindow)
}) })
return lifecycle.Wait(ctx) return lifecycle.Wait(ctx)
} }
func focusHandler(client sway.Client) func(context.Context, *sway.Node) { type handler struct {
return func(ctx context.Context, node *sway.Node) { sway.NoOpEventHandler
if node == nil { client sway.Client
return }
}
opt := "none" func (h handler) Window(ctx context.Context, e sway.WindowEvent) {
if node.AppID == nil || *node.AppID != "kitty" { if e.Change != "focus" {
opt = "altwin:ctrl_win" return
} }
if _, err := client.RunCommand(ctx, `input '*' xkb_options `+opt); err != nil { processFocus(ctx, h.client, e.Container.FocusedNode())
log.Println(err) }
}
func processFocus(ctx context.Context, client sway.Client, node *sway.Node) {
if node == nil {
return
}
opt := "none"
if node.AppID == nil || *node.AppID != "kitty" {
opt = "altwin:ctrl_win"
}
if _, err := client.RunCommand(ctx, `input '*' xkb_options `+opt); err != nil {
log.Println(err)
} }
} }

@ -26,11 +26,6 @@ func TestSocket(t *testing.T) {
} }
defer client.Close() defer client.Close()
n, err := client.GetTree(ctx)
if err != nil {
t.Fatal(err)
}
workspaces, err := client.GetWorkspaces(ctx) workspaces, err := client.GetWorkspaces(ctx)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
@ -109,37 +104,42 @@ func TestSocket(t *testing.T) {
printJSON(seats) printJSON(seats)
fh := focusHandler(client) n, err := client.GetTree(ctx)
fh(ctx, n.FocusedNode()) if err != nil {
t.Fatal(err)
h := sway.EventHandler{
Window: func(ctx context.Context, e sway.WindowEvent) {
if e.Change != "focus" {
return
}
fh(ctx, e.Container.FocusedNode())
},
} }
err = sway.Subscribe(ctx, h, sway.EventTypeWindow, sway.EventTypeShutdown) processFocus(ctx, client, n.FocusedNode())
if err != context.DeadlineExceeded && err != nil {
if err = sway.Subscribe(ctx, testHandler{client: client}, sway.EventTypeWindow); err != context.DeadlineExceeded && err != nil {
t.Fatal(err) t.Fatal(err)
} }
} }
func focusHandler(client sway.Client) func(context.Context, *sway.Node) { type testHandler struct {
return func(ctx context.Context, node *sway.Node) { sway.NoOpEventHandler
if node == nil { client sway.Client
return }
}
opt := "none" func (h testHandler) Window(ctx context.Context, e sway.WindowEvent) {
if node.AppID == nil || *node.AppID != "kitty" { if e.Change != "focus" {
opt = "altwin:ctrl_win" return
} }
if _, err := client.RunCommand(ctx, `input '*' xkb_options `+opt); err != nil { processFocus(ctx, h.client, e.Container.FocusedNode())
log.Println(err) }
}
func processFocus(ctx context.Context, client sway.Client, node *sway.Node) {
if node == nil {
return
}
opt := "none"
if node.AppID == nil || *node.AppID != "kitty" {
opt = "altwin:ctrl_win"
}
if _, err := client.RunCommand(ctx, `input '*' xkb_options `+opt); err != nil {
log.Println(err)
} }
} }

@ -17,17 +17,28 @@ const (
EventTypeBarStatusUpdate EventType = "bar_status_update" EventTypeBarStatusUpdate EventType = "bar_status_update"
) )
type EventHandler struct { type EventHandler interface {
Workspace func(context.Context, WorkspaceEvent) Workspace(context.Context, WorkspaceEvent)
Mode func(context.Context, ModeEvent) Mode(context.Context, ModeEvent)
Window func(context.Context, WindowEvent) Window(context.Context, WindowEvent)
BarConfigUpdate func(context.Context, BarConfigUpdateEvent) BarConfigUpdate(context.Context, BarConfigUpdateEvent)
Binding func(context.Context, BindingEvent) Binding(context.Context, BindingEvent)
Shutdown func(context.Context, ShutdownEvent) Shutdown(context.Context, ShutdownEvent)
Tick func(context.Context, TickEvent) Tick(context.Context, TickEvent)
BarStatusUpdate func(context.Context, BarStatusUpdateEvent) BarStatusUpdate(context.Context, BarStatusUpdateEvent)
} }
type NoOpEventHandler struct{}
func (h NoOpEventHandler) Workspace(context.Context, WorkspaceEvent) {}
func (h NoOpEventHandler) Mode(context.Context, ModeEvent) {}
func (h NoOpEventHandler) Window(context.Context, WindowEvent) {}
func (h NoOpEventHandler) BarConfigUpdate(context.Context, BarConfigUpdateEvent) {}
func (h NoOpEventHandler) Binding(context.Context, BindingEvent) {}
func (h NoOpEventHandler) Shutdown(context.Context, ShutdownEvent) {}
func (h NoOpEventHandler) Tick(context.Context, TickEvent) {}
func (h NoOpEventHandler) BarStatusUpdate(context.Context, BarStatusUpdateEvent) {}
func Subscribe(ctx context.Context, handler EventHandler, events ...EventType) error { func Subscribe(ctx context.Context, handler EventHandler, events ...EventType) error {
n, err := New(ctx) n, err := New(ctx)
if err != nil { if err != nil {
@ -42,87 +53,55 @@ func Subscribe(ctx context.Context, handler EventHandler, events ...EventType) e
} }
for { for {
reply, err := c.recvMsg(ctx) msg, err := c.recvMsg(ctx)
if err != nil { if err != nil {
return err return err
} }
handler.process(ctx, reply) processEvent(ctx, handler, msg)
} }
} }
func (h EventHandler) process(ctx context.Context, reply *message) { func processEvent(ctx context.Context, h EventHandler, msg *message) {
switch reply.Type { switch msg.Type {
case eventTypeWorkspace: case eventTypeWorkspace:
if h.Workspace == nil {
return
}
var e WorkspaceEvent var e WorkspaceEvent
if err := reply.Decode(&e); err == nil { if err := msg.Decode(&e); err == nil {
h.Workspace(ctx, e) h.Workspace(ctx, e)
} }
case eventTypeMode: case eventTypeMode:
if h.Mode == nil {
return
}
var e ModeEvent var e ModeEvent
if err := reply.Decode(&e); err == nil { if err := msg.Decode(&e); err == nil {
h.Mode(ctx, e) h.Mode(ctx, e)
} }
case eventTypeWindow: case eventTypeWindow:
if h.Window == nil {
return
}
var e WindowEvent var e WindowEvent
if err := reply.Decode(&e); err == nil { if err := msg.Decode(&e); err == nil {
h.Window(ctx, e) h.Window(ctx, e)
} }
case eventTypeBarConfigUpdate: case eventTypeBarConfigUpdate:
if h.BarConfigUpdate == nil {
return
}
var e BarConfigUpdateEvent var e BarConfigUpdateEvent
if err := reply.Decode(&e); err == nil { if err := msg.Decode(&e); err == nil {
h.BarConfigUpdate(ctx, e) h.BarConfigUpdate(ctx, e)
} }
case eventTypeBinding: case eventTypeBinding:
if h.Binding == nil {
return
}
var e BindingEvent var e BindingEvent
if err := reply.Decode(&e); err == nil { if err := msg.Decode(&e); err == nil {
h.Binding(ctx, e) h.Binding(ctx, e)
} }
case eventTypeShutdown: case eventTypeShutdown:
if h.Shutdown == nil {
return
}
var e ShutdownEvent var e ShutdownEvent
if err := reply.Decode(&e); err == nil { if err := msg.Decode(&e); err == nil {
h.Shutdown(ctx, e) h.Shutdown(ctx, e)
} }
case eventTypeTick: case eventTypeTick:
if h.Tick == nil {
return
}
var e TickEvent var e TickEvent
if err := reply.Decode(&e); err == nil { if err := msg.Decode(&e); err == nil {
h.Tick(ctx, e) h.Tick(ctx, e)
} }
case eventTypeBarStatusUpdate: case eventTypeBarStatusUpdate:
if h.BarStatusUpdate == nil {
return
}
var e BarStatusUpdateEvent var e BarStatusUpdateEvent
if err := reply.Decode(&e); err == nil { if err := msg.Decode(&e); err == nil {
h.BarStatusUpdate(ctx, e) h.BarStatusUpdate(ctx, e)
} }
} }