-
Notifications
You must be signed in to change notification settings - Fork 4
/
MAAttachedWindow.h
184 lines (156 loc) · 7.76 KB
/
MAAttachedWindow.h
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
174
175
176
177
178
179
180
181
182
183
184
//
// MAAttachedWindow.h
//
// Created by Matt Gemmell on 27/09/2007.
// Copyright 2007 Magic Aubergine.
//
#import <Cocoa/Cocoa.h>
/*
Below are the positions the attached window can be displayed at.
Note that these positions are relative to the point passed to the constructor,
e.g. MAPositionBottomRight will put the window below the point and towards the right,
MAPositionTop will horizontally center the window above the point,
MAPositionRightTop will put the window to the right and above the point,
and so on.
You can also pass MAPositionAutomatic (or use an initializer which omits the 'onSide:'
argument) and the attached window will try to position itself sensibly, based on
available screen-space.
Notes regarding automatically-positioned attached windows:
(a) The window prefers to position itself horizontally centered below the specified point.
This gives a certain enhanced visual sense of an attachment/relationship.
(b) The window will try to align itself with its parent window (if any); i.e. it will
attempt to stay within its parent window's frame if it can.
(c) The algorithm isn't perfect. :) If in doubt, do your own calculations and then
explicitly request that the window attach itself to a particular side.
*/
typedef enum _MAWindowPosition {
// The four primary sides are compatible with the preferredEdge of NSDrawer.
MAPositionLeft = NSMinXEdge, // 0
MAPositionRight = NSMaxXEdge, // 2
MAPositionTop = NSMaxYEdge, // 3
MAPositionBottom = NSMinYEdge, // 1
MAPositionLeftTop = 4,
MAPositionLeftBottom = 5,
MAPositionRightTop = 6,
MAPositionRightBottom = 7,
MAPositionTopLeft = 8,
MAPositionTopRight = 9,
MAPositionBottomLeft = 10,
MAPositionBottomRight = 11,
MAPositionAutomatic = 12
} MAWindowPosition;
@interface MAAttachedWindow : NSWindow {
NSColor *borderColor;
float borderWidth;
float viewMargin;
float arrowBaseWidth;
float arrowHeight;
BOOL hasArrow;
float cornerRadius;
BOOL drawsRoundCornerBesideArrow;
@private
NSColor *_MABackgroundColor;
__weak NSView *_view;
__weak NSWindow *_window;
NSPoint _point;
MAWindowPosition _side;
float _distance;
NSRect _viewFrame;
BOOL _resizing;
}
/*
Initialization methods
Parameters:
view The view to display in the attached window. Must not be nil.
point The point to which the attached window should be attached. If you
are also specifying a parent window, the point should be in the
coordinate system of that parent window. If you are not specifying
a window, the point should be in the screen's coordinate space.
This value is required.
window The parent window to attach this one to. Note that no actual
relationship is created (particularly, this window is not made
a childWindow of the parent window).
Default: nil.
side The side of the specified point on which to attach this window.
Default: MAPositionAutomatic.
distance How far from the specified point this window should be.
Default: 0.
*/
- (MAAttachedWindow *)initWithView:(NSView *)view // designated initializer
attachedToPoint:(NSPoint)point
inWindow:(NSWindow *)window
onSide:(MAWindowPosition)side
atDistance:(float)distance;
- (MAAttachedWindow *)initWithView:(NSView *)view
attachedToPoint:(NSPoint)point
inWindow:(NSWindow *)window
atDistance:(float)distance;
- (MAAttachedWindow *)initWithView:(NSView *)view
attachedToPoint:(NSPoint)point
onSide:(MAWindowPosition)side
atDistance:(float)distance;
- (MAAttachedWindow *)initWithView:(NSView *)view
attachedToPoint:(NSPoint)point
atDistance:(float)distance;
- (MAAttachedWindow *)initWithView:(NSView *)view
attachedToPoint:(NSPoint)point
inWindow:(NSWindow *)window;
- (MAAttachedWindow *)initWithView:(NSView *)view
attachedToPoint:(NSPoint)point
onSide:(MAWindowPosition)side;
- (MAAttachedWindow *)initWithView:(NSView *)view
attachedToPoint:(NSPoint)point;
// Accessor methods
- (void)setPoint:(NSPoint)point side:(MAWindowPosition)side;
- (NSColor *)borderColor;
- (void)setBorderColor:(NSColor *)value;
- (float)borderWidth;
- (void)setBorderWidth:(float)value; // See note 1 below.
- (float)viewMargin;
- (void)setViewMargin:(float)value; // See note 2 below.
- (float)arrowBaseWidth;
- (void)setArrowBaseWidth:(float)value; // See note 2 below.
- (float)arrowHeight;
- (void)setArrowHeight:(float)value; // See note 2 below.
- (float)hasArrow;
- (void)setHasArrow:(float)value;
- (float)cornerRadius;
- (void)setCornerRadius:(float)value; // See note 2 below.
- (float)drawsRoundCornerBesideArrow; // See note 3 below.
- (void)setDrawsRoundCornerBesideArrow:(float)value; // See note 2 below.
- (void)setBackgroundImage:(NSImage *)value;
- (NSColor *)windowBackgroundColor; // See note 4 below.
- (void)setBackgroundColor:(NSColor *)value;
/*
Notes regarding accessor methods:
1. The border is drawn inside the viewMargin area, expanding inwards; it does not
increase the width/height of the window. You can use the -setBorderWidth: and
-setViewMargin: methods together to achieve the exact look/geometry you want.
(viewMargin is the distance between the edge of the view and the window edge.)
2. The specified setter methods are primarily intended to be used _before_ the window
is first shown. If you use them while the window is already visible, be aware
that they may cause the window to move and/or resize, in order to stay anchored
to the point specified in the initializer. They may also cause the view to move
within the window, in order to remain centered there.
Note that the -setHasArrow: method can safely be used at any time, and will not
cause moving/resizing of the window. This is for convenience, in case you want
to add or remove the arrow in response to user interaction. For example, you
could make the attached window movable by its background, and if the user dragged
it away from its initial point, the arrow could be removed. This would duplicate
how Aperture's attached windows behave.
3. drawsRoundCornerBesideArrow takes effect when the arrow is being drawn at a corner,
i.e. when it's not at one of the four primary compass directions. In this situation,
if drawsRoundCornerBesideArrow is YES (the default), then that corner of the window
will be rounded just like the other three corners, thus the arrow will be inset
slightly from the edge of the window to allow room for the rounded corner. If this
value is NO, the corner beside the arrow will be a square corner, and the other
three corners will be rounded.
This is useful when you want to attach a window very near the edge of another window,
and don't want the attached window's edge to be visually outside the frame of the
parent window.
4. Note that to retrieve the background color of the window, you should use the
-windowBackgroundColor method, instead of -backgroundColor. This is because we draw
the entire background of the window (rounded path, arrow, etc) in an NSColor pattern
image, and set it as the backgroundColor of the window.
*/
@end