2011-07-12 16:12:28 -04:00
|
|
|
/* -*- mode: js2; js2-basic-offset: 4; indent-tabs-mode: nil -*- */
|
|
|
|
|
2014-02-05 14:36:50 -05:00
|
|
|
const Atk = imports.gi.Atk;
|
2011-07-12 16:12:28 -04:00
|
|
|
const Cairo = imports.cairo;
|
|
|
|
const Clutter = imports.gi.Clutter;
|
|
|
|
const Lang = imports.lang;
|
|
|
|
const St = imports.gi.St;
|
|
|
|
const Signals = imports.signals;
|
|
|
|
|
2018-02-08 13:04:42 -05:00
|
|
|
const BarLevel = imports.ui.barLevel;
|
|
|
|
|
2017-07-18 13:47:27 -04:00
|
|
|
var SLIDER_SCROLL_STEP = 0.02; /* Slider scrolling step in % */
|
2011-07-12 16:12:28 -04:00
|
|
|
|
2017-07-18 13:41:25 -04:00
|
|
|
var Slider = new Lang.Class({
|
2011-07-12 16:12:28 -04:00
|
|
|
Name: "Slider",
|
2018-02-08 13:04:42 -05:00
|
|
|
Extends: BarLevel.BarLevel,
|
2011-07-12 16:12:28 -04:00
|
|
|
|
2017-10-30 20:03:21 -04:00
|
|
|
_init(value) {
|
2018-02-08 13:04:42 -05:00
|
|
|
let params = {
|
|
|
|
styleClass: 'slider',
|
|
|
|
canFocus: true,
|
|
|
|
reactive: true,
|
|
|
|
accessibleRole: Atk.Role.SLIDER,
|
|
|
|
}
|
|
|
|
this.parent(value, params)
|
|
|
|
|
2017-12-01 19:27:35 -05:00
|
|
|
this.actor.connect('button-press-event', this._startDragging.bind(this));
|
|
|
|
this.actor.connect('touch-event', this._touchDragging.bind(this));
|
|
|
|
this.actor.connect('scroll-event', this._onScrollEvent.bind(this));
|
|
|
|
this.actor.connect('key-press-event', this.onKeyPressEvent.bind(this));
|
2011-07-12 16:12:28 -04:00
|
|
|
|
|
|
|
this._releaseId = this._motionId = 0;
|
|
|
|
this._dragging = false;
|
2013-08-22 17:15:35 -04:00
|
|
|
|
2017-12-01 19:27:35 -05:00
|
|
|
this._customAccessible.connect('get-minimum-increment', this._getMinimumIncrement.bind(this));
|
2011-07-12 16:12:28 -04:00
|
|
|
},
|
|
|
|
|
2018-02-08 13:04:42 -05:00
|
|
|
_barLevelRepaint(area) {
|
|
|
|
this.parent(area);
|
2011-07-12 16:12:28 -04:00
|
|
|
|
2018-02-08 13:04:42 -05:00
|
|
|
// Add handle
|
2011-07-12 16:12:28 -04:00
|
|
|
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);
|
|
|
|
|
2013-06-21 14:26:42 -04:00
|
|
|
const TAU = Math.PI * 2;
|
|
|
|
|
2018-02-09 08:02:21 -05:00
|
|
|
let handleX = handleRadius + (width - 2 * handleRadius) * this._value / this._maxValue;
|
2011-07-12 16:12:28 -04:00
|
|
|
let handleY = height / 2;
|
|
|
|
|
|
|
|
let color = themeNode.get_foreground_color();
|
2013-06-21 14:24:06 -04:00
|
|
|
Clutter.cairo_set_source_color(cr, color);
|
2011-07-12 16:12:28 -04:00
|
|
|
cr.arc(handleX, handleY, handleRadius, 0, 2 * Math.PI);
|
|
|
|
cr.fillPreserve();
|
|
|
|
if (hasHandleColor && handleBorderWidth) {
|
2013-06-21 14:24:06 -04:00
|
|
|
Clutter.cairo_set_source_color(cr, handleBorderColor);
|
|
|
|
cr.setLineWidth(handleBorderWidth);
|
|
|
|
cr.stroke();
|
2011-07-12 16:12:28 -04:00
|
|
|
}
|
|
|
|
cr.$dispose();
|
|
|
|
},
|
|
|
|
|
2017-10-30 20:03:21 -04:00
|
|
|
_startDragging(actor, event) {
|
2013-11-29 13:17:34 -05:00
|
|
|
return this.startDragging(event);
|
2013-06-11 23:39:56 -04:00
|
|
|
},
|
|
|
|
|
2017-10-30 20:03:21 -04:00
|
|
|
startDragging(event) {
|
2013-06-11 23:43:04 -04:00
|
|
|
if (this._dragging)
|
2013-11-29 13:17:34 -05:00
|
|
|
return Clutter.EVENT_PROPAGATE;
|
2011-07-12 16:12:28 -04:00
|
|
|
|
|
|
|
this._dragging = true;
|
|
|
|
|
2013-06-11 23:43:04 -04:00
|
|
|
let device = event.get_device();
|
2014-07-22 06:37:06 -04:00
|
|
|
let sequence = event.get_event_sequence();
|
|
|
|
|
|
|
|
if (sequence != null)
|
|
|
|
device.sequence_grab(sequence, this.actor);
|
|
|
|
else
|
|
|
|
device.grab(this.actor);
|
|
|
|
|
2013-06-11 23:43:04 -04:00
|
|
|
this._grabbedDevice = device;
|
2014-07-22 06:37:06 -04:00
|
|
|
this._grabbedSequence = sequence;
|
|
|
|
|
|
|
|
if (sequence == null) {
|
2017-12-01 19:27:35 -05:00
|
|
|
this._releaseId = this.actor.connect('button-release-event', this._endDragging.bind(this));
|
|
|
|
this._motionId = this.actor.connect('motion-event', this._motionEvent.bind(this));
|
2014-07-22 06:37:06 -04:00
|
|
|
}
|
2013-06-11 23:43:04 -04:00
|
|
|
|
2016-01-27 17:35:11 -05:00
|
|
|
// We need to emit 'drag-begin' before moving the handle to make
|
|
|
|
// sure that no 'value-changed' signal is emitted before this one.
|
|
|
|
this.emit('drag-begin');
|
|
|
|
|
2011-07-12 16:12:28 -04:00
|
|
|
let absX, absY;
|
|
|
|
[absX, absY] = event.get_coords();
|
|
|
|
this._moveHandle(absX, absY);
|
2013-11-29 13:17:34 -05:00
|
|
|
return Clutter.EVENT_STOP;
|
2011-07-12 16:12:28 -04:00
|
|
|
},
|
|
|
|
|
2017-10-30 20:03:21 -04:00
|
|
|
_endDragging() {
|
2011-07-12 16:12:28 -04:00
|
|
|
if (this._dragging) {
|
2014-07-22 06:37:06 -04:00
|
|
|
if (this._releaseId)
|
|
|
|
this.actor.disconnect(this._releaseId);
|
|
|
|
if (this._motionId)
|
|
|
|
this.actor.disconnect(this._motionId);
|
|
|
|
|
|
|
|
if (this._grabbedSequence != null)
|
|
|
|
this._grabbedDevice.sequence_ungrab(this._grabbedSequence);
|
|
|
|
else
|
|
|
|
this._grabbedDevice.ungrab();
|
2011-07-12 16:12:28 -04:00
|
|
|
|
2014-07-22 06:37:06 -04:00
|
|
|
this._grabbedSequence = null;
|
2013-06-11 23:43:04 -04:00
|
|
|
this._grabbedDevice = null;
|
2011-07-12 16:12:28 -04:00
|
|
|
this._dragging = false;
|
|
|
|
|
|
|
|
this.emit('drag-end');
|
|
|
|
}
|
2013-11-29 13:17:34 -05:00
|
|
|
return Clutter.EVENT_STOP;
|
2011-07-12 16:12:28 -04:00
|
|
|
},
|
|
|
|
|
2017-10-30 20:03:21 -04:00
|
|
|
_touchDragging(actor, event) {
|
2014-07-22 06:37:06 -04:00
|
|
|
let device = event.get_device();
|
|
|
|
let sequence = event.get_event_sequence();
|
|
|
|
|
|
|
|
if (!this._dragging &&
|
|
|
|
event.type() == Clutter.EventType.TOUCH_BEGIN) {
|
|
|
|
this.startDragging(event);
|
|
|
|
return Clutter.EVENT_STOP;
|
|
|
|
} else if (device.sequence_get_grabbed_actor(sequence) == actor) {
|
|
|
|
if (event.type() == Clutter.EventType.TOUCH_UPDATE)
|
|
|
|
return this._motionEvent(actor, event);
|
|
|
|
else if (event.type() == Clutter.EventType.TOUCH_END)
|
|
|
|
return this._endDragging();
|
|
|
|
}
|
|
|
|
|
|
|
|
return Clutter.EVENT_PROPAGATE;
|
|
|
|
},
|
|
|
|
|
2017-10-30 20:03:21 -04:00
|
|
|
scroll(event) {
|
2011-07-12 16:12:28 -04:00
|
|
|
let direction = event.get_scroll_direction();
|
|
|
|
let delta;
|
|
|
|
|
|
|
|
if (event.is_pointer_emulated())
|
2013-11-29 13:17:34 -05:00
|
|
|
return Clutter.EVENT_PROPAGATE;
|
2011-07-12 16:12:28 -04: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.
|
2015-01-10 07:12:48 -05:00
|
|
|
delta = -dy * SLIDER_SCROLL_STEP;
|
2011-07-12 16:12:28 -04:00
|
|
|
}
|
|
|
|
|
2018-02-09 08:02:21 -05:00
|
|
|
this._value = Math.min(Math.max(0, this._value + delta), this._maxValue);
|
2011-07-12 16:12:28 -04:00
|
|
|
|
|
|
|
this.actor.queue_repaint();
|
|
|
|
this.emit('value-changed', this._value);
|
2013-11-29 13:17:34 -05:00
|
|
|
return Clutter.EVENT_STOP;
|
2011-07-12 16:12:28 -04:00
|
|
|
},
|
|
|
|
|
2017-10-30 20:03:21 -04:00
|
|
|
_onScrollEvent(actor, event) {
|
2013-11-29 13:17:34 -05:00
|
|
|
return this.scroll(event);
|
2011-07-12 16:12:28 -04:00
|
|
|
},
|
|
|
|
|
2017-10-30 20:03:21 -04:00
|
|
|
_motionEvent(actor, event) {
|
2011-07-12 16:12:28 -04:00
|
|
|
let absX, absY;
|
|
|
|
[absX, absY] = event.get_coords();
|
|
|
|
this._moveHandle(absX, absY);
|
2013-11-29 13:17:34 -05:00
|
|
|
return Clutter.EVENT_STOP;
|
2011-07-12 16:12:28 -04:00
|
|
|
},
|
|
|
|
|
2017-10-30 20:03:21 -04:00
|
|
|
onKeyPressEvent(actor, event) {
|
2011-07-12 16:12:28 -04: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;
|
2018-02-09 08:02:21 -05:00
|
|
|
this._value = Math.max(0, Math.min(this._value + delta, this._maxValue));
|
2013-08-22 06:04:40 -04:00
|
|
|
this.actor.queue_repaint();
|
2016-01-27 17:35:11 -05:00
|
|
|
this.emit('drag-begin');
|
2011-07-12 16:12:28 -04:00
|
|
|
this.emit('value-changed', this._value);
|
|
|
|
this.emit('drag-end');
|
2013-11-29 13:17:34 -05:00
|
|
|
return Clutter.EVENT_STOP;
|
2011-07-12 16:12:28 -04:00
|
|
|
}
|
2013-11-29 13:17:34 -05:00
|
|
|
return Clutter.EVENT_PROPAGATE;
|
2011-07-12 16:12:28 -04:00
|
|
|
},
|
|
|
|
|
2017-10-30 20:03:21 -04:00
|
|
|
_moveHandle(absX, absY) {
|
2011-07-12 16:12:28 -04:00
|
|
|
let relX, relY, sliderX, sliderY;
|
|
|
|
[sliderX, sliderY] = this.actor.get_transformed_position();
|
|
|
|
relX = absX - sliderX;
|
|
|
|
relY = absY - sliderY;
|
|
|
|
|
2018-02-08 13:04:42 -05:00
|
|
|
let width = this._barLevelWidth;
|
2011-07-12 16:12:28 -04:00
|
|
|
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);
|
2018-02-09 08:02:21 -05:00
|
|
|
this._value = newvalue * this._maxValue;
|
2011-07-12 16:12:28 -04:00
|
|
|
this.actor.queue_repaint();
|
|
|
|
this.emit('value-changed', this._value);
|
|
|
|
},
|
|
|
|
|
2017-10-30 20:03:21 -04:00
|
|
|
_getMinimumIncrement(actor) {
|
2013-08-22 17:15:35 -04:00
|
|
|
return 0.1;
|
|
|
|
},
|
2011-07-12 16:12:28 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
Signals.addSignalMethods(Slider.prototype);
|