2014-06-23 19:41:54 +04:00
|
|
|
//
|
|
|
|
// Copyright (C) 2014 Tom Beckmann
|
|
|
|
//
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
//
|
|
|
|
|
|
|
|
using Clutter;
|
|
|
|
using Meta;
|
|
|
|
|
|
|
|
namespace Gala.Plugins.Notify
|
|
|
|
{
|
2014-07-18 15:12:31 +04:00
|
|
|
public abstract class Notification : Actor
|
2014-06-23 19:41:54 +04:00
|
|
|
{
|
2014-07-18 15:12:31 +04:00
|
|
|
public const int WIDTH = 300;
|
|
|
|
public const int ICON_SIZE = 48;
|
|
|
|
public const int MARGIN = 12;
|
2014-06-23 19:41:54 +04:00
|
|
|
|
2014-07-18 15:12:31 +04:00
|
|
|
public const int SPACING = 6;
|
|
|
|
public const int PADDING = 4;
|
2014-06-23 19:41:54 +04:00
|
|
|
|
2014-07-21 06:00:32 +04:00
|
|
|
public signal void closed (uint32 id, uint32 reason);
|
|
|
|
|
2014-06-23 19:41:54 +04:00
|
|
|
public uint32 id { get; construct; }
|
|
|
|
public Gdk.Pixbuf? icon { get; construct set; }
|
|
|
|
public NotificationUrgency urgency { get; construct; }
|
|
|
|
public int32 expire_timeout { get; construct set; }
|
|
|
|
|
|
|
|
public uint64 relevancy_time { get; private set; }
|
2014-06-23 23:21:37 +04:00
|
|
|
public bool being_destroyed { get; private set; default = false; }
|
2014-06-23 19:41:54 +04:00
|
|
|
|
2014-07-18 15:35:01 +04:00
|
|
|
protected bool icon_only { get; protected set; default = false; }
|
2014-07-19 21:03:17 +04:00
|
|
|
protected GtkClutter.Texture icon_texture { get; private set; }
|
|
|
|
protected Actor icon_container { get; private set; }
|
2014-07-18 15:35:01 +04:00
|
|
|
|
2014-07-21 04:23:28 +04:00
|
|
|
/**
|
|
|
|
* Whether we're currently sliding content for an update animation
|
|
|
|
*/
|
|
|
|
protected bool transitioning { get; private set; default = false; }
|
|
|
|
|
2014-06-23 19:41:54 +04:00
|
|
|
GtkClutter.Texture close_button;
|
2014-07-19 21:03:17 +04:00
|
|
|
Granite.Drawing.BufferSurface? buffer = null;
|
2014-06-23 19:41:54 +04:00
|
|
|
|
|
|
|
uint remove_timeout = 0;
|
|
|
|
|
2014-07-21 04:23:28 +04:00
|
|
|
// temporary things needed for the slide transition
|
2014-07-21 05:21:19 +04:00
|
|
|
protected float animation_slide_height { get; private set; }
|
2014-07-21 04:23:28 +04:00
|
|
|
GtkClutter.Texture old_texture;
|
|
|
|
float _animation_slide_y_offset = 0.0f;
|
|
|
|
public float animation_slide_y_offset {
|
|
|
|
get {
|
|
|
|
return _animation_slide_y_offset;
|
|
|
|
}
|
|
|
|
set {
|
|
|
|
_animation_slide_y_offset = value;
|
|
|
|
|
2014-07-21 05:21:19 +04:00
|
|
|
icon_texture.y = -animation_slide_height + _animation_slide_y_offset;
|
2014-07-21 04:23:28 +04:00
|
|
|
old_texture.y = _animation_slide_y_offset;
|
|
|
|
|
|
|
|
update_slide_animation ();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-18 15:12:31 +04:00
|
|
|
public Notification (uint32 id, Gdk.Pixbuf? icon, NotificationUrgency urgency,
|
|
|
|
int32 expire_timeout)
|
2014-06-23 19:41:54 +04:00
|
|
|
{
|
|
|
|
Object (
|
|
|
|
id: id,
|
|
|
|
icon: icon,
|
|
|
|
urgency: urgency,
|
2014-07-18 15:12:31 +04:00
|
|
|
expire_timeout: expire_timeout
|
2014-06-23 19:41:54 +04:00
|
|
|
);
|
2014-07-19 13:37:14 +04:00
|
|
|
}
|
2014-06-23 19:41:54 +04:00
|
|
|
|
2014-07-19 13:37:14 +04:00
|
|
|
construct
|
|
|
|
{
|
2014-06-23 19:41:54 +04:00
|
|
|
relevancy_time = new DateTime.now_local ().to_unix ();
|
|
|
|
width = WIDTH + MARGIN * 2;
|
|
|
|
reactive = true;
|
2014-07-18 04:15:55 +04:00
|
|
|
set_pivot_point (0.5f, 0.5f);
|
2014-06-23 19:41:54 +04:00
|
|
|
|
|
|
|
icon_texture = new GtkClutter.Texture ();
|
2014-07-18 04:15:55 +04:00
|
|
|
icon_texture.set_pivot_point (0.5f, 0.5f);
|
2014-06-23 19:41:54 +04:00
|
|
|
|
2014-07-19 21:03:17 +04:00
|
|
|
icon_container = new Actor ();
|
|
|
|
icon_container.add_child (icon_texture);
|
|
|
|
|
2014-06-24 03:27:33 +04:00
|
|
|
close_button = Utils.create_close_button ();
|
2014-06-23 19:41:54 +04:00
|
|
|
close_button.opacity = 0;
|
|
|
|
close_button.reactive = true;
|
|
|
|
close_button.set_easing_duration (300);
|
|
|
|
|
2014-07-21 06:00:32 +04:00
|
|
|
var close_click = new ClickAction ();
|
|
|
|
close_click.clicked.connect (() => {
|
|
|
|
closed (id, NotificationClosedReason.DISMISSED);
|
|
|
|
close ();
|
|
|
|
});
|
|
|
|
close_button.add_action (close_click);
|
|
|
|
|
2014-07-19 21:03:17 +04:00
|
|
|
add_child (icon_container);
|
2014-06-23 19:41:54 +04:00
|
|
|
add_child (close_button);
|
|
|
|
|
|
|
|
var canvas = new Canvas ();
|
|
|
|
canvas.draw.connect (draw);
|
|
|
|
content = canvas;
|
|
|
|
|
|
|
|
set_values ();
|
|
|
|
|
2014-06-24 03:27:33 +04:00
|
|
|
var click = new ClickAction ();
|
|
|
|
click.clicked.connect (() => {
|
2014-07-18 15:12:31 +04:00
|
|
|
activate ();
|
2014-06-24 03:27:33 +04:00
|
|
|
});
|
|
|
|
add_action (click);
|
2014-07-18 04:15:55 +04:00
|
|
|
|
|
|
|
open ();
|
|
|
|
}
|
|
|
|
|
|
|
|
public void open () {
|
|
|
|
var entry = new TransitionGroup ();
|
|
|
|
entry.remove_on_complete = true;
|
2014-07-19 05:30:46 +04:00
|
|
|
entry.duration = 400;
|
2014-07-18 04:15:55 +04:00
|
|
|
|
|
|
|
var opacity_transition = new PropertyTransition ("opacity");
|
|
|
|
opacity_transition.set_from_value (0);
|
|
|
|
opacity_transition.set_to_value (255);
|
|
|
|
|
|
|
|
var flip_transition = new KeyframeTransition ("rotation-angle-x");
|
2014-07-19 05:30:46 +04:00
|
|
|
flip_transition.set_from_value (90.0);
|
2014-07-18 04:15:55 +04:00
|
|
|
flip_transition.set_to_value (0.0);
|
|
|
|
flip_transition.set_key_frames ({ 0.6 });
|
2014-07-19 05:30:46 +04:00
|
|
|
flip_transition.set_values ({ -10.0 });
|
2014-07-18 04:15:55 +04:00
|
|
|
|
|
|
|
entry.add_transition (opacity_transition);
|
|
|
|
entry.add_transition (flip_transition);
|
|
|
|
add_transition ("entry", entry);
|
|
|
|
|
|
|
|
switch (urgency) {
|
|
|
|
case NotificationUrgency.LOW:
|
|
|
|
case NotificationUrgency.NORMAL:
|
|
|
|
return;
|
|
|
|
case NotificationUrgency.CRITICAL:
|
|
|
|
var icon_entry = new TransitionGroup ();
|
|
|
|
icon_entry.duration = 1000;
|
|
|
|
icon_entry.remove_on_complete = true;
|
|
|
|
icon_entry.progress_mode = AnimationMode.EASE_IN_OUT_CUBIC;
|
|
|
|
|
|
|
|
double[] keyframes = { 0.2, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0 };
|
|
|
|
GLib.Value[] scale = { 0.0, 1.2, 1.6, 1.6, 1.6, 1.6, 1.2, 1.0 };
|
|
|
|
|
|
|
|
var rotate_transition = new KeyframeTransition ("rotation-angle-z");
|
|
|
|
rotate_transition.set_from_value (30.0);
|
|
|
|
rotate_transition.set_to_value (0.0);
|
|
|
|
rotate_transition.set_key_frames (keyframes);
|
|
|
|
rotate_transition.set_values ({ 30.0, -30.0, 30.0, -20.0, 10.0, -5.0, 2.0, 0.0 });
|
|
|
|
|
|
|
|
var scale_x_transition = new KeyframeTransition ("scale-x");
|
|
|
|
scale_x_transition.set_from_value (0.0);
|
|
|
|
scale_x_transition.set_to_value (1.0);
|
|
|
|
scale_x_transition.set_key_frames (keyframes);
|
|
|
|
scale_x_transition.set_values (scale);
|
|
|
|
|
|
|
|
var scale_y_transition = new KeyframeTransition ("scale-y");
|
|
|
|
scale_y_transition.set_from_value (0.0);
|
|
|
|
scale_y_transition.set_to_value (1.0);
|
|
|
|
scale_y_transition.set_key_frames (keyframes);
|
|
|
|
scale_y_transition.set_values (scale);
|
|
|
|
|
|
|
|
icon_entry.add_transition (rotate_transition);
|
|
|
|
icon_entry.add_transition (scale_x_transition);
|
|
|
|
icon_entry.add_transition (scale_y_transition);
|
|
|
|
|
|
|
|
icon_texture.add_transition ("entry", icon_entry);
|
|
|
|
return;
|
|
|
|
}
|
2014-06-23 19:41:54 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
public void close ()
|
|
|
|
{
|
2014-07-22 22:33:33 +04:00
|
|
|
set_easing_duration (100);
|
2014-06-28 03:47:13 +04:00
|
|
|
|
|
|
|
set_easing_mode (AnimationMode.EASE_IN_QUAD);
|
2014-06-23 19:41:54 +04:00
|
|
|
opacity = 0;
|
|
|
|
|
2014-06-28 03:47:13 +04:00
|
|
|
x = WIDTH + MARGIN * 2;
|
|
|
|
|
2014-06-23 23:21:37 +04:00
|
|
|
being_destroyed = true;
|
2014-06-28 03:47:13 +04:00
|
|
|
var transition = get_transition ("x");
|
|
|
|
if (transition != null)
|
|
|
|
transition.completed.connect (() => destroy ());
|
|
|
|
else
|
|
|
|
destroy ();
|
2014-06-23 19:41:54 +04:00
|
|
|
}
|
|
|
|
|
2014-07-19 21:03:17 +04:00
|
|
|
protected void update_base (Gdk.Pixbuf? icon, int32 expire_timeout)
|
2014-06-23 19:41:54 +04:00
|
|
|
{
|
|
|
|
this.icon = icon;
|
|
|
|
this.expire_timeout = expire_timeout;
|
|
|
|
this.relevancy_time = new DateTime.now_local ().to_unix ();
|
|
|
|
|
|
|
|
set_values ();
|
|
|
|
}
|
|
|
|
|
|
|
|
void set_values ()
|
|
|
|
{
|
|
|
|
if (icon != null) {
|
|
|
|
try {
|
|
|
|
icon_texture.set_from_pixbuf (icon);
|
|
|
|
} catch (Error e) {}
|
|
|
|
}
|
|
|
|
|
|
|
|
set_timeout ();
|
|
|
|
}
|
|
|
|
|
|
|
|
void set_timeout ()
|
|
|
|
{
|
|
|
|
// crtitical notifications have to be dismissed manually
|
2014-07-19 13:37:14 +04:00
|
|
|
if (expire_timeout <= 0 || urgency == NotificationUrgency.CRITICAL)
|
2014-06-23 19:41:54 +04:00
|
|
|
return;
|
|
|
|
|
|
|
|
clear_timeout ();
|
|
|
|
|
|
|
|
remove_timeout = Timeout.add (expire_timeout, () => {
|
2014-07-21 06:00:32 +04:00
|
|
|
closed (id, NotificationClosedReason.EXPIRED);
|
2014-06-23 19:41:54 +04:00
|
|
|
close ();
|
|
|
|
remove_timeout = 0;
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
void clear_timeout ()
|
|
|
|
{
|
|
|
|
if (remove_timeout != 0) {
|
|
|
|
Source.remove (remove_timeout);
|
|
|
|
remove_timeout = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public override bool enter_event (CrossingEvent event)
|
|
|
|
{
|
|
|
|
close_button.opacity = 255;
|
|
|
|
|
|
|
|
clear_timeout ();
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
public override bool leave_event (CrossingEvent event)
|
|
|
|
{
|
|
|
|
close_button.opacity = 0;
|
|
|
|
|
|
|
|
// TODO consider decreasing the timeout now or calculating the remaining
|
|
|
|
set_timeout ();
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-07-18 15:12:31 +04:00
|
|
|
public virtual void activate ()
|
2014-06-23 19:41:54 +04:00
|
|
|
{
|
2014-07-18 15:12:31 +04:00
|
|
|
}
|
2014-06-23 19:41:54 +04:00
|
|
|
|
2014-07-18 15:12:31 +04:00
|
|
|
public virtual void draw_content (Cairo.Context cr)
|
|
|
|
{
|
|
|
|
}
|
2014-06-23 19:41:54 +04:00
|
|
|
|
2014-07-18 15:12:31 +04:00
|
|
|
public abstract void update_allocation (out float content_height, AllocationFlags flags);
|
2014-06-23 19:41:54 +04:00
|
|
|
|
2014-07-18 15:12:31 +04:00
|
|
|
public override void allocate (ActorBox box, AllocationFlags flags)
|
|
|
|
{
|
2014-06-23 19:41:54 +04:00
|
|
|
var icon_alloc = ActorBox ();
|
2014-07-18 15:35:01 +04:00
|
|
|
|
|
|
|
icon_alloc.set_origin (icon_only ? (WIDTH - ICON_SIZE) / 2 : MARGIN + PADDING, MARGIN + PADDING);
|
2014-06-23 19:41:54 +04:00
|
|
|
icon_alloc.set_size (ICON_SIZE, ICON_SIZE);
|
2014-07-19 21:03:17 +04:00
|
|
|
icon_container.allocate (icon_alloc, flags);
|
2014-06-23 19:41:54 +04:00
|
|
|
|
|
|
|
var close_alloc = ActorBox ();
|
|
|
|
close_alloc.set_origin (MARGIN + PADDING - close_button.width / 2,
|
|
|
|
MARGIN + PADDING - close_button.height / 2);
|
|
|
|
close_alloc.set_size (close_button.width, close_button.height);
|
|
|
|
close_button.allocate (close_alloc, flags);
|
|
|
|
|
2014-07-18 15:12:31 +04:00
|
|
|
float content_height;
|
|
|
|
update_allocation (out content_height, flags);
|
2014-06-23 19:41:54 +04:00
|
|
|
box.set_size (MARGIN * 2 + WIDTH, (MARGIN + PADDING) * 2 + content_height);
|
|
|
|
|
|
|
|
base.allocate (box, flags);
|
|
|
|
|
|
|
|
var canvas = (Canvas) content;
|
|
|
|
var canvas_width = (int) box.get_width ();
|
|
|
|
var canvas_height = (int) box.get_height ();
|
2014-07-21 05:30:06 +04:00
|
|
|
if (canvas.width != canvas_width || canvas.height != canvas_height)
|
2014-06-23 19:41:54 +04:00
|
|
|
canvas.set_size (canvas_width, canvas_height);
|
|
|
|
}
|
|
|
|
|
|
|
|
public override void get_preferred_height (float for_width, out float min_height, out float nat_height)
|
|
|
|
{
|
2014-07-18 15:12:31 +04:00
|
|
|
min_height = nat_height = ICON_SIZE + (MARGIN + PADDING) * 2;
|
2014-06-23 19:41:54 +04:00
|
|
|
}
|
|
|
|
|
2014-07-21 05:21:19 +04:00
|
|
|
protected void play_update_transition (float slide_height)
|
2014-07-21 04:23:28 +04:00
|
|
|
{
|
|
|
|
Transition transition;
|
|
|
|
if ((transition = get_transition ("switch")) != null) {
|
|
|
|
transition.completed ();
|
|
|
|
remove_transition ("switch");
|
|
|
|
}
|
|
|
|
|
2014-07-21 05:21:19 +04:00
|
|
|
animation_slide_height = slide_height;
|
|
|
|
|
2014-07-21 04:23:28 +04:00
|
|
|
old_texture = new GtkClutter.Texture ();
|
|
|
|
icon_container.add_child (old_texture);
|
|
|
|
icon_container.set_clip (0, -PADDING, ICON_SIZE, ICON_SIZE + PADDING * 2);
|
|
|
|
|
|
|
|
try {
|
|
|
|
old_texture.set_from_pixbuf (this.icon);
|
|
|
|
} catch (Error e) {}
|
|
|
|
|
|
|
|
transition = new PropertyTransition ("animation-slide-y-offset");
|
|
|
|
transition.duration = 200;
|
|
|
|
transition.progress_mode = AnimationMode.EASE_IN_OUT_QUAD;
|
|
|
|
transition.set_from_value (0.0f);
|
2014-07-21 05:21:19 +04:00
|
|
|
transition.set_to_value (animation_slide_height);
|
2014-07-21 04:23:28 +04:00
|
|
|
transition.remove_on_complete = true;
|
|
|
|
|
|
|
|
transition.completed.connect (() => {
|
|
|
|
old_texture.destroy ();
|
|
|
|
icon_container.remove_clip ();
|
|
|
|
_animation_slide_y_offset = 0;
|
|
|
|
transitioning = false;
|
|
|
|
});
|
|
|
|
|
|
|
|
add_transition ("switch", transition);
|
|
|
|
transitioning = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected virtual void update_slide_animation ()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2014-06-23 19:41:54 +04:00
|
|
|
bool draw (Cairo.Context canvas_cr)
|
|
|
|
{
|
|
|
|
var canvas = (Canvas) content;
|
|
|
|
|
|
|
|
var x = MARGIN;
|
|
|
|
var y = MARGIN;
|
|
|
|
var width = canvas.width - MARGIN * 2;
|
|
|
|
var height = canvas.height - MARGIN * 2;
|
|
|
|
|
2014-07-19 21:03:17 +04:00
|
|
|
if (buffer == null || buffer.width != canvas.width || buffer.height != canvas.height) {
|
|
|
|
buffer = new Granite.Drawing.BufferSurface (canvas.width, canvas.height);
|
|
|
|
var cr = buffer.context;
|
|
|
|
|
|
|
|
Granite.Drawing.Utilities.cairo_rounded_rectangle (cr, x , y + 3, width, height, 4);
|
|
|
|
cr.set_source_rgba (0, 0, 0, 0.3);
|
|
|
|
cr.fill ();
|
|
|
|
buffer.exponential_blur (6);
|
|
|
|
|
|
|
|
Granite.Drawing.Utilities.cairo_rounded_rectangle (cr, x - 0.5 , y - 0.5, width + 1, height + 1, 4);
|
|
|
|
cr.set_source_rgba (0, 0, 0, 0.3);
|
|
|
|
cr.set_line_width (1);
|
|
|
|
cr.stroke ();
|
|
|
|
|
|
|
|
Granite.Drawing.Utilities.cairo_rounded_rectangle (cr, x, y, width, height, 4);
|
|
|
|
cr.set_source_rgb (0.945, 0.945, 0.945);
|
|
|
|
cr.fill ();
|
|
|
|
|
|
|
|
Granite.Drawing.Utilities.cairo_rounded_rectangle (cr, x + 0.5, y + 0.5, width - 1, height - 1, 3);
|
|
|
|
var gradient = new Cairo.Pattern.linear (0, 0, 0, height - 2);
|
|
|
|
gradient.add_color_stop_rgba (0, 1, 1, 1, 1.0);
|
|
|
|
gradient.add_color_stop_rgba (1, 1, 1, 1, 0.6);
|
|
|
|
cr.set_source (gradient);
|
|
|
|
cr.set_line_width (1);
|
|
|
|
cr.stroke ();
|
|
|
|
}
|
2014-07-18 15:12:31 +04:00
|
|
|
|
2014-06-23 19:41:54 +04:00
|
|
|
canvas_cr.set_operator (Cairo.Operator.CLEAR);
|
|
|
|
canvas_cr.paint ();
|
|
|
|
canvas_cr.set_operator (Cairo.Operator.OVER);
|
|
|
|
|
|
|
|
canvas_cr.set_source_surface (buffer.surface, 0, 0);
|
|
|
|
canvas_cr.paint ();
|
|
|
|
|
2014-07-19 21:03:17 +04:00
|
|
|
draw_content (canvas_cr);
|
|
|
|
|
2014-06-23 19:41:54 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2014-07-22 22:18:54 +04:00
|
|
|
}
|