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.
5 #import "chrome/browser/ui/cocoa/passwords/manage_passwords_bubble_blacklist_view_controller.h"
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"
14 using namespace password_manager::mac::ui;
16 @interface ManagePasswordsBubbleBlacklistViewController ()
17 - (void)onDoneClicked:(id)sender;
18 - (void)onUndoBlacklistClicked:(id)sender;
21 @implementation ManagePasswordsBubbleBlacklistViewController
23 - (id)initWithModel:(ManagePasswordsBubbleModel*)model
24 delegate:(id<ManagePasswordsBubbleContentViewDelegate>)delegate {
25 if ((self = [super initWithNibName:nil bundle:nil])) {
32 - (void)onDoneClicked:(id)sender {
33 model_->OnDoneClicked();
34 [delegate_ viewShouldDismiss];
37 - (void)onUndoBlacklistClicked:(id)sender {
38 model_->OnUnblacklistClicked();
39 [delegate_ viewShouldDismiss];
43 self.view = [[[NSView alloc] initWithFrame:NSZeroRect] autorelease];
45 // -----------------------------------
51 // -----------------------------------
53 // Create the elements and add them to the view.
56 NSTextField* titleLabel =
57 [self addTitleLabel:base::SysUTF16ToNSString(model_->title())];
59 // Blacklist explanation.
60 NSTextField* explanationLabel =
61 [self addLabel:l10n_util::GetNSString(IDS_MANAGE_PASSWORDS_BLACKLISTED)];
64 doneButton_.reset([[self addButton:l10n_util::GetNSString(IDS_DONE)
66 action:@selector(onDoneClicked:)] retain]);
69 undoBlacklistButton_.reset([[self
70 addButton:l10n_util::GetNSString(IDS_PASSWORD_MANAGER_UNBLACKLIST_BUTTON)
72 action:@selector(onUndoBlacklistClicked:)] retain]);
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;
83 // Layout the elements, starting at the bottom and moving up.
85 // Buttons go on the bottom row and are right-aligned.
87 CGFloat curX = width - kFramePadding - NSWidth([undoBlacklistButton_ frame]);
88 CGFloat curY = kFramePadding;
89 [undoBlacklistButton_ setFrameOrigin:NSMakePoint(curX, curY)];
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)];
97 // Explanation label goes on the next row and is shifted right.
99 curY = NSMaxY([doneButton_ frame]) + kUnrelatedControlVerticalPadding;
100 [explanationLabel setFrameOrigin:NSMakePoint(curX, curY)];
102 // Title goes at the top after some padding.
103 curY = NSMaxY([explanationLabel frame]) + kUnrelatedControlVerticalPadding;
104 [titleLabel setFrameOrigin:NSMakePoint(curX, curY)];
106 // Update the bubble size.
107 const CGFloat height =
108 NSMaxY([titleLabel frame]) + kFramePadding;
109 [self.view setFrame:NSMakeRect(0, 0, width, height)];
114 @implementation ManagePasswordsBubbleBlacklistViewController (Testing)
116 - (NSButton*)doneButton {
117 return doneButton_.get();
120 - (NSButton*)undoBlacklistButton {
121 return undoBlacklistButton_.get();