-
Notifications
You must be signed in to change notification settings - Fork 893
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
452 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
ios/brave-ios/Sources/Brave/Frontend/Browser/Toolbars/UrlBar/File.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
// | ||
// File.swift | ||
// | ||
// | ||
// | ||
// Created by Brandon T on 2024-08-23. | ||
// | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
122 changes: 122 additions & 0 deletions
122
ios/brave-ios/Sources/Brave/Frontend/Browser/Translate/BravePopup.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
// Copyright 2024 The Brave Authors. All rights reserved. | ||
// This Source Code Form is subject to the terms of the Mozilla Public | ||
// License, v. 2.0. If a copy of the MPL was not distributed with this | ||
// file, You can obtain one at https://mozilla.org/MPL/2.0/. | ||
|
||
import BraveUI | ||
import Foundation | ||
import Shared | ||
import SwiftUI | ||
import UIKit | ||
|
||
struct BravePopupViewModifier<PopupContent>: ViewModifier | ||
where PopupContent: View { | ||
@Binding var isPresented: Bool | ||
let content: () -> PopupContent | ||
|
||
func body(content: Content) -> some View { | ||
content | ||
.background( | ||
BravePopupView( | ||
isPresented: self.$isPresented, | ||
content: self.content | ||
) | ||
) | ||
} | ||
} | ||
|
||
extension View { | ||
func bravePopup<Content>( | ||
isPresented: Binding<Bool>, | ||
@ViewBuilder content: @escaping () -> Content | ||
) -> some View where Content: View { | ||
self.modifier( | ||
BravePopupViewModifier( | ||
isPresented: isPresented, | ||
content: content | ||
) | ||
) | ||
} | ||
} | ||
|
||
struct BravePopupView<Content: View>: UIViewControllerRepresentable { | ||
@Binding var isPresented: Bool | ||
private var content: Content | ||
|
||
init( | ||
isPresented: Binding<Bool>, | ||
@ViewBuilder content: () -> Content | ||
) { | ||
self._isPresented = isPresented | ||
self.content = content() | ||
} | ||
|
||
func makeUIViewController(context: Context) -> UIViewController { | ||
.init() | ||
} | ||
|
||
func updateUIViewController(_ uiViewController: UIViewController, context: Context) { | ||
if isPresented { | ||
guard uiViewController.presentedViewController == nil | ||
else { | ||
// The system dismissed our Popup automatically, but never updated our presentation state | ||
// It usually does this if you present another Popup or sheet | ||
// Manually update it | ||
if let controller = context.coordinator.presentedViewController?.value | ||
as? PopupViewController<Content> | ||
{ | ||
DispatchQueue.main.async { | ||
controller.dismiss(animated: true) { | ||
context.coordinator.presentedViewController = nil | ||
self.isPresented = false | ||
} | ||
} | ||
} else if context.coordinator.presentedViewController != nil { | ||
DispatchQueue.main.async { | ||
isPresented = false | ||
} | ||
} | ||
return | ||
} | ||
|
||
if let parent = uiViewController.parent, !parent.isBeingDismissed { | ||
let controller = PopupViewController(rootView: content, isDismissable: true) | ||
context.coordinator.presentedViewController = .init(controller) | ||
|
||
DispatchQueue.main.async { | ||
if KeyboardHelper.defaultHelper.currentState != nil { | ||
UIApplication.shared.sendAction( | ||
#selector(UIResponder.resignFirstResponder), | ||
to: nil, | ||
from: nil, | ||
for: nil | ||
) | ||
|
||
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) { | ||
uiViewController.present(controller, animated: true) | ||
} | ||
} else { | ||
uiViewController.present(controller, animated: true) | ||
} | ||
} | ||
} | ||
} else { | ||
if let presentedViewController = context.coordinator.presentedViewController?.value, | ||
presentedViewController == uiViewController.presentedViewController | ||
{ | ||
uiViewController.presentedViewController?.dismiss(animated: true) { | ||
context.coordinator.presentedViewController = nil | ||
self.isPresented = false | ||
} | ||
} | ||
} | ||
} | ||
|
||
class Coordinator { | ||
var presentedViewController: WeakRef<UIViewController>? | ||
} | ||
|
||
func makeCoordinator() -> Coordinator { | ||
Coordinator() | ||
} | ||
} |
Oops, something went wrong.