~robert-ancell/+junk/mocker

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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
public class Mocker : Gtk.Application
{
    private Gtk.ApplicationWindow window;
    private Mockup mockup;
    private MockupView view;
    private Gtk.ScrolledWindow controls_box;

    public Mocker ()
    {
        Object (application_id: "com.ubuntu.mocker", flags: ApplicationFlags.FLAGS_NONE);
    }

    public override void startup ()
    {
        base.startup ();

        window = new Gtk.ApplicationWindow (this);

        var vbox = new Gtk.Box (Gtk.Orientation.VERTICAL, 0);
        vbox.visible = true;
        window.add (vbox);

        var toolbar = new Gtk.Toolbar ();
        toolbar.visible = true;
        toolbar.get_style_context ().add_class (Gtk.STYLE_CLASS_PRIMARY_TOOLBAR);
        vbox.pack_start (toolbar, false, true, 0);

        var button = new Gtk.ToolButton.from_stock (Gtk.Stock.NEW);
        button.visible = true;
        toolbar.add (button);

        button = new Gtk.ToolButton.from_stock (Gtk.Stock.SAVE);
        button.visible = true;
        toolbar.add (button);

        mockup = new Mockup ();
        mockup.selected_item_changed.connect (selected_item_changed_cb);

        var hbox = new Gtk.Box (Gtk.Orientation.HORIZONTAL, 0);
        hbox.visible = true;
        vbox.pack_start (hbox, true, true, 0);

        var sidebar_box = new Gtk.Box (Gtk.Orientation.VERTICAL, 5);
        sidebar_box.set_size_request (200, -1);
        sidebar_box.visible = true;
        hbox.pack_start (sidebar_box, false, true, 0);

        var item_box = new Gtk.Box (Gtk.Orientation.VERTICAL, 5);
        item_box.visible = true;
        sidebar_box.pack_start (item_box, false, true, 0);

        var item_button = new Gtk.Button.with_label ("Window");
        item_button.visible = true;
        item_button.clicked.connect (() => { mockup.add_item (new MockWindow ()); });
        item_box.pack_start (item_button, false, true, 0);

        item_button = new Gtk.Button.with_label ("Label");
        item_button.visible = true;
        item_button.clicked.connect (() => { mockup.add_item (new MockLabel ()); });
        item_box.pack_start (item_button, false, true, 0);

        item_button = new Gtk.Button.with_label ("Button");
        item_button.visible = true;
        item_button.clicked.connect (() => { mockup.add_item (new MockButton ()); });
        item_box.pack_start (item_button, false, true, 0);

        item_button = new Gtk.Button.with_label ("Check");
        item_button.visible = true;
        item_button.clicked.connect (() => { mockup.add_item (new MockCheck ()); });
        item_box.pack_start (item_button, false, true, 0);

        item_button = new Gtk.Button.with_label ("Radio");
        item_button.visible = true;
        item_button.clicked.connect (() => { mockup.add_item (new MockRadio ()); });
        item_box.pack_start (item_button, false, true, 0);

        item_button = new Gtk.Button.with_label ("Switch");
        item_button.visible = true;
        item_button.clicked.connect (() => { mockup.add_item (new MockSwitch ()); });
        item_box.pack_start (item_button, false, true, 0);

        item_button = new Gtk.Button.with_label ("Icon");
        item_button.visible = true;
        item_button.clicked.connect (() => { mockup.add_item (new MockIcon ()); });
        item_box.pack_start (item_button, false, true, 0);

        item_button = new Gtk.Button.with_label ("Note");
        item_button.visible = true;
        item_button.clicked.connect (() => { mockup.add_item (new MockNote ()); });
        item_box.pack_start (item_button, false, true, 0);

        controls_box = new Gtk.ScrolledWindow (null, null);
        sidebar_box.pack_start (controls_box, true, true, 0);

        view = new MockupView (mockup);
        view.visible = true;
        hbox.pack_start (view, true, true, 0);
    }

    public override void activate ()
    {
        window.visible = true;
    }

    private void selected_item_changed_cb ()
    {
        /* Remove old grid */
        if (controls_box.get_child () != null)
            controls_box.remove (controls_box.get_child ());

        var item = mockup.selected_item;
        if (item == null)
        {
            controls_box.visible = false;
            return;
        }

        controls_box.visible = true;

        var grid = new Gtk.Grid ();
        grid.visible = true;
        grid.row_spacing = 5;
        grid.column_spacing = 5;
        controls_box.add_with_viewport (grid);

        var row = 0;
        foreach (var prop in item.properties)
        {
            if (prop is MockItemStringProperty)
            {
                var p = prop as MockItemStringProperty;

                var label = new Gtk.Label ("%s:".printf (prop.description));
                label.set_alignment (0.0f, 0.5f);
                label.visible = true;
                grid.attach (label, 0, row, 2, 1);
                row++;

                var entry = new Gtk.Entry ();
                entry.text = p.value;
                entry.visible = true;
                entry.changed.connect (() => { p.value = entry.text; });
                grid.attach (entry, 0, row, 2, 1);
                row++;
            }
            else if (prop is MockItemBooleanProperty)
            {
                var p = prop as MockItemBooleanProperty;

                var label = new Gtk.Label (prop.description);
                label.set_alignment (0.0f, 0.5f);
                label.visible = true;
                grid.attach (label, 0, row, 1, 1);

                var s = new Gtk.Switch ();
                s.active = p.value;
                s.visible = true;
                s.notify["active"].connect (() => { p.value = s.active; });
                grid.attach (s, 1, row, 1, 1);
                row++;
            }
            else
            {
                warning ("Unknown property type");
            }
        }
    }
}

private void cairo_rounded_rectangle (Cairo.Context c, double x, double y, double width, double height, double radius)
{
    var w = width - radius * 2;
    var h = height - radius * 2;
    var kappa = 0.5522847498 * radius;
    c.move_to (x + radius, y);
    c.rel_line_to (w, 0);
    c.rel_curve_to (kappa, 0, radius, radius - kappa, radius, radius);
    c.rel_line_to (0, h);
    c.rel_curve_to (0, kappa, kappa - radius, radius, -radius, radius);
    c.rel_line_to (-w, 0);
    c.rel_curve_to (-kappa, 0, -radius, kappa - radius, -radius, -radius);
    c.rel_line_to (0, -h);
    c.rel_curve_to (0, -kappa, radius - kappa, -radius, radius, -radius);
}

public static int main (string[] args)
{
    Gtk.init (ref args);

    var mocker = new Mocker ();
    return mocker.run ();
}