Commit a2bc20da authored by Nico Weber's avatar Nico Weber Committed by Commit Bot

mac: Remove remaining LocationBarDecoration subclasses.

Bug: 832676
Change-Id: Icacc8c67dea39708ff945ac564d39ed94727648c
Reviewed-on: https://chromium-review.googlesource.com/1241657
Commit-Queue: Robert Sesek <rsesek@chromium.org>
Reviewed-by: default avatarRobert Sesek <rsesek@chromium.org>
Cr-Commit-Position: refs/heads/master@{#593772}
parent a15736ca
...@@ -158,16 +158,10 @@ jumbo_split_static_library("ui") { ...@@ -158,16 +158,10 @@ jumbo_split_static_library("ui") {
"cocoa/location_bar/autocomplete_text_field_cell.mm", "cocoa/location_bar/autocomplete_text_field_cell.mm",
"cocoa/location_bar/autocomplete_text_field_editor.h", "cocoa/location_bar/autocomplete_text_field_editor.h",
"cocoa/location_bar/autocomplete_text_field_editor.mm", "cocoa/location_bar/autocomplete_text_field_editor.mm",
"cocoa/location_bar/bubble_decoration.h",
"cocoa/location_bar/bubble_decoration.mm",
"cocoa/location_bar/image_decoration.h",
"cocoa/location_bar/image_decoration.mm",
"cocoa/location_bar/location_bar_decoration.h", "cocoa/location_bar/location_bar_decoration.h",
"cocoa/location_bar/location_bar_decoration.mm", "cocoa/location_bar/location_bar_decoration.mm",
"cocoa/location_bar/location_bar_view_mac.h", "cocoa/location_bar/location_bar_view_mac.h",
"cocoa/location_bar/location_bar_view_mac.mm", "cocoa/location_bar/location_bar_view_mac.mm",
"cocoa/location_bar/zoom_decoration.h",
"cocoa/location_bar/zoom_decoration.mm",
"cocoa/main_menu_item.h", "cocoa/main_menu_item.h",
"cocoa/menu_button.h", "cocoa/menu_button.h",
"cocoa/menu_button.mm", "cocoa/menu_button.mm",
......
// Copyright (c) 2011 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_LOCATION_BAR_BUBBLE_DECORATION_H_
#define CHROME_BROWSER_UI_COCOA_LOCATION_BAR_BUBBLE_DECORATION_H_
#import <Cocoa/Cocoa.h>
#include "base/gtest_prod_util.h"
#include "base/mac/scoped_nsobject.h"
#include "base/macros.h"
#include "chrome/browser/ui/cocoa/location_bar/location_bar_decoration.h"
// Draws an outlined rounded rect, with an optional image to the left
// and an optional text label to the right.
class BubbleDecoration : public LocationBarDecoration {
public:
BubbleDecoration();
~BubbleDecoration() override;
// Setup the drawing parameters.
NSImage* GetImage();
virtual NSColor* GetBackgroundBorderColor() = 0;
void SetImage(NSImage* image);
void SetLabel(NSString* label);
void SetTextColor(NSColor* text_color);
void SetFont(NSFont* font);
void SetRetinaBaselineOffset(CGFloat offset);
// Implement |LocationBarDecoration|.
CGFloat GetWidthForSpace(CGFloat width) override;
NSRect GetBackgroundFrame(NSRect frame) override;
void DrawInFrame(NSRect frame, NSView* control_view) override;
NSRect GetTrackingFrame(NSRect frame) override;
NSFont* GetFont() const override;
protected:
// Helper returning bubble width for the given |image| and |label|
// assuming |font_| (for sizing text). Arguments can be nil.
CGFloat GetWidthForImageAndLabel(NSImage* image, NSString* label);
// Helper to return where the image is drawn, for subclasses to drag
// from. |frame| is the decoration's frame in the containing cell.
NSRect GetImageRectInFrame(NSRect frame);
// Returns the text color when the theme is dark.
virtual NSColor* GetDarkModeTextColor();
// Returns false if the |label_| is nil or empty.
bool HasLabel() const;
// Image drawn in the left side of the bubble.
base::scoped_nsobject<NSImage> image_;
// Label to draw to right of image. Can be |nil|.
base::scoped_nsobject<NSString> label_;
// Contains attribute for drawing |label_|.
base::scoped_nsobject<NSMutableDictionary> attributes_;
private:
// Contains any Retina-only baseline adjustment for |label_|.
CGFloat retina_baseline_offset_;
DISALLOW_COPY_AND_ASSIGN(BubbleDecoration);
};
#endif // CHROME_BROWSER_UI_COCOA_LOCATION_BAR_BUBBLE_DECORATION_H_
// Copyright (c) 2010 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.
#include <cmath>
#import "chrome/browser/ui/cocoa/location_bar/bubble_decoration.h"
#include "base/logging.h"
#include "base/mac/foundation_util.h"
#include "chrome/browser/ui/cocoa/l10n_util.h"
#import "chrome/browser/ui/cocoa/themed_window.h"
#include "skia/ext/skia_utils_mac.h"
#import "ui/base/cocoa/nsview_additions.h"
#include "ui/base/material_design/material_design_controller.h"
#include "ui/gfx/scoped_ns_graphics_context_save_gstate_mac.h"
namespace {
// This is used to increase the left padding of this decoration.
const CGFloat kLeftSidePadding = 5.0;
// Padding between the icon/label and bubble edges.
const CGFloat kBubblePadding = 7.0;
const CGFloat kImageOnlyPadding = 10.0;
// Padding between the icon and label.
const CGFloat kIconLabelPadding = 4.0;
// Inset for the image frame.
const CGFloat kImageFrameYInset = 4.0;
// Inset for the background frame.
const CGFloat kBackgroundFrameYInset = 2.0;
// Margin for the background frame.
const CGFloat kBackgroundFrameXMargin = 1.0;
} // namespace
BubbleDecoration::BubbleDecoration() : retina_baseline_offset_(0) {
attributes_.reset([[NSMutableDictionary alloc] init]);
[attributes_ setObject:LocationBarDecoration::GetFont()
forKey:NSFontAttributeName];
}
BubbleDecoration::~BubbleDecoration() {
}
CGFloat BubbleDecoration::GetWidthForImageAndLabel(NSImage* image,
NSString* label) {
bool has_label = label && label.length;
if (!image && !has_label)
return kOmittedWidth;
const CGFloat image_width = image ? [image size].width : 0.0;
if (!has_label)
return kImageOnlyPadding + image_width;
// The bubble needs to take up an integral number of pixels.
// Generally -sizeWithAttributes: seems to overestimate rather than
// underestimate, so floor() seems to work better.
const CGFloat label_width =
std::floor([label sizeWithAttributes:attributes_].width);
return kBubblePadding + image_width + kIconLabelPadding + label_width +
DividerPadding() + kLeftSidePadding;
}
NSRect BubbleDecoration::GetImageRectInFrame(NSRect frame) {
NSRect image_rect = NSInsetRect(frame, 0.0, kImageFrameYInset);
if (image_) {
// Center the image vertically.
const NSSize image_size = [image_ size];
image_rect.origin.y +=
std::floor((NSHeight(image_rect) - image_size.height) / 2.0);
image_rect.origin.x += kLeftSidePadding;
image_rect.size = image_size;
if (cocoa_l10n_util::ShouldDoExperimentalRTLLayout()) {
image_rect.origin.x =
NSMaxX(frame) - NSWidth(image_rect) - kLeftSidePadding;
}
}
return image_rect;
}
NSColor* BubbleDecoration::GetDarkModeTextColor() {
return skia::SkColorToSRGBNSColor(kMaterialDarkModeTextColor);
}
CGFloat BubbleDecoration::GetWidthForSpace(CGFloat width) {
const CGFloat all_width = GetWidthForImageAndLabel(image_, label_);
if (all_width <= width)
return all_width;
const CGFloat image_width = GetWidthForImageAndLabel(image_, nil);
if (image_width <= width)
return image_width;
return kOmittedWidth;
}
NSRect BubbleDecoration::GetBackgroundFrame(NSRect frame) {
NSRect background_frame = NSInsetRect(frame, 0.0, kBackgroundFrameYInset);
const CGFloat divider_padding = HasLabel() ? DividerPadding() : 0.0;
if (cocoa_l10n_util::ShouldDoExperimentalRTLLayout()) {
background_frame.origin.x += divider_padding - kBackgroundFrameXMargin;
background_frame.size.width -= divider_padding + kBackgroundFrameXMargin;
} else {
background_frame.origin.x += kBackgroundFrameXMargin;
background_frame.size.width -= divider_padding + kBackgroundFrameXMargin;
}
return background_frame;
}
void BubbleDecoration::DrawInFrame(NSRect frame, NSView* control_view) {
const NSRect decoration_frame = NSInsetRect(frame, 0.0, kImageFrameYInset);
CGFloat text_left_offset = NSMinX(decoration_frame);
CGFloat text_right_offset = NSMaxX(decoration_frame);
const BOOL is_rtl = cocoa_l10n_util::ShouldDoExperimentalRTLLayout();
if (image_) {
NSRect image_rect = GetImageRectInFrame(frame);
[image_ drawInRect:image_rect
fromRect:NSZeroRect // Entire image
operation:NSCompositeSourceOver
fraction:1.0
respectFlipped:YES
hints:nil];
if (is_rtl)
text_right_offset = NSMinX(image_rect) - kIconLabelPadding;
else
text_left_offset = NSMaxX(image_rect) + kIconLabelPadding;
}
if (HasLabel()) {
// Draw the divider.
DrawDivider(control_view, decoration_frame, 1.0);
// Set the text color.
bool in_dark_mode = [[control_view window] inIncognitoModeWithSystemTheme];
NSColor* text_color =
in_dark_mode ? GetDarkModeTextColor() : GetBackgroundBorderColor();
SetTextColor(text_color);
NSRect text_rect = frame;
text_rect.origin.x = text_left_offset;
text_rect.size.width = text_right_offset - text_left_offset;
// Transform the coordinate system to adjust the baseline on Retina. This is
// the only way to get fractional adjustments.
gfx::ScopedNSGraphicsContextSaveGState saveGraphicsState;
CGFloat line_width = [control_view cr_lineWidth];
if (line_width < 1) {
NSAffineTransform* transform = [NSAffineTransform transform];
[transform translateXBy:0 yBy:retina_baseline_offset_];
[transform concat];
}
DrawLabel(label_, attributes_, text_rect);
}
}
NSRect BubbleDecoration::GetTrackingFrame(NSRect frame) {
NSRect tracking_frame = GetBackgroundFrame(frame);
// Include the divider width in the frame.
if (cocoa_l10n_util::ShouldDoExperimentalRTLLayout())
tracking_frame.origin.x -= 1;
else
tracking_frame.size.width += 1;
return tracking_frame;
}
NSFont* BubbleDecoration::GetFont() const {
return [attributes_ objectForKey:NSFontAttributeName];
}
NSImage* BubbleDecoration::GetImage() {
return image_;
}
void BubbleDecoration::SetImage(NSImage* image) {
image_.reset([image retain]);
}
void BubbleDecoration::SetLabel(NSString* label) {
// If the initializer was called with |nil|, then the code cannot
// process a label.
DCHECK(attributes_);
if (attributes_)
label_.reset([label copy]);
}
void BubbleDecoration::SetTextColor(NSColor* text_color) {
[attributes_ setObject:text_color forKey:NSForegroundColorAttributeName];
}
void BubbleDecoration::SetFont(NSFont* font) {
[attributes_ setObject:font forKey:NSFontAttributeName];
}
void BubbleDecoration::SetRetinaBaselineOffset(CGFloat offset) {
retina_baseline_offset_ = offset;
}
bool BubbleDecoration::HasLabel() const {
return label_ && [label_ length];
}
// Copyright (c) 2011 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_LOCATION_BAR_IMAGE_DECORATION_H_
#define CHROME_BROWSER_UI_COCOA_LOCATION_BAR_IMAGE_DECORATION_H_
#import "base/mac/scoped_nsobject.h"
#include "base/macros.h"
#include "chrome/browser/ui/cocoa/location_bar/location_bar_decoration.h"
// |LocationBarDecoration| which sizes and draws itself according to
// an |NSImage|.
class ImageDecoration : public LocationBarDecoration {
public:
ImageDecoration();
~ImageDecoration() override;
NSImage* GetImage();
void SetImage(NSImage* image);
// Returns the part of |frame| the image is drawn in.
NSRect GetDrawRectInFrame(NSRect frame);
// Implement |LocationBarDecoration|.
CGFloat GetWidthForSpace(CGFloat width) override;
void DrawInFrame(NSRect frame, NSView* control_view) override;
private:
base::scoped_nsobject<NSImage> image_;
DISALLOW_COPY_AND_ASSIGN(ImageDecoration);
};
#endif // CHROME_BROWSER_UI_COCOA_LOCATION_BAR_IMAGE_DECORATION_H_
// Copyright (c) 2010 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.
#include <cmath>
#import "chrome/browser/ui/cocoa/location_bar/image_decoration.h"
// The amount of horizontal padding around the image.
const CGFloat kImageHorizontalPadding = 10.0;
ImageDecoration::ImageDecoration() {
}
ImageDecoration::~ImageDecoration() {
}
NSImage* ImageDecoration::GetImage() {
return image_;
}
void ImageDecoration::SetImage(NSImage* image) {
image_.reset([image retain]);
}
NSRect ImageDecoration::GetDrawRectInFrame(NSRect frame) {
NSImage* image = GetImage();
if (!image)
return frame;
// Center the image within the frame.
const CGFloat delta_height = NSHeight(frame) - [image size].height;
const CGFloat y_inset = std::floor(delta_height / 2.0);
const CGFloat delta_width = NSWidth(frame) - [image size].width;
const CGFloat x_inset = std::floor(delta_width / 2.0);
return NSInsetRect(frame, x_inset, y_inset);
}
CGFloat ImageDecoration::GetWidthForSpace(CGFloat width) {
NSImage* image = GetImage();
if (image) {
const CGFloat image_width = [image size].width;
if (image_width <= width)
return image_width + kImageHorizontalPadding;
}
return kOmittedWidth;
}
void ImageDecoration::DrawInFrame(NSRect frame, NSView* control_view) {
[GetImage() drawInRect:GetDrawRectInFrame(frame)
fromRect:NSZeroRect // Entire image
operation:NSCompositeSourceOver
fraction:1.0
respectFlipped:YES
hints:nil];
}
// Copyright (c) 2010 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 <Cocoa/Cocoa.h>
#import "chrome/browser/ui/cocoa/location_bar/image_decoration.h"
#import "chrome/browser/ui/cocoa/test/cocoa_test_helper.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace {
class ImageDecorationTest : public CocoaTest {
public:
ImageDecoration decoration_;
};
TEST_F(ImageDecorationTest, SetGetImage) {
EXPECT_FALSE(decoration_.GetImage());
const NSSize kImageSize = NSMakeSize(20.0, 20.0);
base::scoped_nsobject<NSImage> image(
[[NSImage alloc] initWithSize:kImageSize]);
decoration_.SetImage(image);
EXPECT_EQ(decoration_.GetImage(), image);
decoration_.SetImage(nil);
EXPECT_FALSE(decoration_.GetImage());
}
TEST_F(ImageDecorationTest, GetWidthForSpace) {
const CGFloat kWide = 100.0;
const CGFloat kNarrow = 10.0;
const CGFloat kImageHorizontalPadding = 10.0;
// Decoration with no image is omitted.
EXPECT_EQ(decoration_.GetWidthForSpace(kWide),
LocationBarDecoration::kOmittedWidth);
const NSSize kImageSize = NSMakeSize(20.0, 20.0);
base::scoped_nsobject<NSImage> image(
[[NSImage alloc] initWithSize:kImageSize]);
// Decoration takes up the space of the image and the horizontal padding.
decoration_.SetImage(image);
EXPECT_EQ(decoration_.GetWidthForSpace(kWide),
kImageSize.width + kImageHorizontalPadding);
// If the image doesn't fit, decoration is omitted.
EXPECT_EQ(decoration_.GetWidthForSpace(kNarrow),
LocationBarDecoration::kOmittedWidth);
}
// TODO(shess): It would be nice to test mouse clicks and dragging,
// but those are hard because they require a real |owner|.
} // namespace
...@@ -26,8 +26,6 @@ ...@@ -26,8 +26,6 @@
class CommandUpdater; class CommandUpdater;
class LocationBarDecoration; class LocationBarDecoration;
class Profile; class Profile;
class ZoomDecoration;
class ZoomDecorationTest;
namespace { namespace {
class LocationBarViewMacTest; class LocationBarViewMacTest;
...@@ -159,8 +157,6 @@ class LocationBarViewMac : public LocationBar, ...@@ -159,8 +157,6 @@ class LocationBarViewMac : public LocationBar,
// Returns true if the location bar is dark. // Returns true if the location bar is dark.
bool IsLocationBarDark() const; bool IsLocationBarDark() const;
ZoomDecoration* zoom_decoration() const { return zoom_decoration_.get(); }
Browser* browser() const { return browser_; } Browser* browser() const { return browser_; }
// ZoomManagerObserver: // ZoomManagerObserver:
...@@ -174,7 +170,6 @@ class LocationBarViewMac : public LocationBar, ...@@ -174,7 +170,6 @@ class LocationBarViewMac : public LocationBar,
private: private:
friend class LocationBarViewMacTest; friend class LocationBarViewMacTest;
friend ZoomDecorationTest;
// Posts |notification| to the default notification center. // Posts |notification| to the default notification center.
void PostNotification(NSString* notification); void PostNotification(NSString* notification);
...@@ -185,10 +180,6 @@ class LocationBarViewMac : public LocationBar, ...@@ -185,10 +180,6 @@ class LocationBarViewMac : public LocationBar,
// tab contents state. // tab contents state.
bool RefreshContentSettingsDecorations(); bool RefreshContentSettingsDecorations();
// Updates the zoom decoration in the omnibox with the current zoom level.
// Returns whether any updates were made.
bool UpdateZoomDecoration(bool default_zoom_changed);
// Returns pointers to all of the LocationBarDecorations owned by this // Returns pointers to all of the LocationBarDecorations owned by this
// LocationBarViewMac. This helper function is used for positioning and // LocationBarViewMac. This helper function is used for positioning and
// re-positioning accessibility views. // re-positioning accessibility views.
...@@ -203,10 +194,6 @@ class LocationBarViewMac : public LocationBar, ...@@ -203,10 +194,6 @@ class LocationBarViewMac : public LocationBar,
AutocompleteTextField* field_; // owned by tab controller AutocompleteTextField* field_; // owned by tab controller
// A zoom icon at the end of the omnibox, which shows at non-standard zoom
// levels.
std::unique_ptr<ZoomDecoration> zoom_decoration_;
Browser* browser_; Browser* browser_;
// Used to change the visibility of the star decoration. // Used to change the visibility of the star decoration.
......
...@@ -28,7 +28,6 @@ ...@@ -28,7 +28,6 @@
#import "chrome/browser/ui/cocoa/l10n_util.h" #import "chrome/browser/ui/cocoa/l10n_util.h"
#import "chrome/browser/ui/cocoa/location_bar/autocomplete_text_field.h" #import "chrome/browser/ui/cocoa/location_bar/autocomplete_text_field.h"
#import "chrome/browser/ui/cocoa/location_bar/autocomplete_text_field_cell.h" #import "chrome/browser/ui/cocoa/location_bar/autocomplete_text_field_cell.h"
#import "chrome/browser/ui/cocoa/location_bar/zoom_decoration.h"
#import "chrome/browser/ui/cocoa/omnibox/omnibox_view_mac.h" #import "chrome/browser/ui/cocoa/omnibox/omnibox_view_mac.h"
#import "chrome/browser/ui/cocoa/toolbar/toolbar_controller.h" #import "chrome/browser/ui/cocoa/toolbar/toolbar_controller.h"
#include "chrome/browser/ui/content_settings/content_setting_bubble_model.h" #include "chrome/browser/ui/content_settings/content_setting_bubble_model.h"
...@@ -85,7 +84,6 @@ LocationBarViewMac::LocationBarViewMac(AutocompleteTextField* field, ...@@ -85,7 +84,6 @@ LocationBarViewMac::LocationBarViewMac(AutocompleteTextField* field,
ChromeOmniboxEditController(command_updater), ChromeOmniboxEditController(command_updater),
omnibox_view_(new OmniboxViewMac(this, profile, command_updater, field)), omnibox_view_(new OmniboxViewMac(this, profile, command_updater, field)),
field_(field), field_(field),
zoom_decoration_(new ZoomDecoration(this)),
browser_(browser), browser_(browser),
location_bar_visible_(true) { location_bar_visible_(true) {
edit_bookmarks_enabled_.Init( edit_bookmarks_enabled_.Init(
...@@ -211,7 +209,6 @@ bool LocationBarViewMac::IsContentSettingBubbleShowing(size_t index) { ...@@ -211,7 +209,6 @@ bool LocationBarViewMac::IsContentSettingBubbleShowing(size_t index) {
void LocationBarViewMac::SetEditable(bool editable) { void LocationBarViewMac::SetEditable(bool editable) {
[field_ setEditable:editable ? YES : NO]; [field_ setEditable:editable ? YES : NO];
UpdateBookmarkStarVisibility(); UpdateBookmarkStarVisibility();
UpdateZoomDecoration(/*default_zoom_changed=*/false);
Layout(); Layout();
} }
...@@ -220,12 +217,6 @@ bool LocationBarViewMac::IsEditable() { ...@@ -220,12 +217,6 @@ bool LocationBarViewMac::IsEditable() {
} }
void LocationBarViewMac::ZoomChangedForActiveTab(bool can_show_bubble) { void LocationBarViewMac::ZoomChangedForActiveTab(bool can_show_bubble) {
bool changed = UpdateZoomDecoration(/*default_zoom_changed=*/false);
if (changed)
OnDecorationsChanged();
if (can_show_bubble && zoom_decoration_->IsVisible())
zoom_decoration_->ShowBubble(YES);
} }
NSPoint LocationBarViewMac::GetBubblePointForDecoration( NSPoint LocationBarViewMac::GetBubblePointForDecoration(
...@@ -234,14 +225,6 @@ NSPoint LocationBarViewMac::GetBubblePointForDecoration( ...@@ -234,14 +225,6 @@ NSPoint LocationBarViewMac::GetBubblePointForDecoration(
} }
void LocationBarViewMac::OnDecorationsChanged() { void LocationBarViewMac::OnDecorationsChanged() {
// TODO(shess): The field-editor frame and cursor rects should not
// change, here.
std::vector<LocationBarDecoration*> decorations = GetDecorations();
for (auto* decoration : decorations)
UpdateAccessibilityView(decoration);
[field_ updateMouseTracking];
[field_ resetFieldEditorFrameIfNeeded];
[field_ setNeedsDisplay:YES];
} }
// TODO(shess): This function should over time grow to closely match // TODO(shess): This function should over time grow to closely match
...@@ -254,7 +237,6 @@ void LocationBarViewMac::Layout() { ...@@ -254,7 +237,6 @@ void LocationBarViewMac::Layout() {
// the constructor. I am still wrestling with how best to deal with // the constructor. I am still wrestling with how best to deal with
// right-hand decorations, which are not a static set. // right-hand decorations, which are not a static set.
[cell clearDecorations]; [cell clearDecorations];
[cell addTrailingDecoration:zoom_decoration_.get()];
// Get the keyword to use for keyword-search and hinting. // Get the keyword to use for keyword-search and hinting.
const base::string16 keyword = omnibox_view_->model()->keyword(); const base::string16 keyword = omnibox_view_->model()->keyword();
...@@ -288,7 +270,6 @@ void LocationBarViewMac::Update(const WebContents* contents) { ...@@ -288,7 +270,6 @@ void LocationBarViewMac::Update(const WebContents* contents) {
UpdateManagePasswordsIconAndBubble(); UpdateManagePasswordsIconAndBubble();
UpdateBookmarkStarVisibility(); UpdateBookmarkStarVisibility();
UpdateSaveCreditCardIcon(); UpdateSaveCreditCardIcon();
UpdateZoomDecoration(/*default_zoom_changed=*/false);
RefreshContentSettingsDecorations(); RefreshContentSettingsDecorations();
if (contents) { if (contents) {
omnibox_view_->OnTabChanged(contents); omnibox_view_->OnTabChanged(contents);
...@@ -339,19 +320,6 @@ WebContents* LocationBarViewMac::GetWebContents() { ...@@ -339,19 +320,6 @@ WebContents* LocationBarViewMac::GetWebContents() {
} }
void LocationBarViewMac::UpdatePageActionIcon(PageActionIconType type) { void LocationBarViewMac::UpdatePageActionIcon(PageActionIconType type) {
// TODO(https://crbug.com/788051): Return page action icons for updating here
// as update methods are migrated out of LocationBar to the
// PageActionIconContainer interface.
switch (type) {
case PageActionIconType::kFind:
// TODO(crbug/651643): Implement for mac.
NOTIMPLEMENTED();
break;
case PageActionIconType::kZoom:
UpdateZoomDecoration(/*default_zoom_changed=*/false);
OnChanged();
break;
}
} }
PageInfoVerboseType LocationBarViewMac::GetPageInfoVerboseType() const { PageInfoVerboseType LocationBarViewMac::GetPageInfoVerboseType() const {
...@@ -436,77 +404,19 @@ bool LocationBarViewMac::RefreshContentSettingsDecorations() { ...@@ -436,77 +404,19 @@ bool LocationBarViewMac::RefreshContentSettingsDecorations() {
return false; return false;
} }
bool LocationBarViewMac::UpdateZoomDecoration(bool default_zoom_changed) {
WebContents* web_contents = GetWebContents();
if (!web_contents)
return false;
return zoom_decoration_->UpdateIfNecessary(
zoom::ZoomController::FromWebContents(web_contents), default_zoom_changed,
IsLocationBarDark());
}
void LocationBarViewMac::UpdateAccessibilityView( void LocationBarViewMac::UpdateAccessibilityView(
LocationBarDecoration* decoration) { LocationBarDecoration* decoration) {
if (!decoration->IsVisible())
return;
// This uses |frame| instead of |bounds| because the accessibility views are
// parented to the toolbar.
NSRect apparent_frame =
[[field_ cell] frameForDecoration:decoration inFrame:[field_ frame]];
// This is a bit subtle:
// The decorations' accessibility views can become key to allow keyboard
// access to the location bar decorations, but Cocoa's automatic key view loop
// sorts by top-left coordinate. Since the omnibox's top-left coordinate is
// before its leading decorations, the omnibox would sort before its own
// leading decorations, which was logical but visually unintuitive. Therefore,
// for leading decorations, this method moves their frame to be "just before"
// the omnibox in automatic key view loop order, and gives them an apparent
// frame (see DecorationAccessibilityView) so that they still paint their
// focus rings at the right place.
//
// TODO(lgrey): This hack doesn't work in RTL layouts, but the layout of the
// omnibox is currently screwed up in RTL layouts anyway. See
// https://crbug.com/715627.
NSRect real_frame = apparent_frame;
int left_index = [[field_ cell] leadingDecorationIndex:decoration];
// If there are ever too many leading views, the fake x-coords might land
// before the button preceding the omnibox in the key view order. This
// threshold is just a guess.
DCHECK_LT(left_index, 10);
if (left_index != -1) {
CGFloat delta = left_index + 1;
real_frame.origin.x =
cocoa_l10n_util::ShouldDoExperimentalRTLLayout()
? NSMaxX([field_ frame]) + delta - NSWidth(real_frame)
: NSMinX([field_ frame]) - delta;
}
decoration->UpdateAccessibilityView(apparent_frame);
[decoration->GetAccessibilityView() setFrame:real_frame];
[decoration->GetAccessibilityView() setNeedsDisplayInRect:apparent_frame];
} }
std::vector<LocationBarDecoration*> LocationBarViewMac::GetDecorations() { std::vector<LocationBarDecoration*> LocationBarViewMac::GetDecorations() {
std::vector<LocationBarDecoration*> decorations; std::vector<LocationBarDecoration*> decorations;
// TODO(ellyjones): page actions and keyword hints are not included right
// now. Keyword hints have no useful tooltip (issue 752592), and page actions
// are likewise.
decorations.push_back(zoom_decoration_.get());
return decorations; return decorations;
} }
void LocationBarViewMac::OnDefaultZoomLevelChanged() { void LocationBarViewMac::OnDefaultZoomLevelChanged() {
if (UpdateZoomDecoration(/*default_zoom_changed=*/true))
OnDecorationsChanged();
} }
std::vector<NSView*> LocationBarViewMac::GetDecorationAccessibilityViews() { std::vector<NSView*> LocationBarViewMac::GetDecorationAccessibilityViews() {
std::vector<LocationBarDecoration*> decorations = GetDecorations();
std::vector<NSView*> views; std::vector<NSView*> views;
for (auto* decoration : decorations)
views.push_back(decoration->GetAccessibilityView());
return views; return views;
} }
// Copyright (c) 2012 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_LOCATION_BAR_ZOOM_DECORATION_H_
#define CHROME_BROWSER_UI_COCOA_LOCATION_BAR_ZOOM_DECORATION_H_
#import <Cocoa/Cocoa.h>
#include "base/macros.h"
#include "chrome/browser/ui/cocoa/location_bar/image_decoration.h"
class LocationBarViewMac;
class ZoomDecorationTest;
namespace zoom {
class ZoomController;
}
// Zoom icon at the end of the omnibox (close to page actions) when at a
// non-standard zoom level.
class ZoomDecoration : public ImageDecoration {
public:
explicit ZoomDecoration(LocationBarViewMac* owner);
~ZoomDecoration() override;
// Called when this decoration should show or hide itself in its most current
// state. Returns whether any updates were made.
bool UpdateIfNecessary(zoom::ZoomController* zoom_controller,
bool default_zoom_changed,
bool location_bar_is_dark);
// Shows the zoom bubble for this decoration. If |auto_close| is YES, then
// the bubble will automatically close after a fixed period of time.
// If a bubble is already showing, the |auto_close| timer is reset.
void ShowBubble(BOOL auto_close);
// Closes the zoom bubble.
void CloseBubble();
protected:
// Hides all UI associated with the zoom decoration.
// Virtual and protected for testing.
virtual void HideUI();
// Update UI associated with the zoom decoration.
// Virtual and protected for testing.
virtual void UpdateUI(zoom::ZoomController* zoom_controller,
NSString* tooltip_string,
bool location_bar_is_dark);
// Overridden from LocationBarDecoration:
const gfx::VectorIcon* GetMaterialVectorIcon() const override;
private:
friend ZoomDecorationTest;
bool IsAtDefaultZoom() const;
// Returns true when |bubble_| or the views zoom bubble exists.
bool IsBubbleShown() const;
// Virtual for testing.
virtual bool ShouldShowDecoration() const;
// LocationBarDecoration implementation.
AcceptsPress AcceptsMousePress() override;
bool OnMousePressed(NSRect frame, NSPoint location) override;
NSString* GetToolTip() override;
NSPoint GetBubblePointInFrame(NSRect frame) override;
// The control that owns this. Weak.
LocationBarViewMac* owner_;
// The string to show for a tooltip.
base::scoped_nsobject<NSString> tooltip_;
const gfx::VectorIcon* vector_icon_;
DISALLOW_COPY_AND_ASSIGN(ZoomDecoration);
};
#endif // CHROME_BROWSER_UI_COCOA_LOCATION_BAR_ZOOM_DECORATION_H_
// Copyright (c) 2012 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/location_bar/zoom_decoration.h"
#include "base/i18n/number_formatting.h"
#include "base/strings/string16.h"
#include "base/strings/string_number_conversions.h"
#include "chrome/app/chrome_command_ids.h"
#include "chrome/app/vector_icons/vector_icons.h"
#include "chrome/browser/ui/cocoa/browser_dialogs_views_mac.h"
#import "chrome/browser/ui/cocoa/browser_window_controller.h"
#import "chrome/browser/ui/cocoa/l10n_util.h"
#import "chrome/browser/ui/cocoa/location_bar/autocomplete_text_field.h"
#import "chrome/browser/ui/cocoa/location_bar/autocomplete_text_field_cell.h"
#import "chrome/browser/ui/cocoa/location_bar/location_bar_view_mac.h"
#import "chrome/browser/ui/cocoa/omnibox/omnibox_view_mac.h"
#include "chrome/grit/generated_resources.h"
#include "components/zoom/zoom_controller.h"
#include "ui/base/cocoa/cocoa_base_utils.h"
#include "ui/base/l10n/l10n_util_mac.h"
#import "ui/gfx/mac/coordinate_conversion.h"
ZoomDecoration::ZoomDecoration(LocationBarViewMac* owner)
: owner_(owner), vector_icon_(nullptr) {}
ZoomDecoration::~ZoomDecoration() {
CloseBubble();
}
bool ZoomDecoration::UpdateIfNecessary(zoom::ZoomController* zoom_controller,
bool default_zoom_changed,
bool location_bar_is_dark) {
if (!ShouldShowDecoration()) {
if (!IsVisible() && !IsBubbleShown())
return false;
HideUI();
return true;
}
BOOL old_visibility = IsVisible();
SetVisible(true);
base::string16 zoom_percent =
base::FormatPercent(zoom_controller->GetZoomPercent());
// There is no icon at the default zoom factor (100%), so don't display a
// tooltip either.
NSString* tooltip_string =
zoom_controller->IsAtDefaultZoom()
? @""
: l10n_util::GetNSStringF(IDS_TOOLTIP_ZOOM, zoom_percent);
if ([tooltip_ isEqualToString:tooltip_string] && !default_zoom_changed &&
old_visibility == IsVisible()) {
return false;
}
UpdateUI(zoom_controller, tooltip_string, location_bar_is_dark);
return true;
}
void ZoomDecoration::ShowBubble(BOOL auto_close) {
}
void ZoomDecoration::CloseBubble() {
chrome::CloseZoomBubbleViews();
}
void ZoomDecoration::HideUI() {
CloseBubble();
SetVisible(false);
}
void ZoomDecoration::UpdateUI(zoom::ZoomController* zoom_controller,
NSString* tooltip_string,
bool location_bar_is_dark) {
vector_icon_ = zoom_controller->GetZoomRelativeToDefault() ==
zoom::ZoomController::ZOOM_BELOW_DEFAULT_ZOOM
? &kZoomMinusIcon
: &kZoomPlusIcon;
SetImage(GetMaterialIcon(location_bar_is_dark));
tooltip_.reset([tooltip_string retain]);
chrome::RefreshZoomBubbleViews();
}
NSPoint ZoomDecoration::GetBubblePointInFrame(NSRect frame) {
return NSMakePoint(cocoa_l10n_util::ShouldDoExperimentalRTLLayout()
? NSMinX(frame)
: NSMaxX(frame),
NSMaxY(frame));
}
bool ZoomDecoration::IsAtDefaultZoom() const {
content::WebContents* web_contents = owner_->GetWebContents();
if (!web_contents)
return false;
zoom::ZoomController* zoomController =
zoom::ZoomController::FromWebContents(web_contents);
return zoomController && zoomController->IsAtDefaultZoom();
}
bool ZoomDecoration::IsBubbleShown() const {
return chrome::IsZoomBubbleViewsShown();
}
bool ZoomDecoration::ShouldShowDecoration() const {
return owner_->GetWebContents() != NULL &&
!owner_->GetToolbarModel()->input_in_progress() &&
(IsBubbleShown() || !IsAtDefaultZoom());
}
AcceptsPress ZoomDecoration::AcceptsMousePress() {
return AcceptsPress::ALWAYS;
}
bool ZoomDecoration::OnMousePressed(NSRect frame, NSPoint location) {
if (IsBubbleShown()) {
CloseBubble();
} else {
ShowBubble(false);
}
return true;
}
NSString* ZoomDecoration::GetToolTip() {
return tooltip_.get();
}
const gfx::VectorIcon* ZoomDecoration::GetMaterialVectorIcon() const {
return vector_icon_;
}
// Copyright (c) 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/location_bar/zoom_decoration.h"
#include "base/auto_reset.h"
#include "base/command_line.h"
#include "base/macros.h"
#include "base/run_loop.h"
#include "base/test/scoped_feature_list.h"
#include "base/threading/thread_task_runner_handle.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/browser_window.h"
#import "chrome/browser/ui/cocoa/browser_window_controller.h"
#import "chrome/browser/ui/cocoa/location_bar/location_bar_view_mac.h"
#include "chrome/browser/ui/cocoa/test/run_loop_testing.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "chrome/test/views/scoped_macviews_browser_mode.h"
#include "components/toolbar/test_toolbar_model.h"
#include "components/zoom/page_zoom.h"
#include "components/zoom/zoom_controller.h"
#include "content/public/browser/host_zoom_map.h"
#include "content/public/test/test_utils.h"
#include "ui/base/ui_base_features.h"
// TODO(crbug.com/630357): Remove parameterized testing for this class.
class ZoomDecorationTest : public InProcessBrowserTest,
public ::testing::WithParamInterface<bool> {
protected:
ZoomDecorationTest()
: InProcessBrowserTest(),
should_quit_on_zoom_(false) {
}
void SetUpOnMainThread() override {
zoom_subscription_ = content::HostZoomMap::GetDefaultForBrowserContext(
browser()->profile())->AddZoomLevelChangedCallback(
base::Bind(&ZoomDecorationTest::OnZoomChanged,
base::Unretained(this)));
}
void TearDownOnMainThread() override { zoom_subscription_.reset(); }
LocationBarViewMac* GetLocationBar() const {
BrowserWindowController* controller =
[BrowserWindowController browserWindowControllerForWindow:
browser()->window()->GetNativeWindow()];
return [controller locationBarBridge];
}
ZoomDecoration* GetZoomDecoration() const {
return GetLocationBar()->zoom_decoration_.get();
}
ZoomDecoration* GetZoomDecorationForBrowser(Browser* browser) const {
BrowserWindowController* controller =
[BrowserWindowController browserWindowControllerForWindow:
browser->window()->GetNativeWindow()];
return [controller locationBarBridge]->zoom_decoration_.get();
}
void Zoom(content::PageZoom zoom) {
content::WebContents* web_contents =
browser()->tab_strip_model()->GetActiveWebContents();
base::AutoReset<bool> reset(&should_quit_on_zoom_, true);
zoom::PageZoom::Zoom(web_contents, zoom);
content::RunMessageLoop();
}
void OnZoomChanged(const content::HostZoomMap::ZoomLevelChange& host) {
if (should_quit_on_zoom_) {
base::ThreadTaskRunnerHandle::Get()->PostTask(
FROM_HERE, base::Bind(&base::RunLoop::QuitCurrentWhenIdleDeprecated));
}
}
private:
bool should_quit_on_zoom_;
std::unique_ptr<content::HostZoomMap::Subscription> zoom_subscription_;
base::test::ScopedFeatureList scoped_feature_list_;
test::ScopedMacViewsBrowserMode cocoa_browser_mode_{false};
DISALLOW_COPY_AND_ASSIGN(ZoomDecorationTest);
};
// TODO(crbug.com/887811): Re-enable after deflaking
IN_PROC_BROWSER_TEST_P(ZoomDecorationTest, DISABLED_BubbleAtDefaultZoom) {
}
// Regression test for https://crbug.com/462482.
IN_PROC_BROWSER_TEST_P(ZoomDecorationTest, IconRemainsVisibleAfterBubble) {
ZoomDecoration* zoom_decoration = GetZoomDecoration();
// See comment in BubbleAtDefaultZoom regarding this next line.
zoom::ZoomController::FromWebContents(GetLocationBar()->GetWebContents())
->SetShowsNotificationBubble(false);
// Zoom in to turn on decoration icon.
EXPECT_FALSE(zoom_decoration->IsVisible());
Zoom(content::PAGE_ZOOM_IN);
EXPECT_TRUE(zoom_decoration->IsVisible());
// Show zoom bubble, verify decoration icon remains visible.
zoom_decoration->ShowBubble(/* auto_close = */false);
EXPECT_TRUE(zoom_decoration->IsVisible());
// Close bubble and verify the decoration is still visible.
zoom_decoration->CloseBubble();
content::RunAllPendingInMessageLoop();
EXPECT_TRUE(zoom_decoration->IsVisible());
// Verify the decoration does go away when we expect it to.
Zoom(content::PAGE_ZOOM_RESET);
EXPECT_FALSE(zoom_decoration->IsVisible());
}
IN_PROC_BROWSER_TEST_P(ZoomDecorationTest, HideOnInputProgress) {
ZoomDecoration* zoom_decoration = GetZoomDecoration();
// Zoom in and reset.
Zoom(content::PAGE_ZOOM_IN);
EXPECT_TRUE(zoom_decoration->IsVisible());
std::unique_ptr<ToolbarModel> toolbar_model(new TestToolbarModel);
toolbar_model->set_input_in_progress(true);
browser()->swap_toolbar_models(&toolbar_model);
GetLocationBar()->ZoomChangedForActiveTab(false);
EXPECT_FALSE(zoom_decoration->IsVisible());
}
IN_PROC_BROWSER_TEST_P(ZoomDecorationTest, CloseBrowserWithOpenBubble) {
// Create a new browser so that it can be closed properly.
Browser* browser2 = CreateBrowser(browser()->profile());
ZoomDecoration* zoom_decoration = GetZoomDecorationForBrowser(browser2);
zoom_decoration->ShowBubble(true);
// Test shouldn't crash.
browser2->window()->Close();
content::RunAllPendingInMessageLoop();
}
// Prefix for test instantiations intentionally left blank since the test
// fixture class has a single parameterization.
INSTANTIATE_TEST_CASE_P(, ZoomDecorationTest, testing::Bool());
// Copyright 2014 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/location_bar/zoom_decoration.h"
#include "base/command_line.h"
#include "base/macros.h"
#include "base/test/scoped_feature_list.h"
#include "chrome/test/base/chrome_render_view_host_test_harness.h"
#include "components/zoom/zoom_controller.h"
#include "ui/base/ui_base_features.h"
namespace {
// TODO(crbug.com/630357): Remove parameterized testing for this class.
class ZoomDecorationTest : public ChromeRenderViewHostTestHarness,
public ::testing::WithParamInterface<bool> {
public:
ZoomDecorationTest() {}
~ZoomDecorationTest() override {}
private:
base::test::ScopedFeatureList scoped_feature_list_;
DISALLOW_COPY_AND_ASSIGN(ZoomDecorationTest);
};
class MockZoomDecoration : public ZoomDecoration {
public:
explicit MockZoomDecoration(LocationBarViewMac* owner)
: ZoomDecoration(owner), update_ui_count_(0) {}
bool ShouldShowDecoration() const override { return true; }
void UpdateUI(zoom::ZoomController* zoom_controller,
NSString* tooltip_string,
bool location_bar_is_dark) override {
++update_ui_count_;
ZoomDecoration::UpdateUI(zoom_controller, tooltip_string,
location_bar_is_dark);
}
int update_ui_count_;
private:
DISALLOW_COPY_AND_ASSIGN(MockZoomDecoration);
};
class MockZoomController : public zoom::ZoomController {
public:
explicit MockZoomController(content::WebContents* web_contents)
: zoom::ZoomController(web_contents) {}
int GetZoomPercent() const override { return zoom_percent_; }
bool IsAtDefaultZoom() const override { return zoom_percent_ == 100; }
int zoom_percent_;
private:
DISALLOW_COPY_AND_ASSIGN(MockZoomController);
};
// Test that UpdateIfNecessary performs redraws only when the zoom percent
// changes.
TEST_P(ZoomDecorationTest, ChangeZoomPercent) {
MockZoomDecoration decoration(NULL);
MockZoomController controller(web_contents());
controller.zoom_percent_ = 100;
decoration.UpdateIfNecessary(&controller,
/*default_zoom_changed=*/false,
false);
EXPECT_EQ(1, decoration.update_ui_count_);
decoration.UpdateIfNecessary(&controller,
/*default_zoom_changed=*/false,
false);
EXPECT_EQ(1, decoration.update_ui_count_);
controller.zoom_percent_ = 80;
decoration.UpdateIfNecessary(&controller,
/*default_zoom_changed=*/false,
false);
EXPECT_EQ(2, decoration.update_ui_count_);
// Always redraw if the default zoom changes.
decoration.UpdateIfNecessary(&controller,
/*default_zoom_changed=*/true,
false);
EXPECT_EQ(3, decoration.update_ui_count_);
}
// Prefix for test instantiations intentionally left blank since the test
// fixture class has a single parameterization.
INSTANTIATE_TEST_CASE_P(, ZoomDecorationTest, testing::Bool());
} // namespace
...@@ -1966,7 +1966,6 @@ test("browser_tests") { ...@@ -1966,7 +1966,6 @@ test("browser_tests") {
"../browser/ui/cocoa/apps/app_shim_menu_controller_mac_browsertest.mm", "../browser/ui/cocoa/apps/app_shim_menu_controller_mac_browsertest.mm",
"../browser/ui/cocoa/apps/native_app_window_cocoa_browsertest.mm", "../browser/ui/cocoa/apps/native_app_window_cocoa_browsertest.mm",
"../browser/ui/cocoa/dev_tools_controller_browsertest.mm", "../browser/ui/cocoa/dev_tools_controller_browsertest.mm",
"../browser/ui/cocoa/location_bar/zoom_decoration_browsertest.mm",
"../browser/ui/cocoa/omnibox/omnibox_view_mac_browsertest.mm", "../browser/ui/cocoa/omnibox/omnibox_view_mac_browsertest.mm",
"../browser/ui/cocoa/permission_bubble/permission_bubble_views_cocoa_browsertest.mm", "../browser/ui/cocoa/permission_bubble/permission_bubble_views_cocoa_browsertest.mm",
"../browser/ui/cocoa/renderer_context_menu/render_view_context_menu_mac_cocoa_browsertest.mm", "../browser/ui/cocoa/renderer_context_menu/render_view_context_menu_mac_cocoa_browsertest.mm",
...@@ -4151,8 +4150,6 @@ test("unit_tests") { ...@@ -4151,8 +4150,6 @@ test("unit_tests") {
"../browser/ui/cocoa/location_bar/autocomplete_text_field_unittest.mm", "../browser/ui/cocoa/location_bar/autocomplete_text_field_unittest.mm",
"../browser/ui/cocoa/location_bar/autocomplete_text_field_unittest_helper.h", "../browser/ui/cocoa/location_bar/autocomplete_text_field_unittest_helper.h",
"../browser/ui/cocoa/location_bar/autocomplete_text_field_unittest_helper.mm", "../browser/ui/cocoa/location_bar/autocomplete_text_field_unittest_helper.mm",
"../browser/ui/cocoa/location_bar/image_decoration_unittest.mm",
"../browser/ui/cocoa/location_bar/zoom_decoration_unittest.mm",
"../browser/ui/cocoa/main_menu_builder_unittest.mm", "../browser/ui/cocoa/main_menu_builder_unittest.mm",
"../browser/ui/cocoa/media_picker/desktop_media_picker_controller_unittest.mm", "../browser/ui/cocoa/media_picker/desktop_media_picker_controller_unittest.mm",
"../browser/ui/cocoa/menu_button_unittest.mm", "../browser/ui/cocoa/menu_button_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