[Metrics] Make MetricsStateManager take a callback param to check if UMA is enabled.
[chromium-blink-merge.git] / chrome / browser / ui / cocoa / find_bar / find_bar_cocoa_controller.mm
blobb328234f01a6e542e593f142e09bb8283556e24c
1 // Copyright (c) 2012 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 <Cocoa/Cocoa.h>
7 #include "base/auto_reset.h"
8 #include "base/mac/bundle_locations.h"
9 #include "base/mac/mac_util.h"
10 #include "base/strings/sys_string_conversions.h"
11 #include "grit/theme_resources.h"
12 #include "grit/ui_resources.h"
13 #include "chrome/browser/ui/browser_finder.h"
14 #include "chrome/browser/ui/browser_window.h"
15 #include "chrome/browser/ui/cocoa/browser_window_controller.h"
16 #import "chrome/browser/ui/cocoa/find_bar/find_bar_bridge.h"
17 #import "chrome/browser/ui/cocoa/find_bar/find_bar_cocoa_controller.h"
18 #import "chrome/browser/ui/cocoa/find_bar/find_bar_text_field.h"
19 #import "chrome/browser/ui/cocoa/find_bar/find_bar_text_field_cell.h"
20 #import "chrome/browser/ui/cocoa/image_button_cell.h"
21 #import "chrome/browser/ui/cocoa/nsview_additions.h"
22 #import "chrome/browser/ui/cocoa/tabs/tab_strip_controller.h"
23 #include "chrome/browser/ui/find_bar/find_bar_controller.h"
24 #include "chrome/browser/ui/find_bar/find_tab_helper.h"
25 #include "content/public/browser/render_view_host.h"
26 #include "content/public/browser/web_contents.h"
27 #import "third_party/google_toolbox_for_mac/src/AppKit/GTMNSAnimation+Duration.h"
28 #import "ui/base/cocoa/find_pasteboard.h"
29 #import "ui/base/cocoa/focus_tracker.h"
31 using content::NativeWebKeyboardEvent;
33 const float kFindBarOpenDuration = 0.2;
34 const float kFindBarCloseDuration = 0.15;
35 const float kFindBarMoveDuration = 0.15;
36 const float kRightEdgeOffset = 25;
39 @interface FindBarCocoaController (PrivateMethods) <NSAnimationDelegate>
40 // Returns the appropriate frame for a hidden find bar.
41 - (NSRect)hiddenFindBarFrame;
43 // Animates the given |view| to the given |endFrame| within |duration| seconds.
44 // Returns a new NSViewAnimation.
45 - (NSViewAnimation*)createAnimationForView:(NSView*)view
46                                    toFrame:(NSRect)endFrame
47                                   duration:(float)duration;
49 // Sets the frame of |findBarView_|.  |duration| is ignored if |animate| is NO.
50 - (void)setFindBarFrame:(NSRect)endFrame
51                 animate:(BOOL)animate
52                duration:(float)duration;
54 // Returns the horizontal position the FindBar should use in order to avoid
55 // overlapping with the current find result, if there's one.
56 - (float)findBarHorizontalPosition;
58 // Adjusts the horizontal position if necessary to avoid overlapping with the
59 // current find result.
60 - (void)moveFindBarIfNecessary:(BOOL)animate;
62 // Puts |text| into the find bar and enables the buttons, but doesn't start a
63 // new search for |text|.
64 - (void)prepopulateText:(NSString*)text;
66 // Clears the find results for all tabs in browser associated with this find
67 // bar. If |suppressPboardUpdateActions_| is true then the current tab is not
68 // cleared.
69 - (void)clearFindResultsForCurrentBrowser;
71 - (BrowserWindowController*)browserWindowController;
72 @end
74 @implementation FindBarCocoaController
76 @synthesize findBarView = findBarView_;
78 - (id)initWithBrowser:(Browser*)browser {
79   if ((self = [super initWithNibName:@"FindBar"
80                               bundle:base::mac::FrameworkBundle()])) {
81     [[NSNotificationCenter defaultCenter]
82         addObserver:self
83            selector:@selector(findPboardUpdated:)
84                name:kFindPasteboardChangedNotification
85              object:[FindPasteboard sharedInstance]];
86     browser_ = browser;
87   }
88   return self;
91 - (void)dealloc {
92   // All animations should have been explicitly stopped before a tab is closed.
93   DCHECK(!showHideAnimation_.get());
94   DCHECK(!moveAnimation_.get());
95   [[NSNotificationCenter defaultCenter] removeObserver:self];
96   [super dealloc];
99 - (void)setFindBarBridge:(FindBarBridge*)findBarBridge {
100   DCHECK(!findBarBridge_);  // should only be called once.
101   findBarBridge_ = findBarBridge;
104 - (void)awakeFromNib {
105   [[closeButton_ cell] setImageID:IDR_CLOSE_1
106                    forButtonState:image_button_cell::kDefaultState];
107   [[closeButton_ cell] setImageID:IDR_CLOSE_1_H
108                    forButtonState:image_button_cell::kHoverState];
109   [[closeButton_ cell] setImageID:IDR_CLOSE_1_P
110                    forButtonState:image_button_cell::kPressedState];
111   [[closeButton_ cell] setImageID:IDR_CLOSE_1
112                    forButtonState:image_button_cell::kDisabledState];
114   [findBarView_ setFrame:[self hiddenFindBarFrame]];
115   defaultWidth_ = NSWidth([findBarView_ frame]);
117   [self prepopulateText:[[FindPasteboard sharedInstance] findText]];
120 - (IBAction)close:(id)sender {
121   if (findBarBridge_)
122     findBarBridge_->GetFindBarController()->EndFindSession(
123         FindBarController::kKeepSelectionOnPage,
124         FindBarController::kKeepResultsInFindBox);
126   // Turn off hover state on close button else the button will remain
127   // hovered when we bring the find bar back up.
128   // crbug.com/227424
129   [[closeButton_ cell] setIsMouseInside:NO];
132 - (IBAction)previousResult:(id)sender {
133   if (findBarBridge_) {
134     FindTabHelper* findTabHelper = FindTabHelper::FromWebContents(
135         findBarBridge_->GetFindBarController()->web_contents());
136     findTabHelper->StartFinding(
137         base::SysNSStringToUTF16([findText_ stringValue]),
138         false, false);
139   }
142 - (IBAction)nextResult:(id)sender {
143   if (findBarBridge_) {
144     FindTabHelper* findTabHelper = FindTabHelper::FromWebContents(
145         findBarBridge_->GetFindBarController()->web_contents());
146     findTabHelper->StartFinding(
147         base::SysNSStringToUTF16([findText_ stringValue]),
148         true, false);
149   }
152 - (void)findPboardUpdated:(NSNotification*)notification {
153   if (!suppressPboardUpdateActions_)
154     [self prepopulateText:[[FindPasteboard sharedInstance] findText]];
155   [self clearFindResultsForCurrentBrowser];
158 - (void)positionFindBarViewAtMaxY:(CGFloat)maxY maxWidth:(CGFloat)maxWidth {
159   NSView* containerView = [self view];
160   CGFloat containerHeight = NSHeight([containerView frame]);
161   CGFloat containerWidth = std::min(maxWidth, defaultWidth_);
163   // Adjust where we'll actually place the find bar.
164   maxY += [containerView cr_lineWidth];
165   maxY_ = maxY;
166   CGFloat x = [self findBarHorizontalPosition];
167   NSRect newFrame = NSMakeRect(x, maxY - containerHeight,
168                                containerWidth, containerHeight);
170   if (moveAnimation_.get() != nil) {
171     NSRect frame = [containerView frame];
172     [moveAnimation_ stopAnimation];
173     // Restore to the X position before the animation was stopped. The Y
174     // position is immediately adjusted.
175     frame.origin.y = newFrame.origin.y;
176     [containerView setFrame:frame];
177     moveAnimation_.reset([self createAnimationForView:containerView
178                                               toFrame:newFrame
179                                              duration:kFindBarMoveDuration]);
180   } else {
181     [containerView setFrame:newFrame];
182   }
185 - (BOOL)isOffTheRecordProfile {
186   return browser_ && browser_->profile() &&
187          browser_->profile()->IsOffTheRecord();
190 // NSControl delegate method.
191 - (void)controlTextDidChange:(NSNotification*)aNotification {
192   if (!findBarBridge_)
193     return;
195   content::WebContents* webContents =
196       findBarBridge_->GetFindBarController()->web_contents();
197   if (!webContents)
198     return;
199   FindTabHelper* findTabHelper = FindTabHelper::FromWebContents(webContents);
201   NSString* findText = [findText_ stringValue];
202   if (![self isOffTheRecordProfile]) {
203     base::AutoReset<BOOL> suppressReset(&suppressPboardUpdateActions_, YES);
204     [[FindPasteboard sharedInstance] setFindText:findText];
205   }
207   if ([findText length] > 0) {
208     findTabHelper->
209         StartFinding(base::SysNSStringToUTF16(findText), true, false);
210   } else {
211     // The textbox is empty so we reset.
212     findTabHelper->StopFinding(FindBarController::kClearSelectionOnPage);
213     [self updateUIForFindResult:findTabHelper->find_result()
214                        withText:base::string16()];
215   }
218 // NSControl delegate method
219 - (BOOL)control:(NSControl*)control
220     textView:(NSTextView*)textView
221     doCommandBySelector:(SEL)command {
222   if (command == @selector(insertNewline:)) {
223     // Pressing Return
224     NSEvent* event = [NSApp currentEvent];
226     if ([event modifierFlags] & NSShiftKeyMask)
227       [previousButton_ performClick:nil];
228     else
229       [nextButton_ performClick:nil];
231     return YES;
232   } else if (command == @selector(insertLineBreak:)) {
233     // Pressing Ctrl-Return
234     if (findBarBridge_) {
235       findBarBridge_->GetFindBarController()->EndFindSession(
236           FindBarController::kActivateSelectionOnPage,
237           FindBarController::kClearResultsInFindBox);
238     }
239     return YES;
240   } else if (command == @selector(pageUp:) ||
241              command == @selector(pageUpAndModifySelection:) ||
242              command == @selector(scrollPageUp:) ||
243              command == @selector(pageDown:) ||
244              command == @selector(pageDownAndModifySelection:) ||
245              command == @selector(scrollPageDown:) ||
246              command == @selector(scrollToBeginningOfDocument:) ||
247              command == @selector(scrollToEndOfDocument:) ||
248              command == @selector(moveUp:) ||
249              command == @selector(moveDown:)) {
250     content::WebContents* web_contents =
251         findBarBridge_->GetFindBarController()->web_contents();
252     if (!web_contents)
253       return NO;
255     // Sanity-check to make sure we got a keyboard event.
256     NSEvent* event = [NSApp currentEvent];
257     if ([event type] != NSKeyDown && [event type] != NSKeyUp)
258       return NO;
260     // Forward the event to the renderer.
261     // TODO(rohitrao): Should this call -[BaseView keyEvent:]?  Is there code in
262     // that function that we want to keep or avoid? Calling
263     // |ForwardKeyboardEvent()| directly ignores edit commands, which breaks
264     // cmd-up/down if we ever decide to include |moveToBeginningOfDocument:| in
265     // the list above.
266     content::RenderViewHost* render_view_host =
267         web_contents->GetRenderViewHost();
268     render_view_host->ForwardKeyboardEvent(NativeWebKeyboardEvent(event));
269     return YES;
270   }
272   return NO;
275 // Methods from FindBar
276 - (void)showFindBar:(BOOL)animate {
277   // Save the currently-focused view.  |findBarView_| is in the view
278   // hierarchy by now.  showFindBar can be called even when the
279   // findbar is already open, so do not overwrite an already saved
280   // view.
281   if (!focusTracker_.get())
282     focusTracker_.reset(
283         [[FocusTracker alloc] initWithWindow:[findBarView_ window]]);
285   // The browser window might have changed while the FindBar was hidden.
286   // Update its position now.
287   [[self browserWindowController] layoutSubviews];
289   // Move to the correct horizontal position first, to prevent the FindBar
290   // from jumping around when switching tabs.
291   // Prevent jumping while the FindBar is animating (hiding, then showing) too.
292   if (![self isFindBarVisible])
293     [self moveFindBarIfNecessary:NO];
295   // Animate the view into place.
296   NSRect frame = [findBarView_ frame];
297   frame.origin = NSZeroPoint;
298   [self setFindBarFrame:frame animate:animate duration:kFindBarOpenDuration];
301 - (void)hideFindBar:(BOOL)animate {
302   NSRect frame = [self hiddenFindBarFrame];
303   [self setFindBarFrame:frame animate:animate duration:kFindBarCloseDuration];
306 - (void)stopAnimation {
307   if (showHideAnimation_.get()) {
308     [showHideAnimation_ stopAnimation];
309     showHideAnimation_.reset(nil);
310   }
311   if (moveAnimation_.get()) {
312     [moveAnimation_ stopAnimation];
313     moveAnimation_.reset(nil);
314   }
317 - (void)setFocusAndSelection {
318   [[findText_ window] makeFirstResponder:findText_];
320   // Enable the buttons if the find text is non-empty.
321   BOOL buttonsEnabled = ([[findText_ stringValue] length] > 0) ? YES : NO;
322   [previousButton_ setEnabled:buttonsEnabled];
323   [nextButton_ setEnabled:buttonsEnabled];
326 - (void)restoreSavedFocus {
327   if (!(focusTracker_.get() &&
328         [focusTracker_ restoreFocusInWindow:[findBarView_ window]])) {
329     // Fall back to giving focus to the tab contents.
330     findBarBridge_->GetFindBarController()->web_contents()->Focus();
331   }
332   focusTracker_.reset(nil);
335 - (void)setFindText:(NSString*)findText
336       selectedRange:(const NSRange&)selectedRange {
337   [findText_ setStringValue:findText];
339   if (![self isOffTheRecordProfile]) {
340     // Make sure the text in the find bar always ends up in the find pasteboard
341     // (and, via notifications, in the other find bars too).
342     base::AutoReset<BOOL> suppressReset(&suppressPboardUpdateActions_, YES);
343     [[FindPasteboard sharedInstance] setFindText:findText];
344   }
346   NSText* editor = [findText_ currentEditor];
347   if (selectedRange.location != NSNotFound)
348     [editor setSelectedRange:selectedRange];
351 - (NSString*)findText {
352   return [findText_ stringValue];
355 - (NSRange)selectedRange {
356   NSText* editor = [findText_ currentEditor];
357   return (editor != nil) ? [editor selectedRange] : NSMakeRange(NSNotFound, 0);
360 - (NSString*)matchCountText {
361   return [[findText_ findBarTextFieldCell] resultsString];
364 - (void)updateFindBarForChangedWebContents {
365   content::WebContents* contents =
366       findBarBridge_->GetFindBarController()->web_contents();
367   if (!contents)
368     return;
369   FindTabHelper* findTabHelper = FindTabHelper::FromWebContents(contents);
371   // If the find UI is visible but the results are cleared then also clear
372   // the results label and update the buttons.
373   if (findTabHelper->find_ui_active() &&
374       findTabHelper->previous_find_text().empty()) {
375     BOOL buttonsEnabled = [[findText_ stringValue] length] > 0 ? YES : NO;
376     [previousButton_ setEnabled:buttonsEnabled];
377     [nextButton_ setEnabled:buttonsEnabled];
378     [[findText_ findBarTextFieldCell] clearResults];
379   }
382 - (void)clearResults:(const FindNotificationDetails&)results {
383   // Just call updateUIForFindResult, which will take care of clearing
384   // the search text and the results label.
385   [self updateUIForFindResult:results withText:base::string16()];
388 - (void)updateUIForFindResult:(const FindNotificationDetails&)result
389                      withText:(const base::string16&)findText {
390   // If we don't have any results and something was passed in, then
391   // that means someone pressed Cmd-G while the Find box was
392   // closed. In that case we need to repopulate the Find box with what
393   // was passed in.
394   if ([[findText_ stringValue] length] == 0 && !findText.empty()) {
395     [findText_ setStringValue:base::SysUTF16ToNSString(findText)];
396     [findText_ selectText:self];
397   }
399   // Make sure Find Next and Find Previous are enabled if we found any matches.
400   BOOL buttonsEnabled = result.number_of_matches() > 0;
401   [previousButton_ setEnabled:buttonsEnabled];
402   [nextButton_ setEnabled:buttonsEnabled];
404   // Update the results label.
405   BOOL validRange = result.active_match_ordinal() != -1 &&
406                     result.number_of_matches() != -1;
407   NSString* searchString = [findText_ stringValue];
408   if ([searchString length] > 0 && validRange) {
409     [[findText_ findBarTextFieldCell]
410         setActiveMatch:result.active_match_ordinal()
411                     of:result.number_of_matches()];
412   } else {
413     // If there was no text entered, we don't show anything in the results area.
414     [[findText_ findBarTextFieldCell] clearResults];
415   }
417   [findText_ resetFieldEditorFrameIfNeeded];
419   // If we found any results, reset the focus tracker, so we always
420   // restore focus to the tab contents.
421   if (result.number_of_matches() > 0)
422     focusTracker_.reset(nil);
424   // Adjust the FindBar position, even when there are no matches (so that it
425   // goes back to the default position, if required).
426   [self moveFindBarIfNecessary:[self isFindBarVisible]];
429 - (BOOL)isFindBarVisible {
430   // Find bar is visible if any part of it is on the screen.
431   return NSIntersectsRect([[self view] bounds], [findBarView_ frame]);
434 - (BOOL)isFindBarAnimating {
435   return (showHideAnimation_.get() != nil) || (moveAnimation_.get() != nil);
438 // NSAnimation delegate methods.
439 - (void)animationDidEnd:(NSAnimation*)animation {
440   // Autorelease the animations (cannot use release because the animation object
441   // is still on the stack.
442   if (animation == showHideAnimation_.get()) {
443     [showHideAnimation_.release() autorelease];
444   } else if (animation == moveAnimation_.get()) {
445     [moveAnimation_.release() autorelease];
446   } else {
447     NOTREACHED();
448   }
450   // If the find bar is not visible, make it actually hidden, so it'll no longer
451   // respond to key events.
452   [findBarView_ setHidden:![self isFindBarVisible]];
453   [[self browserWindowController] onFindBarVisibilityChanged];
456 - (gfx::Point)findBarWindowPosition {
457   gfx::Rect viewRect(NSRectToCGRect([[self view] frame]));
458   // Convert Cocoa coordinates (Y growing up) to Y growing down.
459   // Offset from |maxY_|, which represents the content view's top, instead
460   // of from the superview, which represents the whole browser window.
461   viewRect.set_y(maxY_ - viewRect.bottom());
462   return viewRect.origin();
465 - (int)findBarWidth {
466   return NSWidth([[self view] frame]);
469 @end
471 @implementation FindBarCocoaController (PrivateMethods)
473 - (NSRect)hiddenFindBarFrame {
474   NSRect frame = [findBarView_ frame];
475   NSRect containerBounds = [[self view] bounds];
476   frame.origin = NSMakePoint(NSMinX(containerBounds), NSMaxY(containerBounds));
477   return frame;
480 - (NSViewAnimation*)createAnimationForView:(NSView*)view
481                                    toFrame:(NSRect)endFrame
482                                   duration:(float)duration {
483   NSDictionary* dict = [NSDictionary dictionaryWithObjectsAndKeys:
484       view, NSViewAnimationTargetKey,
485       [NSValue valueWithRect:endFrame], NSViewAnimationEndFrameKey, nil];
487   NSViewAnimation* animation =
488       [[NSViewAnimation alloc]
489         initWithViewAnimations:[NSArray arrayWithObjects:dict, nil]];
490   [animation gtm_setDuration:duration
491                    eventMask:NSLeftMouseUpMask];
492   [animation setDelegate:self];
493   [animation startAnimation];
494   return animation;
497 - (void)setFindBarFrame:(NSRect)endFrame
498                 animate:(BOOL)animate
499                duration:(float)duration {
500   // Save the current frame.
501   NSRect startFrame = [findBarView_ frame];
503   // Stop any existing animations.
504   [showHideAnimation_ stopAnimation];
506   if (!animate) {
507     [findBarView_ setFrame:endFrame];
508     [findBarView_ setHidden:![self isFindBarVisible]];
509     [[self browserWindowController] onFindBarVisibilityChanged];
510     showHideAnimation_.reset(nil);
511     return;
512   }
514   // If animating, ensure that the find bar is not hidden. Hidden status will be
515   // updated at the end of the animation.
516   [findBarView_ setHidden:NO];
517   //[[self browserWindowController] onFindBarVisibilityChanged];
519   // Reset the frame to what was saved above.
520   [findBarView_ setFrame:startFrame];
522   [[self browserWindowController] onFindBarVisibilityChanged];
524   showHideAnimation_.reset([self createAnimationForView:findBarView_
525                                                 toFrame:endFrame
526                                                duration:duration]);
529 - (float)findBarHorizontalPosition {
530   // Get the rect of the FindBar.
531   NSView* view = [self view];
532   NSRect frame = [view frame];
533   gfx::Rect viewRect(NSRectToCGRect(frame));
535   if (!findBarBridge_ || !findBarBridge_->GetFindBarController())
536     return frame.origin.x;
537   content::WebContents* contents =
538       findBarBridge_->GetFindBarController()->web_contents();
539   if (!contents)
540     return frame.origin.x;
542   // Get the size of the container.
543   gfx::Rect containerRect(contents->GetContainerBounds().size());
545   // Position the FindBar on the top right corner.
546   viewRect.set_x(
547       containerRect.width() - viewRect.width() - kRightEdgeOffset);
548   // Convert from Cocoa coordinates (Y growing up) to Y growing down.
549   // Notice that the view frame's Y offset is relative to the whole window,
550   // while GetLocationForFindbarView() expects it relative to the
551   // content's boundaries. |maxY_| has the correct placement in Cocoa coords,
552   // so we just have to invert the Y coordinate.
553   viewRect.set_y(maxY_ - viewRect.bottom());
555   // Get the rect of the current find result, if there is one.
556   const FindNotificationDetails& findResult =
557       FindTabHelper::FromWebContents(contents)->find_result();
558   if (findResult.number_of_matches() == 0)
559     return viewRect.x();
560   gfx::Rect selectionRect(findResult.selection_rect());
562   // Adjust |view_rect| to avoid the |selection_rect| within |container_rect|.
563   gfx::Rect newPos = FindBarController::GetLocationForFindbarView(
564       viewRect, containerRect, selectionRect);
566   return newPos.x();
569 - (void)moveFindBarIfNecessary:(BOOL)animate {
570   // Don't animate during tests.
571   if (FindBarBridge::disable_animations_during_testing_)
572     animate = NO;
574   NSView* view = [self view];
575   NSRect frame = [view frame];
576   float x = [self findBarHorizontalPosition];
577   if (frame.origin.x == x)
578     return;
580   if (animate) {
581     [moveAnimation_ stopAnimation];
582     // Restore to the position before the animation was stopped.
583     [view setFrame:frame];
584     frame.origin.x = x;
585     moveAnimation_.reset([self createAnimationForView:view
586                                               toFrame:frame
587                                              duration:kFindBarMoveDuration]);
588   } else {
589     frame.origin.x = x;
590     [view setFrame:frame];
591   }
594 - (void)prepopulateText:(NSString*)text {
595   [self setFindText:text selectedRange:NSMakeRange(NSNotFound, 0)];
597   // Has to happen after |ClearResults()| above.
598   BOOL buttonsEnabled = [text length] > 0 ? YES : NO;
599   [previousButton_ setEnabled:buttonsEnabled];
600   [nextButton_ setEnabled:buttonsEnabled];
603 - (void)clearFindResultsForCurrentBrowser {
604   if (!browser_)
605     return;
607   content::WebContents* activeWebContents =
608       findBarBridge_->GetFindBarController()->web_contents();
610   TabStripModel* tabStripModel = browser_->tab_strip_model();
611   for (int i = 0; i < tabStripModel->count(); ++i) {
612     content::WebContents* webContents = tabStripModel->GetWebContentsAt(i);
613     if (suppressPboardUpdateActions_ && activeWebContents == webContents)
614       continue;
615     FindTabHelper* findTabHelper =
616         FindTabHelper::FromWebContents(webContents);
617     findTabHelper->StopFinding(FindBarController::kClearSelectionOnPage);
618     findBarBridge_->ClearResults(findTabHelper->find_result());
619   }
622 - (BrowserWindowController*)browserWindowController {
623   if (!browser_)
624     return nil;
625   return [BrowserWindowController
626       browserWindowControllerForWindow:browser_->window()->GetNativeWindow()];
629 @end