-
Notifications
You must be signed in to change notification settings - Fork 895
/
Copy pathStyle.swift
75 lines (60 loc) · 2.6 KB
/
Style.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
//
// Style.swift
// TGUIKit
//
// Created by keepcoder on 26/09/2016.
// Copyright © 2016 Telegram. All rights reserved.
//
import Cocoa
public struct ControlStyle: Equatable {
public var font: NSFont = .normal(.text)
public var foregroundColor: NSColor = .text
public var backgroundColor: NSColor = .clear
public var borderColor: NSColor = presentation.colors.border
public var grayTextColor: NSColor = presentation.colors.grayText
public var textColor: NSColor = presentation.colors.text
private var _highlightColor: NSColor?
public var highlightColor:NSColor {
return _highlightColor ?? presentation.colors.accent
}
public func highlight(image:CGImage) -> CGImage {
let context = DrawingContext(size:image.backingSize, scale: System.backingScale, clear:true)
context.withContext { ctx in
ctx.clear(NSMakeRect(0, 0, image.backingSize.width, image.backingSize.height))
let imageRect = NSMakeRect(0, 0, image.backingSize.width, image.backingSize.height)
ctx.setFillColor(backgroundColor.cgColor)
ctx.fill(imageRect)
ctx.clip(to: imageRect, mask: image)
ctx.setFillColor(highlightColor.cgColor)
ctx.fill(imageRect)
}
return context.generateImage() ?? image
}
public init(font:NSFont? = nil, foregroundColor:NSColor? = nil,backgroundColor:NSColor? = nil, highlightColor:NSColor? = nil, borderColor: NSColor? = nil, grayTextColor: NSColor? = nil, textColor: NSColor? = nil) {
if let font = font {
self.font = font
}
if let foregroundColor = foregroundColor {
self.foregroundColor = foregroundColor
}
if let backgroundColor = backgroundColor {
self.backgroundColor = backgroundColor
}
if let borderColor = borderColor {
self.borderColor = borderColor
}
if let textColor = textColor {
self.textColor = textColor
}
if let grayTextColor = grayTextColor {
self.grayTextColor = grayTextColor
}
_highlightColor = highlightColor
}
public func text(_ text:String, forState state:ControlState) -> NSAttributedString {
return NSAttributedString.initialize(string: text, color: state == .Normal ? foregroundColor : highlightColor, font: font)
}
}
public func ==(lhs:ControlStyle, rhs:ControlStyle) -> Bool {
return lhs.font == rhs.font && lhs.foregroundColor == rhs.foregroundColor && rhs.backgroundColor == lhs.backgroundColor
}