Skip to content

Commit 1a25cc8

Browse files
committed
Add support for custom colors
1 parent 76719b6 commit 1a25cc8

File tree

3 files changed

+109
-57
lines changed

3 files changed

+109
-57
lines changed

IsThereNet.xcodeproj/project.pbxproj

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
C781D4402B4592590050D04A /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = C781D43F2B4592590050D04A /* Assets.xcassets */; };
1212
C781D4432B4592590050D04A /* Preview Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = C781D4422B4592590050D04A /* Preview Assets.xcassets */; };
1313
C781D44D2B4815510050D04A /* fping in Resources */ = {isa = PBXBuildFile; fileRef = C781D44C2B4815510050D04A /* fping */; };
14+
C7AD8E8F2C36A6E5008E4182 /* ColorCode in Frameworks */ = {isa = PBXBuildFile; productRef = C7AD8E8E2C36A6E5008E4182 /* ColorCode */; };
1415
/* End PBXBuildFile section */
1516

1617
/* Begin PBXFileReference section */
@@ -28,6 +29,7 @@
2829
isa = PBXFrameworksBuildPhase;
2930
buildActionMask = 2147483647;
3031
files = (
32+
C7AD8E8F2C36A6E5008E4182 /* ColorCode in Frameworks */,
3133
);
3234
runOnlyForDeploymentPostprocessing = 0;
3335
};
@@ -88,6 +90,9 @@
8890
dependencies = (
8991
);
9092
name = IsThereNet;
93+
packageProductDependencies = (
94+
C7AD8E8E2C36A6E5008E4182 /* ColorCode */,
95+
);
9196
productName = IsThereNet;
9297
productReference = C781D4382B4592580050D04A /* IsThereNet.app */;
9398
productType = "com.apple.product-type.application";
@@ -116,6 +121,9 @@
116121
Base,
117122
);
118123
mainGroup = C781D42F2B4592570050D04A;
124+
packageReferences = (
125+
C7AD8E8D2C36A6E5008E4182 /* XCRemoteSwiftPackageReference "WFColorCode" */,
126+
);
119127
productRefGroup = C781D4392B4592580050D04A /* Products */;
120128
projectDirPath = "";
121129
projectRoot = "";
@@ -378,6 +386,25 @@
378386
defaultConfigurationName = Release;
379387
};
380388
/* End XCConfigurationList section */
389+
390+
/* Begin XCRemoteSwiftPackageReference section */
391+
C7AD8E8D2C36A6E5008E4182 /* XCRemoteSwiftPackageReference "WFColorCode" */ = {
392+
isa = XCRemoteSwiftPackageReference;
393+
repositoryURL = "https://github.com/1024jp/WFColorCode/";
394+
requirement = {
395+
kind = upToNextMajorVersion;
396+
minimumVersion = 2.10.0;
397+
};
398+
};
399+
/* End XCRemoteSwiftPackageReference section */
400+
401+
/* Begin XCSwiftPackageProductDependency section */
402+
C7AD8E8E2C36A6E5008E4182 /* ColorCode */ = {
403+
isa = XCSwiftPackageProductDependency;
404+
package = C7AD8E8D2C36A6E5008E4182 /* XCRemoteSwiftPackageReference "WFColorCode" */;
405+
productName = ColorCode;
406+
};
407+
/* End XCSwiftPackageProductDependency section */
381408
};
382409
rootObject = C781D4302B4592570050D04A /* Project object */;
383410
}

IsThereNet.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved

Lines changed: 14 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

IsThereNet/IsThereNetApp.swift

Lines changed: 68 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
import AppKit
99
import Cocoa
10+
import ColorCode
1011
import Combine
1112
import Foundation
1213
import Intents
@@ -71,32 +72,6 @@ private func drawColoredTopLine(_ color: NSColor, hideAfter: TimeInterval = 5, s
7172
}
7273
}
7374

