-
-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathWallpaperContainer.vala
More file actions
199 lines (167 loc) · 6.56 KB
/
WallpaperContainer.vala
File metadata and controls
199 lines (167 loc) · 6.56 KB
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
193
194
195
196
197
198
199
/*-
* Copyright 2015-2022 elementary, Inc. (https://elementary.io)
*
* 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/>.
*
* Authored by: Erasmo Marín
*
*/
public class PantheonShell.WallpaperContainer : Granite.Bin {
public signal void trash ();
protected const int THUMB_WIDTH = 256;
protected const int THUMB_HEIGHT = 144;
protected Gtk.Picture image;
private static float monitor_ratio;
private Gtk.Revealer check_revealer;
public string? thumb_path { get; construct set; }
public bool thumb_valid { get; construct; }
public string uri { get; construct; }
public uint64 creation_date = 0;
public bool checked {
get {
return Gtk.StateFlags.CHECKED in get_state_flags ();
} set {
if (value) {
parent.set_state_flags (Gtk.StateFlags.CHECKED, false);
check_revealer.reveal_child = true;
} else {
parent.unset_state_flags (Gtk.StateFlags.CHECKED);
check_revealer.reveal_child = false;
}
queue_draw ();
}
}
public WallpaperContainer (string uri, string? thumb_path, bool thumb_valid) {
Object (uri: uri, thumb_path: thumb_path, thumb_valid: thumb_valid);
}
static construct {
var monitor = Gdk.Display.get_default ().get_monitor_at_surface (
(((Gtk.Application) Application.get_default ()).active_window).get_surface ()
);
monitor_ratio = (float) monitor.geometry.width / monitor.geometry.height;
}
construct {
image = new Gtk.Picture () {
can_shrink = false,
content_fit = COVER
};
image.add_css_class (Granite.CssClass.CARD);
var check = new Gtk.CheckButton () {
active = true,
halign = START,
valign = START,
can_focus = false,
can_target = false
};
check_revealer = new Gtk.Revealer () {
child = check,
transition_type = CROSSFADE
};
var overlay = new Gtk.Overlay () {
child = image
};
overlay.add_overlay (check_revealer);
var frame = new Gtk.AspectFrame (0.5f, 0.5f, monitor_ratio, false) {
child = overlay
};
child = frame;
height_request = THUMB_HEIGHT;
if (uri != null) {
var remove_wallpaper_action = new SimpleAction ("trash", null);
remove_wallpaper_action.activate.connect (() => trash ());
var action_group = new SimpleActionGroup ();
action_group.add_action (remove_wallpaper_action);
insert_action_group ("wallpaper", action_group);
var file = File.new_for_uri (uri);
try {
var info = file.query_info ("*", FileQueryInfoFlags.NONE);
creation_date = info.get_attribute_uint64 (GLib.FileAttribute.TIME_CREATED);
remove_wallpaper_action.set_enabled (info.get_attribute_boolean (GLib.FileAttribute.ACCESS_CAN_DELETE));
} catch (Error e) {
critical (e.message);
}
var menu_model = new Menu ();
menu_model.append (_("Remove"), "wallpaper.trash");
var context_menu = new Gtk.PopoverMenu.from_model (menu_model) {
halign = START,
has_arrow = false
};
context_menu.set_parent (this);
var secondary_click_gesture = new Gtk.GestureClick () {
button = Gdk.BUTTON_SECONDARY
};
secondary_click_gesture.released.connect ((n_press, x, y) => {
secondary_click_gesture.set_state (CLAIMED);
context_menu.pointing_to = Gdk.Rectangle () {
x = (int) x,
y = (int) y
};
context_menu.popup ();
});
add_controller (secondary_click_gesture);
}
try {
if (uri != null) {
if (thumb_path != null && thumb_valid) {
update_thumb.begin ();
} else {
generate_and_load_thumb ();
}
} else {
image.set_filename (thumb_path);
}
} catch (Error e) {
critical ("Failed to load wallpaper thumbnail: %s", e.message);
return;
}
}
private void generate_and_load_thumb () {
ThumbnailGenerator.get_default ().get_thumbnail (uri, THUMB_WIDTH * scale_factor, () => {
try {
var file = File.new_for_uri (uri);
var info = file.query_info (FileAttribute.THUMBNAIL_PATH + "," + FileAttribute.THUMBNAIL_IS_VALID, 0);
thumb_path = info.get_attribute_as_string (FileAttribute.THUMBNAIL_PATH);
update_thumb.begin ();
} catch (Error e) {
warning ("Error loading thumbnail for '%s': %s", uri, e.message);
}
});
}
private async void update_thumb () {
if (thumb_path == null) {
return;
}
image.set_filename (thumb_path);
if (uri != null) {
string path = "";
GExiv2.Metadata metadata;
try {
path = Filename.from_uri (uri);
metadata = new GExiv2.Metadata ();
metadata.open_path (path);
} catch (Error e) {
warning ("Error parsing exif metadata of \"%s\": %s", path, e.message);
return;
}
if (metadata.has_exif ()) {
try {
var artist_name = metadata.try_get_tag_string ("Exif.Image.Artist");
if (artist_name != null) {
tooltip_text = _("Artist: %s").printf (artist_name);
}
} catch (Error e) {
critical ("Unable to set wallpaper artist name: %s", e.message);
}
}
}
}
}