1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 15:07:45 +00:00

Ladybird: Do not assume the web view is embedded in a normal tab

The LadybirdWebView currently assumed it is viewed with a Tab instance.
This will not be true with the JavaScript console. This patch removes
this assumption by plumbing WebContent callbacks through a new protocol.
The Tab interface then implements this protocol.
This commit is contained in:
Timothy Flynn 2023-08-26 16:21:22 -04:00 committed by Andrew Kaster
parent 85b2782052
commit c9b9278092
4 changed files with 154 additions and 98 deletions

View file

@ -4,13 +4,19 @@
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <AK/DeprecatedString.h>
#include <AK/String.h>
#include <AK/URL.h>
#include <Ladybird/Utilities.h>
#include <LibGfx/ImageFormats/PNGWriter.h>
#include <LibGfx/ShareableBitmap.h>
#import <Application/ApplicationDelegate.h>
#import <UI/LadybirdWebView.h>
#import <UI/Tab.h>
#import <UI/TabController.h>
#import <Utilities/Conversions.h>
#include <Ladybird/Utilities.h>
#if !__has_feature(objc_arc)
# error "This project requires ARC"
#endif
@ -18,7 +24,7 @@
static constexpr CGFloat const WINDOW_WIDTH = 1000;
static constexpr CGFloat const WINDOW_HEIGHT = 800;
@interface Tab ()
@interface Tab () <LadybirdWebViewObserver>
@property (nonatomic, strong) NSString* title;
@property (nonatomic, strong) NSImage* favicon;
@ -59,7 +65,7 @@ static constexpr CGFloat const WINDOW_HEIGHT = 800;
defer:NO];
if (self) {
self.web_view = [[LadybirdWebView alloc] init];
self.web_view = [[LadybirdWebView alloc] init:self];
[self.web_view setPostsBoundsChangedNotifications:YES];
self.favicon = [Tab defaultFavicon];
@ -89,29 +95,13 @@ static constexpr CGFloat const WINDOW_HEIGHT = 800;
return self;
}
#pragma mark - Public methods
- (void)onLoadStart:(URL const&)url
{
self.title = Ladybird::string_to_ns_string(url.serialize());
self.favicon = [Tab defaultFavicon];
[self updateTabTitleAndFavicon];
}
- (void)onTitleChange:(NSString*)title
{
self.title = title;
[self updateTabTitleAndFavicon];
}
- (void)onFaviconChange:(NSImage*)favicon
{
self.favicon = favicon;
[self updateTabTitleAndFavicon];
}
#pragma mark - Private methods
- (TabController*)tabController
{
return (TabController*)[self windowController];
}
- (void)updateTabTitleAndFavicon
{
auto* favicon_attachment = [[NSTextAttachment alloc] init];
@ -153,6 +143,93 @@ static constexpr CGFloat const WINDOW_HEIGHT = 800;
[[self web_view] handleScroll];
}
#pragma mark - LadybirdWebViewObserver
- (String const&)onCreateNewTab:(URL const&)url
activateTab:(Web::HTML::ActivateTab)activate_tab
{
auto* delegate = (ApplicationDelegate*)[NSApp delegate];
auto* controller = [delegate createNewTab:url
fromTab:self
activateTab:activate_tab];
auto* tab = (Tab*)[controller window];
return [[tab web_view] handle];
}
- (String const&)onCreateNewTab:(StringView)html
url:(URL const&)url
activateTab:(Web::HTML::ActivateTab)activate_tab
{
auto* delegate = (ApplicationDelegate*)[NSApp delegate];
auto* controller = [delegate createNewTab:html
url:url
fromTab:self
activateTab:activate_tab];
auto* tab = (Tab*)[controller window];
return [[tab web_view] handle];
}
- (void)loadURL:(URL const&)url
{
[[self tabController] loadURL:url];
}
- (void)onLoadStart:(URL const&)url isRedirect:(BOOL)is_redirect
{
[[self tabController] onLoadStart:url isRedirect:is_redirect];
self.title = Ladybird::string_to_ns_string(url.serialize());
self.favicon = [Tab defaultFavicon];
[self updateTabTitleAndFavicon];
}
- (void)onTitleChange:(DeprecatedString const&)title
{
[[self tabController] onTitleChange:title];
self.title = Ladybird::string_to_ns_string(title);
[self updateTabTitleAndFavicon];
}
- (void)onFaviconChange:(Gfx::Bitmap const&)bitmap
{
static constexpr size_t FAVICON_SIZE = 16;
auto png = Gfx::PNGWriter::encode(bitmap);
if (png.is_error()) {
return;
}
auto* data = [NSData dataWithBytes:png.value().data()
length:png.value().size()];
auto* favicon = [[NSImage alloc] initWithData:data];
[favicon setResizingMode:NSImageResizingModeStretch];
[favicon setSize:NSMakeSize(FAVICON_SIZE, FAVICON_SIZE)];
self.favicon = favicon;
[self updateTabTitleAndFavicon];
}
- (void)onNavigateBack
{
[[self tabController] navigateBack:nil];
}
- (void)onNavigateForward
{
[[self tabController] navigateForward:nil];
}
- (void)onReload
{
[[self tabController] reload:nil];
}
#pragma mark - NSWindow
- (void)setIsVisible:(BOOL)flag