Compare commits
5
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
e7387a3ac4 | ||
|
|
7d2dd306ca | ||
|
|
6473a1a4b2 | ||
|
|
f34bb6bafe | ||
|
|
9b3f6ae50e |
@@ -59,12 +59,12 @@ jobs:
|
|||||||
if command -v xcbeautify &> /dev/null; then
|
if command -v xcbeautify &> /dev/null; then
|
||||||
xcodebuild clean build \
|
xcodebuild clean build \
|
||||||
-scheme MacMonitor \
|
-scheme MacMonitor \
|
||||||
-destination 'generic/platform=macOS,arch=x86_64' \
|
-destination 'platform=macOS,arch=x86_64' \
|
||||||
CODE_SIGNING_ALLOWED=NO | xcbeautify
|
CODE_SIGNING_ALLOWED=NO | xcbeautify
|
||||||
else
|
else
|
||||||
xcodebuild clean build \
|
xcodebuild clean build \
|
||||||
-scheme MacMonitor \
|
-scheme MacMonitor \
|
||||||
-destination 'generic/platform=macOS,arch=x86_64' \
|
-destination 'platform=macOS,arch=x86_64' \
|
||||||
CODE_SIGNING_ALLOWED=NO
|
CODE_SIGNING_ALLOWED=NO
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,23 @@
|
|||||||
|
disabled_rules:
|
||||||
|
- type_body_length
|
||||||
|
- function_body_length
|
||||||
|
- file_length
|
||||||
|
- cyclomatic_complexity
|
||||||
|
- identifier_name
|
||||||
|
- line_length
|
||||||
|
- large_tuple
|
||||||
|
- multiple_closures_with_trailing_closure
|
||||||
|
- trailing_whitespace
|
||||||
|
- implicit_optional_initialization
|
||||||
|
|
||||||
|
opt_in_rules:
|
||||||
|
- empty_count
|
||||||
|
|
||||||
|
included:
|
||||||
|
- Sources
|
||||||
|
- Tests
|
||||||
|
|
||||||
|
excluded:
|
||||||
|
- MacMonitor.xcodeproj
|
||||||
|
- build
|
||||||
|
- DerivedData
|
||||||
@@ -11,5 +11,15 @@ struct MacMonitorApp: App {
|
|||||||
.windowStyle(.titleBar)
|
.windowStyle(.titleBar)
|
||||||
.windowToolbarStyle(.unified)
|
.windowToolbarStyle(.unified)
|
||||||
.defaultSize(width: 960, height: 640)
|
.defaultSize(width: 960, height: 640)
|
||||||
|
|
||||||
|
MenuBarExtra {
|
||||||
|
QuickGlancePopoverView(store: store)
|
||||||
|
} label: {
|
||||||
|
MenuBarStatusView(
|
||||||
|
cpuLoad: store.cpuLoad.totalLoad,
|
||||||
|
memoryPercent: store.memory.utilizationPercentage
|
||||||
|
)
|
||||||
|
}
|
||||||
|
.menuBarExtraStyle(.window)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -30,5 +30,6 @@
|
|||||||
#import "MMBatteryTelemetryProvider.h"
|
#import "MMBatteryTelemetryProvider.h"
|
||||||
#import "MMPeripheralsProvider.h"
|
#import "MMPeripheralsProvider.h"
|
||||||
#import "MMAudioTelemetryProvider.h"
|
#import "MMAudioTelemetryProvider.h"
|
||||||
|
#import "MMDisplayManager.h"
|
||||||
|
|
||||||
#endif /* MacMonitor_Bridging_Header_h */
|
#endif /* MacMonitor_Bridging_Header_h */
|
||||||
|
|||||||
@@ -0,0 +1,54 @@
|
|||||||
|
import Foundation
|
||||||
|
|
||||||
|
public struct HistoricalSample<T>: Identifiable {
|
||||||
|
public let id: UUID
|
||||||
|
public let timestamp: Date
|
||||||
|
public let value: T
|
||||||
|
|
||||||
|
public init(timestamp: Date = Date(), value: T) {
|
||||||
|
self.id = UUID()
|
||||||
|
self.timestamp = timestamp
|
||||||
|
self.value = value
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public final class RollingRingBuffer<T> {
|
||||||
|
private var buffer: [HistoricalSample<T>]
|
||||||
|
private let capacity: Int
|
||||||
|
private let lock = NSLock()
|
||||||
|
|
||||||
|
public init(capacity: Int = 60) {
|
||||||
|
self.capacity = max(1, capacity)
|
||||||
|
self.buffer = []
|
||||||
|
self.buffer.reserveCapacity(self.capacity)
|
||||||
|
}
|
||||||
|
|
||||||
|
public func append(_ value: T, timestamp: Date = Date()) {
|
||||||
|
lock.lock()
|
||||||
|
defer { lock.unlock() }
|
||||||
|
|
||||||
|
let sample = HistoricalSample(timestamp: timestamp, value: value)
|
||||||
|
if buffer.count >= capacity {
|
||||||
|
buffer.removeFirst()
|
||||||
|
}
|
||||||
|
buffer.append(sample)
|
||||||
|
}
|
||||||
|
|
||||||
|
public var samples: [HistoricalSample<T>] {
|
||||||
|
lock.lock()
|
||||||
|
defer { lock.unlock() }
|
||||||
|
return buffer
|
||||||
|
}
|
||||||
|
|
||||||
|
public var count: Int {
|
||||||
|
lock.lock()
|
||||||
|
defer { lock.unlock() }
|
||||||
|
return buffer.count
|
||||||
|
}
|
||||||
|
|
||||||
|
public func clear() {
|
||||||
|
lock.lock()
|
||||||
|
defer { lock.unlock() }
|
||||||
|
buffer.removeAll(keepingCapacity: true)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,66 @@
|
|||||||
|
import Foundation
|
||||||
|
import SwiftUI
|
||||||
|
|
||||||
|
public struct TelemetrySnapshotRecord {
|
||||||
|
public let cpuLoad: Double
|
||||||
|
public let memoryUsedPercent: Double
|
||||||
|
public let networkInBps: Double
|
||||||
|
public let networkOutBps: Double
|
||||||
|
public let diskReadBps: Double
|
||||||
|
public let diskWriteBps: Double
|
||||||
|
public let cpuTemp: Double
|
||||||
|
public let systemPowerWatts: Double
|
||||||
|
public let timestamp: Date
|
||||||
|
|
||||||
|
public init(
|
||||||
|
cpuLoad: Double = 0.0,
|
||||||
|
memoryUsedPercent: Double = 0.0,
|
||||||
|
networkInBps: Double = 0.0,
|
||||||
|
networkOutBps: Double = 0.0,
|
||||||
|
diskReadBps: Double = 0.0,
|
||||||
|
diskWriteBps: Double = 0.0,
|
||||||
|
cpuTemp: Double = 0.0,
|
||||||
|
systemPowerWatts: Double = 0.0,
|
||||||
|
timestamp: Date = Date()
|
||||||
|
) {
|
||||||
|
self.cpuLoad = cpuLoad
|
||||||
|
self.memoryUsedPercent = memoryUsedPercent
|
||||||
|
self.networkInBps = networkInBps
|
||||||
|
self.networkOutBps = networkOutBps
|
||||||
|
self.diskReadBps = diskReadBps
|
||||||
|
self.diskWriteBps = diskWriteBps
|
||||||
|
self.cpuTemp = cpuTemp
|
||||||
|
self.systemPowerWatts = systemPowerWatts
|
||||||
|
self.timestamp = timestamp
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Observable
|
||||||
|
public final class TelemetryHistoryStore {
|
||||||
|
public static let shared = TelemetryHistoryStore()
|
||||||
|
|
||||||
|
public let cpuHistory = RollingRingBuffer<Double>(capacity: 60)
|
||||||
|
public let memoryHistory = RollingRingBuffer<Double>(capacity: 60)
|
||||||
|
public let networkInHistory = RollingRingBuffer<Double>(capacity: 60)
|
||||||
|
public let networkOutHistory = RollingRingBuffer<Double>(capacity: 60)
|
||||||
|
public let diskReadHistory = RollingRingBuffer<Double>(capacity: 60)
|
||||||
|
public let diskWriteHistory = RollingRingBuffer<Double>(capacity: 60)
|
||||||
|
public let cpuTempHistory = RollingRingBuffer<Double>(capacity: 60)
|
||||||
|
public let powerHistory = RollingRingBuffer<Double>(capacity: 60)
|
||||||
|
|
||||||
|
public private(set) var sampleCounter: UInt64 = 0
|
||||||
|
|
||||||
|
public init() {}
|
||||||
|
|
||||||
|
public func record(snapshot: TelemetrySnapshotRecord) {
|
||||||
|
cpuHistory.append(snapshot.cpuLoad, timestamp: snapshot.timestamp)
|
||||||
|
memoryHistory.append(snapshot.memoryUsedPercent, timestamp: snapshot.timestamp)
|
||||||
|
networkInHistory.append(snapshot.networkInBps, timestamp: snapshot.timestamp)
|
||||||
|
networkOutHistory.append(snapshot.networkOutBps, timestamp: snapshot.timestamp)
|
||||||
|
diskReadHistory.append(snapshot.diskReadBps, timestamp: snapshot.timestamp)
|
||||||
|
diskWriteHistory.append(snapshot.diskWriteBps, timestamp: snapshot.timestamp)
|
||||||
|
cpuTempHistory.append(snapshot.cpuTemp, timestamp: snapshot.timestamp)
|
||||||
|
powerHistory.append(snapshot.systemPowerWatts, timestamp: snapshot.timestamp)
|
||||||
|
sampleCounter &+= 1
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,42 @@
|
|||||||
|
#import <Foundation/Foundation.h>
|
||||||
|
#import <CoreGraphics/CoreGraphics.h>
|
||||||
|
|
||||||
|
NS_ASSUME_NONNULL_BEGIN
|
||||||
|
|
||||||
|
@interface MMDisplayInfo : NSObject
|
||||||
|
|
||||||
|
@property (nonatomic, readonly) CGDirectDisplayID displayID;
|
||||||
|
@property (nonatomic, readonly, copy) NSString *name;
|
||||||
|
@property (nonatomic, readonly) BOOL isBuiltin;
|
||||||
|
@property (nonatomic, readonly) BOOL isMain;
|
||||||
|
@property (nonatomic, readonly) BOOL isOnline;
|
||||||
|
@property (nonatomic, readonly) uint32_t width;
|
||||||
|
@property (nonatomic, readonly) uint32_t height;
|
||||||
|
@property (nonatomic, readonly) double refreshRate;
|
||||||
|
@property (nonatomic, readonly) float brightness; // 0.0 to 1.0, or -1.0 if not supported
|
||||||
|
|
||||||
|
- (instancetype)initWithDisplayID:(CGDirectDisplayID)displayID
|
||||||
|
name:(NSString *)name
|
||||||
|
isBuiltin:(BOOL)isBuiltin
|
||||||
|
isMain:(BOOL)isMain
|
||||||
|
isOnline:(BOOL)isOnline
|
||||||
|
width:(uint32_t)width
|
||||||
|
height:(uint32_t)height
|
||||||
|
refreshRate:(double)refreshRate
|
||||||
|
brightness:(float)brightness NS_DESIGNATED_INITIALIZER;
|
||||||
|
|
||||||
|
- (instancetype)init NS_UNAVAILABLE;
|
||||||
|
|
||||||
|
@end
|
||||||
|
|
||||||
|
@interface MMDisplayManager : NSObject
|
||||||
|
|
||||||
|
+ (instancetype)sharedManager;
|
||||||
|
|
||||||
|
- (NSArray<MMDisplayInfo *> *)activeDisplays;
|
||||||
|
- (float)brightnessForDisplay:(CGDirectDisplayID)displayID;
|
||||||
|
- (BOOL)setBrightness:(float)brightness forDisplay:(CGDirectDisplayID)displayID;
|
||||||
|
|
||||||
|
@end
|
||||||
|
|
||||||
|
NS_ASSUME_NONNULL_END
|
||||||
@@ -0,0 +1,168 @@
|
|||||||
|
#import "MMDisplayManager.h"
|
||||||
|
#import <IOKit/graphics/IOGraphicsLib.h>
|
||||||
|
#import <dlfcn.h>
|
||||||
|
|
||||||
|
// Private DisplayServices API signatures
|
||||||
|
typedef int (*DisplayServicesGetBrightnessFunc)(CGDirectDisplayID display, float *brightness);
|
||||||
|
typedef int (*DisplayServicesSetBrightnessFunc)(CGDirectDisplayID display, float brightness);
|
||||||
|
|
||||||
|
@implementation MMDisplayInfo
|
||||||
|
|
||||||
|
- (instancetype)initWithDisplayID:(CGDirectDisplayID)displayID
|
||||||
|
name:(NSString *)name
|
||||||
|
isBuiltin:(BOOL)isBuiltin
|
||||||
|
isMain:(BOOL)isMain
|
||||||
|
isOnline:(BOOL)isOnline
|
||||||
|
width:(uint32_t)width
|
||||||
|
height:(uint32_t)height
|
||||||
|
refreshRate:(double)refreshRate
|
||||||
|
brightness:(float)brightness {
|
||||||
|
self = [super init];
|
||||||
|
if (self) {
|
||||||
|
_displayID = displayID;
|
||||||
|
_name = [name copy] ?: @"Unknown Display";
|
||||||
|
_isBuiltin = isBuiltin;
|
||||||
|
_isMain = isMain;
|
||||||
|
_isOnline = isOnline;
|
||||||
|
_width = width;
|
||||||
|
_height = height;
|
||||||
|
_refreshRate = refreshRate;
|
||||||
|
_brightness = brightness;
|
||||||
|
}
|
||||||
|
return self;
|
||||||
|
}
|
||||||
|
|
||||||
|
@end
|
||||||
|
|
||||||
|
@implementation MMDisplayManager {
|
||||||
|
void *_displayServicesHandle;
|
||||||
|
DisplayServicesGetBrightnessFunc _getBrightnessFunc;
|
||||||
|
DisplayServicesSetBrightnessFunc _setBrightnessFunc;
|
||||||
|
}
|
||||||
|
|
||||||
|
+ (instancetype)sharedManager {
|
||||||
|
static MMDisplayManager *sharedInstance = nil;
|
||||||
|
static dispatch_once_t onceToken;
|
||||||
|
dispatch_once(&onceToken, ^{
|
||||||
|
sharedInstance = [[self alloc] init];
|
||||||
|
});
|
||||||
|
return sharedInstance;
|
||||||
|
}
|
||||||
|
|
||||||
|
- (instancetype)init {
|
||||||
|
self = [super init];
|
||||||
|
if (self) {
|
||||||
|
[self loadDisplayServices];
|
||||||
|
}
|
||||||
|
return self;
|
||||||
|
}
|
||||||
|
|
||||||
|
- (void)dealloc {
|
||||||
|
if (_displayServicesHandle) {
|
||||||
|
dlclose(_displayServicesHandle);
|
||||||
|
_displayServicesHandle = NULL;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
- (void)loadDisplayServices {
|
||||||
|
_displayServicesHandle = dlopen("/System/Library/PrivateFrameworks/DisplayServices.framework/DisplayServices", RTLD_LAZY);
|
||||||
|
if (_displayServicesHandle) {
|
||||||
|
_getBrightnessFunc = (DisplayServicesGetBrightnessFunc)dlsym(_displayServicesHandle, "DisplayServicesGetBrightness");
|
||||||
|
_setBrightnessFunc = (DisplayServicesSetBrightnessFunc)dlsym(_displayServicesHandle, "DisplayServicesSetBrightness");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
- (NSArray<MMDisplayInfo *> *)activeDisplays {
|
||||||
|
uint32_t maxDisplays = 16;
|
||||||
|
CGDirectDisplayID onlineDisplays[16];
|
||||||
|
uint32_t displayCount = 0;
|
||||||
|
|
||||||
|
CGError err = CGGetOnlineDisplayList(maxDisplays, onlineDisplays, &displayCount);
|
||||||
|
if (err != kCGErrorSuccess || displayCount == 0) {
|
||||||
|
return @[];
|
||||||
|
}
|
||||||
|
|
||||||
|
NSMutableArray<MMDisplayInfo *> *result = [NSMutableArray arrayWithCapacity:displayCount];
|
||||||
|
CGDirectDisplayID mainDisplay = CGMainDisplayID();
|
||||||
|
|
||||||
|
for (uint32_t i = 0; i < displayCount; i++) {
|
||||||
|
CGDirectDisplayID dID = onlineDisplays[i];
|
||||||
|
BOOL isBuiltin = CGDisplayIsBuiltin(dID);
|
||||||
|
BOOL isMain = (dID == mainDisplay);
|
||||||
|
BOOL isOnline = CGDisplayIsOnline(dID);
|
||||||
|
|
||||||
|
uint32_t width = (uint32_t)CGDisplayPixelsWide(dID);
|
||||||
|
uint32_t height = (uint32_t)CGDisplayPixelsHigh(dID);
|
||||||
|
|
||||||
|
CGDisplayModeRef mode = CGDisplayCopyDisplayMode(dID);
|
||||||
|
double refreshRate = 0.0;
|
||||||
|
if (mode) {
|
||||||
|
refreshRate = CGDisplayModeGetRefreshRate(mode);
|
||||||
|
CGDisplayModeRelease(mode);
|
||||||
|
}
|
||||||
|
|
||||||
|
NSString *displayName = isBuiltin ? @"Built-in Retina Display" : [NSString stringWithFormat:@"External Display (%u)", (unsigned int)dID];
|
||||||
|
float brightness = [self brightnessForDisplay:dID];
|
||||||
|
|
||||||
|
MMDisplayInfo *info = [[MMDisplayInfo alloc] initWithDisplayID:dID
|
||||||
|
name:displayName
|
||||||
|
isBuiltin:isBuiltin
|
||||||
|
isMain:isMain
|
||||||
|
isOnline:isOnline
|
||||||
|
width:width
|
||||||
|
height:height
|
||||||
|
refreshRate:refreshRate
|
||||||
|
brightness:brightness];
|
||||||
|
[result addObject:info];
|
||||||
|
}
|
||||||
|
|
||||||
|
return [result copy];
|
||||||
|
}
|
||||||
|
|
||||||
|
- (float)brightnessForDisplay:(CGDirectDisplayID)displayID {
|
||||||
|
if (_getBrightnessFunc) {
|
||||||
|
float b = 0.0f;
|
||||||
|
int status = _getBrightnessFunc(displayID, &b);
|
||||||
|
if (status == 0) {
|
||||||
|
return b;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Fallback using IOKit
|
||||||
|
io_service_t service = CGDisplayIOServicePort(displayID);
|
||||||
|
if (service != MACH_PORT_NULL) {
|
||||||
|
float brightness = 0.0f;
|
||||||
|
CFStringRef key = CFSTR(kIODisplayBrightnessKey);
|
||||||
|
kern_return_t kr = IODisplayGetFloatParameter(service, kNilOptions, key, &brightness);
|
||||||
|
if (kr == kIOReturnSuccess) {
|
||||||
|
return brightness;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return -1.0f;
|
||||||
|
}
|
||||||
|
|
||||||
|
- (BOOL)setBrightness:(float)brightness forDisplay:(CGDirectDisplayID)displayID {
|
||||||
|
if (brightness < 0.0f) brightness = 0.0f;
|
||||||
|
if (brightness > 1.0f) brightness = 1.0f;
|
||||||
|
|
||||||
|
if (_setBrightnessFunc) {
|
||||||
|
int status = _setBrightnessFunc(displayID, brightness);
|
||||||
|
if (status == 0) {
|
||||||
|
return YES;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
io_service_t service = CGDisplayIOServicePort(displayID);
|
||||||
|
if (service != MACH_PORT_NULL) {
|
||||||
|
CFStringRef key = CFSTR(kIODisplayBrightnessKey);
|
||||||
|
kern_return_t kr = IODisplaySetFloatParameter(service, kNilOptions, key, brightness);
|
||||||
|
if (kr == kIOReturnSuccess) {
|
||||||
|
return YES;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return NO;
|
||||||
|
}
|
||||||
|
|
||||||
|
@end
|
||||||
@@ -0,0 +1,89 @@
|
|||||||
|
import SwiftUI
|
||||||
|
import Charts
|
||||||
|
|
||||||
|
public struct TelemetryTrendChartView: View {
|
||||||
|
public let title: String
|
||||||
|
public let unit: String
|
||||||
|
public let color: Color
|
||||||
|
public let samples: [HistoricalSample<Double>]
|
||||||
|
public let maxY: Double?
|
||||||
|
|
||||||
|
public init(
|
||||||
|
title: String,
|
||||||
|
unit: String,
|
||||||
|
color: Color,
|
||||||
|
samples: [HistoricalSample<Double>],
|
||||||
|
maxY: Double? = nil
|
||||||
|
) {
|
||||||
|
self.title = title
|
||||||
|
self.unit = unit
|
||||||
|
self.color = color
|
||||||
|
self.samples = samples
|
||||||
|
self.maxY = maxY
|
||||||
|
}
|
||||||
|
|
||||||
|
private var currentValue: Double {
|
||||||
|
samples.last?.value ?? 0.0
|
||||||
|
}
|
||||||
|
|
||||||
|
private var averageValue: Double {
|
||||||
|
guard !samples.isEmpty else { return 0.0 }
|
||||||
|
return samples.reduce(0.0) { $0 + $1.value } / Double(samples.count)
|
||||||
|
}
|
||||||
|
|
||||||
|
private var maxValue: Double {
|
||||||
|
samples.map(\.value).max() ?? 0.0
|
||||||
|
}
|
||||||
|
|
||||||
|
public var body: some View {
|
||||||
|
VStack(alignment: .leading, spacing: 10) {
|
||||||
|
HStack {
|
||||||
|
Text(title)
|
||||||
|
.font(.headline)
|
||||||
|
Spacer()
|
||||||
|
HStack(spacing: 12) {
|
||||||
|
Text(String(format: "Cur: %.1f %@", currentValue, unit))
|
||||||
|
.font(.caption)
|
||||||
|
.fontWeight(.semibold)
|
||||||
|
.foregroundColor(color)
|
||||||
|
Text(String(format: "Avg: %.1f %@", averageValue, unit))
|
||||||
|
.font(.caption)
|
||||||
|
.foregroundColor(.secondary)
|
||||||
|
Text(String(format: "Max: %.1f %@", maxValue, unit))
|
||||||
|
.font(.caption)
|
||||||
|
.foregroundColor(.secondary)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Chart {
|
||||||
|
ForEach(Array(samples.enumerated()), id: \.element.id) { index, sample in
|
||||||
|
LineMark(
|
||||||
|
x: .value("Sample", index),
|
||||||
|
y: .value("Value", sample.value)
|
||||||
|
)
|
||||||
|
.interpolationMethod(.monotone)
|
||||||
|
.foregroundStyle(color)
|
||||||
|
|
||||||
|
AreaMark(
|
||||||
|
x: .value("Sample", index),
|
||||||
|
y: .value("Value", sample.value)
|
||||||
|
)
|
||||||
|
.interpolationMethod(.monotone)
|
||||||
|
.foregroundStyle(
|
||||||
|
LinearGradient(
|
||||||
|
colors: [color.opacity(0.35), color.opacity(0.05)],
|
||||||
|
startPoint: .top,
|
||||||
|
endPoint: .bottom
|
||||||
|
)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
.chartYScale(domain: 0...(maxY ?? max(1.0, maxValue * 1.15)))
|
||||||
|
.chartXAxis(.hidden)
|
||||||
|
.frame(height: 120)
|
||||||
|
}
|
||||||
|
.padding(14)
|
||||||
|
.background(Color(NSColor.controlBackgroundColor))
|
||||||
|
.cornerRadius(10)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,28 @@
|
|||||||
|
import SwiftUI
|
||||||
|
|
||||||
|
public struct MenuBarStatusView: View {
|
||||||
|
public let cpuLoad: Double
|
||||||
|
public let memoryPercent: Double
|
||||||
|
|
||||||
|
public init(cpuLoad: Double, memoryPercent: Double) {
|
||||||
|
self.cpuLoad = cpuLoad
|
||||||
|
self.memoryPercent = memoryPercent
|
||||||
|
}
|
||||||
|
|
||||||
|
public var body: some View {
|
||||||
|
HStack(spacing: 5) {
|
||||||
|
Image(systemName: "cpu")
|
||||||
|
.font(.system(size: 11))
|
||||||
|
Text(String(format: "%.0f%%", cpuLoad))
|
||||||
|
.font(.system(size: 11, weight: .medium, design: .monospaced))
|
||||||
|
|
||||||
|
Text("•")
|
||||||
|
.foregroundColor(.secondary)
|
||||||
|
|
||||||
|
Image(systemName: "memorychip")
|
||||||
|
.font(.system(size: 11))
|
||||||
|
Text(String(format: "%.0f%%", memoryPercent))
|
||||||
|
.font(.system(size: 11, weight: .medium, design: .monospaced))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,134 @@
|
|||||||
|
import SwiftUI
|
||||||
|
|
||||||
|
public struct QuickGlancePopoverView: View {
|
||||||
|
var store: SystemTelemetryStore
|
||||||
|
|
||||||
|
public init(store: SystemTelemetryStore) {
|
||||||
|
self.store = store
|
||||||
|
}
|
||||||
|
|
||||||
|
public var body: some View {
|
||||||
|
VStack(alignment: .leading, spacing: 14) {
|
||||||
|
// Header
|
||||||
|
HStack {
|
||||||
|
Label("MacMonitor", systemImage: "macmini")
|
||||||
|
.font(.headline)
|
||||||
|
Spacer()
|
||||||
|
Button {
|
||||||
|
NSApp.activate(ignoringOtherApps: true)
|
||||||
|
if let window = NSApp.windows.first(where: { $0.canBecomeMain }) {
|
||||||
|
window.makeKeyAndOrderFront(nil)
|
||||||
|
}
|
||||||
|
} label: {
|
||||||
|
Image(systemName: "arrow.up.right.square")
|
||||||
|
.foregroundColor(.secondary)
|
||||||
|
}
|
||||||
|
.buttonStyle(.plain)
|
||||||
|
.help("Open Main Window")
|
||||||
|
}
|
||||||
|
|
||||||
|
Divider()
|
||||||
|
|
||||||
|
// Core Metrics Grid
|
||||||
|
VStack(spacing: 8) {
|
||||||
|
glanceRow(
|
||||||
|
icon: "cpu",
|
||||||
|
title: "CPU Activity",
|
||||||
|
value: String(format: "%.1f%%", store.cpuLoad.totalLoad),
|
||||||
|
subtitle: "\(store.logicalCpuCount) Cores"
|
||||||
|
)
|
||||||
|
|
||||||
|
glanceRow(
|
||||||
|
icon: "memorychip",
|
||||||
|
title: "Memory",
|
||||||
|
value: String(format: "%.1f%%", store.memory.utilizationPercentage),
|
||||||
|
subtitle: "\(formatBytes(store.memory.usedBytes)) / \(formatBytes(store.memory.totalBytes))"
|
||||||
|
)
|
||||||
|
|
||||||
|
glanceRow(
|
||||||
|
icon: "thermometer.medium",
|
||||||
|
title: "Thermals",
|
||||||
|
value: String(format: "%.1f°C", store.cpuThermal.packageTemperature),
|
||||||
|
subtitle: "Fans: \(store.fans.map { "\(Int($0.currentRPM)) RPM" }.joined(separator: ", "))"
|
||||||
|
)
|
||||||
|
|
||||||
|
glanceRow(
|
||||||
|
icon: "bolt.fill",
|
||||||
|
title: "Power",
|
||||||
|
value: String(format: "%.1f W", store.power.systemTotalWatts),
|
||||||
|
subtitle: store.batteryHealth.isCharging ? "Charging (\(Int(store.batteryHealth.healthPercent))%)" : "Battery (\(Int(store.batteryHealth.healthPercent))%)"
|
||||||
|
)
|
||||||
|
|
||||||
|
glanceRow(
|
||||||
|
icon: "network",
|
||||||
|
title: "Network",
|
||||||
|
value: "↓ \(formatBps(totalDownloadBps)) / ↑ \(formatBps(totalUploadBps))",
|
||||||
|
subtitle: "\(store.networkBandwidth.count) active interfaces"
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
Divider()
|
||||||
|
|
||||||
|
// Footer action
|
||||||
|
HStack {
|
||||||
|
Text("Last updated: \(store.lastUpdateTimestamp.formatted(date: .omitted, time: .standard))")
|
||||||
|
.font(.caption2)
|
||||||
|
.foregroundColor(.secondary)
|
||||||
|
Spacer()
|
||||||
|
Button("Quit") {
|
||||||
|
NSApplication.shared.terminate(nil)
|
||||||
|
}
|
||||||
|
.font(.caption)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
.padding(14)
|
||||||
|
.frame(width: 320)
|
||||||
|
}
|
||||||
|
|
||||||
|
private var totalDownloadBps: Double {
|
||||||
|
store.networkBandwidth.reduce(0.0) { $0 + $1.downloadBps }
|
||||||
|
}
|
||||||
|
|
||||||
|
private var totalUploadBps: Double {
|
||||||
|
store.networkBandwidth.reduce(0.0) { $0 + $1.uploadBps }
|
||||||
|
}
|
||||||
|
|
||||||
|
private func glanceRow(icon: String, title: String, value: String, subtitle: String) -> some View {
|
||||||
|
HStack {
|
||||||
|
Image(systemName: icon)
|
||||||
|
.frame(width: 18)
|
||||||
|
.foregroundColor(.accentColor)
|
||||||
|
VStack(alignment: .leading, spacing: 2) {
|
||||||
|
Text(title)
|
||||||
|
.font(.caption)
|
||||||
|
.foregroundColor(.secondary)
|
||||||
|
Text(subtitle)
|
||||||
|
.font(.caption2)
|
||||||
|
.foregroundColor(.secondary)
|
||||||
|
.lineLimit(1)
|
||||||
|
}
|
||||||
|
Spacer()
|
||||||
|
Text(value)
|
||||||
|
.font(.subheadline)
|
||||||
|
.fontWeight(.semibold)
|
||||||
|
.fontDesign(.monospaced)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private func formatBytes(_ bytes: UInt64) -> String {
|
||||||
|
let formatter = ByteCountFormatter()
|
||||||
|
formatter.allowedUnits = [.useAll]
|
||||||
|
formatter.countStyle = .memory
|
||||||
|
return formatter.string(fromByteCount: Int64(bytes))
|
||||||
|
}
|
||||||
|
|
||||||
|
private func formatBps(_ bps: Double) -> String {
|
||||||
|
if bps < 1024 {
|
||||||
|
return String(format: "%.0f B/s", bps)
|
||||||
|
} else if bps < 1024 * 1024 {
|
||||||
|
return String(format: "%.1f KB/s", bps / 1024)
|
||||||
|
} else {
|
||||||
|
return String(format: "%.2f MB/s", bps / (1024 * 1024))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,46 @@
|
|||||||
|
import XCTest
|
||||||
|
@testable import MacMonitor
|
||||||
|
|
||||||
|
final class MMDisplayTests: XCTestCase {
|
||||||
|
|
||||||
|
func testDisplayManagerActiveDisplays() {
|
||||||
|
let manager = MMDisplayManager.shared()
|
||||||
|
XCTAssertNotNil(manager, "MMDisplayManager instance should not be nil")
|
||||||
|
|
||||||
|
let displays = manager.activeDisplays()
|
||||||
|
XCTAssertNotNil(displays, "Active displays array should not be nil")
|
||||||
|
// In CI or headless/headless VM or real Mac, online display list can be checked
|
||||||
|
for display in displays {
|
||||||
|
XCTAssertGreaterThan(display.displayID, 0)
|
||||||
|
XCTAssertFalse(display.name.isEmpty)
|
||||||
|
if display.isOnline {
|
||||||
|
XCTAssertGreaterThan(display.width, 0)
|
||||||
|
XCTAssertGreaterThan(display.height, 0)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func testDisplayInfoInitialization() {
|
||||||
|
let info = MMDisplayInfo(
|
||||||
|
displayID: 1001,
|
||||||
|
name: "Test Display",
|
||||||
|
isBuiltin: true,
|
||||||
|
isMain: true,
|
||||||
|
isOnline: true,
|
||||||
|
width: 2560,
|
||||||
|
height: 1600,
|
||||||
|
refreshRate: 60.0,
|
||||||
|
brightness: 0.75
|
||||||
|
)
|
||||||
|
|
||||||
|
XCTAssertEqual(info.displayID, 1001)
|
||||||
|
XCTAssertEqual(info.name, "Test Display")
|
||||||
|
XCTAssertTrue(info.isBuiltin)
|
||||||
|
XCTAssertTrue(info.isMain)
|
||||||
|
XCTAssertTrue(info.isOnline)
|
||||||
|
XCTAssertEqual(info.width, 2560)
|
||||||
|
XCTAssertEqual(info.height, 1600)
|
||||||
|
XCTAssertEqual(info.refreshRate, 60.0)
|
||||||
|
XCTAssertEqual(info.brightness, 0.75, accuracy: 0.001)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,54 @@
|
|||||||
|
import XCTest
|
||||||
|
@testable import MacMonitor
|
||||||
|
|
||||||
|
final class MMHistoryTests: XCTestCase {
|
||||||
|
|
||||||
|
func testRollingRingBufferCapacityAndEviction() {
|
||||||
|
let buffer = RollingRingBuffer<Double>(capacity: 3)
|
||||||
|
XCTAssertEqual(buffer.count, 0)
|
||||||
|
|
||||||
|
buffer.append(10.0)
|
||||||
|
buffer.append(20.0)
|
||||||
|
XCTAssertEqual(buffer.count, 2)
|
||||||
|
XCTAssertEqual(buffer.samples.map(\.value), [10.0, 20.0])
|
||||||
|
|
||||||
|
buffer.append(30.0)
|
||||||
|
XCTAssertEqual(buffer.count, 3)
|
||||||
|
XCTAssertEqual(buffer.samples.map(\.value), [10.0, 20.0, 30.0])
|
||||||
|
|
||||||
|
// 4th append should evict oldest (10.0)
|
||||||
|
buffer.append(40.0)
|
||||||
|
XCTAssertEqual(buffer.count, 3)
|
||||||
|
XCTAssertEqual(buffer.samples.map(\.value), [20.0, 30.0, 40.0])
|
||||||
|
|
||||||
|
buffer.clear()
|
||||||
|
XCTAssertEqual(buffer.count, 0)
|
||||||
|
XCTAssertTrue(buffer.samples.isEmpty)
|
||||||
|
}
|
||||||
|
|
||||||
|
func testTelemetryHistoryStoreRecord() {
|
||||||
|
let store = TelemetryHistoryStore()
|
||||||
|
let snapshot = TelemetrySnapshotRecord(
|
||||||
|
cpuLoad: 25.5,
|
||||||
|
memoryUsedPercent: 62.1,
|
||||||
|
networkInBps: 1024.0,
|
||||||
|
networkOutBps: 2048.0,
|
||||||
|
diskReadBps: 512.0,
|
||||||
|
diskWriteBps: 1024.0,
|
||||||
|
cpuTemp: 55.0,
|
||||||
|
systemPowerWatts: 18.5
|
||||||
|
)
|
||||||
|
store.record(snapshot: snapshot)
|
||||||
|
|
||||||
|
XCTAssertEqual(store.sampleCounter, 1)
|
||||||
|
XCTAssertEqual(store.cpuHistory.count, 1)
|
||||||
|
XCTAssertEqual(store.cpuHistory.samples.first?.value, 25.5)
|
||||||
|
XCTAssertEqual(store.memoryHistory.samples.first?.value, 62.1)
|
||||||
|
XCTAssertEqual(store.networkInHistory.samples.first?.value, 1024.0)
|
||||||
|
XCTAssertEqual(store.networkOutHistory.samples.first?.value, 2048.0)
|
||||||
|
XCTAssertEqual(store.diskReadHistory.samples.first?.value, 512.0)
|
||||||
|
XCTAssertEqual(store.diskWriteHistory.samples.first?.value, 1024.0)
|
||||||
|
XCTAssertEqual(store.cpuTempHistory.samples.first?.value, 55.0)
|
||||||
|
XCTAssertEqual(store.powerHistory.samples.first?.value, 18.5)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,18 @@
|
|||||||
|
import XCTest
|
||||||
|
import SwiftUI
|
||||||
|
@testable import MacMonitor
|
||||||
|
|
||||||
|
final class MMMenuBarTests: XCTestCase {
|
||||||
|
|
||||||
|
func testMenuBarStatusViewInitialization() {
|
||||||
|
let view = MenuBarStatusView(cpuLoad: 24.5, memoryPercent: 55.0)
|
||||||
|
XCTAssertEqual(view.cpuLoad, 24.5)
|
||||||
|
XCTAssertEqual(view.memoryPercent, 55.0)
|
||||||
|
}
|
||||||
|
|
||||||
|
func testQuickGlancePopoverViewInitialization() {
|
||||||
|
let store = SystemTelemetryStore.shared
|
||||||
|
let view = QuickGlancePopoverView(store: store)
|
||||||
|
XCTAssertNotNil(view)
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user