-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApp.swift
More file actions
92 lines (79 loc) · 2.93 KB
/
App.swift
File metadata and controls
92 lines (79 loc) · 2.93 KB
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
import SwiftUI
import AppKit
struct DDC {
static func setVCP(displayID: CGDirectDisplayID, vcp: UInt8, value: UInt8) {
// Placeholder for DDC communication
print("Display \(displayID) -> VCP \(vcp) set to \(value)")
}
}
struct MonitorView: View {
var displayID: CGDirectDisplayID
var name: String
@State private var brightness: Double = 50
@State private var contrast: Double = 50
var body: some View {
VStack(alignment: .leading, spacing: 8) {
Text(name).font(.system(size: 11, weight: .bold))
HStack {
Image(systemName: "sun.max.fill").frame(width: 20)
Slider(value: $brightness, in: 0...100) { _ in
DDC.setVCP(displayID: displayID, vcp: 0x10, value: UInt8(brightness))
}
}
HStack {
Image(systemName: "circle.lefthalf.filled").frame(width: 20)
Slider(value: $contrast, in: 0...100) { _ in
DDC.setVCP(displayID: displayID, vcp: 0x12, value: UInt8(contrast))
}
}
Divider().padding(.vertical, 4)
}
}
}
struct MainView: View {
var body: some View {
VStack(spacing: 12) {
Text("MiniDisplay").font(.caption).opacity(0.6)
ForEach(NSScreen.screens, id: \.self) { screen in
let id = screen.deviceDescription[NSDeviceDescriptionKey(rawValue: "NSScreenNumber")] as? CGDirectDisplayID ?? 0
MonitorView(displayID: id, name: screen.localizedName)
}
Button("Quit") {
NSApplication.shared.terminate(nil)
}
.buttonStyle(.bordered)
.controlSize(.small)
}
.padding()
.frame(width: 220)
}
}
@main
struct MiniDisplayApp: App {
@NSApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
var body: some Scene {
Settings { EmptyView() }
}
}
class AppDelegate: NSObject, NSApplicationDelegate {
var statusItem: NSStatusItem?
let popover = NSPopover()
func applicationDidFinishLaunching(_ notification: Notification) {
popover.contentViewController = NSHostingController(rootView: MainView())
statusItem = NSStatusBar.system.statusItem(withLength: NSStatusItem.variableLength)
if let button = statusItem?.button {
// FIXED LINE BELOW: Using systemSymbolName for AppKit
button.image = NSImage(systemSymbolName: "display", accessibilityDescription: "Display Control")
button.action = #selector(togglePopover)
}
}
@objc func togglePopover() {
if let button = statusItem?.button {
if popover.isShown {
popover.performClose(nil)
} else {
popover.show(relativeTo: button.bounds, of: button, preferredEdge: .minY)
}
}
}
}