tmerr / i3ipc-rs

A Rust library for controlling i3-wm through its IPC interface
MIT License
106 stars 33 forks source link

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

Open soenkehahn opened 5 years ago

soenkehahn commented 5 years ago

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

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?

tmerr commented 5 years ago

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
  }
}
soenkehahn commented 5 years ago

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.

tmerr commented 5 years ago

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.

soenkehahn commented 5 years ago

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

hasufell commented 4 years ago

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