~ricotz/slingshot/port-gmenu3

« back to all changes in this revision

Viewing changes to src/Widgets/AppEntry.vala

  • Committer: RabbitBot
  • Author(s): Corentin Noël
  • Date: 2014-01-14 13:05:03 UTC
  • mfrom: (396.1.1 slingshot)
  • Revision ID: rabbitbot-20140114130503-vl01yvhtdlu0dvs4
* Removed every using statement
* Changed VWidgets and HWidgets to Widgets with the corresponding orientation (Gtk deprecation)
* Ported Zeitgeist parts to version 2.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
16
16
//  along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
17
//
18
18
 
19
 
using Gtk;
20
 
using Gdk;
21
 
using Cairo;
22
 
 
23
 
namespace Slingshot.Widgets {
24
 
 
25
 
    public class AppEntry : Button {
26
 
 
27
 
        public Label app_label;
28
 
        private Pixbuf icon;
29
 
        private Box layout;
30
 
 
31
 
        public string exec_name;
32
 
        public string app_name;
33
 
        public string desktop_id;
34
 
        public int icon_size;
35
 
        public string desktop_path;
36
 
 
37
 
        public signal void app_launched ();
38
 
 
39
 
        private double alpha = 1.0;
40
 
        private bool   dragging = false; //prevent launching
41
 
 
42
 
        private Backend.App application;
43
 
 
44
 
        public AppEntry (Backend.App app) {
45
 
            TargetEntry dnd = {"text/uri-list", 0, 0};
46
 
            Gtk.drag_source_set (this, Gdk.ModifierType.BUTTON1_MASK, {dnd},
47
 
                Gdk.DragAction.COPY);
48
 
 
49
 
            app_paintable = true;
50
 
            set_visual (get_screen ().get_rgba_visual());
51
 
            set_size_request (130, 130);
52
 
            desktop_id = app.desktop_id;
53
 
            desktop_path = app.desktop_path;
54
 
 
55
 
            application = app;
56
 
            app_name = app.name;
57
 
            tooltip_text = app.description;
58
 
            exec_name = app.exec;
59
 
            icon_size = Slingshot.settings.icon_size;
60
 
            icon = app.icon;
61
 
 
62
 
            get_style_context ().add_class ("app");
63
 
 
64
 
            app_label = new Label (app_name);
65
 
            app_label.halign = Align.CENTER;
66
 
            app_label.justify = Justification.CENTER;
67
 
            app_label.set_line_wrap (true); // Need a smarter way
68
 
            app_label.set_single_line_mode (false);
69
 
            app_label.set_ellipsize (Pango.EllipsizeMode.END);
70
 
 
71
 
            layout = new Box (Orientation.VERTICAL, 0);
72
 
            layout.homogeneous = false;
73
 
 
74
 
            layout.pack_start (app_label, false, true, 0);
75
 
 
76
 
            add (Utils.set_padding (layout, 78, 5, 5, 5));
77
 
 
78
 
            this.clicked.connect (launch_app);
79
 
 
80
 
            this.button_press_event.connect ((e) => {return e.button == 3;});
81
 
 
82
 
            this.drag_begin.connect ( (ctx) => {
83
 
                this.dragging = true;
84
 
                Gtk.drag_set_icon_pixbuf (ctx, icon, 0, 0);
85
 
            });
86
 
            this.drag_end.connect ( () => {
87
 
                this.dragging = false;
88
 
            });
89
 
            this.drag_data_get.connect ( (ctx, sel, info, time) => {
90
 
                sel.set_uris ({File.new_for_path (desktop_path).get_uri ()});
91
 
            });
92
 
 
93
 
            app.icon_changed.connect (queue_draw);
94
 
 
95
 
        }
96
 
 
97
 
        protected override bool draw (Context cr) {
98
 
 
99
 
 
100
 
            Allocation size;
101
 
            get_allocation (out size);
102
 
 
103
 
            base.draw (cr);
104
 
 
105
 
            // Draw icon
106
 
            Gdk.cairo_set_source_pixbuf (cr, icon, (icon.width - size.width) / -2.0, 10);
107
 
            cr.paint_with_alpha (alpha);
108
 
 
109
 
            return true;
110
 
 
111
 
        }
112
 
 
113
 
        public void fade_out () {
114
 
 
115
 
            Timeout.add (20, () => {
116
 
 
117
 
                if (alpha <= 0.3) {
118
 
                    queue_draw ();
119
 
                    return false;
120
 
                }
121
 
 
122
 
                alpha -= 0.05;
123
 
                queue_draw ();
124
 
                return true;
125
 
 
126
 
            });
127
 
 
128
 
        }
129
 
 
130
 
        public void fade_in () {
131
 
 
132
 
            Timeout.add (20, () => {
133
 
 
134
 
                if (alpha == 1.0) {
135
 
                    queue_draw ();
136
 
                    return false;
137
 
                }
138
 
 
139
 
                alpha += 0.05;
140
 
                queue_draw ();
141
 
                return true;
142
 
 
143
 
            });
144
 
 
145
 
        }
146
 
 
147
 
