2017-01-19 13:49:52 -05:00
|
|
|
// -*- mode: js; js-indent-level: 4; indent-tabs-mode: nil -*-
|
2019-01-31 09:07:06 -05:00
|
|
|
/* exported Dialog, MessageDialogContent */
|
2017-01-19 13:49:52 -05:00
|
|
|
|
2020-01-13 07:39:41 -05:00
|
|
|
const { Clutter, GObject, Pango, St } = imports.gi;
|
2017-01-19 13:49:52 -05:00
|
|
|
|
2017-10-30 21:23:39 -04:00
|
|
|
var Dialog = GObject.registerClass(
|
|
|
|
class Dialog extends St.Widget {
|
2017-10-30 20:03:21 -04:00
|
|
|
_init(parentActor, styleClass) {
|
2017-10-30 21:23:39 -04:00
|
|
|
super._init({ layout_manager: new Clutter.BinLayout() });
|
2017-12-01 19:27:35 -05:00
|
|
|
this.connect('destroy', this._onDestroy.bind(this));
|
2017-01-19 13:49:52 -05:00
|
|
|
|
2017-09-28 06:38:33 -04:00
|
|
|
this._initialKeyFocus = null;
|
2017-10-05 04:06:10 -04:00
|
|
|
this._initialKeyFocusDestroyId = 0;
|
2017-01-19 13:49:52 -05:00
|
|
|
this._pressedKey = null;
|
|
|
|
this._buttonKeys = {};
|
|
|
|
this._createDialog();
|
|
|
|
this.add_child(this._dialog);
|
|
|
|
|
|
|
|
if (styleClass != null)
|
|
|
|
this._dialog.add_style_class_name(styleClass);
|
|
|
|
|
|
|
|
this._parentActor = parentActor;
|
2017-12-01 19:27:35 -05:00
|
|
|
this._eventId = this._parentActor.connect('event', this._modalEventHandler.bind(this));
|
2017-01-19 13:49:52 -05:00
|
|
|
this._parentActor.add_child(this);
|
2017-10-30 21:23:39 -04:00
|
|
|
}
|
2017-01-19 13:49:52 -05:00
|
|
|
|
2017-10-30 20:03:21 -04:00
|
|
|
_createDialog() {
|
2019-12-05 03:54:38 -05:00
|
|
|
this._dialog = new St.BoxLayout({
|
|
|
|
style_class: 'modal-dialog',
|
|
|
|
x_align: Clutter.ActorAlign.CENTER,
|
|
|
|
y_align: Clutter.ActorAlign.CENTER,
|
|
|
|
vertical: true,
|
|
|
|
});
|
2017-01-19 13:49:52 -05:00
|
|
|
|
|
|
|
// modal dialogs are fixed width and grow vertically; set the request
|
|
|
|
// mode accordingly so wrapped labels are handled correctly during
|
|
|
|
// size requests.
|
|
|
|
this._dialog.request_mode = Clutter.RequestMode.HEIGHT_FOR_WIDTH;
|
2018-04-06 06:26:58 -04:00
|
|
|
this._dialog.set_offscreen_redirect(Clutter.OffscreenRedirect.ALWAYS);
|
2017-01-19 13:49:52 -05:00
|
|
|
|
2019-10-21 14:44:00 -04:00
|
|
|
this.contentLayout = new St.BoxLayout({
|
|
|
|
vertical: true,
|
|
|
|
style_class: 'modal-dialog-content-box',
|
|
|
|
y_expand: true,
|
|
|
|
});
|
|
|
|
this._dialog.add_child(this.contentLayout);
|
2017-01-19 13:49:52 -05:00
|
|
|
|
2019-12-05 03:54:38 -05:00
|
|
|
this.buttonLayout = new St.Widget({
|
|
|
|
layout_manager: new Clutter.BoxLayout({ homogeneous: true }),
|
|
|
|
});
|
2019-10-21 14:44:00 -04:00
|
|
|
this._dialog.add_child(this.buttonLayout);
|
2017-10-30 21:23:39 -04:00
|
|
|
}
|
2017-01-19 13:49:52 -05:00
|
|
|
|
2017-10-30 20:03:21 -04:00
|
|
|
_onDestroy() {
|
2017-01-19 13:49:52 -05:00
|
|
|
if (this._eventId != 0)
|
|
|
|
this._parentActor.disconnect(this._eventId);
|
|
|
|
this._eventId = 0;
|
2017-10-30 21:23:39 -04:00
|
|
|
}
|
2017-01-19 13:49:52 -05:00
|
|
|
|
2017-10-30 20:03:21 -04:00
|
|
|
_modalEventHandler(actor, event) {
|
2017-01-19 13:49:52 -05:00
|
|
|
if (event.type() == Clutter.EventType.KEY_PRESS) {
|
|
|
|
this._pressedKey = event.get_key_symbol();
|
|
|
|
} else if (event.type() == Clutter.EventType.KEY_RELEASE) {
|
|
|
|
let pressedKey = this._pressedKey;
|
|
|
|
this._pressedKey = null;
|
|
|
|
|
|
|
|
let symbol = event.get_key_symbol();
|
|
|
|
if (symbol != pressedKey)
|
|
|
|
return Clutter.EVENT_PROPAGATE;
|
|
|
|
|
|
|
|
let buttonInfo = this._buttonKeys[symbol];
|
|
|
|
if (!buttonInfo)
|
|
|
|
return Clutter.EVENT_PROPAGATE;
|
|
|
|
|
|
|
|
let { button, action } = buttonInfo;
|
|
|
|
|
|
|
|
if (action && button.reactive) {
|
|
|
|
action();
|
|
|
|
return Clutter.EVENT_STOP;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return Clutter.EVENT_PROPAGATE;
|
2017-10-30 21:23:39 -04:00
|
|
|
}
|
2017-01-19 13:49:52 -05:00
|
|
|
|
2017-10-30 20:03:21 -04:00
|
|
|
_setInitialKeyFocus(actor) {
|
2017-10-05 04:06:10 -04:00
|
|
|
if (this._initialKeyFocus)
|
|
|
|
this._initialKeyFocus.disconnect(this._initialKeyFocusDestroyId);
|
|
|
|
|
|
|
|
this._initialKeyFocus = actor;
|
|
|
|
|
|
|
|
this._initialKeyFocusDestroyId = actor.connect('destroy', () => {
|
|
|
|
this._initialKeyFocus = null;
|
|
|
|
this._initialKeyFocusDestroyId = 0;
|
|
|
|
});
|
2017-10-30 21:23:39 -04:00
|
|
|
}
|
2017-10-05 04:06:10 -04:00
|
|
|
|
2017-09-28 06:38:33 -04:00
|
|
|
get initialKeyFocus() {
|
|
|
|
return this._initialKeyFocus || this;
|
2017-10-30 21:23:39 -04:00
|
|
|
}
|
2017-09-28 06:38:33 -04:00
|
|
|
|
2017-10-30 20:03:21 -04:00
|
|
|
addButton(buttonInfo) {
|
2017-01-19 13:49:52 -05:00
|
|
|
let { label, action, key } = buttonInfo;
|
|
|
|
let isDefault = buttonInfo['default'];
|
|
|
|
let keys;
|
|
|
|
|
|
|
|
if (key)
|
|
|
|
keys = [key];
|
|
|
|
else if (isDefault)
|
|
|
|
keys = [Clutter.KEY_Return, Clutter.KEY_KP_Enter, Clutter.KEY_ISO_Enter];
|
|
|
|
else
|
|
|
|
keys = [];
|
|
|
|
|
2019-12-05 03:54:38 -05:00
|
|
|
let button = new St.Button({
|
|
|
|
style_class: 'modal-dialog-linked-button',
|
|
|
|
button_mask: St.ButtonMask.ONE | St.ButtonMask.THREE,
|
|
|
|
reactive: true,
|
|
|
|
can_focus: true,
|
|
|
|
x_expand: true,
|
|
|
|
y_expand: true,
|
|
|
|
label,
|
|
|
|
});
|
2017-01-19 13:49:52 -05:00
|
|
|
button.connect('clicked', action);
|
|
|
|
|
|
|
|
buttonInfo['button'] = button;
|
|
|
|
|
|
|
|
if (isDefault)
|
|
|
|
button.add_style_pseudo_class('default');
|
|
|
|
|
2017-09-28 06:38:33 -04:00
|
|
|
if (this._initialKeyFocus == null || isDefault)
|
2017-10-05 04:06:10 -04:00
|
|
|
this._setInitialKeyFocus(button);
|
2017-01-19 13:49:52 -05:00
|
|
|
|
|
|
|
for (let i in keys)
|
|
|
|
this._buttonKeys[keys[i]] = buttonInfo;
|
|
|
|
|
|
|
|
this.buttonLayout.add_actor(button);
|
|
|
|
|
|
|
|
return button;
|
2017-10-30 21:23:39 -04:00
|
|
|
}
|
2017-01-19 13:49:52 -05:00
|
|
|
|
2017-10-30 20:03:21 -04:00
|
|
|
clearButtons() {
|
2017-01-19 13:49:52 -05:00
|
|
|
this.buttonLayout.destroy_all_children();
|
|
|
|
this._buttonKeys = {};
|
2017-10-30 21:23:39 -04:00
|
|
|
}
|
2017-01-19 13:49:52 -05:00
|
|
|
});
|
2017-07-14 23:12:57 -04:00
|
|
|
|
2017-10-30 21:23:39 -04:00
|
|
|
var MessageDialogContent = GObject.registerClass({
|
2017-07-14 23:12:57 -04:00
|
|
|
Properties: {
|
2019-12-05 03:54:38 -05:00
|
|
|
'title': GObject.ParamSpec.string(
|
|
|
|
'title', 'title', 'title',
|
|
|
|
GObject.ParamFlags.READWRITE |
|
|
|
|
GObject.ParamFlags.CONSTRUCT,
|
|
|
|
null),
|
2020-01-13 08:07:28 -05:00
|
|
|
'description': GObject.ParamSpec.string(
|
|
|
|
'description', 'description', 'description',
|
2019-12-05 03:54:38 -05:00
|
|
|
GObject.ParamFlags.READWRITE |
|
|
|
|
GObject.ParamFlags.CONSTRUCT,
|
|
|
|
null),
|
2019-08-20 17:43:54 -04:00
|
|
|
},
|
2017-10-30 21:23:39 -04:00
|
|
|
}, class MessageDialogContent extends St.BoxLayout {
|
2017-10-30 20:03:21 -04:00
|
|
|
_init(params) {
|
2020-01-13 08:04:40 -05:00
|
|
|
this._title = new St.Label({ style_class: 'message-dialog-title' });
|
|
|
|
this._description = new St.Label({ style_class: 'message-dialog-description' });
|
2017-07-14 23:12:57 -04:00
|
|
|
|
2020-01-13 08:04:40 -05:00
|
|
|
this._description.clutter_text.ellipsize = Pango.EllipsizeMode.NONE;
|
|
|
|
this._description.clutter_text.line_wrap = true;
|
2017-07-14 23:12:57 -04:00
|
|
|
|
2019-12-05 04:55:00 -05:00
|
|
|
let defaultParams = {
|
2019-12-05 03:54:38 -05:00
|
|
|
style_class: 'message-dialog-content',
|
|
|
|
x_expand: true,
|
|
|
|
vertical: true,
|
2019-12-05 04:55:00 -05:00
|
|
|
};
|
|
|
|
super._init(Object.assign(defaultParams, params));
|
|
|
|
|
|
|
|
this.add_child(this._title);
|
2020-01-13 08:07:28 -05:00
|
|
|
this.add_child(this._description);
|
2017-10-30 21:23:39 -04:00
|
|
|
}
|
2017-07-14 23:12:57 -04:00
|
|
|
|
|
|
|
get title() {
|
|
|
|
return this._title.text;
|
2017-10-30 21:23:39 -04:00
|
|
|
}
|
2017-07-14 23:12:57 -04:00
|
|
|
|
2020-01-13 08:07:28 -05:00
|
|
|
get description() {
|
|
|
|
return this._description.text;
|
2017-10-30 21:23:39 -04:00
|
|
|
}
|
2017-07-14 23:12:57 -04:00
|
|
|
|
|
|
|
set title(title) {
|
|
|
|
this._setLabel(this._title, 'title', title);
|
2017-10-30 21:23:39 -04:00
|
|
|
}
|
2017-07-14 23:12:57 -04:00
|
|
|
|
2020-01-13 08:07:28 -05:00
|
|
|
set description(description) {
|
|
|
|
this._setLabel(this._description, 'description', description);
|
2017-10-30 21:23:39 -04:00
|
|
|
}
|
2017-07-14 23:12:57 -04:00
|
|
|
|
|
|
|
_setLabel(label, prop, value) {
|
2019-06-26 13:16:41 -04:00
|
|
|
label.set({
|
|
|
|
text: value || '',
|
2019-08-20 17:43:54 -04:00
|
|
|
visible: value != null,
|
2019-06-26 13:16:41 -04:00
|
|
|
});
|
2017-07-14 23:12:57 -04:00
|
|
|
this.notify(prop);
|
2017-10-30 21:23:39 -04:00
|
|
|
}
|
2017-07-14 23:12:57 -04:00
|
|
|
});
|