<commit_before>// 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_notification_controller.h"

#include <algorithm>

#include "base/logging.h"
#include "base/mac/foundation_util.h"
#include "base/mac/scoped_nsobject.h"
#include "base/strings/sys_string_conversions.h"
#include "chrome/browser/ui/autofill/autofill_dialog_types.h"
#include "chrome/browser/ui/chrome_style.h"
#include "chrome/browser/ui/cocoa/autofill/autofill_dialog_constants.h"
#include "grit/theme_resources.h"
#include "skia/ext/skia_utils_mac.h"

@interface AutofillNotificationView : NSView {
 @private
  // Weak, determines anchor point for arrow.
  NSView* arrowAnchorView_;
  BOOL hasArrow_;
  base::scoped_nsobject<NSColor> backgroundColor_;
}

@property (nonatomic, assign) NSView* anchorView;
@property (nonatomic, assign) BOOL hasArrow;
@property (nonatomic, retain) NSColor* backgroundColor;

@end

@implementation AutofillNotificationView

@synthesize hasArrow = hasArrow_;
@synthesize anchorView = arrowAnchorView_;

- (void)drawRect:(NSRect)dirtyRect {
  [super drawRect:dirtyRect];

  NSRect backgroundRect = [self bounds];
  if (hasArrow_) {
    NSPoint anchorPoint = NSMakePoint(NSMidX([arrowAnchorView_ bounds]), 0);
    anchorPoint = [self convertPoint:anchorPoint fromView:arrowAnchorView_];
    anchorPoint.y = NSMaxY([self bounds]);

    NSBezierPath* arrow = [NSBezierPath bezierPath];
    [arrow moveToPoint:anchorPoint];
    [arrow relativeLineToPoint:
        NSMakePoint(-autofill::kArrowWidth / 2.0, -autofill::kArrowHeight)];
    [arrow relativeLineToPoint:NSMakePoint(autofill::kArrowWidth, 0)];
    [arrow closePath];
    [backgroundColor_ setFill];
    [arrow fill];
    backgroundRect.size.height -= autofill::kArrowHeight;
  }

  dirtyRect = NSIntersectionRect(backgroundRect, dirtyRect);
  [backgroundColor_ setFill];
  NSRectFill(dirtyRect);
}

- (NSColor*)backgroundColor {
  return backgroundColor_;
}

- (void)setBackgroundColor:(NSColor*)backgroundColor {
  backgroundColor_.reset([backgroundColor retain]);
}

@end

@implementation AutofillNotificationController

