mirror of
https://github.com/Toxblh/MTMR.git
synced 2026-01-11 17:38:38 +00:00
Added 2 Support function (trim and resize). Image and title close forever :) resize input image to good size
This commit is contained in:
parent
79eba1a281
commit
6c331619d0
@ -13,6 +13,7 @@
|
|||||||
36C2ECDD207C723B003CDA33 /* ParseConfigTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 36C2ECDC207C723B003CDA33 /* ParseConfigTests.swift */; };
|
36C2ECDD207C723B003CDA33 /* ParseConfigTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 36C2ECDC207C723B003CDA33 /* ParseConfigTests.swift */; };
|
||||||
36C2ECDE207C82DE003CDA33 /* ItemsParsing.swift in Sources */ = {isa = PBXBuildFile; fileRef = 36C2ECDA207C3FE7003CDA33 /* ItemsParsing.swift */; };
|
36C2ECDE207C82DE003CDA33 /* ItemsParsing.swift in Sources */ = {isa = PBXBuildFile; fileRef = 36C2ECDA207C3FE7003CDA33 /* ItemsParsing.swift */; };
|
||||||
36C2ECE0207CB1B0003CDA33 /* defaultPreset.json in Resources */ = {isa = PBXBuildFile; fileRef = 36C2ECDF207CB1B0003CDA33 /* defaultPreset.json */; };
|
36C2ECE0207CB1B0003CDA33 /* defaultPreset.json in Resources */ = {isa = PBXBuildFile; fileRef = 36C2ECDF207CB1B0003CDA33 /* defaultPreset.json */; };
|
||||||
|
B0008E552080286C003AD4DD /* SupportHelpers.swift in Sources */ = {isa = PBXBuildFile; fileRef = B0008E542080286C003AD4DD /* SupportHelpers.swift */; };
|
||||||
B059D622205E03F5006E6B86 /* TouchBarController.swift in Sources */ = {isa = PBXBuildFile; fileRef = B059D621205E03F5006E6B86 /* TouchBarController.swift */; };
|
B059D622205E03F5006E6B86 /* TouchBarController.swift in Sources */ = {isa = PBXBuildFile; fileRef = B059D621205E03F5006E6B86 /* TouchBarController.swift */; };
|
||||||
B059D624205E04F3006E6B86 /* CustomButtonTouchBarItem.swift in Sources */ = {isa = PBXBuildFile; fileRef = B059D623205E04F3006E6B86 /* CustomButtonTouchBarItem.swift */; };
|
B059D624205E04F3006E6B86 /* CustomButtonTouchBarItem.swift in Sources */ = {isa = PBXBuildFile; fileRef = B059D623205E04F3006E6B86 /* CustomButtonTouchBarItem.swift */; };
|
||||||
B059D62D205F11E8006E6B86 /* DFRFoundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = B059D62C205F11E8006E6B86 /* DFRFoundation.framework */; };
|
B059D62D205F11E8006E6B86 /* DFRFoundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = B059D62C205F11E8006E6B86 /* DFRFoundation.framework */; };
|
||||||
@ -53,6 +54,7 @@
|
|||||||
36C2ECDA207C3FE7003CDA33 /* ItemsParsing.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ItemsParsing.swift; sourceTree = "<group>"; };
|
36C2ECDA207C3FE7003CDA33 /* ItemsParsing.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ItemsParsing.swift; sourceTree = "<group>"; };
|
||||||
36C2ECDC207C723B003CDA33 /* ParseConfigTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ParseConfigTests.swift; sourceTree = "<group>"; };
|
36C2ECDC207C723B003CDA33 /* ParseConfigTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ParseConfigTests.swift; sourceTree = "<group>"; };
|
||||||
36C2ECDF207CB1B0003CDA33 /* defaultPreset.json */ = {isa = PBXFileReference; lastKnownFileType = text.json; path = defaultPreset.json; sourceTree = "<group>"; };
|
36C2ECDF207CB1B0003CDA33 /* defaultPreset.json */ = {isa = PBXFileReference; lastKnownFileType = text.json; path = defaultPreset.json; sourceTree = "<group>"; };
|
||||||
|
B0008E542080286C003AD4DD /* SupportHelpers.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SupportHelpers.swift; sourceTree = "<group>"; };
|
||||||
B059D621205E03F5006E6B86 /* TouchBarController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TouchBarController.swift; sourceTree = "<group>"; };
|
B059D621205E03F5006E6B86 /* TouchBarController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TouchBarController.swift; sourceTree = "<group>"; };
|
||||||
B059D623205E04F3006E6B86 /* CustomButtonTouchBarItem.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CustomButtonTouchBarItem.swift; sourceTree = "<group>"; };
|
B059D623205E04F3006E6B86 /* CustomButtonTouchBarItem.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CustomButtonTouchBarItem.swift; sourceTree = "<group>"; };
|
||||||
B059D629205E13E5006E6B86 /* TouchBarPrivateApi.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = TouchBarPrivateApi.h; sourceTree = "<group>"; };
|
B059D629205E13E5006E6B86 /* TouchBarPrivateApi.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = TouchBarPrivateApi.h; sourceTree = "<group>"; };
|
||||||
@ -146,6 +148,7 @@
|
|||||||
36C2ECD6207B6DAE003CDA33 /* TimeTouchBarItem.swift */,
|
36C2ECD6207B6DAE003CDA33 /* TimeTouchBarItem.swift */,
|
||||||
B059D621205E03F5006E6B86 /* TouchBarController.swift */,
|
B059D621205E03F5006E6B86 /* TouchBarController.swift */,
|
||||||
36C2ECDA207C3FE7003CDA33 /* ItemsParsing.swift */,
|
36C2ECDA207C3FE7003CDA33 /* ItemsParsing.swift */,
|
||||||
|
B0008E542080286C003AD4DD /* SupportHelpers.swift */,
|
||||||
B082B258205C7D8000BC04DC /* Main.storyboard */,
|
B082B258205C7D8000BC04DC /* Main.storyboard */,
|
||||||
B082B25B205C7D8000BC04DC /* Info.plist */,
|
B082B25B205C7D8000BC04DC /* Info.plist */,
|
||||||
B082B25C205C7D8000BC04DC /* MTMR.entitlements */,
|
B082B25C205C7D8000BC04DC /* MTMR.entitlements */,
|
||||||
@ -311,6 +314,7 @@
|
|||||||
B059D622205E03F5006E6B86 /* TouchBarController.swift in Sources */,
|
B059D622205E03F5006E6B86 /* TouchBarController.swift in Sources */,
|
||||||
36C2ECD9207B74B4003CDA33 /* AppleScriptTouchBarItem.swift in Sources */,
|
36C2ECD9207B74B4003CDA33 /* AppleScriptTouchBarItem.swift in Sources */,
|
||||||
B0F8771A207AC1EA00D6E430 /* TouchBarSupport.m in Sources */,
|
B0F8771A207AC1EA00D6E430 /* TouchBarSupport.m in Sources */,
|
||||||
|
B0008E552080286C003AD4DD /* SupportHelpers.swift in Sources */,
|
||||||
B082B253205C7D8000BC04DC /* AppDelegate.swift in Sources */,
|
B082B253205C7D8000BC04DC /* AppDelegate.swift in Sources */,
|
||||||
B059D624205E04F3006E6B86 /* CustomButtonTouchBarItem.swift in Sources */,
|
B059D624205E04F3006E6B86 /* CustomButtonTouchBarItem.swift in Sources */,
|
||||||
B09EB1E4207C082000D5C1E0 /* HapticFeedback.swift in Sources */,
|
B09EB1E4207C082000D5C1E0 /* HapticFeedback.swift in Sources */,
|
||||||
|
|||||||
@ -36,7 +36,7 @@ class AppleScriptTouchBarItem: CustomButtonTouchBarItem {
|
|||||||
print("refresh happened")
|
print("refresh happened")
|
||||||
let scriptResult = self.execute()
|
let scriptResult = self.execute()
|
||||||
DispatchQueue.main.async {
|
DispatchQueue.main.async {
|
||||||
self.button.title = scriptResult
|
self.button.title = scriptResult.trim()
|
||||||
self.forceHideConstraint.isActive = scriptResult == ""
|
self.forceHideConstraint.isActive = scriptResult == ""
|
||||||
}
|
}
|
||||||
DispatchQueue.main.asyncAfter(deadline: .now() + self.interval) { [weak self] in
|
DispatchQueue.main.asyncAfter(deadline: .now() + self.interval) { [weak self] in
|
||||||
|
|||||||
@ -134,7 +134,7 @@ enum ItemType: Decodable {
|
|||||||
let interval = try container.decodeIfPresent(Double.self, forKey: .refreshInterval) ?? 1800.0
|
let interval = try container.decodeIfPresent(Double.self, forKey: .refreshInterval) ?? 1800.0
|
||||||
self = .appleScriptTitledButton(source: source, refreshInterval: interval)
|
self = .appleScriptTitledButton(source: source, refreshInterval: interval)
|
||||||
case .staticButton:
|
case .staticButton:
|
||||||
let title = try container.decode(String.self, forKey: .title)
|
let title = try container.decode(String.self, forKey: .title).trim()
|
||||||
self = .staticButton(title: title)
|
self = .staticButton(title: title)
|
||||||
case .timeButton:
|
case .timeButton:
|
||||||
let template = try container.decodeIfPresent(String.self, forKey: .formatTemplate) ?? "HH:mm"
|
let template = try container.decodeIfPresent(String.self, forKey: .formatTemplate) ?? "HH:mm"
|
||||||
@ -311,6 +311,6 @@ extension Data {
|
|||||||
return String(data: self, encoding: .utf8)
|
return String(data: self, encoding: .utf8)
|
||||||
}
|
}
|
||||||
var image: NSImage? {
|
var image: NSImage? {
|
||||||
return NSImage(data: self)
|
return NSImage(data: self)?.resize(maxSize: NSSize(width: 24, height: 24))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
52
MTMR/SupportHelpers.swift
Normal file
52
MTMR/SupportHelpers.swift
Normal file
@ -0,0 +1,52 @@
|
|||||||
|
//
|
||||||
|
// SupportHelpers.swift
|
||||||
|
// MTMR
|
||||||
|
//
|
||||||
|
// Created by Anton Palgunov on 13/04/2018.
|
||||||
|
// Copyright © 2018 Anton Palgunov. All rights reserved.
|
||||||
|
//
|
||||||
|
|
||||||
|
import Foundation
|
||||||
|
|
||||||
|
extension String {
|
||||||
|
func trim() -> String {
|
||||||
|
return self.trimmingCharacters(in: NSCharacterSet.whitespaces)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
extension NSImage {
|
||||||
|
func resize(maxSize:NSSize) -> NSImage {
|
||||||
|
var ratio:Float = 0.0
|
||||||
|
let imageWidth = Float(self.size.width)
|
||||||
|
let imageHeight = Float(self.size.height)
|
||||||
|
let maxWidth = Float(maxSize.width)
|
||||||
|
let maxHeight = Float(maxSize.height)
|
||||||
|
|
||||||
|
// Get ratio (landscape or portrait)
|
||||||
|
if (imageWidth > imageHeight) {
|
||||||
|
// Landscape
|
||||||
|
ratio = maxWidth / imageWidth;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
// Portrait
|
||||||
|
ratio = maxHeight / imageHeight;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Calculate new size based on the ratio
|
||||||
|
let newWidth = imageWidth * ratio
|
||||||
|
let newHeight = imageHeight * ratio
|
||||||
|
|
||||||
|
// Create a new NSSize object with the newly calculated size
|
||||||
|
let newSize:NSSize = NSSize(width: Int(newWidth), height: Int(newHeight))
|
||||||
|
|
||||||
|
// Cast the NSImage to a CGImage
|
||||||
|
var imageRect:NSRect = NSMakeRect(0, 0, self.size.width, self.size.height)
|
||||||
|
let imageRef = self.cgImage(forProposedRect: &imageRect, context: nil, hints: nil)
|
||||||
|
|
||||||
|
// Create NSImage from the CGImage using the new size
|
||||||
|
let imageWithNewSize = NSImage(cgImage: imageRef!, size: newSize)
|
||||||
|
|
||||||
|
// Return the new image
|
||||||
|
return imageWithNewSize
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -14,7 +14,7 @@ struct ExactItem {
|
|||||||
}
|
}
|
||||||
|
|
||||||
extension ItemType {
|
extension ItemType {
|
||||||
|
|
||||||
var identifierBase: String {
|
var identifierBase: String {
|
||||||
switch self {
|
switch self {
|
||||||
case .staticButton(title: _):
|
case .staticButton(title: _):
|
||||||
@ -27,7 +27,7 @@ extension ItemType {
|
|||||||
return "NSTouchBarItem.Identifier.flexibleSpace"
|
return "NSTouchBarItem.Identifier.flexibleSpace"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
extension NSTouchBarItem.Identifier {
|
extension NSTouchBarItem.Identifier {
|
||||||
@ -37,23 +37,23 @@ extension NSTouchBarItem.Identifier {
|
|||||||
class TouchBarController: NSObject, NSTouchBarDelegate {
|
class TouchBarController: NSObject, NSTouchBarDelegate {
|
||||||
|
|
||||||
static let shared = TouchBarController()
|
static let shared = TouchBarController()
|
||||||
|
|
||||||
let touchBar = NSTouchBar()
|
let touchBar = NSTouchBar()
|
||||||
|
|
||||||
var items: [NSTouchBarItem.Identifier: BarItemDefinition] = [:]
|
var items: [NSTouchBarItem.Identifier: BarItemDefinition] = [:]
|
||||||
|
|
||||||
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
|
SupportedTypesHolder.sharedInstance.register(typename: "exitTouchbar", item: .staticButton(title: "exit"), action: .custom(closure: { [weak self] in
|
||||||
self?.dismissTouchBar()
|
self?.dismissTouchBar()
|
||||||
}))
|
}))
|
||||||
|
|
||||||
loadItems()
|
loadItems()
|
||||||
|
|
||||||
touchBar.delegate = self
|
touchBar.delegate = self
|
||||||
self.presentTouchBar()
|
self.presentTouchBar()
|
||||||
}
|
}
|
||||||
|
|
||||||
func loadItems() {
|
func loadItems() {
|
||||||
let appSupportDirectory = NSSearchPathForDirectoriesInDomains(.applicationSupportDirectory, .userDomainMask, true).first!.appending("/MTMR")
|
let appSupportDirectory = NSSearchPathForDirectoriesInDomains(.applicationSupportDirectory, .userDomainMask, true).first!.appending("/MTMR")
|
||||||
let presetPath = appSupportDirectory.appending("/items.json")
|
let presetPath = appSupportDirectory.appending("/items.json")
|
||||||
@ -64,7 +64,7 @@ class TouchBarController: NSObject, NSTouchBarDelegate {
|
|||||||
}
|
}
|
||||||
let jsonData = presetPath.fileData
|
let jsonData = presetPath.fileData
|
||||||
let jsonItems = jsonData?.barItemDefinitions() ?? [BarItemDefinition(type: .staticButton(title: "bad preset"), action: .none, additionalParameters: [])]
|
let jsonItems = jsonData?.barItemDefinitions() ?? [BarItemDefinition(type: .staticButton(title: "bad preset"), action: .none, additionalParameters: [])]
|
||||||
|
|
||||||
for item in jsonItems {
|
for item in jsonItems {
|
||||||
let identifierString = item.type.identifierBase.appending(UUID().uuidString)
|
let identifierString = item.type.identifierBase.appending(UUID().uuidString)
|
||||||
let identifier = item.type == ItemType.flexSpace()
|
let identifier = item.type == ItemType.flexSpace()
|
||||||
@ -82,25 +82,25 @@ class TouchBarController: NSObject, NSTouchBarDelegate {
|
|||||||
NSTouchBarItem.addSystemTrayItem(item)
|
NSTouchBarItem.addSystemTrayItem(item)
|
||||||
DFRElementSetControlStripPresenceForIdentifier(.controlStripItem, true)
|
DFRElementSetControlStripPresenceForIdentifier(.controlStripItem, true)
|
||||||
}
|
}
|
||||||
|
|
||||||
func updateControlStripPresence() {
|
func updateControlStripPresence() {
|
||||||
DFRElementSetControlStripPresenceForIdentifier(.controlStripItem, true)
|
DFRElementSetControlStripPresenceForIdentifier(.controlStripItem, true)
|
||||||
}
|
}
|
||||||
|
|
||||||
@objc private func presentTouchBar() {
|
@objc private func presentTouchBar() {
|
||||||
NSTouchBar.presentSystemModalFunctionBar(touchBar, placement: 1, systemTrayItemIdentifier: .controlStripItem)
|
NSTouchBar.presentSystemModalFunctionBar(touchBar, placement: 1, systemTrayItemIdentifier: .controlStripItem)
|
||||||
}
|
}
|
||||||
|
|
||||||
@objc private func dismissTouchBar() {
|
@objc private func dismissTouchBar() {
|
||||||
NSTouchBar.minimizeSystemModalFunctionBar(touchBar)
|
NSTouchBar.minimizeSystemModalFunctionBar(touchBar)
|
||||||
}
|
}
|
||||||
|
|
||||||
func touchBar(_ touchBar: NSTouchBar, makeItemForIdentifier identifier: NSTouchBarItem.Identifier) -> NSTouchBarItem? {
|
func touchBar(_ touchBar: NSTouchBar, makeItemForIdentifier identifier: NSTouchBarItem.Identifier) -> NSTouchBarItem? {
|
||||||
guard let item = self.items[identifier] else {
|
guard let item = self.items[identifier] else {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
let action = self.action(forItem: item)
|
let action = self.action(forItem: item)
|
||||||
|
|
||||||
var barItem: NSTouchBarItem!
|
var barItem: NSTouchBarItem!
|
||||||
switch item.type {
|
switch item.type {
|
||||||
case .staticButton(title: let title):
|
case .staticButton(title: let title):
|
||||||
@ -120,14 +120,13 @@ class TouchBarController: NSObject, NSTouchBarDelegate {
|
|||||||
let button = item.button!
|
let button = item.button!
|
||||||
button.image = source.image
|
button.image = source.image
|
||||||
button.imagePosition = .imageLeading
|
button.imagePosition = .imageLeading
|
||||||
button.insets
|
button.imageHugsTitle = true
|
||||||
button.bezelColor = .clear
|
button.bezelColor = .clear
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return barItem
|
return barItem
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
func action(forItem item: BarItemDefinition) -> ()->() {
|
func action(forItem item: BarItemDefinition) -> ()->() {
|
||||||
switch item.action {
|
switch item.action {
|
||||||
case .hidKey(keycode: let keycode):
|
case .hidKey(keycode: let keycode):
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user