Skip to content

Commit

Permalink
examples: Add "viewport" example program
Browse files Browse the repository at this point in the history
Add an examples/ subdirectory, and a new simple-stack example program
which shows how to attach multiple views to the CogShell view stack
and switch among them.
  • Loading branch information
aperezdc committed Nov 3, 2023
1 parent a5aeda9 commit ded1a97
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 0 deletions.
8 changes: 8 additions & 0 deletions examples/meson.build
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
examples_c_args = ['-DG_LOG_DOMAIN="Cog-Example"']

executable('viewport',
'viewport.c',
c_args: examples_c_args,
dependencies: cogcore_dep,
install: false,
)
66 changes: 66 additions & 0 deletions examples/viewport.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* viewport.c
* Copyright (C) 2023 Igalia S.L.
*
* SPDX-License-Identifier: MIT
*/

#include "../core/cog.h"

typedef struct {
CogViewport *views;
gsize current_index;
} TimeoutData;

static gboolean
on_timeout_tick(TimeoutData *data)
{
if (++data->current_index >= cog_viewport_get_n_views(data->views))
data->current_index = 0;

CogView *view = cog_viewport_get_nth_view(data->views, data->current_index);
g_message("Set visible view %zu <%p>", data->current_index, view);
cog_viewport_set_visible_view(data->views, view);

return G_SOURCE_CONTINUE;
}

int
main(int argc, char *argv[])
{
if (argc < 2) {
g_printerr("Usage: %s <URL> [URL...]\n", argv[0]);
return EXIT_FAILURE;
}

g_set_prgname("view-stack");
cog_modules_add_directory(g_getenv("COG_MODULEDIR") ?: COG_MODULEDIR);

g_autoptr(GError) error = NULL;
g_autoptr(CogShell) shell = cog_shell_new(g_get_prgname(), FALSE);
g_autoptr(CogPlatform) platform = cog_platform_configure(NULL, NULL, "COG", shell, &error);
if (!platform)
g_error("Cannot configure platform: %s", error->message);

g_autoptr(GMainLoop) loop = g_main_loop_new(NULL, FALSE);

CogViewport *viewport = cog_shell_get_viewport(shell);

for (int i = 1; i < argc; i++) {
g_autoptr(CogView) view = cog_view_new(NULL);
cog_platform_init_web_view(platform, WEBKIT_WEB_VIEW(view));
cog_viewport_add(viewport, view);
webkit_web_view_load_uri(WEBKIT_WEB_VIEW(view), argv[i]);
g_message("Created view %p, URI %s", view, argv[i]);
}

TimeoutData data = {
.views = viewport,
.current_index = SIZE_MAX,
};
g_timeout_add_seconds(3, (GSourceFunc) on_timeout_tick, &data);
on_timeout_tick(&data);

g_main_loop_run(loop);
return EXIT_SUCCESS;
}
4 changes: 4 additions & 0 deletions meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,10 @@ if get_option('documentation')
subdir('docs')
endif

if get_option('examples')
subdir('examples')
endif

if with_programs
subdir('launcher')
if get_option('manpages')
Expand Down
6 changes: 6 additions & 0 deletions meson_options.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ option(
value: true,
description: 'build and install programs (cog, cogctl)'
)
option(
'examples',
type: 'boolean',
value: true,
description: 'build example programs'
)
option(
'wpe_api',
type: 'combo',
Expand Down

0 comments on commit ded1a97

Please sign in to comment.