From e0f95989d422e5318eeecc50a26ad7d5a2a43f52 Mon Sep 17 00:00:00 2001 From: Ilya Laktyushin Date: Thu, 25 Jul 2024 17:04:44 +0200 Subject: [PATCH] Browser improvements --- .../Sources/BrowserAddressListComponent.swift | 9 +++++ .../BrowserAddressListItemComponent.swift | 10 ++++-- .../BrowserUI/Sources/BrowserContent.swift | 2 +- .../Sources/BrowserDocumentContent.swift | 4 +-- .../Sources/BrowserInstantPageContent.swift | 8 ++--- .../BrowserUI/Sources/BrowserPdfContent.swift | 4 +-- .../BrowserUI/Sources/BrowserScreen.swift | 5 +-- .../BrowserUI/Sources/BrowserWebContent.swift | 35 +++++++++++++------ .../Sources/SectionHeaderComponent.swift | 8 ++++- 9 files changed, 61 insertions(+), 24 deletions(-) diff --git a/submodules/BrowserUI/Sources/BrowserAddressListComponent.swift b/submodules/BrowserUI/Sources/BrowserAddressListComponent.swift index 34c91738e7..2070587445 100644 --- a/submodules/BrowserUI/Sources/BrowserAddressListComponent.swift +++ b/submodules/BrowserUI/Sources/BrowserAddressListComponent.swift @@ -13,17 +13,20 @@ final class BrowserAddressListComponent: Component { let context: AccountContext let theme: PresentationTheme let strings: PresentationStrings + let insets: UIEdgeInsets let navigateTo: (String) -> Void init( context: AccountContext, theme: PresentationTheme, strings: PresentationStrings, + insets: UIEdgeInsets, navigateTo: @escaping (String) -> Void ) { self.context = context self.theme = theme self.strings = strings + self.insets = insets self.navigateTo = navigateTo } @@ -37,6 +40,9 @@ final class BrowserAddressListComponent: Component { if lhs.strings !== rhs.strings { return false } + if lhs.insets != rhs.insets { + return false + } return true } @@ -211,6 +217,7 @@ final class BrowserAddressListComponent: Component { theme: component.theme, style: .plain, title: sectionTitle, + insets: component.insets, actionTitle: section.id == 0 ? "Clear" : nil, action: { [weak self] in if let self, let component = self.component { @@ -292,6 +299,7 @@ final class BrowserAddressListComponent: Component { webPage: webPage!, message: itemMessage, hasNext: true, + insets: component.insets, action: { if let url = webPage?.content.url { navigateTo(url) @@ -393,6 +401,7 @@ final class BrowserAddressListComponent: Component { webPage: TelegramMediaWebpage(webpageId: EngineMedia.Id(namespace: 0, id: 0), content: .Loaded(TelegramMediaWebpageLoadedContent(url: "https://telegram.org", displayUrl: "https://telegram.org", hash: 0, type: nil, websiteName: "Telegram", title: "Telegram Telegram", text: "Telegram", embedUrl: nil, embedType: nil, embedSize: nil, duration: nil, author: nil, isMediaLargeByDefault: nil, image: nil, file: nil, story: nil, attributes: [], instantPage: nil))), message: nil, hasNext: true, + insets: .zero, action: {} )), environment: {}, diff --git a/submodules/BrowserUI/Sources/BrowserAddressListItemComponent.swift b/submodules/BrowserUI/Sources/BrowserAddressListItemComponent.swift index 49ebcb2c20..30ad83d58b 100644 --- a/submodules/BrowserUI/Sources/BrowserAddressListItemComponent.swift +++ b/submodules/BrowserUI/Sources/BrowserAddressListItemComponent.swift @@ -16,6 +16,7 @@ final class BrowserAddressListItemComponent: Component { let webPage: TelegramMediaWebpage var message: Message? let hasNext: Bool + let insets: UIEdgeInsets let action: () -> Void init( @@ -24,6 +25,7 @@ final class BrowserAddressListItemComponent: Component { webPage: TelegramMediaWebpage, message: Message?, hasNext: Bool, + insets: UIEdgeInsets, action: @escaping () -> Void ) { self.context = context @@ -31,6 +33,7 @@ final class BrowserAddressListItemComponent: Component { self.webPage = webPage self.message = message self.hasNext = hasNext + self.insets = insets self.action = action } @@ -44,6 +47,9 @@ final class BrowserAddressListItemComponent: Component { if lhs.hasNext != rhs.hasNext { return false } + if lhs.insets != rhs.insets { + return false + } return true } @@ -92,7 +98,7 @@ final class BrowserAddressListItemComponent: Component { let iconSize = CGSize(width: 40.0, height: 40.0) let height: CGFloat = 60.0 - let leftInset: CGFloat = 11.0 + iconSize.width + 11.0 + let leftInset: CGFloat = component.insets.left + 11.0 + iconSize.width + 11.0 let rightInset: CGFloat = 16.0 let titleSpacing: CGFloat = 2.0 @@ -181,7 +187,7 @@ final class BrowserAddressListItemComponent: Component { } - let iconFrame = CGRect(origin: CGPoint(x: 11.0, y: floorToScreenPixels((height - iconSize.height) / 2.0)), size: iconSize) + let iconFrame = CGRect(origin: CGPoint(x: 11.0 + component.insets.left, y: floorToScreenPixels((height - iconSize.height) / 2.0)), size: iconSize) let iconImageLayout = self.icon.asyncLayout() var iconImageApply: (() -> Void)? diff --git a/submodules/BrowserUI/Sources/BrowserContent.swift b/submodules/BrowserUI/Sources/BrowserContent.swift index f9f1687d93..7422bed87e 100644 --- a/submodules/BrowserUI/Sources/BrowserContent.swift +++ b/submodules/BrowserUI/Sources/BrowserContent.swift @@ -188,7 +188,7 @@ protocol BrowserContent: UIView { func addToRecentlyVisited() - func updateLayout(size: CGSize, insets: UIEdgeInsets, fullInsets: UIEdgeInsets, transition: ComponentTransition) + func updateLayout(size: CGSize, insets: UIEdgeInsets, fullInsets: UIEdgeInsets, safeInsets: UIEdgeInsets, transition: ComponentTransition) func makeContentSnapshotView() -> UIView? } diff --git a/submodules/BrowserUI/Sources/BrowserDocumentContent.swift b/submodules/BrowserUI/Sources/BrowserDocumentContent.swift index 1fae1b7ad3..316ab28538 100644 --- a/submodules/BrowserUI/Sources/BrowserDocumentContent.swift +++ b/submodules/BrowserUI/Sources/BrowserDocumentContent.swift @@ -101,7 +101,7 @@ final class BrowserDocumentContent: UIView, BrowserContent, WKNavigationDelegate self.webView.underPageBackgroundColor = presentationData.theme.list.plainBackgroundColor } if let (size, insets, fullInsets) = self.validLayout { - self.updateLayout(size: size, insets: insets, fullInsets: fullInsets, transition: .immediate) + self.updateLayout(size: size, insets: insets, fullInsets: fullInsets, safeInsets: .zero, transition: .immediate) } } @@ -240,7 +240,7 @@ final class BrowserDocumentContent: UIView, BrowserContent, WKNavigationDelegate } private var validLayout: (CGSize, UIEdgeInsets, UIEdgeInsets)? - func updateLayout(size: CGSize, insets: UIEdgeInsets, fullInsets: UIEdgeInsets, transition: ComponentTransition) { + func updateLayout(size: CGSize, insets: UIEdgeInsets, fullInsets: UIEdgeInsets, safeInsets: UIEdgeInsets, transition: ComponentTransition) { self.validLayout = (size, insets, fullInsets) self.previousScrollingOffset = ScrollingOffsetState(value: self.webView.scrollView.contentOffset.y, isDraggingOrDecelerating: self.webView.scrollView.isDragging || self.webView.scrollView.isDecelerating) diff --git a/submodules/BrowserUI/Sources/BrowserInstantPageContent.swift b/submodules/BrowserUI/Sources/BrowserInstantPageContent.swift index a9972dbe90..2ceff5ec34 100644 --- a/submodules/BrowserUI/Sources/BrowserInstantPageContent.swift +++ b/submodules/BrowserUI/Sources/BrowserInstantPageContent.swift @@ -301,7 +301,7 @@ final class BrowserInstantPageContent: UIView, BrowserContent, UIScrollViewDeleg guard let (size, insets, fullInsets) = self.containerLayout else { return } - self.updateLayout(size: size, insets: insets, fullInsets: fullInsets, transition: transition) + self.updateLayout(size: size, insets: insets, fullInsets: fullInsets, safeInsets: .zero, transition: transition) } func reload() { @@ -375,11 +375,11 @@ final class BrowserInstantPageContent: UIView, BrowserContent, UIScrollViewDeleg scrollView.setContentOffset(CGPoint(x: 0.0, y: -scrollView.contentInset.top), animated: true) } - func updateLayout(size: CGSize, insets: UIEdgeInsets, fullInsets: UIEdgeInsets, transition: ComponentTransition) { - self.updateLayout(size: size, insets: insets, fullInsets: fullInsets, transition: transition.containedViewLayoutTransition) + func updateLayout(size: CGSize, insets: UIEdgeInsets, fullInsets: UIEdgeInsets, safeInsets: UIEdgeInsets, transition: ComponentTransition) { + self.updateLayout(size: size, insets: insets, fullInsets: fullInsets, safeInsets: safeInsets, transition: transition.containedViewLayoutTransition) } - func updateLayout(size: CGSize, insets: UIEdgeInsets, fullInsets: UIEdgeInsets, transition: ContainedViewLayoutTransition) { + func updateLayout(size: CGSize, insets: UIEdgeInsets, fullInsets: UIEdgeInsets, safeInsets: UIEdgeInsets, transition: ContainedViewLayoutTransition) { self.containerLayout = (size, insets, fullInsets) var updateVisibleItems = false diff --git a/submodules/BrowserUI/Sources/BrowserPdfContent.swift b/submodules/BrowserUI/Sources/BrowserPdfContent.swift index e037f45663..cb61825774 100644 --- a/submodules/BrowserUI/Sources/BrowserPdfContent.swift +++ b/submodules/BrowserUI/Sources/BrowserPdfContent.swift @@ -111,7 +111,7 @@ final class BrowserPdfContent: UIView, BrowserContent, WKNavigationDelegate, WKU self.backgroundColor = presentationData.theme.list.plainBackgroundColor } if let (size, insets, fullInsets) = self.validLayout { - self.updateLayout(size: size, insets: insets, fullInsets: fullInsets, transition: .immediate) + self.updateLayout(size: size, insets: insets, fullInsets: fullInsets, safeInsets: .zero, transition: .immediate) } } @@ -250,7 +250,7 @@ final class BrowserPdfContent: UIView, BrowserContent, WKNavigationDelegate, WKU } private var validLayout: (CGSize, UIEdgeInsets, UIEdgeInsets)? - func updateLayout(size: CGSize, insets: UIEdgeInsets, fullInsets: UIEdgeInsets, transition: ComponentTransition) { + func updateLayout(size: CGSize, insets: UIEdgeInsets, fullInsets: UIEdgeInsets, safeInsets: UIEdgeInsets, transition: ComponentTransition) { self.validLayout = (size, insets, fullInsets) self.previousScrollingOffset = ScrollingOffsetState(value: self.scrollView.contentOffset.y, isDraggingOrDecelerating: self.scrollView.isDragging || self.scrollView.isDecelerating) diff --git a/submodules/BrowserUI/Sources/BrowserScreen.swift b/submodules/BrowserUI/Sources/BrowserScreen.swift index 661fbbeedb..e6f681aa45 100644 --- a/submodules/BrowserUI/Sources/BrowserScreen.swift +++ b/submodules/BrowserUI/Sources/BrowserScreen.swift @@ -261,6 +261,7 @@ private final class BrowserScreenComponent: CombinedComponent { context: context.component.context, theme: environment.theme, strings: environment.strings, + insets: UIEdgeInsets(top: 0.0, left: environment.safeInsets.left, bottom: 0.0, right: environment.safeInsets.right), navigateTo: { url in performAction.invoke(.navigateTo(url)) } @@ -1370,8 +1371,8 @@ private final class BrowserContentComponent: Component { let bottomInset = (49.0 + component.insets.bottom) * (1.0 - component.scrollingPanelOffsetFraction) let insets = UIEdgeInsets(top: topInset, left: component.insets.left, bottom: bottomInset, right: component.insets.right) let fullInsets = UIEdgeInsets(top: component.insets.top + component.navigationBarHeight, left: component.insets.left, bottom: 49.0 + component.insets.bottom, right: component.insets.right) - - component.content.updateLayout(size: availableSize, insets: insets, fullInsets: fullInsets, transition: transition) + + component.content.updateLayout(size: availableSize, insets: insets, fullInsets: fullInsets, safeInsets: component.insets, transition: transition) transition.setFrame(view: component.content, frame: CGRect(origin: .zero, size: availableSize)) return availableSize diff --git a/submodules/BrowserUI/Sources/BrowserWebContent.swift b/submodules/BrowserUI/Sources/BrowserWebContent.swift index 921447f526..6844c5929c 100644 --- a/submodules/BrowserUI/Sources/BrowserWebContent.swift +++ b/submodules/BrowserUI/Sources/BrowserWebContent.swift @@ -117,11 +117,23 @@ private final class TonSchemeHandler: NSObject, WKURLSchemeHandler { } } +final class WebView: WKWebView { + var customBottomInset: CGFloat = 0.0 { + didSet { + self.setNeedsLayout() + } + } + + override var safeAreaInsets: UIEdgeInsets { + return UIEdgeInsets(top: 0.0, left: 0.0, bottom: self.customBottomInset, right: 0.0) + } +} + final class BrowserWebContent: UIView, BrowserContent, WKNavigationDelegate, WKUIDelegate, UIScrollViewDelegate { private let context: AccountContext private var presentationData: PresentationData - let webView: WKWebView + let webView: WebView private let errorView: ComponentHostView private var currentError: Error? @@ -170,7 +182,7 @@ final class BrowserWebContent: UIView, BrowserContent, WKNavigationDelegate, WKU configuration.mediaPlaybackRequiresUserAction = false } - self.webView = WKWebView(frame: CGRect(), configuration: configuration) + self.webView = WebView(frame: CGRect(), configuration: configuration) self.webView.allowsLinkPreview = true if #available(iOS 11.0, *) { @@ -245,8 +257,8 @@ final class BrowserWebContent: UIView, BrowserContent, WKNavigationDelegate, WKU self.backgroundColor = presentationData.theme.list.plainBackgroundColor self.webView.underPageBackgroundColor = presentationData.theme.list.plainBackgroundColor } - if let (size, insets, fullInsets) = self.validLayout { - self.updateLayout(size: size, insets: insets, fullInsets: fullInsets, transition: .immediate) + if let (size, insets, fullInsets, safeInsets) = self.validLayout { + self.updateLayout(size: size, insets: insets, fullInsets: fullInsets, safeInsets: safeInsets, transition: .immediate) } } @@ -434,13 +446,13 @@ final class BrowserWebContent: UIView, BrowserContent, WKNavigationDelegate, WKU self.webView.scrollView.setContentOffset(CGPoint(x: 0.0, y: -self.webView.scrollView.contentInset.top), animated: true) } - private var validLayout: (CGSize, UIEdgeInsets, UIEdgeInsets)? - func updateLayout(size: CGSize, insets: UIEdgeInsets, fullInsets: UIEdgeInsets, transition: ComponentTransition) { - self.validLayout = (size, insets, fullInsets) + private var validLayout: (CGSize, UIEdgeInsets, UIEdgeInsets, UIEdgeInsets)? + func updateLayout(size: CGSize, insets: UIEdgeInsets, fullInsets: UIEdgeInsets, safeInsets: UIEdgeInsets, transition: ComponentTransition) { + self.validLayout = (size, insets, fullInsets, safeInsets) self.previousScrollingOffset = ScrollingOffsetState(value: self.webView.scrollView.contentOffset.y, isDraggingOrDecelerating: self.webView.scrollView.isDragging || self.webView.scrollView.isDecelerating) - let webViewFrame = CGRect(origin: CGPoint(x: insets.left, y: insets.top), size: CGSize(width: size.width - insets.left - insets.right, height: size.height - insets.top - fullInsets.bottom)) + let webViewFrame = CGRect(origin: CGPoint(x: insets.left, y: insets.top), size: CGSize(width: size.width - insets.left - insets.right, height: size.height - insets.top)) var refresh = false if self.webView.frame.width > 0 && webViewFrame.width != self.webView.frame.width { refresh = true @@ -451,6 +463,9 @@ final class BrowserWebContent: UIView, BrowserContent, WKNavigationDelegate, WKU self.webView.reloadInputViews() } + self.webView.scrollView.contentInset = UIEdgeInsets(top: 0.0, left: 0.0, bottom: fullInsets.bottom, right: 0.0) + self.webView.customBottomInset = max(insets.bottom, safeInsets.bottom) +// self.webView.scrollView.contentInset = UIEdgeInsets(top: 0.0, left: 0.0, bottom: 34.0, right: 0.0) self.webView.scrollView.scrollIndicatorInsets = UIEdgeInsets(top: 0.0, left: -insets.left, bottom: 0.0, right: -insets.right) self.webView.scrollView.horizontalScrollIndicatorInsets = UIEdgeInsets(top: 0.0, left: -insets.left, bottom: 0.0, right: -insets.right) @@ -646,8 +661,8 @@ final class BrowserWebContent: UIView, BrowserContent, WKNavigationDelegate, WKU } else { self.currentError = nil } - if let (size, insets, fullInsets) = self.validLayout { - self.updateLayout(size: size, insets: insets, fullInsets: fullInsets, transition: .immediate) + if let (size, insets, fullInsets, safeInsets) = self.validLayout { + self.updateLayout(size: size, insets: insets, fullInsets: fullInsets, safeInsets: safeInsets, transition: .immediate) } } diff --git a/submodules/BrowserUI/Sources/SectionHeaderComponent.swift b/submodules/BrowserUI/Sources/SectionHeaderComponent.swift index 6ebe3e80c9..294d5c1a34 100644 --- a/submodules/BrowserUI/Sources/SectionHeaderComponent.swift +++ b/submodules/BrowserUI/Sources/SectionHeaderComponent.swift @@ -13,6 +13,7 @@ final class SectionHeaderComponent: Component { let theme: PresentationTheme let style: Style let title: String + let insets: UIEdgeInsets let actionTitle: String? let action: (() -> Void)? @@ -20,12 +21,14 @@ final class SectionHeaderComponent: Component { theme: PresentationTheme, style: Style, title: String, + insets: UIEdgeInsets, actionTitle: String?, action: (() -> Void)? ) { self.theme = theme self.style = style self.title = title + self.insets = insets self.actionTitle = actionTitle self.action = action } @@ -40,6 +43,9 @@ final class SectionHeaderComponent: Component { if lhs.title != rhs.title { return false } + if lhs.insets != rhs.insets { + return false + } if lhs.actionTitle != rhs.actionTitle { return false } @@ -73,7 +79,7 @@ final class SectionHeaderComponent: Component { self.state = state let height: CGFloat = 28.0 - let leftInset: CGFloat = 16.0 + let leftInset: CGFloat = 16.0 + component.insets.left let rightInset: CGFloat = 0.0 let previousTitleFrame = self.title.view?.frame