Skip to content

Commit

Permalink
Add language selection view
Browse files Browse the repository at this point in the history
  • Loading branch information
Brandon-T committed Sep 26, 2024
1 parent 2efaa3b commit 9d59b02
Show file tree
Hide file tree
Showing 7 changed files with 452 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -156,10 +156,9 @@ extension BrowserViewController: TabManagerDelegate {
topToolbar.updateReaderModeState(.unavailable)
}

if ((selected?.getContentScript(
if (selected?.getContentScript(
name: BraveTranslateScriptHandler.scriptName
) as? BraveTranslateScriptHandler) != nil)
{
) as? BraveTranslateScriptHandler) != nil {
updateTranslateURLBar(tab: selected, state: selected?.translationState ?? .unavailable)
updatePlaylistURLBar(
tab: selected,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import DesignSystem
import Foundation
import Onboarding
import Preferences
import UIKit
import SwiftUI
import UIKit

extension BrowserViewController: BraveTranslateScriptHandlerDelegate {
func updateTranslateURLBar(tab: Tab?, state: TranslateURLBarButton.TranslateState) {
Expand All @@ -20,7 +20,7 @@ extension BrowserViewController: BraveTranslateScriptHandlerDelegate {

// translateActivity(info: state == .existingItem ? item : nil)
topToolbar.updateTranslateButtonState(state)

showTranslateOnboarding(tab: tab) { [weak tab] translateEnabled in
if let scriptHandler = tab?.getContentScript(
name: BraveTranslateScriptHandler.scriptName
Expand Down Expand Up @@ -63,6 +63,7 @@ extension BrowserViewController: BraveTranslateScriptHandlerDelegate {
guard let tab = tab, let self = self else { return }

self.topToolbar.locationView.translateButton.setOnboardingState(enabled: false)
Preferences.Translate.translateEnabled.value = true

if let scriptHandler = tab.getContentScript(
name: BraveTranslateScriptHandler.scriptName
Expand Down Expand Up @@ -112,7 +113,7 @@ extension BrowserViewController: BraveTranslateScriptHandlerDelegate {
func presentToast(_ languageInfo: BraveTranslateLanguageInfo) {
let popover = PopoverController(
content: TranslateToast(languageInfo: languageInfo),
autoLayoutConfiguration: .phoneWidth
autoLayoutConfiguration: nil
)

popover.popoverDidDismiss = { [weak self] _ in
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//
// File.swift
//
//
//
// Created by Brandon T on 2024-08-23.
//
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ class TranslateURLBarButton: UIButton {
fatalError()
}

override func layoutSubviews() {
super.layoutSubviews()
gradientView.frame = bounds
}

override var isSelected: Bool {
didSet {
updateAppearance()
Expand Down Expand Up @@ -96,39 +101,24 @@ class TranslateURLBarButton: UIButton {
}
}

private lazy var gradientLayer = CAGradientLayer().then {
let gradient = BraveGradient(
stops: [
.init(color: UIColor(rgb: 0xFA7250), position: 0.0),
.init(color: UIColor(rgb: 0xFF1893), position: 0.43),
.init(color: UIColor(rgb: 0xA78AFF), position: 1.0),
],
angle: .figmaDegrees(314.42)
)

$0.frame = self.bounds
$0.type = gradient.type
$0.colors = gradient.stops.map(\.color.cgColor)
$0.locations = gradient.stops.map({ NSNumber(value: $0.position) })
$0.startPoint = gradient.startPoint
$0.endPoint = gradient.endPoint

let mask = CALayer()
mask.contents = imageIcon?.cgImage
mask.frame = $0.bounds
$0.mask = mask
}
private let gradientView = GradientView(braveSystemName: .iconsActive)

func setOnboardingState(enabled: Bool) {
if enabled {
gradientLayer.frame = imageView?.bounds ?? self.bounds
gradientLayer.mask?.frame = gradientLayer.bounds

imageView?.layer.addSublayer(gradientLayer)
setImage(nil, for: .normal)
addSubview(gradientView)
gradientView.frame = bounds
gradientView.mask = imageView
} else {
gradientLayer.removeFromSuperlayer()
if let imageView = imageView {
// gradientView.mask = imageView automatically removes the imageView from the button :o!
// So we have to add it back lol
addSubview(imageView)
}

gradientView.mask = nil
gradientView.removeFromSuperview()
setImage(imageIcon, for: .normal)
updateIconSize()
}
}

Expand Down
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()
}
}
Loading

0 comments on commit 9d59b02

Please sign in to comment.