-
Notifications
You must be signed in to change notification settings - Fork 0
/
MPPaletteHeaderRowView.m
173 lines (143 loc) · 4.33 KB
/
MPPaletteHeaderRowView.m
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
//
// MPPaletteViewController.h
//
// Created by Alexander Griekspoor on 23/06/2013.
// Copyright (c) 2013 Papersapp.com. All rights reserved.
//
// Based on JKConfigurationHeaderRowView.h from cocoa-configurations by Joris Kluivers
// Copyright (c) 2012 Tarento Software. All rights reserved.
#import "MPPaletteHeaderRowView.h"
#import "MTShadowTextField.h"
#import "NSColor_Extensions.h"
@interface MPPaletteHeaderRowView ()
@property (weak) NSTextField *showHideTextField;
@end
@implementation MPPaletteHeaderRowView
{
BOOL _pressed;
BOOL _highlight;
NSTrackingArea *_area;
}
- (id)initWithFrame:(NSRect)frameRect
{
if (self = [super initWithFrame:frameRect])
{
// add the show/hide label
NSTextField *textField = [[MTShadowTextField alloc] initWithFrame:self.bounds];
textField.frame = NSMakeRect(NSWidth(self.bounds) - 65.0, 8.0, 50.0, 18.0);
textField.autoresizingMask = (NSViewMinXMargin | NSViewMinYMargin);
textField.alignment = NSRightTextAlignment;
textField.textColor = [NSColor headerTextColor];
textField.font = [NSFont systemFontOfSize:11.0];
textField.drawsBackground = NO;
textField.editable = NO;
textField.selectable = NO;
textField.bordered = NO;
textField.stringValue = MTLocalizedString(@"Hide");
textField.hidden = YES;
[self addSubview:textField];
self.showHideTextField = textField;
}
return self;
}
- (void)dealloc
{
if (_area)
{
[self removeTrackingArea:_area];
}
}
- (NSButton *)disclosureButton
{
NSButton *disclosureButton = nil;
for (NSView *view in [self subviews])
{
if ([view isKindOfClass:[NSButton class]])
{
disclosureButton = (NSButton *) view;
break;
}
}
return disclosureButton;
}
- (void)updateButtonState
{
[[self disclosureButton] highlight:_pressed];
}
#pragma mark -
#pragma mark Tracking
- (void)updateTrackingAreas
{
[super updateTrackingAreas];
if (_area) [self removeTrackingArea:_area];
NSTrackingAreaOptions options = NSTrackingMouseEnteredAndExited | NSTrackingActiveAlways;
_area = [[NSTrackingArea alloc] initWithRect:[self bounds]
options:options
owner:self
userInfo:nil];
[self addTrackingArea:_area];
}
- (void)updateShowHideState
{
self.showHideTextField.stringValue = MTLocalizedString((self.disclosureButton.state ? @"Hide" : @"Show"));
self.showHideTextField.hidden = !_highlight;
}
#pragma mark -
#pragma mark Mouse Events
- (void)mouseEntered:(NSEvent *)event
{
_highlight = YES;
[self updateShowHideState];
}
- (void)mouseExited:(NSEvent *)event
{
_highlight = NO;
[self updateShowHideState];
}
- (void)mouseDown:(NSEvent *)theEvent
{
_pressed = YES;
[self updateButtonState];
}
- (void)mouseDragged:(NSEvent *)theEvent
{
NSPoint point = [self convertPoint:[theEvent locationInWindow] fromView:nil];
_pressed = NSMouseInRect(point, [self bounds], [self isFlipped]);
[self updateButtonState];
}
- (void)mouseUp:(NSEvent *)theEvent
{
if (_pressed)
{
[[self disclosureButton] performClick:theEvent];
[self updateShowHideState];
}
_pressed = NO;
}
#pragma mark -
#pragma mark Drawing
- (void)drawRect:(NSRect)dirtyRect
{
// hide our disclosure button
self.disclosureButton.hidden = YES;
// draw our favorite background color
[[NSColor whiteColor] set];
NSRectFill(dirtyRect);
// draw a 1px border if we're closed, also set a height check to prevent weird lines
// being drawn during the animation
if (!self.disclosureButton.state && NSHeight(dirtyRect) > 30.0)
{
[NSGraphicsContext saveGraphicsState];
{
[[NSGraphicsContext currentContext] setShouldAntialias:NO];
NSBezierPath *bezier = [NSBezierPath bezierPath];
[bezier moveToPoint:NSMakePoint(NSMinX(dirtyRect), (self.isFlipped ? NSMaxY(dirtyRect) : NSMinY(dirtyRect))) ];
[bezier lineToPoint:NSMakePoint(NSWidth(dirtyRect), (self.isFlipped ? NSMaxY(dirtyRect) : NSMinY(dirtyRect)))];
[[NSColor tableViewBorderColor] set];
[bezier setLineWidth:1.0];
[bezier stroke];
}
[NSGraphicsContext restoreGraphicsState];
}
}
@end