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 pathDockCustomizer.swift
148 lines (128 loc) · 5.72 KB
/
DockCustomizer.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
//
// DockCustomizer.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 Foundation
import Common
import os.log
protocol DockCustomization {
var isAddedToDock: Bool { get }
@discardableResult
func addToDock() -> Bool
}
final class DockCustomizer: DockCustomization {
private let positionProvider: DockPositionProviding
init(positionProvider: DockPositionProviding = DockPositionProvider()) {
self.positionProvider = positionProvider
}
private var dockPlistURL: URL = URL(fileURLWithPath: NSString(string: "~/Library/Preferences/com.apple.dock.plist").expandingTildeInPath)
private var dockPlistDict: [String: AnyObject]? {
return NSDictionary(contentsOf: dockPlistURL) as? [String: AnyObject]
}
// This checks whether the bundle identifier of the current bundle
// is present in the 'persistent-apps' array of the Dock's plist.
var isAddedToDock: Bool {
guard let bundleIdentifier = Bundle.main.bundleIdentifier,
let dockPlistDict = dockPlistDict,
let persistentApps = dockPlistDict["persistent-apps"] as? [[String: AnyObject]] else {
return false
}
return persistentApps.contains(where: { ($0["tile-data"] as? [String: AnyObject])?["bundle-identifier"] as? String == bundleIdentifier })
}
// Adds a dictionary representing the application, either by using an existing
// one from 'recent-apps' or creating a new one if the application isn't recently used.
// It then inserts this dictionary into the 'persistent-apps' list at a position
// determined by `positionProvider`. Following the plist update, it schedules the Dock
// to restart after a brief delay to apply the changes.
@discardableResult
func addToDock() -> Bool {
PixelExperiment.fireOnboardingAddToDockRequestedPixel()
let appPath = Bundle.main.bundleURL.path
guard !isAddedToDock,
let bundleIdentifier = Bundle.main.bundleIdentifier,
var dockPlistDict = dockPlistDict else {
return false
}
var persistentApps = dockPlistDict["persistent-apps"] as? [[String: AnyObject]] ?? []
let recentApps = dockPlistDict["recent-apps"] as? [[String: AnyObject]] ?? []
let appDict: [String: AnyObject]
// Find the app in recent apps
if let recentAppIndex = recentApps.firstIndex(where: { appDict in
if let tileData = appDict["tile-data"] as? [String: AnyObject],
let appBundleIdentifier = tileData["bundle-identifier"] as? String {
return appBundleIdentifier == bundleIdentifier
}
return false
}) {
// Use existing dictonary from recentApps
appDict = recentApps[recentAppIndex]
} else {
// Create the dictionary for the current application if not found in recent apps
appDict = Self.appDict(appPath: appPath, bundleIdentifier: bundleIdentifier)
}
// Insert to persistent apps
let index = positionProvider.newDockIndex(from: makeAppURLs(from: persistentApps))
persistentApps.insert(appDict, at: index)
// Update the plist
dockPlistDict["persistent-apps"] = persistentApps as AnyObject?
dockPlistDict["recent-apps"] = recentApps as AnyObject?
// Update mod-count
dockPlistDict["mod-count"] = ((dockPlistDict["mod-count"] as? Int) ?? 0) + 1 as AnyObject
do {
try (dockPlistDict as NSDictionary).write(to: dockPlistURL)
DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
self.restartDock()
}
return true
} catch {
Logger.general.error("Error writing to Dock plist: \(error.localizedDescription, privacy: .public)")
return false
}
}
private func restartDock() {
let task = Process()
task.launchPath = "/usr/bin/killall"
task.arguments = ["Dock"]
task.launch()
}
private func makeAppURLs(from persistentApps: [[String: AnyObject]]) -> [URL] {
return persistentApps.compactMap { appDict in
if let tileData = appDict["tile-data"] as? [String: AnyObject],
let appBundleIdentifier = tileData["file-data"] as? [String: AnyObject],
let urlString = appBundleIdentifier["_CFURLString"] as? String,
let url = URL(string: urlString) {
return url
} else {
return nil
}
}
}
static func appDict(appPath: String, bundleIdentifier: String) -> [String: AnyObject] {
return ["tile-type": "file-tile" as AnyObject,
"tile-data": [
"dock-extra": 0 as AnyObject,
"file-type": 1 as AnyObject,
"file-data": [
"_CFURLString": "file://" + appPath + "/",
"_CFURLStringType": 15
],
"file-label": "DuckDuckGo" as AnyObject,
"bundle-identifier": bundleIdentifier as AnyObject,
"is-beta": 0 as AnyObject
] as AnyObject
]
}
}