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

Merge pull request #58 from ReDetection/bordered-parameter

WIP: Bordered parameter, background parameter
This commit is contained in:
Anton Palgunov 2018-04-30 19:20:07 +01:00 committed by GitHub
commit 7646c6e89d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 65 additions and 30 deletions

View File

@ -14,6 +14,7 @@ class AppleScriptTouchBarItem: CustomButtonTouchBarItem {
return
}
self.script = script
button.bezelColor = .clear
DispatchQueue.main.async {
var error: NSDictionary?
guard script.compileAndReturnError(&error) else {

View File

@ -21,14 +21,11 @@ class CustomButtonTouchBarItem: NSCustomTouchBarItem, NSGestureRecognizerDelegat
self.longTapClosure = callbackLong
super.init(identifier: identifier)
button = NSButton(title: title, target: self, action: nil)
button.cell = CustomButtonCell(backgroundColor: bezelColor!)
button.cell?.title = title
button.title = title
button = NSButton(title: title, target: nil, action: nil)
button.cell = CustomButtonCell()
button.isBordered = true
button.bezelStyle = .rounded
button.bezelColor = bezelColor
button.title = title
self.view = button
longClick = NSPressGestureRecognizer(target: self, action: #selector(handleGestureLong))
@ -88,42 +85,37 @@ class CustomButtonTouchBarItem: NSCustomTouchBarItem, NSGestureRecognizerDelegat
}
class CustomButtonCell: NSButtonCell {
init(backgroundColor: NSColor) {
init() {
super.init(textCell: "")
if backgroundColor != .clear {
self.isBordered = true
self.backgroundColor = backgroundColor
} else {
self.isBordered = false
}
}
override func highlight(_ flag: Bool, withFrame cellFrame: NSRect, in controlView: NSView) {
if flag {
self.isBordered = true
} else {
self.isBordered = false
}
super.highlight(flag, withFrame: cellFrame, in: controlView)
if !self.isBordered {
self.setTitle(self.title, withColor: flag ? .lightGray : .white)
}
}
required init(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
extension NSButton {
var title: String {
override var title: String! {
get {
return ""// (self.cell?.title)!
return self.attributedTitle.string
}
set (newTitle) {
let attrTitle = NSMutableAttributedString(string: newTitle as String, attributes: [NSAttributedStringKey.foregroundColor: NSColor.white, NSAttributedStringKey.font: NSFont.systemFont(ofSize: 15, weight: .regular), NSAttributedStringKey.baselineOffset: 1])
attrTitle.setAlignment(.center, range: NSRange(location: 0, length: newTitle.count))
self.attributedTitle = attrTitle
setTitle(newTitle, withColor: .white)
}
}
func setTitle(_ title: String, withColor color: NSColor) {
let attrTitle = NSMutableAttributedString(string: title as String, attributes: [.foregroundColor: color, .font: NSFont.systemFont(ofSize: 15, weight: .regular), .baselineOffset: 1])
attrTitle.setAlignment(.center, range: NSRange(location: 0, length: title.count))
self.attributedTitle = attrTitle
}
}

View File

@ -388,6 +388,8 @@ enum GeneralParameter {
case width(_: CGFloat)
case image(source: SourceProtocol)
case align(_: Align)
case bordered(_: Bool)
case background(_:NSColor)
}
struct GeneralParameters: Decodable {
@ -397,6 +399,8 @@ struct GeneralParameters: Decodable {
case width
case image
case align
case bordered
case background
}
init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
@ -409,6 +413,12 @@ struct GeneralParameters: Decodable {
}
let align = try container.decodeIfPresent(Align.self, forKey: .align) ?? .center
result[.align] = .align(align)
if let borderedFlag = try container.decodeIfPresent(Bool.self, forKey: .bordered) {
result[.bordered] = .bordered(borderedFlag)
}
if let backgroundColor = try container.decodeIfPresent(String.self, forKey: .background)?.hexColor {
result[.background] = .background(backgroundColor)
}
parameters = result
}
}

View File

@ -17,6 +17,24 @@ extension String {
// ((\s|,)\/\*[\s\S]*?\*\/)|(( |, ")\/\/.*)
return self.replacingOccurrences(of: "((\\s|,)\\/\\*[\\s\\S]*?\\*\\/)|(( |, \\\")\\/\\/.*)", with: "", options: .regularExpression)
}
var hexColor: NSColor? {
let hex = trimmingCharacters(in: CharacterSet.alphanumerics.inverted)
var int = UInt32()
Scanner(string: hex).scanHexInt32(&int)
let a, r, g, b: UInt32
switch hex.count {
case 3: // RGB (12-bit)
(r, g, b, a) = ((int >> 8) * 17, (int >> 4 & 0xF) * 17, (int & 0xF) * 17, 255)
case 6: // RGB (24-bit)
(r, g, b, a) = (int >> 16, int >> 8 & 0xFF, int & 0xFF, 255)
case 8: // ARGB (32-bit)
(r, g, b, a) = (int >> 24, int >> 16 & 0xFF, int >> 8 & 0xFF, int & 0xFF)
default:
return nil
}
return NSColor(red: CGFloat(r) / 255, green: CGFloat(g) / 255, blue: CGFloat(b) / 255, alpha: CGFloat(a) / 255)
}
}
extension NSImage {

View File

@ -211,6 +211,20 @@ class TouchBarController: NSObject, NSTouchBarDelegate {
if case .width(let value)? = item.additionalParameters[.width], let widthBarItem = barItem as? CanSetWidth {
widthBarItem.setWidth(value: value)
}
if case .bordered(let bordered)? = item.additionalParameters[.bordered], let item = barItem as? CustomButtonTouchBarItem {
item.button.isBordered = bordered
item.button.bezelStyle = bordered ? .rounded : .inline
}
if case .background(let color)? = item.additionalParameters[.background], let item = barItem as? CustomButtonTouchBarItem {
if item.button.cell?.isBordered == false {
let newCell = NSButtonCell()
newCell.title = item.button.title
item.button.cell = newCell
item.button.cell?.isBordered = true
}
item.button.bezelColor = color
(item.button.cell as? NSButtonCell)?.backgroundColor = color
}
if case .image(let source)? = item.additionalParameters[.image], let item = barItem as? CustomButtonTouchBarItem {
let button = item.button!
button.imageScaling = .scaleProportionallyDown
@ -221,8 +235,7 @@ class TouchBarController: NSObject, NSTouchBarDelegate {
}
button.imageHugsTitle = true
button.cell?.image = source.image
button.bezelColor = .clear
button.image = source.image
}
return barItem
}

View File

@ -10,6 +10,7 @@ class TimeTouchBarItem: CustomButtonTouchBarItem {
super.init(identifier: identifier, title: " ", onTap: onTap, onLongTap: onLongTap)
timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(updateTime), userInfo: nil, repeats: true)
self.view = button
button.bezelColor = .clear
updateTime()
}