2013-03-02 09:48:25 +00:00
|
|
|
// -*- mode: js; js-indent-level: 4; indent-tabs-mode: nil -*-
|
|
|
|
|
|
|
|
const Clutter = imports.gi.Clutter;
|
2013-11-29 00:45:39 +00:00
|
|
|
const GLib = imports.gi.GLib;
|
2013-03-02 09:48:25 +00: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 18:37:58 +00:00
|
|
|
const Meta = imports.gi.Meta;
|
2013-03-02 09:48:25 +00:00
|
|
|
|
|
|
|
const HIDE_TIMEOUT = 1500;
|
|
|
|
const FADE_TIME = 0.1;
|
|
|
|
const LEVEL_ANIMATION_TIME = 0.1;
|
|
|
|
|
|
|
|
const LevelBar = new Lang.Class({
|
|
|
|
Name: 'LevelBar',
|
|
|
|
|
|
|
|
_init: function() {
|
|
|
|
this._level = 0;
|
|
|
|
|
|
|
|
this.actor = new St.Bin({ style_class: 'level',
|
|
|
|
x_fill: true, y_fill: true });
|
|
|
|
this._bar = new St.DrawingArea();
|
|
|
|
this._bar.connect('repaint', Lang.bind(this, this._repaint));
|
|
|
|
|
|
|
|
this.actor.set_child(this._bar);
|
|
|
|
},
|
|
|
|
|
|
|
|
get level() {
|
|
|
|
return this._level;
|
|
|
|
},
|
|
|
|
|
|
|
|
set level(value) {
|
|
|
|
let newValue = Math.max(0, Math.min(value, 100));
|
|
|
|
if (newValue == this._level)
|
|
|
|
return;
|
|
|
|
this._level = newValue;
|
|
|
|
this._bar.queue_repaint();
|
|
|
|
},
|
|
|
|
|
|
|
|
_repaint: function() {
|
|
|
|
let cr = this._bar.get_context();
|
|
|
|
|
|
|
|
let node = this.actor.get_theme_node();
|
|
|
|
let radius = node.get_border_radius(0); // assume same radius for all corners
|
|
|
|
Clutter.cairo_set_source_color(cr, node.get_foreground_color());
|
|
|
|
|
|
|
|
let [w, h] = this._bar.get_surface_size();
|
|
|
|
w *= (this._level / 100.);
|
|
|
|
|
|
|
|
if (w == 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
cr.moveTo(radius, 0);
|
|
|
|
if (w >= radius)
|
|
|
|
cr.arc(w - radius, radius, radius, 1.5 * Math.PI, 2. * Math.PI);
|
|
|
|
else
|
|
|
|
cr.lineTo(w, 0);
|
|
|
|
if (w >= radius)
|
|
|
|
cr.arc(w - radius, h - radius, radius, 0, 0.5 * Math.PI);
|
|
|
|
else
|
|
|
|
cr.lineTo(w, h);
|
|
|
|
cr.arc(radius, h - radius, radius, 0.5 * Math.PI, Math.PI);
|
|
|
|
cr.arc(radius, radius, radius, Math.PI, 1.5 * Math.PI);
|
|
|
|
cr.fill();
|
2014-01-23 03:10:21 +00:00
|
|
|
cr.$dispose();
|
2013-03-02 09:48:25 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
const OsdWindow = new Lang.Class({
|
|
|
|
Name: 'OsdWindow',
|
|
|
|
|
2014-04-17 08:23:30 +00:00
|
|
|
_init: function(monitorIndex) {
|
2013-05-07 18:37:58 +00:00
|
|
|
this._popupSize = 0;
|
2013-03-02 09:48:25 +00: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 08:23:30 +00:00
|
|
|
|
|
|
|
this._monitorIndex = monitorIndex;
|
|
|
|
let constraint = new Layout.MonitorConstraint({ index: monitorIndex });
|
|
|
|
this.actor.add_constraint(constraint);
|
|
|
|
|
2013-03-02 09:48:25 +00:00
|
|
|
this._box = new St.BoxLayout({ style_class: 'osd-window',
|
|
|
|
vertical: true });
|
|
|
|
this.actor.add_actor(this._box);
|
|
|
|
|
2013-05-07 18:37:58 +00:00
|
|
|
this._box.connect('style-changed', Lang.bind(this, this._onStyleChanged));
|
|
|
|
this._box.connect('notify::height', Lang.bind(this,
|
|
|
|
function() {
|
|
|
|
Meta.later_add(Meta.LaterType.BEFORE_REDRAW, Lang.bind(this,
|
|
|
|
function() {
|
|
|
|
this._box.width = this._box.height;
|
|
|
|
}));
|
|
|
|
}));
|
|
|
|
|
2013-03-02 09:48:25 +00:00
|
|
|
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',
|
|
|
|
Lang.bind(this, this._monitorsChanged));
|
|
|
|
this._monitorsChanged();
|
2013-05-19 00:59:46 +00:00
|
|
|
Main.uiGroup.add_child(this.actor);
|
2013-03-02 09:48:25 +00: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);
|
2013-04-14 15:32:21 +00:00
|
|
|
if (level) {
|
|
|
|
if (this.actor.visible)
|
|
|
|
Tweener.addTween(this._level,
|
|
|
|
{ level: level,
|
|
|
|
time: LEVEL_ANIMATION_TIME,
|
|
|
|
transition: 'easeOutQuad' });
|
|
|
|
else
|
|
|
|
this._level.level = level;
|
|
|
|
}
|
2013-03-02 09:48:25 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
show: function() {
|
|
|
|
if (!this._icon.gicon)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (!this.actor.visible) {
|
2013-05-30 08:43:23 +00:00
|
|
|
Meta.disable_unredirect_for_screen(global.screen);
|
2013-03-02 09:48:25 +00:00
|
|
|
this.actor.show();
|
|
|
|
this.actor.opacity = 0;
|
2013-05-30 12:29:31 +00:00
|
|
|
this.actor.get_parent().set_child_above_sibling(this.actor, null);
|
2013-03-02 09:48:25 +00: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 17:26:52 +00:00
|
|
|
GLib.Source.set_name_by_id(this._hideTimeoutId, '[gnome-shell] this._hide');
|
2013-03-02 09:48:25 +00:00
|
|
|
},
|
|
|
|
|
2013-03-02 18:56:37 +00:00
|
|
|
cancel: function() {
|
|
|
|
if (!this._hideTimeoutId)
|
|
|
|
return;
|
|
|
|
|
|
|
|
Mainloop.source_remove(this._hideTimeoutId);
|
|
|
|
this._hide();
|
|
|
|
},
|
|
|
|
|
2013-03-02 09:48:25 +00:00
|
|
|
_hide: function() {
|
2013-06-24 21:15:33 +00:00
|
|
|
this._hideTimeoutId = 0;
|
2013-03-02 09:48:25 +00:00
|
|
|
Tweener.addTween(this.actor,
|
|
|
|
{ opacity: 0,
|
|
|
|
time: FADE_TIME,
|
|
|
|
transition: 'easeOutQuad',
|
2013-06-18 20:43:25 +00:00
|
|
|
onComplete: Lang.bind(this, function() {
|
|
|
|
this._reset();
|
|
|
|
Meta.enable_unredirect_for_screen(global.screen);
|
2013-06-18 20:59:48 +00:00
|
|
|
})
|
|
|
|
});
|
2013-11-29 00:45:39 +00:00
|
|
|
return GLib.SOURCE_REMOVE;
|
2013-03-02 09:48:25 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
_reset: function() {
|
|
|
|
this.actor.hide();
|
|
|
|
this.setLabel(null);
|
|
|
|
this.setLevel(null);
|
|
|
|
},
|
|
|
|
|
|
|
|
_monitorsChanged: function() {
|
2013-03-07 21:45:50 +00:00
|
|
|
/* assume 110x110 on a 640x480 display and scale from there */
|
2014-04-17 08:23:30 +00:00
|
|
|
let monitor = Main.layoutManager.monitors[this._monitorIndex];
|
2013-11-18 12:48:19 +00:00
|
|
|
|
2013-03-02 09:48:25 +00:00
|
|
|
let scalew = monitor.width / 640.0;
|
|
|
|
let scaleh = monitor.height / 480.0;
|
|
|
|
let scale = Math.min(scalew, scaleh);
|
2013-05-07 18:37:58 +00:00
|
|
|
this._popupSize = 110 * Math.max(1, scale);
|
2013-03-02 09:48:25 +00:00
|
|
|
|
2014-02-18 04:55:15 +00:00
|
|
|
let scaleFactor = St.ThemeContext.get_for_stage(global.stage).scale_factor;
|
|
|
|
this._icon.icon_size = this._popupSize / (2 * scaleFactor);
|
2013-03-02 09:48:25 +00:00
|
|
|
this._box.translation_y = monitor.height / 4;
|
2013-05-07 18:37:58 +00:00
|
|
|
this._box.style_changed();
|
|
|
|
},
|
|
|
|
|
|
|
|
_onStyleChanged: function() {
|
|
|
|
let themeNode = this._box.get_theme_node();
|
|
|
|
let horizontalPadding = themeNode.get_horizontal_padding();
|
|
|
|
let verticalPadding = themeNode.get_vertical_padding();
|
|
|
|
let topBorder = themeNode.get_border_width(St.Side.TOP);
|
|
|
|
let bottomBorder = themeNode.get_border_width(St.Side.BOTTOM);
|
|
|
|
let leftBorder = themeNode.get_border_width(St.Side.LEFT);
|
|
|
|
let rightBorder = themeNode.get_border_width(St.Side.RIGHT);
|
|
|
|
|
|
|
|
let minWidth = this._popupSize - verticalPadding - leftBorder - rightBorder;
|
|
|
|
let minHeight = this._popupSize - horizontalPadding - topBorder - bottomBorder;
|
2013-03-02 09:48:25 +00:00
|
|
|
|
2014-02-18 04:55:15 +00:00
|
|
|
// minWidth/minHeight here are in real pixels,
|
|
|
|
// but the theme takes measures in unscaled dimensions
|
|
|
|
let scaleFactor = St.ThemeContext.get_for_stage(global.stage).scale_factor;
|
|
|
|
this._box.style = 'min-height: %dpx;'.format(Math.max(minWidth, minHeight) / scaleFactor);
|
2014-04-17 08:23:30 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
const OsdWindowManager = new Lang.Class({
|
|
|
|
Name: 'OsdWindowManager',
|
|
|
|
|
|
|
|
_init: function() {
|
|
|
|
this._osdWindows = [];
|
|
|
|
Main.layoutManager.connect('monitors-changed',
|
|
|
|
Lang.bind(this, this._monitorsChanged));
|
|
|
|
this._monitorsChanged();
|
2013-11-18 12:48:19 +00:00
|
|
|
},
|
|
|
|
|
2014-04-17 08:23:30 +00: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 12:48:19 +00:00
|
|
|
|
2014-04-17 08:23:30 +00: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 12:48:19 +00:00
|
|
|
|
2014-04-17 08:23:30 +00:00
|
|
|
this._osdWindows.length = Main.layoutManager.monitors.length;
|
|
|
|
},
|
2013-11-18 12:48:19 +00:00
|
|
|
|
2014-04-17 08:23:30 +00: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 09:48:25 +00:00
|
|
|
}
|
|
|
|
});
|