blob: 8b8358b6a099210357bb05e69c03e598d3d6a445 [file] [log] [blame]
James Cookb0bf8e82017-04-09 17:01:441// 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 Cookb0bf8e82017-04-09 17:01:449#include "ash/shell.h"
James Cookb0bf8e82017-04-09 17:01:4410#include "ui/views/controls/image_view.h"
11#include "ui/views/widget/widget.h"
12
13namespace ash {
14
15HeaderView::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 Cookb0bf8e82017-04-09 17:01:4428
29 Shell::Get()->AddShellObserver(this);
30}
31
32HeaderView::~HeaderView() {
33 Shell::Get()->RemoveShellObserver(this);
34}
35
36void HeaderView::SchedulePaintForTitle() {
37 header_painter_->SchedulePaintForTitle();
38}
39
40void HeaderView::ResetWindowControls() {
41 caption_button_container_->ResetWindowControls();
42}
43
44int 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
52int 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
59int HeaderView::GetMinimumWidth() const {
60 return header_painter_->GetMinimumHeaderWidth();
61}
62
xiyuan317fdbe2017-05-31 15:02:0263void HeaderView::SetAvatarIcon(const gfx::ImageSkia& avatar) {
64 const bool show = !avatar.isNull();
James Cookb0bf8e82017-04-09 17:01:4465 if (!show) {
66 if (!avatar_icon_)
67 return;
68 delete avatar_icon_;
69 avatar_icon_ = nullptr;
70 } else {
xiyuan317fdbe2017-05-31 15:02:0271 DCHECK_EQ(avatar.width(), avatar.height());
James Cookb0bf8e82017-04-09 17:01:4472 if (!avatar_icon_) {
73 avatar_icon_ = new views::ImageView();
74 AddChildView(avatar_icon_);
75 }
xiyuan317fdbe2017-05-31 15:02:0276 avatar_icon_->SetImage(avatar);
James Cookb0bf8e82017-04-09 17:01:4477 }
78 header_painter_->UpdateLeftHeaderView(avatar_icon_);
79 Layout();
80}
81
82void HeaderView::SizeConstraintsChanged() {
83 caption_button_container_->ResetWindowControls();
84 caption_button_container_->UpdateSizeButtonVisibility();
85 Layout();
86}
87
88void HeaderView::SetFrameColors(SkColor active_frame_color,
89 SkColor inactive_frame_color) {
90 header_painter_->SetFrameColors(active_frame_color, inactive_frame_color);
91}
92
93SkColor HeaderView::GetActiveFrameColor() const {
94 return header_painter_->GetActiveFrameColor();
95}
96
97SkColor HeaderView::GetInactiveFrameColor() const {
98 return header_painter_->GetInactiveFrameColor();
99}
100
101///////////////////////////////////////////////////////////////////////////////
102// HeaderView, views::View overrides:
103
104void HeaderView::Layout() {
105 did_layout_ = true;
106 header_painter_->LayoutHeader();
107}
108
109void 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
120void 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
133void HeaderView::OnOverviewModeStarting() {
134 caption_button_container_->SetVisible(false);
135}
136
137void HeaderView::OnOverviewModeEnded() {
138 caption_button_container_->SetVisible(true);
139}
140
oshima341337af2017-05-26 23:34:23141void HeaderView::OnMaximizeModeStarted() {
James Cookb0bf8e82017-04-09 17:01:44142 caption_button_container_->UpdateSizeButtonVisibility();
143 parent()->Layout();
144}
145
oshima341337af2017-05-26 23:34:23146void HeaderView::OnMaximizeModeEnded() {
James Cookb0bf8e82017-04-09 17:01:44147 caption_button_container_->UpdateSizeButtonVisibility();
148 parent()->Layout();
149}
150
151views::View* HeaderView::avatar_icon() const {
152 return avatar_icon_;
153}
154
155///////////////////////////////////////////////////////////////////////////////
156// HeaderView,
157// ImmersiveFullscreenControllerDelegate overrides:
158
159void HeaderView::OnImmersiveRevealStarted() {
160 fullscreen_visible_fraction_ = 0;
161 SetPaintToLayer();
162 parent()->Layout();
163}
164
165void HeaderView::OnImmersiveRevealEnded() {
166 fullscreen_visible_fraction_ = 0;
167 DestroyLayer();
168 parent()->Layout();
169}
170
171void HeaderView::OnImmersiveFullscreenExited() {
172 fullscreen_visible_fraction_ = 0;
173 DestroyLayer();
174 parent()->Layout();
175}
176
177void HeaderView::SetVisibleFraction(double visible_fraction) {
178 if (fullscreen_visible_fraction_ != visible_fraction) {
179 fullscreen_visible_fraction_ = visible_fraction;
180 parent()->Layout();
181 }
182}
183
184std::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