1 // Copyright (c) 2013 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 #include "ui/message_center/cocoa/tray_controller.h"
7 #include "ui/base/cocoa/window_size_constants.h"
8 #include "ui/base/resource/resource_bundle.h"
9 #import "ui/message_center/cocoa/popup_collection.h"
10 #import "ui/message_center/cocoa/tray_view_controller.h"
11 #include "ui/message_center/message_center_tray.h"
12 #include "ui/message_center/message_center_tray_delegate.h"
14 @interface MCTrayWindow : NSPanel
17 @implementation MCTrayWindow
19 - (BOOL)canBecomeKeyWindow {
23 - (void)cancelOperation:(id)sender {
29 @implementation MCTrayController
31 - (id)initWithMessageCenterTray:(message_center::MessageCenterTray*)tray {
32 base::scoped_nsobject<MCTrayWindow> window(
33 [[MCTrayWindow alloc] initWithContentRect:ui::kWindowSizeDeterminedLater
34 styleMask:NSBorderlessWindowMask |
35 NSNonactivatingPanelMask
36 backing:NSBackingStoreBuffered
38 if ((self = [super initWithWindow:window])) {
41 [window setDelegate:self];
42 [window setHasShadow:YES];
43 [window setHidesOnDeactivate:NO];
44 [window setLevel:NSFloatingWindowLevel];
46 viewController_.reset([[MCTrayViewController alloc] initWithMessageCenter:
47 tray_->message_center()]);
48 NSView* contentView = [viewController_ view];
49 [window setFrame:[contentView frame] display:NO];
50 [window setContentView:contentView];
52 // The global event monitor will close the tray in response to events
53 // delivered to other applications, and -windowDidResignKey: will catch
54 // events within the application.
55 __block MCTrayController* weakSelf = self;
57 [NSEvent addGlobalMonitorForEventsMatchingMask:NSLeftMouseDownMask |
58 NSRightMouseDownMask |
60 handler:^(NSEvent* event) {
61 [weakSelf windowDidResignKey:nil];
68 [NSEvent removeMonitor:clickEventMonitor_];
72 - (MCTrayViewController*)viewController {
73 return viewController_.get();
77 [viewController_ onWindowClosing];
81 - (void)showTrayAtRightOf:(NSPoint)rightPoint atLeftOf:(NSPoint)leftPoint {
82 NSScreen* screen = [[NSScreen screens] objectAtIndex:0];
83 NSRect screenFrame = [screen visibleFrame];
85 NSRect frame = [[viewController_ view] frame];
87 if (rightPoint.x + NSWidth(frame) < NSMaxX(screenFrame)) {
88 frame.origin.x = rightPoint.x;
89 frame.origin.y = rightPoint.y - NSHeight(frame);
91 frame.origin.x = leftPoint.x - NSWidth(frame);
92 frame.origin.y = leftPoint.y - NSHeight(frame);
95 [[self window] setFrame:frame display:YES];
96 [viewController_ scrollToTop];
97 [self showWindow:nil];
100 - (void)onMessageCenterTrayChanged {
101 [viewController_ onMessageCenterTrayChanged];
104 - (void)windowDidResignKey:(NSNotification*)notification {
105 // The settings bubble data structures assume that the settings dialog is
106 // visible only for short periods of time: There's a fixed list of permissions
108 [viewController_ cleanupSettings];
110 tray_->HideMessageCenterBubble();