<commit_before>// Copyright (c) 2009 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/cocoa/delayedmenu_button.h"

#include "base/logging.h"
#include "base/scoped_nsobject.h"
#import "chrome/browser/cocoa/clickhold_button_cell.h"

@interface DelayedMenuButton (Private)

- (void)resetToDefaults;
- (void)menuAction:(id)sender;

@end  // @interface DelayedMenuButton (Private)

@implementation DelayedMenuButton

// Overrides:

+ (Class)cellClass {
  return [ClickHoldButtonCell class];
}

- (id)init {
  if ((self = [super init]))
    [self resetToDefaults];
  return self;
}

- (id)initWithCoder:(NSCoder*)decoder {
  if ((self = [super initWithCoder:decoder]))
    [self resetToDefaults];
  return self;
}

- (id)initWithFrame:(NSRect)frameRect {
  if ((self = [super initWithFrame:frameRect]))
    [self resetToDefaults];
  return self;
}

- (void)dealloc {
  [menu_ release];
  [super dealloc];
}

- (void)awakeFromNib {
  [self resetToDefaults];
}

// Accessors and mutators:

@synthesize menu = menu_;

// Don't synthesize for menuEnabled_; its mutator must do other things.
- (void)setMenuEnabled:(BOOL)enabled {
  menuEnabled_ = enabled;
  [[self cell] setEnableClickHold:menuEnabled_];
}

- (BOOL)menuEnabled {
  return menuEnabled_;
}

@end  // @implementation DelayedMenuButton

@implementation DelayedMenuButton (Private)

- (void)resetToDefaults {
  id cell = [self cell];
  DCHECK([cell isKindOfClass:[ClickHoldButtonCell class]]);
  [self setEnabled:NO];                 // Make the controller put in a menu and
                                        // enable it explicitly. This also takes
                                        // care of |[cell setEnableClickHold:]|.
  [cell setClickHoldTimeout:0.25];      // Random guess at Cocoa-ish value.
  [cell setTrackOnlyInRect:NO];
  [cell setActivateOnDrag:YES];
  [cell setClickHoldAction:@selector(menuAction:)];
  [cell setClickHoldTarget:self];
}

