Skip to content

Commit 5504e5d

Browse files
committed
#171 Added DarkMode button v0.21
1 parent 8a73fe0 commit 5504e5d

File tree

10 files changed

+122
-3
lines changed

10 files changed

+122
-3
lines changed

MTMR.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161
B0B17439207D6B590004B740 /* Vox.nowPlaying.scpt in Resources */ = {isa = PBXBuildFile; fileRef = B0B1742F207D6B590004B740 /* Vox.nowPlaying.scpt */; };
6262
B0B1743A207D6B590004B740 /* iTunes.nowPlaying.scpt in Resources */ = {isa = PBXBuildFile; fileRef = B0B17430207D6B590004B740 /* iTunes.nowPlaying.scpt */; };
6363
B0F3112520C9E35F0076BB88 /* SupportNSTouchBar.swift in Sources */ = {isa = PBXBuildFile; fileRef = B0F3112420C9E35F0076BB88 /* SupportNSTouchBar.swift */; };
64+
B0F54A7A2295AC7D00B4C509 /* DarkModeBarItem.swift in Sources */ = {isa = PBXBuildFile; fileRef = B0F54A792295AC7D00B4C509 /* DarkModeBarItem.swift */; };
6465
B0F8771A207AC1EA00D6E430 /* TouchBarSupport.m in Sources */ = {isa = PBXBuildFile; fileRef = B0F87719207AC1EA00D6E430 /* TouchBarSupport.m */; };
6566
/* End PBXBuildFile section */
6667

@@ -142,6 +143,7 @@
142143
B0B1742F207D6B590004B740 /* Vox.nowPlaying.scpt */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = Vox.nowPlaying.scpt; sourceTree = "<group>"; };
143144
B0B17430207D6B590004B740 /* iTunes.nowPlaying.scpt */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = iTunes.nowPlaying.scpt; sourceTree = "<group>"; };
144145
B0F3112420C9E35F0076BB88 /* SupportNSTouchBar.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SupportNSTouchBar.swift; sourceTree = "<group>"; };
146+
B0F54A792295AC7D00B4C509 /* DarkModeBarItem.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DarkModeBarItem.swift; sourceTree = "<group>"; };
145147
B0F87719207AC1EA00D6E430 /* TouchBarSupport.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = TouchBarSupport.m; sourceTree = "<group>"; };
146148
B0F8771B207AC92700D6E430 /* TouchBarSupport.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = TouchBarSupport.h; sourceTree = "<group>"; };
147149
/* End PBXFileReference section */
@@ -293,6 +295,7 @@
293295
6027D1B82080E52A004FFDC7 /* VolumeViewController.swift */,
294296
607EEA4A2087835F009DA5F0 /* WeatherBarItem.swift */,
295297
B08126F0217BE19000A98970 /* WidgetProtocol.swift */,
298+
B0F54A792295AC7D00B4C509 /* DarkModeBarItem.swift */,
296299
);
297300
path = Widgets;
298301
sourceTree = "<group>";
@@ -446,6 +449,7 @@
446449
B0008E552080286C003AD4DD /* SupportHelpers.swift in Sources */,
447450
6042B6A72083E03A00C525C8 /* AppScrubberTouchBarItem.swift in Sources */,
448451
B082B253205C7D8000BC04DC /* AppDelegate.swift in Sources */,
452+
B0F54A7A2295AC7D00B4C509 /* DarkModeBarItem.swift in Sources */,
449453
B08126EF217BD0B900A98970 /* PomodoroBarItem.swift in Sources */,
450454
B081732C213739FE005D4908 /* DnDBarItem.swift in Sources */,
451455
60C44AFD20A373A100C0EC91 /* MusicBarItem.swift in Sources */,
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{
2+
"images" : [
3+
{
4+
"idiom" : "universal",
5+
"scale" : "1x"
6+
},
7+
{
8+
"idiom" : "universal",
9+
"filename" : "sun-icon-256.png",
10+
"scale" : "2x"
11+
},
12+
{
13+
"idiom" : "universal",
14+
"scale" : "3x"
15+
}
16+
],
17+
"info" : {
18+
"version" : 1,
19+
"author" : "xcode"
20+
},
21+
"properties" : {
22+
"template-rendering-intent" : "template"
23+
}
24+
}
16.1 KB
Loading
8.11 KB
Loading
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{
2+
"images" : [
3+
{
4+
"idiom" : "universal",
5+
"scale" : "1x"
6+
},
7+
{
8+
"idiom" : "universal",
9+
"filename" : "39857.png",
10+
"scale" : "2x"
11+
},
12+
{
13+
"idiom" : "universal",
14+
"scale" : "3x"
15+
}
16+
],
17+
"info" : {
18+
"version" : 1,
19+
"author" : "xcode"
20+
},
21+
"properties" : {
22+
"template-rendering-intent" : "template"
23+
}
24+
}

