2013-03-02 04:48:25 -05:00
|
|
|
// -*- mode: js; js-indent-level: 4; indent-tabs-mode: nil -*-
|
|
|
|
|
|
|
|
const Clutter = imports.gi.Clutter;
|
2013-11-28 19:45:39 -05:00
|
|
|
const GLib = imports.gi.GLib;
|
2013-03-02 04:48:25 -05:00
|
|
|
const St = imports.gi.St;
|
|
|
|
|
|
|
|
const Lang = imports.lang;
|
|
|
|
const Layout = imports.ui.layout;
|
|
|
|
const Main = imports.ui.main;
|
|
|
|
const Mainloop = imports.mainloop;
|
|
|
|
const Tweener = imports.ui.tweener;
|
2013-05-07 14:37:58 -04:00
|
|
|
const Meta = imports.gi.Meta;
|
2013-03-02 04:48:25 -05:00
|
|
|
|
2017-07-18 13:47:27 -04:00
|
|
|
var HIDE_TIMEOUT = 1500;
|
|
|
|
var FADE_TIME = 0.1;
|
|
|
|
var LEVEL_ANIMATION_TIME = 0.1;
|
2013-03-02 04:48:25 -05:00
|
|
|
|
2017-07-18 13:41:25 -04:00
|
|
|
var LevelBar = new Lang.Class({
|
2013-03-02 04:48:25 -05:00
|
|
|
Name: 'LevelBar',
|
|
|
|
|
|
|
|
_init: function() {
|
|
|
|
this._level = 0;
|
|
|
|
|
|
|
|
this.actor = new St.Bin({ style_class: 'level',
|
2015-11-21 16:06:08 -05:00
|
|
|
x_align: St.Align.START,
|
|
|
|
y_fill: true });
|
|
|
|
this._bar = new St.Widget({ style_class: 'level-bar' });
|
2013-03-02 04:48:25 -05:00
|
|
|
|
|
|
|
this.actor.set_child(this._bar);
|
2016-06-03 19:29:07 -04:00
|
|
|
|
|
|
|
this.actor.connect('notify::width', () => { this.level = this.level; });
|
2013-03-02 04:48:25 -05:00
|
|
|
},
|
|
|
|
|
|
|
|
get level() {
|
|
|
|
return this._level;
|
|
|
|
},
|
|
|
|
|
|
|
|
set level(value) {
|
2016-06-03 19:29:07 -04:00
|
|
|
this._level = Math.max(0, Math.min(value, 100));
|
|
|
|
|
|
|
|
let alloc = this.actor.get_allocation_box();
|
2016-07-01 12:54:03 -04:00
|
|
|
let newWidth = Math.round((alloc.x2 - alloc.x1) * this._level / 100);
|
2016-06-03 19:29:07 -04:00
|
|
|
if (newWidth != this._bar.width)
|
|
|
|
this._bar.width = newWidth;
|
2013-03-02 04:48:25 -05:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2017-07-18 13:41:25 -04:00
|
|
|
var OsdWindowConstraint = new Lang.Class({
|
2016-06-21 13:46:44 -04:00
|
|
|
Name: 'OsdWindowConstraint',
|
|
|
|
Extends: Clutter.Constraint,
|
|
|
|
|
|
|
|
_init: function(props) {
|
|
|
|
this._minSize = 0;
|
|
|
|
this.parent(props);
|
|
|
|
},
|
|
|
|
|
|
|
|
set minSize(v) {
|
|
|
|
this._minSize = v;
|
|
|
|
if (this.actor)
|
|
|
|
this.actor.queue_relayout();
|
|
|
|
},
|
|
|
|
|
|
|
|
vfunc_update_allocation: function(actor, actorBox) {
|
|
|
|
// Clutter will adjust the allocation for margins,
|
|
|
|
// so add it to our minimum size
|
|
|
|
let minSize = this._minSize + actor.margin_top + actor.margin_bottom;
|
|
|
|
let [width, height] = actorBox.get_size();
|
|
|
|
|
|
|
|
// Enforce a ratio of 1
|
|
|
|
let size = Math.ceil(Math.max(minSize, height));
|
|
|
|
actorBox.set_size(size, size);
|
|
|
|
|
|
|
|
// Recenter
|
|
|
|
let [x, y] = actorBox.get_origin();
|
2016-10-10 19:59:31 -04:00
|
|
|
actorBox.set_origin(Math.ceil(x + width / 2 - size / 2),
|
|
|
|
Math.ceil(y + height / 2 - size / 2));
|
2016-06-21 13:46:44 -04:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2017-07-18 13:41:25 -04:00
|
|
|
var OsdWindow = new Lang.Class({
|
2013-03-02 04:48:25 -05:00
|
|
|
Name: 'OsdWindow',
|
|
|
|
|
2014-04-17 04:23:30 -04:00
|
|
|
_init: function(monitorIndex) {
|
2013-03-02 04:48:25 -05:00
|
|
|
this.actor = new St.Widget({ x_expand: true,
|
|
|
|
y_expand: true,
|
|
|
|
x_align: Clutter.ActorAlign.CENTER,
|
|
|
|
y_align: Clutter.ActorAlign.CENTER });
|
2014-04-17 04:23:30 -04:00
|
|
|
|
|
|
|
this._monitorIndex = monitorIndex;
|
|
|
|
let constraint = new Layout.MonitorConstraint({ index: monitorIndex });
|
|
|
|
this.actor.add_constraint(constraint);
|
|
|
|
|
2016-06-21 13:46:44 -04:00
|
|
|
this._boxConstraint = new OsdWindowConstraint();
|
2013-03-02 04:48:25 -05:00
|
|
|
this._box = new St.BoxLayout({ style_class: 'osd-window',
|
|
|
|
vertical: true });
|
2016-06-21 13:46:44 -04:00
|
|
|
this._box.add_constraint(this._boxConstraint);
|
2013-03-02 04:48:25 -05:00
|
|
|
this.actor.add_actor(this._box);
|
|
|
|
|
|
|
|
this._icon = new St.Icon();
|
|
|
|
this._box.add(this._icon, { expand: true });
|
|
|
|
|
|
|
|
this._label = new St.Label();
|
|
|
|
this._box.add(this._label);
|
|
|
|
|
|
|
|
this._level = new LevelBar();
|
|
|
|
this._box.add(this._level.actor);
|
|
|
|
|
|
|
|
this._hideTimeoutId = 0;
|
|
|
|
this._reset();
|
|
|
|
|
|
|
|
Main.layoutManager.connect('monitors-changed',
|
2016-10-10 19:56:35 -04:00
|
|
|
Lang.bind(this, this._relayout));
|
|
|
|
let themeContext = St.ThemeContext.get_for_stage(global.stage);
|
|
|
|
themeContext.connect('notify::scale-factor',
|
|
|
|
Lang.bind(this, this._relayout));
|
|
|
|
this._relayout();
|
2013-05-18 20:59:46 -04:00
|
|
|
Main.uiGroup.add_child(this.actor);
|
2013-03-02 04:48:25 -05:00
|
|
|
},
|
|
|
|
|
|
|
|
setIcon: function(icon) {
|
|
|
|
this._icon.gicon = icon;
|
|
|
|
},
|
|
|
|
|
|
|
|
setLabel: function(label) {
|
|
|
|
this._label.visible = (label != undefined);
|
|
|
|
if (label)
|
|
|
|
this._label.text = label;
|
|
|
|
},
|
|
|
|
|
|
|
|
setLevel: function(level) {
|
|
|
|
this._level.actor.visible = (level != undefined);
|
2014-04-25 09:14:22 -04:00
|
|
|
if (level != undefined) {
|
2013-04-14 11:32:21 -04:00
|
|
|
if (this.actor.visible)
|
|
|
|
Tweener.addTween(this._level,
|
|
|
|
{ level: level,
|
|
|
|
time: LEVEL_ANIMATION_TIME,
|
|
|
|
transition: 'easeOutQuad' });
|
|
|
|
else
|
|
|
|
this._level.level = level;
|
|
|
|
}
|
2013-03-02 04:48:25 -05:00
|
|
|
},
|
|
|
|
|
|
|
|
show: function() {
|
|
|
|
if (!this._icon.gicon)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (!this.actor.visible) {
|
2013-05-30 04:43:23 -04:00
|
|
|
Meta.disable_unredirect_for_screen(global.screen);
|
2013-03-02 04:48:25 -05:00
|
|
|
this.actor.show();
|
|
|
|
this.actor.opacity = 0;
|
2013-05-30 08:29:31 -04:00
|
|
|
this.actor.get_parent().set_child_above_sibling(this.actor, null);
|
2013-03-02 04:48:25 -05:00
|
|
|
|
|
|
|
Tweener.addTween(this.actor,
|
|
|
|
{ opacity: 255,
|
|
|
|
time: FADE_TIME,
|
|
|
|
transition: 'easeOutQuad' });
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this._hideTimeoutId)
|
|
|
|
Mainloop.source_remove(this._hideTimeoutId);
|
|
|
|
this._hideTimeoutId = Mainloop.timeout_add(HIDE_TIMEOUT,
|
|
|
|
Lang.bind(this, this._hide));
|
2014-04-10 13:26:52 -04:00
|
|
|
GLib.Source.set_name_by_id(this._hideTimeoutId, '[gnome-shell] this._hide');
|
2013-03-02 04:48:25 -05:00
|
|
|
},
|
|
|
|
|
2013-03-02 13:56:37 -05:00
|
|
|
cancel: function() {
|
|
|
|
if (!this._hideTimeoutId)
|
|
|
|
return;
|
|
|
|
|
|
|
|
Mainloop.source_remove(this._hideTimeoutId);
|
|
|
|
this._hide();
|
|
|
|
},
|
|
|
|
|
2013-03-02 04:48:25 -05:00
|
|
|
_hide: function() {
|
2013-06-24 17:15:33 -04:00
|
|
|
this._hideTimeoutId = 0;
|
2013-03-02 04:48:25 -05:00
|
|
|
Tweener.addTween(this.actor,
|
|
|
|
{ opacity: 0,
|
|
|
|
time: FADE_TIME,
|
|
|
|
transition: 'easeOutQuad',
|
2013-06-18 16:43:25 -04:00
|
|
|
onComplete: Lang.bind(this, function() {
|
|
|
|
this._reset();
|
|
|
|
Meta.enable_unredirect_for_screen(global.screen);
|
2013-06-18 16:59:48 -04:00
|
|
|
})
|
|
|
|
});
|
2013-11-28 19:45:39 -05:00
|
|
|
return GLib.SOURCE_REMOVE;
|
2013-03-02 04:48:25 -05:00
|
|
|
},
|
|
|
|
|
|
|
|
_reset: function() {
|
|
|
|
this.actor.hide();
|
|
|
|
this.setLabel(null);
|
|
|
|
this.setLevel(null);
|
|
|
|
},
|
|
|
|
|
2016-10-10 19:56:35 -04:00
|
|
|
_relayout: function() {
|
2013-03-07 16:45:50 -05:00
|
|
|
/* assume 110x110 on a 640x480 display and scale from there */
|
2014-04-17 04:23:30 -04:00
|
|
|
let monitor = Main.layoutManager.monitors[this._monitorIndex];
|
2014-05-03 15:12:29 -04:00
|
|
|
if (!monitor)
|
|
|
|
return; // we are about to be removed
|
2013-11-18 07:48:19 -05:00
|
|
|
|
2013-03-02 04:48:25 -05:00
|
|
|
let scalew = monitor.width / 640.0;
|
|
|
|
let scaleh = monitor.height / 480.0;
|
|
|
|
let scale = Math.min(scalew, scaleh);
|
2016-06-21 13:46:44 -04:00
|
|
|
let popupSize = 110 * Math.max(1, scale);
|
2013-03-02 04:48:25 -05:00
|
|
|
|
2014-02-17 23:55:15 -05:00
|
|
|
let scaleFactor = St.ThemeContext.get_for_stage(global.stage).scale_factor;
|
2016-06-21 13:46:44 -04:00
|
|
|
this._icon.icon_size = popupSize / (2 * scaleFactor);
|
2013-03-02 04:48:25 -05:00
|
|
|
this._box.translation_y = monitor.height / 4;
|
2016-06-21 13:46:44 -04:00
|
|
|
this._boxConstraint.minSize = popupSize;
|
2014-04-17 04:23:30 -04:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2017-07-18 13:41:25 -04:00
|
|
|
var OsdWindowManager = new Lang.Class({
|
2014-04-17 04:23:30 -04:00
|
|
|
Name: 'OsdWindowManager',
|
|
|
|
|
|
|
|
_init: function() {
|
|
|
|
this._osdWindows = [];
|
|
|
|
Main.layoutManager.connect('monitors-changed',
|
|
|
|
Lang.bind(this, this._monitorsChanged));
|
|
|
|
this._monitorsChanged();
|
2013-11-18 07:48:19 -05:00
|
|
|
},
|
|
|
|
|
2014-04-17 04:23:30 -04:00
|
|
|
_monitorsChanged: function() {
|
|
|
|
for (let i = 0; i < Main.layoutManager.monitors.length; i++) {
|
|
|
|
if (this._osdWindows[i] == undefined)
|
|
|
|
this._osdWindows[i] = new OsdWindow(i);
|
|
|
|
}
|
2013-11-18 07:48:19 -05:00
|
|
|
|
2014-04-17 04:23:30 -04:00
|
|
|
for (let i = Main.layoutManager.monitors.length; i < this._osdWindows.length; i++) {
|
|
|
|
this._osdWindows[i].actor.destroy();
|
|
|
|
this._osdWindows[i] = null;
|
|
|
|
}
|
2013-11-18 07:48:19 -05:00
|
|
|
|
2014-04-17 04:23:30 -04:00
|
|
|
this._osdWindows.length = Main.layoutManager.monitors.length;
|
|
|
|
},
|
2013-11-18 07:48:19 -05:00
|
|
|
|
2014-04-17 04:23:30 -04:00
|
|
|
_showOsdWindow: function(monitorIndex, icon, label, level) {
|
|
|
|
this._osdWindows[monitorIndex].setIcon(icon);
|
|
|
|
this._osdWindows[monitorIndex].setLabel(label);
|
|
|
|
this._osdWindows[monitorIndex].setLevel(level);
|
|
|
|
this._osdWindows[monitorIndex].show();
|
|
|
|
},
|
|
|
|
|
|
|
|
show: function(monitorIndex, icon, label, level) {
|
|
|
|
if (monitorIndex != -1) {
|
|
|
|
for (let i = 0; i < this._osdWindows.length; i++) {
|
|
|
|
if (i == monitorIndex)
|
|
|
|
this._showOsdWindow(i, icon, label, level);
|
|
|
|
else
|
|
|
|
this._osdWindows[i].cancel();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for (let i = 0; i < this._osdWindows.length; i++)
|
|
|
|
this._showOsdWindow(i, icon, label, level);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
hideAll: function() {
|
|
|
|
for (let i = 0; i < this._osdWindows.length; i++)
|
|
|
|
this._osdWindows[i].cancel();
|
2013-03-02 04:48:25 -05:00
|
|
|
}
|
|
|
|
});
|