diff --git a/iGlance/Libraries/SystemKit b/iGlance/Libraries/SystemKit index 2b86cc1..d5b48a0 160000 --- a/iGlance/Libraries/SystemKit +++ b/iGlance/Libraries/SystemKit @@ -1 +1 @@ -Subproject commit 2b86cc1dedfa1ee93fd7800b2bf2f37d0b468e1d +Subproject commit d5b48a099944ec2d9b25868f2fc9f1c75cbc87f3 diff --git a/iGlance/Podfile.lock b/iGlance/Podfile.lock index 4f6964f..bb41bfe 100644 --- a/iGlance/Podfile.lock +++ b/iGlance/Podfile.lock @@ -23,4 +23,4 @@ SPEC CHECKSUMS: PODFILE CHECKSUM: a4be4af5f64fb9acd8ae8bfdf92b50ea6e14a6b7 -COCOAPODS: 1.9.1 +COCOAPODS: 1.10.0 diff --git a/iGlance/iGlance/iGlance.xcodeproj/xcshareddata/xcschemes/iGlance.xcscheme b/iGlance/iGlance/iGlance.xcodeproj/xcshareddata/xcschemes/iGlance.xcscheme index cb5d052..4890913 100644 --- a/iGlance/iGlance/iGlance.xcodeproj/xcshareddata/xcschemes/iGlance.xcscheme +++ b/iGlance/iGlance/iGlance.xcodeproj/xcshareddata/xcschemes/iGlance.xcscheme @@ -1,6 +1,6 @@ - + - + @@ -1258,167 +1258,241 @@ - - - + + - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + - + diff --git a/iGlance/iGlance/iGlance/MainWindow/MainViews/BatteryViewController.swift b/iGlance/iGlance/iGlance/MainWindow/MainViews/BatteryViewController.swift index 388797c..a055eb4 100644 --- a/iGlance/iGlance/iGlance/MainWindow/MainViews/BatteryViewController.swift +++ b/iGlance/iGlance/iGlance/MainWindow/MainViews/BatteryViewController.swift @@ -38,7 +38,13 @@ class BatteryViewController: MainViewViewController { } } - @IBOutlet private var displayedInfoStackView: NSStackView! { + @IBOutlet private var batteryIconCheckbox: NSButton! { + didSet { + batteryIconCheckbox.state = AppDelegate.userSettings.settings.battery.showBatteryIcon ? .on : .off + } + } + + @IBOutlet private var displayedInfoStackView: NSView! { didSet { // if the usage is not displayed hide it if !AppDelegate.userSettings.settings.battery.showBatteryMenuBarItem { @@ -132,6 +138,19 @@ class BatteryViewController: MainViewViewController { DDLogInfo("Did set battery checkbox value to (\(activated))") } + @IBAction private func batteryIconCheckboxChanged(_ sender: NSButton) { + // get the boolean value of the checkbox + let activated = sender.state == .on + + // set the user settings + AppDelegate.userSettings.settings.battery.showBatteryIcon = activated + + // update the menu bar items to make the change visible immediately + AppDelegate.menuBarItemManager.updateMenuBarItems() + + DDLogInfo("Did set battery icon visibility checkbox value to (\(activated))") + } + @IBAction private func batterySelectorChanged(_ sender: NSPopUpButton) { if batterySelector.indexOfSelectedItem == 0 { // the first item is to display the remaining time diff --git a/iGlance/iGlance/iGlance/MenuBarItems/BatteryMenuBarItem.swift b/iGlance/iGlance/iGlance/MenuBarItems/BatteryMenuBarItem.swift index 09eead2..e26312a 100644 --- a/iGlance/iGlance/iGlance/MenuBarItems/BatteryMenuBarItem.swift +++ b/iGlance/iGlance/iGlance/MenuBarItems/BatteryMenuBarItem.swift @@ -104,32 +104,39 @@ class BatteryMenuBarItem: MenuBarItem { buttonString = getRemainingTimeString(batteryState: batteryState) } - // get the battery icon - let batteryIcon = getBatteryIcon(currentCharge: currentCharge, isOnAC: isOnAC, isCharging: isCharging, isCharged: isCharged, batteryState: batteryState) - // the battery icon is nil loading the icon failed - if batteryIcon == nil { - return + var batteryIcon: NSImage? + if AppDelegate.userSettings.settings.battery.showBatteryIcon { + // get the battery icon + batteryIcon = getBatteryIcon(currentCharge: currentCharge, isOnAC: isOnAC, isCharging: isCharging, isCharged: isCharged, batteryState: batteryState) } + let batteryIconSize: CGSize = batteryIcon?.size ?? .zero // create the menu bar image let marginBetweenIconAndString = CGFloat(5) - let image = NSImage( - size: NSSize( - width: buttonString.size().width + batteryIcon!.size.width + marginBetweenIconAndString, - height: self.menuBarHeight - ) - ) + var iconWidth: CGFloat = buttonString.size().width + if batteryIconSize.width > .ulpOfOne { + iconWidth += batteryIconSize.width + marginBetweenIconAndString + } + + let iconSize = NSSize(width: iconWidth, height: self.menuBarHeight) + let image = NSImage(size: iconSize) // lock the image to render the string image.lockFocus() // render the string - buttonString.draw(at: NSPoint(x: image.size.width - buttonString.size().width, y: image.size.height / 2 - buttonString.size().height / 2)) + buttonString.draw(at: NSPoint(x: iconSize.width - buttonString.size().width, + y: iconSize.height / 2 - buttonString.size().height / 2)) // tint the battery icon to match it to the theme of the os - let tintedBatteryIcon = batteryIcon!.tint(color: ThemeManager.isDarkTheme() ? NSColor.white : NSColor.black) - // 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) + if let tintedBatteryIcon = batteryIcon?.tint(color: ThemeManager.isDarkTheme() ? NSColor.white : NSColor.black) { + // render the battery icon + tintedBatteryIcon.draw( + at: NSPoint(x: 0, y: (iconSize.height - tintedBatteryIcon.size.height) / 2), + from: NSRect.zero, + operation: .sourceOver, + fraction: 1.0) + } // unlock the focus of the image image.unlockFocus() diff --git a/iGlance/iGlance/iGlance/UserSettings.swift b/iGlance/iGlance/iGlance/UserSettings.swift index f9c52e1..d8252bd 100644 --- a/iGlance/iGlance/iGlance/UserSettings.swift +++ b/iGlance/iGlance/iGlance/UserSettings.swift @@ -158,6 +158,7 @@ struct BatteryNotificationSettings: Codable { struct BatterySettings: Codable { var showBatteryMenuBarItem: Bool = AppDelegate.systemInfo.battery.hasBattery() + var showBatteryIcon: Bool = true /// Is true when the percentage of the battery charge is displayed. If the value is false, the remaining time is displayed instead var showPercentage: Bool = false var lowBatteryNotification = BatteryNotificationSettings(notifyUser: true, value: 20) @@ -172,6 +173,10 @@ struct BatterySettings: Codable { self.showBatteryMenuBarItem = decodedShowBatteryMenuBarItem } + if let showBatteryIcon = try? container.decodeIfPresent(Bool.self, forKey: .showBatteryIcon) { + self.showBatteryIcon = showBatteryIcon + } + if let decodedShowPercentage = try? container.decodeIfPresent(Bool.self, forKey: .showBatteryMenuBarItem) { self.showPercentage = decodedShowPercentage }