|
| 1 | +// -*- mode: js; js-indent-level: 4; indent-tabs-mode: nil -*- |
| 2 | + |
| 3 | +const { Clutter, St } = imports.gi; |
| 4 | + |
| 5 | +// Shamelessly copied from the layout "hotcorner" ripples implementation |
| 6 | +var Ripples = class Ripples { |
| 7 | + constructor(px, py, styleClass) { |
| 8 | + this._x = 0; |
| 9 | + this._y = 0; |
| 10 | + |
| 11 | + this._px = px; |
| 12 | + this._py = py; |
| 13 | + |
| 14 | + this._ripple1 = new St.BoxLayout({ |
| 15 | + style_class: styleClass, |
| 16 | + important: true, |
| 17 | + opacity: 0, |
| 18 | + can_focus: false, |
| 19 | + reactive: false, |
| 20 | + visible: false, |
| 21 | + }); |
| 22 | + this._ripple1.set_pivot_point(px, py); |
| 23 | + |
| 24 | + this._ripple2 = new St.BoxLayout({ |
| 25 | + style_class: styleClass, |
| 26 | + important: true, |
| 27 | + opacity: 0, |
| 28 | + can_focus: false, |
| 29 | + reactive: false, |
| 30 | + visible: false, |
| 31 | + }); |
| 32 | + this._ripple2.set_pivot_point(px, py); |
| 33 | + |
| 34 | + this._ripple3 = new St.BoxLayout({ |
| 35 | + style_class: styleClass, |
| 36 | + important: true, |
| 37 | + opacity: 0, |
| 38 | + can_focus: false, |
| 39 | + reactive: false, |
| 40 | + visible: false, |
| 41 | + }); |
| 42 | + this._ripple3.set_pivot_point(px, py); |
| 43 | + } |
| 44 | + |
| 45 | + destroy() { |
| 46 | + this._ripple1.destroy(); |
| 47 | + this._ripple2.destroy(); |
| 48 | + this._ripple3.destroy(); |
| 49 | + } |
| 50 | + |
| 51 | + _animRipple(ripple, delay, duration, startScale, startOpacity, finalScale) { |
| 52 | + // We draw a ripple by using a source image and animating it scaling |
| 53 | + // outwards and fading away. We want the ripples to move linearly |
| 54 | + // or it looks unrealistic, but if the opacity of the ripple goes |
| 55 | + // linearly to zero it fades away too quickly, so we use a separate |
| 56 | + // tween to give a non-linear curve to the fade-away and make |
| 57 | + // it more visible in the middle section. |
| 58 | + |
| 59 | + ripple.x = this._x; |
| 60 | + ripple.y = this._y; |
| 61 | + ripple.visible = true; |
| 62 | + ripple.opacity = 255 * Math.sqrt(startOpacity); |
| 63 | + ripple.scale_x = ripple.scale_y = startScale; |
| 64 | + ripple.set_translation(-this._px * ripple.width, -this._py * ripple.height, 0.0); |
| 65 | + |
| 66 | + ripple.ease({ |
| 67 | + opacity: 0, |
| 68 | + delay, |
| 69 | + duration, |
| 70 | + mode: Clutter.AnimationMode.EASE_IN_QUAD, |
| 71 | + }); |
| 72 | + ripple.ease({ |
| 73 | + scale_x: finalScale, |
| 74 | + scale_y: finalScale, |
| 75 | + delay, |
| 76 | + duration, |
| 77 | + mode: Clutter.AnimationMode.LINEAR, |
| 78 | + onComplete: () => (ripple.visible = false), |
| 79 | + }); |
| 80 | + } |
| 81 | + |
| 82 | + addTo(stage) { |
| 83 | + if (this._stage !== undefined) |
| 84 | + throw new Error('Ripples already added'); |
| 85 | + |
| 86 | + this._stage = stage; |
| 87 | + this._stage.add_actor(this._ripple1); |
| 88 | + this._stage.add_actor(this._ripple2); |
| 89 | + this._stage.add_actor(this._ripple3); |
| 90 | + } |
| 91 | + |
| 92 | + playAnimation(x, y) { |
| 93 | + if (this._stage === undefined) |
| 94 | + throw new Error('Ripples not added'); |
| 95 | + |
| 96 | + this._x = x; |
| 97 | + this._y = y; |
| 98 | + |
| 99 | + this._stage.set_child_above_sibling(this._ripple1, null); |
| 100 | + this._stage.set_child_above_sibling(this._ripple2, this._ripple1); |
| 101 | + this._stage.set_child_above_sibling(this._ripple3, this._ripple2); |
| 102 | + |
| 103 | + // Show three concentric ripples expanding outwards; the exact |
| 104 | + // parameters were found by trial and error, so don't look |
| 105 | + // for them to make perfect sense mathematically |
| 106 | + |
| 107 | + // delay time scale opacity => scale |
| 108 | + this._animRipple(this._ripple1, 0, 830, 0.25, 1.0, 1.5); |
| 109 | + this._animRipple(this._ripple2, 50, 1000, 0.0, 0.7, 1.25); |
| 110 | + this._animRipple(this._ripple3, 350, 1000, 0.0, 0.3, 1); |
| 111 | + } |
| 112 | +}; |
0 commit comments