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
|
|
|
|
2009-02-04 18:45:38 +00:00
|
|
|
const Big = imports.gi.Big;
|
2009-02-10 16:12:58 +00:00
|
|
|
const Clutter = imports.gi.Clutter;
|
2008-11-15 00:44:11 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
2009-05-08 20:27:14 +00:00
|
|
|
this._staysPressed = staysPressed
|
2008-12-01 19:51:43 +00:00
|
|
|
if (staysPressed == null)
|
2009-05-08 20:27:14 +00:00
|
|
|
this._staysPressed = false;
|
|
|
|
|
2008-12-01 19:51:43 +00:00
|
|
|
if (minWidth == null)
|
|
|
|
minWidth = 0;
|
|
|
|
if (minHeight == null)
|
|
|
|
minHeight = 0;
|
2008-11-15 00:44:11 +00:00
|
|
|
|
2009-05-08 20:27:14 +00:00
|
|
|
// if this._staysPressed is true, this._active will be true past the first release of a button, until a subsequent one (the button
|
|
|
|
// is unpressed) or until release() is called explicitly
|
2008-12-01 19:51:43 +00:00
|
|
|
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') {
|
2009-02-23 19:42:00 +00:00
|
|
|
this._widget = new Clutter.Text({ font_name: "Sans Bold 16px",
|
|
|
|
text: widgetOrText });
|
2009-02-04 18:45:38 +00:00
|
|
|
} 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;
|
2009-05-08 20:27:14 +00:00
|
|
|
if (!me._staysPressed || me._active) {
|
2008-12-01 19:51:43 +00:00
|
|
|
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;
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2009-05-08 20:27:14 +00:00
|
|
|
pressIn : function() {
|
|
|
|
if (!this._isBetweenPressAndRelease && this._staysPressed) {
|
|
|
|
this._active = true;
|
|
|
|
this.button.backgroundColor = this._pressedButtonColor;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2008-12-01 19:51:43 +00:00
|
|
|
release : function() {
|
2009-05-08 20:27:14 +00:00
|
|
|
if (!this._isBetweenPressAndRelease && this._staysPressed) {
|
2008-12-01 19:51:43 +00:00
|
|
|
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
|
|
|
};
|