James Cook | b0bf8e8 | 2017-04-09 17:01:44 | [diff] [blame] | 1 | // Copyright 2016 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/header_view.h" |
| 6 | |
| 7 | #include "ash/frame/caption_buttons/frame_caption_button_container_view.h" |
| 8 | #include "ash/frame/default_header_painter.h" |
James Cook | b0bf8e8 | 2017-04-09 17:01:44 | [diff] [blame] | 9 | #include "ash/shell.h" |
James Cook | b0bf8e8 | 2017-04-09 17:01:44 | [diff] [blame] | 10 | #include "ui/views/controls/image_view.h" |
| 11 | #include "ui/views/widget/widget.h" |
| 12 | |
| 13 | namespace ash { |
| 14 | |
| 15 | HeaderView::HeaderView(views::Widget* target_widget, |
| 16 | mojom::WindowStyle window_style) |
| 17 | : target_widget_(target_widget), |
| 18 | header_painter_(base::MakeUnique<DefaultHeaderPainter>(window_style)), |
| 19 | avatar_icon_(nullptr), |
| 20 | caption_button_container_(nullptr), |
| 21 | fullscreen_visible_fraction_(0) { |
| 22 | caption_button_container_ = |
| 23 | new FrameCaptionButtonContainerView(target_widget_); |
| 24 | caption_button_container_->UpdateSizeButtonVisibility(); |
| 25 | AddChildView(caption_button_container_); |
| 26 | |
| 27 | header_painter_->Init(target_widget_, this, caption_button_container_); |
James Cook | b0bf8e8 | 2017-04-09 17:01:44 | [diff] [blame] | 28 | |
| 29 | Shell::Get()->AddShellObserver(this); |
| 30 | } |
| 31 | |
| 32 | HeaderView::~HeaderView() { |
| 33 | Shell::Get()->RemoveShellObserver(this); |
| 34 | } |
| 35 | |
| 36 | void HeaderView::SchedulePaintForTitle() { |
| 37 | header_painter_->SchedulePaintForTitle(); |
| 38 | } |
| 39 | |
| 40 | void HeaderView::ResetWindowControls() { |
| 41 | caption_button_container_->ResetWindowControls(); |
| 42 | } |
| 43 | |
| 44 | int HeaderView::GetPreferredOnScreenHeight() { |
| 45 | if (is_immersive_delegate_ && target_widget_->IsFullscreen()) { |
| 46 | return static_cast<int>(GetPreferredHeight() * |
| 47 | fullscreen_visible_fraction_); |
| 48 | } |
| 49 | return GetPreferredHeight(); |
| 50 | } |
| 51 | |
| 52 | int HeaderView::GetPreferredHeight() { |
| 53 | // Calculating the preferred height requires at least one Layout(). |
| 54 | if (!did_layout_) |
| 55 | Layout(); |
| 56 | return header_painter_->GetHeaderHeightForPainting(); |
| 57 | } |
| 58 | |
| 59 | int HeaderView::GetMinimumWidth() const { |
| 60 | return header_painter_->GetMinimumHeaderWidth(); |
| 61 | } |
| 62 | |
xiyuan | 317fdbe | 2017-05-31 15:02:02 | [diff] [blame] | 63 | void HeaderView::SetAvatarIcon(const gfx::ImageSkia& avatar) { |
| 64 | const bool show = !avatar.isNull(); |
James Cook | b0bf8e8 | 2017-04-09 17:01:44 | [diff] [blame] | 65 | if (!show) { |
| 66 | if (!avatar_icon_) |
| 67 | return; |
| 68 | delete avatar_icon_; |
| 69 | avatar_icon_ = nullptr; |
| 70 | } else { |
xiyuan | 317fdbe | 2017-05-31 15:02:02 | [diff] [blame] | 71 | DCHECK_EQ(avatar.width(), avatar.height()); |
James Cook | b0bf8e8 | 2017-04-09 17:01:44 | [diff] [blame] | 72 | if (!avatar_icon_) { |
| 73 | avatar_icon_ = new views::ImageView(); |
| 74 | AddChildView(avatar_icon_); |
| 75 | } |
xiyuan | 317fdbe | 2017-05-31 15:02:02 | [diff] [blame] | 76 | avatar_icon_->SetImage(avatar); |
James Cook | b0bf8e8 | 2017-04-09 17:01:44 | [diff] [blame] | 77 | } |
| 78 | header_painter_->UpdateLeftHeaderView(avatar_icon_); |
| 79 | Layout(); |
| 80 | } |
| 81 | |
| 82 | void HeaderView::SizeConstraintsChanged() { |
| 83 | caption_button_container_->ResetWindowControls(); |
| 84 | caption_button_container_->UpdateSizeButtonVisibility(); |
| 85 | Layout(); |
| 86 | } |
| 87 | |
| 88 | void HeaderView::SetFrameColors(SkColor active_frame_color, |
| 89 | SkColor inactive_frame_color) { |
| 90 | header_painter_->SetFrameColors(active_frame_color, inactive_frame_color); |
| 91 | } |
| 92 | |
| 93 | SkColor HeaderView::GetActiveFrameColor() const { |
| 94 | return header_painter_->GetActiveFrameColor(); |
| 95 | } |
| 96 | |
| 97 | SkColor HeaderView::GetInactiveFrameColor() const { |
| 98 | return header_painter_->GetInactiveFrameColor(); |
| 99 | } |
| 100 | |
| 101 | /////////////////////////////////////////////////////////////////////////////// |
| 102 | // HeaderView, views::View overrides: |
| 103 | |
| 104 | void HeaderView::Layout() { |
| 105 | did_layout_ = true; |
| 106 | header_painter_->LayoutHeader(); |
| 107 | } |
| 108 | |
| 109 | void HeaderView::OnPaint(gfx::Canvas* canvas) { |
| 110 | bool paint_as_active = |
| 111 | target_widget_->non_client_view()->frame_view()->ShouldPaintAsActive(); |
| 112 | caption_button_container_->SetPaintAsActive(paint_as_active); |
| 113 | |
| 114 | HeaderPainter::Mode header_mode = paint_as_active |
| 115 | ? HeaderPainter::MODE_ACTIVE |
| 116 | : HeaderPainter::MODE_INACTIVE; |
| 117 | header_painter_->PaintHeader(canvas, header_mode); |
| 118 | } |
| 119 | |
| 120 | void HeaderView::ChildPreferredSizeChanged(views::View* child) { |
| 121 | // FrameCaptionButtonContainerView animates the visibility changes in |
| 122 | // UpdateSizeButtonVisibility(false). Due to this a new size is not available |
| 123 | // until the completion of the animation. Layout in response to the preferred |
| 124 | // size changes. |
| 125 | if (child != caption_button_container_) |
| 126 | return; |
| 127 | parent()->Layout(); |
| 128 | } |
| 129 | |
| 130 | /////////////////////////////////////////////////////////////////////////////// |
| 131 | // HeaderView, ShellObserver overrides: |
| 132 | |
| 133 | void HeaderView::OnOverviewModeStarting() { |
| 134 | caption_button_container_->SetVisible(false); |
| 135 | } |
| 136 | |
| 137 | void HeaderView::OnOverviewModeEnded() { |
| 138 | caption_button_container_->SetVisible(true); |
| 139 | } |
| 140 | |
oshima | 341337af | 2017-05-26 23:34:23 | [diff] [blame] | 141 | void HeaderView::OnMaximizeModeStarted() { |
James Cook | b0bf8e8 | 2017-04-09 17:01:44 | [diff] [blame] | 142 | caption_button_container_->UpdateSizeButtonVisibility(); |
| 143 | parent()->Layout(); |
| 144 | } |
| 145 | |
oshima | 341337af | 2017-05-26 23:34:23 | [diff] [blame] | 146 | void HeaderView::OnMaximizeModeEnded() { |
James Cook | b0bf8e8 | 2017-04-09 17:01:44 | [diff] [blame] | 147 | caption_button_container_->UpdateSizeButtonVisibility(); |
| 148 | parent()->Layout(); |
| 149 | } |
| 150 | |
| 151 | views::View* HeaderView::avatar_icon() const { |
| 152 | return avatar_icon_; |
| 153 | } |
| 154 | |
| 155 | /////////////////////////////////////////////////////////////////////////////// |
| 156 | // HeaderView, |
| 157 | // ImmersiveFullscreenControllerDelegate overrides: |
| 158 | |
| 159 | void HeaderView::OnImmersiveRevealStarted() { |
| 160 | fullscreen_visible_fraction_ = 0; |
| 161 | SetPaintToLayer(); |
| 162 | parent()->Layout(); |
| 163 | } |
| 164 | |
| 165 | void HeaderView::OnImmersiveRevealEnded() { |
| 166 | fullscreen_visible_fraction_ = 0; |
| 167 | DestroyLayer(); |
| 168 | parent()->Layout(); |
| 169 | } |
| 170 | |
| 171 | void HeaderView::OnImmersiveFullscreenExited() { |
| 172 | fullscreen_visible_fraction_ = 0; |
| 173 | DestroyLayer(); |
| 174 | parent()->Layout(); |
| 175 | } |
| 176 | |
| 177 | void HeaderView::SetVisibleFraction(double visible_fraction) { |
| 178 | if (fullscreen_visible_fraction_ != visible_fraction) { |
| 179 | fullscreen_visible_fraction_ = visible_fraction; |
| 180 | parent()->Layout(); |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | std::vector<gfx::Rect> HeaderView::GetVisibleBoundsInScreen() const { |
| 185 | // TODO(pkotwicz): Implement views::View::ConvertRectToScreen(). |
| 186 | gfx::Rect visible_bounds(GetVisibleBounds()); |
| 187 | gfx::Point visible_origin_in_screen(visible_bounds.origin()); |
| 188 | views::View::ConvertPointToScreen(this, &visible_origin_in_screen); |
| 189 | std::vector<gfx::Rect> bounds_in_screen; |
| 190 | bounds_in_screen.push_back( |
| 191 | gfx::Rect(visible_origin_in_screen, visible_bounds.size())); |
| 192 | return bounds_in_screen; |
| 193 | } |
| 194 | |
| 195 | } // namespace ash |