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 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/ui/cocoa/panels/panel_cocoa.h"
#include "base/logging.h"
#import "chrome/browser/ui/cocoa/chrome_event_processing_window.h"
#import "chrome/browser/ui/cocoa/panels/panel_titlebar_view_cocoa.h"
#import "chrome/browser/ui/cocoa/panels/panel_utils_cocoa.h"
#import "chrome/browser/ui/cocoa/panels/panel_window_controller_cocoa.h"
#include "chrome/browser/ui/panels/panel.h"
#include "chrome/browser/ui/panels/stacked_panel_collection.h"
#include "content/public/browser/native_web_keyboard_event.h"
using content::NativeWebKeyboardEvent;
using content::WebContents;
// This creates a shim window class, which in turn creates a Cocoa window
// controller which in turn creates actual NSWindow by loading a nib.
// Overall chain of ownership is:
// PanelWindowControllerCocoa -> PanelCocoa -> Panel.
// static
NativePanel* Panel::CreateNativePanel(Panel* panel,
const gfx::Rect& bounds,
bool always_on_top) {
return new PanelCocoa(panel, bounds, always_on_top);
}
PanelCocoa::PanelCocoa(Panel* panel,
const gfx::Rect& bounds,
bool always_on_top)
: panel_(panel),
bounds_(bounds),
always_on_top_(always_on_top),
is_shown_(false),
attention_request_id_(0),
corner_style_(panel::ALL_ROUNDED) {
controller_ = [[PanelWindowControllerCocoa alloc] initWithPanel:this];
}
PanelCocoa::~PanelCocoa() {
}
bool PanelCocoa::IsClosed() const {
return !controller_;
}
void PanelCocoa::ShowPanel() {
ShowPanelInactive();
ActivatePanel();
// |-makeKeyAndOrderFront:| won't send |-windowDidBecomeKey:| until we
// return to the runloop. This causes extension tests that wait for the
// active status change notification to fail, so we send an active status
// notification here.
panel_->OnActiveStateChanged(true);
}
void PanelCocoa::ShowPanelInactive() {
if (IsClosed())
return;
// This method may be called several times, meaning 'ensure it's shown'.
// Animations don't look good when repeated - hence this flag is needed.
if (is_shown_) {
return;
}
// A call to SetPanelBounds() before showing it is required to set
// the panel frame properly.
SetPanelBoundsInstantly(bounds_);
is_shown_ = true;
NSRect finalFrame = cocoa_utils::ConvertRectToCocoaCoordinates(bounds_);
[controller_ revealAnimatedWithFrame:finalFrame];
}
gfx::Rect PanelCocoa::GetPanelBounds() const {
return bounds_;
}
// |bounds| is the platform-independent screen coordinates, with (0,0) at
// top-left of the primary screen.
void PanelCocoa::SetPanelBounds(const gfx::Rect& bounds) {
setBoundsInternal(bounds, true);
}
void PanelCocoa::SetPanelBoundsInstantly(const gfx::Rect& bounds) {
setBoundsInternal(bounds, false);
}
void PanelCocoa::setBoundsInternal(const gfx::Rect& bounds, bool animate) {
// We will call SetPanelBoundsInstantly() once before showing the panel
// and it should set the panel frame correctly.
if (bounds_ == bounds && is_shown_)
return;
bounds_ = bounds;
// Safely ignore calls to animate bounds before the panel is shown to
// prevent the window from loading prematurely.
if (animate && !is_shown_)
return;
NSRect frame = cocoa_utils::ConvertRectToCocoaCoordinates(bounds);
[controller_ setPanelFrame:frame animate:animate];
}
void PanelCocoa::ClosePanel() {
if (IsClosed())
return;
NSWindow* window = [controller_ window];
// performClose: contains a nested message loop which can cause reentrancy
// if the browser is terminating and closing all the windows.
// Use this version that corresponds to protocol of performClose: but does not
// spin a nested loop.
// TODO(dimich): refactor similar method from BWC and reuse here.
if ([controller_ windowShouldClose:window]) {
// Make sure that the panel window is not associated with the underlying
// stack window because otherwise hiding the panel window could cause all
// other panel windows in the same stack to disappear.
NSWindow* stackWindow = [window parentWindow];
if (stackWindow)
[stackWindow removeChildWindow:window];
[window orderOut:nil];
[window close];
}
}
void PanelCocoa::ActivatePanel() {
if (!is_shown_)
return;
[controller_ activate];
}
void PanelCocoa::DeactivatePanel() {
[controller_ deactivate];
}
bool PanelCocoa::IsPanelActive() const {
// TODO(dcheng): It seems like a lot of these methods can be called before
// our NSWindow is created. Do we really need to check in every one of these
// methods if the NSWindow is created, or is there a better way to
// gracefully handle this?
if (!is_shown_)
return false;
return [[controller_ window] isMainWindow];
}
void PanelCocoa::PreventActivationByOS(bool prevent_activation) {
[controller_ preventBecomingKeyWindow:prevent_activation];
return;
}
gfx::NativeWindow PanelCocoa::GetNativePanelWindow() {
return [controller_ window];
}
void PanelCocoa::UpdatePanelTitleBar() {
if (!is_shown_)
return;
[controller_ updateTitleBar];
}
void PanelCocoa::UpdatePanelLoadingAnimations(bool should_animate) {
[controller_ updateThrobber:should_animate];
}
void PanelCocoa::PanelWebContentsFocused(content::WebContents* contents) {
// Nothing to do.
}
void PanelCocoa::PanelCut() {
// Nothing to do since we do not have panel-specific system menu on Mac.
}
void PanelCocoa::PanelCopy() {
// Nothing to do since we do not have panel-specific system menu on Mac.
}
void PanelCocoa::PanelPaste() {
// Nothing to do since we do not have panel-specific system menu on Mac.
}
void PanelCocoa::DrawAttention(bool draw_attention) {
DCHECK((panel_->attention_mode() & Panel::USE_PANEL_ATTENTION) != 0);
PanelTitlebarViewCocoa* titlebar = [controller_ titlebarView];
if (draw_attention)
[titlebar drawAttention];
else
[titlebar stopDrawingAttention];
if ((panel_->attention_mode() & Panel::USE_SYSTEM_ATTENTION) != 0) {
if (draw_attention) {
DCHECK(!attention_request_id_);
attention_request_id_ = [NSApp requestUserAttention:NSCriticalRequest];
} else {
[NSApp cancelUserAttentionRequest:attention_request_id_];
attention_request_id_ = 0;
}
}
}
bool PanelCocoa::IsDrawingAttention() const {
PanelTitlebarViewCocoa* titlebar = [controller_ titlebarView];
return [titlebar isDrawingAttention];
}
void PanelCocoa::HandlePanelKeyboardEvent(
const NativeWebKeyboardEvent& event) {
if (event.skip_in_browser || event.type == NativeWebKeyboardEvent::Char)
return;
ChromeEventProcessingWindow* event_window =
static_cast<ChromeEventProcessingWindow*>([controller_ window]);
DCHECK([event_window isKindOfClass:[ChromeEventProcessingWindow class]]);
[event_window redispatchKeyEvent:event.os_event];
}
void PanelCocoa::FullScreenModeChanged(bool is_full_screen) {
if (!is_shown_) {
// If the panel window is not shown due to that a Chrome tab window is in
// fullscreen mode when the panel is being created, we need to show the
// panel window now. In addition, its titlebar needs to be updated since it
// is not done at the panel creation time.
if (!is_full_screen) {
ShowPanelInactive();
UpdatePanelTitleBar();
}
// No need to proceed when the panel window was not shown previously.
// We either show the panel window or do not show it depending on current
// full screen state.
return;
}
[controller_ fullScreenModeChanged:is_full_screen];
}
bool PanelCocoa::IsPanelAlwaysOnTop() const {
return always_on_top_;
}
void PanelCocoa::SetPanelAlwaysOnTop(bool on_top) {
if (always_on_top_ == on_top)
return;
always_on_top_ = on_top;
[controller_ updateWindowLevel];
[controller_ updateWindowCollectionBehavior];
}
void PanelCocoa::UpdatePanelMinimizeRestoreButtonVisibility() {
[controller_ updateTitleBarMinimizeRestoreButtonVisibility];
}
void PanelCocoa::SetWindowCornerStyle(panel::CornerStyle corner_style) {
corner_style_ = corner_style;
// TODO(dimich): investigate how to support it on Mac.
}
void PanelCocoa::MinimizePanelBySystem() {
[controller_ miniaturize];
}
bool PanelCocoa::IsPanelMinimizedBySystem() const {
return [controller_ isMiniaturized];
}
bool PanelCocoa::IsPanelShownOnActiveDesktop() const {
return [[controller_ window] isOnActiveSpace];
}
void PanelCocoa::ShowShadow(bool show) {
[controller_ showShadow:show];
}
void PanelCocoa::PanelExpansionStateChanging(
Panel::ExpansionState old_state, Panel::ExpansionState new_state) {
[controller_ updateWindowLevel:(new_state != Panel::EXPANDED)];
}
void PanelCocoa::AttachWebContents(content::WebContents* contents) {
[controller_ webContentsInserted:contents];
}
void PanelCocoa::DetachWebContents(content::WebContents* contents) {
[controller_ webContentsDetached:contents];
}
gfx::Size PanelCocoa::WindowSizeFromContentSize(
const gfx::Size& content_size) const {
NSRect content = NSMakeRect(0, 0,
content_size.width(), content_size.height());
NSRect frame = [controller_ frameRectForContentRect:content];
return gfx::Size(NSWidth(frame), NSHeight(frame));
}
gfx::Size PanelCocoa::ContentSizeFromWindowSize(
const gfx::Size& window_size) const {
NSRect frame = NSMakeRect(0, 0, window_size.width(), window_size.height());
NSRect content = [controller_ contentRectForFrameRect:frame];
return gfx::Size(NSWidth(content), NSHeight(content));
}
int PanelCocoa::TitleOnlyHeight() const {
return [controller_ titlebarHeightInScreenCoordinates];
}
Panel* PanelCocoa::panel() const {
return panel_.get();
}
void PanelCocoa::DidCloseNativeWindow() {
DCHECK(!IsClosed());
controller_ = NULL;
panel_->OnNativePanelClosed();
}
// NativePanelTesting implementation.
class CocoaNativePanelTesting : public NativePanelTesting {
public:
CocoaNativePanelTesting(NativePanel* native_panel);
~CocoaNativePanelTesting() override {}
// Overridden from NativePanelTesting
void PressLeftMouseButtonTitlebar(const gfx::Point& mouse_location,
panel::ClickModifier modifier) override;
void ReleaseMouseButtonTitlebar(panel::ClickModifier modifier) override;
void DragTitlebar(const gfx::Point& mouse_location) override;
void CancelDragTitlebar() override;
void FinishDragTitlebar() override;
bool VerifyDrawingAttention() const override;
bool VerifyActiveState(bool is_active) override;
bool VerifyAppIcon() const override;
bool VerifySystemMinimizeState() const override;
bool IsWindowVisible() const override;
bool IsWindowSizeKnown() const override;
bool IsAnimatingBounds() const override;
bool IsButtonVisible(panel::TitlebarButtonType button_type) const override;
panel::CornerStyle GetWindowCornerStyle() const override;
bool EnsureApplicationRunOnForeground() override;
private:
PanelTitlebarViewCocoa* titlebar() const;
// Weak, assumed always to outlive this test API object.
PanelCocoa* native_panel_window_;
};
NativePanelTesting* PanelCocoa::CreateNativePanelTesting() {
return new CocoaNativePanelTesting(this);
}
CocoaNativePanelTesting::CocoaNativePanelTesting(NativePanel* native_panel)
: native_panel_window_(static_cast<PanelCocoa*>(native_panel)) {
}
PanelTitlebarViewCocoa* CocoaNativePanelTesting::titlebar() const {
return [native_panel_window_->controller_ titlebarView];
}
void CocoaNativePanelTesting::PressLeftMouseButtonTitlebar(
const gfx::Point& mouse_location, panel::ClickModifier modifier) {
// Convert from platform-indepedent screen coordinates to Cocoa's screen
// coordinates because PanelTitlebarViewCocoa method takes Cocoa's screen
// coordinates.
int modifierFlags =
(modifier == panel::APPLY_TO_ALL ? NSShiftKeyMask : 0);
[titlebar() pressLeftMouseButtonTitlebar:
cocoa_utils::ConvertPointToCocoaCoordinates(mouse_location)
modifiers:modifierFlags];
}
void CocoaNativePanelTesting::ReleaseMouseButtonTitlebar(
panel::ClickModifier modifier) {
int modifierFlags =
(modifier == panel::APPLY_TO_ALL ? NSShiftKeyMask : 0);
[titlebar() releaseLeftMouseButtonTitlebar:modifierFlags];
}
void CocoaNativePanelTesting::DragTitlebar(const gfx::Point& mouse_location) {
// Convert from platform-indepedent screen coordinates to Cocoa's screen
// coordinates because PanelTitlebarViewCocoa method takes Cocoa's screen
// coordinates.
[titlebar() dragTitlebar:
cocoa_utils::ConvertPointToCocoaCoordinates(mouse_location)];
}
void CocoaNativePanelTesting::CancelDragTitlebar() {
[titlebar() cancelDragTitlebar];
}
void CocoaNativePanelTesting::FinishDragTitlebar() {
[titlebar() finishDragTitlebar];
}
bool CocoaNativePanelTesting::VerifyDrawingAttention() const {
return [titlebar() isDrawingAttention];
}
bool CocoaNativePanelTesting::VerifyActiveState(bool is_active) {
// TODO(jianli): to be implemented.
return false;
}
bool CocoaNativePanelTesting::VerifyAppIcon() const {
// Nothing to do since panel does not show dock icon.
return true;
}
bool CocoaNativePanelTesting::VerifySystemMinimizeState() const {
// TODO(jianli): to be implemented.
return true;
}
bool CocoaNativePanelTesting::IsWindowVisible() const {
return [[native_panel_window_->controller_ window] isVisible];
}
bool CocoaNativePanelTesting::IsWindowSizeKnown() const {
return true;
}
bool CocoaNativePanelTesting::IsAnimatingBounds() const {
if ([native_panel_window_->controller_ isAnimatingBounds])
return true;
StackedPanelCollection* stack = native_panel_window_->panel()->stack();
if (!stack)
return false;
return stack->IsAnimatingPanelBounds(native_panel_window_->panel());
}
bool CocoaNativePanelTesting::IsButtonVisible(
panel::TitlebarButtonType button_type) const {
switch (button_type) {
case panel::CLOSE_BUTTON:
return ![[titlebar() closeButton] isHidden];
case panel::MINIMIZE_BUTTON:
return ![[titlebar() minimizeButton] isHidden];
case panel::RESTORE_BUTTON:
return ![[titlebar() restoreButton] isHidden];
default:
NOTREACHED();
}
return false;
}
panel::CornerStyle CocoaNativePanelTesting::GetWindowCornerStyle() const {
return native_panel_window_->corner_style_;
}
bool CocoaNativePanelTesting::EnsureApplicationRunOnForeground() {
if ([NSApp isActive])
return true;
[NSApp activateIgnoringOtherApps:YES];
return [NSApp isActive];
}
|