examples/canvas: Resize the canvas on allocation changes

This should show how to make a Canvas resize whenever the actor that
uses it as content changes size. For good measure, it also shows how to
coalesce multiple allocations into one Canvas resize through a timeout
source.
This commit is contained in:
Emmanuele Bassi 2012-05-11 17:38:53 +01:00
parent ae32136fcc
commit 193bf6123d
1 changed files with 38 additions and 1 deletions

View File

@ -77,7 +77,41 @@ invalidate_clock (gpointer data_)
clutter_content_invalidate (data_);
/* keep the timeout source */
return TRUE;
return G_SOURCE_CONTINUE;
}
static guint idle_resize_id;
static gboolean
idle_resize (gpointer data)
{
ClutterActor *actor = data;
float width, height;
/* match the canvas size to the actor's */
clutter_actor_get_size (actor, &width, &height);
clutter_canvas_set_size (CLUTTER_CANVAS (clutter_actor_get_content (actor)),
ceilf (width),
ceilf (height));
/* unset the guard */
idle_resize_id = 0;
/* remove the timeout */
return G_SOURCE_REMOVE;
}
static void
on_actor_resize (ClutterActor *actor,
const ClutterActorBox *allocation,
ClutterAllocationFlags flags,
gpointer user_data)
{
/* throttle multiple actor allocations to one canvas resize; we use a guard
* variable to avoid queueing multiple resize operations
*/
if (idle_resize_id == 0)
idle_resize_id = clutter_threads_add_timeout (1000, idle_resize, actor);
}
int
@ -115,6 +149,9 @@ main (int argc, char *argv[])
/* bind the size of the actor to that of the stage */
clutter_actor_add_constraint (actor, clutter_bind_constraint_new (stage, CLUTTER_BIND_SIZE, 0));
/* resize the canvas whenever the actor changes size */
g_signal_connect (actor, "allocation-changed", G_CALLBACK (on_actor_resize), NULL);
/* quit on destroy */
g_signal_connect (stage, "destroy", G_CALLBACK (clutter_main_quit), NULL);