ltk_webkit/wpe/
hover.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
// SPDX-License-Identifier: LGPL-2.1-only
// Copyright (C) 2026 Liberux Labs, S. L. <info@liberux.net>

//! The link under the pointer, from WebKit's `mouse-target-changed`, for the host to show where it leads.

use std::ffi::{ c_char, c_uint };
use std::sync::{ Mutex, Weak };

use glib_sys::gpointer;
use gobject_sys::{ g_signal_connect_data, GObject };
use wpe_webkit_sys::*;

use super::{ context_menu, popups, Inner, WebView };

type MouseTargetFn = unsafe extern "C" fn( *mut WebKitWebView, *mut WebKitHitTestResult, c_uint, gpointer );

impl WebView
{
	/// The address of the link the pointer is over, as of its last motion over the page.
	pub fn hovered_link( &self ) -> Option<String>
	{
		self.inner.lock().ok().and_then( |g| g.hovered_link.clone() )
	}
}

pub( crate ) unsafe fn connect( webview: *mut WebKitWebView, inner: Weak<Mutex<Inner>> )
{
	// SAFETY: the handler matches the signal's signature; the closure owns its boxed `Weak`.
	unsafe
	{
		let handler = std::mem::transmute::<MouseTargetFn, unsafe extern "C" fn()>( on_mouse_target );
		let data    = Box::into_raw( Box::new( inner ) ) as gpointer;
		g_signal_connect_data( webview as *mut GObject, b"mouse-target-changed\0".as_ptr() as *const c_char, Some( handler ), data, Some( popups::drop_weak ), 0 );
	}
}

unsafe extern "C" fn on_mouse_target( _webview: *mut WebKitWebView, hit: *mut WebKitHitTestResult, _modifiers: c_uint, user_data: gpointer )
{
	// SAFETY: `user_data` is the boxed `Weak` owned by the closure.
	let Some( inner ) = unsafe { &*( user_data as *const Weak<Mutex<Inner>> ) }.upgrade() else { return };
	// SAFETY: `hit` is alive for the signal; the string is copied here.
	let link = if hit.is_null() { None } else { unsafe { context_menu::text( webkit_hit_test_result_get_link_uri( hit ) ) } };
	if let Ok( mut g ) = inner.lock() { g.hovered_link = link; };
}