1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
|
// -*- mode: js; js-indent-level: 4; indent-tabs-mode: nil -*-
const Clutter = imports.gi.Clutter;
const GObject = imports.gi.GObject;
const St = imports.gi.St;
var BarLevel = GObject.registerClass({
Properties: {
'value': GObject.ParamSpec.double(
'value', 'value', 'value',
GObject.ParamFlags.READWRITE,
0, 2, 0),
'maximum-value': GObject.ParamSpec.double(
'maximum-value', 'maximum-value', 'maximum-value',
GObject.ParamFlags.READWRITE,
1, 2, 1),
'amplify-start': GObject.ParamSpec.double(
'amplify-start', 'amplify-start', 'amplify-start',
GObject.ParamFlags.READWRITE,
1, 2, 1),
},
}, class BarLevel extends St.DrawingArea {
_init(params) {
this._maxValue = 1;
this._value = 0;
this._amplifyStart = 1;
this._barLevelWidth = 0;
this._barLevelHeight = 0;
this._amplifySeparatorWidth = 0;
this._barLevelColor = null;
this._barLevelActiveColor = null;
this._barLevelAmplifyColor = null;
let defaultParams = {
style_class: 'barlevel',
};
super._init(Object.assign(defaultParams, params));
this.connect('notify::allocation', () => {
this._barLevelWidth = this.allocation.get_width();
});
}
get value() {
return this._value;
}
set value(value) {
value = Math.max(Math.min(value, this._maxValue), 0);
if (this._value == value)
return;
this._value = value;
this.notify('value');
this.queue_repaint();
}
get maximum_value() {
return this._maxValue;
}
set maximum_value(value) {
value = Math.max(value, 1);
if (this._maxValue == value)
return;
this._maxValue = value;
this._amplifyStart = Math.min(this._amplifyStart, this._maxValue);
this.notify('maximum-value');
this.queue_repaint();
}
get amplify_start() {
return this._amplifyStart;
}
set amplify_start(value) {
if (this._amplifyStart == value)
return;
if (value > this._maxValue) {
throw new Error(`Tried to set amplify value to ${value}, ` +
`which is a number greater than the maximum allowed value ${this._maxValue}`);
}
this._amplifyStart = value;
this.notify('amplify-start');
this.queue_repaint();
}
vfunc_style_changed() {
const themeNode = this.get_theme_node();
this._barLevelHeight = themeNode.get_length('-barlevel-height');
this._amplifySeparatorWidth =
themeNode.get_length('-barlevel-amplify-separator-width');
this._barLevelColor = themeNode.get_color('-barlevel-background-color');
this._barLevelActiveColor = themeNode.get_color('-barlevel-active-background-color');
this._barLevelAmplifyColor = themeNode.get_color('-barlevel-amplify-color');
super.vfunc_style_changed();
}
vfunc_repaint() {
let cr = this.get_context();
let themeNode = this.get_theme_node();
let [width, height] = this.get_surface_size();
const rtl = this.get_direction() === St.TextDirection.RTL;
const barLevelBorderRadius = Math.min(width, this._barLevelHeight) / 2;
let fgColor = themeNode.get_foreground_color();
const TAU = Math.PI * 2;
let endX = 0;
if (this._maxValue > 0) {
let progress = this._value / this._maxValue;
if (rtl)
progress = 1 - progress;
endX = barLevelBorderRadius + (width - 2 * barLevelBorderRadius) * progress;
}
let amplifyRatio = this._amplifyStart / this._maxValue;
if (rtl)
amplifyRatio = 1 - amplifyRatio;
let amplifySeparatorX = barLevelBorderRadius + (width - 2 * barLevelBorderRadius) * amplifyRatio;
let amplifyActive = this._amplifyStart !== this._maxValue;
const amplifySeparatorWidth = amplifyActive
? this._amplifySeparatorWidth : 0;
let xcArcStart = barLevelBorderRadius;
let xcArcEnd = width - xcArcStart;
if (rtl)
[xcArcStart, xcArcEnd] = [xcArcEnd, xcArcStart];
/* background bar */
if (!rtl)
cr.arc(xcArcEnd, height / 2, barLevelBorderRadius, TAU * (3 / 4), TAU * (1 / 4));
else
cr.arcNegative(xcArcEnd, height / 2, barLevelBorderRadius, TAU * (3 / 4), TAU * (1 / 4));
cr.lineTo(endX, (height + this._barLevelHeight) / 2);
cr.lineTo(endX, (height - this._barLevelHeight) / 2);
cr.lineTo(xcArcEnd, (height - this._barLevelHeight) / 2);
Clutter.cairo_set_source_color(cr, this._barLevelColor);
cr.fillPreserve();
cr.fill();
/* normal progress bar */
let x = 0;
if (!rtl) {
x = Math.min(endX, amplifySeparatorX - amplifySeparatorWidth / 2);
cr.arc(xcArcStart, height / 2, barLevelBorderRadius, TAU * (1 / 4), TAU * (3 / 4));
} else {
x = Math.max(endX, amplifySeparatorX + amplifySeparatorWidth / 2);
cr.arcNegative(xcArcStart, height / 2, barLevelBorderRadius, TAU * (1 / 4), TAU * (3 / 4));
}
cr.lineTo(x, (height - this._barLevelHeight) / 2);
cr.lineTo(x, (height + this._barLevelHeight) / 2);
cr.lineTo(xcArcStart, (height + this._barLevelHeight) / 2);
if (this._value > 0)
Clutter.cairo_set_source_color(cr, this._barLevelActiveColor);
cr.fillPreserve();
cr.fill();
/* amplify progress barLevel */
if (!rtl)
x = Math.min(endX, amplifySeparatorX) + amplifySeparatorWidth / 2;
else
x = Math.max(endX, amplifySeparatorX) - amplifySeparatorWidth / 2;
if (this._value > this._amplifyStart) {
cr.moveTo(x, (height - this._barLevelHeight) / 2);
cr.lineTo(endX, (height - this._barLevelHeight) / 2);
cr.lineTo(endX, (height + this._barLevelHeight) / 2);
cr.lineTo(x, (height + this._barLevelHeight) / 2);
cr.lineTo(x, (height - this._barLevelHeight) / 2);
Clutter.cairo_set_source_color(cr, this._barLevelAmplifyColor);
cr.fillPreserve();
cr.fill();
}
/* end progress bar arc */
if (this._value > 0) {
if (this._value <= this._amplifyStart)
Clutter.cairo_set_source_color(cr, this._barLevelActiveColor);
else
Clutter.cairo_set_source_color(cr, this._barLevelAmplifyColor);
if (!rtl) {
cr.arc(endX, height / 2, barLevelBorderRadius, TAU * (3 / 4), TAU * (1 / 4));
cr.lineTo(Math.floor(endX), (height + this._barLevelHeight) / 2);
cr.lineTo(Math.floor(endX), (height - this._barLevelHeight) / 2);
} else {
cr.arcNegative(endX, height / 2, barLevelBorderRadius, TAU * (3 / 4), TAU * (1 / 4));
cr.lineTo(Math.ceil(endX), (height + this._barLevelHeight) / 2);
cr.lineTo(Math.ceil(endX), (height - this._barLevelHeight) / 2);
}
cr.lineTo(endX, (height - this._barLevelHeight) / 2);
cr.fillPreserve();
}
/* draw amplify separator */
if (amplifyActive) {
cr.moveTo(amplifySeparatorX - amplifySeparatorWidth / 2, (height - this._barLevelHeight) / 2);
cr.lineTo(amplifySeparatorX + amplifySeparatorWidth / 2, (height - this._barLevelHeight) / 2);
cr.lineTo(amplifySeparatorX + amplifySeparatorWidth / 2, (height + this._barLevelHeight) / 2);
cr.lineTo(amplifySeparatorX - amplifySeparatorWidth / 2, (height + this._barLevelHeight) / 2);
cr.lineTo(amplifySeparatorX - amplifySeparatorWidth / 2, (height - this._barLevelHeight) / 2);
if (this._value <= this._amplifyStart)
Clutter.cairo_set_source_color(cr, fgColor);
else
Clutter.cairo_set_source_color(cr, this._barLevelColor);
cr.fill();
}
cr.$dispose();
}
vfunc_get_preferred_height(_forWidth) {
const themeNode = this.get_theme_node();
const height = this._getPreferredHeight();
return themeNode.adjust_preferred_height(height, height);
}
vfunc_get_preferred_width(_forHeight) {
const themeNode = this.get_theme_node();
const width = this._getPreferredWidth();
return themeNode.adjust_preferred_width(width, width);
}
_getPreferredHeight() {
return this._barLevelHeight;
}
_getPreferredWidth() {
return this._amplifySeparatorWidth;
}
_getCurrentValue() {
return this._value;
}
_getAmplifyStart() {
return this._amplifyStart;
}
_getMinimumValue() {
return 0;
}
_getMaximumValue() {
return this._maxValue;
}
_setCurrentValue(_actor, value) {
this._value = value;
}
});
|