-
Notifications
You must be signed in to change notification settings - Fork 0
/
PanelBackground.swift
109 lines (88 loc) · 3.53 KB
/
PanelBackground.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
//
// PanelBackGround.swift
// MenubarApplication
//
// Created by tasuku tozawa on 2016/10/28.
// Copyright © 2016年 tasuwo. All rights reserved.
//
import Cocoa
import Foundation
class PanelBackground: NSView {
let LINE_THICKNESS: CGFloat = 1.0
let CORNER_RADIUS: CGFloat = 6.0
let ARROW_WIDTH: CGFloat = 10.0
let ARROW_HEIGHT: CGFloat = 10.0
let FILL_OPACITY: CGFloat = 0.9
let STROKE_OPACITY: CGFloat = 0.0
var arrowX_: CGFloat = 10.0
func setArrowX(value: CGFloat) {
arrowX_ = value
self.needsDisplay = true
}
/*
⑧ ⑨ ① ②
┏━━^-━┓
⑦┃ ┃③
┗━━━━┛
⑥ ⑤ ④
*/
override func draw(_ dirtyRect: NSRect) {
let contentRect = NSInsetRect(self.bounds, LINE_THICKNESS, LINE_THICKNESS)
let path = NSBezierPath()
// ①
path.move(to: NSMakePoint(arrowX_, contentRect.maxY))
path.line(to: NSMakePoint(arrowX_ + ARROW_WIDTH / 2,
contentRect.maxY - ARROW_HEIGHT))
path.line(to: NSMakePoint(contentRect.maxX - CORNER_RADIUS,
contentRect.maxY - ARROW_HEIGHT))
// ②
let topRightCorner = NSMakePoint(contentRect.maxX,
contentRect.maxY - ARROW_HEIGHT)
path.curve(to: NSMakePoint(contentRect.maxX,
contentRect.maxY - ARROW_HEIGHT - CORNER_RADIUS),
controlPoint1: topRightCorner,
controlPoint2: topRightCorner)
// ③
path.line(to: NSMakePoint(contentRect.maxX,
contentRect.minY + CORNER_RADIUS))
// ④
let bottomRightCorner = NSMakePoint(contentRect.maxX,
contentRect.minY)
path.curve(to: NSMakePoint(contentRect.maxX - CORNER_RADIUS,
contentRect.minY),
controlPoint1: bottomRightCorner,
controlPoint2: bottomRightCorner)
// ⑤
path.line(to: NSMakePoint(contentRect.minX + CORNER_RADIUS,
contentRect.minY))
// ⑥
path.curve(to: NSMakePoint(contentRect.minX,
contentRect.minY + CORNER_RADIUS),
controlPoint1: contentRect.origin,
controlPoint2: contentRect.origin)
// ⑦
path.line(to: NSMakePoint(contentRect.minX,
contentRect.maxY - ARROW_HEIGHT - CORNER_RADIUS))
// ⑧
let topLeftCorner = NSMakePoint(contentRect.minX,
contentRect.maxY - ARROW_HEIGHT)
path.curve(to: NSMakePoint(contentRect.minX + CORNER_RADIUS,
contentRect.maxY - ARROW_HEIGHT),
controlPoint1: topLeftCorner,
controlPoint2: topLeftCorner)
// ⑨
path.line(to: NSMakePoint(arrowX_ - ARROW_WIDTH / 2,
contentRect.maxY - ARROW_HEIGHT))
path.close()
NSColor(deviceWhite: 1, alpha: FILL_OPACITY).setFill()
path.fill()
NSGraphicsContext.saveGraphicsState()
let clip = NSBezierPath(rect: self.bounds)
clip.append(path)
clip.addClip()
path.lineWidth = LINE_THICKNESS * 2
NSColor.black.setStroke()
path.stroke()
NSGraphicsContext.restoreGraphicsState()
}
}