Commit 6704f1f8 authored by groby@chromium.org's avatar groby@chromium.org

[rAC] added popup highlighting

Also added a shared protocol for text fields and popups so main logic
can access fields without differentiating by type.

R=sail@chromium.org
BUG=157274

Review URL: https://chromiumcodereview.appspot.com/18112007

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@209330 0039d316-1c4b-4281-b951-d872f2087c98
parent 920e6c6f
// Copyright 2013 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.
#ifndef CHROME_BROWSER_UI_COCOA_AUTOFILL_AUTOFILL_INPUT_FIELD_H_
#define CHROME_BROWSER_UI_COCOA_AUTOFILL_AUTOFILL_INPUT_FIELD_H_
// Protocol to allow access to any given input field in an Autofill dialog, no
// matter what the underlying control is.
@protocol AutofillInputField
@property(nonatomic, assign) BOOL invalid;
@property(nonatomic, copy) NSString* fieldValue;
@end
#endif // CHROME_BROWSER_UI_COCOA_AUTOFILL_AUTOFILL_INPUT_FIELD_H_
// Copyright 2013 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.
#ifndef CHROME_BROWSER_UI_COCOA_AUTOFILL_AUTOFILL_POP_UP_BUTTON_H_
#define CHROME_BROWSER_UI_COCOA_AUTOFILL_AUTOFILL_POP_UP_BUTTON_H_
#import <Cocoa/Cocoa.h>
#include "chrome/browser/ui/cocoa/autofill/autofill_input_field.h"
@interface AutofillPopUpButton : NSPopUpButton<AutofillInputField>
@end
@interface AutofillPopUpCell : NSPopUpButtonCell<AutofillInputField> {
@private
BOOL invalid_;
}
@end
#endif // CHROME_BROWSER_UI_COCOA_AUTOFILL_AUTOFILL_POP_UP_BUTTON_H_
// Copyright 2013 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.
#import "chrome/browser/ui/cocoa/autofill/autofill_pop_up_button.h"
#include <ApplicationServices/ApplicationServices.h>
#include "base/mac/scoped_nsobject.h"
#include "ui/gfx/scoped_ns_graphics_context_save_gstate_mac.h"
@implementation AutofillPopUpButton
+ (Class)cellClass {
return [AutofillPopUpCell class];
}
- (BOOL)invalid {
return [[self cell] invalid];
}
- (void)setInvalid:(BOOL)invalid {
[[self cell] setInvalid:invalid];
}
- (NSString*)fieldValue {
return [[self cell] fieldValue];
}
- (void)setFieldValue:(NSString*)fieldValue {
[[self cell] setFieldValue:fieldValue];
}
@end
@implementation AutofillPopUpCell
@synthesize invalid = invalid_;
// Draw a bezel that's highlighted.
- (void)drawBezelWithFrame:(NSRect)frame inView:(NSView*)controlView {
if (invalid_) {
CGContextRef context = static_cast<CGContextRef>(
[[NSGraphicsContext currentContext] graphicsPort]);
// Create a highlight-shaded bezel in a transparency layer.
CGContextBeginTransparencyLayerWithRect(context, NSRectToCGRect(frame), 0);
// 1. Draw bezel.
[super drawBezelWithFrame:frame inView:controlView];
// 2. Use that as stencil against solid color rect.
[[NSColor redColor] set];
NSRectFillUsingOperation(frame, NSCompositeSourceAtop);
// 3. Composite the solid color bezel and the actual bezel.
CGContextSetBlendMode(context, kCGBlendModePlusDarker);
[super drawBezelWithFrame:frame inView:controlView];
CGContextEndTransparencyLayer(context);
} else {
[super drawBezelWithFrame:frame inView:controlView];
}
}
- (NSString*)fieldValue {
return [self titleOfSelectedItem];
}
- (void)setFieldValue:(NSString*)fieldValue {
[self selectItemWithTitle:fieldValue];
}
@end
// Copyright 2013 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.
#import "chrome/browser/ui/cocoa/autofill/autofill_pop_up_button.h"
#import "base/mac/scoped_nsobject.h"
#import "chrome/browser/ui/cocoa/cocoa_test_helper.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "testing/platform_test.h"
class AutofillPopUpButtonTest : public CocoaTest {
public:
AutofillPopUpButtonTest() {
NSRect frame = NSMakeRect(0, 0, 50, 30);
button_.reset([[AutofillPopUpButton alloc] initWithFrame:frame]);
[button_ sizeToFit];
[[test_window() contentView] addSubview:button_];
}
protected:
base::scoped_nsobject<AutofillPopUpButton> button_;
DISALLOW_COPY_AND_ASSIGN(AutofillPopUpButtonTest);
};
TEST_VIEW(AutofillPopUpButtonTest, button_)
// Test invalid, mostly to ensure nothing leaks or crashes.
TEST_F(AutofillPopUpButtonTest, DisplayWithInvalid) {
[button_ setInvalid:YES];
[button_ display];
[button_ setInvalid:NO];
[button_ display];
}
......@@ -7,6 +7,7 @@
#include "base/strings/sys_string_conversions.h"
#include "base/strings/utf_string_conversions.h"
#include "chrome/browser/ui/autofill/autofill_dialog_controller.h"
#import "chrome/browser/ui/cocoa/autofill/autofill_pop_up_button.h"
#import "chrome/browser/ui/cocoa/autofill/autofill_section_view.h"
#import "chrome/browser/ui/cocoa/autofill/autofill_suggestion_container.h"
#import "chrome/browser/ui/cocoa/autofill/autofill_textfield.h"
......@@ -91,12 +92,11 @@ void BreakSuggestionText(const string16& text,
}
- (void)getInputs:(autofill::DetailOutputMap*)output {
for (id input in [inputs_ subviews]) {
for (NSControl<AutofillInputField>* input in [inputs_ subviews]) {
const autofill::DetailInput* detailInput =
reinterpret_cast<autofill::DetailInput*>([input tag]);
DCHECK(detailInput);
NSString* value = [input isKindOfClass:[NSPopUpButton class]] ?
[input titleOfSelectedItem] : [input stringValue];
NSString* value = [input fieldValue];
output->insert(
std::make_pair(detailInput,base::SysNSStringToUTF16(value)));
}
......@@ -277,17 +277,15 @@ void BreakSuggestionText(const string16& text,
ui::ComboboxModel* input_model =
controller_->ComboboxModelForAutofillType(input.type);
base::scoped_nsprotocol<NSControl<AutofillInputField>*> control;
if (input_model) {
base::scoped_nsobject<NSPopUpButton> popup(
[[NSPopUpButton alloc] initWithFrame:NSZeroRect pullsDown:YES]);
base::scoped_nsobject<AutofillPopUpButton> popup(
[[AutofillPopUpButton alloc] initWithFrame:NSZeroRect pullsDown:NO]);
for (int i = 0; i < input_model->GetItemCount(); ++i) {
[popup addItemWithTitle:
base::SysUTF16ToNSString(input_model->GetItemAt(i))];
}
[popup selectItemWithTitle:base::SysUTF16ToNSString(input.initial_value)];
[popup sizeToFit];
[popup setTag:reinterpret_cast<NSInteger>(&input)];
layout->AddView(popup);
control.reset(popup.release());
} else {
base::scoped_nsobject<AutofillTextField> field(
[[AutofillTextField alloc] init]);
......@@ -296,11 +294,13 @@ void BreakSuggestionText(const string16& text,
[[field cell] setIcon:
controller_->IconForField(
input.type, input.initial_value).AsNSImage()];
[[field cell] setInvalid:YES];
[field sizeToFit];
[field setTag:reinterpret_cast<NSInteger>(&input)];
layout->AddView(field);
control.reset(field.release());
}
[control setFieldValue:base::SysUTF16ToNSString(input.initial_value)];
[control setInvalid:YES];
[control sizeToFit];
[control setTag:reinterpret_cast<NSInteger>(&input)];
layout->AddView(control);
}
return view.autorelease();
......
......@@ -7,21 +7,21 @@
#import <Cocoa/Cocoa.h>
#include <base/mac/scoped_nsobject.h>
#include "base/mac/scoped_nsobject.h"
#include "chrome/browser/ui/cocoa/autofill/autofill_input_field.h"
// Text field used for text inputs inside Autofill.
// Provide both dog ear and red outline when the contents are marked invalid.
@interface AutofillTextField : NSTextField
@interface AutofillTextField : NSTextField<AutofillInputField>
@end
@interface AutofillTextFieldCell : NSTextFieldCell {
@interface AutofillTextFieldCell : NSTextFieldCell<AutofillInputField> {
@private
BOOL invalid_;
base::scoped_nsobject<NSImage> icon_;
}
@property(assign, nonatomic) BOOL invalid;
@property(nonatomic, retain, getter=icon, setter=setIcon:) NSImage* icon;
@property(nonatomic, retain) NSImage* icon;
@end
......
......@@ -27,6 +27,22 @@ const CGFloat kGap = 6.0; // gap between icon and text.
return [AutofillTextFieldCell class];
}
- (BOOL)invalid {
return [[self cell] invalid];
}
- (void)setInvalid:(BOOL)invalid {
[[self cell] setInvalid:invalid];
}
- (NSString*)fieldValue {
return [[self cell] fieldValue];
}
- (void)setFieldValue:(NSString*)fieldValue {
[[self cell] setFieldValue:fieldValue];
}
@end
@implementation AutofillTextFieldCell
......@@ -41,6 +57,14 @@ const CGFloat kGap = 6.0; // gap between icon and text.
icon_.reset([icon retain]);
}
- (NSString*)fieldValue {
return [self stringValue];
}
- (void)setFieldValue:(NSString*)fieldValue {
[self setStringValue:fieldValue];
}
- (NSRect)textFrameForFrame:(NSRect)frame {
if (icon_) {
NSRect textFrame, iconFrame;
......
......@@ -443,6 +443,8 @@
'browser/ui/cocoa/autofill/down_arrow_popup_menu_cell.mm',
'browser/ui/cocoa/autofill/layout_view.h',
'browser/ui/cocoa/autofill/layout_view.mm',
'browser/ui/cocoa/autofill/autofill_pop_up_button.h',
'browser/ui/cocoa/autofill/autofill_pop_up_button.mm',
'browser/ui/cocoa/autofill/simple_grid_layout.h',
'browser/ui/cocoa/autofill/simple_grid_layout.mm',
'browser/ui/cocoa/background_gradient_view.h',
......
......@@ -1352,6 +1352,7 @@
'browser/ui/cocoa/autofill/autofill_account_chooser_unittest.mm',
'browser/ui/cocoa/autofill/autofill_details_container_unittest.mm',
'browser/ui/cocoa/autofill/autofill_main_container_unittest.mm',
'browser/ui/cocoa/autofill/autofill_pop_up_button_unittest.mm',
'browser/ui/cocoa/autofill/autofill_textfield_unittest.mm',
'browser/ui/cocoa/autofill/autofill_section_container_unittest.mm',
'browser/ui/cocoa/autofill/autofill_section_view_unittest.mm',
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment