2011-09-28 13:16:26 +00:00
|
|
|
// -*- mode: js; js-indent-level: 4; indent-tabs-mode: nil -*-
|
2019-01-31 14:07:06 +00:00
|
|
|
/* exported Lightbox */
|
2009-09-22 19:24:14 +00:00
|
|
|
|
2019-02-09 03:21:36 +00:00
|
|
|
const { Clutter, GObject, Shell, St } = imports.gi;
|
2009-09-22 19:24:14 +00:00
|
|
|
|
2010-03-17 14:36:57 +00:00
|
|
|
const Params = imports.misc.params;
|
|
|
|
|
2017-07-18 17:47:27 +00:00
|
|
|
var DEFAULT_FADE_FACTOR = 0.4;
|
2020-01-19 17:01:57 +00:00
|
|
|
var VIGNETTE_BRIGHTNESS = 0.5;
|
2017-07-18 17:47:27 +00:00
|
|
|
var VIGNETTE_SHARPNESS = 0.7;
|
2014-03-06 15:17:36 +00:00
|
|
|
|
|
|
|
const VIGNETTE_DECLARATIONS = '\
|
|
|
|
uniform float brightness;\n\
|
|
|
|
uniform float vignette_sharpness;\n';
|
|
|
|
|
|
|
|
const VIGNETTE_CODE = '\
|
|
|
|
cogl_color_out.a = cogl_color_in.a;\n\
|
|
|
|
cogl_color_out.rgb = vec3(0.0, 0.0, 0.0);\n\
|
|
|
|
vec2 position = cogl_tex_coord_in[0].xy - 0.5;\n\
|
|
|
|
float t = length(2.0 * position);\n\
|
|
|
|
t = clamp(t, 0.0, 1.0);\n\
|
|
|
|
float pixel_brightness = mix(1.0, 1.0 - vignette_sharpness, t);\n\
|
|
|
|
cogl_color_out.a = cogl_color_out.a * (1 - pixel_brightness * brightness);';
|
2012-12-15 01:41:03 +00:00
|
|
|
|
2019-07-25 22:13:19 +00:00
|
|
|
var RadialShaderEffect = GObject.registerClass({
|
|
|
|
Properties: {
|
|
|
|
'brightness': GObject.ParamSpec.float(
|
|
|
|
'brightness', 'brightness', 'brightness',
|
|
|
|
GObject.ParamFlags.READWRITE,
|
2020-04-03 23:52:29 +00:00
|
|
|
0, 1, 1),
|
2019-07-25 22:13:19 +00:00
|
|
|
'sharpness': GObject.ParamSpec.float(
|
|
|
|
'sharpness', 'sharpness', 'sharpness',
|
|
|
|
GObject.ParamFlags.READWRITE,
|
2020-04-03 23:52:29 +00:00
|
|
|
0, 1, 0),
|
2019-08-20 21:43:54 +00:00
|
|
|
},
|
2019-07-25 22:13:19 +00:00
|
|
|
}, class RadialShaderEffect extends Shell.GLSLEffect {
|
2017-10-31 00:03:21 +00:00
|
|
|
_init(params) {
|
2021-02-24 01:28:09 +00:00
|
|
|
this._brightness = undefined;
|
|
|
|
this._sharpness = undefined;
|
|
|
|
|
2017-10-31 01:23:39 +00:00
|
|
|
super._init(params);
|
2014-03-06 15:17:36 +00:00
|
|
|
|
|
|
|
this._brightnessLocation = this.get_uniform_location('brightness');
|
|
|
|
this._sharpnessLocation = this.get_uniform_location('vignette_sharpness');
|
|
|
|
|
2021-02-24 01:28:09 +00:00
|
|
|
this.brightness = 1.0;
|
|
|
|
this.sharpness = 0.0;
|
2017-10-31 01:23:39 +00:00
|
|
|
}
|
2014-03-06 15:17:36 +00:00
|
|
|
|
2017-10-31 00:03:21 +00:00
|
|
|
vfunc_build_pipeline() {
|
2012-12-15 01:41:03 +00:00
|
|
|
this.add_glsl_snippet(Shell.SnippetHook.FRAGMENT,
|
2014-03-06 15:17:36 +00:00
|
|
|
VIGNETTE_DECLARATIONS, VIGNETTE_CODE, true);
|
2017-10-31 01:23:39 +00:00
|
|
|
}
|
2014-03-06 15:17:36 +00:00
|
|
|
|
2021-02-24 01:28:09 +00:00
|
|
|
get brightness() {
|
|
|
|
return this._brightness;
|
|
|
|
}
|
|
|
|
|
|
|
|
set brightness(v) {
|
|
|
|
if (this._brightness === v)
|
|
|
|
return;
|
|
|
|
this._brightness = v;
|
2014-03-06 15:17:36 +00:00
|
|
|
this.set_uniform_float(this._brightnessLocation,
|
2021-02-24 01:28:09 +00:00
|
|
|
1, [this._brightness]);
|
|
|
|
this.notify('brightness');
|
|
|
|
}
|
|
|
|
|
|
|
|
get sharpness() {
|
|
|
|
return this._sharpness;
|
2017-10-31 01:23:39 +00:00
|
|
|
}
|
2014-03-06 15:17:36 +00:00
|
|
|
|
2021-02-24 01:28:09 +00:00
|
|
|
set sharpness(v) {
|
|
|
|
if (this._sharpness === v)
|
|
|
|
return;
|
|
|
|
this._sharpness = v;
|
2014-03-06 15:17:36 +00:00
|
|
|
this.set_uniform_float(this._sharpnessLocation,
|
2021-02-24 01:28:09 +00:00
|
|
|
1, [this._sharpness]);
|
|
|
|
this.notify('sharpness');
|
2014-03-06 15:17:36 +00:00
|
|
|
}
|
2012-12-15 01:41:03 +00:00
|
|
|
});
|
|
|
|
|
2009-09-22 19:24:14 +00:00
|
|
|
/**
|
|
|
|
* Lightbox:
|
|
|
|
* @container: parent Clutter.Container
|
2010-03-17 14:36:57 +00:00
|
|
|
* @params: (optional) additional parameters:
|
|
|
|
* - inhibitEvents: whether to inhibit events for @container
|
|
|
|
* - width: shade actor width
|
|
|
|
* - height: shade actor height
|
2019-07-16 09:24:13 +00:00
|
|
|
* - fadeFactor: fading opacity factor
|
|
|
|
* - radialEffect: whether to enable the GLSL radial effect
|
2009-09-22 19:24:14 +00:00
|
|
|
*
|
|
|
|
* Lightbox creates a dark translucent "shade" actor to hide the
|
|
|
|
* contents of @container, and allows you to specify particular actors
|
|
|
|
* in @container to highlight by bringing them above the shade. It
|
|
|
|
* tracks added and removed actors in @container while the lightboxing
|
|
|
|
* is active, and ensures that all actors are returned to their
|
|
|
|
* original stacking order when the lightboxing is removed. (However,
|
|
|
|
* if actors are restacked by outside code while the lightboxing is
|
|
|
|
* active, the lightbox may later revert them back to their original
|
|
|
|
* order.)
|
|
|
|
*
|
|
|
|
* By default, the shade window will have the height and width of
|
|
|
|
* @container and will track any changes in its size. You can override
|
2010-03-17 14:36:57 +00:00
|
|
|
* this by passing an explicit width and height in @params.
|
2009-09-22 19:24:14 +00:00
|
|
|
*/
|
2019-07-16 09:24:13 +00:00
|
|
|
var Lightbox = GObject.registerClass({
|
|
|
|
Properties: {
|
|
|
|
'active': GObject.ParamSpec.boolean(
|
|
|
|
'active', 'active', 'active', GObject.ParamFlags.READABLE, false),
|
2019-08-20 21:43:54 +00:00
|
|
|
},
|
2019-07-16 09:24:13 +00:00
|
|
|
}, class Lightbox extends St.Bin {
|
|
|
|
_init(container, params) {
|
2019-02-12 14:02:09 +00:00
|
|
|
params = Params.parse(params, {
|
|
|
|
inhibitEvents: false,
|
|
|
|
width: null,
|
|
|
|
height: null,
|
|
|
|
fadeFactor: DEFAULT_FADE_FACTOR,
|
|
|
|
radialEffect: false,
|
|
|
|
});
|
2010-03-17 14:36:57 +00:00
|
|
|
|
2019-07-16 09:24:13 +00:00
|
|
|
super._init({
|
|
|
|
reactive: params.inhibitEvents,
|
|
|
|
width: params.width,
|
|
|
|
height: params.height,
|
2019-08-20 21:43:54 +00:00
|
|
|
visible: false,
|
2019-07-16 09:24:13 +00:00
|
|
|
});
|
|
|
|
|
2019-08-28 20:06:14 +00:00
|
|
|
this._active = false;
|
2009-09-22 19:24:14 +00:00
|
|
|
this._container = container;
|
|
|
|
this._children = container.get_children();
|
2012-02-06 22:28:48 +00:00
|
|
|
this._fadeFactor = params.fadeFactor;
|
2014-07-24 13:40:19 +00:00
|
|
|
this._radialEffect = Clutter.feature_available(Clutter.FeatureFlags.SHADERS_GLSL) && params.radialEffect;
|
2019-07-26 20:08:52 +00:00
|
|
|
|
2014-07-24 13:40:19 +00:00
|
|
|
if (this._radialEffect)
|
2019-07-16 09:24:13 +00:00
|
|
|
this.add_effect(new RadialShaderEffect({ name: 'radial' }));
|
2012-12-15 01:41:03 +00:00
|
|
|
else
|
2019-07-16 09:24:13 +00:00
|
|
|
this.set({ opacity: 0, style_class: 'lightbox' });
|
2009-09-22 19:24:14 +00:00
|
|
|
|
2019-07-16 09:24:13 +00:00
|
|
|
container.add_actor(this);
|
2019-11-05 19:17:19 +00:00
|
|
|
container.set_child_above_sibling(this, null);
|
2009-09-22 19:24:14 +00:00
|
|
|
|
2019-07-16 09:24:13 +00:00
|
|
|
this.connect('destroy', this._onDestroy.bind(this));
|
2009-09-22 19:24:14 +00:00
|
|
|
|
2019-07-16 09:24:13 +00:00
|
|
|
if (!params.width || !params.height) {
|
|
|
|
this.add_constraint(new Clutter.BindConstraint({
|
|
|
|
source: container,
|
2019-08-20 21:43:54 +00:00
|
|
|
coordinate: Clutter.BindCoordinate.ALL,
|
2019-07-16 09:24:13 +00:00
|
|
|
}));
|
2009-09-22 19:24:14 +00:00
|
|
|
}
|
|
|
|
|
2017-12-02 00:27:35 +00:00
|
|
|
this._actorAddedSignalId = container.connect('actor-added', this._actorAdded.bind(this));
|
|
|
|
this._actorRemovedSignalId = container.connect('actor-removed', this._actorRemoved.bind(this));
|
2009-09-22 19:24:14 +00:00
|
|
|
|
|
|
|
this._highlighted = null;
|
2017-10-31 01:19:44 +00:00
|
|
|
}
|
2009-09-22 19:24:14 +00:00
|
|
|
|
2019-08-28 20:06:14 +00:00
|
|
|
get active() {
|
|
|
|
return this._active;
|
|
|
|
}
|
|
|
|
|
2017-10-31 00:03:21 +00:00
|
|
|
_actorAdded(container, newChild) {
|
2009-09-22 19:24:14 +00:00
|
|
|
let children = this._container.get_children();
|
2019-07-16 09:24:13 +00:00
|
|
|
let myIndex = children.indexOf(this);
|
2009-09-22 19:24:14 +00:00
|
|
|
let newChildIndex = children.indexOf(newChild);
|
|
|
|
|
2009-09-22 21:48:44 +00:00
|
|
|
if (newChildIndex > myIndex) {
|
2009-09-22 19:24:14 +00:00
|
|
|
// The child was added above the shade (presumably it was
|
|
|
|
// made the new top-most child). Move it below the shade,
|
|
|
|
// and add it to this._children as the new topmost actor.
|
2019-07-16 09:24:13 +00:00
|
|
|
this._container.set_child_above_sibling(this, newChild);
|
2009-09-22 19:24:14 +00:00
|
|
|
this._children.push(newChild);
|
2009-09-22 21:48:44 +00:00
|
|
|
} else if (newChildIndex == 0) {
|
|
|
|
// Bottom of stack
|
|
|
|
this._children.unshift(newChild);
|
2009-09-22 19:24:14 +00:00
|
|
|
} else {
|
|
|
|
// Somewhere else; insert it into the correct spot
|
2009-09-22 21:48:44 +00:00
|
|
|
let prevChild = this._children.indexOf(children[newChildIndex - 1]);
|
|
|
|
if (prevChild != -1) // paranoia
|
|
|
|
this._children.splice(prevChild + 1, 0, newChild);
|
2009-09-22 19:24:14 +00:00
|
|
|
}
|
2017-10-31 01:19:44 +00:00
|
|
|
}
|
2009-09-22 19:24:14 +00:00
|
|
|
|
2019-08-28 20:06:14 +00:00
|
|
|
lightOn(fadeInTime) {
|
2019-07-16 09:24:13 +00:00
|
|
|
this.remove_all_transitions();
|
2019-07-25 00:06:05 +00:00
|
|
|
|
2019-07-31 12:10:31 +00:00
|
|
|
let easeProps = {
|
|
|
|
duration: fadeInTime || 0,
|
2019-08-20 21:43:54 +00:00
|
|
|
mode: Clutter.AnimationMode.EASE_OUT_QUAD,
|
2019-07-31 12:10:31 +00:00
|
|
|
};
|
|
|
|
|
2019-07-25 00:06:05 +00:00
|
|
|
let onComplete = () => {
|
2019-08-28 20:06:14 +00:00
|
|
|
this._active = true;
|
2019-07-16 09:24:13 +00:00
|
|
|
this.notify('active');
|
2019-07-25 00:06:05 +00:00
|
|
|
};
|
|
|
|
|
2019-07-16 09:24:13 +00:00
|
|
|
this.show();
|
2019-10-03 13:13:21 +00:00
|
|
|
|
2014-03-06 15:17:36 +00:00
|
|
|
if (this._radialEffect) {
|
2019-07-16 09:24:13 +00:00
|
|
|
this.ease_property(
|
2019-07-31 12:10:31 +00:00
|
|
|
'@effects.radial.brightness', VIGNETTE_BRIGHTNESS, easeProps);
|
2019-07-16 09:24:13 +00:00
|
|
|
this.ease_property(
|
2019-07-31 12:10:31 +00:00
|
|
|
'@effects.radial.sharpness', VIGNETTE_SHARPNESS,
|
|
|
|
Object.assign({ onComplete }, easeProps));
|
2010-03-17 14:36:57 +00:00
|
|
|
} else {
|
2019-07-16 09:24:13 +00:00
|
|
|
this.ease(Object.assign(easeProps, {
|
2018-07-20 19:46:19 +00:00
|
|
|
opacity: 255 * this._fadeFactor,
|
2019-08-20 21:43:54 +00:00
|
|
|
onComplete,
|
2019-07-31 12:10:31 +00:00
|
|
|
}));
|
2010-03-17 14:36:57 +00:00
|
|
|
}
|
2017-10-31 01:19:44 +00:00
|
|
|
}
|
2010-03-17 14:36:57 +00:00
|
|
|
|
2019-08-28 20:06:14 +00:00
|
|
|
lightOff(fadeOutTime) {
|
2019-07-16 09:24:13 +00:00
|
|
|
this.remove_all_transitions();
|
2019-07-25 00:06:05 +00:00
|
|
|
|
2019-08-28 20:06:14 +00:00
|
|
|
this._active = false;
|
2019-07-16 09:24:13 +00:00
|
|
|
this.notify('active');
|
2019-08-28 20:06:14 +00:00
|
|
|
|
2019-07-31 12:10:31 +00:00
|
|
|
let easeProps = {
|
|
|
|
duration: fadeOutTime || 0,
|
2019-08-20 21:43:54 +00:00
|
|
|
mode: Clutter.AnimationMode.EASE_OUT_QUAD,
|
2019-07-31 12:10:31 +00:00
|
|
|
};
|
|
|
|
|
2019-07-16 09:24:13 +00:00
|
|
|
let onComplete = () => this.hide();
|
2019-07-31 12:54:56 +00:00
|
|
|
|
2014-03-06 15:17:36 +00:00
|
|
|
if (this._radialEffect) {
|
2019-07-16 09:24:13 +00:00
|
|
|
this.ease_property(
|
2019-07-31 12:10:31 +00:00
|
|
|
'@effects.radial.brightness', 1.0, easeProps);
|
2019-07-16 09:24:13 +00:00
|
|
|
this.ease_property(
|
2019-07-31 12:10:31 +00:00
|
|
|
'@effects.radial.sharpness', 0.0, Object.assign({ onComplete }, easeProps));
|
2010-03-17 14:36:57 +00:00
|
|
|
} else {
|
2019-07-16 09:24:13 +00:00
|
|
|
this.ease(Object.assign(easeProps, { opacity: 0, onComplete }));
|
2010-03-17 14:36:57 +00:00
|
|
|
}
|
2017-10-31 01:19:44 +00:00
|
|
|
}
|
2010-03-17 14:36:57 +00:00
|
|
|
|
2017-10-31 00:03:21 +00:00
|
|
|
_actorRemoved(container, child) {
|
2009-09-22 19:24:14 +00:00
|
|
|
let index = this._children.indexOf(child);
|
|
|
|
if (index != -1) // paranoia
|
|
|
|
this._children.splice(index, 1);
|
|
|
|
|
|
|
|
if (child == this._highlighted)
|
|
|
|
this._highlighted = null;
|
2017-10-31 01:19:44 +00:00
|
|
|
}
|
2009-09-22 19:24:14 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* highlight:
|
2019-10-17 16:41:52 +00:00
|
|
|
* @param {Clutter.Actor=} window: actor to highlight
|
2009-09-22 19:24:14 +00:00
|
|
|
*
|
|
|
|
* Highlights the indicated actor and unhighlights any other
|
|
|
|
* currently-highlighted actor. With no arguments or a false/null
|
|
|
|
* argument, all actors will be unhighlighted.
|
|
|
|
*/
|
2017-10-31 00:03:21 +00:00
|
|
|
highlight(window) {
|
2009-09-22 19:24:14 +00:00
|
|
|
if (this._highlighted == window)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Walk this._children raising and lowering actors as needed.
|
|
|
|
// Things get a little tricky if the to-be-raised and
|
|
|
|
// to-be-lowered actors were originally adjacent, in which
|
|
|
|
// case we may need to indicate some *other* actor as the new
|
|
|
|
// sibling of the to-be-lowered one.
|
|
|
|
|
2019-07-16 09:24:13 +00:00
|
|
|
let below = this;
|
2009-09-22 21:48:44 +00:00
|
|
|
for (let i = this._children.length - 1; i >= 0; i--) {
|
2009-09-22 19:24:14 +00:00
|
|
|
if (this._children[i] == window)
|
2019-11-05 19:17:19 +00:00
|
|
|
this._container.set_child_above_sibling(this._children[i], null);
|
2009-09-22 19:24:14 +00:00
|
|
|
else if (this._children[i] == this._highlighted)
|
2019-11-05 19:17:19 +00:00
|
|
|
this._container.set_child_below_sibling(this._children[i], below);
|
2009-09-22 19:24:14 +00:00
|
|
|
else
|
|
|
|
below = this._children[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
this._highlighted = window;
|
2017-10-31 01:19:44 +00:00
|
|
|
}
|
2009-09-22 19:24:14 +00:00
|
|
|
|
2010-02-18 15:43:58 +00:00
|
|
|
/**
|
|
|
|
* _onDestroy:
|
|
|
|
*
|
|
|
|
* This is called when the lightbox' actor is destroyed, either
|
|
|
|
* by destroying its container or by explicitly calling this.destroy().
|
|
|
|
*/
|
2017-10-31 00:03:21 +00:00
|
|
|
_onDestroy() {
|
2019-07-16 09:24:13 +00:00
|
|
|
if (this._actorAddedSignalId) {
|
|
|
|
this._container.disconnect(this._actorAddedSignalId);
|
|
|
|
this._actorAddedSignalId = 0;
|
|
|
|
}
|
|
|
|
if (this._actorRemovedSignalId) {
|
|
|
|
this._container.disconnect(this._actorRemovedSignalId);
|
|
|
|
this._actorRemovedSignalId = 0;
|
|
|
|
}
|
2009-09-22 19:24:14 +00:00
|
|
|
|
|
|
|
this.highlight(null);
|
|
|
|
}
|
2019-07-16 09:24:13 +00:00
|
|
|
});
|