|
| 1 | +/* |
| 2 | + * Copyright 2021 elementary, Inc (https://elementary.io) |
| 3 | + * 2021 José Expósito <[email protected]> |
| 4 | + * |
| 5 | + * This program is free software: you can redistribute it and/or modify |
| 6 | + * it under the terms of the GNU General Public License as published by |
| 7 | + * the Free Software Foundation, either version 3 of the License, or |
| 8 | + * (at your option) any later version. |
| 9 | + * |
| 10 | + * This program is distributed in the hope that it will be useful, |
| 11 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | + * GNU General Public License for more details. |
| 14 | + * |
| 15 | + * You should have received a copy of the GNU General Public License |
| 16 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 | + */ |
| 18 | + |
| 19 | +/** |
| 20 | + * Utility class to draw a Cairo.Surface in a Clutter.Actor. |
| 21 | + */ |
| 22 | +private class Gala.CairoImageActor : Clutter.Actor { |
| 23 | + public Cairo.Surface? image { get; construct; } |
| 24 | + |
| 25 | + public CairoImageActor (Cairo.Surface image, int x, int y, int width, int height) { |
| 26 | + Object (image: image); |
| 27 | + this.x = x; |
| 28 | + this.y = y; |
| 29 | + this.width = width; |
| 30 | + this.height = height; |
| 31 | + |
| 32 | + var canvas = new Clutter.Canvas (); |
| 33 | + canvas.set_size (width, height); |
| 34 | + canvas.draw.connect (draw_image); |
| 35 | + set_content (canvas); |
| 36 | + |
| 37 | + canvas.invalidate (); |
| 38 | + } |
| 39 | + |
| 40 | + private bool draw_image (Cairo.Context ctx) { |
| 41 | + Clutter.cairo_clear (ctx); |
| 42 | + |
| 43 | + ctx.set_source_surface (image, 0.0, 0.0); |
| 44 | + ctx.rectangle (0.0, 0.0, width, height); |
| 45 | + ctx.paint (); |
| 46 | + |
| 47 | + return true; |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +/** |
| 52 | + * Watch for style transitions (default to dark or or vice versa) and performs |
| 53 | + * an animation. |
| 54 | + */ |
| 55 | +public class Gala.StyleAnimationManager : Object { |
| 56 | + public WindowManager wm { get; construct; } |
| 57 | + |
| 58 | + /** |
| 59 | + * Time to wait displaying the CairoImageActor before starting |
| 60 | + * the fade animation. |
| 61 | + */ |
| 62 | + private const int WAIT_BEFORE_ANIMATE = 300; |
| 63 | + |
| 64 | + public StyleAnimationManager (WindowManager wm) { |
| 65 | + Object (wm: wm); |
| 66 | + } |
| 67 | + |
| 68 | + public void watch_style_transitions () { |
| 69 | + var granite_settings = Granite.Settings.get_default (); |
| 70 | + granite_settings.notify["prefers-color-scheme"].connect (() => { |
| 71 | + if (wm.enable_animations) { |
| 72 | + animate (); |
| 73 | + } |
| 74 | + }); |
| 75 | + } |
| 76 | + |
| 77 | + private void animate () { |
| 78 | + unowned var workspace_manager = wm.get_display ().get_workspace_manager (); |
| 79 | + unowned var workspace = workspace_manager.get_active_workspace (); |
| 80 | + |
| 81 | + foreach (unowned Meta.Window window in workspace.list_windows ()) { |
| 82 | + unowned var window_actor = window.get_compositor_private () as Meta.WindowActor; |
| 83 | + var rect = window.get_frame_rect (); |
| 84 | + int x = rect.x - (int) window_actor.x; |
| 85 | + int y = rect.y - (int) window_actor.y; |
| 86 | + int width = rect.width; |
| 87 | + int height = rect.height; |
| 88 | + |
| 89 | + var image = window_actor.get_image ({ x, y, width, height }); |
| 90 | + var image_actor = new CairoImageActor (image, x, y, width, height); |
| 91 | + window_actor.add_child (image_actor); |
| 92 | + |
| 93 | + Timeout.add (WAIT_BEFORE_ANIMATE, () => { |
| 94 | + image_actor.set_easing_duration (AnimationDuration.STYLE); |
| 95 | + image_actor.set_easing_mode (Clutter.AnimationMode.LINEAR); |
| 96 | + image_actor.opacity = 0; |
| 97 | + |
| 98 | + ulong signal_id = 0U; |
| 99 | + signal_id = image_actor.transitions_completed.connect (() => { |
| 100 | + image_actor.disconnect (signal_id); |
| 101 | + window_actor.remove_child (image_actor); |
| 102 | + image_actor.destroy (); |
| 103 | + }); |
| 104 | + |
| 105 | + return Source.REMOVE; |
| 106 | + }); |
| 107 | + } |
| 108 | + } |
| 109 | +} |
0 commit comments