1 /*
2  * Copyright © 2012 Red Hat Inc.
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library. If not, see <http://www.gnu.org/licenses/>.
16  *
17  * Authors: Benjamin Otte <otte@gnome.org>
18  */
19 
20 #include "config.h"
21 
22 #include "gtkcsstransitionprivate.h"
23 
24 #include "gtkcsseasevalueprivate.h"
25 #include "gtkprogresstrackerprivate.h"
26 
G_DEFINE_TYPE(GtkCssTransition,_gtk_css_transition,GTK_TYPE_STYLE_ANIMATION)27 G_DEFINE_TYPE (GtkCssTransition, _gtk_css_transition, GTK_TYPE_STYLE_ANIMATION)
28 
29 static GtkStyleAnimation *
30 gtk_css_transition_advance (GtkStyleAnimation    *style_animation,
31                            gint64                timestamp)
32 {
33   GtkCssTransition *source = GTK_CSS_TRANSITION (style_animation);
34 
35   GtkCssTransition *transition;
36 
37   transition = g_object_new (GTK_TYPE_CSS_TRANSITION, NULL);
38 
39   transition->property = source->property;
40   transition->start = _gtk_css_value_ref (source->start);
41   transition->ease = _gtk_css_value_ref (source->ease);
42 
43   gtk_progress_tracker_init_copy (&source->tracker, &transition->tracker);
44   gtk_progress_tracker_advance_frame (&transition->tracker, timestamp);
45 
46   return GTK_STYLE_ANIMATION (transition);
47 }
48 
49 static void
gtk_css_transition_apply_values(GtkStyleAnimation * style_animation,GtkCssAnimatedStyle * style)50 gtk_css_transition_apply_values (GtkStyleAnimation   *style_animation,
51                                  GtkCssAnimatedStyle *style)
52 {
53   GtkCssTransition *transition = GTK_CSS_TRANSITION (style_animation);
54   GtkCssValue *value, *end;
55   double progress;
56   GtkProgressState state;
57 
58   end = gtk_css_animated_style_get_intrinsic_value (style, transition->property);
59 
60   state = gtk_progress_tracker_get_state (&transition->tracker);
61 
62   if (state == GTK_PROGRESS_STATE_BEFORE)
63     value = _gtk_css_value_ref (transition->start);
64   else if (state == GTK_PROGRESS_STATE_DURING)
65     {
66       progress = gtk_progress_tracker_get_progress (&transition->tracker, FALSE);
67       progress = _gtk_css_ease_value_transform (transition->ease, progress);
68 
69       value = _gtk_css_value_transition (transition->start,
70                                          end,
71                                          transition->property,
72                                          progress);
73     }
74   else
75     return;
76 
77   if (value == NULL)
78     value = _gtk_css_value_ref (end);
79 
80   gtk_css_animated_style_set_animated_value (style, transition->property, value);
81   _gtk_css_value_unref (value);
82 }
83 
84 static gboolean
gtk_css_transition_is_finished(GtkStyleAnimation * animation)85 gtk_css_transition_is_finished (GtkStyleAnimation *animation)
86 {
87   GtkCssTransition *transition = GTK_CSS_TRANSITION (animation);
88 
89   return gtk_progress_tracker_get_state (&transition->tracker) == GTK_PROGRESS_STATE_AFTER;
90 }
91 
92 static gboolean
gtk_css_transition_is_static(GtkStyleAnimation * animation)93 gtk_css_transition_is_static (GtkStyleAnimation *animation)
94 {
95   GtkCssTransition *transition = GTK_CSS_TRANSITION (animation);
96 
97   return gtk_progress_tracker_get_state (&transition->tracker) == GTK_PROGRESS_STATE_AFTER;
98 }
99 
100 static void
gtk_css_transition_finalize(GObject * object)101 gtk_css_transition_finalize (GObject *object)
102 {
103   GtkCssTransition *transition = GTK_CSS_TRANSITION (object);
104 
105   _gtk_css_value_unref (transition->start);
106   _gtk_css_value_unref (transition->ease);
107 
108   G_OBJECT_CLASS (_gtk_css_transition_parent_class)->finalize (object);
109 }
110 
111 static void
_gtk_css_transition_class_init(GtkCssTransitionClass * klass)112 _gtk_css_transition_class_init (GtkCssTransitionClass *klass)
113 {
114   GObjectClass *object_class = G_OBJECT_CLASS (klass);
115   GtkStyleAnimationClass *animation_class = GTK_STYLE_ANIMATION_CLASS (klass);
116 
117   object_class->finalize = gtk_css_transition_finalize;
118 
119   animation_class->advance = gtk_css_transition_advance;
120   animation_class->apply_values = gtk_css_transition_apply_values;
121   animation_class->is_finished = gtk_css_transition_is_finished;
122   animation_class->is_static = gtk_css_transition_is_static;
123 }
124 
125 static void
_gtk_css_transition_init(GtkCssTransition * transition)126 _gtk_css_transition_init (GtkCssTransition *transition)
127 {
128 }
129 
130 GtkStyleAnimation *
_gtk_css_transition_new(guint property,GtkCssValue * start,GtkCssValue * ease,gint64 timestamp,gint64 duration_us,gint64 delay_us)131 _gtk_css_transition_new (guint        property,
132                          GtkCssValue *start,
133                          GtkCssValue *ease,
134                          gint64       timestamp,
135                          gint64       duration_us,
136                          gint64       delay_us)
137 {
138   GtkCssTransition *transition;
139 
140   g_return_val_if_fail (start != NULL, NULL);
141   g_return_val_if_fail (ease != NULL, NULL);
142 
143   transition = g_object_new (GTK_TYPE_CSS_TRANSITION, NULL);
144 
145   transition->property = property;
146   transition->start = _gtk_css_value_ref (start);
147   transition->ease = _gtk_css_value_ref (ease);
148   gtk_progress_tracker_start (&transition->tracker, duration_us, delay_us, 1.0);
149   gtk_progress_tracker_advance_frame (&transition->tracker, timestamp);
150 
151   return GTK_STYLE_ANIMATION (transition);
152 }
153 
154 guint
_gtk_css_transition_get_property(GtkCssTransition * transition)155 _gtk_css_transition_get_property (GtkCssTransition *transition)
156 {
157   g_return_val_if_fail (GTK_IS_CSS_TRANSITION (transition), 0);
158 
159   return transition->property;
160 }
161