Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Zoom menu items; Menu Items validation #105

Merged
merged 2 commits into from
May 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions DuckDuckGo/BrowserTab/Model/Tab.swift
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,7 @@ final class Tab: NSObject {
private func setupWebView(shouldLoadInBackground: Bool) {
webView.navigationDelegate = self
webView.allowsBackForwardNavigationGestures = true
webView.allowsMagnification = true

subscribeToUserScripts()
subscribeToOpenExternalUrlEvents()
Expand Down
26 changes: 26 additions & 0 deletions DuckDuckGo/BrowserTab/View/WebView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,32 @@ final class WebView: WKWebView {

]

static private let maxMagnification: CGFloat = 3.0
static private let minMagnification: CGFloat = 0.5
static private let magnificationStep: CGFloat = 0.1

var canZoomToActualSize: Bool {
self.window != nil && self.magnification != 1.0
}

var canZoomIn: Bool {
self.window != nil && self.magnification < Self.maxMagnification
}

var canZoomOut: Bool {
self.window != nil && self.magnification > Self.minMagnification
}

func zoomIn() {
guard canZoomIn else { return }
self.magnification = min(self.magnification + Self.magnificationStep, Self.maxMagnification)
}

func zoomOut() {
guard canZoomOut else { return }
self.magnification = max(self.magnification - Self.magnificationStep, Self.minMagnification)
}

deinit {
self.configuration.userContentController.removeAllUserScripts()
}
Expand Down
4 changes: 3 additions & 1 deletion DuckDuckGo/Common/Localizables/UserText.swift
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,9 @@ struct UserText {
static let editFavorite = NSLocalizedString("edit.favorite", value: "Edit Favorite", comment: "Header of the view that edits a favorite bookmark")
static let removeFromFavorites = NSLocalizedString("remove.from.favorites", value: "Remove from Favorites", comment: "Button for removing bookmarks from favorites")
static let bookmarkThisPage = NSLocalizedString("bookmark.this.page", value: "Bookmark This Page...", comment: "Menu item for bookmarking current page")


static let zoom = NSLocalizedString("zoom", value: "Zoom", comment: "Menu with Zooming commands")

static let emailOptionsMenuItem = NSLocalizedString("email.optionsMenu", value: "Email Protection", comment: "Menu item email feature")
static let emailOptionsMenuCreateAddressSubItem = NSLocalizedString("email.optionsMenu.createAddress", value: "Create a Duck Address", comment: "Create an email alias sub menu item")
static let emailOptionsMenuViewDashboardSubItem = NSLocalizedString("email.optionsMenu.viewDashboard", value: "View Dashboard", comment: "View email dashboard sub menu item")
Expand Down
95 changes: 40 additions & 55 deletions DuckDuckGo/Main/MainViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,7 @@ final class MainViewController: NSViewController {
let tabCollectionViewModel: TabCollectionViewModel

private var selectedTabViewModelCancellable: AnyCancellable?
private var canGoForwardCancellable: AnyCancellable?
private var canGoBackCancellable: AnyCancellable?
private var navigationalCancellables = Set<AnyCancellable>()
private var canBookmarkCancellable: AnyCancellable?
private var canInsertLastRemovedTabCancellable: AnyCancellable?
private var findInPageCancellable: AnyCancellable?
Expand All @@ -58,7 +57,6 @@ final class MainViewController: NSViewController {

listenToKeyDownEvents()
subscribeToSelectedTabViewModel()
subscribeToCanInsertLastRemovedTab()
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All validation moved to MainMenuActions.swift

findInPageContainerView.applyDropShadow()
}

Expand All @@ -70,18 +68,6 @@ final class MainViewController: NSViewController {
fatalError("Default AppKit State Restoration should not be used")
}

func windowDidBecomeMain() {
NSApplication.shared.mainMenuTyped.setWindowRelatedMenuItems(enabled: true)

updateBackMenuItem()
updateForwardMenuItem()
updateReopenLastClosedTabMenuItem()
}

func windowDidResignMain() {
NSApplication.shared.mainMenuTyped.setWindowRelatedMenuItems(enabled: false)
}

func windowWillClose() {
if let monitor = keyDownMonitor {
NSEvent.removeMonitor(monitor)
Expand Down Expand Up @@ -132,43 +118,32 @@ final class MainViewController: NSViewController {

private func subscribeToSelectedTabViewModel() {
selectedTabViewModelCancellable = tabCollectionViewModel.$selectedTabViewModel.receive(on: DispatchQueue.main).sink { [weak self] _ in
self?.navigationalCancellables = []
self?.subscribeToCanGoBackForward()
self?.subscribeToFindInPage()
self?.subscribeToCanBookmark()
}
}

private func subscribeToFindInPage() {
findInPageCancellable?.cancel()
let model = tabCollectionViewModel.selectedTabViewModel?.findInPage
findInPageCancellable = model?.$visible.receive(on: DispatchQueue.main).sink { [weak self] _ in
model?.$visible.receive(on: DispatchQueue.main).sink { [weak self] _ in
self?.updateFindInPage()
}
}.store(in: &self.navigationalCancellables)
}

private func subscribeToCanGoBackForward() {
canGoBackCancellable?.cancel()
canGoBackCancellable = tabCollectionViewModel.selectedTabViewModel?.$canGoBack.receive(on: DispatchQueue.main).sink { [weak self] _ in
tabCollectionViewModel.selectedTabViewModel?.$canGoBack.receive(on: DispatchQueue.main).sink { [weak self] _ in
self?.updateBackMenuItem()
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only left validation for states that can change while Main Menu is open; Other validation moved to MainMenuActions

}
canGoForwardCancellable?.cancel()
canGoForwardCancellable = tabCollectionViewModel.selectedTabViewModel?.$canGoForward.receive(on: DispatchQueue.main).sink { [weak self] _ in
}.store(in: &self.navigationalCancellables)
tabCollectionViewModel.selectedTabViewModel?.$canGoForward.receive(on: DispatchQueue.main).sink { [weak self] _ in
self?.updateForwardMenuItem()
}
}

private func subscribeToCanBookmark() {
canBookmarkCancellable?.cancel()
canBookmarkCancellable = tabCollectionViewModel.selectedTabViewModel?.$canBeBookmarked.receive(on: DispatchQueue.main).sink { [weak self] _ in
self?.updateBookmarksMenu()
}
}

private func subscribeToCanInsertLastRemovedTab() {
canInsertLastRemovedTabCancellable?.cancel()
canInsertLastRemovedTabCancellable = tabCollectionViewModel.$canInsertLastRemovedTab.receive(on: DispatchQueue.main).sink { [weak self] _ in
self?.updateReopenLastClosedTabMenuItem()
}
}.store(in: &self.navigationalCancellables)
tabCollectionViewModel.selectedTabViewModel?.$canReload.receive(on: DispatchQueue.main).sink { [weak self] _ in
self?.updateReloadMenuItem()
}.store(in: &self.navigationalCancellables)
tabCollectionViewModel.selectedTabViewModel?.$isLoading.receive(on: DispatchQueue.main).sink { [weak self] _ in
self?.updateStopMenuItem()
}.store(in: &self.navigationalCancellables)
}

private func updateFindInPage() {
Expand All @@ -191,53 +166,63 @@ final class MainViewController: NSViewController {
}

private func updateBackMenuItem() {
guard let selectedTabViewModel = tabCollectionViewModel.selectedTabViewModel else {
guard self.view.window?.isMainWindow == true,
let selectedTabViewModel = tabCollectionViewModel.selectedTabViewModel
else {
os_log("MainViewController: No tab view model selected", type: .error)
return
}
guard let backMenuItem = NSApplication.shared.mainMenuTyped.backMenuItem else {
os_log("MainViewController: Failed to get reference to back menu item", type: .error)
assertionFailure("MainViewController: Failed to get reference to back menu item")
return
}

backMenuItem.isEnabled = selectedTabViewModel.canGoBack
}

func updateForwardMenuItem() {
guard let selectedTabViewModel = tabCollectionViewModel.selectedTabViewModel else {
private func updateForwardMenuItem() {
guard self.view.window?.isMainWindow == true,
let selectedTabViewModel = tabCollectionViewModel.selectedTabViewModel
else {
os_log("MainViewController: No tab view model selected", type: .error)
return
}
guard let forwardMenuItem = NSApplication.shared.mainMenuTyped.forwardMenuItem else {
os_log("MainViewController: Failed to get reference to back menu item", type: .error)
assertionFailure("MainViewController: Failed to get reference to Forward menu item")
return
}

forwardMenuItem.isEnabled = selectedTabViewModel.canGoForward
}

private func updateBookmarksMenu() {
guard let selectedTabViewModel = tabCollectionViewModel.selectedTabViewModel else {
private func updateReloadMenuItem() {
guard self.view.window?.isMainWindow == true,
let selectedTabViewModel = tabCollectionViewModel.selectedTabViewModel
else {
os_log("MainViewController: No tab view model selected", type: .error)
return
}
guard let bookmarkThisPageMenuItem = NSApplication.shared.mainMenuTyped.bookmarkThisPageMenuItem,
let favoriteThisPageMenuItem = NSApplication.shared.mainMenuTyped.favoriteThisPageMenuItem else {
os_log("MainViewController: Failed to get reference to bookmarks menu items", type: .error)
guard let reloadMenuItem = NSApplication.shared.mainMenuTyped.reloadMenuItem else {
assertionFailure("MainViewController: Failed to get reference to Reload menu item")
return
}

bookmarkThisPageMenuItem.isEnabled = selectedTabViewModel.canBeBookmarked
favoriteThisPageMenuItem.isEnabled = selectedTabViewModel.canBeBookmarked
reloadMenuItem.isEnabled = selectedTabViewModel.canReload
}

func updateReopenLastClosedTabMenuItem() {
guard let reopenLastClosedTabMenuItem = NSApplication.shared.mainMenuTyped.reopenLastClosedTabMenuItem else {
os_log("MainViewController: Failed to get reference to back menu item", type: .error)
private func updateStopMenuItem() {
guard self.view.window?.isMainWindow == true,
let selectedTabViewModel = tabCollectionViewModel.selectedTabViewModel
else {
os_log("MainViewController: No tab view model selected", type: .error)
return
}
guard let stopMenuItem = NSApplication.shared.mainMenuTyped.stopMenuItem else {
assertionFailure("MainViewController: Failed to get reference to Stop menu item")
return
}

reopenLastClosedTabMenuItem.isEnabled = tabCollectionViewModel.canInsertLastRemovedTab
stopMenuItem.isEnabled = selectedTabViewModel.isLoading
}

}
Expand Down
26 changes: 24 additions & 2 deletions DuckDuckGo/Main/View/Launch.storyboard
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@
<items>
<menuItem title="Stop" keyEquivalent="." id="FyY-q1-f0k">
<connections>
<action selector="stopLoading:" target="Ady-hI-5gd" id="hhx-Ut-x0X"/>
<action selector="stopLoadingPage:" target="Ady-hI-5gd" id="hhx-Ut-x0X"/>
</connections>
</menuItem>
<menuItem title="Reload Page" keyEquivalent="r" id="aKZ-VG-MOp">
Expand All @@ -337,12 +337,28 @@
<action selector="toggleFullScreen:" target="Ady-hI-5gd" id="dU3-MA-1Rq"/>
</connections>
</menuItem>
<menuItem isSeparatorItem="YES" id="ZRv-VX-Njo"/>
<menuItem title="Actual Size" keyEquivalent="0" id="X4G-sF-YZC">
<connections>
<action selector="actualSize:" target="Ady-hI-5gd" id="fDo-Ir-xVA"/>
</connections>
</menuItem>
<menuItem title="Zoom In" keyEquivalent="+" id="lMK-tC-RJd">
<connections>
<action selector="zoomIn:" target="Ady-hI-5gd" id="Zdk-lO-aZk"/>
</connections>
</menuItem>
<menuItem title="Zoom Out" keyEquivalent="-" id="flw-Fc-cm6">
<connections>
<action selector="zoomOut:" target="Ady-hI-5gd" id="prL-67-8bi"/>
</connections>
</menuItem>
</items>
</menu>
</menuItem>
<menuItem title="History" id="sRV-6k-P2o">
<modifierMask key="keyEquivalentModifierMask"/>
<menu key="submenu" title="History" autoenablesItems="NO" id="yLz-zX-NKV">
<menu key="submenu" title="History" id="yLz-zX-NKV">
<items>
<menuItem title="Back" enabled="NO" keyEquivalent="[" id="VWV-vB-Mtc">
<connections>
Expand Down Expand Up @@ -553,6 +569,7 @@ CQ
</menuItem>
</items>
<connections>
<outlet property="actualSizeMenuItem" destination="X4G-sF-YZC" id="7Yi-ui-Gp4"/>
<outlet property="backMenuItem" destination="VWV-vB-Mtc" id="Keo-4E-dRW"/>
<outlet property="bookmarkThisPageMenuItem" destination="Nkm-zy-JXD" id="HVV-JT-gvV"/>
<outlet property="bookmarksMenuItem" destination="xf8-PW-TOA" id="Pfa-kN-nIj"/>
Expand All @@ -567,8 +584,13 @@ CQ
<outlet property="homeMenuItem" destination="FPm-eZ-BQ8" id="Taj-EN-6R8"/>
<outlet property="printMenuItem" destination="uFk-FD-FTB" id="mOP-Xp-myS"/>
<outlet property="printSeparatorItem" destination="anH-0y-7dE" id="E6b-rl-clQ"/>
<outlet property="reloadMenuItem" destination="aKZ-VG-MOp" id="J3D-X3-Xg3"/>
<outlet property="reopenLastClosedTabMenuItem" destination="c3l-v1-kxk" id="eGw-Dg-del"/>
<outlet property="sendFeedbackMenuItem" destination="V8t-Ts-q0L" id="qwz-Oh-HGm"/>
<outlet property="stopMenuItem" destination="FyY-q1-f0k" id="BFN-Kd-aAb"/>
<outlet property="toggleFullscreenMenuItem" destination="4J7-dP-txa" id="lyl-pp-TR0"/>
<outlet property="zoomInMenuItem" destination="lMK-tC-RJd" id="Ddo-RX-A6Z"/>
<outlet property="zoomOutMenuItem" destination="flw-Fc-cm6" id="1AB-0r-vvx"/>
</connections>
</menu>
<connections>
Expand Down
8 changes: 0 additions & 8 deletions DuckDuckGo/Main/View/MainWindowController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,6 @@ extension MainWindowController: NSWindowDelegate {
return [.fullScreen, .autoHideMenuBar]
}

func windowDidBecomeMain(_ notification: Notification) {
mainViewController.windowDidBecomeMain()
}

func windowDidBecomeKey(_ notification: Notification) {
WindowControllersManager.shared.lastKeyMainWindowController = self
}
Expand All @@ -112,10 +108,6 @@ extension MainWindowController: NSWindowDelegate {
mainViewController.tabBarViewController.draggingSpace.isHidden = false
}

func windowDidResignMain(_ notification: Notification) {
mainViewController.windowDidResignMain()
}

func windowWillClose(_ notification: Notification) {
mainViewController.windowWillClose()

Expand Down
16 changes: 7 additions & 9 deletions DuckDuckGo/Menus/MainMenu.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ final class MainMenu: NSMenu {

@IBOutlet weak var backMenuItem: NSMenuItem?
@IBOutlet weak var forwardMenuItem: NSMenuItem?
@IBOutlet weak var reloadMenuItem: NSMenuItem?
@IBOutlet weak var stopMenuItem: NSMenuItem?
@IBOutlet weak var homeMenuItem: NSMenuItem?
@IBOutlet weak var reopenLastClosedTabMenuItem: NSMenuItem?

Expand All @@ -44,6 +46,11 @@ final class MainMenu: NSMenu {
@IBOutlet weak var helpSeparatorMenuItem: NSMenuItem?
@IBOutlet weak var sendFeedbackMenuItem: NSMenuItem?

@IBOutlet weak var toggleFullscreenMenuItem: NSMenuItem?
@IBOutlet weak var zoomInMenuItem: NSMenuItem?
@IBOutlet weak var zoomOutMenuItem: NSMenuItem?
@IBOutlet weak var actualSizeMenuItem: NSMenuItem?

required init(coder: NSCoder) {
super.init(coder: coder)

Expand All @@ -61,15 +68,6 @@ final class MainMenu: NSMenu {
}
}

func setWindowRelatedMenuItems(enabled: Bool) {
backMenuItem?.isEnabled = enabled
forwardMenuItem?.isEnabled = enabled
homeMenuItem?.isEnabled = enabled
reopenLastClosedTabMenuItem?.isEnabled = enabled
bookmarkThisPageMenuItem?.isEnabled = enabled
favoriteThisPageMenuItem?.isEnabled = enabled
}

private func setup() {

#if !FEEDBACK
Expand Down
Loading