i3ipc-rs icon indicating copy to clipboard operation
i3ipc-rs copied to clipboard

How to find the currently focused workspace in the `Node` tree?

Open soenkehahn opened this issue 6 years ago • 5 comments

I'd like to be able to get the Node from the Node tree that represents the currently focused workspace.

I think in the current state a possible solution is to

  • call I3Connection::get_workspaces(),
  • traverse the workspaces and find the one where focused is true,
  • remember the name of that workspace,
  • call I3Connection::get_tree(),
  • traverse the tree to find the node that is a workspace and has the same name.

This seems very cumbersome. Also, I'm not 100% sure workspace names in i3 are guaranteed to always be unique. Are there any better solutions?

Also, would it be in the scope of this library to add a helper function for this?

soenkehahn avatar Feb 08 '19 15:02 soenkehahn

I think the solution would be similar to the example in https://github.com/tmerr/i3ipc-rs/issues/29, but you would stop the recursion short once Node.nodetype == NodeType::Workspace. I agree this is harder than it needs to be and a helper of some sort would make sense. How about this: we provide an node.iter_focused() function, so you could do:

for n in node.iter_focused() {
  if n.nodetype == NodeType::Workspace {
    ... do something
  }
}

tmerr avatar Feb 08 '19 16:02 tmerr

While the current workspace has focused set to true in the result of get_workspaces(), that's not true in the Node tree (the result from get_tree()). All the Nodes that are workspaces in there have focused set to false. So your proposed solution wouldn't work, if I understand correctly.

soenkehahn avatar Feb 08 '19 16:02 soenkehahn

Only one node in the tree has focused set to true. You can walk from the root of the tree toward it using the method is showed. The steps look something like root -> display -> workspace -> ... -> the focused container. Finding the workspace is a matter of filtering that list for the node with workspace type.

tmerr avatar Feb 08 '19 16:02 tmerr

Oh, I see. Thanks for explaining. That would indeed work fairly well.

soenkehahn avatar Feb 08 '19 16:02 soenkehahn

What's the status? the python bindings have this https://i3ipc-python.readthedocs.io/en/latest/con.html#i3ipc.Con under workspace.

hasufell avatar Feb 09 '20 20:02 hasufell