- (void)menuAction:(id)sender {
  // We shouldn't get here unless the menu is enabled.
  DCHECK(menuEnabled_);

  // If we don't have a menu (in which case the person using this control is
  // being bad), just wait for a mouse up.
  if (!menu_) {
    LOG(WARNING) << "No menu available.";
    [NSApp nextEventMatchingMask:NSLeftMouseUpMask
                       untilDate:[NSDate distantFuture]
                          inMode:NSEventTrackingRunLoopMode
                         dequeue:YES];
    return;
  }

  // FIXME(<EMAIL>): Don't ask me. I don't know what's going on.
  // But it yields unquestionably right results (even when the menu has to flip
  // upwards because you've stupidly dragged the top of the window to the bottom
  // of the screen) -- this demonstrates that the y-coordinate (in our
  // superview's coordinates) is right. The x-coordinate (in our coordinates) is
  // right since the menu appears horizontally in the right place (more or
  // less). The |- 2.0| factor is an inexplicable fudge to make it approximately
  // line up. If someone figures out what's going on, please fix this.
  NSRect frame = [self frame];
  frame.origin.x = [self convertPoint:frame.origin
                             fromView:[self superview]].x - 2.0;

  // Make our pop-up button cell and set things up. This is, as of 10.5, the
  // official Apple-recommended hack. Later, perhaps |-[NSMenu
  // popUpMenuPositioningItem:atLocation:inView:]| may be a better option.
  // However, using a pulldown has the benefit that Cocoa automatically places
  // the menu correctly even when we're at the edge of the screen (including
  // "dragging upwards" when the button is close to the bottom of the screen).
  scoped_nsobject<NSPopUpButtonCell> popUpCell(
      [[NSPopUpButtonCell alloc] initTextCell:@""
                                    pullsDown:YES]);
  DCHECK(popUpCell.get());
  [popUpCell setMenu:menu_];
  [popUpCell selectItem:nil];
  [popUpCell attachPopUpWithFrame:frame<commit_msg>(Mac) Place back/forward toolbar menu correctly when there's a bookmark bar.<commit_after>// Copyright (c) 2009 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/cocoa/delayedmenu_button.h"

#include "base/logging.h"
#include "base/scoped_nsobject.h"
#import "chrome/browser/cocoa/clickhold_button_cell.h"

@interface DelayedMenuButton (Private)

- (void)resetToDefaults;
- (void)menuAction:(id)sender;

@end  // @interface DelayedMenuButton (Private)

@implementation DelayedMenuButton

// Overrides:

+ (Class)cellClass {
  return [ClickHoldButtonCell class];
}

- (id)init {
  if ((self = [super init]))
    [self resetToDefaults];
  return self;
}

- (id)initWithCoder:(NSCoder*)decoder {
  if ((self = [super initWithCoder:decoder]))
    [self resetToDefaults];
  return self;
}

- (id)initWithFrame:(NSRect)frameRect {
  if ((self = [super initWithFrame:frameRect]))
    [self resetToDefaults];
  return self;
}

- (void)dealloc {
  [menu_ release];
  [super dealloc];
}

- (void)awakeFromNib {
  [self resetToDefaults];
}

// Accessors and mutators:

@synthesize menu = menu_;

// Don't synthesize for menuEnabled_; its mutator must do other things.
- (void)setMenuEnabled:(BOOL)enabled {
  menuEnabled_ = enabled;
  [[self cell] setEnableClickHold:menuEnabled_];
}

- (BOOL)menuEnabled {
  return menuEnabled_;
}

@end  // @implementation DelayedMenuButton

@implementation DelayedMenuButton (Private)

- (void)resetToDefaults {
  id cell = [self cell];
  DCHECK([cell isKindOfClass:[ClickHoldButtonCell class]]);
  [self setEnabled:NO];                 // Make the controller put in a menu and
                                        // enable it explicitly. This also takes
                                        // care of |[cell setEnableClickHold:]|.
  [cell setClickHoldTimeout:0.25];      // Random guess at Cocoa-ish value.
  [cell setTrackOnlyInRect:NO];
  [cell setActivateOnDrag:YES];
  [cell setClickHoldAction:@selector(menuAction:)];
  [cell setClickHoldTarget:self];
}

- (void)menuAction:(id)sender {
  // We shouldn't get here unless the menu is enabled.
  DCHECK(menuEnabled_);

  // If we don't have a menu (in which case the person using this control is
  // being bad), just wait for a mouse up.
  if (!menu_) {
    LOG(WARNING) << "No menu available.";
    [NSApp nextEventMatchingMask:NSLeftMouseUpMask
                       untilDate:[NSDate distantFuture]
                          inMode:NSEventTrackingRunLoopMode
                         dequeue:YES];
    return;
  }

  // FIXME(<EMAIL>): We have some fudge factors below to make
  // things line up (approximately). I wish I knew how to get rid of them. (Note
  // that our view is flipped, and that frame should be in our coordinates.)
  // The y/height is very odd, since it doesn't seem to respond to changes the
  // way that it should. I don't understand it.
  NSRect frame = [self convertRect:[self frame]
                          fromView:[self superview]];
  frame.origin.x -= 2.0;
  frame.size.height += 10.0;

  // Make our pop-up button cell and set things up. This is, as of 10.5, the
  // official Apple-recommended hack. Later, perhaps |-[NSMenu
  // popUpMenuPositioningItem:atLocation:inView:]| may be a better option.
  // However, using a pulldown has the benefit that Cocoa automatically places
  // the menu correctly even when we're at the edge of the screen (including
  // "dragging upwards" when the button is close to the bottom of the screen).
  scoped_nsobject<NSPopUpButtonCell> popUpCell(
      [[NSPopUpButtonCell alloc] initTextCell:@""
                                    pullsDown:YES]);
  DCHECK(popUpCell.get());
  [popUpCell setMenu:menu_];
  [popUpCell selectItem:nil];
  [popUpCell attachPopUpWithFrame:frame
{'fraction_non_alphanumeric': 0.10728744939271255, 'fraction_numerical': 0.0036690283400809716, 'mean_word_length': 3.1517857142857144, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 10, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3094749', 'n_tokens_mistral': 2499, 'n_tokens_neox': 2351, 'n_words': 753}