From dbe1377df937a21fec3cb89e1221f677870e655a Mon Sep 17 00:00:00 2001 From: James Magahern Date: Mon, 15 Feb 2021 18:13:38 -0800 Subject: [PATCH] TabView: Animations when closing tabs as well --- App/Browser View/BrowserViewController.swift | 6 +-- App/Tabs/TabBarView.swift | 46 +++++++++++++------- App/Tabs/TabBarViewController.swift | 4 ++ 3 files changed, 37 insertions(+), 19 deletions(-) diff --git a/App/Browser View/BrowserViewController.swift b/App/Browser View/BrowserViewController.swift index e89d2fb..46d2083 100644 --- a/App/Browser View/BrowserViewController.swift +++ b/App/Browser View/BrowserViewController.swift @@ -257,6 +257,9 @@ class BrowserViewController: UIViewController, WKNavigationDelegate, WKUIDelegat self.tab = tab } } + + // Show tab bar view? + browserView.tabBarViewVisible = tabController.tabs.count > 1 }) self.view = browserView @@ -307,9 +310,6 @@ class BrowserViewController: UIViewController, WKNavigationDelegate, WKUIDelegat // Autocomplete view browserView.autocompleteView = autocompleteViewController.view - // Show tab bar view? - browserView.tabBarViewVisible = tabController.tabs.count > 1 - // Color theme browserView.titlebarView.setColorTheme(tab.colorTheme) diff --git a/App/Tabs/TabBarView.swift b/App/Tabs/TabBarView.swift index 6a72097..a1d3f14 100644 --- a/App/Tabs/TabBarView.swift +++ b/App/Tabs/TabBarView.swift @@ -11,6 +11,8 @@ class TabView: UIControl { var active: Bool = false { didSet { setNeedsLayout() } } var collapsed: Bool = false + var identifier: UUID? + let label = UILabel(frame: .zero) let closeButton = UIButton(frame: .zero) let imageView = UIImageView(image: nil) @@ -104,6 +106,7 @@ protocol TabBarViewDataSource: AnyObject { func numberOfTabs(forTabBarView: TabBarView) -> Int func tabBarView(_ tabBarView: TabBarView, titleForTabAtIndex: Int) -> String func tabBarView(_ tabBarView: TabBarView, imageForTabAtIndex: Int) -> UIImage? + func tabBarView(_ tabBarView: TabBarView, uniqueIdentifierForTabAtIndex: Int) -> UUID } protocol TabBarViewDelegate: AnyObject { @@ -143,29 +146,36 @@ class TabBarView: UIView public func reloadTabs(animated: Bool = true) { guard let dataSource = self.dataSource else { return } + var tabViewsRemoved = Set(tabViews) + let numberOfTabs = dataSource.numberOfTabs(forTabBarView: self) - while numberOfTabs < tabViews.count { - let tabView = tabViews.removeLast() - tabView.removeFromSuperview() - } - - while numberOfTabs > tabViews.count { - let newTabView = makeTabView() - if animated { newTabView.collapsed = true } - tabViews.append(newTabView) - } - - for (i, _) in tabViews.enumerated() { - self.reloadTab(atIndex: i) + for i in 0.. TabView { + private func makeTabView(withIdentifier identifier: UUID) -> TabView { let tabView = TabView() + tabView.identifier = identifier + tabView.addAction(UIAction(handler: { [unowned self, tabView] _ in guard let delegate = self.delegate else { return } guard let tabIndex = self.tabViews.firstIndex(of: tabView) else { return } @@ -244,6 +256,8 @@ class TabBarView: UIView for (i, tabView) in tabViews.enumerated() { tabContainerView.addSubview(tabView) + tabView.alpha = tabView.collapsed ? 0.0 : 1.0 + tabView.frame = CGRect( x: xOffset, y: tabContainerBounds.minY, diff --git a/App/Tabs/TabBarViewController.swift b/App/Tabs/TabBarViewController.swift index 750b3bb..eb8eae8 100644 --- a/App/Tabs/TabBarViewController.swift +++ b/App/Tabs/TabBarViewController.swift @@ -71,4 +71,8 @@ class TabBarViewController: UIViewController, TabBarViewDataSource, TabBarViewDe tabController.tabs[index].favicon } + func tabBarView(_ tabBarView: TabBarView, uniqueIdentifierForTabAtIndex index: Int) -> UUID { + tabController.tabs[index].identifier + } + }