Debian Codegraph @

src/widgets/PhotoWindow.vala

raw
1 /*
2 * Copyright (c) 2017-2019 José Amuedo (https://github.com/spheras)
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18 /**
19 * @class
20 * Photo Window to show a photo
21 */
22 public class DesktopFolder.PhotoWindow : Gtk.ApplicationWindow {
23 /** parent manager of this window */
24 private PhotoManager manager = null;
25 /** Context menu of the Folder Window */
26 private Gtk.Menu menu = null;
27
28 // private const string FIXO_TAGS_COLORS[7] = { null, "#fce94f", "#8ae234", "#729fcf", "#fe44f8", "#FFFFFF", "#000000" };
29 private const string FIXO_TAGS_COLORS[9] = { null, "#ffe16b", "#ffa154", "#9bdb4d", "#64baff", "#ad65d6", "#ed5353", "#ffffff", "#000000" };
30
31 // cached shadow photo and fixos
32 private Cairo.Surface shadowSurface = null;
33 private Cairo.Surface photoSurface = null;
34 private Gdk.Pixbuf fixoPixbuf = null;
35
36 // flag to know if the window is being resized
37 private bool flag_resizing = false;
38 private uint timeout_id = 0;
39 // flag to know if the mouse is over the Window
40 private bool flag_over = false;
41 /** flag to know if the window was painted /packed already */
42 private bool flag_realized = false;
43 /** flag to know if the window is being dragged */
44 private bool flag_dragged = false;
45
46 construct {
47 set_keep_below (true);
48 stick ();
49 this.hide_titlebar_when_maximized = false;
50 set_type_hint (Gdk.WindowTypeHint.MENU);
51 set_skip_taskbar_hint (true);
52 this.set_property ("skip-taskbar-hint", true);
53 }
54
55 /**
56 * @constructor
57 * @param FolderManager manager the manager of this window
58 */
59 public PhotoWindow (PhotoManager manager) {
60 Object (
61 application: manager.get_application (),
62 icon_name: "com.github.spheras.desktopfolder",
63 resizable: true,
64 skip_taskbar_hint: true,
65 decorated: true,
66 type_hint: Gdk.WindowTypeHint.DESKTOP,
67 title: "",
68 deletable: false,
69 width_request: 0,
70 height_request: 0
71 );
72
73 DesktopManager desktop_manager = manager.get_application ().get_fake_desktop ();
74 if (desktop_manager != null) {
75 this.set_transient_for (desktop_manager.get_view ());
76 }
77
78 // this.set_titlebar (null);
79 var header = new Gtk.HeaderBar ();
80 this.set_titlebar (header);
81
82
83 this.set_skip_taskbar_hint (true);
84 this.set_property ("skip-taskbar-hint", true);
85 // setting the folder name
86 this.manager = manager;
87
88 // important to load settings 2 times, now and after realized event
89 this.reload_settings ();
90 this.realize.connect (() => {
91 if (!this.flag_realized) {
92 this.flag_realized = true;
93 // we need to reload settings to ensure that it get the real sizes and positiions
94 this.reload_settings ();
95 }
96 });
97
98 this.show_all ();
99
100 // connecting to events
101 this.configure_event.connect (this.on_configure);
102 this.motion_notify_event.connect ((event) => {
103 this.flag_dragged = false;
104 // forzing to draw the borders background
105 this.flag_over = true;
106 this.queue_draw ();
107 return true;
108 });
109 this.leave_notify_event.connect ((event) => {
110 var is_dragging_window = (event.detail == 3 && this.flag_dragged);
111 if (!is_dragging_window) {
112 this.flag_over = false;
113 this.queue_draw ();
114 }
115 return true;
116 });
117
118 this.button_press_event.connect (this.on_button_press);
119 this.key_release_event.connect (this.on_key_release);
120 this.button_release_event.connect (this.on_button_release);
121 this.draw.connect (this.draw_background);
122
123 // help: doesn't have the gtk window any active signal? or css :active state?
124 Wnck.Screen screen = Wnck.Screen.get_default ();
125 screen.active_window_changed.connect (on_active_change);
126 }
127
128 /**
129 * @name fade_in
130 * @description fade the view in. does not call show
131 */
132 public void fade_in () {
133 this.get_style_context ().remove_class ("df_fadeout");
134 this.get_style_context ().add_class ("df_fadein");
135 }
136
137 /**
138 * @name fade_out
139 * @description fade the view out. does not call hide
140 */
141 public void fade_out () {
142 this.get_style_context ().remove_class ("df_fadein");
143 this.get_style_context ().add_class ("df_fadeout");
144 }
145
146 /**
147 * @name move_to
148 * @description move the window to other position
149 */
150 protected virtual void move_to (int x, int y) {
151 // debug ("MOVE_TO: %d,%d", x, y);
152 this.move (x, y);
153 }
154
155 /**
156 * @name resize_to
157 * @description resize the window to other position
158 */
159 public virtual void resize_to (int width, int height) {
160 // debug ("RESIZE_TO: %d,%d", width, height);
161 this.set_default_size (width, height);
162 this.resize (width, height);
163 }
164
165 /**
166 * @name reload_settings
167 * @description reload the window style in general
168 */
169 public void reload_settings () {
170 // let's load the settings of the folder (if exist or a new one)
171 PhotoSettings settings = this.manager.get_settings ();
172 if (settings.w > 0) {
173 // applying existing position and size configuration
174 this.resize_to (settings.w, settings.h);
175 }
176 if (settings.x > 0 || settings.y > 0) {
177 this.move_to (settings.x, settings.y);
178 }
179
180 List <unowned string> classes = this.get_style_context ().list_classes ();
181 for (int i = 0; i < classes.length (); i++) {
182 string class = classes.nth_data (i);
183 if (class.has_prefix ("df_")) {
184 this.get_style_context ().remove_class (class);
185 }
186 }
187
188
189 // we set a class to this window to manage the css
190 this.get_style_context ().add_class ("df_folder");
191 this.get_style_context ().add_class ("df_photo");
192 this.get_style_context ().add_class ("df_transparent");
193 this.get_style_context ().add_class ("df_headless");
194
195 this.get_style_context ().add_class ("df_fadingwindow");
196 if (this.manager.get_application ().get_desktop_visibility ()) {
197 this.get_style_context ().add_class ("df_fadein");
198 // setting opacity to stop the folder window flashing at startup
199 this.opacity = 1;
200 } else {
201 this.get_style_context ().add_class ("df_fadeout");
202 // ditto
203 this.opacity = 0;
204 }
205 }
206
207 /**
208 * @name on_active_change
209 * @description the screen actived window has change signal
210 * @param {Wnck.Window} the previous actived window
211 */
212 private void on_active_change (Wnck.Window ? previous) {
213 string sclass = "df_active";
214 Gtk.StyleContext style = this.get_style_context ();
215 if (this.is_active) {
216 if (!style.has_class (sclass)) {
217 style.add_class ("df_active");
218 // we need to force a queue_draw
219 this.queue_draw ();
220 }
221 } else {
222 if (style.has_class (sclass)) {
223 style.remove_class ("df_active");
224 this.type_hint = Gdk.WindowTypeHint.DESKTOP;
225 // we need to force a queue_draw
226 this.queue_draw ();
227 }
228 }
229
230 this.save_current_position_and_size ();
231 }
232
233 /**
234 * @name save_position_and_size
235 * @description save the current position and size of the window
236 */
237 public void save_current_position_and_size () {
238 // we are saving here the last position and size
239 // we avoid doing it at on_configure because it launches a lot of events
240 Gtk.Allocation all;
241 int x = 0;
242 int y = 0;
243 int w = 0;
244 int h = 0;
245 this.get_position (out x, out y);
246 this.get_allocation (out all);
247 this.get_size (out w, out h);
248 // debug ("set_new_shape:%i,%i,%i,%i", x, y, w, h);
249 this.manager.set_new_shape (x, y, w, h);
250 }
251
252 /**
253 * @name on_configure
254 * @description the configure event is produced when the window change its dimensions or location settings
255 * @return {bool} @see configure_event signal
256 */
257 private bool on_configure (Gdk.EventConfigure event) {
258 if (event.type == Gdk.EventType.CONFIGURE) {
259 // we are now a dock Window, to avoid minimization when show desktop
260 // TODO exists a way to make resizable and moveable a dock window?
261 this.type_hint = Gdk.WindowTypeHint.DESKTOP;
262
263 // debug("configure event:%i,%i,%i,%i",event.x,event.y,event.width,event.height);
264 /*this.manager.set_new_shape (event.x, event.y, event.width, event.height);*/
265
266 // reseting cached images
267 this.shadowSurface = null;
268 this.photoSurface = null;
269 this.fixoPixbuf = null;
270 }
271 return false;
272 }
273
274 /**
275 * @name on_release
276 * @description release event captured.
277 * @return bool @see widget on_release signal
278 */
279 private bool on_button_release (Gdk.EventButton event) {
280 // we are now a dock Window, to avoid minimization when show desktop
281 // TODO exists a way to make resizable and moveable a dock window?
282 this.type_hint = Gdk.WindowTypeHint.DESKTOP;
283 return false;
284 }
285
286 /**
287 * @name on_key_release
288 * @description a key pressed was released
289 * @return bool @see widget key_release_event
290 */
291 private bool on_key_release (Gdk.EventKey event) {
292 if (event.type == Gdk.EventType.KEY_RELEASE && event.str == " ") {
293 this.manager.open ();
294 return true;
295 }
296 return false;
297 }
298
299 /**
300 * @name on_button_press
301 * @description press event captured. The Window should show the popup on right button
302 * @return bool @see widget button_press_event signal
303 */
304 private bool on_button_press (Gdk.EventButton event) {
305 // we are now a normal Window, to allow resizing and movement
306 // TODO exists a way to make resizable and moveable a dock window?
307 this.type_hint = Gdk.WindowTypeHint.NORMAL;
308
309 // debug("press:%i,%i",(int)event.button,(int)event.y);
310 if (event.type == Gdk.EventType.BUTTON_PRESS &&
311 (event.button == Gdk.BUTTON_SECONDARY)) {
312 this.show_popup (event);
313 return true;
314 } else if (event.type == Gdk.EventType.BUTTON_PRESS &&
315 (event.button == Gdk.BUTTON_PRIMARY)) {
316 int width = this.get_allocated_width ();
317 int height = this.get_allocated_height ();
318 int margin = 30;
319 // debug("x:%d,y:%d,width:%d,height:%d",(int)event.x,(int) event.y,width,height);
320 if (event.x > margin && event.y > margin && event.x < width - margin && event.y < height - margin) {
321 this.flag_dragged = true;
322 this.begin_move_drag ((int) event.button, (int) event.x_root, (int) event.y_root, event.time);
323 } else {
324 this.flag_resizing = true;
325 }
326 } else if (event.type == Gdk.EventType.DOUBLE_BUTTON_PRESS) {
327 this.manager.open ();
328 }
329
330 return false;
331 }
332
333 /**
334 * @name show_popup
335 * @description build and show the popup menu
336 * @param event EventButton the origin event, needed to position the menu
337 */
338 private void show_popup (Gdk.EventButton event) {
339 this.type_hint = Gdk.WindowTypeHint.DESKTOP;
340
341 this.menu = new Gtk.Menu ();
342
343 if (!this.manager.get_application ().get_desktoppanel_enabled ()) {
344 Gtk.MenuItem item_new = new Gtk.MenuItem.with_label (DesktopFolder.Lang.DESKTOPFOLDER_MENU_NEW_SUBMENU);
345 menu.append (item_new);
346
347 Gtk.Menu newmenu = new Gtk.Menu ();
348 item_new.set_submenu (newmenu);
349
350 Gtk.MenuItem item = new Gtk.MenuItem.with_label (DesktopFolder.Lang.DESKTOPFOLDER_MENU_NEW_DESKTOP_FOLDER);
351 item.activate.connect ((item) => {
352 this.new_desktop_folder ((int) event.x, (int) event.y);
353 });
354 newmenu.append (item);
355
356 item = new Gtk.MenuItem.with_label (DesktopFolder.Lang.DESKTOPFOLDER_MENU_LINK_PANEL);
357 item.activate.connect ((item) => {
358 this.new_link_panel ((int) event.x, (int) event.y);
359 });
360 newmenu.append (item);
361
362 item = new Gtk.MenuItem.with_label (DesktopFolder.Lang.DESKTOPFOLDER_MENU_NEW_NOTE);
363 item.activate.connect ((item) => {
364 this.new_note ((int) event.x, (int) event.y);
365 });
366 newmenu.append (item);
367
368 item = new Gtk.MenuItem.with_label (DesktopFolder.Lang.DESKTOPFOLDER_MENU_NEW_PHOTO);
369 item.activate.connect ((item) => {
370 this.new_photo ((int) event.x, (int) event.y);
371 });
372 newmenu.append (item);
373
374 menu.append (new MenuItemSeparator ());
375 }
376
377 Gtk.MenuItem item = new Gtk.MenuItem.with_label (DesktopFolder.Lang.ITEM_MENU_OPEN);
378 item.activate.connect ((item) => { this.manager.open (); });
379 menu.append (item);
380
381 item = new Gtk.MenuItem.with_label (DesktopFolder.Lang.PHOTO_MENU_DELETE_PHOTO);
382 item.activate.connect ((item) => { this.manager.delete (); });
383 menu.append (item);
384
385 item = new MenuItemColor (FIXO_TAGS_COLORS, null);
386 ((MenuItemColor) item).color_changed.connect (change_fixo_color);
387 menu.append (item);
388
389 menu.show_all ();
390 menu.popup_at_pointer (null);
391 }
392
393 /**
394 * @name change_fixo_color
395 * @description change event captured from the popup for a new color to the fixo color
396 * @param ncolor int the new color for the fixo
397 */
398 private void change_fixo_color (int ncolor) {
399 this.manager.get_settings ().fixocolor = ncolor;
400 // reseting fixo images
401 this.fixoPixbuf = null;
402 this.queue_draw ();
403 }
404
405 /**
406 * @name new_desktop_folder
407 * @description create a new desktop folder
408 */
409 private void new_desktop_folder (int x, int y) {
410 DesktopFolder.Util.create_new_desktop_folder (this, x, y);
411 }
412
413 /**
414 * @name new_link_panel
415 * @description create a new link panel
416 */
417 private void new_link_panel (int x, int y) {
418 DesktopFolder.Util.create_new_link_panel (this, x, y);
419 }
420
421 /*
422 * @name new_note
423 * @description create a new note
424 */
425 private void new_note (int x, int y) {
426 DesktopFolder.Util.create_new_note (this, x, y);
427 }
428
429 /**
430 * @name new_photo
431 * @description create a new photo
432 */
433 private void new_photo (int x, int y) {
434 DesktopFolder.Util.create_new_photo (this, x, y);
435 }
436
437 /**
438 * @name draw_background
439 * @description draw the note window background intercepting the draw signal
440 * @param {Cairo.Context} cr the cairo context
441 * @bool @see draw signal
442 */
443 private bool draw_background (Cairo.Context cr) {
444 int width = 0;
445 int height = 0;
446 this.get_size (out width, out height);
447 int MAGIC_NUMBER = 56;
448 height = height + MAGIC_NUMBER;
449
450 // debug ("-width:%d, -height:%d", width, height);
451
452 cr.set_operator (Cairo.Operator.CLEAR);
453 cr.paint ();
454 cr.set_operator (Cairo.Operator.OVER);
455
456 try {
457 // the image dimensions
458 int margin = 50;
459 int halfmargin = 25;
460 int pixwidth = width - margin;
461 int pixheight = height - margin;
462 if (this.manager.get_settings ().original_width > 0) {
463 pixheight = (pixwidth * this.manager.get_settings ().original_height) / this.manager.get_settings ().original_width;
464 if (pixheight > (height - margin)) {
465 pixheight = height - margin;
466 pixwidth = (pixheight * this.manager.get_settings ().original_width) / this.manager.get_settings ().original_height;
467 }
468 }
469
470 // debug ("pixwidth:%d, pixheight:%d", pixwidth, pixheight);
471 if (this.flag_resizing || this.flag_over) {
472 if (this.flag_resizing) {
473 cr.set_source_rgba (0, 0, 0, 0.4);
474 } else {
475 cr.set_source_rgba (0, 0, 0, 0.2);
476 }
477 cr.rectangle (0, 0, width, height);
478 cr.fill ();
479 } else {
480 cr.set_source_rgba (255, 0, 0, 0);
481 cr.rectangle (0, 0, width, height);
482 cr.fill ();
483 }
484
485 if (this.flag_resizing) {
486 if (this.timeout_id > 0) {
487 GLib.Source.remove (this.timeout_id);
488 this.timeout_id = 0;
489 }
490 this.timeout_id = GLib.Timeout.add (1000, () => {
491 // we force to resize to adapt to the image size, (to maintain aspect ratio)
492 // this.resize (pixwidth + margin, pixheight + margin);
493 this.flag_resizing = false;
494 this.queue_draw ();
495 // debug ("!!resizing to %d,%d", pixwidth + margin, pixheight + margin);
496 this.resize_to (pixwidth + margin, pixheight + margin - MAGIC_NUMBER);
497 this.timeout_id = 0;
498
499 this.save_current_position_and_size ();
500 return false;
501 });
502 }
503
504 // drawing the shadow
505 if (this.manager.get_settings ().fixocolor == 0) {
506 if (this.shadowSurface == null) {
507 var shadowPixbuf = new Gdk.Pixbuf.from_resource ("/com/github/spheras/desktopfolder/shadow.png");
508 var shadowHeight = 40;
509 if (pixwidth < 100 || pixheight < 100) {
510 if (pixwidth < pixheight) {
511 shadowHeight = (int) (shadowHeight * (pixwidth / 100f));
512 } else {
513 shadowHeight = (int) (shadowHeight * (pixheight / 100f));
514 }
515 }
516 shadowPixbuf = shadowPixbuf.scale_simple (pixwidth, shadowHeight, Gdk.InterpType.BILINEAR);
517 this.shadowSurface = Gdk.cairo_surface_create_from_pixbuf (shadowPixbuf, 0, null);
518 }
519 cr.set_source_surface (this.shadowSurface, halfmargin, pixheight + 20);
520 cr.paint ();
521 }
522
523
524 // the photo
525 if (photoSurface == null) {
526 var photopath = this.manager.get_settings ().photo_path;
527 var photoPixbuf = new Gdk.Pixbuf.from_file (photopath);
528 photoPixbuf = photoPixbuf.scale_simple (pixwidth, pixheight, Gdk.InterpType.BILINEAR);
529 this.photoSurface = Gdk.cairo_surface_create_from_pixbuf (photoPixbuf, 0, null);
530 // DesktopFolder.Util.blur_image_surface((Cairo.ImageSurface)this.photoSurface,4);
531 }
532 cr.set_source_surface (this.photoSurface, halfmargin, halfmargin);
533 cr.paint ();
534
535 // lets draw the fixo
536 int defaultFixoWidth = 56;
537 int defaultFixoHeight = 56;
538 int fixoWidth = 56;
539 int fixoHeight = 56;
540 int fixoMargin = 4;
541 int fixocolor = this.manager.get_settings ().fixocolor;
542 var color = "";
543 switch (fixocolor) {
544 case 0:
545 color = null;
546 break;
547 case 1:
548 color = "banana";
549 break;
550 case 2:
551 color = "orange";
552 break;
553 case 3:
554 color = "lime";
555 break;
556 case 4:
557 color = "blueberry";
558 break;
559 case 5:
560 color = "grape";
561 break;
562 case 6:
563 color = "strawberry";
564 break;
565 case 7:
566 color = "white";
567 break;
568 default:
569 case 8:
570 color = "black";
571 break;
572 }
573 if (color != null) {
574 if (this.fixoPixbuf == null) {
575 this.fixoPixbuf = new Gdk.Pixbuf.from_resource ("/com/github/spheras/desktopfolder/fixo-" + color + ".svg");
576
577 var fixoWidthScaled = fixoWidth;
578 var fixoHeightScaled = fixoHeight;
579 if (pixwidth < 100 || pixheight < 100) {
580 if (pixwidth < pixheight) {
581 fixoWidthScaled = fixoWidth * pixwidth / 100;
582 fixoHeightScaled = fixoWidthScaled;
583 } else {
584 fixoHeightScaled = fixoHeight * pixheight / 100;
585 fixoWidthScaled = fixoHeightScaled;
586 }
587 this.fixoPixbuf = fixoPixbuf.scale_simple (fixoWidthScaled, fixoHeightScaled, Gdk.InterpType.BILINEAR);
588 fixoWidth = fixoWidthScaled;
589 fixoHeight = fixoHeightScaled;
590 }
591
592 } else {
593 fixoWidth = this.fixoPixbuf.get_width ();
594 fixoHeight = this.fixoPixbuf.get_height ();
595 }
596
597 var fixoSurface = Gdk.cairo_surface_create_from_pixbuf (this.fixoPixbuf, 0, null);
598 var fixoLeft = fixoMargin + (defaultFixoWidth - fixoWidth) / 2.5;
599 var fixoTop = fixoMargin + (defaultFixoHeight - fixoHeight) / 2.5;
600 cr.set_source_surface (fixoSurface, fixoLeft, fixoTop);
601 cr.paint ();
602
603 var rotatedPixbuf = this.fixoPixbuf.rotate_simple (Gdk.PixbufRotation.COUNTERCLOCKWISE);
604 fixoSurface = Gdk.cairo_surface_create_from_pixbuf (rotatedPixbuf, 0, null);
605 fixoLeft = pixwidth + margin - fixoWidth - fixoMargin - (defaultFixoWidth - fixoWidth) / 2.5;
606 fixoTop = fixoMargin + (defaultFixoHeight - fixoHeight) / 2.5;
607 cr.set_source_surface (fixoSurface, fixoLeft, fixoTop);
608 cr.paint ();
609
610 rotatedPixbuf = rotatedPixbuf.rotate_simple (Gdk.PixbufRotation.COUNTERCLOCKWISE);
611 fixoSurface = Gdk.cairo_surface_create_from_pixbuf (rotatedPixbuf, 0, null);
612 fixoLeft = pixwidth + margin - fixoWidth - fixoMargin - (defaultFixoWidth - fixoWidth) / 2.5;
613 fixoTop = pixheight + margin - fixoHeight - fixoMargin - (defaultFixoHeight - fixoHeight) / 2.5;
614 cr.set_source_surface (fixoSurface, fixoLeft, fixoTop);
615 cr.paint ();
616
617 rotatedPixbuf = rotatedPixbuf.rotate_simple (Gdk.PixbufRotation.COUNTERCLOCKWISE);
618 fixoSurface = Gdk.cairo_surface_create_from_pixbuf (rotatedPixbuf, 0, null);
619 fixoLeft = fixoMargin + (defaultFixoWidth - fixoWidth) / 2.5;
620 fixoTop = pixheight + margin - fixoHeight - fixoMargin - (defaultFixoHeight - fixoHeight) / 2.5;
621 cr.set_source_surface (fixoSurface, fixoLeft, fixoTop);
622 cr.paint ();
623 }
624
625 } catch (Error e) {
626 // error! ??
627 stderr.printf ("Error: %s\n", e.message);
628 }
629
630 return true;
631 }
632
633 }