Upstream version 10.39.225.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / ui / cocoa / passwords / manage_passwords_bubble_blacklist_view_controller.mm
1 // Copyright 2014 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.
4
5 #import "chrome/browser/ui/cocoa/passwords/manage_passwords_bubble_blacklist_view_controller.h"
6
7 #include "base/strings/sys_string_conversions.h"
8 #include "chrome/browser/ui/passwords/manage_passwords_bubble_model.h"
9 #include "grit/generated_resources.h"
10 #import "third_party/google_toolbox_for_mac/src/AppKit/GTMUILocalizerAndLayoutTweaker.h"
11 #include "ui/base/l10n/l10n_util.h"
12 #include "ui/base/models/combobox_model.h"
13
14 using namespace password_manager::mac::ui;
15
16 @interface ManagePasswordsBubbleBlacklistViewController ()
17 - (void)onDoneClicked:(id)sender;
18 - (void)onUndoBlacklistClicked:(id)sender;
19 @end
20
21 @implementation ManagePasswordsBubbleBlacklistViewController
22
23 - (id)initWithModel:(ManagePasswordsBubbleModel*)model
24            delegate:(id<ManagePasswordsBubbleContentViewDelegate>)delegate {
25   if ((self = [super initWithNibName:nil bundle:nil])) {
26     model_ = model;
27     delegate_ = delegate;
28   }
29   return self;
30 }
31
32 - (void)onDoneClicked:(id)sender {
33   model_->OnDoneClicked();
34   [delegate_ viewShouldDismiss];
35 }
36
37 - (void)onUndoBlacklistClicked:(id)sender {
38   model_->OnUnblacklistClicked();
39   [delegate_ viewShouldDismiss];
40 }
41
42 - (void)loadView {
43   self.view = [[[NSView alloc] initWithFrame:NSZeroRect] autorelease];
44
45   // -----------------------------------
46   // |  Title                          |
47   // |                                 |
48   // |  Blacklisted!                   |
49   // |                                 |
50   // |                  [Done] [Undo]  |
51   // -----------------------------------
52
53   // Create the elements and add them to the view.
54
55   // Title.
56   NSTextField* titleLabel =
57       [self addTitleLabel:base::SysUTF16ToNSString(model_->title())];
58
59   // Blacklist explanation.
60   NSTextField* explanationLabel =
61       [self addLabel:l10n_util::GetNSString(IDS_MANAGE_PASSWORDS_BLACKLISTED)];
62
63   // Done button.
64   doneButton_.reset([[self addButton:l10n_util::GetNSString(IDS_DONE)
65                               target:self
66                               action:@selector(onDoneClicked:)] retain]);
67
68   // Undo button.
69   undoBlacklistButton_.reset([[self
70       addButton:l10n_util::GetNSString(IDS_PASSWORD_MANAGER_UNBLACKLIST_BUTTON)
71          target:self
72          action:@selector(onUndoBlacklistClicked:)] retain]);
73
74   // Compute the bubble width using the title and explanation labels.
75   // The explanation label can wrap to multiple lines
76   const CGFloat contentWidth =
77       std::max(NSWidth([titleLabel frame]), kDesiredBubbleWidth);
78   [explanationLabel setFrameSize:NSMakeSize(contentWidth, 0)];
79   [GTMUILocalizerAndLayoutTweaker
80       sizeToFitFixedWidthTextField:explanationLabel];
81   const CGFloat width = kFramePadding + contentWidth + kFramePadding;
82
83   // Layout the elements, starting at the bottom and moving up.
84
85   // Buttons go on the bottom row and are right-aligned.
86   // Start with [Undo].
87   CGFloat curX = width - kFramePadding - NSWidth([undoBlacklistButton_ frame]);
88   CGFloat curY = kFramePadding;
89   [undoBlacklistButton_ setFrameOrigin:NSMakePoint(curX, curY)];
90
91   // [Done] goes to the left of [Undo].
92   curX = NSMinX([undoBlacklistButton_ frame]) -
93          kRelatedControlHorizontalPadding -
94          NSWidth([doneButton_ frame]);
95   [doneButton_ setFrameOrigin:NSMakePoint(curX, curY)];
96
97   // Explanation label goes on the next row and is shifted right.
98   curX = kFramePadding;
99   curY = NSMaxY([doneButton_ frame]) + kUnrelatedControlVerticalPadding;
100   [explanationLabel setFrameOrigin:NSMakePoint(curX, curY)];
101
102   // Title goes at the top after some padding.
103   curY = NSMaxY([explanationLabel frame]) + kUnrelatedControlVerticalPadding;
104   [titleLabel setFrameOrigin:NSMakePoint(curX, curY)];
105
106   // Update the bubble size.
107   const CGFloat height =
108       NSMaxY([titleLabel frame]) + kFramePadding;
109   [self.view setFrame:NSMakeRect(0, 0, width, height)];
110 }
111
112 @end
113
114 @implementation ManagePasswordsBubbleBlacklistViewController (Testing)
115
116 - (NSButton*)doneButton {
117   return doneButton_.get();
118 }
119
120 - (NSButton*)undoBlacklistButton {
121   return undoBlacklistButton_.get();
122 }
123
124 @end
125