Skip to content

Commit

Permalink
gtk4: Update cursor from WebView cursor events
Browse files Browse the repository at this point in the history
The cursor is updated from mouse-target-changed signal.

It can be: hand, text or left_ptr.

To get each cursor it is using the gdk_cursor_new_from_name func with the
cursor_names definitions.
  • Loading branch information
joantolo committed Oct 6, 2023
1 parent 111f20c commit 934f99b
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions platform/gtk4/cog-platform-gtk4.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

#include "../../core/cog.h"
#include "../common/cog-gl-utils.h"
#include "../common/cursors.h"
#include "cog-gtk-settings-dialog.h"

#define DEFAULT_WIDTH 1280
Expand Down Expand Up @@ -744,6 +745,37 @@ on_back_forward_changed(WebKitBackForwardList* back_forward_list,
gtk_widget_set_sensitive(win->forward_button,
webkit_web_view_can_go_forward(win->web_view));
}
static void
set_cursor(enum cursor_type type)
{
GdkCursor *cursor = NULL;

for (int i = 0; !cursor && i < G_N_ELEMENTS(cursor_names[type]); i++) {
cursor = gdk_cursor_new_from_name(cursor_names[type][i], NULL);
}

if (!cursor) {
g_warning("Could not get %s cursor", cursor_names[type][0]);
return;
}

gtk_widget_set_cursor(win.gtk_window, cursor);
g_clear_object(&cursor);
}

static void
on_mouse_target_changed(WebKitWebView *view, WebKitHitTestResult *hitTestResult, guint mouseModifiers)
{
if (webkit_hit_test_result_context_is_link(hitTestResult)) {
set_cursor(CURSOR_HAND);
} else if (webkit_hit_test_result_context_is_editable(hitTestResult)) {
set_cursor(CURSOR_TEXT);
} else if (webkit_hit_test_result_context_is_selection(hitTestResult)) {
set_cursor(CURSOR_TEXT);
} else {
set_cursor(CURSOR_LEFT_PTR);
}
}

static void
cog_gtk4_platform_init_web_view(CogPlatform* platform, WebKitWebView* view)
Expand All @@ -754,6 +786,7 @@ cog_gtk4_platform_init_web_view(CogPlatform* platform, WebKitWebView* view)
G_CALLBACK(on_load_progress), &win);
g_signal_connect(webkit_web_view_get_back_forward_list(view), "changed",
G_CALLBACK(on_back_forward_changed), &win);
g_signal_connect(view, "mouse-target-changed", G_CALLBACK(on_mouse_target_changed), NULL);
win.web_view = view;

win.device_scale_factor = gtk_widget_get_scale_factor(win.gl_drawing_area);
Expand Down

0 comments on commit 934f99b

Please sign in to comment.