| OLD | NEW |
| (Empty) |
| 1 // Copyright 2014 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. | |
| 4 | |
| 5 #include "ash/frame/default_header_painter.h" | |
| 6 | |
| 7 #include "ash/common/ash_layout_constants.h" | |
| 8 #include "ash/frame/caption_buttons/frame_caption_button_container_view.h" | |
| 9 #include "ash/frame/header_painter_util.h" | |
| 10 #include "base/debug/leak_annotations.h" | |
| 11 #include "base/logging.h" // DCHECK | |
| 12 #include "grit/ash_resources.h" | |
| 13 #include "third_party/skia/include/core/SkPaint.h" | |
| 14 #include "third_party/skia/include/core/SkPath.h" | |
| 15 #include "ui/base/resource/resource_bundle.h" | |
| 16 #include "ui/gfx/animation/slide_animation.h" | |
| 17 #include "ui/gfx/canvas.h" | |
| 18 #include "ui/gfx/color_utils.h" | |
| 19 #include "ui/gfx/font_list.h" | |
| 20 #include "ui/gfx/geometry/rect.h" | |
| 21 #include "ui/gfx/image/image.h" | |
| 22 #include "ui/gfx/scoped_canvas.h" | |
| 23 #include "ui/gfx/skia_util.h" | |
| 24 #include "ui/gfx/vector_icons_public.h" | |
| 25 #include "ui/views/view.h" | |
| 26 #include "ui/views/widget/native_widget_aura.h" | |
| 27 #include "ui/views/widget/widget.h" | |
| 28 #include "ui/views/widget/widget_delegate.h" | |
| 29 | |
| 30 using views::Widget; | |
| 31 | |
| 32 namespace { | |
| 33 | |
| 34 // Color for the window title text. | |
| 35 const SkColor kTitleTextColor = SkColorSetRGB(40, 40, 40); | |
| 36 // Color of the active window header/content separator line. | |
| 37 const SkColor kHeaderContentSeparatorColor = SkColorSetRGB(150, 150, 152); | |
| 38 // Color of the inactive window header/content separator line. | |
| 39 const SkColor kHeaderContentSeparatorInactiveColor = | |
| 40 SkColorSetRGB(180, 180, 182); | |
| 41 // The default color of the frame. | |
| 42 const SkColor kDefaultFrameColor = SkColorSetRGB(242, 242, 242); | |
| 43 // Duration of crossfade animation for activating and deactivating frame. | |
| 44 const int kActivationCrossfadeDurationMs = 200; | |
| 45 | |
| 46 // Tiles an image into an area, rounding the top corners. | |
| 47 void TileRoundRect(gfx::Canvas* canvas, | |
| 48 const SkPaint& paint, | |
| 49 const gfx::Rect& bounds, | |
| 50 int corner_radius) { | |
| 51 SkRect rect = gfx::RectToSkRect(bounds); | |
| 52 const SkScalar corner_radius_scalar = SkIntToScalar(corner_radius); | |
| 53 SkScalar radii[8] = {corner_radius_scalar, | |
| 54 corner_radius_scalar, // top-left | |
| 55 corner_radius_scalar, | |
| 56 corner_radius_scalar, // top-right | |
| 57 0, | |
| 58 0, // bottom-right | |
| 59 0, | |
| 60 0}; // bottom-left | |
| 61 SkPath path; | |
| 62 path.addRoundRect(rect, radii, SkPath::kCW_Direction); | |
| 63 canvas->DrawPath(path, paint); | |
| 64 } | |
| 65 | |
| 66 // Returns the FontList to use for the title. | |
| 67 const gfx::FontList& GetTitleFontList() { | |
| 68 static const gfx::FontList* title_font_list = | |
| 69 new gfx::FontList(views::NativeWidgetAura::GetWindowTitleFontList()); | |
| 70 ANNOTATE_LEAKING_OBJECT_PTR(title_font_list); | |
| 71 return *title_font_list; | |
| 72 } | |
| 73 | |
| 74 } // namespace | |
| 75 | |
| 76 namespace ash { | |
| 77 | |
| 78 /////////////////////////////////////////////////////////////////////////////// | |
| 79 // DefaultHeaderPainter, public: | |
| 80 | |
| 81 DefaultHeaderPainter::DefaultHeaderPainter() | |
| 82 : frame_(NULL), | |
| 83 view_(NULL), | |
| 84 left_header_view_(NULL), | |
| 85 active_frame_color_(kDefaultFrameColor), | |
| 86 inactive_frame_color_(kDefaultFrameColor), | |
| 87 caption_button_container_(NULL), | |
| 88 painted_height_(0), | |
| 89 mode_(MODE_INACTIVE), | |
| 90 initial_paint_(true), | |
| 91 activation_animation_(new gfx::SlideAnimation(this)) {} | |
| 92 | |
| 93 DefaultHeaderPainter::~DefaultHeaderPainter() {} | |
| 94 | |
| 95 void DefaultHeaderPainter::Init( | |
| 96 views::Widget* frame, | |
| 97 views::View* header_view, | |
| 98 FrameCaptionButtonContainerView* caption_button_container) { | |
| 99 DCHECK(frame); | |
| 100 DCHECK(header_view); | |
| 101 DCHECK(caption_button_container); | |
| 102 frame_ = frame; | |
| 103 view_ = header_view; | |
| 104 caption_button_container_ = caption_button_container; | |
| 105 caption_button_container_->SetButtonSize( | |
| 106 GetAshLayoutSize(AshLayoutSize::NON_BROWSER_CAPTION_BUTTON)); | |
| 107 UpdateAllButtonImages(); | |
| 108 } | |
| 109 | |
| 110 int DefaultHeaderPainter::GetMinimumHeaderWidth() const { | |
| 111 // Ensure we have enough space for the window icon and buttons. We allow | |
| 112 // the title string to collapse to zero width. | |
| 113 return GetTitleBounds().x() + | |
| 114 caption_button_container_->GetMinimumSize().width(); | |
| 115 } | |
| 116 | |
| 117 void DefaultHeaderPainter::PaintHeader(gfx::Canvas* canvas, Mode mode) { | |
| 118 Mode old_mode = mode_; | |
| 119 mode_ = mode; | |
| 120 | |
| 121 if (mode_ != old_mode) { | |
| 122 UpdateAllButtonImages(); | |
| 123 if (!initial_paint_ && HeaderPainterUtil::CanAnimateActivation(frame_)) { | |
| 124 activation_animation_->SetSlideDuration(kActivationCrossfadeDurationMs); | |
| 125 if (mode_ == MODE_ACTIVE) | |
| 126 activation_animation_->Show(); | |
| 127 else | |
| 128 activation_animation_->Hide(); | |
| 129 } else { | |
| 130 if (mode_ == MODE_ACTIVE) | |
| 131 activation_animation_->Reset(1); | |
| 132 else | |
| 133 activation_animation_->Reset(0); | |
| 134 } | |
| 135 initial_paint_ = false; | |
| 136 } | |
| 137 | |
| 138 int corner_radius = (frame_->IsMaximized() || frame_->IsFullscreen()) | |
| 139 ? 0 | |
| 140 : HeaderPainterUtil::GetTopCornerRadiusWhenRestored(); | |
| 141 | |
| 142 SkPaint paint; | |
| 143 int active_alpha = activation_animation_->CurrentValueBetween(0, 255); | |
| 144 paint.setColor(color_utils::AlphaBlend(active_frame_color_, | |
| 145 inactive_frame_color_, active_alpha)); | |
| 146 | |
| 147 TileRoundRect(canvas, paint, GetLocalBounds(), corner_radius); | |
| 148 | |
| 149 if (!frame_->IsMaximized() && !frame_->IsFullscreen() && | |
| 150 mode_ == MODE_INACTIVE && !UsesCustomFrameColors()) { | |
| 151 PaintHighlightForInactiveRestoredWindow(canvas); | |
| 152 } | |
| 153 if (frame_->widget_delegate() && | |
| 154 frame_->widget_delegate()->ShouldShowWindowTitle()) { | |
| 155 PaintTitleBar(canvas); | |
| 156 } | |
| 157 if (!UsesCustomFrameColors()) | |
| 158 PaintHeaderContentSeparator(canvas); | |
| 159 } | |
| 160 | |
| 161 void DefaultHeaderPainter::LayoutHeader() { | |
| 162 caption_button_container_->SetUseLightImages(ShouldUseLightImages()); | |
| 163 UpdateSizeButtonImages(); | |
| 164 caption_button_container_->Layout(); | |
| 165 | |
| 166 gfx::Size caption_button_container_size = | |
| 167 caption_button_container_->GetPreferredSize(); | |
| 168 caption_button_container_->SetBounds( | |
| 169 view_->width() - caption_button_container_size.width(), 0, | |
| 170 caption_button_container_size.width(), | |
| 171 caption_button_container_size.height()); | |
| 172 | |
| 173 if (left_header_view_) { | |
| 174 // Vertically center the left header view with respect to the caption button | |
| 175 // container. | |
| 176 // Floor when computing the center of |caption_button_container_|. | |
| 177 gfx::Size size = left_header_view_->GetPreferredSize(); | |
| 178 int icon_offset_y = | |
| 179 caption_button_container_->height() / 2 - size.height() / 2; | |
| 180 left_header_view_->SetBounds(HeaderPainterUtil::GetLeftViewXInset(), | |
| 181 icon_offset_y, size.width(), size.height()); | |
| 182 } | |
| 183 | |
| 184 // The header/content separator line overlays the caption buttons. | |
| 185 SetHeaderHeightForPainting(caption_button_container_->height()); | |
| 186 } | |
| 187 | |
| 188 int DefaultHeaderPainter::GetHeaderHeight() const { | |
| 189 return caption_button_container_->height(); | |
| 190 } | |
| 191 | |
| 192 int DefaultHeaderPainter::GetHeaderHeightForPainting() const { | |
| 193 return painted_height_; | |
| 194 } | |
| 195 | |
| 196 void DefaultHeaderPainter::SetHeaderHeightForPainting(int height) { | |
| 197 painted_height_ = height; | |
| 198 } | |
| 199 | |
| 200 void DefaultHeaderPainter::SchedulePaintForTitle() { | |
| 201 view_->SchedulePaintInRect(GetTitleBounds()); | |
| 202 } | |
| 203 | |
| 204 void DefaultHeaderPainter::SetFrameColors(SkColor active_frame_color, | |
| 205 SkColor inactive_frame_color) { | |
| 206 active_frame_color_ = active_frame_color; | |
| 207 inactive_frame_color_ = inactive_frame_color; | |
| 208 UpdateAllButtonImages(); | |
| 209 } | |
| 210 | |
| 211 void DefaultHeaderPainter::UpdateLeftHeaderView(views::View* left_header_view) { | |
| 212 left_header_view_ = left_header_view; | |
| 213 } | |
| 214 | |
| 215 /////////////////////////////////////////////////////////////////////////////// | |
| 216 // gfx::AnimationDelegate overrides: | |
| 217 | |
| 218 void DefaultHeaderPainter::AnimationProgressed( | |
| 219 const gfx::Animation* animation) { | |
| 220 view_->SchedulePaintInRect(GetLocalBounds()); | |
| 221 } | |
| 222 | |
| 223 /////////////////////////////////////////////////////////////////////////////// | |
| 224 // DefaultHeaderPainter, private: | |
| 225 | |
| 226 void DefaultHeaderPainter::PaintHighlightForInactiveRestoredWindow( | |
| 227 gfx::Canvas* canvas) { | |
| 228 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); | |
| 229 gfx::ImageSkia top_edge = | |
| 230 *rb.GetImageSkiaNamed(IDR_AURA_WINDOW_HEADER_SHADE_INACTIVE_TOP); | |
| 231 gfx::ImageSkia left_edge = | |
| 232 *rb.GetImageSkiaNamed(IDR_AURA_WINDOW_HEADER_SHADE_INACTIVE_LEFT); | |
| 233 gfx::ImageSkia right_edge = | |
| 234 *rb.GetImageSkiaNamed(IDR_AURA_WINDOW_HEADER_SHADE_INACTIVE_RIGHT); | |
| 235 gfx::ImageSkia bottom_edge = | |
| 236 *rb.GetImageSkiaNamed(IDR_AURA_WINDOW_HEADER_SHADE_INACTIVE_BOTTOM); | |
| 237 | |
| 238 int left_edge_width = left_edge.width(); | |
| 239 int right_edge_width = right_edge.width(); | |
| 240 canvas->DrawImageInt(left_edge, 0, 0); | |
| 241 canvas->DrawImageInt(right_edge, view_->width() - right_edge_width, 0); | |
| 242 canvas->TileImageInt(top_edge, left_edge_width, 0, | |
| 243 view_->width() - left_edge_width - right_edge_width, | |
| 244 top_edge.height()); | |
| 245 | |
| 246 DCHECK_EQ(left_edge.height(), right_edge.height()); | |
| 247 int bottom = left_edge.height(); | |
| 248 int bottom_height = bottom_edge.height(); | |
| 249 canvas->TileImageInt(bottom_edge, left_edge_width, bottom - bottom_height, | |
| 250 view_->width() - left_edge_width - right_edge_width, | |
| 251 bottom_height); | |
| 252 } | |
| 253 | |
| 254 void DefaultHeaderPainter::PaintTitleBar(gfx::Canvas* canvas) { | |
| 255 // The window icon is painted by its own views::View. | |
| 256 gfx::Rect title_bounds = GetTitleBounds(); | |
| 257 title_bounds.set_x(view_->GetMirroredXForRect(title_bounds)); | |
| 258 canvas->DrawStringRectWithFlags( | |
| 259 frame_->widget_delegate()->GetWindowTitle(), GetTitleFontList(), | |
| 260 kTitleTextColor, title_bounds, gfx::Canvas::NO_SUBPIXEL_RENDERING); | |
| 261 } | |
| 262 | |
| 263 void DefaultHeaderPainter::PaintHeaderContentSeparator(gfx::Canvas* canvas) { | |
| 264 gfx::ScopedCanvas scoped_canvas(canvas); | |
| 265 const float scale = canvas->UndoDeviceScaleFactor(); | |
| 266 gfx::RectF rect(0, painted_height_ * scale - 1, view_->width() * scale, 1); | |
| 267 SkPaint paint; | |
| 268 paint.setColor((mode_ == MODE_ACTIVE) ? kHeaderContentSeparatorColor | |
| 269 : kHeaderContentSeparatorInactiveColor); | |
| 270 canvas->sk_canvas()->drawRect(gfx::RectFToSkRect(rect), paint); | |
| 271 } | |
| 272 | |
| 273 bool DefaultHeaderPainter::ShouldUseLightImages() { | |
| 274 return color_utils::IsDark(mode_ == MODE_INACTIVE ? inactive_frame_color_ | |
| 275 : active_frame_color_); | |
| 276 } | |
| 277 | |
| 278 void DefaultHeaderPainter::UpdateAllButtonImages() { | |
| 279 caption_button_container_->SetUseLightImages(ShouldUseLightImages()); | |
| 280 caption_button_container_->SetButtonImage( | |
| 281 CAPTION_BUTTON_ICON_MINIMIZE, gfx::VectorIconId::WINDOW_CONTROL_MINIMIZE); | |
| 282 | |
| 283 UpdateSizeButtonImages(); | |
| 284 | |
| 285 caption_button_container_->SetButtonImage( | |
| 286 CAPTION_BUTTON_ICON_CLOSE, gfx::VectorIconId::WINDOW_CONTROL_CLOSE); | |
| 287 | |
| 288 caption_button_container_->SetButtonImage( | |
| 289 CAPTION_BUTTON_ICON_LEFT_SNAPPED, | |
| 290 gfx::VectorIconId::WINDOW_CONTROL_LEFT_SNAPPED); | |
| 291 | |
| 292 caption_button_container_->SetButtonImage( | |
| 293 CAPTION_BUTTON_ICON_RIGHT_SNAPPED, | |
| 294 gfx::VectorIconId::WINDOW_CONTROL_RIGHT_SNAPPED); | |
| 295 } | |
| 296 | |
| 297 void DefaultHeaderPainter::UpdateSizeButtonImages() { | |
| 298 gfx::VectorIconId icon_id = frame_->IsMaximized() || frame_->IsFullscreen() | |
| 299 ? gfx::VectorIconId::WINDOW_CONTROL_RESTORE | |
| 300 : gfx::VectorIconId::WINDOW_CONTROL_MAXIMIZE; | |
| 301 caption_button_container_->SetButtonImage( | |
| 302 CAPTION_BUTTON_ICON_MAXIMIZE_RESTORE, icon_id); | |
| 303 } | |
| 304 | |
| 305 gfx::Rect DefaultHeaderPainter::GetLocalBounds() const { | |
| 306 return gfx::Rect(view_->width(), painted_height_); | |
| 307 } | |
| 308 | |
| 309 gfx::Rect DefaultHeaderPainter::GetTitleBounds() const { | |
| 310 return HeaderPainterUtil::GetTitleBounds( | |
| 311 left_header_view_, caption_button_container_, GetTitleFontList()); | |
| 312 } | |
| 313 | |
| 314 bool DefaultHeaderPainter::UsesCustomFrameColors() const { | |
| 315 return active_frame_color_ != kDefaultFrameColor || | |
| 316 inactive_frame_color_ != kDefaultFrameColor; | |
| 317 } | |
| 318 | |
| 319 } // namespace ash | |
| OLD | NEW |