mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 07:37:46 +00:00
Ladybird/AppKit: Implement pasting Web data to the clipboard
This commit is contained in:
parent
6732a38cf8
commit
9e637de58a
3 changed files with 34 additions and 13 deletions
|
@ -189,6 +189,15 @@ struct HideCursor {
|
||||||
|
|
||||||
#pragma mark - Private methods
|
#pragma mark - Private methods
|
||||||
|
|
||||||
|
static void copy_data_to_clipboard(StringView data, NSPasteboardType pasteboard_type)
|
||||||
|
{
|
||||||
|
auto* ns_data = Ladybird::string_to_ns_data(data);
|
||||||
|
|
||||||
|
auto* pasteBoard = [NSPasteboard generalPasteboard];
|
||||||
|
[pasteBoard clearContents];
|
||||||
|
[pasteBoard setData:ns_data forType:pasteboard_type];
|
||||||
|
}
|
||||||
|
|
||||||
- (void)updateViewportRect:(Ladybird::WebViewBridge::ForResize)for_resize
|
- (void)updateViewportRect:(Ladybird::WebViewBridge::ForResize)for_resize
|
||||||
{
|
{
|
||||||
auto content_rect = [self frame];
|
auto content_rect = [self frame];
|
||||||
|
@ -667,6 +676,21 @@ struct HideCursor {
|
||||||
alpha:1.0];
|
alpha:1.0];
|
||||||
[self.observer onThemeColorChange:color];
|
[self.observer onThemeColorChange:color];
|
||||||
};
|
};
|
||||||
|
|
||||||
|
m_web_view_bridge->on_insert_clipboard_entry = [](auto const& data, auto const&, auto const& mime_type) {
|
||||||
|
NSPasteboardType pasteboard_type = nil;
|
||||||
|
|
||||||
|
// https://w3c.github.io/clipboard-apis/#os-specific-well-known-format
|
||||||
|
if (mime_type == "text/plain"sv)
|
||||||
|
pasteboard_type = NSPasteboardTypeString;
|
||||||
|
else if (mime_type == "text/html"sv)
|
||||||
|
pasteboard_type = NSPasteboardTypeHTML;
|
||||||
|
else if (mime_type == "text/png"sv)
|
||||||
|
pasteboard_type = NSPasteboardTypePNG;
|
||||||
|
|
||||||
|
if (pasteboard_type)
|
||||||
|
copy_data_to_clipboard(data, pasteboard_type);
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
- (void)colorPickerClosed:(NSNotification*)notification
|
- (void)colorPickerClosed:(NSNotification*)notification
|
||||||
|
@ -679,18 +703,9 @@ struct HideCursor {
|
||||||
return (NSScrollView*)[self superview];
|
return (NSScrollView*)[self superview];
|
||||||
}
|
}
|
||||||
|
|
||||||
static void copy_text_to_clipboard(StringView text)
|
|
||||||
{
|
|
||||||
auto* string = Ladybird::string_to_ns_string(text);
|
|
||||||
|
|
||||||
auto* pasteBoard = [NSPasteboard generalPasteboard];
|
|
||||||
[pasteBoard clearContents];
|
|
||||||
[pasteBoard setString:string forType:NSPasteboardTypeString];
|
|
||||||
}
|
|
||||||
|
|
||||||
- (void)copy:(id)sender
|
- (void)copy:(id)sender
|
||||||
{
|
{
|
||||||
copy_text_to_clipboard(m_web_view_bridge->selected_text());
|
copy_data_to_clipboard(m_web_view_bridge->selected_text(), NSPasteboardTypeString);
|
||||||
}
|
}
|
||||||
|
|
||||||
- (void)selectAll:(id)sender
|
- (void)selectAll:(id)sender
|
||||||
|
@ -730,7 +745,7 @@ static void copy_text_to_clipboard(StringView text)
|
||||||
|
|
||||||
- (void)copyLink:(id)sender
|
- (void)copyLink:(id)sender
|
||||||
{
|
{
|
||||||
copy_text_to_clipboard(m_context_menu_url.serialize());
|
copy_data_to_clipboard(m_context_menu_url.serialize(), NSPasteboardTypeString);
|
||||||
}
|
}
|
||||||
|
|
||||||
- (void)copyImage:(id)sender
|
- (void)copyImage:(id)sender
|
||||||
|
|
|
@ -20,6 +20,8 @@ namespace Ladybird {
|
||||||
String ns_string_to_string(NSString*);
|
String ns_string_to_string(NSString*);
|
||||||
NSString* string_to_ns_string(StringView);
|
NSString* string_to_ns_string(StringView);
|
||||||
|
|
||||||
|
NSData* string_to_ns_data(StringView);
|
||||||
|
|
||||||
NSDictionary* deserialize_json_to_dictionary(StringView);
|
NSDictionary* deserialize_json_to_dictionary(StringView);
|
||||||
|
|
||||||
Gfx::IntRect ns_rect_to_gfx_rect(NSRect);
|
Gfx::IntRect ns_rect_to_gfx_rect(NSRect);
|
||||||
|
|
|
@ -16,8 +16,12 @@ String ns_string_to_string(NSString* string)
|
||||||
|
|
||||||
NSString* string_to_ns_string(StringView string)
|
NSString* string_to_ns_string(StringView string)
|
||||||
{
|
{
|
||||||
auto* data = [NSData dataWithBytes:string.characters_without_null_termination() length:string.length()];
|
return [[NSString alloc] initWithData:string_to_ns_data(string) encoding:NSUTF8StringEncoding];
|
||||||
return [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
|
}
|
||||||
|
|
||||||
|
NSData* string_to_ns_data(StringView string)
|
||||||
|
{
|
||||||
|
return [NSData dataWithBytes:string.characters_without_null_termination() length:string.length()];
|
||||||
}
|
}
|
||||||
|
|
||||||
NSDictionary* deserialize_json_to_dictionary(StringView json)
|
NSDictionary* deserialize_json_to_dictionary(StringView json)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue