2010-05-19 17:26:41 +00:00
|
|
|
/* -*- mode: js2; js2-basic-offset: 4; indent-tabs-mode: nil -*- */
|
2010-05-04 18:32:28 +00:00
|
|
|
|
|
|
|
const Clutter = imports.gi.Clutter;
|
2010-05-19 17:26:41 +00:00
|
|
|
const Lang = imports.lang;
|
2010-05-04 18:32:28 +00:00
|
|
|
const St = imports.gi.St;
|
|
|
|
const Shell = imports.gi.Shell;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* BoxPointer:
|
|
|
|
* @side: A St.Side type; currently only St.Side.TOP is implemented
|
|
|
|
* @binProperties: Properties to set on contained bin
|
|
|
|
*
|
|
|
|
* An actor which displays a triangle "arrow" pointing to a given
|
|
|
|
* side. The .bin property is a container in which content can be
|
|
|
|
* placed. The arrow position may be controlled via setArrowOrigin().
|
|
|
|
*
|
|
|
|
*/
|
2010-05-12 21:07:41 +00:00
|
|
|
function BoxPointer(side, binProperties) {
|
|
|
|
this._init(side, binProperties);
|
2010-05-04 18:32:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
BoxPointer.prototype = {
|
2010-05-12 21:07:41 +00:00
|
|
|
_init: function(arrowSide, binProperties) {
|
2010-05-04 18:32:28 +00:00
|
|
|
this._arrowSide = arrowSide;
|
|
|
|
this._arrowOrigin = 0;
|
|
|
|
this.actor = new St.Bin({ x_fill: true,
|
|
|
|
y_fill: true });
|
|
|
|
this._container = new Shell.GenericContainer();
|
|
|
|
this.actor.set_child(this._container);
|
|
|
|
this._container.connect('get-preferred-width', Lang.bind(this, this._getPreferredWidth));
|
|
|
|
this._container.connect('get-preferred-height', Lang.bind(this, this._getPreferredHeight));
|
|
|
|
this._container.connect('allocate', Lang.bind(this, this._allocate));
|
|
|
|
this.bin = new St.Bin(binProperties);
|
|
|
|
this._container.add_actor(this.bin);
|
|
|
|
this._border = new St.DrawingArea();
|
|
|
|
this._border.connect('repaint', Lang.bind(this, this._drawBorder));
|
|
|
|
this._container.add_actor(this._border);
|
|
|
|
this.bin.raise(this._border);
|
|
|
|
},
|
|
|
|
|
|
|
|
_adjustAllocationForArrow: function(isWidth, alloc) {
|
|
|
|
let themeNode = this.actor.get_theme_node();
|
|
|
|
let found, borderWidth, base, rise;
|
|
|
|
[found, borderWidth] = themeNode.get_length('-arrow-border-width', false);
|
|
|
|
alloc.min_size += borderWidth * 2;
|
|
|
|
alloc.natural_size += borderWidth * 2;
|
|
|
|
if ((!isWidth && (this._arrowSide == St.Side.TOP || this._arrowSide == St.Side.BOTTOM))
|
|
|
|
|| (isWidth && (this._arrowSide == St.Side.LEFT || this._arrowSide == St.Side.RIGHT))) {
|
|
|
|
let [found, rise] = themeNode.get_length('-arrow-rise', false);
|
|
|
|
alloc.min_size += rise;
|
|
|
|
alloc.natural_size += rise;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
_getPreferredWidth: function(actor, forHeight, alloc) {
|
|
|
|
let [minInternalSize, natInternalSize] = this.bin.get_preferred_width(forHeight);
|
2010-05-12 21:07:41 +00:00
|
|
|
alloc.min_size = minInternalSize;
|
|
|
|
alloc.natural_size = natInternalSize;
|
2010-05-04 18:32:28 +00:00
|
|
|
this._adjustAllocationForArrow(true, alloc);
|
|
|
|
},
|
|
|
|
|
|
|
|
_getPreferredHeight: function(actor, forWidth, alloc) {
|
|
|
|
let [minSize, naturalSize] = this.bin.get_preferred_height(forWidth);
|
|
|
|
alloc.min_size = minSize;
|
|
|
|
alloc.natural_size = naturalSize;
|
|
|
|
this._adjustAllocationForArrow(false, alloc);
|
|
|
|
},
|
|
|
|
|
|
|
|
_allocate: function(actor, box, flags) {
|
|
|
|
let themeNode = this.actor.get_theme_node();
|
|
|
|
let found, borderWidth, borderRadius, rise, base;
|
|
|
|
[found, borderWidth] = themeNode.get_length('-arrow-border-width', false);
|
|
|
|
[found, rise] = themeNode.get_length('-arrow-rise', false);
|
|
|
|
let childBox = new Clutter.ActorBox();
|
|
|
|
let availWidth = box.x2 - box.x1;
|
|
|
|
let availHeight = box.y2 - box.y1;
|
|
|
|
|
2010-05-19 17:26:41 +00:00
|
|
|
childBox.x1 = 0;
|
2010-05-04 18:32:28 +00:00
|
|
|
childBox.y1 = 0;
|
|
|
|
childBox.x2 = availWidth;
|
|
|
|
childBox.y2 = availHeight;
|
|
|
|
this._border.allocate(childBox, flags);
|
2010-05-20 15:18:46 +00:00
|
|
|
|
|
|
|
childBox.x1 = borderWidth;
|
|
|
|
childBox.y1 = borderWidth;
|
|
|
|
childBox.x2 = availWidth - borderWidth;
|
|
|
|
childBox.y2 = availHeight - borderWidth;
|
2010-05-04 18:32:28 +00:00
|
|
|
switch (this._arrowSide) {
|
|
|
|
case St.Side.TOP:
|
2010-05-20 15:18:46 +00:00
|
|
|
childBox.y1 += rise;
|
|
|
|
break;
|
|
|
|
case St.Side.BOTTOM:
|
|
|
|
childBox.y2 -= rise;
|
|
|
|
break;
|
|
|
|
case St.Side.LEFT:
|
|
|
|
childBox.x1 += rise;
|
2010-05-04 18:32:28 +00:00
|
|
|
break;
|
2010-05-20 15:18:46 +00:00
|
|
|
case St.Side.RIGHT:
|
|
|
|
childBox.x2 -= rise;
|
2010-05-04 18:32:28 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
this.bin.allocate(childBox, flags);
|
|
|
|
},
|
|
|
|
|
|
|
|
_drawBorder: function(area) {
|
|
|
|
let themeNode = this.actor.get_theme_node();
|
|
|
|
|
|
|
|
let found, borderWidth, borderRadius, rise, base;
|
|
|
|
[found, borderWidth] = themeNode.get_length('-arrow-border-width', false);
|
|
|
|
[found, base] = themeNode.get_length('-arrow-base', false);
|
|
|
|
[found, rise] = themeNode.get_length('-arrow-rise', false);
|
|
|
|
[found, borderRadius] = themeNode.get_length('-arrow-border-radius', false);
|
|
|
|
|
|
|
|
let halfBorder = borderWidth / 2;
|
2010-05-20 15:18:46 +00:00
|
|
|
let halfBase = Math.floor(base/2);
|
2010-05-04 18:32:28 +00:00
|
|
|
|
|
|
|
let borderColor = new Clutter.Color();
|
|
|
|
themeNode.get_color('-arrow-border-color', false, borderColor);
|
|
|
|
let backgroundColor = new Clutter.Color();
|
|
|
|
themeNode.get_color('-arrow-background-color', false, backgroundColor);
|
|
|
|
|
|
|
|
let [width, height] = area.get_surface_size();
|
|
|
|
let [boxWidth, boxHeight] = [width, height];
|
|
|
|
if (this._arrowSide == St.Side.TOP || this._arrowSide == St.Side.BOTTOM) {
|
|
|
|
boxHeight -= rise;
|
|
|
|
} else {
|
|
|
|
boxWidth -= rise;
|
|
|
|
}
|
|
|
|
let cr = area.get_context();
|
|
|
|
Clutter.cairo_set_source_color(cr, borderColor);
|
2010-05-20 15:18:46 +00:00
|
|
|
|
|
|
|
// Translate so that box goes from 0,0 to boxWidth,boxHeight,
|
|
|
|
// with the arrow poking out of that
|
2010-05-04 18:32:28 +00:00
|
|
|
if (this._arrowSide == St.Side.TOP) {
|
|
|
|
cr.translate(0, rise);
|
2010-05-20 15:18:46 +00:00
|
|
|
} else if (this._arrowSide == St.Side.LEFT) {
|
|
|
|
cr.translate(rise, 0);
|
2010-05-04 18:32:28 +00:00
|
|
|
}
|
2010-05-20 15:18:46 +00:00
|
|
|
|
2010-05-04 18:32:28 +00:00
|
|
|
cr.moveTo(borderRadius, halfBorder);
|
2010-05-20 15:18:46 +00:00
|
|
|
|
2010-05-04 18:32:28 +00:00
|
|
|
if (this._arrowSide == St.Side.TOP) {
|
2010-05-20 15:18:46 +00:00
|
|
|
cr.lineTo(this._arrowOrigin - halfBase, halfBorder);
|
|
|
|
cr.lineTo(this._arrowOrigin, halfBorder - rise);
|
|
|
|
cr.lineTo(this._arrowOrigin + halfBase, halfBorder);
|
2010-05-04 18:32:28 +00:00
|
|
|
}
|
|
|
|
cr.lineTo(boxWidth - borderRadius, halfBorder);
|
2010-05-20 15:18:46 +00:00
|
|
|
|
2010-05-04 18:32:28 +00:00
|
|
|
cr.arc(boxWidth - borderRadius - halfBorder, borderRadius + halfBorder, borderRadius,
|
|
|
|
3*Math.PI/2, Math.PI*2);
|
2010-05-20 15:18:46 +00:00
|
|
|
|
|
|
|
if (this._arrowSide == St.Side.RIGHT) {
|
|
|
|
cr.lineTo(boxWidth - halfBorder, this._arrowOrigin - halfBase);
|
|
|
|
cr.lineTo(boxWidth - halfBorder + rise, this._arrowOrigin);
|
|
|
|
cr.lineTo(boxWidth - halfBorder, this._arrowOrigin + halfBase);
|
|
|
|
}
|
2010-05-04 18:32:28 +00:00
|
|
|
cr.lineTo(boxWidth - halfBorder, boxHeight - borderRadius);
|
2010-05-20 15:18:46 +00:00
|
|
|
|
2010-05-04 18:32:28 +00:00
|
|
|
cr.arc(boxWidth - borderRadius - halfBorder, boxHeight - borderRadius - halfBorder, borderRadius,
|
|
|
|
0, Math.PI/2);
|
2010-05-20 15:18:46 +00:00
|
|
|
|
|
|
|
if (this._arrowSide == St.Side.BOTTOM) {
|
|
|
|
cr.lineTo(this._arrowOrigin + halfBase, boxHeight - halfBorder);
|
|
|
|
cr.lineTo(this._arrowOrigin, boxHeight - halfBorder + rise);
|
|
|
|
cr.lineTo(this._arrowOrigin - halfBase, boxHeight - halfBorder);
|
|
|
|
}
|
2010-05-04 18:32:28 +00:00
|
|
|
cr.lineTo(borderRadius, boxHeight - halfBorder);
|
2010-05-20 15:18:46 +00:00
|
|
|
|
2010-05-04 18:32:28 +00:00
|
|
|
cr.arc(borderRadius + halfBorder, boxHeight - borderRadius - halfBorder, borderRadius,
|
|
|
|
Math.PI/2, Math.PI);
|
2010-05-20 15:18:46 +00:00
|
|
|
|
|
|
|
if (this._arrowSide == St.Side.LEFT) {
|
|
|
|
cr.lineTo(halfBorder, this._arrowOrigin + halfBase);
|
|
|
|
cr.lineTo(halfBorder - rise, this._arrowOrigin);
|
|
|
|
cr.lineTo(halfBorder, this._arrowOrigin - halfBase);
|
|
|
|
}
|
2010-05-04 18:32:28 +00:00
|
|
|
cr.lineTo(halfBorder, borderRadius);
|
2010-05-20 15:18:46 +00:00
|
|
|
|
2010-05-04 18:32:28 +00:00
|
|
|
cr.arc(borderRadius + halfBorder, borderRadius + halfBorder, borderRadius,
|
|
|
|
Math.PI, 3*Math.PI/2);
|
2010-05-20 15:18:46 +00:00
|
|
|
|
2010-05-04 18:32:28 +00:00
|
|
|
Clutter.cairo_set_source_color(cr, backgroundColor);
|
|
|
|
cr.fillPreserve();
|
|
|
|
Clutter.cairo_set_source_color(cr, borderColor);
|
|
|
|
cr.setLineWidth(borderWidth);
|
|
|
|
cr.stroke();
|
|
|
|
},
|
|
|
|
|
|
|
|
// @origin: Coordinate specifying middle of the arrow, along
|
|
|
|
// the Y axis for St.Side.LEFT, St.Side.RIGHT from the top and X axis from
|
|
|
|
// the left for St.Side.TOP and St.Side.BOTTOM.
|
|
|
|
setArrowOrigin: function(origin) {
|
|
|
|
if (this._arrowOrigin != origin) {
|
|
|
|
this._arrowOrigin = origin;
|
|
|
|
this._border.queue_repaint();
|
|
|
|
}
|
|
|
|
}
|
2010-05-19 17:26:41 +00:00
|
|
|
};
|