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