|
| 1 | +/* |
| 2 | + * Copyright 2020-2025 elementary, Inc (https://elementary.io) |
| 3 | + * 2014 Tom Beckmann |
| 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 | +public class GreeterCompositor.NotificationStack : Object { |
| 20 | + private const string TRANSITION_ENTRY_NAME = "entry"; |
| 21 | + private const int CLOSE_ANIMATION_DURATION = 195; |
| 22 | + |
| 23 | + // we need to keep a small offset to the top, because we clip the container to |
| 24 | + // its allocations and the close button would be off for the first notification |
| 25 | + private const int TOP_OFFSET = 2; |
| 26 | + private const int ADDITIONAL_MARGIN = 12; |
| 27 | + private const int MARGIN = 12; |
| 28 | + |
| 29 | + private const int WIDTH = 300; |
| 30 | + |
| 31 | + private int stack_y; |
| 32 | + private int stack_width; |
| 33 | + |
| 34 | + public Meta.Display display { get; construct; } |
| 35 | + |
| 36 | + private Gee.ArrayList<unowned Meta.WindowActor> notifications; |
| 37 | + |
| 38 | + public NotificationStack (Meta.Display display) { |
| 39 | + Object (display: display); |
| 40 | + } |
| 41 | + |
| 42 | + construct { |
| 43 | + notifications = new Gee.ArrayList<unowned Meta.WindowActor> (); |
| 44 | + |
| 45 | + unowned var monitor_manager = display.get_context ().get_backend ().get_monitor_manager (); |
| 46 | + monitor_manager.monitors_changed_internal.connect (update_stack_allocation); |
| 47 | + display.workareas_changed.connect (update_stack_allocation); |
| 48 | + update_stack_allocation (); |
| 49 | + } |
| 50 | + |
| 51 | + public void show_notification (Meta.WindowActor notification) |
| 52 | + requires (notification != null && !notification.is_destroyed () && !notifications.contains (notification)) { |
| 53 | + |
| 54 | + notification.set_pivot_point (0.5f, 0.5f); |
| 55 | + |
| 56 | + unowned var window = notification.get_meta_window (); |
| 57 | + if (window == null) { |
| 58 | + warning ("NotificationStack: Unable to show notification, window is null"); |
| 59 | + return; |
| 60 | + } |
| 61 | + |
| 62 | + var window_rect = window.get_frame_rect (); |
| 63 | + window.stick (); |
| 64 | + |
| 65 | + if (Meta.Prefs.get_gnome_animations ()) { |
| 66 | + // Don't flicker at the beginning of the animation |
| 67 | + notification.opacity = 0; |
| 68 | + notification.rotation_angle_x = 90; |
| 69 | + |
| 70 | + var opacity_transition = new Clutter.PropertyTransition ("opacity"); |
| 71 | + opacity_transition.set_from_value (0); |
| 72 | + opacity_transition.set_to_value (255); |
| 73 | + |
| 74 | + var flip_transition = new Clutter.KeyframeTransition ("rotation-angle-x"); |
| 75 | + flip_transition.set_from_value (90.0); |
| 76 | + flip_transition.set_to_value (0.0); |
| 77 | + flip_transition.set_key_frames ({ 0.6 }); |
| 78 | + flip_transition.set_values ({ -10.0 }); |
| 79 | + |
| 80 | + var entry = new Clutter.TransitionGroup () { |
| 81 | + duration = 400 |
| 82 | + }; |
| 83 | + entry.add_transition (opacity_transition); |
| 84 | + entry.add_transition (flip_transition); |
| 85 | + |
| 86 | + notification.transitions_completed.connect (() => notification.remove_all_transitions ()); |
| 87 | + notification.add_transition (TRANSITION_ENTRY_NAME, entry); |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * We will make space for the incoming notification |
| 92 | + * by shifting all current notifications by height |
| 93 | + * and then add it to the notifications list. |
| 94 | + */ |
| 95 | + update_positions (window_rect.height); |
| 96 | + |
| 97 | + var primary = display.get_primary_monitor (); |
| 98 | + var area = display.get_workspace_manager ().get_active_workspace ().get_work_area_for_monitor (primary); |
| 99 | + var scale = display.get_monitor_scale (primary); |
| 100 | + |
| 101 | + int notification_x_pos = area.x + area.width - window_rect.width; |
| 102 | + if (Clutter.get_default_text_direction () == Clutter.TextDirection.RTL) { |
| 103 | + notification_x_pos = 0; |
| 104 | + } |
| 105 | + |
| 106 | + move_window (notification, notification_x_pos, stack_y + TOP_OFFSET + Utils.scale_to_int (ADDITIONAL_MARGIN, scale)); |
| 107 | + notifications.insert (0, notification); |
| 108 | + } |
| 109 | + |
| 110 | + private void update_stack_allocation () { |
| 111 | + var primary = display.get_primary_monitor (); |
| 112 | + var area = display.get_workspace_manager ().get_active_workspace ().get_work_area_for_monitor (primary); |
| 113 | + |
| 114 | + var scale = display.get_monitor_scale (primary); |
| 115 | + stack_width = Utils.scale_to_int (WIDTH + MARGIN, scale); |
| 116 | + |
| 117 | + stack_y = area.y; |
| 118 | + |
| 119 | + update_positions (); |
| 120 | + } |
| 121 | + |
| 122 | + private void update_positions (float add_y = 0.0f) { |
| 123 | + var scale = display.get_monitor_scale (display.get_primary_monitor ()); |
| 124 | + |
| 125 | + var y = stack_y + TOP_OFFSET + add_y + Utils.scale_to_int (ADDITIONAL_MARGIN, scale); |
| 126 | + var i = notifications.size; |
| 127 | + var delay_step = i > 0 ? 150 / i : 0; |
| 128 | + var iterator = 0; |
| 129 | + // Need to iterate like this since we might be removing entries |
| 130 | + while (notifications.size > iterator) { |
| 131 | + unowned var actor = notifications.get (iterator); |
| 132 | + iterator++; |
| 133 | + if (actor == null || actor.is_destroyed ()) { |
| 134 | + warning ("NotificationStack: Notification actor was null or destroyed"); |
| 135 | + continue; |
| 136 | + } |
| 137 | + |
| 138 | + if (Meta.Prefs.get_gnome_animations ()) { |
| 139 | + actor.save_easing_state (); |
| 140 | + actor.set_easing_mode (Clutter.AnimationMode.EASE_OUT_BACK); |
| 141 | + actor.set_easing_duration (200); |
| 142 | + actor.set_easing_delay ((i--) * delay_step); |
| 143 | + } |
| 144 | + |
| 145 | + move_window (actor, -1, (int)y); |
| 146 | + |
| 147 | + if (Meta.Prefs.get_gnome_animations ()) { |
| 148 | + actor.restore_easing_state (); |
| 149 | + } |
| 150 | + |
| 151 | + unowned var window = actor.get_meta_window (); |
| 152 | + if (window == null) { |
| 153 | + // Mutter doesn't let us know when a window is closed if a workspace |
| 154 | + // transition is in progress. I'm not really sure why, but what this |
| 155 | + // means is that we have to remove the notification from the stack |
| 156 | + // manually. |
| 157 | + // See https://github.com/GNOME/mutter/blob/3.36.9/src/compositor/meta-window-actor.c#L882 |
| 158 | + notifications.remove (actor); |
| 159 | + warning ("NotificationStack: Notification window was null (probably removed during workspace transition?)"); |
| 160 | + continue; |
| 161 | + } |
| 162 | + |
| 163 | + y += window.get_frame_rect ().height; |
| 164 | + } |
| 165 | + } |
| 166 | + |
| 167 | + public void destroy_notification (Meta.WindowActor notification) { |
| 168 | + notification.save_easing_state (); |
| 169 | + notification.set_easing_duration (Utils.get_animation_duration (CLOSE_ANIMATION_DURATION)); |
| 170 | + notification.set_easing_mode (Clutter.AnimationMode.EASE_IN_QUAD); |
| 171 | + notification.opacity = 0; |
| 172 | + |
| 173 | + notification.x += stack_width; |
| 174 | + notification.restore_easing_state (); |
| 175 | + |
| 176 | + notifications.remove (notification); |
| 177 | + update_positions (); |
| 178 | + } |
| 179 | + |
| 180 | + /** |
| 181 | + * This function takes care of properly updating both the actor |
| 182 | + * position and the actual window position. |
| 183 | + * |
| 184 | + * To enable animations for a window we first need to move it's frame |
| 185 | + * in the compositor and then calculate & apply the coordinates for the window |
| 186 | + * actor. |
| 187 | + */ |
| 188 | + private static void move_window (Meta.WindowActor actor, int x, int y) requires (actor != null && !actor.is_destroyed ()) { |
| 189 | + unowned var window = actor.get_meta_window (); |
| 190 | + if (window == null) { |
| 191 | + warning ("NotificationStack: Unable to move the window, window is null"); |
| 192 | + return; |
| 193 | + } |
| 194 | + |
| 195 | + var rect = window.get_frame_rect (); |
| 196 | + |
| 197 | + window.move_frame (false, x != -1 ? x : rect.x, y != -1 ? y : rect.y); |
| 198 | + |
| 199 | + /** |
| 200 | + * move_frame does not guarantee that the frame rectangle |
| 201 | + * will be updated instantly, get the buffer rectangle. |
| 202 | + */ |
| 203 | + rect = window.get_buffer_rect (); |
| 204 | + actor.set_position (rect.x - ((actor.width - rect.width) / 2), rect.y - ((actor.height - rect.height) / 2)); |
| 205 | + } |
| 206 | + |
| 207 | + public static bool is_notification (Meta.Window window) { |
| 208 | + return window.window_type == NOTIFICATION || window.get_data (NOTIFICATION_DATA_KEY); |
| 209 | + } |
| 210 | +} |
0 commit comments