Skip to content

Commit

Permalink
iglance#241 Showing battery time without battery icon (as an option)
Browse files Browse the repository at this point in the history
ftp27 committed Nov 7, 2020
1 parent 3a0fe71 commit bca2360
Showing 4 changed files with 232 additions and 152 deletions.
321 changes: 185 additions & 136 deletions iGlance/iGlance/iGlance/Base.lproj/Main.storyboard

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -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
37 changes: 22 additions & 15 deletions iGlance/iGlance/iGlance/MenuBarItems/BatteryMenuBarItem.swift
Original file line number Diff line number Diff line change
@@ -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()
5 changes: 5 additions & 0 deletions iGlance/iGlance/iGlance/UserSettings.swift
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit bca2360

Please sign in to comment.