summaryrefslogtreecommitdiff
path: root/libcaribou/level-model.vala
blob: 6c041d8736c9347d12738f6c6f5b80954c35f377 (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
using GLib;

namespace Caribou {
    public class LevelModel : ScannableGroup, IKeyboardObject {
        public signal void level_toggled (string new_level);
        public string mode { get; private set; default = ""; }

        private Gee.ArrayList<RowModel> rows;

        public LevelModel (string mode) {
            this.mode = mode;
            rows = new Gee.ArrayList<RowModel> ();
        }

        internal void add_key (int rownum, int colnum, KeyModel key) {
            int rowindex = rownum;
            RowModel row = null;

            if (rownum < 0)
                rowindex = rows.size + rownum;

            if (rownum >= rows.size) {
                row = new RowModel ();
                row.key_activated.connect (on_key_activated);
                rows.add(row);
            } else {
                row = rows[rowindex];
            }

            row.add_key (colnum, key);
        }

        public List<RowModel> get_rows () {
            return (List<RowModel>) get_children ();
        }

        private void on_key_activated (KeyModel key) {
            if (key.toggle != "")
                level_toggled (key.toggle);
            else if (mode == "latched")
                level_toggled ("default");
            key_activated (key);
        }

        public override IScannableItem[] get_scan_children () {
            if (scan_grouping == ScanGrouping.LINEAR)
                return (IScannableItem[]) get_keys ();
            else
                return (IScannableItem[]) rows.to_array ();
        }

        public List<IKeyboardObject> get_children () {
            return (List<IKeyboardObject>) collection_to_object_list (rows);
        }
    }
}