summaryrefslogtreecommitdiff
path: root/lib/sway/node.vala
blob: 124385401555d335ccdfaf2f5dd33f2c2bf31545 (plain)
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
namespace AstalSway {
  public enum NodeType {
    ROOT,
    WORKSPACE,
    CONTAINER,
    WINDOW,
    OUTPUT
  }

  public struct Rectangle {
    public static Rectangle from_json(Json.Object obj) {
      return Rectangle() {
        x = (int)obj.get_int_member("x"),
        y = (int)obj.get_int_member("y"),
        width = (int)obj.get_int_member("width"),
        height = (int)obj.get_int_member("height"),
      };

    }

    public int x;
    public int y;
    public int width;
    public int height;
  }

  public class Node : Object {
    public int id {get; private set; }
    public bool focused { get; private set; }
    public bool urgent { get; private set; }
    public string name { get; private set; }
    public string layout { get; private set; }
    public string orientation { get; private set; }
    public float? percent { get; private set; }
    public Rectangle rect { get; private set; }
    public Rectangle window_rect { get; private set; }
    public Rectangle deco_rect { get; private set; }
    public NodeType node_type {get; protected set;}

    public weak Node parent;

    public List<weak Node> nodes;
    
    protected Json.Object data;
    
    private static HashTable<int, Node> _temp_nodes =
        new HashTable<int, Node>(i => i, (a,b) => a==b);
    internal static HashTable<int, Node> _all_nodes =
        new HashTable<int, Node>(i => i, (a,b) => a==b);

    public static Node? build(Json.Object obj) {
      if (obj == null) {
        return null;
      }
      switch (obj.get_string_member("type")) {
        case "workspace":
          var ws = new Workspace();
          return ws as Node;

        default:
          var node = new Node(); 
          return node;
          break;

      }
      return null;
    }

    internal static async void sync_tree() {
      var str = yield Sway.get_default().message_async(PayloadType.MESSAGE_GET_TREE, "");
      var obj = Json.from_string(str).get_object();
      if (obj == null) {
        return;
      }
      
      Node root = build(obj);
      _temp_nodes = new HashTable<int, Node>(i => i, (a,b) => a==b);
      root.sync(obj);
      _temp_nodes.insert(root.id, root);
      _all_nodes = _temp_nodes;

    }
    
    internal virtual void sync(Json.Object obj) {
      if (obj == null) {
        return;
      }
      data = obj;
      id = (int)obj.get_int_member("id");
      name = obj.get_string_member("name");
      orientation = obj.get_string_member("orientation");
      layout = obj.get_string_member("layout");
      focused = obj.get_boolean_member("focused");
      urgent = obj.get_boolean_member("urgent");
      rect = Rectangle.from_json(obj.get_object_member("rect"));
      window_rect = Rectangle.from_json(obj.get_object_member("window_rect"));
      deco_rect = Rectangle.from_json(obj.get_object_member("deco_rect"));

      var arr = obj.get_array_member("nodes");
      var arr2 = obj.get_array_member("floating_nodes");
      sync_nodes(arr);
      sync_nodes(arr2);

    }

    private void sync_nodes(Json.Array arr) {
      var new_nodes = new List<weak Node> ();

      foreach (var item in arr.get_elements()) {
        var obj = item.get_object();
        int id = (int)obj.get_int_member("id");
        var node = _all_nodes.get(id);
        
        if (node == null) {
          node = Node.build(obj);
        }

       new_nodes.append(node);
       _temp_nodes.insert(id, node);     
       node.sync(obj);
      }

      nodes = (owned) new_nodes;
    }
  }
}