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

#241 Showing battery time without battery icon (as an option) #245

Merged
merged 8 commits into from
Dec 27, 2020
Merged
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
2 changes: 1 addition & 1 deletion iGlance/Libraries/SystemKit
2 changes: 1 addition & 1 deletion iGlance/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,4 @@ SPEC CHECKSUMS:

PODFILE CHECKSUM: a4be4af5f64fb9acd8ae8bfdf92b50ea6e14a6b7

COCOAPODS: 1.9.1
COCOAPODS: 1.10.0
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "1220"
LastUpgradeVersion = "1230"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "1220"
LastUpgradeVersion = "1230"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "1220"
LastUpgradeVersion = "1230"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "1220"
LastUpgradeVersion = "1230"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
Expand Down
350 changes: 212 additions & 138 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
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down
37 changes: 22 additions & 15 deletions iGlance/iGlance/iGlance/MenuBarItems/BatteryMenuBarItem.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
5 changes: 5 additions & 0 deletions iGlance/iGlance/iGlance/UserSettings.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
}
Expand Down