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

add a function to traverse in nodes (#3)

This commit is contained in:
smoka7 2022-02-11 22:41:31 +03:30 committed by GitHub
parent 7764779055
commit 470ebafea4
Signed by: GitHub
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 23 additions and 1 deletions

@ -148,3 +148,17 @@ func processFocus(ctx context.Context, client sway.Client, node *sway.Node) {
log.Println(err) log.Println(err)
} }
} }
func TestFocused(t *testing.T) {
ctx := context.Background()
client, err := sway.New(ctx)
if err != nil {
t.Error(err)
}
tree, err := client.GetTree(ctx)
if err != nil {
t.Error(err)
}
f := tree.FocusedNode()
printJSON(f)
}

@ -124,6 +124,14 @@ type Node struct {
// FocusedNode traverses the node tree and returns the focused node // FocusedNode traverses the node tree and returns the focused node
func (n *Node) FocusedNode() *Node { func (n *Node) FocusedNode() *Node {
focusedNode := n.TraverseNodes(func(n *Node) bool {
return n.Focused
})
return focusedNode
}
// TraverseNodes returns the first Node matching the predicate
func (n *Node) TraverseNodes(predicate func(*Node) bool) *Node {
queue := []*Node{n} queue := []*Node{n}
for len(queue) > 0 { for len(queue) > 0 {
n = queue[0] n = queue[0]
@ -133,7 +141,7 @@ func (n *Node) FocusedNode() *Node {
continue continue
} }
if n.Focused { if predicate(n) {
return n return n
} }