-
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathAbstractBubble.vala
More file actions
264 lines (216 loc) · 8.76 KB
/
AbstractBubble.vala
File metadata and controls
264 lines (216 loc) · 8.76 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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
/*
* Copyright 2020-2025 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, write to the
* Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301 USA
*
*/
public enum Notifications.CloseReason {
EXPIRED = 1,
DISMISSED = 2,
/**
* This value is unique for org.freedesktop.Notifications server interface and must not be used elsewhere.
*/
CLOSE_NOTIFICATION_CALL = 3,
UNDEFINED = 4
}
public class Notifications.AbstractBubble : Gtk.Window {
public signal void closed (CloseReason reason) {
close ();
}
public uint32 timeout { get; set; }
protected Gtk.Stack content_area;
private static Settings? transparency_settings;
private Gtk.Revealer close_revealer;
private Gtk.Box draw_area;
private Gtk.Overlay overlay;
private uint timeout_id;
private double current_swipe_progress = 1.0;
private Pantheon.Desktop.Shell? desktop_shell;
private Pantheon.Desktop.Panel? desktop_panel;
static construct {
var transparency_schema = SettingsSchemaSource.get_default ().lookup ("io.elementary.desktop.wingpanel", true);
if (transparency_schema != null && transparency_schema.has_key ("use-transparency")) {
transparency_settings = new Settings ("io.elementary.desktop.wingpanel");
}
}
construct {
content_area = new Gtk.Stack () {
transition_type = Gtk.StackTransitionType.SLIDE_DOWN,
vhomogeneous = false
};
draw_area = new Gtk.Box (HORIZONTAL, 0) {
hexpand = true
};
draw_area.add_css_class ("draw-area");
draw_area.append (content_area);
var close_button = new Gtk.Button.from_icon_name ("window-close-symbolic") {
halign = Gtk.Align.START,
valign = Gtk.Align.START
};
close_button.add_css_class ("close");
close_revealer = new Gtk.Revealer () {
reveal_child = false,
transition_type = Gtk.RevealerTransitionType.CROSSFADE,
halign = Gtk.Align.START,
valign = Gtk.Align.START,
child = close_button,
overflow = VISIBLE
};
overlay = new Gtk.Overlay () {
child = draw_area
};
overlay.add_overlay (close_revealer);
var carousel = new Adw.Carousel () {
hexpand = true
};
carousel.append (new Gtk.Grid ());
carousel.append (overlay);
carousel.scroll_to (overlay, false);
child = carousel;
default_width = 332;
resizable = false;
add_css_class ("notification");
// Prevent stealing focus when an app window is closed
can_focus = false;
set_titlebar (new Gtk.Grid ());
carousel.page_changed.connect (on_page_changed);
close_button.clicked.connect (() => closed (CloseReason.DISMISSED));
var motion_controller = new Gtk.EventControllerMotion ();
motion_controller.enter.connect (pointer_enter);
motion_controller.leave.connect (pointer_leave);
carousel.add_controller (motion_controller);
child.realize.connect (() => {
if (Gdk.Display.get_default () is Gdk.Wayland.Display) {
// We have to wrap in Idle otherwise the Meta.Window of the WaylandSurface in Gala is still null
Idle.add_once (init_wl);
} else {
x11_make_notification ();
x11_update_mutter_hints ();
}
});
carousel.notify["position"].connect (update_swipe_progress);
transparency_settings.changed["use-transparency"].connect (update_transparency);
update_transparency ();
}
private void update_transparency () requires (transparency_settings != null) {
if (transparency_settings.get_boolean ("use-transparency")) {
remove_css_class ("reduce-transparency");
} else {
add_css_class ("reduce-transparency");
}
}
private void on_page_changed (Adw.Carousel carousel, uint index) {
if (carousel.get_nth_page (index) != overlay) {
closed (CloseReason.DISMISSED);
}
}
private void update_swipe_progress (Object obj, ParamSpec pspec) {
var carousel = (Adw.Carousel) obj;
current_swipe_progress = carousel.position;
if (desktop_panel != null) {
int left, right;
get_blur_margins (out left, out right);
desktop_panel.add_blur (left, right, 16, 16, 9);
} else if (Gdk.Display.get_default () is Gdk.X11.Display) {
x11_update_mutter_hints ();
}
}
public new void present () {
if (timeout_id != 0) {
Source.remove (timeout_id);
timeout_id = 0;
}
base.present ();
if (timeout != 0) {
timeout_id = Timeout.add (timeout, timeout_expired);
}
}
private void pointer_enter () {
close_revealer.reveal_child = true;
if (timeout_id != 0) {
Source.remove (timeout_id);
timeout_id = 0;
}
}
private void pointer_leave () {
close_revealer.reveal_child = false;
if (timeout != 0) {
timeout_id = Timeout.add (timeout, timeout_expired);
}
}
private bool timeout_expired () {
closed (CloseReason.EXPIRED);
return Source.REMOVE;
}
private void get_blur_margins (out int left, out int right) {
var width = get_width ();
var distance = (1 - current_swipe_progress) * width;
left = (int) (16 + distance).clamp (0, width);
right = (int) (16 - distance).clamp (0, width);
}
private void x11_update_mutter_hints () {
var display = Gdk.Display.get_default ();
if (display is Gdk.X11.Display) {
unowned var xdisplay = ((Gdk.X11.Display) display).get_xdisplay ();
var window = ((Gdk.X11.Surface) get_surface ()).get_xid ();
var prop = xdisplay.intern_atom ("_MUTTER_HINTS", false);
int left, right;
get_blur_margins (out left, out right);
var value = "blur=%d,%d,16,16,9".printf (left, right);
xdisplay.change_property (window, prop, X.XA_STRING, 8, 0, (uchar[]) value, value.length);
}
}
private void x11_make_notification () {
unowned var display = Gdk.Display.get_default ();
if (display is Gdk.X11.Display) {
unowned var xdisplay = ((Gdk.X11.Display) display).get_xdisplay ();
var window = ((Gdk.X11.Surface) get_surface ()).get_xid ();
var atom = xdisplay.intern_atom ("_NET_WM_WINDOW_TYPE", false);
var notification_atom = xdisplay.intern_atom ("_NET_WM_WINDOW_TYPE_NOTIFICATION", false);
// (X.Atom) 4 is XA_ATOM
// 32 is format
// 0 means replace
xdisplay.change_property (window, atom, (X.Atom) 4, 32, 0, (uchar[]) notification_atom, 1);
}
}
private static Wl.RegistryListener registry_listener;
private void init_wl () {
registry_listener.global = registry_handle_global;
unowned var display = Gdk.Display.get_default ();
if (display is Gdk.Wayland.Display) {
unowned var wl_display = ((Gdk.Wayland.Display) display).get_wl_display ();
var wl_registry = wl_display.get_registry ();
wl_registry.add_listener (
registry_listener,
this
);
if (wl_display.roundtrip () < 0) {
return;
}
}
}
public void registry_handle_global (Wl.Registry wl_registry, uint32 name, string @interface, uint32 version) {
if (@interface == "io_elementary_pantheon_shell_v1") {
desktop_shell = wl_registry.bind<Pantheon.Desktop.Shell> (name, ref Pantheon.Desktop.Shell.iface, uint32.min (version, 1));
unowned var surface = get_surface ();
if (surface is Gdk.Wayland.Surface) {
unowned var wl_surface = ((Gdk.Wayland.Surface) surface).get_wl_surface ();
desktop_panel = desktop_shell.get_panel (wl_surface);
desktop_panel.add_blur (16, 16, 16, 16, 9);
}
}
}
}