st-scroll-view-fade: Add horizontal fade support

St-scroll-view-fade only allowed adding a fade effect in the vertical
direction; extend it so it can work horizontally too.

https://bugzilla.gnome.org/show_bug.cgi?id=613194
This commit is contained in:
Adel Gadllah 2012-02-08 18:47:23 +01:00
parent 24ad59ea37
commit fd4d645687
3 changed files with 157 additions and 44 deletions

View File

@ -44,6 +44,11 @@ StScrollView.vfade
-st-vfade-offset: 68px; -st-vfade-offset: 68px;
} }
StScrollView.hfade
{
-st-hfade-offset: 68px;
}
StScrollView StScrollBar StScrollView StScrollBar
{ {
min-width: 16px; min-width: 16px;

View File

@ -43,6 +43,8 @@ static const gchar *fade_glsl_shader =
"uniform float width;\n" "uniform float width;\n"
"uniform float offset_bottom;\n" "uniform float offset_bottom;\n"
"uniform float offset_top;\n" "uniform float offset_top;\n"
"uniform float offset_right;\n"
"uniform float offset_left;\n"
/* /*
* Used to pass the fade area to the shader * Used to pass the fade area to the shader
* *
@ -61,11 +63,16 @@ static const gchar *fade_glsl_shader =
" float x = width * cogl_tex_coord_in[0].x;\n" " float x = width * cogl_tex_coord_in[0].x;\n"
" float ratio = 1.0;\n" " float ratio = 1.0;\n"
" float fade_bottom_start = fade_area[1][1] - offset_bottom;\n" " float fade_bottom_start = fade_area[1][1] - offset_bottom;\n"
" float fade_right_start = fade_area[1][0] - offset_right;\n"
" float ratio_top = y / offset_top;\n" " float ratio_top = y / offset_top;\n"
" float ratio_bottom = (fade_area[1][1] - y)/(fade_area[1][1] - fade_bottom_start);\n" " float ratio_bottom = (fade_area[1][1] - y)/(fade_area[1][1] - fade_bottom_start);\n"
" float ratio_left = x / offset_left;\n"
" float ratio_right = (fade_area[1][0] - x)/(fade_area[1][0] - fade_right_start);\n"
" bool in_scroll_area = fade_area[0][0] <= x && fade_area[1][0] >= x;\n" " bool in_scroll_area = fade_area[0][0] <= x && fade_area[1][0] >= x;\n"
" bool fade_top = y < offset_top && in_scroll_area && (y >= fade_area[0][1]);\n" " bool fade_top = y < offset_top && in_scroll_area && (y >= fade_area[0][1]);\n"
" bool fade_bottom = y > fade_bottom_start && in_scroll_area && (y <= fade_area[1][1]);\n" " bool fade_bottom = y > fade_bottom_start && in_scroll_area && (y <= fade_area[1][1]);\n"
" bool fade_left = x < offset_left && in_scroll_area && (x >= fade_area[0][0]);\n"
" bool fade_right = x > fade_right_start && in_scroll_area && (x <= fade_area[1][0]);\n"
"\n" "\n"
" if (fade_top) {\n" " if (fade_top) {\n"
" ratio *= ratio_top;\n" " ratio *= ratio_top;\n"
@ -75,6 +82,14 @@ static const gchar *fade_glsl_shader =
" ratio *= ratio_bottom;\n" " ratio *= ratio_bottom;\n"
" }\n" " }\n"
"\n" "\n"
" if (fade_left) {\n"
" ratio *= ratio_left;\n"
" }\n"
"\n"
" if (fade_right) {\n"
" ratio *= ratio_right;\n"
" }\n"
"\n"
" cogl_color_out = color * ratio;\n" " cogl_color_out = color * ratio;\n"
"}"; "}";
@ -94,12 +109,16 @@ struct _StScrollViewFade
gint fade_area_uniform; gint fade_area_uniform;
gint offset_top_uniform; gint offset_top_uniform;
gint offset_bottom_uniform; gint offset_bottom_uniform;
gint offset_left_uniform;
gint offset_right_uniform;
StAdjustment *vadjustment; StAdjustment *vadjustment;
StAdjustment *hadjustment;
guint is_attached : 1; guint is_attached : 1;
float fade_offset; float vfade_offset;
float hfade_offset;
}; };
struct _StScrollViewFadeClass struct _StScrollViewFadeClass
@ -114,7 +133,8 @@ G_DEFINE_TYPE (StScrollViewFade,
enum { enum {
PROP_0, PROP_0,
PROP_FADE_OFFSET, PROP_VFADE_OFFSET,
PROP_HFADE_OFFSET
}; };
static gboolean static gboolean
@ -159,6 +179,10 @@ st_scroll_view_fade_pre_paint (ClutterEffect *effect)
cogl_program_get_uniform_location (self->program, "offset_top"); cogl_program_get_uniform_location (self->program, "offset_top");
self->offset_bottom_uniform = self->offset_bottom_uniform =
cogl_program_get_uniform_location (self->program, "offset_bottom"); cogl_program_get_uniform_location (self->program, "offset_bottom");
self->offset_left_uniform =
cogl_program_get_uniform_location (self->program, "offset_left");
self->offset_right_uniform =
cogl_program_get_uniform_location (self->program, "offset_right");
} }
parent_class = CLUTTER_EFFECT_CLASS (st_scroll_view_fade_parent_class); parent_class = CLUTTER_EFFECT_CLASS (st_scroll_view_fade_parent_class);
@ -241,18 +265,34 @@ st_scroll_view_fade_paint_target (ClutterOffscreenEffect *effect)
if (self->offset_top_uniform > -1) { if (self->offset_top_uniform > -1) {
if (value > lower + 0.1) if (value > lower + 0.1)
cogl_program_set_uniform_1f (self->program, self->offset_top_uniform, self->fade_offset); cogl_program_set_uniform_1f (self->program, self->offset_top_uniform, self->vfade_offset);
else else
cogl_program_set_uniform_1f (self->program, self->offset_top_uniform, 0.0f); cogl_program_set_uniform_1f (self->program, self->offset_top_uniform, 0.0f);
} }
if (self->offset_bottom_uniform > -1) { if (self->offset_bottom_uniform > -1) {
if (value < upper - page_size - 0.1) if (value < upper - page_size - 0.1)
cogl_program_set_uniform_1f (self->program, self->offset_bottom_uniform, self->fade_offset); cogl_program_set_uniform_1f (self->program, self->offset_bottom_uniform, self->vfade_offset);
else else
cogl_program_set_uniform_1f (self->program, self->offset_bottom_uniform, 0.0f); cogl_program_set_uniform_1f (self->program, self->offset_bottom_uniform, 0.0f);
} }
st_adjustment_get_values (self->hadjustment, &value, &lower, &upper, NULL, NULL, &page_size);
if (self->offset_left_uniform > -1) {
if (value > lower + 0.1)
cogl_program_set_uniform_1f (self->program, self->offset_left_uniform, self->hfade_offset);
else
cogl_program_set_uniform_1f (self->program, self->offset_left_uniform, 0.0f);
}
if (self->offset_right_uniform > -1) {
if (value < upper - page_size - 0.1)
cogl_program_set_uniform_1f (self->program, self->offset_right_uniform, self->hfade_offset);
else
cogl_program_set_uniform_1f (self->program, self->offset_right_uniform, 0.0f);
}
if (self->tex_uniform > -1) if (self->tex_uniform > -1)
cogl_program_set_uniform_1i (self->program, self->tex_uniform, 0); cogl_program_set_uniform_1i (self->program, self->tex_uniform, 0);
if (self->height_uniform > -1) if (self->height_uniform > -1)
@ -271,15 +311,22 @@ out:
} }
static void static void
on_vadjustment_changed (StAdjustment *adjustment, on_adjustment_changed (StAdjustment *adjustment,
ClutterEffect *effect) ClutterEffect *effect)
{ {
gdouble value, lower, upper, page_size; gdouble value, lower, upper, page_size;
gboolean needs_fade; gboolean needs_fade;
StScrollViewFade *self = ST_SCROLL_VIEW_FADE (effect);
st_adjustment_get_values (adjustment, &value, &lower, &upper, NULL, NULL, &page_size); st_adjustment_get_values (self->vadjustment, &value, &lower, &upper, NULL, NULL, &page_size);
needs_fade = (value > lower + 0.1) || (value < upper - page_size - 0.1); needs_fade = (value > lower + 0.1) || (value < upper - page_size - 0.1);
if (!needs_fade)
{
st_adjustment_get_values (self->hadjustment, &value, &lower, &upper, NULL, NULL, &page_size);
needs_fade = (value > lower + 0.1) || (value < upper - page_size - 0.1);
}
clutter_actor_meta_set_enabled (CLUTTER_ACTOR_META (effect), needs_fade); clutter_actor_meta_set_enabled (CLUTTER_ACTOR_META (effect), needs_fade);
} }
@ -301,22 +348,37 @@ st_scroll_view_fade_set_actor (ClutterActorMeta *meta,
if (self->vadjustment) if (self->vadjustment)
{ {
g_signal_handlers_disconnect_by_func (self->vadjustment, g_signal_handlers_disconnect_by_func (self->vadjustment,
(gpointer)on_vadjustment_changed, (gpointer)on_adjustment_changed,
self); self);
self->vadjustment = NULL; self->vadjustment = NULL;
} }
if (self->hadjustment)
{
g_signal_handlers_disconnect_by_func (self->hadjustment,
(gpointer)on_adjustment_changed,
self);
self->hadjustment = NULL;
}
if (actor) if (actor)
{ {
StScrollView *scroll_view = ST_SCROLL_VIEW (actor); StScrollView *scroll_view = ST_SCROLL_VIEW (actor);
StScrollBar *vscroll = ST_SCROLL_BAR (st_scroll_view_get_vscroll_bar (scroll_view)); StScrollBar *vscroll = ST_SCROLL_BAR (st_scroll_view_get_vscroll_bar (scroll_view));
StScrollBar *hscroll = ST_SCROLL_BAR (st_scroll_view_get_hscroll_bar (scroll_view));
self->vadjustment = ST_ADJUSTMENT (st_scroll_bar_get_adjustment (vscroll)); self->vadjustment = ST_ADJUSTMENT (st_scroll_bar_get_adjustment (vscroll));
self->hadjustment = ST_ADJUSTMENT (st_scroll_bar_get_adjustment (hscroll));
g_signal_connect (self->vadjustment, "changed", g_signal_connect (self->vadjustment, "changed",
G_CALLBACK (on_vadjustment_changed), G_CALLBACK (on_adjustment_changed),
self); self);
on_vadjustment_changed (self->vadjustment, CLUTTER_EFFECT (self)); g_signal_connect (self->hadjustment, "changed",
G_CALLBACK (on_adjustment_changed),
self);
on_adjustment_changed (NULL, CLUTTER_EFFECT (self));
} }
parent = CLUTTER_ACTOR_META_CLASS (st_scroll_view_fade_parent_class); parent = CLUTTER_ACTOR_META_CLASS (st_scroll_view_fade_parent_class);
@ -342,31 +404,57 @@ st_scroll_view_fade_dispose (GObject *gobject)
if (self->vadjustment) if (self->vadjustment)
{ {
g_signal_handlers_disconnect_by_func (self->vadjustment, g_signal_handlers_disconnect_by_func (self->vadjustment,
(gpointer)on_vadjustment_changed, (gpointer)on_adjustment_changed,
self); self);
self->vadjustment = NULL; self->vadjustment = NULL;
} }
if (self->hadjustment)
{
g_signal_handlers_disconnect_by_func (self->hadjustment,
(gpointer)on_adjustment_changed,
self);
self->hadjustment = NULL;
}
self->actor = NULL; self->actor = NULL;
G_OBJECT_CLASS (st_scroll_view_fade_parent_class)->dispose (gobject); G_OBJECT_CLASS (st_scroll_view_fade_parent_class)->dispose (gobject);
} }
static void static void
st_scroll_view_fade_set_offset (StScrollViewFade *self, st_scroll_view_vfade_set_offset (StScrollViewFade *self,
float fade_offset) float fade_offset)
{ {
if (self->fade_offset == fade_offset) if (self->vfade_offset == fade_offset)
return; return;
g_object_freeze_notify (G_OBJECT (self)); g_object_freeze_notify (G_OBJECT (self));
self->fade_offset = fade_offset; self->vfade_offset = fade_offset;
if (self->actor != NULL) if (self->actor != NULL)
clutter_actor_queue_redraw (self->actor); clutter_actor_queue_redraw (self->actor);
g_object_notify (G_OBJECT (self), "fade-offset"); g_object_notify (G_OBJECT (self), "vfade-offset");
g_object_thaw_notify (G_OBJECT (self));
}
static void
st_scroll_view_hfade_set_offset (StScrollViewFade *self,
float fade_offset)
{
if (self->hfade_offset == fade_offset)
return;
g_object_freeze_notify (G_OBJECT (self));
self->hfade_offset = fade_offset;
if (self->actor != NULL)
clutter_actor_queue_redraw (self->actor);
g_object_notify (G_OBJECT (self), "hfade-offset");
g_object_thaw_notify (G_OBJECT (self)); g_object_thaw_notify (G_OBJECT (self));
} }
@ -380,8 +468,11 @@ st_scroll_view_fade_set_property (GObject *object,
switch (prop_id) switch (prop_id)
{ {
case PROP_FADE_OFFSET: case PROP_VFADE_OFFSET:
st_scroll_view_fade_set_offset (self, g_value_get_float (value)); st_scroll_view_vfade_set_offset (self, g_value_get_float (value));
break;
case PROP_HFADE_OFFSET:
st_scroll_view_hfade_set_offset (self, g_value_get_float (value));
break; break;
default: default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
@ -399,8 +490,11 @@ st_scroll_view_fade_get_property (GObject *object,
switch (prop_id) switch (prop_id)
{ {
case PROP_FADE_OFFSET: case PROP_HFADE_OFFSET:
g_value_set_float (value, self->fade_offset); g_value_set_float (value, self->vfade_offset);
break;
case PROP_VFADE_OFFSET:
g_value_set_float (value, self->hfade_offset);
break; break;
default: default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
@ -429,12 +523,19 @@ st_scroll_view_fade_class_init (StScrollViewFadeClass *klass)
offscreen_class->paint_target = st_scroll_view_fade_paint_target; offscreen_class->paint_target = st_scroll_view_fade_paint_target;
g_object_class_install_property (gobject_class, g_object_class_install_property (gobject_class,
PROP_FADE_OFFSET, PROP_VFADE_OFFSET,
g_param_spec_float ("fade-offset", g_param_spec_float ("vfade-offset",
"Fade Offset", "Vertical Fade Offset",
"The height of the area which is faded at the edge", "The height of the area which is faded at the edge",
0.f, G_MAXFLOAT, DEFAULT_FADE_OFFSET, 0.f, G_MAXFLOAT, DEFAULT_FADE_OFFSET,
G_PARAM_READWRITE)); G_PARAM_READWRITE));
g_object_class_install_property (gobject_class,
PROP_HFADE_OFFSET,
g_param_spec_float ("hfade-offset",
"Horizontal Fade Offset",
"The width of the area which is faded at the edge",
0.f, G_MAXFLOAT, DEFAULT_FADE_OFFSET,
G_PARAM_READWRITE));
} }
@ -472,7 +573,8 @@ st_scroll_view_fade_init (StScrollViewFade *self)
self->fade_area_uniform = -1; self->fade_area_uniform = -1;
self->offset_top_uniform = -1; self->offset_top_uniform = -1;
self->offset_bottom_uniform = -1; self->offset_bottom_uniform = -1;
self->fade_offset = DEFAULT_FADE_OFFSET; self->vfade_offset = DEFAULT_FADE_OFFSET;
self->hfade_offset = DEFAULT_FADE_OFFSET;
if (shader != COGL_INVALID_HANDLE) if (shader != COGL_INVALID_HANDLE)
cogl_handle_ref (self->shader); cogl_handle_ref (self->shader);

View File

@ -96,7 +96,7 @@ struct _StScrollViewPrivate
gfloat row_size; gfloat row_size;
gfloat column_size; gfloat column_size;
StScrollViewFade *vfade_effect; StScrollViewFade *fade_effect;
gboolean row_size_set : 1; gboolean row_size_set : 1;
gboolean column_size_set : 1; gboolean column_size_set : 1;
@ -154,38 +154,43 @@ st_scroll_view_get_property (GObject *object,
} }
/** /**
* st_scroll_view_update_vfade_effect: * st_scroll_view_update_fade_effect:
* @self: a #StScrollView * @self: a #StScrollView
* @fade_offset: The length of the fade effect, in pixels. * @vfade_offset: The length of the veritcal fade effect, in pixels.
* @hfade_offset: The length of the horizontal fade effect, in pixels.
* *
* Sets the height of the fade area area in pixels. A value of 0 * Sets the height of the fade area area in pixels. A value of 0
* disables the effect. * disables the effect.
*/ */
static void static void
st_scroll_view_update_vfade_effect (StScrollView *self, st_scroll_view_update_fade_effect (StScrollView *self,
float fade_offset) float vfade_offset,
float hfade_offset)
{ {
StScrollViewPrivate *priv = ST_SCROLL_VIEW (self)->priv; StScrollViewPrivate *priv = ST_SCROLL_VIEW (self)->priv;
/* A fade amount of more than 0 enables the effect. */ /* A fade amount of more than 0 enables the effect. */
if (fade_offset > 0.) if (vfade_offset > 0. || hfade_offset > 0.)
{ {
if (priv->vfade_effect == NULL) { if (priv->fade_effect == NULL) {
priv->vfade_effect = g_object_new (ST_TYPE_SCROLL_VIEW_FADE, NULL); priv->fade_effect = g_object_new (ST_TYPE_SCROLL_VIEW_FADE, NULL);
clutter_actor_add_effect_with_name (CLUTTER_ACTOR (self), "vfade", clutter_actor_add_effect_with_name (CLUTTER_ACTOR (self), "fade",
CLUTTER_EFFECT (priv->vfade_effect)); CLUTTER_EFFECT (priv->fade_effect));
} }
g_object_set (priv->vfade_effect, g_object_set (priv->fade_effect,
"fade-offset", fade_offset, "vfade-offset", vfade_offset,
NULL);
g_object_set (priv->fade_effect,
"hfade-offset", hfade_offset,
NULL); NULL);
} }
else else
{ {
if (priv->vfade_effect != NULL) { if (priv->fade_effect != NULL) {
clutter_actor_remove_effect (CLUTTER_ACTOR (self), CLUTTER_EFFECT (priv->vfade_effect)); clutter_actor_remove_effect (CLUTTER_ACTOR (self), CLUTTER_EFFECT (priv->fade_effect));
priv->vfade_effect = NULL; priv->fade_effect = NULL;
} }
} }
@ -227,10 +232,10 @@ st_scroll_view_dispose (GObject *object)
{ {
StScrollViewPrivate *priv = ST_SCROLL_VIEW (object)->priv; StScrollViewPrivate *priv = ST_SCROLL_VIEW (object)->priv;
if (priv->vfade_effect) if (priv->fade_effect)
{ {
clutter_actor_remove_effect (CLUTTER_ACTOR (object), CLUTTER_EFFECT (priv->vfade_effect)); clutter_actor_remove_effect (CLUTTER_ACTOR (object), CLUTTER_EFFECT (priv->fade_effect));
priv->vfade_effect = NULL; priv->fade_effect = NULL;
} }
if (priv->vscroll) if (priv->vscroll)
@ -668,8 +673,9 @@ st_scroll_view_style_changed (StWidget *widget)
StScrollViewPrivate *priv = self->priv; StScrollViewPrivate *priv = self->priv;
StThemeNode *theme_node = st_widget_get_theme_node (widget); StThemeNode *theme_node = st_widget_get_theme_node (widget);
gdouble fade_offset = st_theme_node_get_length (theme_node, "-st-vfade-offset"); gdouble vfade_offset = st_theme_node_get_length (theme_node, "-st-vfade-offset");
st_scroll_view_update_vfade_effect (self, fade_offset); gdouble hfade_offset = st_theme_node_get_length (theme_node, "-st-hfade-offset");
st_scroll_view_update_fade_effect (self, vfade_offset, hfade_offset);
st_widget_style_changed (ST_WIDGET (priv->hscroll)); st_widget_style_changed (ST_WIDGET (priv->hscroll));
st_widget_style_changed (ST_WIDGET (priv->vscroll)); st_widget_style_changed (ST_WIDGET (priv->vscroll));