|
| 1 | +// The Swift Programming Language |
| 2 | +// https://docs.swift.org/swift-book |
| 3 | + |
| 4 | +// |
| 5 | +// LCWindowButton.swift |
| 6 | +// WindowButton |
| 7 | +// |
| 8 | +// Created by DevLiuSir on 2020/12/11. |
| 9 | +// |
| 10 | + |
| 11 | +import Foundation |
| 12 | +import Cocoa |
| 13 | + |
| 14 | + |
| 15 | +/// 窗口按钮 |
| 16 | +class LCWindowButton: NSControl { |
| 17 | + |
| 18 | + // MARK: - Properties |
| 19 | + |
| 20 | + /// 按钮的类型,用于表示窗口按钮的功能(关闭、最小化、全屏等)。 |
| 21 | + var buttonType: LCWindowButtonType = .close { |
| 22 | + didSet { |
| 23 | + hover = false // 当按钮类型变化时,取消鼠标悬停效果。 |
| 24 | + needsDisplay = true // 标记需要重新绘制视图。 |
| 25 | + } |
| 26 | + } |
| 27 | + |
| 28 | + /// 按钮的`激活状态`,用于指示按钮是否处于激活状态(例如,与当前窗口相关)。 |
| 29 | + var isActive: Bool = false { |
| 30 | + didSet { |
| 31 | + needsDisplay = true // 标记需要重新绘制视图。 |
| 32 | + } |
| 33 | + } |
| 34 | + |
| 35 | + /// 窗口是否处于`全屏状态`的标志。 |
| 36 | + private var isWindowFullScreen: Bool = false { |
| 37 | + didSet { |
| 38 | + needsDisplay = true // 标记需要重新绘制视图。 |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + /// 忽略自己的鼠标滑入, default = false |
| 43 | + var ignoreMouseHover: Bool = false { |
| 44 | + didSet { |
| 45 | + updateTrackingAreas() |
| 46 | + } |
| 47 | + } |
| 48 | + /// 是否选中状态 |
| 49 | + var hover: Bool = false { |
| 50 | + didSet { |
| 51 | + needsDisplay = true |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + // MARK: - Initializers |
| 56 | + |
| 57 | + /// 根据`指定的按钮类型`创建一个窗口按钮实例。 |
| 58 | + /// - Parameter buttonType: 按钮的类型,例如关闭、最小化、全屏等。 |
| 59 | + /// - Returns: 配置好的 `LCWindowButton` 实例。 |
| 60 | + static func button(withType buttonType: LCWindowButtonType) -> LCWindowButton { |
| 61 | + return LCWindowButton(type: buttonType) |
| 62 | + } |
| 63 | + |
| 64 | + /// 使用`指定的按钮类型`初始化`窗口按钮`。 |
| 65 | + /// - Parameter type: 按钮的类型,例如关闭、最小化、全屏等。 |
| 66 | + init(type: LCWindowButtonType) { |
| 67 | + self.buttonType = type // 设置按钮的类型。 |
| 68 | + super.init(frame: .zero) // 调用父类的初始化方法,设置默认的 frame。 |
| 69 | + } |
| 70 | + |
| 71 | + required init?(coder: NSCoder) { |
| 72 | + super.init(coder: coder) |
| 73 | + } |
| 74 | + |
| 75 | + // MARK: - Window State Listeners |
| 76 | + |
| 77 | + override func viewDidMoveToWindow() { |
| 78 | + super.viewDidMoveToWindow() |
| 79 | + NotificationCenter.default.addObserver(self, selector: #selector(windowDidBecomeActive), name: NSWindow.didBecomeKeyNotification, object: window) |
| 80 | + NotificationCenter.default.addObserver(self, selector: #selector(windowDidResignActive), name: NSWindow.didResignKeyNotification, object: window) |
| 81 | + NotificationCenter.default.addObserver(self, selector: #selector(windowDidFullScreen), name: NSWindow.didEnterFullScreenNotification, object: window) |
| 82 | + NotificationCenter.default.addObserver(self, selector: #selector(windowDidExitFullScreen), name: NSWindow.didExitFullScreenNotification, object: window) |
| 83 | + } |
| 84 | + |
| 85 | + deinit { |
| 86 | + NotificationCenter.default.removeObserver(self) |
| 87 | + } |
| 88 | + |
| 89 | + //MARK: - Handle notification |
| 90 | + |
| 91 | + @objc private func windowDidBecomeActive() { |
| 92 | + isActive = true |
| 93 | + } |
| 94 | + |
| 95 | + @objc private func windowDidResignActive() { |
| 96 | + isActive = false |
| 97 | + } |
| 98 | + |
| 99 | + @objc private func windowDidFullScreen() { |
| 100 | + isWindowFullScreen = true |
| 101 | + } |
| 102 | + |
| 103 | + @objc private func windowDidExitFullScreen() { |
| 104 | + isWindowFullScreen = false |
| 105 | + } |
| 106 | + |
| 107 | + // MARK: - Mouse Tracking |
| 108 | + |
| 109 | + |
| 110 | + /// 更新追踪区域以响应鼠标进入和退出事件 |
| 111 | + override func updateTrackingAreas() { |
| 112 | + super.updateTrackingAreas() |
| 113 | + // 移除所有现有的追踪区域 |
| 114 | + trackingAreas.forEach(removeTrackingArea) |
| 115 | + guard !ignoreMouseHover else { return } |
| 116 | + // 添加新的追踪区域 |
| 117 | + let trackingArea = NSTrackingArea( |
| 118 | + rect: bounds, |
| 119 | + options: [.activeAlways, .mouseEnteredAndExited], |
| 120 | + owner: self, |
| 121 | + userInfo: nil |
| 122 | + ) |
| 123 | + addTrackingArea(trackingArea) |
| 124 | + } |
| 125 | + |
| 126 | + /*** 光标进入跟踪区域 ***/ |
| 127 | + override func mouseEntered(with event: NSEvent) { |
| 128 | + hover = true // 更新记录值 |
| 129 | + } |
| 130 | + |
| 131 | + /*** 光标已退出跟踪区域 ***/ |
| 132 | + override func mouseExited(with event: NSEvent) { |
| 133 | + hover = false // 更新记录值 |
| 134 | + } |
| 135 | + |
| 136 | + // MARK: - Mouse Events |
| 137 | + |
| 138 | + /// 处理鼠标按下事件 |
| 139 | + override func mouseDown(with event: NSEvent) { |
| 140 | + if isEnabled { |
| 141 | + // 设置窗口的第一响应者为当前控件 |
| 142 | + window?.makeFirstResponder(self) |
| 143 | + } |
| 144 | + super.mouseDown(with: event) |
| 145 | + } |
| 146 | + |
| 147 | + /// 处理鼠标抬起事件 |
| 148 | + override func mouseUp(with event: NSEvent) { |
| 149 | + if isEnabled { |
| 150 | + if let action = action { |
| 151 | + // 发送指定的动作到目标对象 |
| 152 | + NSApp.sendAction(action, to: target, from: self) |
| 153 | + } |
| 154 | + } |
| 155 | + super.mouseUp(with: event) |
| 156 | + } |
| 157 | + |
| 158 | + /// 控件是否接受第一响应者状态 |
| 159 | + override var acceptsFirstResponder: Bool { |
| 160 | + return true |
| 161 | + } |
| 162 | + |
| 163 | + /// 成为第一响应者时的处理 |
| 164 | + override func becomeFirstResponder() -> Bool { |
| 165 | + return true |
| 166 | + } |
| 167 | + |
| 168 | + |
| 169 | + |
| 170 | + // MARK: - Drawing |
| 171 | + |
| 172 | + override func draw(_ dirtyRect: NSRect) { |
| 173 | + super.draw(dirtyRect) |
| 174 | + let width = bounds.width |
| 175 | + let height = bounds.height |
| 176 | + |
| 177 | + var bgGradient: NSGradient? |
| 178 | + var strokeColor: NSColor = .clear |
| 179 | + var symbolColor: NSColor = .clear |
| 180 | + |
| 181 | + switch buttonType { |
| 182 | + case .close: |
| 183 | + bgGradient = NSGradient(starting: .rgba(255, 95, 86, 1), ending: .rgba(255, 99, 91, 1)) |
| 184 | + strokeColor = .rgba(226, 62, 55, 1) |
| 185 | + symbolColor = .rgba(77, 0, 0, 1) |
| 186 | + case .mini: |
| 187 | + bgGradient = NSGradient(starting: .rgba(255, 189, 46, 1), ending: .rgba(255, 197, 47, 1)) |
| 188 | + strokeColor = .rgba(223, 157, 24, 1) |
| 189 | + symbolColor = .rgba(153, 88, 1, 1) |
| 190 | + case .fullScreen, .exitFullScreen: |
| 191 | + bgGradient = NSGradient(starting: .rgba(39, 201, 63, 1), ending: .rgba(39, 208, 65, 1)) |
| 192 | + strokeColor = .rgba(46, 176, 60, 1) |
| 193 | + symbolColor = .rgba(1, 100, 0, 1) |
| 194 | + } |
| 195 | + |
| 196 | + if !isActive && !hover { |
| 197 | + bgGradient = NSGradient(starting: .rgba(79, 83, 79, 1), ending: .rgba(75, 79, 75, 1)) |
| 198 | + strokeColor = .rgba(65, 65, 65, 1) |
| 199 | + } |
| 200 | + |
| 201 | + if buttonType == .mini && isWindowFullScreen { |
| 202 | + bgGradient = NSGradient(starting: .rgba(94, 98, 94, 1), ending: .rgba(90, 94, 90, 1)) |
| 203 | + strokeColor = .rgba(80, 80, 80, 1) |
| 204 | + } |
| 205 | + |
| 206 | + let path = NSBezierPath(ovalIn: NSRect(x: 0.5, y: 0.5, width: width - 1, height: height - 1)) |
| 207 | + bgGradient?.draw(in: path, relativeCenterPosition: .zero) |
| 208 | + strokeColor.setStroke() |
| 209 | + path.lineWidth = 0.5 |
| 210 | + path.stroke() |
| 211 | + |
| 212 | + if buttonType == .mini && isWindowFullScreen { return } |
| 213 | + |
| 214 | + guard hover else { return } |
| 215 | + |
| 216 | + drawSymbol(for: buttonType, in: NSRect(x: 0, y: 0, width: width, height: height), with: symbolColor) |
| 217 | + } |
| 218 | + |
| 219 | + |
| 220 | + /// 绘制按钮符号(如关闭、最小化、全屏等) |
| 221 | + /// - Parameters: |
| 222 | + /// - buttonType: 按钮类型,用于决定绘制的符号类型 |
| 223 | + /// - rect: 绘制区域的矩形框 |
| 224 | + /// - color: 符号的颜色 |
| 225 | + private func drawSymbol(for buttonType: LCWindowButtonType, in rect: NSRect, with color: NSColor) { |
| 226 | + let width = rect.width |
| 227 | + let height = rect.height |
| 228 | + |
| 229 | + switch buttonType { |
| 230 | + case .close: |
| 231 | + let path = NSBezierPath() |
| 232 | + path.move(to: NSPoint(x: width * 0.3, y: height * 0.3)) |
| 233 | + path.line(to: NSPoint(x: width * 0.7, y: height * 0.7)) |
| 234 | + path.move(to: NSPoint(x: width * 0.7, y: height * 0.3)) |
| 235 | + path.line(to: NSPoint(x: width * 0.3, y: height * 0.7)) |
| 236 | + path.lineWidth = 1 |
| 237 | + color.setStroke() |
| 238 | + path.stroke() |
| 239 | + case .mini: |
| 240 | + let path = NSBezierPath() |
| 241 | + path.move(to: NSPoint(x: width * 0.2, y: height * 0.5)) |
| 242 | + path.line(to: NSPoint(x: width * 0.8, y: height * 0.5)) |
| 243 | + path.lineWidth = 2 |
| 244 | + color.setStroke() |
| 245 | + path.stroke() |
| 246 | + case .fullScreen: |
| 247 | + let path = NSBezierPath() |
| 248 | + path.move(to: NSPoint(x: width * 0.25, y: height * 0.75)) |
| 249 | + path.line(to: NSPoint(x: width * 0.25, y: height / 3)) |
| 250 | + path.line(to: NSPoint(x: width * 2 / 3, y: height * 0.75)) |
| 251 | + path.close() |
| 252 | + color.setFill() |
| 253 | + path.fill() |
| 254 | + |
| 255 | + path.move(to: NSPoint(x: width * 0.75, y: height * 0.25)) |
| 256 | + path.line(to: NSPoint(x: width * 0.75, y: height * 2 / 3)) |
| 257 | + path.line(to: NSPoint(x: width / 3, y: height * 0.25)) |
| 258 | + path.close() |
| 259 | + color.setFill() |
| 260 | + path.fill() |
| 261 | + case .exitFullScreen: |
| 262 | + let path = NSBezierPath() |
| 263 | + path.move(to: NSPoint(x: width * 0.1, y: height * 0.52)) |
| 264 | + path.line(to: NSPoint(x: width * 0.48, y: height * 0.52)) |
| 265 | + path.line(to: NSPoint(x: width * 0.48, y: height * 0.9)) |
| 266 | + path.close() |
| 267 | + color.setFill() |
| 268 | + path.fill() |
| 269 | + |
| 270 | + path.move(to: NSPoint(x: width * 0.9, y: height * 0.48)) |
| 271 | + path.line(to: NSPoint(x: width * 0.52, y: height * 0.48)) |
| 272 | + path.line(to: NSPoint(x: width * 0.52, y: height * 0.1)) |
| 273 | + path.close() |
| 274 | + color.setFill() |
| 275 | + path.fill() |
| 276 | + } |
| 277 | + } |
| 278 | + |
| 279 | + |
| 280 | +} |
0 commit comments