- (id)initWithNotification:(const autofill::DialogNotification*)notification {
  if (self = [super init]) {
    base::scoped_nsobject<AutofillNotificationView> view(
        [[AutofillNotificationView alloc] initWithFrame:NSZeroRect]);
    [view setBackgroundColor:
        gfx::SkColorToCalibratedNSColor(notification->GetBackgroundColor())];
    [self setView:view];

    textfield_.reset([[NSTextField alloc] initWithFrame:NSZeroRect]);
    [textfield_ setEditable:NO];
    [textfield_ setBordered:NO];
    [textfield_ setDrawsBackground:NO];
    [textfield_ setTextColor:
         gfx::SkColorToCalibratedNSColor(notification->GetTextColor())];
    [textfield_ setStringValue:
        base::SysUTF16ToNSString(notification->display_text())];
    [textfield_ setHidden:notification->HasCheckbox()];

    checkbox_.reset([[NSButton alloc] initWithFrame:NSZeroRect]);
    [checkbox_ setButtonType:NSSwitchButton];
    [checkbox_ setHidden:!notification->HasCheckbox()];
    [checkbox_ setState:(notification->checked() ? NSOnState : NSOffState)];
    [checkbox_ setAttributedTitle:[textfield_ attributedStringValue]];
    // Update the size that preferredSizeForWidth will use. Do this here because
    //   (1) preferredSizeForWidth is logically const, and so shouldn't have a
    //       side-effect of updating the checkbox's frame, and
    //   (2) this way, the sizing computation can be cached.
    [checkbox_ sizeToFit];

    tooltipIcon_.reset([[NSImageView alloc] initWithFrame:NSZeroRect]);
    [tooltipIcon_ setImage:
        ui::ResourceBundle::GetSharedInstance().GetNativeImageNamed(<commit_msg>[rAc OSX] Add support for drawing borders around dialog notifications.<commit_after>// 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_notification_controller.h"

#include <algorithm>

#include "base/logging.h"
#include "base/mac/foundation_util.h"
#include "base/mac/scoped_nsobject.h"
#include "base/strings/sys_string_conversions.h"
#include "chrome/browser/ui/autofill/autofill_dialog_types.h"
#include "chrome/browser/ui/chrome_style.h"
#include "chrome/browser/ui/cocoa/autofill/autofill_dialog_constants.h"
#include "grit/theme_resources.h"
#include "skia/ext/skia_utils_mac.h"

@interface AutofillNotificationView : NSView {
 @private
  // Weak, determines anchor point for arrow.
  NSView* arrowAnchorView_;
  BOOL hasArrow_;
  base::scoped_nsobject<NSColor> backgroundColor_;
  base::scoped_nsobject<NSColor> borderColor_;
}

@property (nonatomic, assign) NSView* anchorView;
@property (nonatomic, assign) BOOL hasArrow;
@property (nonatomic, retain) NSColor* backgroundColor;
@property (nonatomic, retain) NSColor* borderColor;

@end

@implementation AutofillNotificationView

@synthesize hasArrow = hasArrow_;
@synthesize anchorView = arrowAnchorView_;

- (void)drawRect:(NSRect)dirtyRect {
  [super drawRect:dirtyRect];

  NSBezierPath* path;
  NSRect bounds = [self bounds];
  if (!hasArrow_) {
    path = [NSBezierPath bezierPathWithRect:bounds];
  } else {
    // The upper tip of the arrow.
    NSPoint anchorPoint = NSMakePoint(NSMidX([arrowAnchorView_ bounds]), 0);
    anchorPoint = [self convertPoint:anchorPoint fromView:arrowAnchorView_];
    anchorPoint.y = NSMaxY(bounds);
    // The minimal rectangle that encloses the arrow.
    NSRect arrowRect = NSMakeRect(anchorPoint.x - autofill::kArrowWidth / 2.0,
                                  anchorPoint.y - autofill::kArrowHeight,
                                  autofill::kArrowWidth,
                                  autofill::kArrowHeight);

    // Include the arrow and the rectangular non-arrow region in the same path,
    // so that the stroke is easier to draw. Start at the upper-left of the
    // rectangular region, and proceed clockwise.
    path = [NSBezierPath bezierPath];
    [path moveToPoint:NSMakePoint(NSMinX(bounds), NSMinY(arrowRect))];
    [path lineToPoint:arrowRect.origin];
    [path lineToPoint:NSMakePoint(NSMidX(arrowRect), NSMaxY(arrowRect))];
    [path lineToPoint:NSMakePoint(NSMaxX(arrowRect), NSMinY(arrowRect))];
    [path lineToPoint:NSMakePoint(NSMaxX(bounds), NSMinY(arrowRect))];
    [path lineToPoint:NSMakePoint(NSMaxX(bounds), NSMinY(bounds))];
    [path lineToPoint:NSMakePoint(NSMinX(bounds), NSMinY(bounds))];
    [path closePath];
  }

  [backgroundColor_ setFill];
  [path fill];
  [borderColor_ setStroke];
  [path stroke];
}

- (NSColor*)backgroundColor {
  return backgroundColor_;
}

- (void)setBackgroundColor:(NSColor*)backgroundColor {
  backgroundColor_.reset([backgroundColor retain]);
}

- (NSColor*)borderColor {
  return borderColor_;
}

- (void)setBorderColor:(NSColor*)borderColor {
  borderColor_.reset([borderColor retain]);
}

@end

@implementation AutofillNotificationController

- (id)initWithNotification:(const autofill::DialogNotification*)notification {
  if (self = [super init]) {
    base::scoped_nsobject<AutofillNotificationView> view(
        [[AutofillNotificationView alloc] initWithFrame:NSZeroRect]);
    [view setBackgroundColor:
        gfx::SkColorToCalibratedNSColor(notification->GetBackgroundColor())];
    [view setBorderColor:
        gfx::SkColorToCalibratedNSColor(notification->GetBorderColor())];
    [self setView:view];

    textfield_.reset([[NSTextField alloc] initWithFrame:NSZeroRect]);
    [textfield_ setEditable:NO];
    [textfield_ setBordered:NO];
    [textfield_ setDrawsBackground:NO];
    [textfield_ setTextColor:
         gfx::SkColorToCalibratedNSColor(notification->GetTextColor())];
    [textfield_ setStringValue:
        base::SysUTF16ToNSString(notification->display_text())];
    [textfield_ setHidden:notification->HasCheckbox()];

    checkbox_.reset([[NSButton alloc] initWithFrame:NSZeroRect]);
    [checkbox_ setButtonType:NSSwitchButton];
    [checkbox_ setHidden:!notification->HasCheckbox()];
    [checkbox_ setState:(notification->checked() ? NSOnState : NSOffState)];
    [checkbox_ setAttributedTitle:[textfield_ attributedStringValue]];
    // Update the size that preferredSizeForWidth will use. Do this here because
    //   (1) preferredSizeForWidth is logically const, and so shouldn't have a
    //       side-effect of updating the checkbox's frame, and
    //   (2) this way, the sizing computation can be cached.
    [checkbox_ sizeToFit];

    tooltipIcon_.reset([[NSImageView alloc] initWithFrame:NSZeroRect]);
    [tooltipIcon_ setImage:
        ui::ResourceBundle::GetSharedInstance().GetNativeImageNamed(
{'fraction_non_alphanumeric': 0.11145055442407785, 'fraction_numerical': 0.002602398732744965, 'mean_word_length': 4.984427894380501, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 3, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1793196', 'n_tokens_mistral': 2768, 'n_tokens_neox': 2664, 'n_words': 567}