-
Notifications
You must be signed in to change notification settings - Fork 45
/
rounded_corners_manager.ts
565 lines (491 loc) · 17.6 KB
/
rounded_corners_manager.ts
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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
// imports.gi
import * as Clutter from '@gi/Clutter'
import * as GLib from '@gi/GLib'
import { ShadowMode, WindowType } from '@gi/Meta'
import { WindowClientType } from '@gi/Meta'
import { Bin } from '@gi/St'
import { BindingFlags } from '@gi/GObject'
import { ThemeContext } from '@gi/St'
// local modules
import * as UI from '@me/utils/ui'
import { _log } from '@me/utils/log'
import { constants } from '@me/utils/constants'
import { ClipShadowEffect } from '@me/effect/clip_shadow_effect'
import * as types from '@me/utils/types'
import { settings } from '@me/utils/settings'
import { RoundedCornersEffect } from '@me/effect/rounded_corners_effect'
// types, those import statements will be removed in output javascript files.
import { SchemasKeys } from '../utils/settings'
import { Window, WindowActor } from '@gi/Meta'
import { global } from '@global'
import { EffectManager } from '@me/utils/types'
import { ExtensionsWindowActor } from '@me/utils/types'
type RoundedCornersEffectType = InstanceType<typeof RoundedCornersEffect>
// --------------------------------------------------------------- [end imports]
export class RoundedCornersManager implements EffectManager {
enabled = true
/** Rounded corners settings */
private global_rounded_corners = settings ().global_rounded_corner_settings
private custom_rounded_corners = settings ().custom_rounded_corner_settings
// ---------------------------------------------------------- [public methods]
on_add_effect (actor: ExtensionsWindowActor): void {
_log ('opened: ' + actor?.meta_window.title + ': ' + actor)
const win = actor.meta_window
// If application failed check, then just return.
if (!this._should_enable_effect (win)) {
return
}
// Add rounded corners shader to window
this._actor_to_rounded (actor)?.add_effect_with_name (
constants.ROUNDED_CORNERS_EFFECT,
new RoundedCornersEffect ()
)
// Turn off original shadow for ssd x11 window.
// - For ssd client in X11, shadow is drew by window manager
// - For csd client, shadow is drew by application itself, it has been cut
// out by rounded corners effect
if (actor.shadow_mode !== undefined) {
actor.shadow_mode = ShadowMode.FORCED_OFF
}
// So we have to create an shadow actor for rounded corners shadows
const shadow = this._create_shadow (actor)
// Bind properties between shadow and window
const flag = BindingFlags.SYNC_CREATE
for (const prop of [
'pivot-point',
'translation-x',
'translation-y',
'scale-x',
'scale-y',
]) {
actor.bind_property (prop, shadow, prop, flag)
}
// Store visible binding so that we can control the visible of shadow
// in some time.
const prop = 'visible'
const visible_binding = actor.bind_property (prop, shadow, prop, flag)
// Store shadow, app type, visible binding, so that we can query them later
actor.__rwc_rounded_window_info = {
shadow,
visible_binding,
unminimized_timeout_id: 0,
}
}
on_remove_effect (actor: ExtensionsWindowActor): void {
// Remove rounded corners effect
const name = constants.ROUNDED_CORNERS_EFFECT
this._actor_to_rounded (actor)?.remove_effect_by_name (name)
// Restore shadow for x11 windows
if (actor.shadow_mode) {
actor.shadow_mode = ShadowMode.AUTO
}
// Remove shadow actor
const shadow = actor.__rwc_rounded_window_info?.shadow
if (shadow) {
global.window_group.remove_child (shadow)
shadow.clear_effects ()
shadow.destroy ()
}
// Remove all timeout handler
const timeout_id = actor.__rwc_rounded_window_info?.unminimized_timeout_id
if (timeout_id) GLib.source_remove (timeout_id)
delete actor.__rwc_rounded_window_info
}
on_minimize (actor: ExtensionsWindowActor): void {
const info = actor.__rwc_rounded_window_info
const binding = info?.visible_binding
const shadow = info?.shadow
if (shadow && binding) {
binding.unbind ()
shadow.visible = false
}
}
on_unminimize (actor: ExtensionsWindowActor): void {
this._restore_shadow (actor)
// Requeue layout after 300ms
if (actor.first_child && actor.__rwc_rounded_window_info) {
const info = actor.__rwc_rounded_window_info
// Clear prev handler
let id = info.unminimized_timeout_id
if (id) GLib.source_remove (id)
id = GLib.timeout_add (GLib.PRIORITY_DEFAULT, 300, () => {
actor.first_child.queue_relayout ()
return false
})
// update handler, it will be clear when window is closed
info.unminimized_timeout_id = id
}
}
on_switch_workspace (actor: types.ExtensionsWindowActor) {
this._restore_shadow (actor)
}
on_restacked (actor: ExtensionsWindowActor): void {
// When windows restacked, change order of shadow actor too
if (!actor.visible) {
return
}
const shadow = actor.__rwc_rounded_window_info?.shadow
if (shadow) {
global.window_group.set_child_below_sibling (shadow, actor)
}
}
on_size_changed (actor: ExtensionsWindowActor): void {
const win = actor.meta_window
const window_info = actor.__rwc_rounded_window_info
// Get rounded corners effect from window actor
const effect = this._actor_to_rounded (actor)?.get_effect (
constants.ROUNDED_CORNERS_EFFECT
) as RoundedCornersEffectType | null
if (!effect || !window_info) {
return
}
// Skip rounded corners when window is fullscreen & maximize
const cfg = this._get_rounded_corners_cfg (win)
const should_rounded = UI.ShouldHasRoundedCorners (win, cfg)
if (!should_rounded && effect.enabled) {
_log ('Disable rounded corners effect for maximized window', win.title)
effect.enabled = false
this.on_focus_changed (actor)
return
}
// Restore Rounded effect when un-maximized
if (should_rounded && !effect.enabled) {
_log ('Restore rounded effect for maximized window', win.title)
effect.enabled = true
this.on_focus_changed (actor)
}
// Cache the offset, so that we can calculate this value once
const content_offset_of_win = UI.computeWindowContentsOffset (win)
// When size changed. update uniforms for window
effect.update_uniforms (
UI.WindowScaleFactor (win),
cfg,
this._compute_bounds (actor, content_offset_of_win),
{
width: settings ().border_width,
color: settings ().border_color,
}
)
// Update BindConstraint for shadow
const shadow = window_info.shadow
const offsets = this._compute_shadow_actor_offset (
actor,
content_offset_of_win
)
const constraints = shadow.get_constraints ()
constraints.forEach ((constraint, i) => {
if (constraint instanceof Clutter.BindConstraint) {
constraint.offset = offsets[i]
}
})
}
on_focus_changed (actor: ExtensionsWindowActor): void {
const win = actor.meta_window
const shadow = actor.__rwc_rounded_window_info?.shadow
if (!shadow) {
return
}
const shadow_settings = win.appears_focused
? settings ().focused_shadow
: settings ().unfocused_shadow
const { border_radius, padding } = this._get_rounded_corners_cfg (win)
this._update_shadow_actor_style (
win,
shadow,
border_radius,
shadow_settings,
padding
)
}
on_settings_changed (key: SchemasKeys): void {
switch (key) {
case 'skip-libadwaita-app':
case 'skip-libhandy-app':
case 'black-list':
this._update_all_window_effect_state ()
break
case 'focused-shadow':
case 'unfocused-shadow':
this._update_all_shadow_actor_style ()
break
case 'global-rounded-corner-settings':
case 'custom-rounded-corner-settings':
case 'border-color':
case 'border-width':
case 'tweak-kitty-terminal':
this._update_all_rounded_corners_settings ()
break
default:
}
}
// --------------------------------------------------------- [private methods]
private _restore_shadow (actor: ExtensionsWindowActor) {
const info = actor.__rwc_rounded_window_info
if (!info) {
return
}
const prop = 'visible'
const flag = BindingFlags.SYNC_CREATE
info.visible_binding = actor.bind_property (prop, info.shadow, prop, flag)
}
/**
* Check whether a window should be enable rounded corners effect
* @param win Meta.Window to test
*/
private _should_enable_effect (
win: Window & { __app_type?: UI.AppType }
): boolean {
// DING (Desktop Icons NG) is a extensions that create a gtk
// application to show desktop grid on background, we need to
// skip it coercively.
// https://extensions.gnome.org/extension/2087/desktop-icons-ng-ding/
if (win.gtk_application_id === 'com.rastersoft.ding') {
return false
}
// Skip applications in black list.
const wm_class_instance = win.get_wm_class_instance ()
if (wm_class_instance == null) {
_log (`Warning: wm_class_instance of ${win}: ${win.title} is null`)
return false
}
if (settings ().black_list.includes (wm_class_instance)) {
return false
}
// Check type of window, only need to add rounded corners to normal
// window and dialog.
const normal_type = [
WindowType.NORMAL,
WindowType.DIALOG,
WindowType.MODAL_DIALOG,
].includes (win.window_type)
if (!normal_type) {
return false
}
// Skip libhandy / libadwaita applications according the settings.
const { AppType, getAppType } = UI
const app_type = win.__app_type ?? getAppType (win)
win.__app_type = app_type // cache result
_log ('Check Type of window:' + `${win.title} => ${AppType[app_type]}`)
if (settings ().skip_libadwaita_app && app_type === AppType.LibAdwaita) {
return false
}
if (settings ().skip_libhandy_app && app_type === AppType.LibHandy) {
return false
}
return true
}
/**
* return Clutter.Actor that should be add rounded corners,
* In Wayland, we will add rounded corners effect to WindowActor
* In XOrg, we will add rounded corners effect to WindowActor.first_child
*/
private _actor_to_rounded (actor: WindowActor): Clutter.Actor | null {
const type = actor.meta_window.get_client_type ()
return type == WindowClientType.X11 ? actor.get_first_child () : actor
}
/**
* Create Shadow for rounded corners window
* @param actor - window actor which has been setup rounded corners effect
*/
private _create_shadow (actor: WindowActor): Bin {
const shadow = new Bin ({
name: 'Shadow Actor',
child: new Bin ({
x_expand: true,
y_expand: true,
}),
})
;(shadow.first_child as Bin).add_style_class_name ('shadow')
this._update_shadow_actor_style (
actor.meta_window,
shadow,
this.global_rounded_corners?.border_radius,
actor.meta_window.appears_focused
? settings ().focused_shadow
: settings ().unfocused_shadow
)
// We have to clip the shadow because of this issues:
// https://gitlab.gnome.org/GNOME/gnome-shell/-/issues/4474
shadow.add_effect_with_name (
constants.CLIP_SHADOW_EFFECT,
new ClipShadowEffect ()
)
// Insert shadow actor below window actor, now shadow actor
// will show below window actor
global.window_group.insert_child_below (shadow, actor)
// Bind position and size between window and shadow
for (let i = 0; i < 4; i++) {
const constraint = new Clutter.BindConstraint ({
source: actor,
coordinate: i,
offset: 0,
})
shadow.add_constraint (constraint)
}
// Return the shadow we create, it will be store into
// this.rounded_windows
return shadow
}
/** Compute outer bound of rounded corners for window actor */
private _compute_bounds (
actor: WindowActor,
[x, y, width, height]: [number, number, number, number]
): types.Bounds {
const bounds = {
x1: x + 1,
y1: y + 1,
x2: x + actor.width + width,
y2: y + actor.height + height,
}
// Kitty draw it's window decoration by itself, we need recompute the
// outer bounds for kitty.
if (settings ().tweak_kitty_terminal) {
const type = WindowClientType.WAYLAND
if (
actor.meta_window.get_client_type () == type &&
actor.meta_window.get_wm_class_instance () === 'kitty'
) {
const scale = UI.WindowScaleFactor (actor.meta_window)
bounds.x1 += 11 * scale /* shadow in left of kitty */
bounds.y1 += 35 * scale /* shadow in top of kitty */
bounds.x2 -= 11 * scale /* shadow in right of kitty */
bounds.y2 -= 11 * scale /* shadow in bottom of kitty */
}
}
return bounds
}
private _compute_shadow_actor_offset (
actor: WindowActor,
[offset_x, offset_y, offset_width, offset_height]: [
number,
number,
number,
number
]
): number[] {
const win = actor.meta_window
const shadow_padding = constants.SHADOW_PADDING * UI.WindowScaleFactor (win)
// If remove UI.scaleFactor(), it should can be works if
// experimental-features of mutter 'scale-monitor-framebuffer' enabled
// (Fractional scaling in Wayland)
// const shadow_padding = constants.SHADOW_PADDING * UI.scaleFactor ()
return [
offset_x - shadow_padding,
offset_y - shadow_padding,
2 * shadow_padding + offset_width,
2 * shadow_padding + offset_height,
]
}
/** Update css style of shadow actor */
private _update_shadow_actor_style (
win: Window,
actor: Bin,
border_radius = this.global_rounded_corners?.border_radius,
shadow = settings ().focused_shadow,
padding = this.global_rounded_corners?.padding
) {
if (!border_radius || !padding) {
return
}
const { left, right, top, bottom } = padding
// Increasing border_radius when smoothing is on
if (this.global_rounded_corners !== null) {
border_radius *= 1.0 + this.global_rounded_corners.smoothing
}
// Sadly, the scale of style of St.Widget may be different between scale
// of window if there are two monitor with different scale factor.
// - Scale of Style always as same as primary monitor
// - Scale of window as same as the monitor window located.
//
// So, we have to adjustment this different
const original_scale = ThemeContext.get_for_stage (
global.stage as Clutter.Stage
).scale_factor
const win_scale = UI.WindowScaleFactor (win)
// Now scale factor for shadow actor should be correct.
const scale_of_style = win_scale / original_scale
// _log (JSON.stringify ({ original_scale, win_scale }))
actor.style = `padding: ${constants.SHADOW_PADDING * scale_of_style}px
/*background: yellow*/;`
const child = actor.first_child as Bin
if (
win.maximized_horizontally ||
win.maximized_vertically ||
win.fullscreen
) {
child.style = 'opacity: 0;'
} else {
child.style = `
background: white;
border-radius: ${border_radius * scale_of_style}px;
${types.box_shadow_css (shadow, scale_of_style)};
margin: ${top * scale_of_style}px
${right * scale_of_style}px
${bottom * scale_of_style}px
${left * scale_of_style}px;`
}
child.queue_redraw ()
}
/** Traversal all windows, add or remove rounded corners for them */
private _update_all_window_effect_state () {
global.get_window_actors ().forEach ((actor) => {
const should_have_effect = this._should_enable_effect (actor.meta_window)
const has_effect = UI.get_rounded_corners_effect (actor) != null
if (should_have_effect && !has_effect) {
this.on_add_effect (actor)
this.on_size_changed (actor)
return
}
if (!should_have_effect && has_effect) {
this.on_remove_effect (actor)
return
}
})
}
/** Update style for all shadow actors */
private _update_all_shadow_actor_style () {
for (const actor of global.get_window_actors ()) {
const info = (actor as ExtensionsWindowActor).__rwc_rounded_window_info
if (!info) {
continue
}
const { shadow } = info
const win = actor.meta_window
const shadow_cfg = actor.meta_window.appears_focused
? settings ().focused_shadow
: settings ().unfocused_shadow
const { border_radius, padding } = this._get_rounded_corners_cfg (
actor.meta_window
)
this._update_shadow_actor_style (
win,
shadow,
border_radius,
shadow_cfg,
padding
)
}
}
private _get_rounded_corners_cfg (win: Window): types.RoundedCornersCfg {
return UI.ChoiceRoundedCornersCfg (
this.global_rounded_corners ?? settings ().global_rounded_corner_settings,
this.custom_rounded_corners ?? settings ().custom_rounded_corner_settings,
win
)
}
/**
* This method will be called when global rounded corners settings changed.
*/
private _update_all_rounded_corners_settings () {
this.global_rounded_corners = settings ().global_rounded_corner_settings
this.custom_rounded_corners = settings ().custom_rounded_corner_settings
for (const actor of global.get_window_actors ()) {
const info = (actor as ExtensionsWindowActor).__rwc_rounded_window_info
if (!info) {
continue
}
this.on_size_changed (actor)
}
this._update_all_shadow_actor_style ()
}
}