2011-07-12 20:12:28 +00:00
|
|
|
/* -*- mode: js2; js2-basic-offset: 4; indent-tabs-mode: nil -*- */
|
|
|
|
|
|
|
|
const Cairo = imports.cairo;
|
|
|
|
const Clutter = imports.gi.Clutter;
|
|
|
|
const Lang = imports.lang;
|
|
|
|
const St = imports.gi.St;
|
|
|
|
const Signals = imports.signals;
|
2013-08-21 16:24:30 +00:00
|
|
|
const Atk = imports.gi.Atk;
|
2011-07-12 20:12:28 +00:00
|
|
|
|
|
|
|
const SLIDER_SCROLL_STEP = 0.05; /* Slider scrolling step in % */
|
|
|
|
|
|
|
|
const Slider = new Lang.Class({
|
|
|
|
Name: "Slider",
|
|
|
|
|
|
|
|
_init: function(value) {
|
|
|
|
if (isNaN(value))
|
|
|
|
// Avoid spreading NaNs around
|
|
|
|
throw TypeError('The slider value must be a number');
|
|
|
|
this._value = Math.max(Math.min(value, 1), 0);
|
|
|
|
|
|
|
|
this.actor = new St.DrawingArea({ style_class: 'slider',
|
|
|
|
can_focus: true,
|
2013-08-21 16:24:30 +00:00
|
|
|
reactive: true,
|
|
|
|
accessible_role: Atk.Role.SLIDER });
|
2011-07-12 20:12:28 +00:00
|
|
|
this.actor.connect('repaint', Lang.bind(this, this._sliderRepaint));
|
|
|
|
this.actor.connect('button-press-event', Lang.bind(this, this._startDragging));
|
|
|
|
this.actor.connect('scroll-event', Lang.bind(this, this._onScrollEvent));
|
2013-08-22 11:15:00 +00:00
|
|
|
this.actor.connect('key-press-event', Lang.bind(this, this.onKeyPressEvent));
|
2011-07-12 20:12:28 +00:00
|
|
|
|
|
|
|
this._releaseId = this._motionId = 0;
|
|
|
|
this._dragging = false;
|
2013-08-22 21:15:35 +00:00
|
|
|
|
|
|
|
this._customAccessible = St.GenericAccessible.new_for_actor(this.actor);
|
|
|
|
this.actor.set_accessible(this._customAccessible);
|
|
|
|
|
|
|
|
this._customAccessible.connect('get-current-value', Lang.bind(this, this._getCurrentValue));
|
|
|
|
this._customAccessible.connect('get-minimum-value', Lang.bind(this, this._getMinimumValue));
|
|
|
|
this._customAccessible.connect('get-maximum-value', Lang.bind(this, this._getMaximumValue));
|
|
|
|
this._customAccessible.connect('get-minimum-increment', Lang.bind(this, this._getMinimumIncrement));
|
|
|
|
this._customAccessible.connect('set-current-value', Lang.bind(this, this._setCurrentValue));
|
|
|
|
|
|
|
|
this.connect('value-changed', Lang.bind(this, this._valueChanged));
|
2011-07-12 20:12:28 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
setValue: function(value) {
|
|
|
|
if (isNaN(value))
|
|
|
|
throw TypeError('The slider value must be a number');
|
|
|
|
|
|
|
|
this._value = Math.max(Math.min(value, 1), 0);
|
|
|
|
this.actor.queue_repaint();
|
|
|
|
},
|
|
|
|
|
|
|
|
_sliderRepaint: function(area) {
|
|
|
|
let cr = area.get_context();
|
|
|
|
let themeNode = area.get_theme_node();
|
|
|
|
let [width, height] = area.get_surface_size();
|
|
|
|
|
|
|
|
let handleRadius = themeNode.get_length('-slider-handle-radius');
|
|
|
|
|
|
|
|
let handleBorderWidth = themeNode.get_length('-slider-handle-border-width');
|
|
|
|
let [hasHandleColor, handleBorderColor] =
|
|
|
|
themeNode.lookup_color('-slider-handle-border-color', false);
|
|
|
|
|
|
|
|
let sliderHeight = themeNode.get_length('-slider-height');
|
|
|
|
|
|
|
|
let sliderBorderWidth = themeNode.get_length('-slider-border-width');
|
2013-06-21 18:26:42 +00:00
|
|
|
let sliderBorderRadius = Math.min(width, sliderHeight) / 2;
|
2011-07-12 20:12:28 +00:00
|
|
|
|
|
|
|
let sliderBorderColor = themeNode.get_color('-slider-border-color');
|
|
|
|
let sliderColor = themeNode.get_color('-slider-background-color');
|
|
|
|
|
|
|
|
let sliderActiveBorderColor = themeNode.get_color('-slider-active-border-color');
|
|
|
|
let sliderActiveColor = themeNode.get_color('-slider-active-background-color');
|
|
|
|
|
2013-06-21 18:26:42 +00:00
|
|
|
const TAU = Math.PI * 2;
|
|
|
|
|
|
|
|
let handleX = handleRadius + (width - 2 * handleRadius) * this._value;
|
|
|
|
|
|
|
|
cr.arc(sliderBorderRadius + sliderBorderWidth, height / 2, sliderBorderRadius, TAU * 1/4, TAU * 3/4);
|
|
|
|
cr.lineTo(handleX, (height - sliderHeight) / 2);
|
|
|
|
cr.lineTo(handleX, (height + sliderHeight) / 2);
|
|
|
|
cr.lineTo(sliderBorderRadius + sliderBorderWidth, (height + sliderHeight) / 2);
|
2013-06-21 18:24:06 +00:00
|
|
|
Clutter.cairo_set_source_color(cr, sliderActiveColor);
|
2011-07-12 20:12:28 +00:00
|
|
|
cr.fillPreserve();
|
2013-06-21 18:24:06 +00:00
|
|
|
Clutter.cairo_set_source_color(cr, sliderActiveBorderColor);
|
2011-07-12 20:12:28 +00:00
|
|
|
cr.setLineWidth(sliderBorderWidth);
|
|
|
|
cr.stroke();
|
|
|
|
|
2013-06-21 18:26:42 +00:00
|
|
|
cr.arc(width - sliderBorderRadius - sliderBorderWidth, height / 2, sliderBorderRadius, TAU * 3/4, TAU * 1/4);
|
|
|
|
cr.lineTo(handleX, (height + sliderHeight) / 2);
|
|
|
|
cr.lineTo(handleX, (height - sliderHeight) / 2);
|
|
|
|
cr.lineTo(width - sliderBorderRadius - sliderBorderWidth, (height - sliderHeight) / 2);
|
2013-06-21 18:24:06 +00:00
|
|
|
Clutter.cairo_set_source_color(cr, sliderColor);
|
2011-07-12 20:12:28 +00:00
|
|
|
cr.fillPreserve();
|
2013-06-21 18:24:06 +00:00
|
|
|
Clutter.cairo_set_source_color(cr, sliderBorderColor);
|
2011-07-12 20:12:28 +00:00
|
|
|
cr.setLineWidth(sliderBorderWidth);
|
|
|
|
cr.stroke();
|
|
|
|
|
|
|
|
let handleY = height / 2;
|
|
|
|
|
|
|
|
let color = themeNode.get_foreground_color();
|
2013-06-21 18:24:06 +00:00
|
|
|
Clutter.cairo_set_source_color(cr, color);
|
2011-07-12 20:12:28 +00:00
|
|
|
cr.arc(handleX, handleY, handleRadius, 0, 2 * Math.PI);
|
|
|
|
cr.fillPreserve();
|
|
|
|
if (hasHandleColor && handleBorderWidth) {
|
2013-06-21 18:24:06 +00:00
|
|
|
Clutter.cairo_set_source_color(cr, handleBorderColor);
|
|
|
|
cr.setLineWidth(handleBorderWidth);
|
|
|
|
cr.stroke();
|
2011-07-12 20:12:28 +00:00
|
|
|
}
|
|
|
|
cr.$dispose();
|
|
|
|
},
|
|
|
|
|
|
|
|
_startDragging: function(actor, event) {
|
2013-11-29 18:17:34 +00:00
|
|
|
return this.startDragging(event);
|
2013-06-12 03:39:56 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
startDragging: function(event) {
|
2013-06-12 03:43:04 +00:00
|
|
|
if (this._dragging)
|
2013-11-29 18:17:34 +00:00
|
|
|
return Clutter.EVENT_PROPAGATE;
|
2011-07-12 20:12:28 +00:00
|
|
|
|
|
|
|
this._dragging = true;
|
|
|
|
|
2013-06-12 03:43:04 +00:00
|
|
|
let device = event.get_device();
|
|
|
|
device.grab(this.actor);
|
|
|
|
this._grabbedDevice = device;
|
|
|
|
|
2011-07-12 20:12:28 +00:00
|
|
|
this._releaseId = this.actor.connect('button-release-event', Lang.bind(this, this._endDragging));
|
|
|
|
this._motionId = this.actor.connect('motion-event', Lang.bind(this, this._motionEvent));
|
|
|
|
let absX, absY;
|
|
|
|
[absX, absY] = event.get_coords();
|
|
|
|
this._moveHandle(absX, absY);
|
2013-11-29 18:17:34 +00:00
|
|
|
return Clutter.EVENT_STOP;
|
2011-07-12 20:12:28 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
_endDragging: function() {
|
|
|
|
if (this._dragging) {
|
|
|
|
this.actor.disconnect(this._releaseId);
|
|
|
|
this.actor.disconnect(this._motionId);
|
|
|
|
|
2013-06-12 03:43:04 +00:00
|
|
|
this._grabbedDevice.ungrab();
|
|
|
|
this._grabbedDevice = null;
|
2011-07-12 20:12:28 +00:00
|
|
|
this._dragging = false;
|
|
|
|
|
|
|
|
this.emit('drag-end');
|
|
|
|
}
|
2013-11-29 18:17:34 +00:00
|
|
|
return Clutter.EVENT_STOP;
|
2011-07-12 20:12:28 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
scroll: function(event) {
|
|
|
|
let direction = event.get_scroll_direction();
|
|
|
|
let delta;
|
|
|
|
|
|
|
|
if (event.is_pointer_emulated())
|
2013-11-29 18:17:34 +00:00
|
|
|
return Clutter.EVENT_PROPAGATE;
|
2011-07-12 20:12:28 +00:00
|
|
|
|
|
|
|
if (direction == Clutter.ScrollDirection.DOWN) {
|
|
|
|
delta = -SLIDER_SCROLL_STEP;
|
|
|
|
} else if (direction == Clutter.ScrollDirection.UP) {
|
|
|
|
delta = +SLIDER_SCROLL_STEP;
|
|
|
|
} else if (direction == Clutter.ScrollDirection.SMOOTH) {
|
|
|
|
let [dx, dy] = event.get_scroll_delta();
|
|
|
|
// Even though the slider is horizontal, use dy to match
|
|
|
|
// the UP/DOWN above.
|
|
|
|
delta = -dy / 10;
|
|
|
|
}
|
|
|
|
|
|
|
|
this._value = Math.min(Math.max(0, this._value + delta), 1);
|
|
|
|
|
|
|
|
this.actor.queue_repaint();
|
|
|
|
this.emit('value-changed', this._value);
|
2013-11-29 18:17:34 +00:00
|
|
|
return Clutter.EVENT_STOP;
|
2011-07-12 20:12:28 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
_onScrollEvent: function(actor, event) {
|
2013-11-29 18:17:34 +00:00
|
|
|
return this.scroll(event);
|
2011-07-12 20:12:28 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
_motionEvent: function(actor, event) {
|
|
|
|
let absX, absY;
|
|
|
|
[absX, absY] = event.get_coords();
|
|
|
|
this._moveHandle(absX, absY);
|
2013-11-29 18:17:34 +00:00
|
|
|
return Clutter.EVENT_STOP;
|
2011-07-12 20:12:28 +00:00
|
|
|
},
|
|
|
|
|
2013-08-22 11:15:00 +00:00
|
|
|
onKeyPressEvent: function (actor, event) {
|
2011-07-12 20:12:28 +00:00
|
|
|
let key = event.get_key_symbol();
|
|
|
|
if (key == Clutter.KEY_Right || key == Clutter.KEY_Left) {
|
|
|
|
let delta = key == Clutter.KEY_Right ? 0.1 : -0.1;
|
|
|
|
this._value = Math.max(0, Math.min(this._value + delta, 1));
|
2013-08-22 10:04:40 +00:00
|
|
|
this.actor.queue_repaint();
|
2011-07-12 20:12:28 +00:00
|
|
|
this.emit('value-changed', this._value);
|
|
|
|
this.emit('drag-end');
|
2013-11-29 18:17:34 +00:00
|
|
|
return Clutter.EVENT_STOP;
|
2011-07-12 20:12:28 +00:00
|
|
|
}
|
2013-11-29 18:17:34 +00:00
|
|
|
return Clutter.EVENT_PROPAGATE;
|
2011-07-12 20:12:28 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
_moveHandle: function(absX, absY) {
|
|
|
|
let relX, relY, sliderX, sliderY;
|
|
|
|
[sliderX, sliderY] = this.actor.get_transformed_position();
|
|
|
|
relX = absX - sliderX;
|
|
|
|
relY = absY - sliderY;
|
|
|
|
|
|
|
|
let width = this.actor.width;
|
|
|
|
let handleRadius = this.actor.get_theme_node().get_length('-slider-handle-radius');
|
|
|
|
|
|
|
|
let newvalue;
|
|
|
|
if (relX < handleRadius)
|
|
|
|
newvalue = 0;
|
|
|
|
else if (relX > width - handleRadius)
|
|
|
|
newvalue = 1;
|
|
|
|
else
|
|
|
|
newvalue = (relX - handleRadius) / (width - 2 * handleRadius);
|
|
|
|
this._value = newvalue;
|
|
|
|
this.actor.queue_repaint();
|
|
|
|
this.emit('value-changed', this._value);
|
|
|
|
},
|
|
|
|
|
2013-08-22 21:15:35 +00:00
|
|
|
_getCurrentValue: function (actor) {
|
|
|
|
return this._value;
|
|
|
|
},
|
|
|
|
|
|
|
|
_getMinimumValue: function (actor) {
|
|
|
|
return 0;
|
|
|
|
},
|
|
|
|
|
|
|
|
_getMaximumValue: function (actor) {
|
|
|
|
return 1;
|
|
|
|
},
|
|
|
|
|
|
|
|
_getMinimumIncrement: function (actor) {
|
|
|
|
return 0.1;
|
|
|
|
},
|
|
|
|
|
|
|
|
_setCurrentValue: function (actor, value) {
|
|
|
|
this._value = value;
|
|
|
|
},
|
|
|
|
|
|
|
|
_valueChanged: function (slider, value, property) {
|
|
|
|
this._customAccessible.notify ("accessible-value");
|
|
|
|
},
|
|
|
|
|
2011-07-12 20:12:28 +00:00
|
|
|
get value() {
|
|
|
|
return this._value;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
Signals.addSignalMethods(Slider.prototype);
|