Skip to content

Commit

Permalink
Fix icons tint
Browse files Browse the repository at this point in the history
Fix icon colors
  • Loading branch information
AlexPerathoner authored Jan 6, 2023
2 parents 79aa364 + ff384d4 commit a187586
Show file tree
Hide file tree
Showing 14 changed files with 89 additions and 87 deletions.
2 changes: 1 addition & 1 deletion .swiftlint.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Swiftlint configuration file

line_length:
warning: 150
warning: 155
error: 165

function_parameter_count:
Expand Down
4 changes: 2 additions & 2 deletions SlimHUD.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -311,13 +311,11 @@
FA95EE4B2956FE50005B39CD /* Extensions */ = {
isa = PBXGroup;
children = (
FA95EE4C2956FE5D005B39CD /* EnabledBars.swift */,
FA95EE4E2956FE69005B39CD /* NSViewExtension.swift */,
FA95EE502956FE73005B39CD /* NSButtonExtension.swift */,
FA95EE522956FE7C005B39CD /* NSControlExtension.swift */,
FA95EE542956FE86005B39CD /* NSImageExtension.swift */,
FA95EE562956FE91005B39CD /* BoolExtension.swift */,
FA95EE582956FE9A005B39CD /* Position.swift */,
FA95EE5A2956FEA9005B39CD /* NSSegmentedControlExtension.swift */,
FAB5AA38295B031900060CB6 /* UserDefaultsExtension.swift */,
);
Expand All @@ -329,6 +327,8 @@
children = (
FA95EE5F2956FECE005B39CD /* Errors */,
FA95EE5D2956FEC3005B39CD /* Constants.swift */,
FA95EE4C2956FE5D005B39CD /* EnabledBars.swift */,
FA95EE582956FE9A005B39CD /* Position.swift */,
FAB5AA36295AF0FF00060CB6 /* SensorMethod.swift */,
FA64B59E296638DA00600F85 /* Alert.swift */,
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,16 @@
"images" : [
{
"filename" : "backlight.png",
"idiom" : "universal",
"scale" : "1x"
},
{
"idiom" : "universal",
"scale" : "2x"
},
{
"idiom" : "universal",
"scale" : "3x"
"idiom" : "universal"
}
],
"info" : {
"author" : "xcode",
"version" : 1
},
"properties" : {
"compression-type" : "lossless",
"preserves-vector-representation" : true,
"template-rendering-intent" : "original"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,16 @@
"images" : [
{
"filename" : "brightness.png",
"idiom" : "universal",
"scale" : "1x"
},
{
"idiom" : "universal",
"scale" : "2x"
},
{
"idiom" : "universal",
"scale" : "3x"
"idiom" : "universal"
}
],
"info" : {
"author" : "xcode",
"version" : 1
},
"properties" : {
"compression-type" : "lossless",
"preserves-vector-representation" : true,
"template-rendering-intent" : "original"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,16 @@
"images" : [
{
"filename" : "noVolume.png",
"idiom" : "universal",
"scale" : "1x"
},
{
"idiom" : "universal",
"scale" : "2x"
},
{
"idiom" : "universal",
"scale" : "3x"
"idiom" : "universal"
}
],
"info" : {
"author" : "xcode",
"version" : 1
},
"properties" : {
"compression-type" : "lossless",
"preserves-vector-representation" : true,
"template-rendering-intent" : "original"
}
}
16 changes: 6 additions & 10 deletions SlimHUD/Assets.xcassets/TemplateBars/volume.imageset/Contents.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,16 @@
"images" : [
{
"filename" : "volume.png",
"idiom" : "universal",
"scale" : "1x"
},
{
"idiom" : "universal",
"scale" : "2x"
},
{
"idiom" : "universal",
"scale" : "3x"
"idiom" : "universal"
}
],
"info" : {
"author" : "xcode",
"version" : 1
},
"properties" : {
"compression-type" : "lossless",
"preserves-vector-representation" : true,
"template-rendering-intent" : "original"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -100,11 +100,13 @@ extension SettingsViewController {
delegate?.setKeyboardColor(color: sender.color)
preview.setKeyboardColor(color: sender.color)
}

@available(OSX 10.14, *)
@IBAction func volumeIconColorChanged(_ sender: NSColorWell) {
settingsManager.volumeIconColor = sender.color
delegate?.setVolumeIconsTint(sender.color)
preview.setVolumeIconsTint(sender.color)
preview.setVolumeDisabledColor(color: settingsManager.volumeDisabledColor)
preview.setVolumeEnabledColor(color: settingsManager.volumeEnabledColor)
}
@available(OSX 10.14, *)
@IBAction func brightnessIconChanged(_ sender: NSColorWell) {
Expand Down
29 changes: 11 additions & 18 deletions SlimHUD/Extensions/NSImageExtension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,15 @@ extension NSImage {
static let KeyboardImageFileName = "backlight"
static let StatusIconFileName = "statusIcon"

}
func tint(with color: NSColor) -> NSImage {
// swiftlint:disable:next force_cast
let image = self.copy() as! NSImage
self.lockFocus()
color.set()
let srcSpacePortionRect = NSRect(origin: NSZeroPoint, size: image.size)
srcSpacePortionRect.fill(using: .sourceAtop)
self.unlockFocus()
return image
}

// extension NSImage {
// /// https://stackoverflow.com/a/50074538/6884062
// /// - Returns: returns the tinted version of a template image
// func tint(color: NSColor) -> NSImage {
// let image = self.copy() as! NSImage
// image.lockFocus()
//
// color.set()
//
// let imageRect = NSRect(origin: NSZeroPoint, size: image.size)
// imageRect.fill(using: .sourceAtop)
//
// image.unlockFocus()
//
// return image
// }
// }
}
2 changes: 1 addition & 1 deletion SlimHUD/Services/Displayer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ class Displayer: HudsControllerInterface {
barView.bar.layer?.cornerRadius = thickness/2 // setting up outer layer
barView.bar.frame.size.width = thickness
}

@available(OSX 10.14, *)
func setVolumeIconsTint(_ color: NSColor) {
getIcon(hud: volumeHud).contentTintColor = color
Expand Down
2 changes: 1 addition & 1 deletion SlimHUD/Services/SettingsManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ class SettingsManager {
keyboardColor = UserDefaultsManager.getItem(for: SettingsManager.KeyboardColorKey, defaultValue: DefaultColors.Azure)

volumeIconColor = UserDefaultsManager.getItem(for: SettingsManager.VolumeIconColorKey, defaultValue: .white)
brightnessIconColor = UserDefaultsManager.getItem(for: SettingsManager.BrightnessColorKey, defaultValue: .white)
brightnessIconColor = UserDefaultsManager.getItem(for: SettingsManager.BrightnessIconColorKey, defaultValue: .white)
keyboardIconColor = UserDefaultsManager.getItem(for: SettingsManager.KeyboardIconColorKey, defaultValue: .white)

shouldShowShadows = UserDefaultsManager.getBool(for: SettingsManager.ShouldShowShadowsKey, defaultValue: true)
Expand Down
File renamed without changes.
File renamed without changes.
5 changes: 3 additions & 2 deletions SlimHUD/Views/Settings/Settings.storyboard
Original file line number Diff line number Diff line change
Expand Up @@ -679,7 +679,7 @@
<constraints>
<constraint firstAttribute="height" constant="21" id="Vaa-jG-PxP"/>
</constraints>
<imageCell key="cell" refusesFirstResponder="YES" alignment="left" animates="YES" imageScaling="proportionallyDown" image="brightness" id="b04-95-vyq"/>
<imageCell key="cell" refusesFirstResponder="YES" alignment="left" animates="YES" imageScaling="proportionallyDown" image="volume" id="b04-95-vyq"/>
</imageView>
</subviews>
<constraints>
Expand Down Expand Up @@ -912,11 +912,12 @@
</scene>
</scenes>
<resources>
<image name="NSRefreshTemplate" width="17" height="20"/>
<image name="NSRefreshTemplate" width="18" height="21"/>
<image name="backlight" width="225" height="120"/>
<image name="brightness" width="224" height="223"/>
<image name="brightnessSegmTemplate" width="116" height="115"/>
<image name="keyboardSegmTemplate" width="116" height="115"/>
<image name="volume" width="116" height="75"/>
<image name="volumeSegmTemplate" width="116" height="115"/>
</resources>
</document>
64 changes: 43 additions & 21 deletions SlimHUD/Views/Settings/SettingsView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,10 @@ class SettingsController: NSView, HudsControllerInterface {
keyboardBar.foreground = DefaultColors.Azure
setBackgroundColor(color: DefaultColors.DarkGray)
}


@available(OSX 10.14, *)
func setupDefaultIconsColors() {
settingsManager.volumeIconColor = .white
setVolumeIconsTint(.white)
setBrightnessIconsTint(.white)
setKeyboardIconsTint(.white)
Expand All @@ -68,12 +70,16 @@ class SettingsController: NSView, HudsControllerInterface {

func setVolumeEnabledColor(color: NSColor) {
volumeBar.foreground = color
volumeImage.image = NSImage(named: NSImage.VolumeImageFileName)
if #available(OSX 10.14, *) {
setVolumeIconsTint(settingsManager.volumeIconColor, enabled: true)
}
}

func setVolumeDisabledColor(color: NSColor) {
volumeBar.foreground = color
volumeImage.image = NSImage(named: NSImage.NoVolumeImageFileName)
if #available(OSX 10.14, *) {
setVolumeIconsTint(settingsManager.volumeIconColor, enabled: false)
}
}

func setBrightnessColor(color: NSColor) {
Expand Down Expand Up @@ -116,27 +122,40 @@ class SettingsController: NSView, HudsControllerInterface {
keyboardView.isHidden = !enabledBars.keyboardBar
}
}

func setVolumeIconsTint(_ color: NSColor) {
if #available(OSX 10.14, *) {
volumeImage.contentTintColor = color

// FIXME: changing icons tint in preview only works if instantiation AND tinting are called twice
@available(OSX 10.14, *)
func setVolumeIconsTint(_ color: NSColor, enabled: Bool) {
if(enabled) {
volumeImage.image = NSImage(named: NSImage.VolumeImageFileName)
} else {
NSLog("Can't change icons' tint - MacOS 10.14+ needed")
volumeImage.image = NSImage(named: NSImage.NoVolumeImageFileName)
}
}
func setBrightnessIconsTint(_ color: NSColor) {
if #available(OSX 10.14, *) {
brightnessImage.contentTintColor = color
volumeImage.image = volumeImage.image?.tint(with: color)
if(enabled) {
volumeImage.image = NSImage(named: NSImage.VolumeImageFileName)
} else {
NSLog("Can't change icons' tint - MacOS 10.14+ needed")
volumeImage.image = NSImage(named: NSImage.NoVolumeImageFileName)
}
volumeImage.image = volumeImage.image?.tint(with: color)
}
@available(OSX 10.14, *)
func setVolumeIconsTint(_ color: NSColor) {
setVolumeIconsTint(settingsManager.volumeIconColor, enabled: true)
}
@available(OSX 10.14, *)
func setBrightnessIconsTint(_ color: NSColor) {
brightnessImage.image = NSImage(named: NSImage.BrightnessImageFileName)
brightnessImage.image = brightnessImage.image?.tint(with: color)
brightnessImage.image = NSImage(named: NSImage.BrightnessImageFileName)
brightnessImage.image = brightnessImage.image?.tint(with: color)
}
@available(OSX 10.14, *)
func setKeyboardIconsTint(_ color: NSColor) {
if #available(OSX 10.14, *) {
keyboardImage.contentTintColor = color
} else {
NSLog("Can't change icons' tint - MacOS 10.14+ needed")
}
keyboardImage.image = NSImage(named: NSImage.KeyboardImageFileName)
keyboardImage.image = keyboardImage.image?.tint(with: color)
keyboardImage.image = NSImage(named: NSImage.KeyboardImageFileName)
keyboardImage.image = keyboardImage.image?.tint(with: color)
}

func updateAll() {
Expand All @@ -149,9 +168,12 @@ class SettingsController: NSView, HudsControllerInterface {
setBrightnessColor(color: settingsManager.brightnessColor)
setKeyboardColor(color: settingsManager.keyboardColor)
setShouldUseAnimation(shouldUseAnimation: settingsManager.shouldUseAnimation)
setVolumeIconsTint(settingsManager.volumeIconColor)
setBrightnessIconsTint(settingsManager.brightnessIconColor)
setKeyboardIconsTint(settingsManager.keyboardIconColor)

if #available(OSX 10.14, *) {
setVolumeIconsTint(settingsManager.volumeIconColor)
setBrightnessIconsTint(settingsManager.brightnessIconColor)
setKeyboardIconsTint(settingsManager.keyboardIconColor)
}
}

var value: Float = 0.5
Expand Down

0 comments on commit a187586

Please sign in to comment.