Skip to content

Commit

Permalink
Update localizations and translation settings. Obey translate setting…
Browse files Browse the repository at this point in the history
…s when disabled so URL-Bar doesn't show the button when disabled
  • Loading branch information
Brandon-T committed Nov 11, 2024
1 parent 199a933 commit 87e2878
Show file tree
Hide file tree
Showing 18 changed files with 448 additions and 238 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import FeedKit
import Growth
import Preferences
import Shared
import SwiftUI
import UIKit
import os.log

Expand Down Expand Up @@ -98,26 +97,32 @@ extension BrowserViewController {
}

// Translate Activity
activities.append(
BasicMenuActivity(
activityType: .translatePage,
callback: { [weak self] in
guard let self = self, let tab = tab else { return }

if let scriptHandler = tab.getContentScript(name: BraveTranslateScriptHandler.scriptName)
as? BraveTranslateScriptHandler
{
scriptHandler.presentUI(on: self)
if let translationState = tab?.translationState, translationState != .unavailable,
Preferences.Translate.translateEnabled.value
{
activities.append(
BasicMenuActivity(
activityType: .translatePage,
callback: { [weak self] in
guard let self = self, let tab = tab else { return }

if tab.translationState == .active {
scriptHandler.revertTranslation()
} else if tab.translationState != .active {
scriptHandler.startTranslation(canShowToast: true)
if let scriptHandler = tab.getContentScript(
name: BraveTranslateScriptHandler.scriptName
)
as? BraveTranslateScriptHandler
{
scriptHandler.presentUI(on: self)

if tab.translationState == .active {
scriptHandler.revertTranslation()
} else if tab.translationState != .active {
scriptHandler.startTranslation(canShowToast: true)
}
}
}
}
)
)
)
}

// Find In Page Activity
activities.append(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ extension BrowserViewController: BraveTranslateScriptHandlerDelegate {

if tab === tabManager.selectedTab {
tab.translationState = state

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

showTranslateOnboarding(tab: tab) { [weak tab] translateEnabled in
Expand Down Expand Up @@ -99,9 +97,25 @@ extension BrowserViewController: BraveTranslateScriptHandlerDelegate {
}
)

popover.popoverDidDismiss = { [weak self] _ in
self?.topToolbar.locationView.translateButton.setOnboardingState(enabled: false)
completion(Preferences.Translate.translateEnabled.value)
popover.popoverDidDismiss = { [weak self, weak tab] _ in
guard let tab = tab, let self = self else { return }

self.topToolbar.locationView.translateButton.setOnboardingState(enabled: false)

if Preferences.Translate.translateEnabled.value {
if let scriptHandler = tab.getContentScript(
name: BraveTranslateScriptHandler.scriptName
)
as? BraveTranslateScriptHandler
{
scriptHandler.presentUI(on: self)
}

completion(true)
return
}

completion(false)
}
popover.present(from: self.topToolbar.locationView.translateButton, on: self)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ import MarketplaceKit
import Preferences
import SafariServices
import Shared
import SwiftUI
import Translation
import UniformTypeIdentifiers
import WebKit
import os.log
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -493,6 +493,7 @@ public class BrowserViewController: UIViewController {
Preferences.NewTabPage.backgroundMediaTypeRaw.observe(from: self)
ShieldPreferences.blockAdsAndTrackingLevelRaw.observe(from: self)
Preferences.Privacy.screenTimeEnabled.observe(from: self)
Preferences.Translate.translateEnabled.observe(from: self)

pageZoomListener = NotificationCenter.default.addObserver(
forName: PageZoomView.notificationName,
Expand Down Expand Up @@ -3404,6 +3405,11 @@ extension BrowserViewController: PreferencesObserver {
screenTimeViewController?.suppressUsageRecording = true
screenTimeViewController = nil
}
case Preferences.Translate.translateEnabled.key:
tabManager.selectedTab?.setScripts(scripts: [
.braveTranslate: Preferences.Translate.translateEnabled.value
])
tabManager.reloadSelectedTab()
default:
Logger.module.debug(
"Received a preference change for an unknown key: \(key, privacy: .public) on \(type(of: self), privacy: .public)"
Expand Down
1 change: 1 addition & 0 deletions ios/brave-ios/Sources/Brave/Frontend/Browser/Tab.swift
Original file line number Diff line number Diff line change
Expand Up @@ -533,6 +533,7 @@ class Tab: NSObject {
.mediaBackgroundPlay: Preferences.General.mediaAutoBackgrounding.value,
.nightMode: Preferences.General.nightModeEnabled.value,
.playlistMediaSource: Preferences.Playlist.webMediaSourceCompatibility.value,
.braveTranslate: Preferences.Translate.translateEnabled.value,
]

userScripts = Set(scriptPreferences.filter({ $0.value }).map({ $0.key }))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,18 +70,6 @@ class ReaderModeButton: UIButton {
)
}

// override var intrinsicContentSize: CGSize {
// var size = super.intrinsicContentSize
// let toolbarTraitCollection = UITraitCollection(
// preferredContentSizeCategory: traitCollection.toolbarButtonContentSizeCategory
// )
// size.width = UIFontMetrics(forTextStyle: .body).scaledValue(
// for: 44,
// compatibleWith: toolbarTraitCollection
// )
// return size
// }

var readerModeState: ReaderModeState {
get {
return _readerModeState
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// 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 BraveStrings
import BraveUI
import Preferences
import Shared
import SwiftUI

struct BraveTranslateSettingsView: View {
@ObservedObject
private var translateEnabled = Preferences.Translate.translateEnabled

var body: some View {
Form {
Section {
Toggle(isOn: $translateEnabled.value) {
Text(Strings.BraveTranslate.settingsTranslateEnabledOptionTitle)
}
.toggleStyle(SwitchToggleStyle(tint: .accentColor))
.listRowBackground(Color(.secondaryBraveGroupedBackground))
} footer: {
Text(Strings.BraveTranslate.settingsTranslateEnabledOptionDescription)
}
}
.listStyle(.insetGrouped)
.listBackgroundColor(Color(UIColor.braveGroupedBackground))
.navigationTitle(Strings.BraveTranslate.settingsScreenTitle)
}
}

#if DEBUG
struct BraveTranslateSettingsView_Previews: PreviewProvider {
static var previews: some View {
BraveTranslateSettingsView()
}
}
#endif
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// 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 BraveStrings
import BraveUI
import DesignSystem
import SwiftUI
Expand Down Expand Up @@ -36,11 +37,11 @@ private struct TranslationOptionsView: View {
}
}
.listStyle(.plain)
.navigationTitle("Select Language")
.navigationTitle(Strings.BraveTranslate.languageSelectionButtonTitle)
.navigationBarTitleDisplayMode(.inline)
.toolbar {
ToolbarItem(placement: .navigationBarLeading) {
Button("Cancel") {
Button(Strings.CancelString) {
dismiss()
}
}
Expand All @@ -49,19 +50,8 @@ private struct TranslationOptionsView: View {
.searchable(
text: $searchText,
placement: .navigationBarDrawer(displayMode: .always),
prompt: "Search"
prompt: Strings.BraveTranslate.searchInputTitle
)
.onChange(of: searchText) { searchText in
self.timer?.invalidate()
self.timer = Timer.scheduledTimer(
withTimeInterval: 0.1,
repeats: false,
block: { timer in
timer.invalidate()
// TODO: Filter here
}
)
}
}

private var filteredLanguages: [Locale.Language] {
Expand Down Expand Up @@ -247,7 +237,7 @@ struct TranslateToast: View {
)
.padding(.trailing)
VStack(alignment: .leading) {
Text("Page Translated")
Text(Strings.BraveTranslate.pageTranslatedTitle)
.font(.callout.weight(.semibold))
.foregroundColor(Color(braveSystemName: .textPrimary))
.padding(.bottom, 8.0)
Expand All @@ -270,11 +260,9 @@ struct TranslateToast: View {
.containerShape(RoundedRectangle(cornerRadius: 4.0, style: .continuous))
}

Text(
"To"
)
.font(.callout)
.foregroundColor(Color(braveSystemName: .textSecondary))
Text(Strings.BraveTranslate.translateFromToTitle)
.font(.callout)
.foregroundColor(Color(braveSystemName: .textSecondary))

Button {
showTargetLanguageSelection = true
Expand Down Expand Up @@ -337,7 +325,7 @@ struct TranslateToast: View {
{
return languageName
}
return "Unknown Language"
return Strings.BraveTranslate.unknownLanguageTitle
}

private var pageLanguageName: String {
Expand All @@ -346,7 +334,7 @@ struct TranslateToast: View {
{
return languageName
}
return "Unknown Language"
return Strings.BraveTranslate.unknownLanguageTitle
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,5 +97,11 @@ extension Preferences {
key: "userscript.preferences.\(UserScriptManager.ScriptType.braveLeoAIChat.rawValue)",
default: true
)

public static let translate =
Option<Bool>(
key: "userscript.preferences.\(UserScriptManager.ScriptType.braveTranslate.rawValue)",
default: true
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ struct UserScriptsDebugView: View {
@ObservedObject
private var leo = Preferences.UserScript.leo

@ObservedObject
private var translate = Preferences.UserScript.translate

var body: some View {
List {
Section {
Expand Down Expand Up @@ -94,6 +97,8 @@ struct UserScriptsDebugView: View {
Toggle("Youtube Quality", isOn: $youtubeQuality.value)

Toggle("Leo/AI-Chat", isOn: $leo.value)

Toggle("Brave Translate", isOn: $translate.value)
}
.font(.callout)
.tint(Color(braveSystemName: .primary60))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ class UserScriptManager {
.faviconFetcher,
.rewardsReporting,
.resourceDownloader,
.braveTranslate,
.nightMode,
]

Expand Down Expand Up @@ -182,7 +181,8 @@ class UserScriptManager {
? YoutubeQualityScriptHandler.userScript : nil
case .braveLeoAIChat:
return Preferences.UserScript.leo.value ? BraveLeoScriptHandler.userScript : nil
case .braveTranslate: return BraveTranslateScriptHandler.userScript
case .braveTranslate:
return Preferences.UserScript.translate.value ? BraveTranslateScriptHandler.userScript : nil
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,18 @@ class SettingsViewController: TableViewController {
)
)

section.rows.append(
Row(
text: Strings.BraveTranslate.settingsMenuTitle,
selection: { [unowned self] in
let translateSettings = UIHostingController(rootView: BraveTranslateSettingsView())
self.navigationController?.pushViewController(translateSettings, animated: true)
},
image: UIImage(braveSystemNamed: "leo.product.translate"),
accessory: .disclosureIndicator
)
)

return section
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ extension BasicMenuActivity.ActivityType {
static let translatePage: Self = .init(
id: "TranslatePage",
title: Strings.translatePage,
braveSystemImage: "leo.discover"
braveSystemImage: "leo.product.translate"
)
static let findInPage: Self = .init(
id: "FindInPage",
Expand Down
Loading

0 comments on commit 87e2878

Please sign in to comment.