forked from linuxmint/cinnamon
/
messageTray.js
1910 lines (1633 loc) · 72.8 KB
/
messageTray.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// -*- mode: js; js-indent-level: 4; indent-tabs-mode: nil -*-
const Clutter = imports.gi.Clutter;
const GLib = imports.gi.GLib;
const Gio = imports.gi.Gio;
const Gtk = imports.gi.Gtk;
const Atk = imports.gi.Atk;
const Lang = imports.lang;
const Mainloop = imports.mainloop;
const Meta = imports.gi.Meta;
const Pango = imports.gi.Pango;
const Cinnamon = imports.gi.Cinnamon;
const Signals = imports.signals;
const St = imports.gi.St;
const GnomeSession = imports.misc.gnomeSession;
const Main = imports.ui.main;
const PopupMenu = imports.ui.popupMenu;
const Params = imports.misc.params;
const Tweener = imports.ui.tweener;
const Util = imports.misc.util;
const AppletManager = imports.ui.appletManager;
const ANIMATION_TIME = .2;
const NOTIFICATION_TIMEOUT = 4;
const NOTIFICATION_CRITICAL_TIMEOUT_WITH_APPLET = 10;
const SUMMARY_TIMEOUT = 1;
const LONGER_SUMMARY_TIMEOUT = 4;
const HIDE_TIMEOUT = 0.2;
const LONGER_HIDE_TIMEOUT = 0.6;
const MAX_SOURCE_TITLE_WIDTH = 180;
// We delay hiding of the tray if the mouse is within MOUSE_LEFT_ACTOR_THRESHOLD
// range from the point where it left the tray.
const MOUSE_LEFT_ACTOR_THRESHOLD = 20;
const State = {
HIDDEN: 0,
SHOWING: 1,
SHOWN: 2,
HIDING: 3
};
// These reasons are useful when we destroy the notifications received through
// the notification daemon. We use EXPIRED for transient notifications that the
// user did not interact with, DISMISSED for all other notifications that were
// destroyed as a result of a user action, and SOURCE_CLOSED for the notifications
// that were requested to be destroyed by the associated source.
const NotificationDestroyedReason = {
EXPIRED: 1,
DISMISSED: 2,
SOURCE_CLOSED: 3
};
// Message tray has its custom Urgency enumeration. LOW, NORMAL and CRITICAL
// urgency values map to the corresponding values for the notifications received
// through the notification daemon. HIGH urgency value is used for chats received
// through the Telepathy client.
const Urgency = {
LOW: 0,
NORMAL: 1,
HIGH: 2,
CRITICAL: 3
}
function _fixMarkup(text, allowMarkup) {
if (allowMarkup) {
// Support &, ", ', < and >, escape all other
// occurrences of '&'.
let _text = text.replace(/&(?!amp;|quot;|apos;|lt;|gt;)/g, '&');
// Support <b>, <i>, and <u>, escape anything else
// so it displays as raw markup.
_text = _text.replace(/<(?!\/?[biu]>)/g, '<');
try {
Pango.parse_markup(_text, -1, '');
return _text;
} catch (e) {}
}
// !allowMarkup, or invalid markup
return GLib.markup_escape_text(text, -1);
}
function URLHighlighter(text, lineWrap, allowMarkup) {
this._init(text, lineWrap, allowMarkup);
}
URLHighlighter.prototype = {
_init: function(text, lineWrap, allowMarkup) {
if (!text)
text = '';
this.actor = new St.Label({ reactive: true, style_class: 'url-highlighter' });
this._linkColor = '#ccccff';
this.actor.connect('style-changed', Lang.bind(this, function() {
let [hasColor, color] = this.actor.get_theme_node().lookup_color('link-color', false);
if (hasColor) {
let linkColor = color.to_string().substr(0, 7);
if (linkColor != this._linkColor) {
this._linkColor = linkColor;
this._highlightUrls();
}
}
}));
if (lineWrap) {
this.actor.clutter_text.line_wrap = true;
this.actor.clutter_text.line_wrap_mode = Pango.WrapMode.WORD_CHAR;
this.actor.clutter_text.ellipsize = Pango.EllipsizeMode.NONE;
}
this.setMarkup(text, allowMarkup);
this.actor.connect('button-press-event', Lang.bind(this, function(actor, event) {
// Don't try to URL highlight when invisible.
// The MessageTray doesn't actually hide us, so
// we need to check for paint opacities as well.
if (!actor.visible || actor.get_paint_opacity() == 0)
return false;
// Keep Notification.actor from seeing this and taking
// a pointer grab, which would block our button-release-event
// handler, if an URL is clicked
return this._findUrlAtPos(event) != -1;
}));
this.actor.connect('button-release-event', Lang.bind(this, function (actor, event) {
if (!actor.visible || actor.get_paint_opacity() == 0)
return false;
let urlId = this._findUrlAtPos(event);
if (urlId != -1) {
let url = this._urls[urlId].url;
if (url.indexOf(':') == -1)
url = 'http://' + url;
try {
Gio.app_info_launch_default_for_uri(url, global.create_app_launch_context());
return true;
} catch (e) {
// TODO: remove this after gnome 3 release
Util.spawn(['gvfs-open', url]);
return true;
}
}
return false;
}));
this.actor.connect('motion-event', Lang.bind(this, function(actor, event) {
if (!actor.visible || actor.get_paint_opacity() == 0)
return false;
let urlId = this._findUrlAtPos(event);
if (urlId != -1 && !this._cursorChanged) {
global.set_cursor(Cinnamon.Cursor.POINTING_HAND);
this._cursorChanged = true;
} else if (urlId == -1) {
global.unset_cursor();
this._cursorChanged = false;
}
return false;
}));
this.actor.connect('leave-event', Lang.bind(this, function() {
if (!this.actor.visible || this.actor.get_paint_opacity() == 0)
return;
if (this._cursorChanged) {
this._cursorChanged = false;
global.unset_cursor();
}
}));
},
setMarkup: function(text, allowMarkup) {
text = text ? _fixMarkup(text, allowMarkup) : '';
this._text = text;
this.actor.clutter_text.set_markup(text);
/* clutter_text.text contain text without markup */
this._urls = Util.findUrls(this.actor.clutter_text.text);
this._highlightUrls();
},
_highlightUrls: function() {
// text here contain markup
let urls = Util.findUrls(this._text);
let markup = '';
let pos = 0;
for (let i = 0; i < urls.length; i++) {
let url = urls[i];
let str = this._text.substr(pos, url.pos - pos);
markup += str + '<span foreground="' + this._linkColor + '"><u>' + url.url + '</u></span>';
pos = url.pos + url.url.length;
}
markup += this._text.substr(pos);
this.actor.clutter_text.set_markup(markup);
},
_findUrlAtPos: function(event) {
let success;
let [x, y] = event.get_coords();
[success, x, y] = this.actor.transform_stage_point(x, y);
let find_pos = -1;
for (let i = 0; i < this.actor.clutter_text.text.length; i++) {
let [success, px, py, line_height] = this.actor.clutter_text.position_to_coords(i);
if (py > y || py + line_height < y || x < px)
continue;
find_pos = i;
}
if (find_pos != -1) {
for (let i = 0; i < this._urls.length; i++)
if (find_pos >= this._urls[i].pos &&
this._urls[i].pos + this._urls[i].url.length > find_pos)
return i;
}
return -1;
}
};
function FocusGrabber() {
this._init();
}
FocusGrabber.prototype = {
_init: function() {
this.actor = null;
this._hasFocus = false;
// We use this._prevFocusedWindow and this._prevKeyFocusActor to return the
// focus where it previously belonged after a focus grab, unless the user
// has explicitly changed that.
this._prevFocusedWindow = null;
this._prevKeyFocusActor = null;
this._focusActorChangedId = 0;
this._stageInputModeChangedId = 0;
this._capturedEventId = 0;
this._togglingFocusGrabMode = false;
Main.overview.connect('showing', Lang.bind(this,
function() {
this._toggleFocusGrabMode();
}));
Main.overview.connect('hidden', Lang.bind(this,
function() {
this._toggleFocusGrabMode();
}));
Main.expo.connect('showing', Lang.bind(this,
function() {
this._toggleFocusGrabMode();
}));
Main.expo.connect('hidden', Lang.bind(this,
function() {
this._toggleFocusGrabMode();
}));
},
grabFocus: function(actor) {
if (this._hasFocus)
return;
this.actor = actor;
this._prevFocusedWindow = global.display.focus_window;
this._prevKeyFocusActor = global.stage.get_key_focus();
if (global.stage_input_mode == Cinnamon.StageInputMode.NONREACTIVE ||
global.stage_input_mode == Cinnamon.StageInputMode.NORMAL)
global.set_stage_input_mode(Cinnamon.StageInputMode.FOCUSED);
// Use captured-event to notice clicks outside the focused actor
// without consuming them.
this._capturedEventId = global.stage.connect('captured-event', Lang.bind(this, this._onCapturedEvent));
this._stageInputModeChangedId = global.connect('notify::stage-input-mode', Lang.bind(this, this._stageInputModeChanged));
this._focusActorChangedId = global.stage.connect('notify::key-focus', Lang.bind(this, this._focusActorChanged));
this._hasFocus = true;
this.actor.navigate_focus(null, Gtk.DirectionType.TAB_FORWARD, false);
this.emit('focus-grabbed');
},
_focusActorChanged: function() {
let focusedActor = global.stage.get_key_focus();
if (!focusedActor || !this.actor.contains(focusedActor)) {
this._prevKeyFocusActor = null;
this.ungrabFocus();
}
},
_stageInputModeChanged: function() {
this.ungrabFocus();
},
_onCapturedEvent: function(actor, event) {
let source = event.get_source();
switch (event.type()) {
case Clutter.EventType.BUTTON_PRESS:
if (!this.actor.contains(source) &&
!Main.layoutManager.keyboardBox.contains(source))
this.emit('button-pressed', source);
break;
case Clutter.EventType.KEY_PRESS:
let symbol = event.get_key_symbol();
if (symbol == Clutter.Escape) {
this.emit('escape-pressed');
return true;
}
break;
}
return false;
},
ungrabFocus: function() {
if (!this._hasFocus)
return;
if (this._focusActorChangedId > 0) {
global.stage.disconnect(this._focusActorChangedId);
this._focusActorChangedId = 0;
}
if (this._stageInputModeChangedId) {
global.disconnect(this._stageInputModeChangedId);
this._stageInputModeChangedId = 0;
}
if (this._capturedEventId > 0) {
global.stage.disconnect(this._capturedEventId);
this._capturedEventId = 0;
}
this._hasFocus = false;
this.emit('focus-ungrabbed');
if (this._prevFocusedWindow && !global.display.focus_window) {
global.display.set_input_focus_window(this._prevFocusedWindow, false, global.get_current_time());
this._prevFocusedWindow = null;
}
if (this._prevKeyFocusActor) {
global.stage.set_key_focus(this._prevKeyFocusActor);
this._prevKeyFocusActor = null;
} else {
// We don't want to keep any actor inside the previously focused actor focused.
let focusedActor = global.stage.get_key_focus();
if (focusedActor && this.actor.contains(focusedActor))
global.stage.set_key_focus(null);
}
if (!this._togglingFocusGrabMode)
this.actor = null;
},
// Because we grab focus differently in the overview
// and in the main view, we need to change how it is
// done when we move between the two.
_toggleFocusGrabMode: function() {
if (this._hasFocus) {
this._togglingFocusGrabMode = true;
this.ungrabFocus();
this.grabFocus(this.actor);
this._togglingFocusGrabMode = false;
}
}
}
Signals.addSignalMethods(FocusGrabber.prototype);
// Notification:
// @source: the notification's Source
// @title: the title
// @banner: the banner text
// @params: optional additional params
//
// Creates a notification. In the banner mode, the notification
// will show an icon, @title (in bold) and @banner, all on a single
// line (with @banner ellipsized if necessary).
//
// The notification will be expandable if either it has additional
// elements that were added to it or if the @banner text did not
// fit fully in the banner mode. When the notification is expanded,
// the @banner text from the top line is always removed. The complete
// @banner text is added as the first element in the content section,
// unless 'customContent' parameter with the value 'true' is specified
// in @params.
//
// Additional notification content can be added with addActor() and
// addBody() methods. The notification content is put inside a
// scrollview, so if it gets too tall, the notification will scroll
// rather than continue to grow. In addition to this main content
// area, there is also a single-row action area, which is not
// scrolled and can contain a single actor. The action area can
// be set by calling setActionArea() method. There is also a
// convenience method addButton() for adding a button to the action
// area.
//
// @params can contain values for 'customContent', 'body', 'icon',
// 'titleMarkup', 'bannerMarkup', 'bodyMarkup', and 'clear'
// parameters.
//
// If @params contains a 'customContent' parameter with the value %true,
// then @banner will not be shown in the body of the notification when the
// notification is expanded and calls to update() will not clear the content
// unless 'clear' parameter with value %true is explicitly specified.
//
// If @params contains a 'body' parameter, then that text will be added to
// the content area (as with addBody()).
//
// By default, the icon shown is created by calling
// source.createNotificationIcon(). However, if @params contains an 'icon'
// parameter, the passed in icon will be used.
//
// If @params contains a 'titleMarkup', 'bannerMarkup', or
// 'bodyMarkup' parameter with the value %true, then the corresponding
// element is assumed to use pango markup. If the parameter is not
// present for an element, then anything that looks like markup in
// that element will appear literally in the output.
//
// If @params contains a 'clear' parameter with the value %true, then
// the content and the action area of the notification will be cleared.
// The content area is also always cleared if 'customContent' is false
// because it might contain the @banner that didn't fit in the banner mode.
function Notification(source, title, banner, params) {
this._init(source, title, banner, params);
}
Notification.prototype = {
IMAGE_SIZE: 125,
_init: function(source, title, banner, params) {
this.source = source;
this.title = title;
this.urgency = Urgency.NORMAL;
this.resident = false;
// 'transient' is a reserved keyword in JS, so we have to use an alternate variable name
this.isTransient = false;
this.expanded = false;
this._destroyed = false;
this._useActionIcons = false;
this._customContent = false;
this._bannerBodyText = null;
this._bannerBodyMarkup = false;
this._titleFitsInBannerMode = true;
this._inhibitTransparency = false;
this._titleDirection = St.TextDirection.NONE;
this._spacing = 0;
this._imageBin = null;
this._timestamp = new Date();
this._inNotificationBin = false;
this.enter_id = 0;
this.leave_id = 0;
source.connect('destroy', Lang.bind(this,
function (source, reason) {
this.destroy(reason);
}));
this.actor = new St.Button({ accessible_role: Atk.Role.NOTIFICATION });
this.actor._delegate = this;
this.actor._parent_container = null;
this.actor.connect('clicked', Lang.bind(this, this._onClicked));
this.actor.connect('destroy', Lang.bind(this, this._onDestroy));
this.updateFadeOnMouseover();
this._table = new St.Table({ name: 'notification',
reactive: true });
this._table.connect('style-changed', Lang.bind(this, this._styleChanged));
this.actor.set_child(this._table);
this._buttonFocusManager = St.FocusManager.get_for_stage(global.stage);
// The first line should have the title, followed by the
// banner text, but ellipsized if they won't both fit. We can't
// make St.Table or St.BoxLayout do this the way we want (don't
// show banner at all if title needs to be ellipsized), so we
// use Cinnamon.GenericContainer.
this._bannerBox = new Cinnamon.GenericContainer();
this._bannerBox.connect('get-preferred-width', Lang.bind(this, this._bannerBoxGetPreferredWidth));
this._bannerBox.connect('get-preferred-height', Lang.bind(this, this._bannerBoxGetPreferredHeight));
this._bannerBox.connect('allocate', Lang.bind(this, this._bannerBoxAllocate));
this._table.add(this._bannerBox, { row: 0,
col: 1,
col_span: 2,
x_expand: false,
y_expand: false,
y_fill: false });
// This is an empty cell that overlaps with this._bannerBox cell to ensure
// that this._bannerBox cell expands horizontally, while not forcing the
// this._imageBin that is also in col: 2 to expand horizontally.
this._table.add(new St.Bin(), { row: 0,
col: 2,
y_expand: false,
y_fill: false });
this._timeLabel = new St.Label();
this._titleLabel = new St.Label();
this._bannerBox.add_actor(this._titleLabel);
this._bannerBox.add_actor(this._timeLabel);
this._timeLabel.hide();
this._bannerUrlHighlighter = new URLHighlighter();
this._bannerLabel = this._bannerUrlHighlighter.actor;
this._bannerBox.add_actor(this._bannerLabel);
this.update(title, banner, params);
},
// update:
// @title: the new title
// @banner: the new banner
// @params: as in the Notification constructor
//
// Updates the notification by regenerating its icon and updating
// the title/banner. If @params.clear is %true, it will also
// remove any additional actors/action buttons previously added.
update: function(title, banner, params) {
this._timestamp = new Date();
this._inNotificationBin = false;
params = Params.parse(params, { customContent: false,
body: null,
icon: null,
titleMarkup: false,
bannerMarkup: false,
bodyMarkup: false,
clear: false });
this._customContent = params.customContent;
let oldFocus = global.stage.key_focus;
if (this._icon && (params.icon || params.clear)) {
this._icon.destroy();
this._icon = null;
}
// We always clear the content area if we don't have custom
// content because it might contain the @banner that didn't
// fit in the banner mode.
if (this._scrollArea && (!this._customContent || params.clear)) {
if (oldFocus && this._scrollArea.contains(oldFocus))
this.actor.grab_key_focus();
this._scrollArea.destroy();
this._scrollArea = null;
this._contentArea = null;
}
if (this._actionArea && params.clear) {
if (oldFocus && this._actionArea.contains(oldFocus))
this.actor.grab_key_focus();
this._actionArea.destroy();
this._actionArea = null;
this._buttonBox = null;
}
if (this._imageBin && params.clear)
this.unsetImage();
if (!this._scrollArea && !this._actionArea && !this._imageBin)
this._table.remove_style_class_name('multi-line-notification');
if (!this._icon) {
this._icon = params.icon || this.source.createNotificationIcon();
this._table.add(this._icon, { row: 0,
col: 0,
x_expand: false,
y_expand: false,
y_fill: false,
y_align: St.Align.START });
}
this.title = title;
title = title ? _fixMarkup(title.replace(/\n/g, ' '), params.titleMarkup) : '';
this._titleLabel.clutter_text.set_markup('<b>' + title + '</b>');
this._timeLabel.clutter_text.set_markup(this._timestamp.toLocaleTimeString());
this._timeLabel.hide();
if (Pango.find_base_dir(title, -1) == Pango.Direction.RTL)
this._titleDirection = St.TextDirection.RTL;
else
this._titleDirection = St.TextDirection.LTR;
// Let the title's text direction control the overall direction
// of the notification - in case where different scripts are used
// in the notification, this is the right thing for the icon, and
// arguably for action buttons as well. Labels other than the title
// will be allocated at the available width, so that their alignment
// is done correctly automatically.
this._table.set_direction(this._titleDirection);
// Unless the notification has custom content, we save this._bannerBodyText
// to add it to the content of the notification if the notification is
// expandable due to other elements in its content area or due to the banner
// not fitting fully in the single-line mode.
this._bannerBodyText = this._customContent ? null : banner;
this._bannerBodyMarkup = params.bannerMarkup;
banner = banner ? banner.replace(/\n/g, ' ') : '';
this._bannerUrlHighlighter.setMarkup(banner, params.bannerMarkup);
this._bannerLabel.queue_relayout();
// Add the bannerBody now if we know for sure we'll need it
if (this._bannerBodyText && this._bannerBodyText.indexOf('\n') > -1)
this._addBannerBody();
if (params.body)
this.addBody(params.body, params.bodyMarkup);
this._updated();
},
updateFadeOnMouseover: function() {
// Transparency on mouse over?
if (Main.messageTray.fadeOnMouseover && !this._inhibitTransparency) {
// Register to every notification as we intend to support multiple notifications on screen.
this.enter_id = this.actor.connect('enter-event', Lang.bind(this, function() {
Tweener.addTween(this.actor, {
opacity: ((Main.messageTray.fadeOpacity / 100) * 255).clamp(0, 255),
time: ANIMATION_TIME,
transition: 'easeOutQuad'
});
}));
this.leave_id = this.actor.connect('leave-event', Lang.bind(this, function() {
Tweener.addTween(this.actor, {
opacity: (this._table.get_theme_node().get_length('opacity') / global.ui_scale) || 255,
time: ANIMATION_TIME,
transition: 'easeOutQuad'
});
}));
} else {
if (this.enter_id > 0) {
this.actor.disconnect(this.enter_id);
this.enter_id = 0;
}
if (this.leave_id > 0) {
this.actor.disconnect(this.leave_id);
this.leave_id = 0;
}
}
},
setIconVisible: function(visible) {
this._icon.visible = visible;
},
_createScrollArea: function() {
this._table.add_style_class_name('multi-line-notification');
this._scrollArea = new St.ScrollView({ name: 'notification-scrollview',
vscrollbar_policy: Gtk.PolicyType.NEVER,
hscrollbar_policy: Gtk.PolicyType.NEVER,
style_class: 'vfade' });
this._table.add(this._scrollArea, { row: 1,
col: 2 });
this._updateLastColumnSettings();
this._contentArea = new St.BoxLayout({ name: 'notification-body',
vertical: true });
this._scrollArea.add_actor(this._contentArea);
// If we know the notification will be expandable, we need to add
// the banner text to the body as the first element.
this._addBannerBody();
},
// addActor:
// @actor: actor to add to the body of the notification
//
// Appends @actor to the notification's body
addActor: function(actor, style) {
if (!this._scrollArea) {
this._createScrollArea();
}
this._contentArea.add(actor, style ? style : {});
this._updated();
},
// addBody:
// @text: the text
// @markup: %true if @text contains pango markup
// @style: style to use when adding the actor containing the text
//
// Adds a multi-line label containing @text to the notification.
//
// Return value: the newly-added label
addBody: function(text, markup, style) {
let label = new URLHighlighter(text, true, markup);
this.addActor(label.actor, style);
return label.actor;
},
_addBannerBody: function() {
if (this._bannerBodyText) {
let text = this._bannerBodyText;
this._bannerBodyText = null;
this.addBody(text, this._bannerBodyMarkup);
}
},
// scrollTo:
// @side: St.Side.TOP or St.Side.BOTTOM
//
// Scrolls the content area (if scrollable) to the indicated edge
scrollTo: function(side) {
let adjustment = this._scrollArea.vscroll.adjustment;
if (side == St.Side.TOP)
adjustment.value = adjustment.lower;
else if (side == St.Side.BOTTOM)
adjustment.value = adjustment.upper;
},
// setActionArea:
// @actor: the actor
// @props: (option) St.Table child properties
//
// Puts @actor into the action area of the notification, replacing
// the previous contents
setActionArea: function(actor, props) {
if (this._actionArea) {
this._actionArea.destroy();
this._actionArea = null;
if (this._buttonBox)
this._buttonBox = null;
} else {
this._addBannerBody();
}
this._actionArea = actor;
if (!props)
props = {};
props.row = 2;
props.col = 2;
this._table.add_style_class_name('multi-line-notification');
this._table.add(this._actionArea, props);
this._updateLastColumnSettings();
this._updated();
},
_updateLastColumnSettings: function() {
if (this._scrollArea)
this._table.child_set(this._scrollArea, { col: this._imageBin ? 2 : 1,
col_span: this._imageBin ? 1 : 2 });
if (this._actionArea)
this._table.child_set(this._actionArea, { col: this._imageBin ? 2 : 1,
col_span: this._imageBin ? 1 : 2 });
},
setImage: function(image) {
if (this._imageBin)
this.unsetImage();
this._imageBin = new St.Bin();
this._imageBin.child = image;
this._imageBin.opacity = 230;
this._table.add_style_class_name('multi-line-notification');
this._table.add_style_class_name('notification-with-image');
this._addBannerBody();
this._updateLastColumnSettings();
this._table.add(this._imageBin, { row: 1,
col: 1,
row_span: 2,
x_expand: false,
y_expand: false,
x_fill: false,
y_fill: false });
},
unsetImage: function() {
if (this._imageBin) {
this._table.remove_style_class_name('notification-with-image');
this._table.remove_actor(this._imageBin);
this._imageBin = null;
this._updateLastColumnSettings();
if (!this._scrollArea && !this._actionArea)
this._table.remove_style_class_name('multi-line-notification');
}
},
// addButton:
// @id: the action ID
// @label: the label for the action's button
//
// Adds a button with the given @label to the notification. All
// action buttons will appear in a single row at the bottom of
// the notification.
//
// If the button is clicked, the notification will emit the
// %action-invoked signal with @id as a parameter
addButton: function(id, label) {
if (!this._buttonBox) {
let box = new St.BoxLayout({ name: 'notification-actions' });
this.setActionArea(box, { x_expand: true,
y_expand: false,
x_fill: true,
y_fill: false,
x_align: St.Align.START });
this._buttonBox = box;
}
let button = new St.Button({ can_focus: true });
if (this._useActionIcons && Gtk.IconTheme.get_default().has_icon(id)) {
button.add_style_class_name('notification-icon-button');
button.child = new St.Icon({ icon_name: id });
} else {
button.add_style_class_name('notification-button');
button.label = label;
}
if (this._buttonBox.get_n_children() > 0)
this._buttonFocusManager.remove_group(this._buttonBox);
this._buttonBox.add(button);
this._buttonFocusManager.add_group(this._buttonBox);
button.connect('clicked', Lang.bind(this, this._onActionInvoked, id));
this._inhibitTransparency = true;
this.updateFadeOnMouseover();
this._updated();
},
setUrgency: function(urgency) {
this.urgency = urgency;
},
setResident: function(resident) {
this.resident = resident;
},
setTransient: function(isTransient) {
this.isTransient = isTransient;
},
setUseActionIcons: function(useIcons) {
this._useActionIcons = useIcons;
},
_styleChanged: function() {
this._spacing = this._table.get_theme_node().get_length('spacing-columns');
},
_bannerBoxGetPreferredWidth: function(actor, forHeight, alloc) {
let [titleMin, titleNat] = this._titleLabel.get_preferred_width(forHeight);
let [bannerMin, bannerNat] = this._bannerLabel.get_preferred_width(forHeight);
let [timeMin, timeNat] = this._timeLabel.get_preferred_width(forHeight);
if (this._inNotificationBin) {
alloc.min_size = Math.max(titleMin, timeMin);
alloc.natural_size = Math.max(titleNat, timeNat) + this._spacing + bannerNat;
} else {
alloc.min_size = titleMin;
alloc.natural_size = titleNat + this._spacing + bannerNat;
}
},
_bannerBoxGetPreferredHeight: function(actor, forWidth, alloc) {
if (this._inNotificationBin) {
let [titleMin, titleNat] = this._titleLabel.get_preferred_height(forWidth);
let [timeMin, timeNat] = this._timeLabel.get_preferred_height(forWidth);
alloc.min_size = titleMin + timeMin;
alloc.natural_size = titleNat + timeNat;
} else {
[alloc.min_size, alloc.natural_size] =
this._titleLabel.get_preferred_height(forWidth);
}
},
_bannerBoxAllocate: function(actor, box, flags) {
let availWidth = box.x2 - box.x1;
let [titleMinW, titleNatW] = this._titleLabel.get_preferred_width(-1);
let [titleMinH, titleNatH] = this._titleLabel.get_preferred_height(availWidth);
let [timeMinW, timeNatW] = this._timeLabel.get_preferred_width(-1);
let [timeMinH, timeNatH] = this._timeLabel.get_preferred_height(availWidth);
let [bannerMinW, bannerNatW] = this._bannerLabel.get_preferred_width(availWidth);
let titleBox = new Clutter.ActorBox();
let timeBox = new Clutter.ActorBox();
let titleBoxW = Math.min(titleNatW, availWidth);
let timeBoxW = Math.min(timeNatW, availWidth);
if (this._titleDirection == St.TextDirection.RTL) {
titleBox.x1 = availWidth - titleBoxW;
titleBox.x2 = availWidth;
timeBox.x1 = availWidth - timeBoxW;
timeBox.x2 = availWidth;
} else {
titleBox.x1 = 0;
timeBox.x1 = 0;
titleBox.x2 = titleBoxW;
timeBox.x2 = timeBoxW;
}
if (this._inNotificationBin) {
timeBox.y1 = 0;
timeBox.y2 = timeNatH;
titleBox.y1 = timeNatH;
titleBox.y2 = timeNatH + titleNatH;
} else {
titleBox.y1 = 0;
titleBox.y2 = titleNatH;
}
this._titleLabel.allocate(titleBox, flags);
if (this._inNotificationBin) {
this._timeLabel.allocate(timeBox, flags);
}
this._titleFitsInBannerMode = (titleNatW <= availWidth);
let bannerFits = true;
if (titleBoxW + this._spacing > availWidth) {
this._bannerLabel.opacity = 0;
bannerFits = false;
} else {
let bannerBox = new Clutter.ActorBox();
if (this._titleDirection == St.TextDirection.RTL) {
bannerBox.x1 = 0;
bannerBox.x2 = titleBox.x1 - this._spacing;
bannerFits = (bannerBox.x2 - bannerNatW >= 0);
} else {
bannerBox.x1 = titleBox.x2 + this._spacing;
bannerBox.x2 = availWidth;
bannerFits = (bannerBox.x1 + bannerNatW <= availWidth);
}
if (this._inNotificationBin) {
bannerBox.y1 = timeNatH;
bannerBox.y2 = timeNatH + titleNatH;
} else {
bannerBox.y1 = 0;
bannerBox.y2 = titleNatH;
}
this._bannerLabel.allocate(bannerBox, flags);
// Make _bannerLabel visible if the entire notification
// fits on one line, or if the notification is currently
// unexpanded and only showing one line anyway.
if (!this.expanded || (bannerFits && this._table.row_count == 1))
this._bannerLabel.opacity = 255;
}
// If the banner doesn't fully fit in the banner box, we possibly need to add the
// banner to the body. We can't do that from here though since that will force a
// relayout, so we add it to the main loop.
if (!bannerFits && this._canExpandContent())
Meta.later_add(Meta.LaterType.BEFORE_REDRAW,
Lang.bind(this,
function() {
if (this._canExpandContent()) {
this._addBannerBody();
this._table.add_style_class_name('multi-line-notification');
this._updated();
}
return false;
}));
},
_canExpandContent: function() {
return this._bannerBodyText ||
(!this._titleFitsInBannerMode && !this._table.has_style_class_name('multi-line-notification'));
},
_updated: function() {
if (this.expanded)
this.expand(false);
},
expand: function(animate) {
this.expanded = true;
// The banner is never shown when the title did not fit, so this
// can be an if-else statement.
if (!this._titleFitsInBannerMode) {
// Remove ellipsization from the title label and make it wrap so that
// we show the full title when the notification is expanded.
this._titleLabel.clutter_text.line_wrap = true;
this._titleLabel.clutter_text.line_wrap_mode = Pango.WrapMode.WORD_CHAR;
this._titleLabel.clutter_text.ellipsize = Pango.EllipsizeMode.NONE;
} else if (this._table.row_count > 1 && this._bannerLabel.opacity != 0) {
// We always hide the banner if the notification has additional content.
//
// We don't need to wrap the banner that doesn't fit the way we wrap the
// title that doesn't fit because we won't have a notification with
// row_count=1 that has a banner that doesn't fully fit. We'll either add
// that banner to the content of the notification in _bannerBoxAllocate()
// or the notification will have custom content.
if (animate)
Tweener.addTween(this._bannerLabel,
{ opacity: 0,
time: ANIMATION_TIME,
transition: 'easeOutQuad' });
else
this._bannerLabel.opacity = 0;
}
this.emit('expanded');
},
collapseCompleted: function() {