mirror of
https://github.com/Toxblh/MTMR.git
synced 2026-10-31 10:28:36 +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:
@@ -36,7 +36,7 @@ class AppleScriptTouchBarItem: CustomButtonTouchBarItem {
|
||||
print("refresh happened")
|
||||
let scriptResult = self.execute()
|
||||
DispatchQueue.main.async {
|
||||
self.button.title = scriptResult
|
||||
self.button.title = scriptResult.trim()
|
||||
self.forceHideConstraint.isActive = scriptResult == ""
|
||||
}
|
||||
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
|
||||
self = .appleScriptTitledButton(source: source, refreshInterval: interval)
|
||||
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)
|
||||
case .timeButton:
|
||||
let template = try container.decodeIfPresent(String.self, forKey: .formatTemplate) ?? "HH:mm"
|
||||
@@ -311,6 +311,6 @@ extension Data {
|
||||
return String(data: self, encoding: .utf8)
|
||||
}
|
||||
var image: NSImage? {
|
||||
return NSImage(data: self)
|
||||
return NSImage(data: self)?.resize(maxSize: NSSize(width: 24, height: 24))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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 {
|
||||
|
||||
|
||||
var identifierBase: String {
|
||||
switch self {
|
||||
case .staticButton(title: _):
|
||||
@@ -27,7 +27,7 @@ extension ItemType {
|
||||
return "NSTouchBarItem.Identifier.flexibleSpace"
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
extension NSTouchBarItem.Identifier {
|
||||
@@ -37,23 +37,23 @@ extension NSTouchBarItem.Identifier {
|
||||
class TouchBarController: NSObject, NSTouchBarDelegate {
|
||||
|
||||
static let shared = TouchBarController()
|
||||
|
||||
|
||||
let touchBar = NSTouchBar()
|
||||
|
||||
|
||||
var items: [NSTouchBarItem.Identifier: BarItemDefinition] = [:]
|
||||
|
||||
|
||||
private override init() {
|
||||
super.init()
|
||||
SupportedTypesHolder.sharedInstance.register(typename: "exitTouchbar", item: .staticButton(title: "exit"), action: .custom(closure: { [weak self] in
|
||||
self?.dismissTouchBar()
|
||||
}))
|
||||
|
||||
|
||||
loadItems()
|
||||
|
||||
|
||||
touchBar.delegate = self
|
||||
self.presentTouchBar()
|
||||
}
|
||||
|
||||
|
||||
func loadItems() {
|
||||
let appSupportDirectory = NSSearchPathForDirectoriesInDomains(.applicationSupportDirectory, .userDomainMask, true).first!.appending("/MTMR")
|
||||
let presetPath = appSupportDirectory.appending("/items.json")
|
||||
@@ -64,7 +64,7 @@ class TouchBarController: NSObject, NSTouchBarDelegate {
|
||||
}
|
||||
let jsonData = presetPath.fileData
|
||||
let jsonItems = jsonData?.barItemDefinitions() ?? [BarItemDefinition(type: .staticButton(title: "bad preset"), action: .none, additionalParameters: [])]
|
||||
|
||||
|
||||
for item in jsonItems {
|
||||
let identifierString = item.type.identifierBase.appending(UUID().uuidString)
|
||||
let identifier = item.type == ItemType.flexSpace()
|
||||
@@ -82,25 +82,25 @@ class TouchBarController: NSObject, NSTouchBarDelegate {
|
||||
NSTouchBarItem.addSystemTrayItem(item)
|
||||
DFRElementSetControlStripPresenceForIdentifier(.controlStripItem, true)
|
||||
}
|
||||
|
||||
|
||||
func updateControlStripPresence() {
|
||||
DFRElementSetControlStripPresenceForIdentifier(.controlStripItem, true)
|
||||
}
|
||||
|
||||
|
||||
@objc private func presentTouchBar() {
|
||||
NSTouchBar.presentSystemModalFunctionBar(touchBar, placement: 1, systemTrayItemIdentifier: .controlStripItem)
|
||||
}
|
||||
|
||||
|
||||
@objc private func dismissTouchBar() {
|
||||
NSTouchBar.minimizeSystemModalFunctionBar(touchBar)
|
||||
}
|
||||
|
||||
|
||||
func touchBar(_ touchBar: NSTouchBar, makeItemForIdentifier identifier: NSTouchBarItem.Identifier) -> NSTouchBarItem? {
|
||||
guard let item = self.items[identifier] else {
|
||||
return nil
|
||||
}
|
||||
let action = self.action(forItem: item)
|
||||
|
||||
|
||||
var barItem: NSTouchBarItem!
|
||||
switch item.type {
|
||||
case .staticButton(title: let title):
|
||||
@@ -120,14 +120,13 @@ class TouchBarController: NSObject, NSTouchBarDelegate {
|
||||
let button = item.button!
|
||||
button.image = source.image
|
||||
button.imagePosition = .imageLeading
|
||||
button.insets
|
||||
button.imageHugsTitle = true
|
||||
button.bezelColor = .clear
|
||||
}
|
||||
}
|
||||
return barItem
|
||||
}
|
||||
|
||||
|
||||
|
||||
func action(forItem item: BarItemDefinition) -> ()->() {
|
||||
switch item.action {
|
||||
case .hidKey(keycode: let keycode):
|
||||
|
||||
Reference in New Issue
Block a user