2017-01-19 18:49:52 +00:00
|
|
|
// -*- mode: js; js-indent-level: 4; indent-tabs-mode: nil -*-
|
|
|
|
|
2019-02-09 03:21:36 +00:00
|
|
|
const { Clutter, Gio, GObject, Pango, St } = imports.gi;
|
2017-01-19 18:49:52 +00:00
|
|
|
|
2017-10-31 01:23:39 +00:00
|
|
|
var Dialog = GObject.registerClass(
|
|
|
|
class Dialog extends St.Widget {
|
2017-10-31 00:03:21 +00:00
|
|
|
_init(parentActor, styleClass) {
|
2017-10-31 01:23:39 +00:00
|
|
|
super._init({ layout_manager: new Clutter.BinLayout() });
|
2017-12-02 00:27:35 +00:00
|
|
|
this.connect('destroy', this._onDestroy.bind(this));
|
2017-01-19 18:49:52 +00:00
|
|
|
|
2017-09-28 10:38:33 +00:00
|
|
|
this._initialKeyFocus = null;
|
2017-10-05 08:06:10 +00:00
|
|
|
this._initialKeyFocusDestroyId = 0;
|
2017-01-19 18:49:52 +00: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-02 00:27:35 +00:00
|
|
|
this._eventId = this._parentActor.connect('event', this._modalEventHandler.bind(this));
|
2017-01-19 18:49:52 +00:00
|
|
|
this._parentActor.add_child(this);
|
2017-10-31 01:23:39 +00:00
|
|
|
}
|
2017-01-19 18:49:52 +00:00
|
|
|
|
2017-10-31 00:03:21 +00:00
|
|
|
_createDialog() {
|
2017-01-19 18:49:52 +00:00
|
|
|
this._dialog = new St.BoxLayout({ style_class: 'modal-dialog',
|
|
|
|
x_align: Clutter.ActorAlign.CENTER,
|
|
|
|
y_align: Clutter.ActorAlign.CENTER,
|
|
|
|
vertical: true });
|
|
|
|
|
|
|
|
// 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 10:26:58 +00:00
|
|
|
this._dialog.set_offscreen_redirect(Clutter.OffscreenRedirect.ALWAYS);
|
2017-01-19 18:49:52 +00:00
|
|
|
|
|
|
|
this.contentLayout = new St.BoxLayout({ vertical: true,
|
|
|
|
style_class: "modal-dialog-content-box" });
|
|
|
|
this._dialog.add(this.contentLayout,
|
|
|
|
{ expand: true,
|
|
|
|
x_fill: true,
|
|
|
|
y_fill: true,
|
|
|
|
x_align: St.Align.MIDDLE,
|
|
|
|
y_align: St.Align.START });
|
|
|
|
|
|
|
|
this.buttonLayout = new St.Widget ({ layout_manager: new Clutter.BoxLayout({ homogeneous:true }) });
|
|
|
|
this._dialog.add(this.buttonLayout,
|
|
|
|
{ x_align: St.Align.MIDDLE,
|
|
|
|
y_align: St.Align.START });
|
2017-10-31 01:23:39 +00:00
|
|
|
}
|
2017-01-19 18:49:52 +00:00
|
|
|
|
2017-10-31 00:03:21 +00:00
|
|
|
_onDestroy() {
|
2017-01-19 18:49:52 +00:00
|
|
|
if (this._eventId != 0)
|
|
|
|
this._parentActor.disconnect(this._eventId);
|
|
|
|
this._eventId = 0;
|
2017-10-31 01:23:39 +00:00
|
|
|
}
|
2017-01-19 18:49:52 +00:00
|
|
|
|
2017-10-31 00:03:21 +00:00
|
|
|
_modalEventHandler(actor, event) {
|
2017-01-19 18:49:52 +00: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-31 01:23:39 +00:00
|
|
|
}
|
2017-01-19 18:49:52 +00:00
|
|
|
|
2017-10-31 00:03:21 +00:00
|
|
|
_setInitialKeyFocus(actor) {
|
2017-10-05 08:06:10 +00: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-31 01:23:39 +00:00
|
|
|
}
|
2017-10-05 08:06:10 +00:00
|
|
|
|
2017-09-28 10:38:33 +00:00
|
|
|
get initialKeyFocus() {
|
|
|
|
return this._initialKeyFocus || this;
|
2017-10-31 01:23:39 +00:00
|
|
|
}
|
2017-09-28 10:38:33 +00:00
|
|
|
|
2017-10-31 00:03:21 +00:00
|
|
|
addContent(actor) {
|
2017-01-19 18:49:52 +00:00
|
|
|
this.contentLayout.add (actor, { expand: true });
|
2017-10-31 01:23:39 +00:00
|
|
|
}
|
2017-01-19 18:49:52 +00:00
|
|
|
|
2017-10-31 00:03:21 +00:00
|
|
|
addButton(buttonInfo) {
|
2017-01-19 18:49:52 +00: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 = [];
|
|
|
|
|
|
|
|
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: label });
|
|
|
|
button.connect('clicked', action);
|
|
|
|
|
|
|
|
buttonInfo['button'] = button;
|
|
|
|
|
|
|
|
if (isDefault)
|
|
|
|
button.add_style_pseudo_class('default');
|
|
|
|
|
2017-09-28 10:38:33 +00:00
|
|
|
if (this._initialKeyFocus == null || isDefault)
|
2017-10-05 08:06:10 +00:00
|
|
|
this._setInitialKeyFocus(button);
|
2017-01-19 18:49:52 +00:00
|
|
|
|
|
|
|
for (let i in keys)
|
|
|
|
this._buttonKeys[keys[i]] = buttonInfo;
|
|
|
|
|
|
|
|
this.buttonLayout.add_actor(button);
|
|
|
|
|
|
|
|
return button;
|
2017-10-31 01:23:39 +00:00
|
|
|
}
|
2017-01-19 18:49:52 +00:00
|
|
|
|
2017-10-31 00:03:21 +00:00
|
|
|
clearButtons() {
|
2017-01-19 18:49:52 +00:00
|
|
|
this.buttonLayout.destroy_all_children();
|
|
|
|
this._buttonKeys = {};
|
2017-10-31 01:23:39 +00:00
|
|
|
}
|
2017-01-19 18:49:52 +00:00
|
|
|
});
|
2017-07-15 03:12:57 +00:00
|
|
|
|
2017-10-31 01:23:39 +00:00
|
|
|
var MessageDialogContent = GObject.registerClass({
|
2017-07-15 03:12:57 +00:00
|
|
|
Properties: {
|
|
|
|
'icon': GObject.ParamSpec.object('icon', 'icon', 'icon',
|
|
|
|
GObject.ParamFlags.READWRITE |
|
|
|
|
GObject.ParamFlags.CONSTRUCT,
|
|
|
|
Gio.Icon.$gtype),
|
|
|
|
'title': GObject.ParamSpec.string('title', 'title', 'title',
|
|
|
|
GObject.ParamFlags.READWRITE |
|
|
|
|
GObject.ParamFlags.CONSTRUCT,
|
|
|
|
null),
|
|
|
|
'subtitle': GObject.ParamSpec.string('subtitle', 'subtitle', 'subtitle',
|
|
|
|
GObject.ParamFlags.READWRITE |
|
|
|
|
GObject.ParamFlags.CONSTRUCT,
|
|
|
|
null),
|
|
|
|
'body': GObject.ParamSpec.string('body', 'body', 'body',
|
|
|
|
GObject.ParamFlags.READWRITE |
|
|
|
|
GObject.ParamFlags.CONSTRUCT,
|
|
|
|
null)
|
2017-10-31 01:23:39 +00:00
|
|
|
}
|
|
|
|
}, class MessageDialogContent extends St.BoxLayout {
|
2017-10-31 00:03:21 +00:00
|
|
|
_init(params) {
|
2017-07-15 03:12:57 +00:00
|
|
|
this._icon = new St.Icon({ y_align: Clutter.ActorAlign.START });
|
|
|
|
this._title = new St.Label({ style_class: 'headline' });
|
|
|
|
this._subtitle = new St.Label();
|
|
|
|
this._body = new St.Label();
|
|
|
|
|
|
|
|
['icon', 'title', 'subtitle', 'body'].forEach(prop => {
|
|
|
|
this[`_${prop}`].add_style_class_name(`message-dialog-${prop}`);
|
|
|
|
});
|
|
|
|
|
2019-05-03 20:27:13 +00:00
|
|
|
let textProps = { ellipsize: Pango.EllipsizeMode.NONE,
|
2017-07-15 03:12:57 +00:00
|
|
|
line_wrap: true };
|
2019-05-03 20:30:42 +00:00
|
|
|
this._subtitle.clutter_text.set(textProps);
|
|
|
|
this._body.clutter_text.set(textProps);
|
2017-07-15 03:12:57 +00:00
|
|
|
|
|
|
|
if (!params.hasOwnProperty('style_class'))
|
|
|
|
params.style_class = 'message-dialog-main-layout';
|
|
|
|
|
2017-10-31 01:23:39 +00:00
|
|
|
super._init(params);
|
2017-07-15 03:12:57 +00:00
|
|
|
|
|
|
|
this.messageBox = new St.BoxLayout({ style_class: 'message-dialog-content',
|
|
|
|
x_expand: true,
|
|
|
|
vertical: true });
|
|
|
|
|
|
|
|
this.messageBox.add_actor(this._title);
|
|
|
|
this.messageBox.add_actor(this._subtitle);
|
|
|
|
this.messageBox.add_actor(this._body);
|
|
|
|
|
|
|
|
this.add_actor(this._icon);
|
|
|
|
this.add_actor(this.messageBox);
|
2017-10-31 01:23:39 +00:00
|
|
|
}
|
2017-07-15 03:12:57 +00:00
|
|
|
|
|
|
|
get icon() {
|
|
|
|
return this._icon.gicon;
|
2017-10-31 01:23:39 +00:00
|
|
|
}
|
2017-07-15 03:12:57 +00:00
|
|
|
|
|
|
|
get title() {
|
|
|
|
return this._title.text;
|
2017-10-31 01:23:39 +00:00
|
|
|
}
|
2017-07-15 03:12:57 +00:00
|
|
|
|
|
|
|
get subtitle() {
|
|
|
|
return this._subtitle.text;
|
2017-10-31 01:23:39 +00:00
|
|
|
}
|
2017-07-15 03:12:57 +00:00
|
|
|
|
|
|
|
get body() {
|
|
|
|
return this._body.text;
|
2017-10-31 01:23:39 +00:00
|
|
|
}
|
2017-07-15 03:12:57 +00:00
|
|
|
|
|
|
|
set icon(icon) {
|
2019-06-26 17:16:41 +00:00
|
|
|
this._icon.set({
|
|
|
|
gicon: icon,
|
|
|
|
visible: icon != null
|
|
|
|
});
|
2017-07-15 03:12:57 +00:00
|
|
|
this.notify('icon');
|
2017-10-31 01:23:39 +00:00
|
|
|
}
|
2017-07-15 03:12:57 +00:00
|
|
|
|
|
|
|
set title(title) {
|
|
|
|
this._setLabel(this._title, 'title', title);
|
2017-10-31 01:23:39 +00:00
|
|
|
}
|
2017-07-15 03:12:57 +00:00
|
|
|
|
|
|
|
set subtitle(subtitle) {
|
|
|
|
this._setLabel(this._subtitle, 'subtitle', subtitle);
|
2017-10-31 01:23:39 +00:00
|
|
|
}
|
2017-07-15 03:12:57 +00:00
|
|
|
|
|
|
|
set body(body) {
|
|
|
|
this._setLabel(this._body, 'body', body);
|
2017-10-31 01:23:39 +00:00
|
|
|
}
|
2017-07-15 03:12:57 +00:00
|
|
|
|
|
|
|
_setLabel(label, prop, value) {
|
2019-06-26 17:16:41 +00:00
|
|
|
label.set({
|
|
|
|
text: value || '',
|
|
|
|
visible: value != null
|
|
|
|
});
|
2017-07-15 03:12:57 +00:00
|
|
|
this.notify(prop);
|
2017-10-31 01:23:39 +00:00
|
|
|
}
|
2017-07-15 03:12:57 +00:00
|
|
|
|
2017-10-31 00:03:21 +00:00
|
|
|
insertBeforeBody(actor) {
|
2017-07-15 03:12:57 +00:00
|
|
|
this.messageBox.insert_child_below(actor, this._body);
|
|
|
|
}
|
|
|
|
});
|