Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix MacOS Big Sur Dark/Light theme font/border color issue #248

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion iGlance/iGlance/iGlance/Graphs/Graph.swift
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,11 @@ class GraphClass {
if ThemeManager.isDarkTheme() {
NSColor.white.set()
} else {
NSColor.black.set()
if #available(macOS 11.0, *) {
NSColor.white.set()
} else {
NSColor.black.set()
}
}

// draw the border
Expand Down
61 changes: 55 additions & 6 deletions iGlance/iGlance/iGlance/MenuBarItems/BatteryMenuBarItem.swift
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,16 @@ class BatteryMenuBarItem: MenuBarItem {
// render the string
buttonString.draw(at: NSPoint(x: image.size.width - buttonString.size().width, y: image.size.height / 2 - buttonString.size().height / 2))

func blackOrWhite() -> NSColor {
var returnColor = NSColor.black
if #available(macOS 11.0, *) {
returnColor = NSColor.white
}
return returnColor
}

// tint the battery icon to match it to the theme of the os
let tintedBatteryIcon = batteryIcon!.tint(color: ThemeManager.isDarkTheme() ? NSColor.white : NSColor.black)
let tintedBatteryIcon = batteryIcon!.tint(color: ThemeManager.isDarkTheme() ? NSColor.white : blackOrWhite())
// render the battery icon
tintedBatteryIcon.draw(at: NSPoint(x: 0, y: 18 / 2 - tintedBatteryIcon.size.height / 2), from: NSRect.zero, operation: .sourceOver, fraction: 1.0)

