This repository was archived by the owner on Feb 24, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathDefaultsFavoritesActionHandler.swift
97 lines (81 loc) · 3.35 KB
/
DefaultsFavoritesActionHandler.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
//
// DefaultsFavoritesActionHandler.swift
//
// Copyright © 2024 DuckDuckGo. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
import Combine
import NewTabPage
final class DefaultFavoritesActionsHandler: FavoritesActionsHandling {
typealias Favorite = Bookmark
let bookmarkManager: BookmarkManager
init(bookmarkManager: BookmarkManager = LocalBookmarkManager.shared) {
self.bookmarkManager = bookmarkManager
}
@MainActor
func open(_ url: URL, target: LinkOpenTarget) {
guard let tabCollectionViewModel else {
return
}
PixelExperiment.fireOnboardingBookmarkUsed5to7Pixel()
if target == .newWindow || NSApplication.shared.isCommandPressed && NSApplication.shared.isOptionPressed {
WindowsManager.openNewWindow(with: url, source: .bookmark, isBurner: tabCollectionViewModel.isBurner)
} else if target == .newTab || NSApplication.shared.isCommandPressed && NSApplication.shared.isShiftPressed {
tabCollectionViewModel.insertOrAppendNewTab(.contentFromURL(url, source: .bookmark), selected: true)
} else if NSApplication.shared.isCommandPressed {
tabCollectionViewModel.insertOrAppendNewTab(.contentFromURL(url, source: .bookmark), selected: false)
} else {
tabCollectionViewModel.selectedTabViewModel?.tab.setContent(.contentFromURL(url, source: .bookmark))
}
}
func removeFavorite(_ favorite: Bookmark) {
favorite.isFavorite = false
bookmarkManager.update(bookmark: favorite)
}
func deleteBookmark(for favorite: Bookmark) {
bookmarkManager.remove(bookmark: favorite, undoManager: nil)
}
@MainActor
func addNewFavorite() {
guard let window else { return }
BookmarksDialogViewFactory.makeAddFavoriteView().show(in: window)
}
@MainActor
func edit(_ favorite: Bookmark) {
guard let window else { return }
BookmarksDialogViewFactory.makeEditBookmarkView(bookmark: favorite).show(in: window)
}
func move(_ bookmarkID: String, toIndex index: Int) {
bookmarkManager.moveFavorites(with: [bookmarkID], toIndex: index) { _ in }
}
@MainActor
private var window: NSWindow? {
WindowControllersManager.shared.lastKeyMainWindowController?.mainViewController.view.window
}
@MainActor
private var tabCollectionViewModel: TabCollectionViewModel? {
WindowControllersManager.shared.lastKeyMainWindowController?.mainViewController.tabCollectionViewModel
}
}
extension Bookmark: NewTabPageFavorite {
private enum Const {
static let wwwPrefix = "www."
}
var etldPlusOne: String? {
guard let domain = urlObject?.host else {
return nil
}
return ContentBlocking.shared.tld.eTLDplus1(domain)?.dropping(prefix: Const.wwwPrefix)
}
}