~embik/appcenter/fix-schema-install

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
/* Copyright 2015 Marvin Beckers <beckersmarvin@gmail.com>
*
* This program is free software: you can redistribute it
* and/or modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be
* useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
* Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program. If not, see http://www.gnu.org/licenses/.
*/

public class AppCenter.MainWindow : Gtk.Window {
    private Granite.Widgets.ModeButton view_mode;
    private Gtk.HeaderBar headerbar;
    private Gtk.Stack stack;
    private Gtk.SearchEntry search_entry;
    private Views.CategoryView category_view;
    private Views.FeaturedView featured_view;
    private Views.InstalledView installed_view;
    private Views.SearchView search_view;
    private Gtk.Button current_button;
    private ulong task_finished_connection = 0U;

    public MainWindow () {
        window_position = Gtk.WindowPosition.CENTER;
        create_headerbar ();
        create_views ();
        title = _("App Center");
        icon_name = "system-software-installer";
        set_size_request (750, 550);

        view_mode.selected = 0;
        stack.set_visible_child (featured_view);
        show_all ();
    }

    public override bool delete_event (Gdk.EventAny event) {
        unowned AppCenterCore.Client client = AppCenterCore.Client.get_default ();
        if (client.has_tasks ()) {
            if (task_finished_connection != 0U) {
                client.disconnect (task_finished_connection);
            }

            hide ();
            task_finished_connection = client.tasks_finished.connect (() => {
                if (!visible) {
                    delete_event (event);
                }
            });

            return true;
        }

        return false;
    }

    private void create_headerbar () {
        headerbar = new Gtk.HeaderBar ();
        headerbar.show_close_button = true;
        set_titlebar (headerbar);

        view_mode = new Granite.Widgets.ModeButton ();
        view_mode.append_text (_("Featured"));
        view_mode.append_text (_("Categories"));
        view_mode.append_text (C_("view", "Installed"));

        search_entry = new Gtk.SearchEntry ();
        search_entry.placeholder_text = _("Search App");

        headerbar.set_custom_title (view_mode);
        headerbar.pack_end (search_entry);

        view_mode.notify["selected"].connect (() => {
            switch (view_mode.selected) {
                case 0:
                    stack.set_visible_child (featured_view);
                    break;
                case 1:
                    stack.set_visible_child (category_view);
                    break;
                default:
                    stack.set_visible_child (installed_view);
                    break;
            }
        });
    }

    private void create_views () {
        stack = new Gtk.Stack ();
        stack.transition_type = Gtk.StackTransitionType.SLIDE_LEFT_RIGHT;
        stack.expand = true;

        featured_view = new Views.FeaturedView ();
        category_view = new Views.CategoryView ();
        installed_view = new Views.InstalledView ();
        search_view = new Views.SearchView ();
        stack.add (featured_view);
        stack.add (category_view);
        stack.add (installed_view);
        stack.add (search_view);
        add (stack);

        category_view.subview_entered.connect ((name) => {
            show_return_button (name, category_view);
        });

        installed_view.subview_entered.connect ((name) => {
            show_return_button (name, installed_view);
        });
    }

    private void show_return_button (string return_label, View view) {
        var return_button = new Gtk.Button.with_label (return_label);
        return_button.get_style_context ().add_class ("back-button");
        return_button.show_all ();
        if (current_button != null) {
            current_button.destroy ();
        }

        current_button = return_button;
        headerbar.pack_start (return_button);
        view_mode.sensitive = false;
        return_button.clicked.connect (() => {
            view_mode.sensitive = true;
            view.return_clicked ();
            return_button.destroy ();
        });
    }
}