i3ipc-rs
i3ipc-rs copied to clipboard
How to find the currently focused workspace in the `Node` tree?
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
istrue
, - 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?
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
}
}
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 Node
s that are workspaces in there have focused
set to false
. So your proposed solution wouldn't work, if I understand correctly.
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.
Oh, I see. Thanks for explaining. That would indeed work fairly well.
What's the status? the python bindings have this https://i3ipc-python.readthedocs.io/en/latest/con.html#i3ipc.Con under workspace
.