1
0
mirror of https://github.com/Toxblh/MTMR.git synced 2026-01-11 09:28:38 +00:00

Update CurrencyBarItem.swift

This commit is contained in:
Simon Rogers 2020-04-09 11:36:57 +01:00 committed by GitHub
parent 47561ce71b
commit e011160f0b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -15,7 +15,9 @@ class CurrencyBarItem: CustomButtonTouchBarItem {
private var postfix: String private var postfix: String
private var from: String private var from: String
private var to: String private var to: String
private var decimal as Int private var decimal: Int
private var decimalValue: Float32!
private var decimalString: String!
private var oldValue: Float32! private var oldValue: Float32!
private var full: Bool = false private var full: Bool = false
@ -28,36 +30,33 @@ class CurrencyBarItem: CustomButtonTouchBarItem {
"CAD": "$", "CAD": "$",
"KRW": "", "KRW": "",
"CNY": "¥", "CNY": "¥",
"AUD": "Aus", "AUD": "$",
"BRL": "R$", "BRL": "R$",
"IDR": "Rp", "IDR": "Rp",
"INR": "Rp", "MXN": "$",
"MXN": "Mx", "SGD": "$",
"SGD": "Sg",
"CHF": "Fr",
"BTC": "฿", "BTC": "฿",
"LTC": "Ł", "LTC": "Ł",
"ETH": "Ξ", "ETH": "Ξ",
] ]
private let decimals = [ private let decimals = [
"USD": "4", "USD": 4,
"EUR": "4", "EUR": 4,
"RUB": "2", "RUB": 2,
"JPY": "2", "JPY": 2,
"GBP": "4", "GBP": 4,
"CAD": "4", "CAD": 4,
"KRW": "4", "KRW": 4,
"CNY": "4", "CNY": 4,
"AUD": "4", "AUD": 4,
"BRL": "4", "BRL": 4,
"IDR": "0", "IDR": 1,
"INR": "2", "MXN": 2,
"MXN": "2", "SGD": 4,
"SGD": "4", "CHF": 4,
"CHF": "4", "BTC": 2,
"BTC": "2", "LTC": 2,
"LTC": "2", "ETH": 2,
"ETH": "2",
] ]
init(identifier: NSTouchBarItem.Identifier, interval: TimeInterval, from: String, to: String, full: Bool) { init(identifier: NSTouchBarItem.Identifier, interval: TimeInterval, from: String, to: String, full: Bool) {
@ -79,12 +78,14 @@ class CurrencyBarItem: CustomButtonTouchBarItem {
postfix = to postfix = to
} }
if let decimal = decimals[to] { if let decimal = decimals[to] {
self.decimal = decimal self.decimal = decimal
} else { } else {
decimal = "2" decimal = 2
} }
super.init(identifier: identifier, title: "") super.init(identifier: identifier, title: "")
activity.repeats = true activity.repeats = true
@ -142,12 +143,14 @@ class CurrencyBarItem: CustomButtonTouchBarItem {
} }
oldValue = value oldValue = value
decimalValue = (value * pow(10,Float(decimal))).rounded() / pow(10,Float(decimal))
decimalString = String(decimalValue)
var title = "" var title = ""
if full { if full {
title = String(format: "%@%@‣%.%@f%@", prefix, postfix, decimal, value) title = String(format: "%@%@‣%@", prefix, postfix, decimalString)
} else { } else {
title = String(format: "%@%.2f", prefix, value) title = String(format: "%.2f", value)
} }
let regularFont = attributedTitle.attribute(.font, at: 0, effectiveRange: nil) as? NSFont ?? NSFont.systemFont(ofSize: 15) let regularFont = attributedTitle.attribute(.font, at: 0, effectiveRange: nil) as? NSFont ?? NSFont.systemFont(ofSize: 15)