[gnome-shell/wip/fmuellner/notification-redux: 28/82] calendar: Strip down Notification
- From: Florian Müllner <fmuellner src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gnome-shell/wip/fmuellner/notification-redux: 28/82] calendar: Strip down Notification
- Date: Sat, 14 Feb 2015 10:08:42 +0000 (UTC)
commit 5a0551a1b0de6cd26d9caaa9019d54015240e1f0
Author: Florian Müllner <fmuellner gnome org>
Date: Tue Feb 10 19:00:16 2015 +0100
calendar: Strip down Notification
The notification object is now a pure data object that can be shared
between different UI representations.
js/ui/calendar.js | 634 +++--------------------------------------------------
1 files changed, 26 insertions(+), 608 deletions(-)
---
diff --git a/js/ui/calendar.js b/js/ui/calendar.js
index b95d4ab..be67975 100644
--- a/js/ui/calendar.js
+++ b/js/ui/calendar.js
@@ -734,36 +734,10 @@ Signals.addSignalMethods(Source.prototype);
// 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.
-//
-// 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.
-//
// By default, the icon shown is the same as the source's.
// However, if @params contains a 'gicon' parameter, the passed in gicon
// will be used.
//
-// You can add a secondary icon to the banner with 'secondaryGIcon'. There
-// is no fallback for this icon.
-//
// If @params contains 'bannerMarkup', 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
@@ -780,196 +754,34 @@ Signals.addSignalMethods(Source.prototype);
const Notification = new Lang.Class({
Name: 'Notification',
- ICON_SIZE: 24,
-
- IMAGE_SIZE: 125,
-
- _init: function(source, title, banner, params) {
+ _init: function(source, title, body, params) {
this.source = source;
- this.title = title;
- this.urgency = Urgency.NORMAL;
+ this.icon = null;
+ this.priority = Gio.NotificationPriority.NORMAL;
this.resident = false;
// 'transient' is a reserved keyword in JS, so we have to use an alternate variable name
this.isTransient = false;
- this.isMusic = false;
this.forFeedback = false;
- this.expanded = false;
- this.focused = false;
this.acknowledged = false;
- this._destroyed = false;
- this._customContent = false;
- this.bannerBodyText = null;
- this.bannerBodyMarkup = false;
- this._bannerBodyAdded = false;
- this._titleFitsInBannerMode = true;
- this._spacing = 0;
- this._scrollPolicy = Gtk.PolicyType.AUTOMATIC;
- this._imageBin = null;
+ this.buttons = [];
this._soundName = null;
this._soundFile = null;
this._soundPlayed = false;
+ this._defaultAction = null;
- this.actor = new St.Button({ accessible_role: Atk.Role.NOTIFICATION });
- this.actor.add_style_class_name('notification-unexpanded');
- this.actor._delegate = this;
- this.actor.connect('clicked', Lang.bind(this, this._onClicked));
- this.actor.connect('destroy', Lang.bind(this, this._onDestroy));
-
- this._table = new St.Table({ style_class: 'notification',
- reactive: true });
- this._table.connect('style-changed', Lang.bind(this, this._styleChanged));
- this.actor.set_child(this._table);
-
- // 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 Shell.GenericContainer.
- this._bannerBox = new Shell.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._titleLabel = new St.Label();
- this._bannerBox.add_actor(this._titleLabel);
- this._bannerUrlHighlighter = new URLHighlighter();
- this._bannerLabel = this._bannerUrlHighlighter.actor;
- this._bannerBox.add_actor(this._bannerLabel);
-
- // If called with only one argument we assume the caller
- // will call .update() later on. This is the case of
- // NotificationDaemon, which wants to use the same code
- // for new and updated notifications
- if (arguments.length != 1)
- this.update(title, banner, params);
+ this._updateProperties(title, body, 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) {
- params = Params.parse(params, { customContent: false,
- gicon: null,
- secondaryGIcon: null,
- bannerMarkup: false,
- clear: false,
- soundName: null,
- soundFile: null });
-
- this._customContent = params.customContent;
-
- let oldFocus = global.stage.key_focus;
-
- if (this._icon && (params.gicon || params.clear)) {
- this._icon.destroy();
- this._icon = null;
- }
-
- if (this._secondaryIcon && (params.secondaryGIcon || params.clear)) {
- this._secondaryIcon.destroy();
- this._secondaryIcon = 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 (params.clear)
- this.unsetImage();
-
- if (!this._scrollArea && !this._actionArea && !this._imageBin)
- this._table.remove_style_class_name('multi-line-notification');
-
- if (params.gicon) {
- this._icon = new St.Icon({ gicon: params.gicon,
- icon_size: this.ICON_SIZE });
- } else {
- this._icon = this.source.createIcon(this.ICON_SIZE);
- }
-
- if (this._icon) {
- this._table.add(this._icon, { row: 0,
- col: 0,
- x_expand: false,
- y_expand: false,
- y_fill: false,
- y_align: St.Align.START });
- }
-
- if (params.secondaryGIcon) {
- this._secondaryIcon = new St.Icon({ gicon: params.secondaryGIcon,
- style_class: 'secondary-icon' });
- this._bannerBox.add_actor(this._secondaryIcon);
- }
-
+ _updateProperties: function(title, body, params) {
this.title = title;
- title = title ? _fixMarkup(title.replace(/\n/g, ' '), false) : '';
- this._titleLabel.clutter_text.set_markup('<b>' + title + '</b>');
-
- let titleDirection;
- if (Pango.find_base_dir(title, -1) == Pango.Direction.RTL)
- titleDirection = Clutter.TextDirection.RTL;
- else
- titleDirection = Clutter.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_text_direction(titleDirection);
+ this.body = body;
- // 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;
- this._bannerBodyAdded = false;
-
- banner = banner ? banner.replace(/\n/g, ' ') : '';
-
- this._bannerUrlHighlighter.setMarkup(banner, params.bannerMarkup);
- this._bannerLabel.queue_relayout();
+ params = Params.parse(params, { gicon: null,
+ soundName: null,
+ soundFile: null });
- // 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.gicon)
+ this.icon = params.gicon;
if (this._soundName != params.soundName ||
this._soundFile != params.soundFile) {
@@ -977,205 +789,27 @@ const Notification = new Lang.Class({
this._soundFile = params.soundFile;
this._soundPlayed = false;
}
-
- this.updated();
- },
-
- setIconVisible: function(visible) {
- this._icon.visible = visible;
- },
-
- enableScrolling: function(enableScrolling) {
- this._scrollPolicy = enableScrolling ? Gtk.PolicyType.AUTOMATIC : Gtk.PolicyType.NEVER;
- if (this._scrollArea) {
- this._scrollArea.vscrollbar_policy = this._scrollPolicy;
- this._scrollArea.enable_mouse_scrolling = enableScrolling;
- }
- },
-
- _createScrollArea: function() {
- this._table.add_style_class_name('multi-line-notification');
- this._scrollArea = new St.ScrollView({ style_class: 'notification-scrollview',
- vscrollbar_policy: this._scrollPolicy,
- hscrollbar_policy: Gtk.PolicyType.NEVER,
- visible: this.expanded });
- this._table.add(this._scrollArea, { row: 1,
- col: 2 });
- this._updateLastColumnSettings();
- this._contentArea = new St.BoxLayout({ style_class: '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 && !this._bannerBodyAdded) {
- this._bannerBodyAdded = true;
- this.addBody(this.bannerBodyText, 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;
- this._actionArea.visible = this.expanded;
-
- 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) {
- this.unsetImage();
-
- if (!image)
- return;
-
- this._imageBin = new St.Bin({ opacity: 230,
- child: image,
- visible: this.expanded });
-
- 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');
- }
+ // update:
+ // @title: the new title
+ // @banner: the new banner
+ // @params: as in the Notification constructor
+ update: function(title, body, params) {
+ this._updateProperties(title, body, params);
+ this.emit('updated');
},
addButton: function(button, callback) {
- if (!this._buttonBox) {
- let box = new St.BoxLayout({ style_class: 'notification-actions' });
- this.setActionArea(box, { x_expand: false,
- y_expand: false,
- x_fill: false,
- y_fill: false,
- x_align: St.Align.END });
- this._buttonBox = box;
- global.focus_manager.add_group(this._buttonBox);
- }
-
- this._buttonBox.add(button);
- button.connect('clicked', Lang.bind(this, function() {
- callback();
-
- if (!this.resident) {
- // We don't hide a resident notification when the user invokes one of its actions,
- // because it is common for such notifications to update themselves with new
- // information based on the action. We'd like to display the updated information
- // in place, rather than pop-up a new notification.
- this.emit('done-displaying');
- this.destroy();
- }
- }));
-
- this.updated();
- return button;
+ this.buttons.push({ label: button, callback: callback });
},
- // addAction:
- // @label: the label for the action's button
- // @callback: the callback for the action
- //
- // 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.
- addAction: function(label, callback) {
- let button = new St.Button({ style_class: 'notification-button',
- label: label,
- can_focus: true });
-
- return this.addButton(button, callback);
+ setDefaultAction(callback) {
+ this._defaultAction = callback;
},
- setUrgency: function(urgency) {
- this.urgency = urgency;
+ setPriority: function(priority) {
+ this.priority = priority;
},
setResident: function(resident) {
@@ -1190,132 +824,6 @@ const Notification = new Lang.Class({
this.forFeedback = forFeedback;
},
- _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);
-
- if (this._secondaryIcon) {
- let [secondaryIconMin, secondaryIconNat] = this._secondaryIcon.get_preferred_width(forHeight);
-
- alloc.min_size = secondaryIconMin + this._spacing + titleMin;
- alloc.natural_size = secondaryIconNat + this._spacing + titleNat + this._spacing + bannerNat;
- } else {
- alloc.min_size = titleMin;
- alloc.natural_size = titleNat + this._spacing + bannerNat;
- }
- },
-
- _bannerBoxGetPreferredHeight: function(actor, forWidth, alloc) {
- [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 [bannerMinW, bannerNatW] = this._bannerLabel.get_preferred_width(availWidth);
-
- let rtl = (this._table.text_direction == Clutter.TextDirection.RTL);
- let x = rtl ? availWidth : 0;
-
- if (this._secondaryIcon) {
- let [iconMinW, iconNatW] = this._secondaryIcon.get_preferred_width(-1);
- let [iconMinH, iconNatH] = this._secondaryIcon.get_preferred_height(availWidth);
-
- let secondaryIconBox = new Clutter.ActorBox();
- let secondaryIconBoxW = Math.min(iconNatW, availWidth);
-
- // allocate secondary icon box
- if (rtl) {
- secondaryIconBox.x1 = x - secondaryIconBoxW;
- secondaryIconBox.x2 = x;
- x = x - (secondaryIconBoxW + this._spacing);
- } else {
- secondaryIconBox.x1 = x;
- secondaryIconBox.x2 = x + secondaryIconBoxW;
- x = x + secondaryIconBoxW + this._spacing;
- }
- secondaryIconBox.y1 = 0;
- // Using titleNatH ensures that the secondary icon is centered vertically
- secondaryIconBox.y2 = titleNatH;
-
- availWidth = availWidth - (secondaryIconBoxW + this._spacing);
- this._secondaryIcon.allocate(secondaryIconBox, flags);
- }
-
- let titleBox = new Clutter.ActorBox();
- let titleBoxW = Math.min(titleNatW, availWidth);
- if (rtl) {
- titleBox.x1 = availWidth - titleBoxW;
- titleBox.x2 = availWidth;
- } else {
- titleBox.x1 = x;
- titleBox.x2 = titleBox.x1 + titleBoxW;
- }
- titleBox.y1 = 0;
- titleBox.y2 = titleNatH;
- this._titleLabel.allocate(titleBox, 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 (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);
- }
- 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._destroyed)
- return false;
-
- if (this._canExpandContent()) {
- this._addBannerBody();
- this._table.add_style_class_name('multi-line-notification');
- this.updated();
- }
- return false;
- }));
- },
-
- _canExpandContent: function() {
- return (this.bannerBodyText && !this._bannerBodyAdded) ||
- (!this._titleFitsInBannerMode &&
!this._table.has_style_class_name('multi-line-notification'));
- },
-
playSound: function() {
if (this._soundPlayed)
return;
@@ -1348,98 +856,8 @@ const Notification = new Lang.Class({
}
},
- updated: function() {
- if (this.expanded)
- this.expand(false);
- },
-
- expand: function(animate) {
- this.expanded = true;
- this.actor.remove_style_class_name('notification-unexpanded');
-
- // Show additional content that we keep hidden in banner mode
- if (this._imageBin)
- this._imageBin.show();
- if (this._actionArea)
- this._actionArea.show();
- if (this._scrollArea)
- this._scrollArea.show();
-
- // 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() {
- if (this._destroyed)
- return;
- this.expanded = false;
-
- // Hide additional content that we keep hidden in banner mode
- if (this._imageBin)
- this._imageBin.hide();
- if (this._actionArea)
- this._actionArea.hide();
- if (this._scrollArea)
- this._scrollArea.hide();
-
- // Make sure we don't line wrap the title, and ellipsize it instead.
- this._titleLabel.clutter_text.line_wrap = false;
- this._titleLabel.clutter_text.ellipsize = Pango.EllipsizeMode.END;
-
- // Restore banner opacity in case the notification is shown in the
- // banner mode again on update.
- this._bannerLabel.opacity = 255;
-
- // Restore height requisition
- this.actor.add_style_class_name('notification-unexpanded');
- },
-
- _onClicked: function() {
- this.emit('clicked');
- // We hide all types of notifications once the user clicks on them because the common
- // outcome of clicking should be the relevant window being brought forward and the user's
- // attention switching to the window.
- this.emit('done-displaying');
- if (!this.resident)
- this.destroy();
- },
-
- _onDestroy: function() {
- if (this._destroyed)
- return;
- this._destroyed = true;
- if (!this._destroyedReason)
- this._destroyedReason = NotificationDestroyedReason.DISMISSED;
- this.emit('destroy', this._destroyedReason);
- },
-
destroy: function(reason) {
- this._destroyedReason = reason;
- this.actor.destroy();
- this.actor._delegate = null;
+ this.emit('destroy', reason);
}
});
Signals.addSignalMethods(Notification.prototype);
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]