Skip to content

Commit 0ce9dc4

Browse files
bplaattrflynn89
authored andcommitted
Ladybird/AppKit: Add color picker support
1 parent ebee480 commit 0ce9dc4

File tree

3 files changed

+44
-0
lines changed

3 files changed

+44
-0
lines changed

Ladybird/AppKit/UI/LadybirdWebView.mm

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -527,6 +527,20 @@ - (void)setWebViewCallbacks
527527
returnCode:NSModalResponseCancel];
528528
};
529529

530+
m_web_view_bridge->on_request_color_picker = [self](Color current_color) {
531+
auto* panel = [NSColorPanel sharedColorPanel];
532+
[panel setColor:Ladybird::gfx_color_to_ns_color(current_color)];
533+
[panel setShowsAlpha:NO];
534+
535+
NSNotificationCenter* notification_center = [NSNotificationCenter defaultCenter];
536+
[notification_center addObserver:self
537+
selector:@selector(colorPickerClosed:)
538+
name:NSWindowWillCloseNotification
539+
object:panel];
540+
541+
[panel makeKeyAndOrderFront:nil];
542+
};
543+
530544
m_web_view_bridge->on_get_all_cookies = [](auto const& url) {
531545
auto* delegate = (ApplicationDelegate*)[NSApp delegate];
532546
return [delegate cookieJar].get_all_cookies(url);
@@ -603,6 +617,11 @@ - (void)setWebViewCallbacks
603617
};
604618
}
605619

620+
- (void)colorPickerClosed:(NSNotification*)notification
621+
{
622+
m_web_view_bridge->color_picker_closed(Ladybird::ns_color_to_gfx_color([[NSColorPanel sharedColorPanel] color]));
623+
}
624+
606625
- (NSScrollView*)scrollView
607626
{
608627
return (NSScrollView*)[self superview];

Ladybird/AppKit/Utilities/Conversions.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
#include <AK/String.h>
1010
#include <AK/StringView.h>
11+
#include <LibGfx/Color.h>
1112
#include <LibGfx/Point.h>
1213
#include <LibGfx/Rect.h>
1314
#include <LibGfx/Size.h>
@@ -30,4 +31,7 @@ NSSize gfx_size_to_ns_size(Gfx::IntSize);
3031
Gfx::IntPoint ns_point_to_gfx_point(NSPoint);
3132
NSPoint gfx_point_to_ns_point(Gfx::IntPoint);
3233

34+
Gfx::Color ns_color_to_gfx_color(NSColor*);
35+
NSColor* gfx_color_to_ns_color(Gfx::Color);
36+
3337
}

Ladybird/AppKit/Utilities/Conversions.mm

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,4 +86,25 @@ NSPoint gfx_point_to_ns_point(Gfx::IntPoint point)
8686
static_cast<CGFloat>(point.y()));
8787
}
8888

89+
Gfx::Color ns_color_to_gfx_color(NSColor* color)
90+
{
91+
auto rgb_color = [color colorUsingColorSpace:NSColorSpace.genericRGBColorSpace];
92+
if (rgb_color != nil)
93+
return {
94+
static_cast<u8>([rgb_color redComponent] * 255),
95+
static_cast<u8>([rgb_color greenComponent] * 255),
96+
static_cast<u8>([rgb_color blueComponent] * 255),
97+
static_cast<u8>([rgb_color alphaComponent] * 255)
98+
};
99+
return {};
100+
}
101+
102+
NSColor* gfx_color_to_ns_color(Gfx::Color color)
103+
{
104+
return [NSColor colorWithRed:(color.red() / 255.f)
105+
green:(color.green() / 255.f)
106+
blue:(color.blue() / 255.f)
107+
alpha:(color.alpha() / 255.f)];
108+
}
109+
89110
}

0 commit comments

Comments
 (0)