Skip to content

Commit

Permalink
Multiple SF Symbols with different colours #180
Browse files Browse the repository at this point in the history
  • Loading branch information
melonamin committed Apr 1, 2021
1 parent dd66266 commit 8fcceeb
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 4 deletions.
2 changes: 1 addition & 1 deletion SwiftBar/MenuBar/MenuBarItem.swift
Original file line number Diff line number Diff line change
Expand Up @@ -538,7 +538,7 @@ extension MenubarItem {
var attributedTitle = NSMutableAttributedString(string: title)

if params.symbolize, !params.ansi {
attributedTitle = title.symbolize(font: font, color: params.sfcolor)
attributedTitle = title.symbolize(font: font, colors: params.sfcolors)
}
if params.ansi {
attributedTitle = title.colorizedWithANSIColor()
Expand Down
12 changes: 12 additions & 0 deletions SwiftBar/MenuBar/MenuLineParameters.swift
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,18 @@ struct MenuLineParameters {
return NSColor.webColor(from: AppShared.isDarkTheme ? darkColor : lightColor)
}

var sfcolors: [NSColor] {
var out: [NSColor?] = []
out.append(sfcolor)
for i in 1 ... 10 {
guard let colors = params["sfcolor\(i)"]?.components(separatedBy: ",") else { continue }
let lightColor = colors.first
let darkColor = colors.last
out.append(NSColor.webColor(from: AppShared.isDarkTheme ? darkColor : lightColor))
}
return out.compactMap { $0 }
}

var font: String? {
params["font"]
}
Expand Down
11 changes: 8 additions & 3 deletions SwiftBar/Utility/String+SFSymbols.swift
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
import Cocoa

extension String {
func symbolize(font: NSFont, color: NSColor?) -> NSMutableAttributedString {
func symbolize(font: NSFont, colors: [NSColor]) -> NSMutableAttributedString {
if #available(OSX 11.0, *) {
var colors: [NSColor] = colors
let out = NSMutableAttributedString()
self.components(separatedBy: .whitespaces).forEach { word in
out.append(NSAttributedString(string: " "))
guard word.hasPrefix(":"), word.hasSuffix(":") else {
out.append(NSAttributedString(string: word))
return
}
if let image = NSImage(systemSymbolName: String(word.dropFirst().dropLast()), accessibilityDescription: nil)?.tintedImage(color: color) {
out.append(NSAttributedString(attachment: NSTextAttachment.centeredImage(with: image, and: font)))
if let image = NSImage(systemSymbolName: String(word.dropFirst().dropLast()), accessibilityDescription: nil) {
let tintColor = colors.first
if colors.count > 1 {
colors = Array(colors.dropFirst())
}
out.append(NSAttributedString(attachment: NSTextAttachment.centeredImage(with: image.tintedImage(color: tintColor), and: font)))
return
}
out.append(NSAttributedString(string: word))
Expand Down

0 comments on commit 8fcceeb

Please sign in to comment.