From 53d5c7084e8ddd29dc85314274d54c30fea9bbaf Mon Sep 17 00:00:00 2001 From: Timothy Flynn Date: Tue, 29 Oct 2024 11:05:25 -0400 Subject: [PATCH] UI/AppKit: Extract bare minimum WebView functionality into its own class When a window containing a WebView becomes visibile, we have to inform WebContent. This was only implemented for the Tab class (not Inspector or Task Manaager). This patch adds LadybirdWebViewWindow to contain the bare minimum needed to render a LadybirdWebView. All windows containing a WebView inherit from this class, to ensure their WebContent processes know they became visible. --- Ladybird/AppKit/CMakeLists.txt | 1 + Ladybird/AppKit/UI/Inspector.h | 7 +- Ladybird/AppKit/UI/Inspector.mm | 24 +------ Ladybird/AppKit/UI/LadybirdWebViewWindow.h | 20 ++++++ Ladybird/AppKit/UI/LadybirdWebViewWindow.mm | 75 +++++++++++++++++++++ Ladybird/AppKit/UI/Tab.h | 7 +- Ladybird/AppKit/UI/Tab.mm | 49 +------------- Ladybird/AppKit/UI/TaskManager.h | 7 +- Ladybird/AppKit/UI/TaskManager.mm | 24 +------ 9 files changed, 114 insertions(+), 100 deletions(-) create mode 100644 Ladybird/AppKit/UI/LadybirdWebViewWindow.h create mode 100644 Ladybird/AppKit/UI/LadybirdWebViewWindow.mm diff --git a/Ladybird/AppKit/CMakeLists.txt b/Ladybird/AppKit/CMakeLists.txt index 9fac7881644..481f0576a36 100644 --- a/Ladybird/AppKit/CMakeLists.txt +++ b/Ladybird/AppKit/CMakeLists.txt @@ -8,6 +8,7 @@ add_library(ladybird_impl STATIC UI/InspectorController.mm UI/LadybirdWebView.mm UI/LadybirdWebViewBridge.cpp + UI/LadybirdWebViewWindow.mm UI/Palette.mm UI/SearchPanel.mm UI/Tab.mm diff --git a/Ladybird/AppKit/UI/Inspector.h b/Ladybird/AppKit/UI/Inspector.h index 391700ac2f3..35b314234fa 100644 --- a/Ladybird/AppKit/UI/Inspector.h +++ b/Ladybird/AppKit/UI/Inspector.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2023, Tim Flynn + * Copyright (c) 2023-2024, Tim Flynn * * SPDX-License-Identifier: BSD-2-Clause */ @@ -7,11 +7,12 @@ #pragma once #import +#import @class LadybirdWebView; @class Tab; -@interface Inspector : NSWindow +@interface Inspector : LadybirdWebViewWindow - (instancetype)init:(Tab*)tab; @@ -20,6 +21,4 @@ - (void)selectHoveredElement; -@property (nonatomic, strong) LadybirdWebView* web_view; - @end diff --git a/Ladybird/AppKit/UI/Inspector.mm b/Ladybird/AppKit/UI/Inspector.mm index b7437625c67..abb62bf56ef 100644 --- a/Ladybird/AppKit/UI/Inspector.mm +++ b/Ladybird/AppKit/UI/Inspector.mm @@ -1,5 +1,5 @@ /* - * Copyright (c) 2023, Tim Flynn + * Copyright (c) 2023-2024, Tim Flynn * * SPDX-License-Identifier: BSD-2-Clause */ @@ -54,21 +54,11 @@ static constexpr NSInteger CONTEXT_MENU_DELETE_COOKIE_TAG = 4; auto tab_rect = [tab frame]; auto position_x = tab_rect.origin.x + (tab_rect.size.width - WINDOW_WIDTH) / 2; auto position_y = tab_rect.origin.y + (tab_rect.size.height - WINDOW_HEIGHT) / 2; - auto window_rect = NSMakeRect(position_x, position_y, WINDOW_WIDTH, WINDOW_HEIGHT); - auto style_mask = NSWindowStyleMaskTitled | NSWindowStyleMaskClosable | NSWindowStyleMaskMiniaturizable | NSWindowStyleMaskResizable; - self = [super initWithContentRect:window_rect - styleMask:style_mask - backing:NSBackingStoreBuffered - defer:NO]; - - if (self) { + if (self = [super initWithWebView:nil windowRect:window_rect]) { self.tab = tab; - self.web_view = [[LadybirdWebView alloc] init:nil]; - [self.web_view setPostsBoundsChangedNotifications:YES]; - m_inspector_client = make([[tab web_view] view], [[self web_view] view]); __weak Inspector* weak_self = self; @@ -139,15 +129,7 @@ static constexpr NSInteger CONTEXT_MENU_DELETE_COOKIE_TAG = 4; [NSMenu popUpContextMenu:strong_self.cookie_context_menu withEvent:event forView:strong_self.web_view]; }; - auto* scroll_view = [[NSScrollView alloc] init]; - [scroll_view setHasVerticalScroller:YES]; - [scroll_view setHasHorizontalScroller:YES]; - [scroll_view setLineScroll:24]; - - [scroll_view setContentView:self.web_view]; - [scroll_view setDocumentView:[[NSView alloc] init]]; - - [self setContentView:scroll_view]; + [self setContentView:self.web_view.enclosingScrollView]; [self setTitle:@"Inspector"]; [self setIsVisible:YES]; } diff --git a/Ladybird/AppKit/UI/LadybirdWebViewWindow.h b/Ladybird/AppKit/UI/LadybirdWebViewWindow.h new file mode 100644 index 00000000000..771ce21fce8 --- /dev/null +++ b/Ladybird/AppKit/UI/LadybirdWebViewWindow.h @@ -0,0 +1,20 @@ +/* + * Copyright (c) 2024, Tim Flynn + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#pragma once + +#import + +@class LadybirdWebView; + +@interface LadybirdWebViewWindow : NSWindow + +- (instancetype)initWithWebView:(LadybirdWebView*)web_view + windowRect:(NSRect)window_rect; + +@property (nonatomic, strong) LadybirdWebView* web_view; + +@end diff --git a/Ladybird/AppKit/UI/LadybirdWebViewWindow.mm b/Ladybird/AppKit/UI/LadybirdWebViewWindow.mm new file mode 100644 index 00000000000..926fc63c1fd --- /dev/null +++ b/Ladybird/AppKit/UI/LadybirdWebViewWindow.mm @@ -0,0 +1,75 @@ +/* + * Copyright (c) 2024, Tim Flynn + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#import +#import + +#if !__has_feature(objc_arc) +# error "This project requires ARC" +#endif + +@interface LadybirdWebViewWindow () +@end + +@implementation LadybirdWebViewWindow + +- (instancetype)initWithWebView:(LadybirdWebView*)web_view + windowRect:(NSRect)window_rect +{ + static constexpr auto style_mask = NSWindowStyleMaskTitled | NSWindowStyleMaskClosable | NSWindowStyleMaskMiniaturizable | NSWindowStyleMaskResizable; + + self = [super initWithContentRect:window_rect + styleMask:style_mask + backing:NSBackingStoreBuffered + defer:NO]; + + if (self) { + self.web_view = web_view; + if (self.web_view == nil) + self.web_view = [[LadybirdWebView alloc] init:nil]; + + [self.web_view setPostsBoundsChangedNotifications:YES]; + + auto* scroll_view = [[NSScrollView alloc] init]; + [scroll_view setHasVerticalScroller:NO]; + [scroll_view setHasHorizontalScroller:NO]; + [scroll_view setLineScroll:24]; + + [scroll_view setContentView:self.web_view]; + [scroll_view setDocumentView:[[NSView alloc] init]]; + + [[NSNotificationCenter defaultCenter] + addObserver:self + selector:@selector(onContentScroll:) + name:NSViewBoundsDidChangeNotification + object:[scroll_view contentView]]; + } + + return self; +} + +#pragma mark - Private methods + +- (void)onContentScroll:(NSNotification*)notification +{ + [self.web_view handleScroll]; +} + +#pragma mark - NSWindow + +- (void)setIsVisible:(BOOL)flag +{ + [self.web_view handleVisibility:flag]; + [super setIsVisible:flag]; +} + +- (void)setIsMiniaturized:(BOOL)flag +{ + [self.web_view handleVisibility:!flag]; + [super setIsMiniaturized:flag]; +} + +@end diff --git a/Ladybird/AppKit/UI/Tab.h b/Ladybird/AppKit/UI/Tab.h index 26055b73f82..772318ab701 100644 --- a/Ladybird/AppKit/UI/Tab.h +++ b/Ladybird/AppKit/UI/Tab.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2023, Tim Flynn + * Copyright (c) 2023-2024, Tim Flynn * * SPDX-License-Identifier: BSD-2-Clause */ @@ -9,10 +9,11 @@ #include #import +#import @class LadybirdWebView; -@interface Tab : NSWindow +@interface Tab : LadybirdWebViewWindow - (instancetype)init; - (instancetype)initAsChild:(Tab*)parent @@ -23,6 +24,4 @@ - (void)openInspector:(id)sender; - (void)onInspectorClosed; -@property (nonatomic, strong) LadybirdWebView* web_view; - @end diff --git a/Ladybird/AppKit/UI/Tab.mm b/Ladybird/AppKit/UI/Tab.mm index fdd54f413be..82704f4637c 100644 --- a/Ladybird/AppKit/UI/Tab.mm +++ b/Ladybird/AppKit/UI/Tab.mm @@ -1,5 +1,5 @@ /* - * Copyright (c) 2023-2024, Tim Flynn + * Copyright (c) 2023-2024, Tim Flynn * * SPDX-License-Identifier: BSD-2-Clause */ @@ -77,22 +77,12 @@ static constexpr CGFloat const WINDOW_HEIGHT = 800; auto screen_rect = [[NSScreen mainScreen] frame]; auto position_x = (NSWidth(screen_rect) - WINDOW_WIDTH) / 2; auto position_y = (NSHeight(screen_rect) - WINDOW_HEIGHT) / 2; - auto window_rect = NSMakeRect(position_x, position_y, WINDOW_WIDTH, WINDOW_HEIGHT); - auto style_mask = NSWindowStyleMaskTitled | NSWindowStyleMaskClosable | NSWindowStyleMaskMiniaturizable | NSWindowStyleMaskResizable; - self = [super initWithContentRect:window_rect - styleMask:style_mask - backing:NSBackingStoreBuffered - defer:NO]; - - if (self) { + if (self = [super initWithWebView:web_view windowRect:window_rect]) { // Remember last window position self.frameAutosaveName = @"window"; - self.web_view = web_view; - [self.web_view setPostsBoundsChangedNotifications:YES]; - self.favicon = [Tab defaultFavicon]; self.title = @"New Tab"; [self updateTabTitleAndFavicon]; @@ -103,28 +93,14 @@ static constexpr CGFloat const WINDOW_HEIGHT = 800; self.search_panel = [[SearchPanel alloc] init]; [self.search_panel setHidden:YES]; - auto* scroll_view = [[NSScrollView alloc] init]; - [scroll_view setHasVerticalScroller:NO]; - [scroll_view setHasHorizontalScroller:NO]; - [scroll_view setLineScroll:24]; - - [scroll_view setContentView:self.web_view]; - [scroll_view setDocumentView:[[NSView alloc] init]]; - auto* stack_view = [NSStackView stackViewWithViews:@[ self.search_panel, - scroll_view, + self.web_view.enclosingScrollView, ]]; [stack_view setOrientation:NSUserInterfaceLayoutOrientationVertical]; [stack_view setSpacing:0]; - [[NSNotificationCenter defaultCenter] - addObserver:self - selector:@selector(onContentScroll:) - name:NSViewBoundsDidChangeNotification - object:[scroll_view contentView]]; - [self setContentView:stack_view]; [[self.search_panel leadingAnchor] constraintEqualToAnchor:[self.contentView leadingAnchor]].active = YES; @@ -279,11 +255,6 @@ static constexpr CGFloat const WINDOW_HEIGHT = 800; VERIFY_NOT_REACHED(); } -- (void)onContentScroll:(NSNotification*)notification -{ - [[self web_view] handleScroll]; -} - #pragma mark - LadybirdWebViewObserver - (String const&)onCreateNewTab:(Optional const&)url @@ -422,18 +393,4 @@ static constexpr CGFloat const WINDOW_HEIGHT = 800; totalMatchCount:total_match_count]; } -#pragma mark - NSWindow - -- (void)setIsVisible:(BOOL)flag -{ - [[self web_view] handleVisibility:flag]; - [super setIsVisible:flag]; -} - -- (void)setIsMiniaturized:(BOOL)flag -{ - [[self web_view] handleVisibility:!flag]; - [super setIsMiniaturized:flag]; -} - @end diff --git a/Ladybird/AppKit/UI/TaskManager.h b/Ladybird/AppKit/UI/TaskManager.h index b14006007dc..5ed8ecf08eb 100644 --- a/Ladybird/AppKit/UI/TaskManager.h +++ b/Ladybird/AppKit/UI/TaskManager.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2024, Tim Flynn + * Copyright (c) 2024, Tim Flynn * * SPDX-License-Identifier: BSD-2-Clause */ @@ -7,13 +7,12 @@ #pragma once #import +#import @class LadybirdWebView; -@interface TaskManager : NSWindow +@interface TaskManager : LadybirdWebViewWindow - (instancetype)init; -@property (nonatomic, strong) LadybirdWebView* web_view; - @end diff --git a/Ladybird/AppKit/UI/TaskManager.mm b/Ladybird/AppKit/UI/TaskManager.mm index 32bb0f16e48..1b4037064aa 100644 --- a/Ladybird/AppKit/UI/TaskManager.mm +++ b/Ladybird/AppKit/UI/TaskManager.mm @@ -1,5 +1,5 @@ /* - * Copyright (c) 2024, Tim Flynn + * Copyright (c) 2024, Tim Flynn * * SPDX-License-Identifier: BSD-2-Clause */ @@ -32,27 +32,9 @@ static constexpr CGFloat const WINDOW_HEIGHT = 400; auto tab_rect = [[NSApp keyWindow] frame]; auto position_x = tab_rect.origin.x + (tab_rect.size.width - WINDOW_WIDTH) / 2; auto position_y = tab_rect.origin.y + (tab_rect.size.height - WINDOW_HEIGHT) / 2; - auto window_rect = NSMakeRect(position_x, position_y, WINDOW_WIDTH, WINDOW_HEIGHT); - auto style_mask = NSWindowStyleMaskTitled | NSWindowStyleMaskClosable | NSWindowStyleMaskMiniaturizable | NSWindowStyleMaskResizable; - - self = [super initWithContentRect:window_rect - styleMask:style_mask - backing:NSBackingStoreBuffered - defer:NO]; - - if (self) { - self.web_view = [[LadybirdWebView alloc] init:nil]; - [self.web_view setPostsBoundsChangedNotifications:YES]; - - auto* scroll_view = [[NSScrollView alloc] init]; - [scroll_view setHasVerticalScroller:YES]; - [scroll_view setHasHorizontalScroller:YES]; - [scroll_view setLineScroll:24]; - - [scroll_view setContentView:self.web_view]; - [scroll_view setDocumentView:[[NSView alloc] init]]; + if (self = [super initWithWebView:nil windowRect:window_rect]) { __weak TaskManager* weak_self = self; m_update_timer = Core::Timer::create_repeating(1000, [weak_self] { @@ -64,7 +46,7 @@ static constexpr CGFloat const WINDOW_HEIGHT = 400; [strong_self updateStatistics]; }); - [self setContentView:scroll_view]; + [self setContentView:self.web_view.enclosingScrollView]; [self setTitle:@"Task Manager"]; [self setIsVisible:YES];