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
|
// 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/tab_modal_confirm_dialog_mac.h"
#include <utility>
#include "base/mac/scoped_nsobject.h"
#include "chrome/browser/ui/browser_dialogs.h"
#import "chrome/browser/ui/cocoa/constrained_window/constrained_window_alert.h"
#import "chrome/browser/ui/cocoa/constrained_window/constrained_window_custom_sheet.h"
#import "chrome/browser/ui/cocoa/key_equivalent_constants.h"
#include "chrome/browser/ui/tab_modal_confirm_dialog_delegate.h"
#include "chrome/common/chrome_switches.h"
#include "ui/base/cocoa/cocoa_base_utils.h"
#include "ui/base/l10n/l10n_util_mac.h"
#include "ui/gfx/image/image.h"
// static
TabModalConfirmDialog* TabModalConfirmDialog::Create(
TabModalConfirmDialogDelegate* delegate,
content::WebContents* web_contents) {
// Deletes itself when closed.
return new TabModalConfirmDialogMac(delegate, web_contents);
}
@interface TabModalConfirmDialogMacBridge : NSObject {
TabModalConfirmDialogDelegate* delegate_; // weak
}
@end
@implementation TabModalConfirmDialogMacBridge
- (id)initWithDelegate:(TabModalConfirmDialogDelegate*)delegate {
if ((self = [super init])) {
delegate_ = delegate;
DCHECK(delegate_);
}
return self;
}
- (void)onAcceptButton:(id)sender {
delegate_->Accept();
}
- (void)onCancelButton:(id)sender {
delegate_->Cancel();
}
- (void)onCloseButton:(id)sender {
delegate_->Close();
}
- (void)onLinkClicked:(id)sender {
WindowOpenDisposition disposition =
ui::WindowOpenDispositionFromNSEvent([NSApp currentEvent]);
delegate_->LinkClicked(disposition);
}
@end
TabModalConfirmDialogMac::TabModalConfirmDialogMac(
TabModalConfirmDialogDelegate* delegate,
content::WebContents* web_contents)
: closing_(false),
delegate_(delegate) {
bridge_.reset([[TabModalConfirmDialogMacBridge alloc]
initWithDelegate:delegate]);
alert_.reset([[ConstrainedWindowAlert alloc] init]);
[alert_ setMessageText:
l10n_util::FixUpWindowsStyleLabel(delegate->GetTitle())];
[alert_ setLinkText:l10n_util::FixUpWindowsStyleLabel(
delegate->GetLinkText())
target:bridge_
action:@selector(onLinkClicked:)];
[alert_ setInformativeText:
l10n_util::FixUpWindowsStyleLabel(delegate->GetDialogMessage())];
[alert_ addButtonWithTitle:
l10n_util::FixUpWindowsStyleLabel(delegate->GetAcceptButtonTitle())
keyEquivalent:kKeyEquivalentReturn
target:bridge_
action:@selector(onAcceptButton:)];
[alert_ addButtonWithTitle:
l10n_util::FixUpWindowsStyleLabel(delegate->GetCancelButtonTitle())
keyEquivalent:kKeyEquivalentEscape
target:bridge_
action:@selector(onCancelButton:)];
[[alert_ closeButton] setTarget:bridge_];
[[alert_ closeButton] setAction:@selector(onCloseButton:)];
[alert_ layout];
base::scoped_nsobject<CustomConstrainedWindowSheet> sheet(
[[CustomConstrainedWindowSheet alloc]
initWithCustomWindow:[alert_ window]]);
window_ = CreateAndShowWebModalDialogMac(this, web_contents, sheet);
delegate_->set_close_delegate(this);
}
TabModalConfirmDialogMac::~TabModalConfirmDialogMac() {
}
void TabModalConfirmDialogMac::AcceptTabModalDialog() {
delegate_->Accept();
}
void TabModalConfirmDialogMac::CancelTabModalDialog() {
delegate_->Cancel();
}
void TabModalConfirmDialogMac::CloseDialog() {
if (!closing_) {
closing_ = true;
window_->CloseWebContentsModalDialog();
}
}
void TabModalConfirmDialogMac::OnConstrainedWindowClosed(
ConstrainedWindowMac* window) {
// If this method should mistakenly be called during Delegate::Close(),
// prevent a double-delete by moving delegate_ to a stack variable.
if (!delegate_)
return;
std::unique_ptr<TabModalConfirmDialogDelegate> delegate(std::move(delegate_));
// Provide a disposition in case the dialog was closed without accepting or
// cancelling.
delegate->Close();
delete this;
}
|