-
Notifications
You must be signed in to change notification settings - Fork 2
/
myAlertDialog.js
131 lines (102 loc) · 3.2 KB
/
myAlertDialog.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/* myAlertDialog.js
*
* SPDX-License-Identifier: GPL-3.0-or-later
*/
// Implements most of Gtk.AlertDialog the extension needs.
import Gio from 'gi://Gio';
import GLib from 'gi://GLib';
import GObject from 'gi://GObject';
import Gtk from 'gi://Gtk';
import {
gettext as _
} from 'resource:///org/gnome/Shell/Extensions/js/extensions/prefs.js';
const DialogError = {
CANCELLED: 1,
DISMISSED: 2,
};
const DIALOG_ERROR_QUARK = 'my-dialog-error-quark';
export default
class AlertDialog extends Gtk.MessageDialog {
static {
GObject.registerClass(this);
}
#cancel_return = -1;
#cancellable_signal = 0;
#task;
constructor({message,
detail,
default_button = null,
cancel_button = null,
buttons = [],
...args})
{
args.text = message;
args.secondary_text = detail;
super(args);
if (buttons?.length > 0) {
buttons.forEach((label, i) => {
this.add_button(label, i);
if (default_button === i)
this.set_default_response(i);
if (cancel_button === i)
this.#cancel_return = i;
});
} else {
this.add_button(_('_Close'), 0);
this.set_default_response(0);
this.#cancel_return = 0;
}
}
choose(parent, cancellable, callback)
{
this.#task = Gio.Task.new(this, cancellable, callback);
if (cancellable)
this.#cancellable_signal = cancellable.connect('cancelled',
c => this._cancelled_cb(c));
this.transient_for = parent;
this.present();
}
show(parent)
{
this.choose(parent, null, null);
}
choose_finish(result)
{
return result.propagate_int();
}
_cancelled_cb(cancellable)
{
this.on_response(Gtk.ResponseType.CLOSE);
}
on_response(response)
{
const cancellable = this.#task.get_cancellable();
if (cancellable) {
cancellable.disconnect(this.#cancellable_signal);
this.#cancellable_signal = 0;
}
if (response == Gtk.ResponseType.CLOSE) {
this.#task.return_error(
new GLib.Error(GLib.quark_from_string(DIALOG_ERROR_QUARK),
DialogError.CANCELLED,
"Cancelled by application")
);
} else if (response >= 0) {
// clicked on a button
this.#task.return_int(response);
} else {
if (this.#cancel_return >= 0) {
// dialog was closed -> interpret as a cancel response
this.#task.return_int(this.#cancel_return);
} else {
// no cancel response on close, so generate an error
this.#task.return_error(
new GLib.Error(GLib.quark_from_string(DIALOG_ERROR_QUARK),
DialogError.DISMISSED,
"Dismissed by user")
);
}
}
this.destroy();
}
};