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()
fh := focusHandler(client)
n, err := client.GetTree(ctx)
if err != nil {
return err
}
fh(ctx, n.FocusedNode())
h := sway.EventHandler{
Window: func(ctx context.Context, e sway.WindowEvent) {
if e.Change != "focus" {
return
}
fh(ctx, e.Container.FocusedNode())
},
}
processFocus(ctx, client, n.FocusedNode())
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)
}
func focusHandler(client sway.Client) func(context.Context, *sway.Node) {
return func(ctx context.Context, node *sway.Node) {
if node == nil {
return
}
type handler struct {
sway.NoOpEventHandler
client sway.Client
}
opt := "none"
if node.AppID == nil || *node.AppID != "kitty" {
opt = "altwin:ctrl_win"
}
func (h handler) Window(ctx context.Context, e sway.WindowEvent) {
if e.Change != "focus" {
return
}
if _, err := client.RunCommand(ctx, `input '*' xkb_options `+opt); err != nil {
log.Println(err)
}
processFocus(ctx, h.client, e.Container.FocusedNode())
}
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()
n, err := client.GetTree(ctx)
if err != nil {
t.Fatal(err)
}
workspaces, err := client.GetWorkspaces(ctx)
if err != nil {
t.Fatal(err)
@ -109,37 +104,42 @@ func TestSocket(t *testing.T) {
printJSON(seats)
fh := focusHandler(client)
fh(ctx, n.FocusedNode())
h := sway.EventHandler{
Window: func(ctx context.Context, e sway.WindowEvent) {
if e.Change != "focus" {
return
}
fh(ctx, e.Container.FocusedNode())
},
n, err := client.GetTree(ctx)
if err != nil {
t.Fatal(err)
}
err = sway.Subscribe(ctx, h, sway.EventTypeWindow, sway.EventTypeShutdown)
if err != context.DeadlineExceeded && err != nil {
processFocus(ctx, client, n.FocusedNode())
if err = sway.Subscribe(ctx, testHandler{client: client}, sway.EventTypeWindow); err != context.DeadlineExceeded && err != nil {
t.Fatal(err)
}
}
func focusHandler(client sway.Client) func(context.Context, *sway.Node) {
return func(ctx context.Context, node *sway.Node) {
if node == nil {
return
}
type testHandler struct {
sway.NoOpEventHandler
client sway.Client
}
opt := "none"
if node.AppID == nil || *node.AppID != "kitty" {
opt = "altwin:ctrl_win"
}
func (h testHandler) Window(ctx context.Context, e sway.WindowEvent) {
if e.Change != "focus" {
return
}
if _, err := client.RunCommand(ctx, `input '*' xkb_options `+opt); err != nil {
log.Println(err)
}
processFocus(ctx, h.client, e.Container.FocusedNode())
}
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"
)
type EventHandler struct {
Workspace func(context.Context, WorkspaceEvent)
Mode func(context.Context, ModeEvent)
Window func(context.Context, WindowEvent)
BarConfigUpdate func(context.Context, BarConfigUpdateEvent)
Binding func(context.Context, BindingEvent)
Shutdown func(context.Context, ShutdownEvent)
Tick func(context.Context, TickEvent)
BarStatusUpdate func(context.Context, BarStatusUpdateEvent)
type EventHandler interface {
Workspace(context.Context, WorkspaceEvent)
Mode(context.Context, ModeEvent)
Window(context.Context, WindowEvent)
BarConfigUpdate(context.Context, BarConfigUpdateEvent)
Binding(context.Context, BindingEvent)
Shutdown(context.Context, ShutdownEvent)
Tick(context.Context, TickEvent)
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 {
n, err := New(ctx)
if err != nil {
@ -42,87 +53,55 @@ func Subscribe(ctx context.Context, handler EventHandler, events ...EventType) e
}
for {
reply, err := c.recvMsg(ctx)
msg, err := c.recvMsg(ctx)
if err != nil {
return err
}
handler.process(ctx, reply)
processEvent(ctx, handler, msg)
}
}
func (h EventHandler) process(ctx context.Context, reply *message) {
switch reply.Type {
func processEvent(ctx context.Context, h EventHandler, msg *message) {
switch msg.Type {
case eventTypeWorkspace:
if h.Workspace == nil {
return
}
var e WorkspaceEvent
if err := reply.Decode(&e); err == nil {
if err := msg.Decode(&e); err == nil {
h.Workspace(ctx, e)
}
case eventTypeMode:
if h.Mode == nil {
return
}
var e ModeEvent
if err := reply.Decode(&e); err == nil {
if err := msg.Decode(&e); err == nil {
h.Mode(ctx, e)
}
case eventTypeWindow:
if h.Window == nil {
return
}
var e WindowEvent
if err := reply.Decode(&e); err == nil {
if err := msg.Decode(&e); err == nil {
h.Window(ctx, e)
}
case eventTypeBarConfigUpdate:
if h.BarConfigUpdate == nil {
return
}
var e BarConfigUpdateEvent
if err := reply.Decode(&e); err == nil {
if err := msg.Decode(&e); err == nil {
h.BarConfigUpdate(ctx, e)
}
case eventTypeBinding:
if h.Binding == nil {
return
}
var e BindingEvent
if err := reply.Decode(&e); err == nil {
if err := msg.Decode(&e); err == nil {
h.Binding(ctx, e)
}
case eventTypeShutdown:
if h.Shutdown == nil {
return
}
var e ShutdownEvent
if err := reply.Decode(&e); err == nil {
if err := msg.Decode(&e); err == nil {
h.Shutdown(ctx, e)
}
case eventTypeTick:
if h.Tick == nil {
return
}
var e TickEvent
if err := reply.Decode(&e); err == nil {
if err := msg.Decode(&e); err == nil {
h.Tick(ctx, e)
}
case eventTypeBarStatusUpdate:
if h.BarStatusUpdate == nil {
return
}
var e BarStatusUpdateEvent
if err := reply.Decode(&e); err == nil {
if err := msg.Decode(&e); err == nil {
h.BarStatusUpdate(ctx, e)
}
}