1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #import <Cocoa/Cocoa.h>
7 #include "base/memory/scoped_ptr.h"
8 #include "components/bubble/bubble_reference.h"
10 @
class InfoBubbleView
;
11 class TabStripModelObserverBridge
;
13 // Base class for bubble controllers. Manages a xib that contains an
14 // InfoBubbleWindow which contains an InfoBubbleView. Contains code to close
15 // the bubble window on clicks outside of the window, and the like.
17 // 1. Create a new xib that contains a window. Change the window's class to
18 // InfoBubbleWindow. Give it a child view that autosizes to the window's full
19 // size, give it class InfoBubbleView. Make the controller the window's
21 // 2. Create a subclass of BaseBubbleController.
22 // 3. Change the xib's File Owner to your subclass.
23 // 4. Hook up the File Owner's |bubble_| to the InfoBubbleView in the xib.
24 @interface BaseBubbleController
: NSWindowController
<NSWindowDelegate
> {
26 NSWindow
* parentWindow_
; // weak
28 // Offset of the anchor point relative to the parent window's upper-left-hand
29 // corner. Used to ensure that if the parent window is resized with the bubble
30 // remaining visible, the bubble continues to be anchored correctly.
31 NSPoint anchorOffset_
;
33 IBOutlet InfoBubbleView
* bubble_
; // to set arrow position
34 // Bridge for tab change notifications.
35 scoped_ptr
<TabStripModelObserverBridge
> tabStripObserverBridge_
;
37 // Non-nil only on 10.7+. Both weak, owned by AppKit.
38 // A local event tap that will dismiss the bubble when a click is delivered
39 // outside the window. This is needed because the window shares first
40 // responder with its parent.
42 // A notification observer that gets triggered when any window resigns key.
43 id resignationObserver_
;
44 // The controlled window should be the key window when it's opened. True by
46 BOOL shouldOpenAsKeyWindow_
;
47 // The bubble window should close if it (or its parent) resigns key status.
48 BOOL shouldCloseOnResignKey_
;
49 BubbleReference bubbleReference_
;
52 @
property(nonatomic
, assign
) NSWindow
* parentWindow
;
53 // The point in base screen coordinates at which the bubble should open and the
55 @
property(nonatomic
, assign
) NSPoint anchorPoint
;
56 @
property(nonatomic
, readonly
) InfoBubbleView
* bubble
;
57 @
property(nonatomic
, assign
) BOOL shouldOpenAsKeyWindow
;
58 // Controls if the bubble auto-closes if the user clicks outside the bubble.
59 @
property(nonatomic
, assign
) BOOL shouldCloseOnResignKey
;
60 // A reference for bubbles that are managed by the BubbleManager.
61 @
property(nonatomic
, assign
) BubbleReference bubbleReference
;
63 // Creates a bubble. |nibPath| is just the basename, e.g. @"FirstRunBubble".
64 // |anchoredAt| is in screen space. You need to call -showWindow: to make the
65 // bubble visible. It will autorelease itself when the user dismisses the
67 // This is the designated initializer.
68 - (id
)initWithWindowNibPath
:(NSString
*)nibPath
69 parentWindow
:(NSWindow
*)parentWindow
70 anchoredAt
:(NSPoint
)anchoredAt
;
73 // Creates a bubble. |nibPath| is just the basename, e.g. @"FirstRunBubble".
74 // |view| must be in a window. The bubble will point at |offset| relative to
75 // |view|'s lower left corner. You need to call -showWindow: to make the
76 // bubble visible. It will autorelease itself when the user dismisses the
78 - (id
)initWithWindowNibPath
:(NSString
*)nibPath
79 relativeToView
:(NSView
*)view
80 offset
:(NSPoint
)offset
;
83 // For subclasses that do not load from a XIB, this will simply set the instance
84 // variables appropriately. This will also replace the |-[self window]|'s
85 // contentView with an instance of InfoBubbleView.
86 - (id
)initWithWindow
:(NSWindow
*)theWindow
87 parentWindow
:(NSWindow
*)parentWindow
88 anchoredAt
:(NSPoint
)anchoredAt
;
90 // Creates an autoreleased horizontal separator view with a given frame. The
91 // height of the frame is ignored.
92 - (NSBox
*)horizontalSeparatorWithFrame
:(NSRect
)frame
;
94 // Creates an autoreleased vertical separator view with a given frame. The
95 // width of frame is ignored.
96 - (NSBox
*)verticalSeparatorWithFrame
:(NSRect
)frame
;
101 // Methods for use by subclasses.
102 @interface
BaseBubbleController (Protected
)
103 // Registers event taps *after* the window is shown so that the bubble is
104 // dismissed when it resigns key. This only needs to be called if
105 // |-showWindow:| is overriden and does not call super. Noop on OSes <10.7.
106 - (void)registerKeyStateEventTap
;