| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include <cmath> | 5 #include <cmath> |
| 6 | 6 |
| 7 #import "chrome/browser/ui/cocoa/location_bar/bubble_decoration.h" | 7 #import "chrome/browser/ui/cocoa/location_bar/bubble_decoration.h" |
| 8 | 8 |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/mac/foundation_util.h" | 10 #include "base/mac/foundation_util.h" |
| 11 #include "chrome/browser/ui/cocoa/l10n_util.h" | 11 #include "chrome/browser/ui/cocoa/l10n_util.h" |
| 12 #import "chrome/browser/ui/cocoa/themed_window.h" | 12 #import "chrome/browser/ui/cocoa/themed_window.h" |
| 13 #include "skia/ext/skia_utils_mac.h" | 13 #include "skia/ext/skia_utils_mac.h" |
| 14 #import "ui/base/cocoa/nsview_additions.h" | 14 #import "ui/base/cocoa/nsview_additions.h" |
| 15 #include "ui/base/material_design/material_design_controller.h" | 15 #include "ui/base/material_design/material_design_controller.h" |
| 16 #include "ui/gfx/scoped_ns_graphics_context_save_gstate_mac.h" | 16 #include "ui/gfx/scoped_ns_graphics_context_save_gstate_mac.h" |
| 17 | 17 |
| 18 namespace { | 18 namespace { |
| 19 | 19 |
| 20 // This is used to increase the left padding of this decoration. | 20 // This is used to increase the left padding of this decoration. |
| 21 const CGFloat kLeftSidePadding = 5.0; | 21 const CGFloat kLeftSidePadding = 5.0; |
| 22 | 22 |
| 23 // Padding between the icon/label and bubble edges. | 23 // Padding between the icon/label and bubble edges. |
| 24 const CGFloat kBubblePadding = 7.0; | 24 const CGFloat kBubblePadding = 7.0; |
| 25 | 25 |
| 26 // Additional padding between the divider and the label. | |
| 27 const CGFloat kDividerPadding = 6.0; | |
| 28 | |
| 29 // Padding between the icon and label. | 26 // Padding between the icon and label. |
| 30 const CGFloat kIconLabelPadding = 4.0; | 27 const CGFloat kIconLabelPadding = 4.0; |
| 31 | 28 |
| 32 // Inset for the image frame. | 29 // Inset for the image frame. |
| 33 const CGFloat kImageFrameYInset = 4.0; | 30 const CGFloat kImageFrameYInset = 4.0; |
| 34 | 31 |
| 35 // Inset for the background frame. | 32 // Inset for the background frame. |
| 36 const CGFloat kBackgroundFrameYInset = 2.0; | 33 const CGFloat kBackgroundFrameYInset = 2.0; |
| 37 | 34 |
| 38 // Left margin for the background frame. | 35 // Left margin for the background frame. |
| 39 const CGFloat kBackgroundFrameLeftMargin = 1.0; | 36 const CGFloat kBackgroundFrameLeftMargin = 1.0; |
| 40 | 37 |
| 41 } // namespace | 38 } // namespace |
| 42 | 39 |
| 43 BubbleDecoration::BubbleDecoration() : retina_baseline_offset_(0) { | 40 BubbleDecoration::BubbleDecoration() : retina_baseline_offset_(0) { |
| 44 attributes_.reset([[NSMutableDictionary alloc] init]); | 41 attributes_.reset([[NSMutableDictionary alloc] init]); |
| 45 [attributes_ setObject:LocationBarDecoration::GetFont() | 42 [attributes_ setObject:LocationBarDecoration::GetFont() |
| 46 forKey:NSFontAttributeName]; | 43 forKey:NSFontAttributeName]; |
| 47 } | 44 } |
| 48 | 45 |
| 49 BubbleDecoration::~BubbleDecoration() { | 46 BubbleDecoration::~BubbleDecoration() { |
| 50 } | 47 } |
| 51 | 48 |
| 52 CGFloat BubbleDecoration::DividerPadding() const { | |
| 53 return kDividerPadding; | |
| 54 } | |
| 55 | |
| 56 CGFloat BubbleDecoration::GetWidthForImageAndLabel(NSImage* image, | 49 CGFloat BubbleDecoration::GetWidthForImageAndLabel(NSImage* image, |
| 57 NSString* label) { | 50 NSString* label) { |
| 58 if (!image && !label) | 51 if (!image && !label) |
| 59 return kOmittedWidth; | 52 return kOmittedWidth; |
| 60 | 53 |
| 61 const CGFloat image_width = image ? [image size].width : 0.0; | 54 const CGFloat image_width = image ? [image size].width : 0.0; |
| 62 if (!label) | 55 if (!label) |
| 63 return kBubblePadding + image_width; | 56 return kBubblePadding + image_width; |
| 64 | 57 |
| 65 // The bubble needs to take up an integral number of pixels. | 58 // The bubble needs to take up an integral number of pixels. |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 100 | 93 |
| 101 const CGFloat image_width = GetWidthForImageAndLabel(image_, nil); | 94 const CGFloat image_width = GetWidthForImageAndLabel(image_, nil); |
| 102 if (image_width <= width) | 95 if (image_width <= width) |
| 103 return image_width; | 96 return image_width; |
| 104 | 97 |
| 105 return kOmittedWidth; | 98 return kOmittedWidth; |
| 106 } | 99 } |
| 107 | 100 |
| 108 NSRect BubbleDecoration::GetBackgroundFrame(NSRect frame) { | 101 NSRect BubbleDecoration::GetBackgroundFrame(NSRect frame) { |
| 109 NSRect background_frame = NSInsetRect(frame, 0.0, kBackgroundFrameYInset); | 102 NSRect background_frame = NSInsetRect(frame, 0.0, kBackgroundFrameYInset); |
| 103 const CGFloat divider_padding = DividerPadding(); |
| 110 if (cocoa_l10n_util::ShouldDoExperimentalRTLLayout()) { | 104 if (cocoa_l10n_util::ShouldDoExperimentalRTLLayout()) { |
| 111 background_frame.origin.x += kDividerPadding; | 105 background_frame.origin.x += divider_padding; |
| 112 background_frame.size.width -= kDividerPadding + kBackgroundFrameLeftMargin; | 106 background_frame.size.width -= divider_padding + kBackgroundFrameLeftMargin; |
| 113 } else { | 107 } else { |
| 114 background_frame.origin.x += kBackgroundFrameLeftMargin; | 108 background_frame.origin.x += kBackgroundFrameLeftMargin; |
| 115 background_frame.size.width -= kDividerPadding; | 109 background_frame.size.width -= divider_padding; |
| 116 } | 110 } |
| 117 return background_frame; | 111 return background_frame; |
| 118 } | 112 } |
| 119 | 113 |
| 120 void BubbleDecoration::DrawInFrame(NSRect frame, NSView* control_view) { | 114 void BubbleDecoration::DrawInFrame(NSRect frame, NSView* control_view) { |
| 121 const NSRect decoration_frame = NSInsetRect(frame, 0.0, kImageFrameYInset); | 115 const NSRect decoration_frame = NSInsetRect(frame, 0.0, kImageFrameYInset); |
| 122 CGFloat text_left_offset = NSMinX(decoration_frame); | 116 CGFloat text_left_offset = NSMinX(decoration_frame); |
| 123 CGFloat text_right_offset = NSMaxX(decoration_frame); | 117 CGFloat text_right_offset = NSMaxX(decoration_frame); |
| 124 const BOOL is_rtl = cocoa_l10n_util::ShouldDoExperimentalRTLLayout(); | 118 const BOOL is_rtl = cocoa_l10n_util::ShouldDoExperimentalRTLLayout(); |
| 125 | 119 |
| 126 if (image_) { | 120 if (image_) { |
| 127 NSRect image_rect = GetImageRectInFrame(frame); | 121 NSRect image_rect = GetImageRectInFrame(frame); |
| 128 [image_ drawInRect:image_rect | 122 [image_ drawInRect:image_rect |
| 129 fromRect:NSZeroRect // Entire image | 123 fromRect:NSZeroRect // Entire image |
| 130 operation:NSCompositeSourceOver | 124 operation:NSCompositeSourceOver |
| 131 fraction:1.0 | 125 fraction:1.0 |
| 132 respectFlipped:YES | 126 respectFlipped:YES |
| 133 hints:nil]; | 127 hints:nil]; |
| 134 if (is_rtl) | 128 if (is_rtl) |
| 135 text_right_offset = NSMinX(image_rect) - kIconLabelPadding; | 129 text_right_offset = NSMinX(image_rect) - kIconLabelPadding; |
| 136 else | 130 else |
| 137 text_left_offset = NSMaxX(image_rect) + kIconLabelPadding; | 131 text_left_offset = NSMaxX(image_rect) + kIconLabelPadding; |
| 138 } | 132 } |
| 139 | 133 |
| 140 // Draw the divider and set the text color. | 134 // Draw the divider. |
| 141 NSBezierPath* line = [NSBezierPath bezierPath]; | 135 DrawDivider(control_view, decoration_frame, 1.0); |
| 142 const CGFloat divider_x_position = | |
| 143 is_rtl ? NSMinX(decoration_frame) + DividerPadding() | |
| 144 : NSMaxX(decoration_frame) - DividerPadding(); | |
| 145 | 136 |
| 146 [line setLineWidth:1]; | 137 // Set the text color. |
| 147 [line moveToPoint:NSMakePoint(divider_x_position, NSMinY(decoration_frame))]; | |
| 148 [line lineToPoint:NSMakePoint(divider_x_position, NSMaxY(decoration_frame))]; | |
| 149 | |
| 150 bool in_dark_mode = [[control_view window] inIncognitoModeWithSystemTheme]; | 138 bool in_dark_mode = [[control_view window] inIncognitoModeWithSystemTheme]; |
| 151 [GetDividerColor(in_dark_mode) set]; | |
| 152 [line stroke]; | |
| 153 | |
| 154 NSColor* text_color = | 139 NSColor* text_color = |
| 155 in_dark_mode ? GetDarkModeTextColor() : GetBackgroundBorderColor(); | 140 in_dark_mode ? GetDarkModeTextColor() : GetBackgroundBorderColor(); |
| 156 SetTextColor(text_color); | 141 SetTextColor(text_color); |
| 157 | 142 |
| 158 if (label_) { | 143 if (label_) { |
| 159 NSRect text_rect = frame; | 144 NSRect text_rect = frame; |
| 160 text_rect.origin.x = text_left_offset; | 145 text_rect.origin.x = text_left_offset; |
| 161 text_rect.size.width = text_right_offset - text_left_offset; | 146 text_rect.size.width = text_right_offset - text_left_offset; |
| 162 // Transform the coordinate system to adjust the baseline on Retina. This is | 147 // Transform the coordinate system to adjust the baseline on Retina. This is |
| 163 // the only way to get fractional adjustments. | 148 // the only way to get fractional adjustments. |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 196 [attributes_ setObject:text_color forKey:NSForegroundColorAttributeName]; | 181 [attributes_ setObject:text_color forKey:NSForegroundColorAttributeName]; |
| 197 } | 182 } |
| 198 | 183 |
| 199 void BubbleDecoration::SetFont(NSFont* font) { | 184 void BubbleDecoration::SetFont(NSFont* font) { |
| 200 [attributes_ setObject:font forKey:NSFontAttributeName]; | 185 [attributes_ setObject:font forKey:NSFontAttributeName]; |
| 201 } | 186 } |
| 202 | 187 |
| 203 void BubbleDecoration::SetRetinaBaselineOffset(CGFloat offset) { | 188 void BubbleDecoration::SetRetinaBaselineOffset(CGFloat offset) { |
| 204 retina_baseline_offset_ = offset; | 189 retina_baseline_offset_ = offset; |
| 205 } | 190 } |
| OLD | NEW |