2008-12-01 19:51:43 +00:00
|
|
|
/* -*- mode: js2; js2-basic-offset: 4; indent-tabs-mode: nil -*- */
|
2008-11-15 00:44:11 +00:00
|
|
|
|
|
|
|
const Clutter = imports.gi.Clutter;
|
2009-02-04 18:45:38 +00:00
|
|
|
const Big = imports.gi.Big;
|
2008-11-15 00:44:11 +00:00
|
|
|
const Tweener = imports.tweener.tweener;
|
|
|
|
|
|
|
|
const DEFAULT_BUTTON_COLOR = new Clutter.Color();
|
2009-02-04 18:45:38 +00:00
|
|
|
DEFAULT_BUTTON_COLOR.from_pixel(0xeeddcc66);
|
2008-11-15 00:44:11 +00:00
|
|
|
|
|
|
|
const DEFAULT_PRESSED_BUTTON_COLOR = new Clutter.Color();
|
2009-02-04 18:45:38 +00:00
|
|
|
DEFAULT_PRESSED_BUTTON_COLOR.from_pixel(0xccbbaa66);
|
2008-11-15 00:44:11 +00:00
|
|
|
|
2009-02-04 18:45:38 +00:00
|
|
|
function Button(widget, buttonColor, pressedButtonColor, staysPressed, minWidth, minHeight) {
|
|
|
|
this._init(widget, buttonColor, pressedButtonColor, staysPressed, minWidth, minHeight);
|
2008-11-15 00:44:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Button.prototype = {
|
2009-02-04 18:45:38 +00:00
|
|
|
_init : function(widgetOrText, buttonColor, pressedButtonColor, staysPressed, minWidth, minHeight) {
|
2008-12-01 19:51:43 +00:00
|
|
|
let me = this;
|
2008-11-15 00:44:11 +00:00
|
|
|
|
2008-12-01 19:51:43 +00:00
|
|
|
this._buttonColor = buttonColor
|
|
|
|
if (buttonColor == null)
|
|
|
|
this._buttonColor = DEFAULT_BUTTON_COLOR;
|
2008-11-15 00:44:11 +00:00
|
|
|
|
2008-12-01 19:51:43 +00:00
|
|
|
this._pressedButtonColor = pressedButtonColor
|
|
|
|
if (pressedButtonColor == null)
|
|
|
|
this._pressedButtonColor = DEFAULT_PRESSED_BUTTON_COLOR;
|
2008-11-15 00:44:11 +00:00
|
|
|
|
2008-12-01 19:51:43 +00:00
|
|
|
if (staysPressed == null)
|
|
|
|
staysPressed = false;
|
|
|
|
if (minWidth == null)
|
|
|
|
minWidth = 0;
|
|
|
|
if (minHeight == null)
|
|
|
|
minHeight = 0;
|
2008-11-15 00:44:11 +00:00
|
|
|
|
2008-12-01 19:51:43 +00:00
|
|
|
// if staysPressed is true, this.active will be true past the first release of a button, untill a subsequent one (the button
|
|
|
|
// is unpressed) or untill release() is called explicitly
|
|
|
|
this._active = false;
|
|
|
|
this._isBetweenPressAndRelease = false;
|
|
|
|
this._mouseIsOverButton = false;
|
|
|
|
|
2009-02-04 18:45:38 +00:00
|
|
|
this.button = new Big.Box({ reactive: true,
|
|
|
|
corner_radius: 5,
|
|
|
|
padding_left: 4,
|
|
|
|
padding_right: 4,
|
|
|
|
orientation: Big.BoxOrientation.HORIZONTAL,
|
|
|
|
y_align: Big.BoxAlignment.CENTER
|
|
|
|
});
|
|
|
|
if (typeof widgetOrText == 'string') {
|
|
|
|
this._widget = new Clutter.Label({ font_name: "Sans Bold 16px",
|
|
|
|
text: widgetOrText });
|
|
|
|
} else {
|
|
|
|
this._widget = widgetOrText;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.button.append(this._widget, Big.BoxPackFlags.EXPAND);
|
|
|
|
|
|
|
|
this._minWidth = minWidth;
|
|
|
|
this._minHeight = minHeight;
|
|
|
|
|
2008-12-01 19:51:43 +00:00
|
|
|
this.button.connect('button-press-event',
|
|
|
|
function(o, event) {
|
|
|
|
me._isBetweenPressAndRelease = true;
|
2009-02-04 18:45:38 +00:00
|
|
|
me.button.backgroundColor = me._pressedButtonColor;
|
2008-12-01 19:51:43 +00:00
|
|
|
return false;
|
|
|
|
});
|
|
|
|
this.button.connect('button-release-event',
|
|
|
|
function(o, event) {
|
|
|
|
me._isBetweenPressAndRelease = false;
|
|
|
|
if (!staysPressed || me._active) {
|
|
|
|
me.release();
|
|
|
|
} else {
|
|
|
|
me._active = true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
this.button.connect('enter-event',
|
|
|
|
function(o, event) {
|
|
|
|
me._mouseIsOverButton = true;
|
|
|
|
if (!me._active) {
|
2009-02-04 18:45:38 +00:00
|
|
|
me.button.backgroundColor = me._buttonColor;
|
2008-12-01 19:51:43 +00:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
this.button.connect('leave-event',
|
|
|
|
function(o, event) {
|
|
|
|
me._isBetweenPressAndRelease = false;
|
|
|
|
me._mouseIsOverButton = false;
|
|
|
|
if (!me._active) {
|
2009-02-04 18:45:38 +00:00
|
|
|
me.button.backgroundColor = null;
|
2008-12-01 19:51:43 +00:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
release : function() {
|
|
|
|
if (!this._isBetweenPressAndRelease) {
|
|
|
|
this._active = false;
|
|
|
|
if (this._mouseIsOverButton) {
|
2009-02-04 18:45:38 +00:00
|
|
|
this.button.backgroundColor = this._buttonColor;
|
2008-11-15 00:44:11 +00:00
|
|
|
} else {
|
2009-02-04 18:45:38 +00:00
|
|
|
this.button.backgroundColor = null;
|
2008-11-15 00:44:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2008-12-01 19:51:43 +00:00
|
|
|
};
|