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

added key full for currency widget.

This commit is contained in:
Toxblh 2019-01-21 19:59:10 +03:00
parent 560d8ed508
commit 1d1d666986
8 changed files with 57 additions and 38 deletions

View File

@ -340,7 +340,7 @@
isa = PBXProject; isa = PBXProject;
attributes = { attributes = {
LastSwiftUpdateCheck = 0920; LastSwiftUpdateCheck = 0920;
LastUpgradeCheck = 1000; LastUpgradeCheck = 1010;
ORGANIZATIONNAME = "Anton Palgunov"; ORGANIZATIONNAME = "Anton Palgunov";
TargetAttributes = { TargetAttributes = {
B082B24E205C7D8000BC04DC = { B082B24E205C7D8000BC04DC = {
@ -626,7 +626,7 @@
isa = XCBuildConfiguration; isa = XCBuildConfiguration;
buildSettings = { buildSettings = {
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
CODE_SIGN_IDENTITY = "Developer ID Application"; CODE_SIGN_IDENTITY = "Mac Developer";
CODE_SIGN_STYLE = Manual; CODE_SIGN_STYLE = Manual;
COMBINE_HIDPI_IMAGES = YES; COMBINE_HIDPI_IMAGES = YES;
DEVELOPMENT_TEAM = D6D8BR2QNB; DEVELOPMENT_TEAM = D6D8BR2QNB;

View File

@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<Scheme <Scheme
LastUpgradeVersion = "1000" LastUpgradeVersion = "1010"
version = "1.3"> version = "1.3">
<BuildAction <BuildAction
parallelizeBuildables = "YES" parallelizeBuildables = "YES"

View File

@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<Scheme <Scheme
LastUpgradeVersion = "1000" LastUpgradeVersion = "1010"
version = "1.3"> version = "1.3">
<BuildAction <BuildAction
parallelizeBuildables = "YES" parallelizeBuildables = "YES"

View File

@ -17,9 +17,9 @@
<key>CFBundlePackageType</key> <key>CFBundlePackageType</key>
<string>APPL</string> <string>APPL</string>
<key>CFBundleShortVersionString</key> <key>CFBundleShortVersionString</key>
<string>0.19</string> <string>0.19.1</string>
<key>CFBundleVersion</key> <key>CFBundleVersion</key>
<string>112</string> <string>125</string>
<key>LSApplicationCategoryType</key> <key>LSApplicationCategoryType</key>
<string>public.app-category.utilities</string> <string>public.app-category.utilities</string>
<key>LSMinimumSystemVersion</key> <key>LSMinimumSystemVersion</key>

View File

@ -181,15 +181,16 @@ class SupportedTypesHolder {
}, },
"currency": { decoder in "currency": { decoder in
enum CodingKeys: String, CodingKey { case refreshInterval; case from; case to } enum CodingKeys: String, CodingKey { case refreshInterval; case from; case to; case full }
let container = try decoder.container(keyedBy: CodingKeys.self) let container = try decoder.container(keyedBy: CodingKeys.self)
let interval = try container.decodeIfPresent(Double.self, forKey: .refreshInterval) let interval = try container.decodeIfPresent(Double.self, forKey: .refreshInterval)
let from = try container.decodeIfPresent(String.self, forKey: .from) let from = try container.decodeIfPresent(String.self, forKey: .from)
let to = try container.decodeIfPresent(String.self, forKey: .to) let to = try container.decodeIfPresent(String.self, forKey: .to)
let full = try container.decodeIfPresent(Bool.self, forKey: .full)
let action = try ActionType(from: decoder) let action = try ActionType(from: decoder)
let longAction = try LongActionType(from: decoder) let longAction = try LongActionType(from: decoder)
return ( return (
item: .currency(interval: interval ?? 600.00, from: from ?? "RUB", to: to ?? "USD"), item: .currency(interval: interval ?? 600.00, from: from ?? "RUB", to: to ?? "USD", full: full ?? false),
action, action,
longAction, longAction,
parameters: [:] parameters: [:]
@ -330,7 +331,7 @@ enum ItemType: Decodable {
case volume() case volume()
case brightness(refreshInterval: Double) case brightness(refreshInterval: Double)
case weather(interval: Double, units: String, api_key: String, icon_type: String) case weather(interval: Double, units: String, api_key: String, icon_type: String)
case currency(interval: Double, from: String, to: String) case currency(interval: Double, from: String, to: String, full: Bool)
case inputsource() case inputsource()
case music(interval: Double) case music(interval: Double)
case groupBar(items: [BarItemDefinition]) case groupBar(items: [BarItemDefinition])
@ -345,6 +346,7 @@ enum ItemType: Decodable {
case refreshInterval case refreshInterval
case from case from
case to case to
case full
case units case units
case api_key case api_key
case icon_type case icon_type
@ -416,7 +418,8 @@ enum ItemType: Decodable {
let interval = try container.decodeIfPresent(Double.self, forKey: .refreshInterval) ?? 600.0 let interval = try container.decodeIfPresent(Double.self, forKey: .refreshInterval) ?? 600.0
let from = try container.decodeIfPresent(String.self, forKey: .from) ?? "RUB" let from = try container.decodeIfPresent(String.self, forKey: .from) ?? "RUB"
let to = try container.decodeIfPresent(String.self, forKey: .to) ?? "USD" let to = try container.decodeIfPresent(String.self, forKey: .to) ?? "USD"
self = .currency(interval: interval, from: from, to: to) let full = try container.decodeIfPresent(Bool.self, forKey: .full) ?? false
self = .currency(interval: interval, from: from, to: to, full: full)
case .inputsource: case .inputsource:
self = .inputsource() self = .inputsource()

View File

@ -36,7 +36,7 @@ extension ItemType {
return "com.toxblh.mtmr.brightness" return "com.toxblh.mtmr.brightness"
case .weather(interval: _, units: _, api_key: _, icon_type: _): case .weather(interval: _, units: _, api_key: _, icon_type: _):
return "com.toxblh.mtmr.weather" return "com.toxblh.mtmr.weather"
case .currency(interval: _, from: _, to: _): case .currency(interval: _, from: _, to: _, full: _):
return "com.toxblh.mtmr.currency" return "com.toxblh.mtmr.currency"
case .inputsource(): case .inputsource():
return "com.toxblh.mtmr.inputsource." return "com.toxblh.mtmr.inputsource."
@ -84,7 +84,7 @@ class TouchBarController: NSObject, NSTouchBarDelegate {
UserDefaults.standard.set(newValue, forKey: "com.toxblh.mtmr.settings.showControlStrip") UserDefaults.standard.set(newValue, forKey: "com.toxblh.mtmr.settings.showControlStrip")
} }
} }
var blacklistAppIdentifiers: [String] = [] var blacklistAppIdentifiers: [String] = []
var frontmostApplicationIdentifier: String? { var frontmostApplicationIdentifier: String? {
get { get {
@ -92,11 +92,11 @@ class TouchBarController: NSObject, NSTouchBarDelegate {
return frontmostId return frontmostId
} }
} }
private override init() { private override init() {
super.init() super.init()
SupportedTypesHolder.sharedInstance.register(typename: "exitTouchbar", item: .staticButton(title: "exit"), action: .custom(closure: { [weak self] in self?.dismissTouchBar()}), longAction: .none) SupportedTypesHolder.sharedInstance.register(typename: "exitTouchbar", item: .staticButton(title: "exit"), action: .custom(closure: { [weak self] in self?.dismissTouchBar()}), longAction: .none)
SupportedTypesHolder.sharedInstance.register(typename: "close") { _ in SupportedTypesHolder.sharedInstance.register(typename: "close") { _ in
return (item: .staticButton(title: ""), action: .custom(closure: { [weak self] in return (item: .staticButton(title: ""), action: .custom(closure: { [weak self] in
guard let `self` = self else { return } guard let `self` = self else { return }
@ -107,14 +107,14 @@ class TouchBarController: NSObject, NSTouchBarDelegate {
if let blackListed = UserDefaults.standard.stringArray(forKey: "com.toxblh.mtmr.blackListedApps") { if let blackListed = UserDefaults.standard.stringArray(forKey: "com.toxblh.mtmr.blackListedApps") {
self.blacklistAppIdentifiers = blackListed self.blacklistAppIdentifiers = blackListed
} }
NSWorkspace.shared.notificationCenter.addObserver(self, selector: #selector(activeApplicationChanged), name: NSWorkspace.didLaunchApplicationNotification, object: nil) NSWorkspace.shared.notificationCenter.addObserver(self, selector: #selector(activeApplicationChanged), name: NSWorkspace.didLaunchApplicationNotification, object: nil)
NSWorkspace.shared.notificationCenter.addObserver(self, selector: #selector(activeApplicationChanged), name: NSWorkspace.didTerminateApplicationNotification, object: nil) NSWorkspace.shared.notificationCenter.addObserver(self, selector: #selector(activeApplicationChanged), name: NSWorkspace.didTerminateApplicationNotification, object: nil)
NSWorkspace.shared.notificationCenter.addObserver(self, selector: #selector(activeApplicationChanged), name: NSWorkspace.didActivateApplicationNotification, object: nil) NSWorkspace.shared.notificationCenter.addObserver(self, selector: #selector(activeApplicationChanged), name: NSWorkspace.didActivateApplicationNotification, object: nil)
reloadStandardConfig() reloadStandardConfig()
} }
func createAndUpdatePreset(newJsonItems: [BarItemDefinition]) { func createAndUpdatePreset(newJsonItems: [BarItemDefinition]) {
if let oldBar = self.touchBar { if let oldBar = self.touchBar {
minimizeSystemModal(oldBar) minimizeSystemModal(oldBar)
@ -126,28 +126,28 @@ class TouchBarController: NSObject, NSTouchBarDelegate {
self.leftIdentifiers = [] self.leftIdentifiers = []
self.centerItems = [] self.centerItems = []
self.rightIdentifiers = [] self.rightIdentifiers = []
loadItemDefinitions(jsonItems: self.jsonItems) loadItemDefinitions(jsonItems: self.jsonItems)
createItems() createItems()
centerItems = centerIdentifiers.compactMap({ (identifier) -> NSTouchBarItem? in centerItems = centerIdentifiers.compactMap({ (identifier) -> NSTouchBarItem? in
return items[identifier] return items[identifier]
}) })
self.centerScrollArea = NSTouchBarItem.Identifier("com.toxblh.mtmr.scrollArea.".appending(UUID().uuidString)) self.centerScrollArea = NSTouchBarItem.Identifier("com.toxblh.mtmr.scrollArea.".appending(UUID().uuidString))
self.scrollArea = ScrollViewItem(identifier: centerScrollArea, items: centerItems) self.scrollArea = ScrollViewItem(identifier: centerScrollArea, items: centerItems)
touchBar.delegate = self touchBar.delegate = self
touchBar.defaultItemIdentifiers = [] touchBar.defaultItemIdentifiers = []
touchBar.defaultItemIdentifiers = self.leftIdentifiers + [centerScrollArea] + self.rightIdentifiers touchBar.defaultItemIdentifiers = self.leftIdentifiers + [centerScrollArea] + self.rightIdentifiers
self.updateActiveApp() self.updateActiveApp()
} }
@objc func activeApplicationChanged(_ n: Notification) { @objc func activeApplicationChanged(_ n: Notification) {
updateActiveApp() updateActiveApp()
} }
func updateActiveApp() { func updateActiveApp() {
if self.blacklistAppIdentifiers.index(of: self.frontmostApplicationIdentifier!) != nil { if self.blacklistAppIdentifiers.index(of: self.frontmostApplicationIdentifier!) != nil {
dismissTouchBar() dismissTouchBar()
@ -155,7 +155,7 @@ class TouchBarController: NSObject, NSTouchBarDelegate {
presentTouchBar() presentTouchBar()
} }
} }
func reloadStandardConfig() { func reloadStandardConfig() {
let presetPath = standardConfigPath let presetPath = standardConfigPath
if !FileManager.default.fileExists(atPath: presetPath), if !FileManager.default.fileExists(atPath: presetPath),
@ -163,16 +163,16 @@ class TouchBarController: NSObject, NSTouchBarDelegate {
try? FileManager.default.createDirectory(atPath: appSupportDirectory, withIntermediateDirectories: true, attributes: nil) try? FileManager.default.createDirectory(atPath: appSupportDirectory, withIntermediateDirectories: true, attributes: nil)
try? FileManager.default.copyItem(atPath: defaultPreset, toPath: presetPath) try? FileManager.default.copyItem(atPath: defaultPreset, toPath: presetPath)
} }
reloadPreset(path: presetPath) reloadPreset(path: presetPath)
} }
func reloadPreset(path: String) { func reloadPreset(path: String) {
lastPresetPath = path lastPresetPath = path
let items = path.fileData?.barItemDefinitions() ?? [BarItemDefinition(type: .staticButton(title: "bad preset"), action: .none, longAction: .none, additionalParameters: [:])] let items = path.fileData?.barItemDefinitions() ?? [BarItemDefinition(type: .staticButton(title: "bad preset"), action: .none, longAction: .none, additionalParameters: [:])]
createAndUpdatePreset(newJsonItems: items) createAndUpdatePreset(newJsonItems: items)
} }
func loadItemDefinitions(jsonItems: [BarItemDefinition]) { func loadItemDefinitions(jsonItems: [BarItemDefinition]) {
let dateFormatter = DateFormatter() let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "HH-mm-ss" dateFormatter.dateFormat = "HH-mm-ss"
@ -192,13 +192,13 @@ class TouchBarController: NSObject, NSTouchBarDelegate {
} }
} }
} }
func createItems() { func createItems() {
for (identifier, definition) in self.itemDefinitions { for (identifier, definition) in self.itemDefinitions {
self.items[identifier] = self.createItem(forIdentifier: identifier, definition: definition) self.items[identifier] = self.createItem(forIdentifier: identifier, definition: definition)
} }
} }
@objc func setupControlStripPresence() { @objc func setupControlStripPresence() {
DFRSystemModalShowsCloseBoxWhenFrontMost(false) DFRSystemModalShowsCloseBoxWhenFrontMost(false)
let item = NSCustomTouchBarItem(identifier: .controlStripItem) let item = NSCustomTouchBarItem(identifier: .controlStripItem)
@ -224,7 +224,7 @@ class TouchBarController: NSObject, NSTouchBarDelegate {
minimizeSystemModal(touchBar) minimizeSystemModal(touchBar)
updateControlStripPresence() updateControlStripPresence()
} }
@objc func resetControlStrip() { @objc func resetControlStrip() {
dismissTouchBar() dismissTouchBar()
presentTouchBar() presentTouchBar()
@ -242,7 +242,7 @@ class TouchBarController: NSObject, NSTouchBarDelegate {
} }
return item return item
} }
func createItem(forIdentifier identifier: NSTouchBarItem.Identifier, definition item: BarItemDefinition) -> NSTouchBarItem? { func createItem(forIdentifier identifier: NSTouchBarItem.Identifier, definition item: BarItemDefinition) -> NSTouchBarItem? {
var barItem: NSTouchBarItem! var barItem: NSTouchBarItem!
@ -271,8 +271,8 @@ class TouchBarController: NSObject, NSTouchBarDelegate {
} }
case .weather(interval: let interval, units: let units, api_key: let api_key, icon_type: let icon_type): case .weather(interval: let interval, units: let units, api_key: let api_key, icon_type: let icon_type):
barItem = WeatherBarItem(identifier: identifier, interval: interval, units: units, api_key: api_key, icon_type: icon_type) barItem = WeatherBarItem(identifier: identifier, interval: interval, units: units, api_key: api_key, icon_type: icon_type)
case .currency(interval: let interval, from: let from, to: let to): case .currency(interval: let interval, from: let from, to: let to, full: let full):
barItem = CurrencyBarItem(identifier: identifier, interval: interval, from: from, to: to) barItem = CurrencyBarItem(identifier: identifier, interval: interval, from: from, to: to, full: full)
case .inputsource(): case .inputsource():
barItem = InputSourceBarItem(identifier: identifier) barItem = InputSourceBarItem(identifier: identifier)
case .music(interval: let interval): case .music(interval: let interval):
@ -286,7 +286,7 @@ class TouchBarController: NSObject, NSTouchBarDelegate {
case .pomodoro(workTime: let workTime, restTime: let restTime): case .pomodoro(workTime: let workTime, restTime: let restTime):
barItem = PomodoroBarItem(identifier: identifier, workTime: workTime, restTime: restTime) barItem = PomodoroBarItem(identifier: identifier, workTime: workTime, restTime: restTime)
} }
if let action = self.action(forItem: item), let item = barItem as? CustomButtonTouchBarItem { if let action = self.action(forItem: item), let item = barItem as? CustomButtonTouchBarItem {
item.tapClosure = action item.tapClosure = action
} }
@ -359,7 +359,7 @@ class TouchBarController: NSObject, NSTouchBarDelegate {
} }
} }
func longAction(forItem item: BarItemDefinition) -> (()->())? { func longAction(forItem item: BarItemDefinition) -> (()->())? {
switch item.longAction { switch item.longAction {
case .hidKey(keycode: let keycode): case .hidKey(keycode: let keycode):

View File

@ -12,9 +12,11 @@ import CoreLocation
class CurrencyBarItem: CustomButtonTouchBarItem { class CurrencyBarItem: CustomButtonTouchBarItem {
private let activity: NSBackgroundActivityScheduler private let activity: NSBackgroundActivityScheduler
private var prefix: String private var prefix: String
private var postfix: String
private var from: String private var from: String
private var to: String private var to: String
private var oldValue: Float32! private var oldValue: Float32!
private var full: Bool = false
private let currencies = [ private let currencies = [
"USD": "$", "USD": "$",
@ -36,11 +38,12 @@ class CurrencyBarItem: CustomButtonTouchBarItem {
"ETH": "Ξ", "ETH": "Ξ",
] ]
init(identifier: NSTouchBarItem.Identifier, interval: TimeInterval, from: String, to: String) { init(identifier: NSTouchBarItem.Identifier, interval: TimeInterval, from: String, to: String, full: Bool) {
activity = NSBackgroundActivityScheduler(identifier: "\(identifier.rawValue).updatecheck") activity = NSBackgroundActivityScheduler(identifier: "\(identifier.rawValue).updatecheck")
activity.interval = interval activity.interval = interval
self.from = from self.from = from
self.to = to self.to = to
self.full = full
if let prefix = currencies[from] { if let prefix = currencies[from] {
self.prefix = prefix self.prefix = prefix
@ -48,6 +51,12 @@ class CurrencyBarItem: CustomButtonTouchBarItem {
prefix = from prefix = from
} }
if let postfix = currencies[to] {
self.postfix = postfix
} else {
postfix = to
}
super.init(identifier: identifier, title: "") super.init(identifier: identifier, title: "")
activity.repeats = true activity.repeats = true
@ -103,9 +112,15 @@ class CurrencyBarItem: CustomButtonTouchBarItem {
color = NSColor.red color = NSColor.red
} }
} }
oldValue = value oldValue = value
let title = String(format: "%@%.2f", prefix, value) var title = ""
if full {
title = String(format: "%@‣%.2f%@", prefix, value, postfix)
} else {
title = String(format: "%@%.2f", prefix, 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)
let newTitle = NSMutableAttributedString(string: title as String, attributes: [.foregroundColor: color, .font: regularFont]) let newTitle = NSMutableAttributedString(string: title as String, attributes: [.foregroundColor: color, .font: regularFont])

View File

@ -137,6 +137,7 @@ To close a group, use the button:
"align": "right", "align": "right",
"from": "BTC", "from": "BTC",
"to": "USD", "to": "USD",
"full": true // £‣1.29$
``` ```
#### `music` #### `music`