From 0ce9dc4c8e08265ad331c56198511edb560cdf76 Mon Sep 17 00:00:00 2001 From: Bastiaan van der Plaat Date: Fri, 29 Sep 2023 08:28:48 +0200 Subject: [PATCH] Ladybird/AppKit: Add color picker support --- Ladybird/AppKit/UI/LadybirdWebView.mm | 19 +++++++++++++++++++ Ladybird/AppKit/Utilities/Conversions.h | 4 ++++ Ladybird/AppKit/Utilities/Conversions.mm | 21 +++++++++++++++++++++ 3 files changed, 44 insertions(+) diff --git a/Ladybird/AppKit/UI/LadybirdWebView.mm b/Ladybird/AppKit/UI/LadybirdWebView.mm index 76934a7b7b7..0e7f919c5c4 100644 --- a/Ladybird/AppKit/UI/LadybirdWebView.mm +++ b/Ladybird/AppKit/UI/LadybirdWebView.mm @@ -527,6 +527,20 @@ struct HideCursor { returnCode:NSModalResponseCancel]; }; + m_web_view_bridge->on_request_color_picker = [self](Color current_color) { + auto* panel = [NSColorPanel sharedColorPanel]; + [panel setColor:Ladybird::gfx_color_to_ns_color(current_color)]; + [panel setShowsAlpha:NO]; + + NSNotificationCenter* notification_center = [NSNotificationCenter defaultCenter]; + [notification_center addObserver:self + selector:@selector(colorPickerClosed:) + name:NSWindowWillCloseNotification + object:panel]; + + [panel makeKeyAndOrderFront:nil]; + }; + m_web_view_bridge->on_get_all_cookies = [](auto const& url) { auto* delegate = (ApplicationDelegate*)[NSApp delegate]; return [delegate cookieJar].get_all_cookies(url); @@ -603,6 +617,11 @@ struct HideCursor { }; } +- (void)colorPickerClosed:(NSNotification*)notification +{ + m_web_view_bridge->color_picker_closed(Ladybird::ns_color_to_gfx_color([[NSColorPanel sharedColorPanel] color])); +} + - (NSScrollView*)scrollView { return (NSScrollView*)[self superview]; diff --git a/Ladybird/AppKit/Utilities/Conversions.h b/Ladybird/AppKit/Utilities/Conversions.h index a5e8aa68050..b9ed3044a77 100644 --- a/Ladybird/AppKit/Utilities/Conversions.h +++ b/Ladybird/AppKit/Utilities/Conversions.h @@ -8,6 +8,7 @@ #include #include +#include #include #include #include @@ -30,4 +31,7 @@ NSSize gfx_size_to_ns_size(Gfx::IntSize); Gfx::IntPoint ns_point_to_gfx_point(NSPoint); NSPoint gfx_point_to_ns_point(Gfx::IntPoint); +Gfx::Color ns_color_to_gfx_color(NSColor*); +NSColor* gfx_color_to_ns_color(Gfx::Color); + } diff --git a/Ladybird/AppKit/Utilities/Conversions.mm b/Ladybird/AppKit/Utilities/Conversions.mm index 3fc4d5dfcb1..9d8b1d45ead 100644 --- a/Ladybird/AppKit/Utilities/Conversions.mm +++ b/Ladybird/AppKit/Utilities/Conversions.mm @@ -86,4 +86,25 @@ NSPoint gfx_point_to_ns_point(Gfx::IntPoint point) static_cast(point.y())); } +Gfx::Color ns_color_to_gfx_color(NSColor* color) +{ + auto rgb_color = [color colorUsingColorSpace:NSColorSpace.genericRGBColorSpace]; + if (rgb_color != nil) + return { + static_cast([rgb_color redComponent] * 255), + static_cast([rgb_color greenComponent] * 255), + static_cast([rgb_color blueComponent] * 255), + static_cast([rgb_color alphaComponent] * 255) + }; + return {}; +} + +NSColor* gfx_color_to_ns_color(Gfx::Color color) +{ + return [NSColor colorWithRed:(color.red() / 255.f) + green:(color.green() / 255.f) + blue:(color.blue() / 255.f) + alpha:(color.alpha() / 255.f)]; +} + }