diff --git a/iGlance/iGlance/iGlance/Base.lproj/Main.storyboard b/iGlance/iGlance/iGlance/Base.lproj/Main.storyboard index eff2ff4..6108305 100644 --- a/iGlance/iGlance/iGlance/Base.lproj/Main.storyboard +++ b/iGlance/iGlance/iGlance/Base.lproj/Main.storyboard @@ -1258,167 +1258,216 @@ - - - + + - - - - - - - - - - - - - - - - - - - - + - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + - - - - - - - - - - - - - + + + + - + diff --git a/iGlance/iGlance/iGlance/MainWindow/MainViews/BatteryViewController.swift b/iGlance/iGlance/iGlance/MainWindow/MainViews/BatteryViewController.swift index 388797c..3d4827a 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.showBatterIcon ? .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.showBatterIcon = 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..3586840 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.showBatterIcon { + // get the battery icon + batteryIcon = getBatteryIcon(currentCharge: currentCharge, isOnAC: isOnAC, isCharging: isCharging, isCharged: isCharged, batteryState: batteryState) } + let betteryIconSize: CGSize = batteryIcon.map { $0.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 betteryIconSize.width > .ulpOfOne { + iconWidth += betteryIconSize.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 025aef0..abc02aa 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 showBatterIcon: 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 showBatterIcon = try? container.decodeIfPresent(Bool.self, forKey: .showBatterIcon) { + self.showBatterIcon = showBatterIcon + } + if let decodedShowPercentage = try? container.decodeIfPresent(Bool.self, forKey: .showBatteryMenuBarItem) { self.showPercentage = decodedShowPercentage }