Expand Down Expand Up @@ -194,10 +202,18 @@ class BatteryMenuBarItem: MenuBarItem {
private func getRemainingTimeString(batteryState: InternalBatteryState) -> NSAttributedString {
var buttonString: NSAttributedString

func blackOrWhite() -> NSColor {
var returnColor = NSColor.black
if #available(macOS 11.0, *) {
returnColor = NSColor.white
}
return returnColor
}

// define the attributes
let attributes = [
NSAttributedString.Key.font: NSFont.systemFont(ofSize: 13),
NSAttributedString.Key.foregroundColor: ThemeManager.isDarkTheme() ? NSColor.white : NSColor.black
NSAttributedString.Key.foregroundColor: ThemeManager.isDarkTheme() ? NSColor.white : blackOrWhite()
]

switch batteryState {
Expand Down Expand Up @@ -227,10 +243,18 @@ class BatteryMenuBarItem: MenuBarItem {
let timeToFull = convertMinutesToHours(minutes: timeToFullMinutes)
let timeToFullString = "\(timeToFull.hours):\(String(format: "%02d", timeToFull.minutes))"

func blackOrWhite() -> NSColor {
var returnColor = NSColor.black
if #available(macOS 11.0, *) {
returnColor = NSColor.white
}
return returnColor
}

// define the attributes
let attributes = [
NSAttributedString.Key.font: NSFont.systemFont(ofSize: 13),
NSAttributedString.Key.foregroundColor: ThemeManager.isDarkTheme() ? NSColor.white : NSColor.black
NSAttributedString.Key.foregroundColor: ThemeManager.isDarkTheme() ? NSColor.white : blackOrWhite()
]

return NSAttributedString(string: timeToFullString, attributes: attributes)
Expand All @@ -245,10 +269,18 @@ class BatteryMenuBarItem: MenuBarItem {
let timeToEmpty = convertMinutesToHours(minutes: timeToEmptyMinutes)
let timeToEmptyString = "\(timeToEmpty.hours):\(String(format: "%02d", timeToEmpty.minutes))"

func blackOrWhite() -> NSColor {
var returnColor = NSColor.black
if #available(macOS 11.0, *) {
returnColor = NSColor.white
}
return returnColor
}

// define the attributes
let attributes = [
NSAttributedString.Key.font: NSFont.systemFont(ofSize: 13),
NSAttributedString.Key.foregroundColor: ThemeManager.isDarkTheme() ? NSColor.white : NSColor.black
NSAttributedString.Key.foregroundColor: ThemeManager.isDarkTheme() ? NSColor.white : blackOrWhite()
]

return NSAttributedString(string: timeToEmptyString, attributes: attributes)
Expand All @@ -272,10 +304,18 @@ class BatteryMenuBarItem: MenuBarItem {
* Returns the string that is displaying the current charge of the battery in percentage.
*/
private func getPercentageString(currentCharge: Int) -> NSAttributedString {
func blackOrWhite() -> NSColor {
var returnColor = NSColor.black
if #available(macOS 11.0, *) {
returnColor = NSColor.white
}
return returnColor
}

// define the attributes
let attributes = [
NSAttributedString.Key.font: NSFont.systemFont(ofSize: 13),
NSAttributedString.Key.foregroundColor: ThemeManager.isDarkTheme() ? NSColor.white : NSColor.black
NSAttributedString.Key.foregroundColor: ThemeManager.isDarkTheme() ? NSColor.white : blackOrWhite()
]

return NSAttributedString(string: "\(currentCharge)%", attributes: attributes)
Expand Down Expand Up @@ -312,8 +352,17 @@ class BatteryMenuBarItem: MenuBarItem {

// create and draw the charge indicator rectangle as a rounded rectangle
let chargeBar = NSRect(x: borderWidth + chargeBarMargin, y: borderWidth + chargeBarMargin, width: chargeBarWidth, height: chargeBarCurrentHeight)

func blackOrWhite() -> NSColor {
var returnColor = NSColor.black
if #available(macOS 11.0, *) {
returnColor = NSColor.white
}
return returnColor
}

let roundedRect = NSBezierPath(roundedRect: chargeBar, xRadius: 1.5, yRadius: 1.5)
(ThemeManager.isDarkTheme() ? NSColor.white : NSColor.black).set()
(ThemeManager.isDarkTheme() ? NSColor.white : blackOrWhite()).set()
roundedRect.fill()

// unlock the focus
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,15 @@ class CpuTempMenuBarItem: MenuBarItem {
let font = NSFont.systemFont(ofSize: 13)

attribString.addAttribute(.font, value: font, range: NSRange(location: 0, length: string.count))
let fontColor = ThemeManager.isDarkTheme() ? NSColor.white : NSColor.black
attribString.addAttribute(.foregroundColor, value: fontColor, range: NSRange(location: 0, length: string.count))

return attribString
if #available(macOS 11.0, *) {
let fontColor = NSColor.white
attribString.addAttribute(.foregroundColor, value: fontColor, range: NSRange(location: 0, length: string.count))
return attribString
} else {
let fontColor = ThemeManager.isDarkTheme() ? NSColor.white : NSColor.black
attribString.addAttribute(.foregroundColor, value: fontColor, range: NSRange(location: 0, length: string.count))
return attribString
}
}
}
10 changes: 9 additions & 1 deletion iGlance/iGlance/iGlance/MenuBarItems/DiskUsageMenuBarItem.swift
Original file line number Diff line number Diff line change
Expand Up @@ -115,12 +115,20 @@ class DiskUsageMenuBarItem: MenuBarItem {
* Creates an attributed string that can be drawn on the menu bar image.
*/
private func createAttributedString(text: String) -> NSAttributedString {
func blackOrWhite() -> NSColor {
var returnColor = NSColor.black
if #available(macOS 11.0, *) {
returnColor = NSColor.white
}
return returnColor
}

let attrString = NSMutableAttributedString(
string: text,
attributes: [
NSAttributedString.Key.font: NSFont.systemFont(ofSize: 9),
NSAttributedString.Key.kern: 1.2,
NSAttributedString.Key.foregroundColor: ThemeManager.isDarkTheme() ? NSColor.white : NSColor.black
NSAttributedString.Key.foregroundColor: ThemeManager.isDarkTheme() ? NSColor.white : blackOrWhite()
]
)

Expand Down
10 changes: 9 additions & 1 deletion iGlance/iGlance/iGlance/MenuBarItems/FanMenuBarItem.swift
Original file line number Diff line number Diff line change
Expand Up @@ -140,10 +140,18 @@ class FanMenuBarItem: MenuBarItem {
// create the attributed string
let string = String(value) + " " + (unit ? "RPM" : "")

func blackOrWhite() -> NSColor {
var returnColor = NSColor.black
if #available(macOS 11.0, *) {
returnColor = NSColor.white
}
return returnColor
}

// define the attributes
let attributes = [
NSAttributedString.Key.font: NSFont.systemFont(ofSize: 13),
NSAttributedString.Key.foregroundColor: ThemeManager.isDarkTheme() ? NSColor.white : NSColor.black
NSAttributedString.Key.foregroundColor: ThemeManager.isDarkTheme() ? NSColor.white : blackOrWhite()
]

// create the string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,8 @@ class NetworkMenuBarItem: MenuBarItem {
self.totalBytesOnLastReset[interfaceName] = AppDelegate.systemInfo.network.getTotalTransmittedBytesOf(interface: interfaceName)
}



/**
* Returns the image that can be rendered on the menu bar.
*/
Expand All @@ -181,13 +183,23 @@ class NetworkMenuBarItem: MenuBarItem {
let uploadString = self.createAttributedBandwidthString(value: valueStringUp, unit: up.unit.rawValue + "/s")
let downloadString = self.createAttributedBandwidthString(value: valueStringDown, unit: down.unit.rawValue + "/s")

func blackOrWhite() -> NSColor {
var returnColor = NSColor.black
if #available(macOS 11.0, *) {
returnColor = NSColor.white
}
return returnColor
}

// get the up arrow icon
guard let arrowUpIcon = NSImage(named: "ArrowUpIcon")?.tint(color: ThemeManager.isDarkTheme() ? NSColor.white : NSColor.black) else {
guard let arrowUpIcon = NSImage(named: "ArrowUpIcon")?.tint(color: ThemeManager.isDarkTheme() ? NSColor.white : blackOrWhite())
else {
DDLogError("An error occurred while loading the bandwidth arrow up menu bar icon")
return nil
}
// get the down arrow icon
guard let arrowDownIcon = NSImage(named: "ArrowDownIcon")?.tint(color: ThemeManager.isDarkTheme() ? NSColor.white : NSColor.black) else {
guard let arrowDownIcon = NSImage(named: "ArrowDownIcon")?.tint(color: ThemeManager.isDarkTheme() ? NSColor.white : blackOrWhite())
else {
DDLogError("An error occurred while loading the bandwidth arrow down menu bar icon")
return nil
}
Expand Down Expand Up @@ -260,9 +272,17 @@ class NetworkMenuBarItem: MenuBarItem {
attrString.addAttribute(.font, value: font, range: NSRange(location: 0, length: attrString.length - 1 - unit.count))
attrString.addAttribute(.kern, value: 1.2, range: NSRange(location: 0, length: attrString.length - 1 - unit.count))
attrString.addAttribute(.font, value: font, range: NSRange(location: attrString.length - unit.count, length: unit.count))
let fontColor = ThemeManager.isDarkTheme() ? NSColor.white : NSColor.black
attrString.addAttribute(.foregroundColor, value: fontColor, range: NSRange(location: 0, length: attrString.length))

return attrString
if #available(macOS 11.0, *) {
let fontColor = NSColor.white
attrString.addAttribute(.foregroundColor, value: fontColor, range: NSRange(location: 0, length: attrString.length))

return attrString
} else {
let fontColor = ThemeManager.isDarkTheme() ? NSColor.white : NSColor.black
attrString.addAttribute(.foregroundColor, value: fontColor, range: NSRange(location: 0, length: attrString.length))

return attrString
}
}
}