        public void launch_app () {
148
 
            application.launch ();
149
 
            app_launched ();
150
 
        }
 
19
public class Slingshot.Widgets.AppEntry : Gtk.Button {
 
20
 
 
21
    public Gtk.Label app_label;
 
22
    private Gdk.Pixbuf icon;
 
23
    private Gtk.Box layout;
 
24
 
 
25
    public string exec_name;
 
26
    public string app_name;
 
27
    public string desktop_id;
 
28
    public int icon_size;
 
29
    public string desktop_path;
 
30
 
 
31
    public signal void app_launched ();
 
32
 
 
33
    private double alpha = 1.0;
 
34
    private bool   dragging = false; //prevent launching
 
35
 
 
36
    private Backend.App application;
 
37
 
 
38
    public AppEntry (Backend.App app) {
 
39
        Gtk.TargetEntry dnd = {"text/uri-list", 0, 0};
 
40
        Gtk.drag_source_set (this, Gdk.ModifierType.BUTTON1_MASK, {dnd},
 
41
            Gdk.DragAction.COPY);
 
42
 
 
43
        app_paintable = true;
 
44
        set_visual (get_screen ().get_rgba_visual());
 
45
        set_size_request (130, 130);
 
46
        desktop_id = app.desktop_id;
 
47
        desktop_path = app.desktop_path;
 
48
 
 
49
        application = app;
 
50
        app_name = app.name;
 
51
        tooltip_text = app.description;
 
52
        exec_name = app.exec;
 
53
        icon_size = Slingshot.settings.icon_size;
 
54
        icon = app.icon;
 
55
 
 
56
        get_style_context ().add_class ("app");
 
57
 
 
58
        app_label = new Gtk.Label (app_name);
 
59
        app_label.halign = Gtk.Align.CENTER;
 
60
        app_label.justify = Gtk.Justification.CENTER;
 
61
        app_label.set_line_wrap (true); // Need a smarter way
 
62
        app_label.set_single_line_mode (false);
 
63
        app_label.set_ellipsize (Pango.EllipsizeMode.END);
 
64
 
 
65
        layout = new Gtk.Box (Gtk.Orientation.VERTICAL, 0);
 
66
        layout.homogeneous = false;
 
67
 
 
68
        layout.pack_start (app_label, false, true, 0);
 
69
 
 
70
        add (Utils.set_padding (layout, 78, 5, 5, 5));
 
71
 
 
72
        this.clicked.connect (launch_app);
 
73
 
 
74
        this.button_press_event.connect ((e) => {return e.button == 3;});
 
75
 
 
76
        this.drag_begin.connect ( (ctx) => {
 
77
            this.dragging = true;
 
78
            Gtk.drag_set_icon_pixbuf (ctx, icon, 0, 0);
 
79
        });
 
80
        this.drag_end.connect ( () => {
 
81
            this.dragging = false;
 
82
        });
 
83
        this.drag_data_get.connect ( (ctx, sel, info, time) => {
 
84
            sel.set_uris ({File.new_for_path (desktop_path).get_uri ()});
 
85
        });
 
86
 
 
87
        app.icon_changed.connect (queue_draw);
 
88
 
 
89
    }
 
90
 
 
91
    protected override bool draw (Cairo.Context cr) {
 
92
 
 
93
 
 
94
        Gtk.Allocation size;
 
95
        get_allocation (out size);
 
96
 
 
97
        base.draw (cr);
 
98
 
 
99
        // Draw icon
 
100
        Gdk.cairo_set_source_pixbuf (cr, icon, (icon.width - size.width) / -2.0, 10);
 
101
        cr.paint_with_alpha (alpha);
 
102
 
 
103
        return true;
 
104
 
 
105
    }
 
106
 
 
107
    public void fade_out () {
 
108
 
 
109
        Timeout.add (20, () => {
 
110
 
 
111
            if (alpha <= 0.3) {
 
112
                queue_draw ();
 
113
                return false;
 
114
            }
 
115
 
 
116
            alpha -= 0.05;
 
117
            queue_draw ();
 
118
            return true;
 
119
 
 
120
        });
 
121
 
 
122
    }
 
123
 
 
124
    public void fade_in () {
 
125
 
 
126
        Timeout.add (20, () => {
 
127
 
 
128
            if (alpha == 1.0) {
 
129
                queue_draw ();
 
130
                return false;
 
131
            }
 
132
 
 
133
            alpha += 0.05;
 
134
            queue_draw ();
 
135
            return true;
 
136
 
 
137
        });
 
138
 
 
139
    }
 
140
 
 
141
    public void launch_app () {
 
142
        application.launch ();
 
143
        app_launched ();
151
144
    }
152
145
}