-
Notifications
You must be signed in to change notification settings - Fork 6
/
JWToolbarAdaptiveSpaceItem.m
executable file
·123 lines (103 loc) · 2.62 KB
/
JWToolbarAdaptiveSpaceItem.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
//
// JWToolbarAdaptiveSpaceItem.m
//
// Created by John Wells on 7/14/13.
// Copyright (c) 2013 John Wells. All rights reserved.
//
#import "JWToolbarAdaptiveSpaceItem.h"
@implementation JWToolbarAdaptiveSpaceItem
-(void)dealloc
{
// Clean up our key-value observer
[self.linkedView removeObserver:self forKeyPath:@"frame"];
}
-(void)setLinkedView:(NSView *)linkedView
{
/*
Start observing the linked view's frame as soon its
IBOutlet is assigned
*/
_linkedView = linkedView;
[self.linkedView addObserver:self forKeyPath:@"frame" options:0 context:NULL];
}
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
/*
Update the toolbar item's width whenever a change to the
linked view's frame is observed
*/
if ([keyPath isEqualToString:@"frame"] && object == self.linkedView)
{
[self updateWidth];
}
}
- (void)updateWidth
{
if (self.linkedView)
{
/*
Resizes the adaptive toolbar item and makes sure that we don't
make the toolbar do funky things when the linked view collapses
*/
if (self.linkedView.frame.size.width-16 > -8)
{
[self setMinSize:NSMakeSize(self.linkedView.frame.size.width-16, 0)];
[self setMaxSize:NSMakeSize(self.linkedView.frame.size.width-16, 0)];
} else {
[self setMinSize:NSMakeSize(-8, 0)];
[self setMaxSize:NSMakeSize(-8, 0)];
}
}
}
#pragma mark - NSToolbarItem Methods
-(NSView *)view
{
/*
Set the toolbar item's content to a view that ignores
clicks so the user can drag the window by the portion
of the toolbar covered by the adaptive toolbar item
*/
return [[JWClickThroughView alloc] init];;
}
-(NSString *)label
{
return @"";
}
-(NSString *)paletteLabel
{
return @"Adaptive Space Item";
}
-(NSSize)minSize
{
if (self.linkedView)
{
if (self.linkedView.frame.size.width-16 > -8)
{
return NSMakeSize(self.linkedView.frame.size.width-16, 0);
} else {
return NSMakeSize(-8, 0);
}
}
return [super minSize];
}
-(NSSize)maxSize
{
if (self.linkedView)
{
if (self.linkedView.frame.size.width-16 > -8)
{
return NSMakeSize(self.linkedView.frame.size.width-16, 0);
} else {
return NSMakeSize(-8, 0);
}
}
return [super maxSize];
}
@end
@implementation JWClickThroughView
// A super-simple NSView that ignores clicks
-(BOOL)acceptsFirstMouse:(NSEvent *)theEvent
{
return NO;
}
@end