~erasmo-marin/webby-browser/stable

« back to all changes in this revision

Viewing changes to src/ApplicationsView.vala

  • Committer: Erasmo Marín
  • Date: 2015-10-12 22:25:31 UTC
  • mfrom: (13.1.1 webby-browser)
  • Revision ID: erasmo.marin@gmail.com-20151012222531-8k72q64kci4psjwg
fix bug #1505417

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
public class ApplicationsView : Gtk.Box {
2
2
 
3
3
    public signal void add_request();
 
4
    public signal void edit_request(DesktopFile desktop_file);
4
5
 
5
6
    private Gtk.FlowBox icon_view;
6
7
    private Gee.HashMap<string, GLib.DesktopAppInfo> applications;
51
52
    }
52
53
 
53
54
    public void add_button (GLib.DesktopAppInfo app) {
54
 
        var image = new ApplicationIcon(app.get_icon ().to_string (), app.get_display_name ());
55
 
        var desktop_file = new DesktopFile.from_desktopappinfo (app);
56
 
        image.delete_request.connect (()=>{
57
 
            desktop_file.delete_file();
 
55
        var image = new ApplicationIcon (app);
 
56
        image.edit_request.connect ((desktop_file) => {
 
57
            edit_request (desktop_file);
 
58
            icon_view.unselect_all ();
 
59
        });
 
60
        image.deleted.connect ((parent) => {
 
61
            this.icon_view.remove (parent);
 
62
            this.select_first_item ();
58
63
        });
59
64
        icon_view.add (image);
60
65
        icon_view.show_all ();
63
68
    public void select_last_item () {
64
69
        icon_view.select_child (icon_view.get_child_at_index ((int)icon_view.get_children ().length () - 1));
65
70
    }
 
71
 
 
72
    public void select_first_item () {
 
73
        icon_view.select_child (icon_view.get_child_at_index (0));
 
74
    }
 
75
 
 
76
    public void update_button (GLib.DesktopAppInfo app) {
 
77
        foreach (var item in icon_view.get_children ()) {
 
78
            if ((item as Gtk.FlowBoxChild).get_child () is ApplicationIcon) {
 
79
                var app_icon = (item as Gtk.FlowBoxChild).get_child () as ApplicationIcon;
 
80
 
 
81
                if (app_icon.desktop_file.name == app.get_display_name ()) {
 
82
                    app_icon.set_new_desktopfile (new DesktopFile.from_desktopappinfo (app));
 
83
                    icon_view.select_child (item as Gtk.FlowBoxChild);
 
84
                    break;
 
85
                }
 
86
            }
 
87
        }
 
88
    }
66
89
}
67
90
 
68
91
 
74
97
    Gtk.Box box;
75
98
    Gtk.ActionGroup action_group;
76
99
 
77
 
    public signal void delete_request ();
78
 
 
79
 
    public ApplicationIcon (string icon, string label_str) {
 
100
    internal DesktopFile desktop_file { get; private set; }
 
101
 
 
102
    public signal void deleted (Gtk.Container? parent);
 
103
    public signal void edit_request (DesktopFile desktop_file);
 
104
 
 
105
    public ApplicationIcon (GLib.DesktopAppInfo app) {
 
106
        this.desktop_file = new DesktopFile.from_desktopappinfo (app);
 
107
 
80
108
        hexpand = false;
81
109
        vexpand = false;
82
110
 
83
 
        label = new Gtk.Label (label_str);
84
 
 
 
111
        label = new Gtk.Label (this.desktop_file.name);
 
112
 
 
113
        set_icon (this.desktop_file.icon);
 
114
 
 
115
        this.margin = 6;
 
116
        this.margin_start = 20;
 
117
        this.margin_end = 20;
 
118
 
 
119
        var menu = new ActionMenu ();
 
120
        menu.halign = Gtk.Align.CENTER;
 
121
        menu.delete_clicked.connect (() => { remove_application (); });
 
122
        menu.edit_clicked.connect (() => { edit_request (desktop_file); });
 
123
 
 
124
        box = new Gtk.Box (Gtk.Orientation.VERTICAL, 0);
 
125
        box.pack_start (image, false, false, 0);
 
126
        box.pack_start (label, false, false, 0);
 
127
        box.pack_start (menu, false, false, 0);
 
128
 
 
129
        box.hexpand = false;
 
130
        box.vexpand = false;
 
131
 
 
132
        var event_box = new Gtk.EventBox ();
 
133
        event_box.add (box);
 
134
        event_box.events |= Gdk.EventMask.ENTER_NOTIFY_MASK|Gdk.EventMask.LEAVE_NOTIFY_MASK;
 
135
 
 
136
        event_box.enter_notify_event.connect ((event) => {
 
137
            menu.set_reveal_child (true);
 
138
            return false;
 
139
        });
 
140
 
 
141
        event_box.leave_notify_event.connect ((event) => {
 
142
            if (event.detail == Gdk.NotifyType.INFERIOR)
 
143
                return false;
 
144
            menu.set_reveal_child (false);
 
145
            return false;
 
146
        });
 
147
 
 
148
        this.add (event_box);
 
149
    }
 
150
 
 
151
    public void set_new_desktopfile (DesktopFile desktop_file) {
 
152
        this.desktop_file = desktop_file;
 
153
        set_icon (this.desktop_file.icon);
 
154
    }
 
155
 
 
156
    private void set_icon (string icon) {
85
157
        if (File.new_for_path (icon).query_exists ()) {
86
158
            var pix = new Gdk.Pixbuf.from_file (icon);
87
159
 
97
169
                new_height = new_height * pix.height / pix.width;
98
170
                margin_vertical = (new_width - new_height) / 2;
99
171
            }
100
 
            image = new Gtk.Image.from_pixbuf (pix.scale_simple (new_width, new_height, Gdk.InterpType.BILINEAR));
 
172
            if (image == null)
 
173
                image = new Gtk.Image.from_pixbuf (pix.scale_simple (new_width, new_height, Gdk.InterpType.BILINEAR));
 
174
            else
 
175
                image.set_from_pixbuf (pix.scale_simple (new_width, new_height, Gdk.InterpType.BILINEAR));
 
176
 
101
177
            image.margin_top = margin_vertical;
102
178
            image.margin_bottom = margin_vertical;
103
179
            image.margin_right = margin_horizontal;
107
183
            image.icon_name = icon;
108
184
            image.pixel_size = 64;
109
185
        }
110
 
 
111
 
        this.margin = 5;
112
 
        this.margin_start = 20;
113
 
        this.margin_end = 20;
114
 
 
115
 
        box = new Gtk.Box (Gtk.Orientation.VERTICAL, 0);
116
 
        box.pack_start (image, false, false, 0);
117
 
        box.pack_start (label, false, false, 0);
118
 
        box.hexpand = false;
119
 
        box.vexpand = false;
120
 
 
121
 
        conf_btn = new Gtk.MenuButton();
122
 
        conf_btn.valign = Gtk.Align.START;
123
 
        conf_btn.halign = Gtk.Align.END;
124
 
        conf_btn.get_style_context ().remove_class ("button");
125
 
        conf_btn.get_style_context ().add_class ("titlebutton");
126
 
        conf_btn.set_image (new Gtk.Image.from_icon_name("document-properties", Gtk.IconSize.MENU));
127
 
 
128
 
        build_menu ();
129
 
 
130
 
        this.add (box);
131
 
        this.add_overlay (conf_btn);
132
186
    }
133
187
 
134
188
    private void remove_application () {
135
 
        delete_request ();
136
 
        this.get_parent().get_parent().remove(this.get_parent());
 
189
        desktop_file.delete_file ();
 
190
        deleted (this.get_parent());
137
191
        this.destroy ();
138
192
    }
139
 
 
140
 
    private void build_menu () {
141
 
 
142
 
                Gtk.Menu menu = new Gtk.Menu ();
143
 
 
144
 
                Gtk.MenuItem remove_app = new Gtk.MenuItem.with_label (_("Remove Application"));
145
 
                menu.add (remove_app);
146
 
 
147
 
        remove_app.activate.connect( () => {
148
 
            remove_application ();
149
 
        });
150
 
 
151
 
        menu.show_all();
152
 
        conf_btn.set_popup (menu);
 
193
}
 
194
 
 
195
public class ActionMenu : Gtk.Revealer {
 
196
 
 
197
    public signal void delete_clicked ();
 
198
    public signal void edit_clicked ();
 
199
 
 
200
    public ActionMenu () {
 
201
        var delete_button = new Gtk.Button.from_icon_name ("edit-delete-symbolic", Gtk.IconSize.BUTTON);
 
202
        delete_button.tooltip_text = _("Delete Webapp");
 
203
        delete_button.relief = Gtk.ReliefStyle.NONE;
 
204
        delete_button.clicked.connect (() => { delete_clicked (); });
 
205
 
 
206
        var edit_button = new Gtk.Button.from_icon_name ("edit-symbolic", Gtk.IconSize.BUTTON);
 
207
        edit_button.tooltip_text = _("Edit Webapp Properties");
 
208
        edit_button.relief = Gtk.ReliefStyle.NONE;
 
209
        edit_button.clicked.connect (() => { edit_clicked (); });
 
210
 
 
211
        var buttons = new Gtk.Grid ();
 
212
        buttons.orientation = Gtk.Orientation.HORIZONTAL;
 
213
        buttons.add (edit_button);
 
214
        buttons.add (delete_button);
 
215
        buttons.opacity = 0.5;
 
216
 
 
217
        this.transition_type = Gtk.RevealerTransitionType.CROSSFADE;
 
218
        this.add (buttons);
153
219
    }
154
220
}