74-
extension NSSound {
75-
func playIfNotDND() {
76-
if #available(macOS 12.0, *), focused {
77-
return
78-
}
79-
play()
80-
}
81-
}
82-
83-
let SONOMA_TO_ORIGINAL_SOUND_NAMES = [
84-
"Mezzo": "Basso",
85-
"Breeze": "Blow",
86-
"Pebble": "Bottle",
87-
"Jump": "Frog",
88-
"Funky": "Funk",
89-
"Crystal": "Glass",
90-
"Heroine": "Hero",
91-
"Pong": "Morse",
92-
"Sonar": "Ping",
93-
"Bubble": "Pop",
94-
"Pluck": "Purr",
95-
"Sonumi": "Sosumi",
96-
"Submerge": "Submarine",
97-
"Boop": "Tink",
98-
]
99-
10075
@available(macOS 12.0, *)
10176
var focused: Bool {
10277
guard INFocusStatusCenter.default.authorizationStatus == .authorized else {
@@ -109,43 +84,16 @@ var focused: Bool {
10984
return INFocusStatusCenter.default.focusStatus.isFocused ?? false
11085
}
11186

112-
private struct FadeSecondsConfig: Codable, Equatable {
113-
var connected: Double? = 5.0
114-
var disconnected: Double? = 0.0
115-
var slow: Double? = 10.0
116-
}
117-
118-
private struct SoundsConfig: Codable, Equatable {
119-
var connected: String? = "" // e.g. "Funky"
120-
var disconnected: String? = "" // e.g. "Mezzo"
121-
var slow: String? = "" // e.g. "Submerge"
122-
var volume: Float? = 0.7
123-
124-
var connectedSound: NSSound? { connected != nil ? sound(named: connected!) : nil }
125-
var disconnectedSound: NSSound? { disconnected != nil ? sound(named: disconnected!) : nil }
126-
var slowSound: NSSound? { slow != nil ? sound(named: slow!) : nil }
127-
128-
func sound(named name: String) -> NSSound? {
129-
guard let s = NSSound(named: name) ?? NSSound(named: SONOMA_TO_ORIGINAL_SOUND_NAMES[name] ?? "") else {
130-
return nil
131-
}
132-
133-
s.volume = max(min(volume ?? 0.7, 1.0), 0.0)
134-
return s
135-
}
136-
137-
}
138-
13987
private enum PingStatus: Equatable {
14088
case reachable(Double)
14189
case timedOut
14290
case slow(Double)
14391

14492
var color: NSColor {
14593
switch self {
146-
case .reachable: .systemGreen
147-
case .timedOut: .systemRed
148-
case .slow: .systemYellow
94+
case .reachable: CONFIG.colors?.connectedColor ?? .systemGreen
95+
case .timedOut: CONFIG.colors?.disconnectedColor ?? .systemRed
96+
case .slow: CONFIG.colors?.slowColor ?? .systemYellow
14997
}
15098
}
15199

@@ -268,7 +216,7 @@ func start() {
268216
process = nil
269217
pingRestartTask = nil
270218
}
271-
drawColoredTopLine(.systemRed, hideAfter: CONFIG.fadeSeconds?.disconnected ?? 0, sound: CONFIG.sounds?.disconnectedSound)
219+
drawColoredTopLine(PingStatus.timedOut.color, hideAfter: PingStatus.timedOut.hideAfter, sound: PingStatus.timedOut.sound)
272220
@unknown default:
273221
log("Internet connection: \(path.status)")
274222
}
@@ -405,8 +353,61 @@ private let LOG_FILE: FileHandle? = {
405353
}()
406354
private let CONFIG_PATH = FileManager.default.urls(for: .applicationSupportDirectory, in: .userDomainMask)[0].appendingPathComponent("config.json")
407355

356+
let SONOMA_TO_ORIGINAL_SOUND_NAMES = [
357+
"Mezzo": "Basso",
358+
"Breeze": "Blow",
359+
"Pebble": "Bottle",
360+
"Jump": "Frog",
361+
"Funky": "Funk",
362+
"Crystal": "Glass",
363+
"Heroine": "Hero",
364+
"Pong": "Morse",
365+
"Sonar": "Ping",
366+
"Bubble": "Pop",
367+
"Pluck": "Purr",
368+
"Sonumi": "Sosumi",
369+
"Submerge": "Submarine",
370+
"Boop": "Tink",
371+
]
372+
408373
// MARK: Config
409374

375+
private struct FadeSecondsConfig: Codable, Equatable {
376+
var connected: Double? = 5.0
377+
var disconnected: Double? = 0.0
378+
var slow: Double? = 10.0
379+
}
380+
381+
private struct ColorsConfig: Codable, Equatable {
382+
var connected: String? = "systemGreen"
383+
var disconnected: String? = "systemRed"
384+
var slow: String? = "systemYellow"
385+
386+
var connectedColor: NSColor { connected != nil ? NSColor(colorCode: connected!) ?? .systemGreen : .systemGreen }
387+
var disconnectedColor: NSColor { disconnected != nil ? NSColor(colorCode: disconnected!) ?? .systemRed : .systemRed }
388+
var slowColor: NSColor { slow != nil ? NSColor(colorCode: slow!) ?? .systemYellow : .systemYellow }
389+
}
390+
391+
private struct SoundsConfig: Codable, Equatable {
392+
var connected: String? = "" // e.g. "Funky"
393+
var disconnected: String? = "" // e.g. "Mezzo"
394+
var slow: String? = "" // e.g. "Submerge"
395+
var volume: Float? = 0.7 // relative to system volume, 0.0 - 1.0
396+
397+
var connectedSound: NSSound? { connected != nil ? sound(named: connected!) : nil }
398+
var disconnectedSound: NSSound? { disconnected != nil ? sound(named: disconnected!) : nil }
399+
var slowSound: NSSound? { slow != nil ? sound(named: slow!) : nil }
400+
401+
func sound(named name: String) -> NSSound? {
402+
guard let s = NSSound(named: name) ?? NSSound(named: SONOMA_TO_ORIGINAL_SOUND_NAMES[name] ?? "") else {
403+
return nil
404+
}
405+
406+
s.volume = max(min(volume ?? 0.7, 1.0), 0.0)
407+
return s
408+
}
409+
}
410+
410411
private struct Config: Codable, Equatable {
411412
var pingIP = "1.1.1.1"
412413
var pingIntervalSeconds = 5.0
@@ -415,6 +416,7 @@ private struct Config: Codable, Equatable {
415416

416417
var fadeSeconds: FadeSecondsConfig? = FadeSecondsConfig()
417418
var sounds: SoundsConfig? = SoundsConfig()
419+
var colors: ColorsConfig? = ColorsConfig()
418420
}
419421

420422
private var CONFIG_FS_WATCHER: FSEventStreamRef?
@@ -474,6 +476,15 @@ private var CONFIG: Config = {
474476

475477
// MARK: Extensions
476478

479+
extension NSSound {
480+
func playIfNotDND() {
481+
if #available(macOS 12.0, *), focused {
482+
return
483+
}
484+
play()
485+
}
486+
}
487+
477488
extension Double {
478489
var intround: Int { Int(rounded()) }
479490
}

0 commit comments

Comments
 (0)