1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
//! Extensions of swayipc types and IPC structs.

extern crate serde;
extern crate serde_json;
extern crate swayipc;
extern crate users;

use clap::Clap;
use serde::{Deserialize, Serialize};
use swayipc::reply as r;

/// Immutable Node Iterator
///
/// Iterates nodes in depth-first order, tiled nodes before floating nodes.
pub struct NodeIter<'a> {
    stack: Vec<&'a r::Node>,
}

impl<'a> NodeIter<'a> {
    pub fn new(node: &'a r::Node) -> NodeIter {
        NodeIter { stack: vec![node] }
    }
}

impl<'a> Iterator for NodeIter<'a> {
    type Item = &'a r::Node;

    fn next(&mut self) -> Option<Self::Item> {
        if let Some(node) = self.stack.pop() {
            for n in &node.floating_nodes {
                self.stack.push(&n);
            }
            for n in &node.nodes {
                self.stack.push(&n);
            }
            Some(node)
        } else {
            None
        }
    }
}

/// Extension methods for [`swayipc::reply::Node`].
pub trait NodeMethods {
    /// Returns an iterator for this [`swayipc::reply::Node`] and its childres.
    fn iter(&self) -> NodeIter;

    /// Returns all nodes being application windows.
    fn windows(&self) -> Vec<&r::Node>;

    /// Returns all nodes being workspaces.
    fn workspaces(&self) -> Vec<&r::Node>;

    fn is_scratchpad(&self) -> bool;
}

impl NodeMethods for r::Node {
    fn iter(&self) -> NodeIter {
        NodeIter::new(self)
    }

    fn windows(&self) -> Vec<&r::Node> {
        self.iter()
            .filter(|n| {
                (n.node_type == r::NodeType::Con
                    || n.node_type == r::NodeType::FloatingCon)
                    && n.name.is_some()
            })
            .collect()
    }

    fn workspaces(&self) -> Vec<&r::Node> {
        self.iter()
            .filter(|n| n.node_type == r::NodeType::Workspace)
            .collect()
    }

    fn is_scratchpad(&self) -> bool {
        self.name.is_some() && self.name.as_ref().unwrap().eq("__i3_scratch")
    }
}

#[derive(Clap, Debug, Deserialize, Serialize)]
pub enum SwayrCommand {
    /// Switch to next urgent window (if any) or to last recently used window.
    SwitchToUrgentOrLRUWindow,
    /// Focus the selected window
    SwitchWindow,
    /// Focus the next window.
    NextWindow,
    /// Focus the previous window.
    PrevWindow,
    /// Quit the selected window
    QuitWindow,
    /// Switch to the selected workspace
    SwitchWorkspace,
    /// Switch to the selected workspace or focus the selected window
    SwitchWorkspaceOrWindow,
    /// Quit all windows of selected workspace or the selected window
    QuitWorkspaceOrWindow,
    /// Select and execute a swaymsg command
    ExecuteSwaymsgCommand,
    /// Select and execute a swayr command
    ExecuteSwayrCommand,
}

/// Extra properties gathered by swayrd for windows and workspaces.
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct ExtraProps {
    /// Milliseconds since UNIX epoch.
    pub last_focus_time: u128,
}