libservo: Add a ClipboardDelegate and a default implementation (#35297)

Add a `ClipboardDelegate` to the `WebView` API and a default
implementation in libservo for this delegate that works on Mac, Windows,
and Linux. Support for Android will be added in the future. This means
that embedders do not need to do anything special to get clipboard
support, but can choose to override it or implement it for other
platforms.

In addition, this adds support for handling fetches of clipboard contents
and renames things to reflect that eventually other types of clipboard
content will be supported. Part of this is removing the string
argument from the `ClipboardEventType::Paste` enum because script will
need to get other types of content from the clipboard than just a
string. It now talks to the embedder to get this information directly.

Signed-off-by: Martin Robinson <mrobinson@igalia.com>
This commit is contained in:
Martin Robinson 2025-02-07 11:43:46 +01:00 committed by GitHub
parent b5b69988cc
commit 19e41ab9f9
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
20 changed files with 212 additions and 127 deletions

View file

@ -105,7 +105,6 @@ serde_json = { workspace = true }
[target.'cfg(not(any(target_os = "android", target_env = "ohos")))'.dependencies]
# For optional feature servo_allocator/use-system-allocator
servo_allocator = { path = "../../components/allocator" }
arboard = { version = "3" }
dirs = "5.0"
egui = { version = "0.31.0" }
egui_glow = { version = "0.31.0", features = ["winit"] }

View file

@ -8,10 +8,9 @@ use std::path::PathBuf;
use std::rc::Rc;
use std::thread;
use arboard::Clipboard;
use euclid::Vector2D;
use keyboard_types::{Key, KeyboardEvent, Modifiers, ShortcutMatcher};
use log::{error, info, warn};
use log::{error, info};
use servo::base::id::WebViewId;
use servo::config::pref;
use servo::ipc_channel::ipc::IpcSender;
@ -49,9 +48,6 @@ pub struct RunningAppStateInner {
/// Whether or not this is a headless servoshell window.
headless: bool,
/// The clipboard to use for this collection of [`WebView`]s.
pub(crate) clipboard: Option<Clipboard>,
/// List of top-level browsing contexts.
/// Modified by EmbedderMsg::WebViewOpened and EmbedderMsg::WebViewClosed,
/// and we exit if it ever becomes empty.
@ -96,7 +92,6 @@ impl RunningAppState {
servo,
inner: RefCell::new(RunningAppStateInner {
headless,
clipboard: Clipboard::new().ok(),
webviews: HashMap::default(),
creation_order: Default::default(),
focused_webview_id: None,
@ -467,33 +462,6 @@ impl WebViewDelegate for RunningAppState {
self.handle_overridable_key_bindings(webview, keyboard_event);
}
fn clear_clipboard_contents(&self, _webview: servo::WebView) {
self.inner_mut()
.clipboard
.as_mut()
.and_then(|clipboard| clipboard.clear().ok());
}
fn get_clipboard_contents(&self, _webview: servo::WebView, result_sender: IpcSender<String>) {
let contents = self
.inner_mut()
.clipboard
.as_mut()
.and_then(|clipboard| clipboard.get_text().ok())
.unwrap_or_default();
if let Err(e) = result_sender.send(contents) {
warn!("Failed to send clipboard ({})", e);
}
}
fn set_clipboard_contents(&self, _webview: servo::WebView, text: String) {
if let Some(clipboard) = self.inner_mut().clipboard.as_mut() {
if let Err(e) = clipboard.set_text(text) {
warn!("Error setting clipboard contents ({})", e);
}
}
}
fn notify_cursor_changed(&self, _webview: servo::WebView, cursor: servo::Cursor) {
self.inner().window.set_cursor(cursor);
}

View file

@ -320,13 +320,7 @@ impl Window {
focused_webview.notify_clipboard_event(ClipboardEventType::Copy);
})
.shortcut(CMD_OR_CONTROL, 'V', || {
let text = state
.inner_mut()
.clipboard
.as_mut()
.and_then(|clipboard| clipboard.get_text().ok())
.unwrap_or_default();
focused_webview.notify_clipboard_event(ClipboardEventType::Paste(text));
focused_webview.notify_clipboard_event(ClipboardEventType::Paste);
})
.shortcut(Modifiers::CONTROL, Key::F9, || {
focused_webview.capture_webrender();

View file

@ -605,12 +605,6 @@ impl HostTrait for HostCallbacks {
}
fn on_ime_hide(&self) {}
fn get_clipboard_contents(&self) -> Option<String> {
None
}
fn set_clipboard_contents(&self, _contents: String) {}
fn on_media_session_metadata(&self, title: String, artist: String, album: String) {
info!("on_media_session_metadata");
let mut env = self.jvm.get_env().unwrap();

View file

@ -295,15 +295,6 @@ impl WebViewDelegate for RunningAppState {
fn hide_ime(&self, _webview: WebView) {
self.callbacks.host_callbacks.on_ime_hide();
}
fn get_clipboard_contents(&self, _webview: WebView, sender: IpcSender<String>) {
let contents = self.callbacks.host_callbacks.get_clipboard_contents();
let _ = sender.send(contents.unwrap_or("".to_owned()));
}
fn set_clipboard_contents(&self, _webview: WebView, text: String) {
self.callbacks.host_callbacks.set_clipboard_contents(text);
}
}
#[allow(unused)]

View file

@ -64,10 +64,6 @@ pub trait HostTrait {
);
/// Input lost focus
fn on_ime_hide(&self);
/// Gets sytem clipboard contents.
fn get_clipboard_contents(&self) -> Option<String>;
/// Sets system clipboard contents.
fn set_clipboard_contents(&self, contents: String);
/// Called when we get the media session metadata/
fn on_media_session_metadata(&self, title: String, artist: String, album: String);
/// Called when the media session playback state changes.

View file

@ -818,15 +818,6 @@ impl HostTrait for HostCallbacks {
}
}
fn get_clipboard_contents(&self) -> Option<String> {
warn!("get_clipboard_contents not implemented");
None
}
fn set_clipboard_contents(&self, contents: String) {
warn!("set_clipboard_contents not implemented");
}
fn on_media_session_metadata(&self, title: String, artist: String, album: String) {
warn!("on_media_session_metadata not implemented");
}