MTMR/Info.plist

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@
1717
<key>CFBundlePackageType</key>
1818
<string>APPL</string>
1919
<key>CFBundleShortVersionString</key>
20-
<string>0.20.1</string>
20+
<string>0.21</string>
2121
<key>CFBundleVersion</key>
22-
<string>185</string>
22+
<string>207</string>
2323
<key>LSApplicationCategoryType</key>
2424
<string>public.app-category.utilities</string>
2525
<key>LSMinimumSystemVersion</key>

MTMR/ItemsParsing.swift

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,7 @@ enum ItemType: Decodable {
339339
case dnd()
340340
case pomodoro(workTime: Double, restTime: Double)
341341
case network(flip: Bool)
342+
case darkMode()
342343

343344
private enum CodingKeys: String, CodingKey {
344345
case type
@@ -379,6 +380,7 @@ enum ItemType: Decodable {
379380
case dnd
380381
case pomodoro
381382
case network
383+
case darkMode
382384
}
383385

384386
init(from decoder: Decoder) throws {
@@ -447,10 +449,13 @@ enum ItemType: Decodable {
447449
let workTime = try container.decodeIfPresent(Double.self, forKey: .workTime) ?? 1500.0
448450
let restTime = try container.decodeIfPresent(Double.self, forKey: .restTime) ?? 600.0
449451
self = .pomodoro(workTime: workTime, restTime: restTime)
450-
452+
451453
case .network:
452454
let flip = try container.decodeIfPresent(Bool.self, forKey: .flip) ?? false
453455
self = .network(flip: flip)
456+
457+
case .darkMode:
458+
self = .darkMode()
454459
}
455460
}
456461
}

MTMR/TouchBarController.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ extension ItemType {
5151
return PomodoroBarItem.identifier
5252
case .network(flip: _):
5353
return NetworkBarItem.identifier
54+
case .darkMode(items: _):
55+
return DarkModeBarItem.identifier
5456
}
5557
}
5658
}
@@ -283,6 +285,8 @@ class TouchBarController: NSObject, NSTouchBarDelegate {
283285
barItem = PomodoroBarItem(identifier: identifier, workTime: workTime, restTime: restTime)
284286
case let .network(flip: flip):
285287
barItem = NetworkBarItem(identifier: identifier, flip: flip)
288+
case .darkMode():
289+
barItem = DarkModeBarItem(identifier: identifier)
286290
}
287291

288292
if let action = self.action(forItem: item), let item = barItem as? CustomButtonTouchBarItem {

MTMR/Widgets/DarkModeBarItem.swift

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import Foundation
2+
3+
class DarkModeBarItem: CustomButtonTouchBarItem, Widget {
4+
static var name: String = "darkmode"
5+
static var identifier: String = "com.toxblh.mtmr.darkmode"
6+
7+
private var timer: Timer!
8+
9+
init(identifier: NSTouchBarItem.Identifier) {
10+
super.init(identifier: identifier, title: "")
11+
isBordered = false
12+
setWidth(value: 24)
13+
14+
tapClosure = { [weak self] in self?.DarkModeToggle() }
15+
16+
timer = Timer.scheduledTimer(timeInterval: 3, target: self, selector: #selector(refresh), userInfo: nil, repeats: true)
17+
18+
refresh()
19+
}
20+
21+
required init?(coder _: NSCoder) {
22+
fatalError("init(coder:) has not been implemented")
23+
}
24+
25+
func DarkModeToggle() {
26+
DarkMode.isEnabled = !DarkMode.isEnabled
27+
refresh()
28+
}
29+
30+
@objc func refresh() {
31+
image = DarkMode.isEnabled ? #imageLiteral(resourceName: "dark-mode-on") : #imageLiteral(resourceName: "dark-mode-off")
32+
}
33+
}
34+
35+
36+
struct DarkMode {
37+
private static let prefix = "tell application \"System Events\" to tell appearance preferences to"
38+
39+
static var isEnabled: Bool {
40+
get {
41+
return UserDefaults.standard.string(forKey: "AppleInterfaceStyle") == "Dark"
42+
}
43+
set {
44+
toggle(force: newValue)
45+
}
46+
}
47+
48+
static func toggle(force: Bool? = nil) {
49+
let value = force.map(String.init) ?? "not dark mode"
50+
runAppleScript("\(prefix) set dark mode to \(value)")
51+
}
52+
}
53+
54+
func runAppleScript(_ source: String) -> String? {
55+
return NSAppleScript(source: source)?.executeAndReturnError(nil).stringValue
56+
}
57+

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ Re-tick or check a tick for access 🍏→ System Preferences → Security and P
7070
- dock (half-long click to open app, full-long click to kill app)
7171
- nightShift
7272
- dnd (Don't disturb)
73+
- darkMode
7374
- pomodoro
7475
- network
7576

0 commit comments

Comments
 (0)