blob: c59c5774b4fe34a00977d05bd1801c147549afc7 [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"
9#include "ash/session/session_state_delegate.h"
10#include "ash/shell.h"
sky11cf8db92017-04-10 23:38:0811#include "ash/shell_port.h"
James Cookb0bf8e82017-04-09 17:01:4412#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
17namespace ash {
18
19HeaderView::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
37HeaderView::~HeaderView() {
38 Shell::Get()->RemoveShellObserver(this);
39}
40
41void HeaderView::SchedulePaintForTitle() {
42 header_painter_->SchedulePaintForTitle();
43}
44
45void HeaderView::ResetWindowControls() {
46 caption_button_container_->ResetWindowControls();
47}
48
49int 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
57int 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
64int HeaderView::GetMinimumWidth() const {
65 return header_painter_->GetMinimumHeaderWidth();
66}
67
68void HeaderView::UpdateAvatarIcon() {
sky11cf8db92017-04-10 23:38:0869 SessionStateDelegate* delegate = ShellPort::Get()->GetSessionStateDelegate();
James Cookb0bf8e82017-04-09 17:01:4470 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
90void HeaderView::SizeConstraintsChanged() {
91 caption_button_container_->ResetWindowControls();
92 caption_button_container_->UpdateSizeButtonVisibility();
93 Layout();
94}
95
96void HeaderView::SetFrameColors(SkColor active_frame_color,
97 SkColor inactive_frame_color) {
98 header_painter_->SetFrameColors(active_frame_color, inactive_frame_color);
99}
100
101SkColor HeaderView::GetActiveFrameColor() const {
102 return header_painter_->GetActiveFrameColor();
103}
104
105SkColor HeaderView::GetInactiveFrameColor() const {
106 return header_painter_->GetInactiveFrameColor();
107}
108
109///////////////////////////////////////////////////////////////////////////////
110// HeaderView, views::View overrides:
111
112void HeaderView::Layout() {
113 did_layout_ = true;
114 header_painter_->LayoutHeader();
115}
116
117void 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
128void 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
141void HeaderView::OnOverviewModeStarting() {
142 caption_button_container_->SetVisible(false);
143}
144
145void HeaderView::OnOverviewModeEnded() {
146 caption_button_container_->SetVisible(true);
147}
148
oshima341337af2017-05-26 23:34:23149void HeaderView::OnMaximizeModeStarted() {
James Cookb0bf8e82017-04-09 17:01:44150 caption_button_container_->UpdateSizeButtonVisibility();
151 parent()->Layout();
152}
153
oshima341337af2017-05-26 23:34:23154void HeaderView::OnMaximizeModeEnded() {
James Cookb0bf8e82017-04-09 17:01:44155 caption_button_container_->UpdateSizeButtonVisibility();
156 parent()->Layout();
157}
158
159views::View* HeaderView::avatar_icon() const {
160 return avatar_icon_;
161}
162
163///////////////////////////////////////////////////////////////////////////////
164// HeaderView,
165// ImmersiveFullscreenControllerDelegate overrides:
166
167void HeaderView::OnImmersiveRevealStarted() {
168 fullscreen_visible_fraction_ = 0;
169 SetPaintToLayer();
170 parent()->Layout();
171}
172
173void HeaderView::OnImmersiveRevealEnded() {
174 fullscreen_visible_fraction_ = 0;
175 DestroyLayer();
176 parent()->Layout();
177}
178
179void HeaderView::OnImmersiveFullscreenExited() {
180 fullscreen_visible_fraction_ = 0;
181 DestroyLayer();
182 parent()->Layout();
183}
184
185void HeaderView::SetVisibleFraction(double visible_fraction) {
186 if (fullscreen_visible_fraction_ != visible_fraction) {
187 fullscreen_visible_fraction_ = visible_fraction;
188 parent()->Layout();
189 }
190}
191
192std::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