2018-02-08 13:04:42 -05:00
|
|
|
/* -*- mode: js2; js2-basic-offset: 4; indent-tabs-mode: nil -*- */
|
2019-01-31 09:07:06 -05:00
|
|
|
/* exported BarLevel */
|
2018-02-08 13:04:42 -05:00
|
|
|
|
2019-07-25 12:53:00 -04:00
|
|
|
const { Atk, Clutter, GObject, St } = imports.gi;
|
|
|
|
|
|
|
|
var BarLevel = GObject.registerClass({
|
|
|
|
Properties: {
|
|
|
|
'value': GObject.ParamSpec.double(
|
|
|
|
'value', 'value', 'value',
|
|
|
|
GObject.ParamFlags.READWRITE,
|
2019-08-07 08:33:08 -04:00
|
|
|
0, 2, 0),
|
2019-07-25 12:53:00 -04:00
|
|
|
'maximum-value': GObject.ParamSpec.double(
|
|
|
|
'maximum-value', 'maximum-value', 'maximum-value',
|
|
|
|
GObject.ParamFlags.READWRITE,
|
|
|
|
1, 2, 1),
|
|
|
|
'overdrive-start': GObject.ParamSpec.double(
|
|
|
|
'overdrive-start', 'overdrive-start', 'overdrive-start',
|
|
|
|
GObject.ParamFlags.READWRITE,
|
2019-08-20 17:43:54 -04:00
|
|
|
1, 2, 1),
|
|
|
|
},
|
2019-07-25 12:53:00 -04:00
|
|
|
}, class BarLevel extends St.DrawingArea {
|
|
|
|
_init(params) {
|
2018-02-09 08:02:21 -05:00
|
|
|
this._maxValue = 1;
|
2019-07-25 12:53:00 -04:00
|
|
|
this._value = 0;
|
2018-07-31 09:49:06 -04:00
|
|
|
this._overdriveStart = 1;
|
2018-02-08 13:04:42 -05:00
|
|
|
this._barLevelWidth = 0;
|
|
|
|
|
2019-07-25 12:53:00 -04:00
|
|
|
let defaultParams = {
|
|
|
|
style_class: 'barlevel',
|
2019-08-20 17:43:54 -04:00
|
|
|
accessible_role: Atk.Role.LEVEL_BAR,
|
2019-07-25 12:53:00 -04:00
|
|
|
};
|
|
|
|
super._init(Object.assign(defaultParams, params));
|
2020-05-09 15:37:18 -04:00
|
|
|
this.connect('notify::allocation', () => {
|
|
|
|
this._barLevelWidth = this.allocation.get_width();
|
2018-02-08 13:04:42 -05:00
|
|
|
});
|
|
|
|
|
2019-07-25 12:53:00 -04:00
|
|
|
this._customAccessible = St.GenericAccessible.new_for_actor(this);
|
|
|
|
this.set_accessible(this._customAccessible);
|
2018-02-08 13:04:42 -05:00
|
|
|
|
|
|
|
this._customAccessible.connect('get-current-value', this._getCurrentValue.bind(this));
|
|
|
|
this._customAccessible.connect('get-minimum-value', this._getMinimumValue.bind(this));
|
|
|
|
this._customAccessible.connect('get-maximum-value', this._getMaximumValue.bind(this));
|
|
|
|
this._customAccessible.connect('set-current-value', this._setCurrentValue.bind(this));
|
|
|
|
|
2019-07-25 12:53:00 -04:00
|
|
|
this.connect('notify::value', this._valueChanged.bind(this));
|
2017-10-30 21:19:44 -04:00
|
|
|
}
|
2018-02-08 13:04:42 -05:00
|
|
|
|
2019-07-25 11:33:00 -04:00
|
|
|
get value() {
|
|
|
|
return this._value;
|
|
|
|
}
|
|
|
|
|
|
|
|
set value(value) {
|
2019-02-02 12:01:56 -05:00
|
|
|
value = Math.max(Math.min(value, this._maxValue), 0);
|
|
|
|
|
|
|
|
if (this._value == value)
|
|
|
|
return;
|
|
|
|
|
2019-07-25 12:53:00 -04:00
|
|
|
this._value = value;
|
|
|
|
this.notify('value');
|
|
|
|
this.queue_repaint();
|
2017-10-30 21:19:44 -04:00
|
|
|
}
|
2018-02-09 08:02:21 -05:00
|
|
|
|
2019-07-25 11:33:00 -04:00
|
|
|
// eslint-disable-next-line camelcase
|
|
|
|
get maximum_value() {
|
|
|
|
return this._maxValue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// eslint-disable-next-line camelcase
|
|
|
|
set maximum_value(value) {
|
2019-02-02 12:01:56 -05:00
|
|
|
value = Math.max(value, 1);
|
|
|
|
|
|
|
|
if (this._maxValue == value)
|
|
|
|
return;
|
|
|
|
|
|
|
|
this._maxValue = value;
|
2018-07-31 09:49:06 -04:00
|
|
|
this._overdriveStart = Math.min(this._overdriveStart, this._maxValue);
|
2019-07-25 12:53:00 -04:00
|
|
|
this.notify('maximum-value');
|
|
|
|
this.queue_repaint();
|
2017-10-30 21:19:44 -04:00
|
|
|
}
|
2018-07-31 09:49:06 -04:00
|
|
|
|
2019-07-25 11:33:00 -04:00
|
|
|
// eslint-disable-next-line camelcase
|
|
|
|
get overdrive_start() {
|
|
|
|
return this._overdriveStart;
|
|
|
|
}
|
|
|
|
|
|
|
|
// eslint-disable-next-line camelcase
|
|
|
|
set overdrive_start(value) {
|
2019-02-02 12:01:56 -05:00
|
|
|
if (this._overdriveStart == value)
|
|
|
|
return;
|
|
|
|
|
2019-08-19 20:51:42 -04:00
|
|
|
if (value > this._maxValue) {
|
2018-07-31 09:49:06 -04:00
|
|
|
throw new Error(`Tried to set overdrive value to ${value}, ` +
|
|
|
|
`which is a number greater than the maximum allowed value ${this._maxValue}`);
|
2019-08-19 20:51:42 -04:00
|
|
|
}
|
2018-07-31 09:49:06 -04:00
|
|
|
|
|
|
|
this._overdriveStart = value;
|
2019-07-25 12:53:00 -04:00
|
|
|
this.notify('overdrive-start');
|
|
|
|
this.queue_repaint();
|
2017-10-30 21:19:44 -04:00
|
|
|
}
|
2018-02-08 13:04:42 -05:00
|
|
|
|
2019-07-25 12:53:00 -04:00
|
|
|
vfunc_repaint() {
|
|
|
|
let cr = this.get_context();
|
|
|
|
let themeNode = this.get_theme_node();
|
|
|
|
let [width, height] = this.get_surface_size();
|
2018-02-08 13:04:42 -05:00
|
|
|
|
|
|
|
let barLevelHeight = themeNode.get_length('-barlevel-height');
|
|
|
|
let barLevelBorderRadius = Math.min(width, barLevelHeight) / 2;
|
|
|
|
let fgColor = themeNode.get_foreground_color();
|
|
|
|
|
|
|
|
let barLevelColor = themeNode.get_color('-barlevel-background-color');
|
|
|
|
let barLevelActiveColor = themeNode.get_color('-barlevel-active-background-color');
|
2018-07-31 09:49:06 -04:00
|
|
|
let barLevelOverdriveColor = themeNode.get_color('-barlevel-overdrive-color');
|
2018-02-08 13:04:42 -05:00
|
|
|
|
2018-02-08 13:22:29 -05:00
|
|
|
let barLevelBorderWidth = Math.min(themeNode.get_length('-barlevel-border-width'), 1);
|
|
|
|
let [hasBorderColor, barLevelBorderColor] =
|
|
|
|
themeNode.lookup_color('-barlevel-border-color', false);
|
|
|
|
if (!hasBorderColor)
|
|
|
|
barLevelBorderColor = barLevelColor;
|
|
|
|
let [hasActiveBorderColor, barLevelActiveBorderColor] =
|
|
|
|
themeNode.lookup_color('-barlevel-active-border-color', false);
|
|
|
|
if (!hasActiveBorderColor)
|
|
|
|
barLevelActiveBorderColor = barLevelActiveColor;
|
2018-07-31 09:49:06 -04:00
|
|
|
let [hasOverdriveBorderColor, barLevelOverdriveBorderColor] =
|
|
|
|
themeNode.lookup_color('-barlevel-overdrive-border-color', false);
|
|
|
|
if (!hasOverdriveBorderColor)
|
|
|
|
barLevelOverdriveBorderColor = barLevelOverdriveColor;
|
2018-02-08 13:04:42 -05:00
|
|
|
|
|
|
|
const TAU = Math.PI * 2;
|
|
|
|
|
2018-02-09 08:02:21 -05:00
|
|
|
let endX = 0;
|
|
|
|
if (this._maxValue > 0)
|
|
|
|
endX = barLevelBorderRadius + (width - 2 * barLevelBorderRadius) * this._value / this._maxValue;
|
2018-02-08 13:04:42 -05:00
|
|
|
|
2018-07-31 09:49:06 -04:00
|
|
|
let overdriveSeparatorX = barLevelBorderRadius + (width - 2 * barLevelBorderRadius) * this._overdriveStart / this._maxValue;
|
|
|
|
let overdriveActive = this._overdriveStart !== this._maxValue;
|
|
|
|
let overdriveSeparatorWidth = 0;
|
|
|
|
if (overdriveActive)
|
|
|
|
overdriveSeparatorWidth = themeNode.get_length('-barlevel-overdrive-separator-width');
|
|
|
|
|
2018-02-08 13:04:42 -05:00
|
|
|
/* background bar */
|
2019-01-29 18:01:21 -05:00
|
|
|
cr.arc(width - barLevelBorderRadius - barLevelBorderWidth, height / 2, barLevelBorderRadius, TAU * (3 / 4), TAU * (1 / 4));
|
2018-02-08 13:04:42 -05:00
|
|
|
cr.lineTo(endX, (height + barLevelHeight) / 2);
|
|
|
|
cr.lineTo(endX, (height - barLevelHeight) / 2);
|
|
|
|
cr.lineTo(width - barLevelBorderRadius - barLevelBorderWidth, (height - barLevelHeight) / 2);
|
|
|
|
Clutter.cairo_set_source_color(cr, barLevelColor);
|
|
|
|
cr.fillPreserve();
|
|
|
|
Clutter.cairo_set_source_color(cr, barLevelBorderColor);
|
|
|
|
cr.setLineWidth(barLevelBorderWidth);
|
|
|
|
cr.stroke();
|
|
|
|
|
|
|
|
/* normal progress bar */
|
2018-07-31 09:49:06 -04:00
|
|
|
let x = Math.min(endX, overdriveSeparatorX - overdriveSeparatorWidth / 2);
|
2019-01-29 18:01:21 -05:00
|
|
|
cr.arc(barLevelBorderRadius + barLevelBorderWidth, height / 2, barLevelBorderRadius, TAU * (1 / 4), TAU * (3 / 4));
|
2018-07-31 09:49:06 -04:00
|
|
|
cr.lineTo(x, (height - barLevelHeight) / 2);
|
|
|
|
cr.lineTo(x, (height + barLevelHeight) / 2);
|
2018-02-08 13:04:42 -05:00
|
|
|
cr.lineTo(barLevelBorderRadius + barLevelBorderWidth, (height + barLevelHeight) / 2);
|
2019-02-02 11:01:29 -05:00
|
|
|
if (this._value > 0)
|
2019-01-29 14:36:54 -05:00
|
|
|
Clutter.cairo_set_source_color(cr, barLevelActiveColor);
|
2018-02-08 13:04:42 -05:00
|
|
|
cr.fillPreserve();
|
|
|
|
Clutter.cairo_set_source_color(cr, barLevelActiveBorderColor);
|
|
|
|
cr.setLineWidth(barLevelBorderWidth);
|
|
|
|
cr.stroke();
|
|
|
|
|
2018-07-31 09:49:06 -04:00
|
|
|
/* overdrive progress barLevel */
|
|
|
|
x = Math.min(endX, overdriveSeparatorX) + overdriveSeparatorWidth / 2;
|
|
|
|
if (this._value > this._overdriveStart) {
|
|
|
|
cr.moveTo(x, (height - barLevelHeight) / 2);
|
|
|
|
cr.lineTo(endX, (height - barLevelHeight) / 2);
|
|
|
|
cr.lineTo(endX, (height + barLevelHeight) / 2);
|
|
|
|
cr.lineTo(x, (height + barLevelHeight) / 2);
|
|
|
|
cr.lineTo(x, (height - barLevelHeight) / 2);
|
|
|
|
Clutter.cairo_set_source_color(cr, barLevelOverdriveColor);
|
|
|
|
cr.fillPreserve();
|
|
|
|
Clutter.cairo_set_source_color(cr, barLevelOverdriveBorderColor);
|
|
|
|
cr.setLineWidth(barLevelBorderWidth);
|
|
|
|
cr.stroke();
|
|
|
|
}
|
|
|
|
|
2018-02-08 13:04:42 -05:00
|
|
|
/* end progress bar arc */
|
2019-02-02 11:01:29 -05:00
|
|
|
if (this._value > 0) {
|
2019-01-29 14:36:54 -05:00
|
|
|
if (this._value <= this._overdriveStart)
|
|
|
|
Clutter.cairo_set_source_color(cr, barLevelActiveColor);
|
|
|
|
else
|
|
|
|
Clutter.cairo_set_source_color(cr, barLevelOverdriveColor);
|
|
|
|
cr.arc(endX, height / 2, barLevelBorderRadius, TAU * (3 / 4), TAU * (1 / 4));
|
|
|
|
cr.lineTo(Math.floor(endX), (height + barLevelHeight) / 2);
|
|
|
|
cr.lineTo(Math.floor(endX), (height - barLevelHeight) / 2);
|
|
|
|
cr.lineTo(endX, (height - barLevelHeight) / 2);
|
|
|
|
cr.fillPreserve();
|
|
|
|
cr.setLineWidth(barLevelBorderWidth);
|
|
|
|
cr.stroke();
|
2019-02-02 11:01:29 -05:00
|
|
|
}
|
2018-02-08 13:04:42 -05:00
|
|
|
|
2018-07-31 09:49:06 -04:00
|
|
|
/* draw overdrive separator */
|
|
|
|
if (overdriveActive) {
|
|
|
|
cr.moveTo(overdriveSeparatorX - overdriveSeparatorWidth / 2, (height - barLevelHeight) / 2);
|
|
|
|
cr.lineTo(overdriveSeparatorX + overdriveSeparatorWidth / 2, (height - barLevelHeight) / 2);
|
|
|
|
cr.lineTo(overdriveSeparatorX + overdriveSeparatorWidth / 2, (height + barLevelHeight) / 2);
|
|
|
|
cr.lineTo(overdriveSeparatorX - overdriveSeparatorWidth / 2, (height + barLevelHeight) / 2);
|
|
|
|
cr.lineTo(overdriveSeparatorX - overdriveSeparatorWidth / 2, (height - barLevelHeight) / 2);
|
|
|
|
if (this._value <= this._overdriveStart)
|
|
|
|
Clutter.cairo_set_source_color(cr, fgColor);
|
|
|
|
else
|
|
|
|
Clutter.cairo_set_source_color(cr, barLevelColor);
|
|
|
|
cr.fill();
|
|
|
|
}
|
|
|
|
|
2018-02-08 13:04:42 -05:00
|
|
|
cr.$dispose();
|
2017-10-30 21:19:44 -04:00
|
|
|
}
|
2018-02-08 13:04:42 -05:00
|
|
|
|
2019-02-04 06:30:53 -05:00
|
|
|
_getCurrentValue() {
|
2018-02-08 13:04:42 -05:00
|
|
|
return this._value;
|
2017-10-30 21:19:44 -04:00
|
|
|
}
|
2018-02-08 13:04:42 -05:00
|
|
|
|
2019-02-04 06:30:53 -05:00
|
|
|
_getOverdriveStart() {
|
2018-07-31 09:49:06 -04:00
|
|
|
return this._overdriveStart;
|
2017-10-30 21:19:44 -04:00
|
|
|
}
|
2018-07-31 09:49:06 -04:00
|
|
|
|
2019-02-04 06:30:53 -05:00
|
|
|
_getMinimumValue() {
|
2018-02-08 13:04:42 -05:00
|
|
|
return 0;
|
2017-10-30 21:19:44 -04:00
|
|
|
}
|
2018-02-08 13:04:42 -05:00
|
|
|
|
2019-02-04 06:30:53 -05:00
|
|
|
_getMaximumValue() {
|
2018-02-09 08:02:21 -05:00
|
|
|
return this._maxValue;
|
2017-10-30 21:19:44 -04:00
|
|
|
}
|
2018-02-08 13:04:42 -05:00
|
|
|
|
2019-01-31 09:08:10 -05:00
|
|
|
_setCurrentValue(_actor, value) {
|
2018-02-08 13:04:42 -05:00
|
|
|
this._value = value;
|
2017-10-30 21:19:44 -04:00
|
|
|
}
|
2018-02-08 13:04:42 -05:00
|
|
|
|
2019-02-04 06:30:53 -05:00
|
|
|
_valueChanged() {
|
2018-02-08 13:04:42 -05:00
|
|
|
this._customAccessible.notify("accessible-value");
|
2017-10-30 21:19:44 -04:00
|
|
|
}
|
2019-07-25 12:53:00 -04:00
|
|
|
});
|