1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398
|
// Copyright 2016 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chromeos/ui/frame/header_view.h"
#include <memory>
#include <vector>
#include "base/auto_reset.h"
#include "base/memory/raw_ptr.h"
#include "chromeos/ui/base/window_properties.h"
#include "chromeos/ui/base/window_state_type.h"
#include "chromeos/ui/frame/caption_buttons/caption_button_model.h"
#include "chromeos/ui/frame/caption_buttons/frame_back_button.h"
#include "chromeos/ui/frame/caption_buttons/frame_caption_button_container_view.h"
#include "chromeos/ui/frame/caption_buttons/frame_center_button.h"
#include "chromeos/ui/frame/default_frame_header.h"
#include "chromeos/ui/frame/frame_utils.h"
#include "ui/aura/client/aura_constants.h"
#include "ui/base/metadata/metadata_impl_macros.h"
#include "ui/compositor/layer.h"
#include "ui/display/screen.h"
#include "ui/display/tablet_state.h"
#include "ui/views/controls/image_view.h"
#include "ui/views/widget/widget.h"
#include "ui/views/window/non_client_view.h"
namespace chromeos {
using ::chromeos::DefaultFrameHeader;
using ::chromeos::kFrameActiveColorKey;
using ::chromeos::kFrameInactiveColorKey;
// The view used to draw the content (background and title string)
// of the header. This is a separate view so that it can use
// different scaling strategy than the rest of the frame such
// as caption buttons.
class HeaderView::HeaderContentView : public views::View {
METADATA_HEADER(HeaderContentView, views::View)
public:
explicit HeaderContentView(HeaderView* header_view)
: header_view_(header_view) {}
HeaderContentView(const HeaderContentView&) = delete;
HeaderContentView& operator=(const HeaderContentView&) = delete;
~HeaderContentView() override = default;
// views::View:
views::PaintInfo::ScaleType GetPaintScaleType() const override {
return scale_type_;
}
void OnPaint(gfx::Canvas* canvas) override {
header_view_->PaintHeaderContent(canvas);
}
void SetScaleType(views::PaintInfo::ScaleType scale_type) {
scale_type_ = scale_type;
}
private:
raw_ptr<HeaderView> header_view_;
views::PaintInfo::ScaleType scale_type_ =
views::PaintInfo::ScaleType::kScaleWithEdgeSnapping;
};
BEGIN_METADATA(HeaderView, HeaderContentView)
END_METADATA
HeaderView::HeaderView(views::Widget* target_widget,
views::NonClientFrameView* frame_view)
: target_widget_(target_widget) {
header_content_view_ =
AddChildView(std::make_unique<HeaderContentView>(this));
caption_button_container_ =
AddChildView(std::make_unique<chromeos::FrameCaptionButtonContainerView>(
target_widget_));
frame_header_ = std::make_unique<DefaultFrameHeader>(
target_widget,
(frame_view ? static_cast<views::View*>(frame_view) : this),
caption_button_container_);
}
void HeaderView::Init() {
UpdateBackButton();
UpdateCenterButton();
frame_header_->UpdateFrameColors();
aura::Window* window = target_widget_->GetNativeWindow();
window_observation_.Observe(window);
display_observer_.emplace(this);
}
HeaderView::~HeaderView() = default;
void HeaderView::SchedulePaintForTitle() {
frame_header_->SchedulePaintForTitle();
}
void HeaderView::ResetWindowControls() {
caption_button_container_->ResetWindowControls();
}
int HeaderView::GetPreferredOnScreenHeight() {
if (in_immersive_mode_) {
return static_cast<int>(GetPreferredHeight() *
fullscreen_visible_fraction_);
}
return (target_widget_ && target_widget_->IsFullscreen())
? 0
: GetPreferredHeight();
}
int HeaderView::GetPreferredHeight() {
// Calculating the preferred height requires at least one layout.
if (!did_layout_)
DeprecatedLayoutImmediately();
return frame_header_->GetHeaderHeightForPainting();
}
int HeaderView::GetMinimumWidth() const {
return frame_header_->GetMinimumHeaderWidth();
}
void HeaderView::SetAvatarIcon(const gfx::ImageSkia& avatar) {
const bool show = !avatar.isNull();
if (!show) {
if (!avatar_icon_)
return;
delete avatar_icon_;
avatar_icon_ = nullptr;
} else {
DCHECK_EQ(avatar.width(), avatar.height());
if (!avatar_icon_) {
avatar_icon_ = new views::ImageView();
AddChildViewRaw(avatar_icon_.get());
}
avatar_icon_->SetImage(ui::ImageModel::FromImageSkia(avatar));
}
frame_header_->SetLeftHeaderView(avatar_icon_);
DeprecatedLayoutImmediately();
}
void HeaderView::UpdateCaptionButtons() {
caption_button_container_->ResetWindowControls();
caption_button_container_->UpdateCaptionButtonState(true /*=animate*/);
UpdateBackButton();
UpdateCenterButton();
DeprecatedLayoutImmediately();
}
void HeaderView::SetWidthInPixels(int width_in_pixels) {
frame_header_->SetWidthInPixels(width_in_pixels);
// If the width is given in pixels, use uniform scaling
// so that UndoDeviceScaleFactor can correctly undo the scaling.
header_content_view_->SetScaleType(
width_in_pixels > 0
? views::PaintInfo::ScaleType::kUniformScaling
: views::PaintInfo::ScaleType::kScaleWithEdgeSnapping);
}
void HeaderView::SetHeaderCornerRadius(int radius) {
frame_header_->SetHeaderCornerRadius(radius);
}
void HeaderView::Layout(PassKey) {
did_layout_ = true;
header_content_view_->SetBoundsRect(GetLocalBounds());
frame_header_->LayoutHeader();
}
void HeaderView::ChildPreferredSizeChanged(views::View* child) {
if (child != caption_button_container_)
return;
// May be null during view initialization.
if (parent())
parent()->DeprecatedLayoutImmediately();
}
bool HeaderView::IsDrawn() const {
if (is_drawn_override_)
return true;
return views::View::IsDrawn();
}
void HeaderView::OnWindowPropertyChanged(aura::Window* window,
const void* key,
intptr_t old) {
if (!target_widget_)
return;
DCHECK_EQ(target_widget_->GetNativeWindow(), window);
if (key == aura::client::kAvatarIconKey) {
gfx::ImageSkia* const avatar_icon =
window->GetProperty(aura::client::kAvatarIconKey);
SetAvatarIcon(avatar_icon ? *avatar_icon : gfx::ImageSkia());
} else if (key == kFrameActiveColorKey || key == kFrameInactiveColorKey) {
frame_header_->UpdateFrameColors();
} else if (key == aura::client::kShowStateKey) {
frame_header_->OnShowStateChanged(
window->GetProperty(aura::client::kShowStateKey));
} else if (key == chromeos::kWindowStateTypeKey) {
// Float state is an ash specific state that changes the header UI. It isn't
// a show state so we need to watch the window state type key as well.
if (window->GetProperty(chromeos::kWindowStateTypeKey) ==
chromeos::WindowStateType::kFloated ||
static_cast<chromeos::WindowStateType>(old) ==
chromeos::WindowStateType::kFloated) {
frame_header_->OnFloatStateChanged();
}
}
}
void HeaderView::OnWindowDestroying(aura::Window* window) {
DCHECK(window_observation_.IsObservingSource(window));
window_observation_.Reset();
display_observer_.reset();
// A HeaderView may outlive the target widget.
target_widget_ = nullptr;
}
void HeaderView::OnDisplayMetricsChanged(const display::Display& display,
uint32_t changed_metrics) {
// When the display is rotated, the frame header may have invalid snap icons.
// For example, rotating from landscape display to portrait display layout
// should update snap icons from left/right arrows to upward/downward arrows
// for top and bottom snaps.
if ((changed_metrics & display::DisplayObserver::DISPLAY_METRIC_ROTATION) &&
frame_header_) {
CHECK(target_widget_);
frame_header_->LayoutHeader();
}
}
void HeaderView::OnDisplayTabletStateChanged(display::TabletState state) {
switch (state) {
case display::TabletState::kInTabletMode:
UpdateCaptionButtonsVisibility();
caption_button_container_->UpdateCaptionButtonState(true /*=animate*/);
parent()->DeprecatedLayoutImmediately();
if (target_widget_) {
target_widget_->non_client_view()->DeprecatedLayoutImmediately();
}
break;
case display::TabletState::kInClamshellMode:
UpdateCaptionButtonsVisibility();
caption_button_container_->UpdateCaptionButtonState(true /*=animate*/);
parent()->DeprecatedLayoutImmediately();
if (target_widget_)
target_widget_->non_client_view()->DeprecatedLayoutImmediately();
break;
case display::TabletState::kEnteringTabletMode:
break;
case display::TabletState::kExitingTabletMode:
break;
}
}
views::View* HeaderView::avatar_icon() const {
return avatar_icon_;
}
void HeaderView::SetShouldPaintHeader(bool paint) {
if (should_paint_ == paint)
return;
should_paint_ = paint;
UpdateCaptionButtonsVisibility();
SchedulePaint();
}
views::FrameCaptionButton* HeaderView::GetBackButton() {
return frame_header_->GetBackButton();
}
void HeaderView::OnImmersiveRevealStarted() {
fullscreen_visible_fraction_ = 0;
add_layer_for_immersive_ = !layer();
if (add_layer_for_immersive_)
SetPaintToLayer();
// AppWindow may call this before being added to the widget.
// https://crbug.com/825260.
if (layer()->parent()) {
// The immersive layer should always be top.
layer()->parent()->StackAtTop(layer());
}
parent()->DeprecatedLayoutImmediately();
}
void HeaderView::OnImmersiveRevealEnded() {
fullscreen_visible_fraction_ = 0;
if (add_layer_for_immersive_)
DestroyLayer();
parent()->DeprecatedLayoutImmediately();
}
void HeaderView::OnImmersiveFullscreenEntered() {
in_immersive_mode_ = true;
parent()->InvalidateLayout();
if (!immersive_mode_changed_callback_.is_null())
immersive_mode_changed_callback_.Run();
}
void HeaderView::OnImmersiveFullscreenExited() {
in_immersive_mode_ = false;
fullscreen_visible_fraction_ = 0;
if (add_layer_for_immersive_)
DestroyLayer();
parent()->InvalidateLayout();
if (!immersive_mode_changed_callback_.is_null())
immersive_mode_changed_callback_.Run();
}
void HeaderView::SetVisibleFraction(double visible_fraction) {
if (fullscreen_visible_fraction_ != visible_fraction) {
fullscreen_visible_fraction_ = visible_fraction;
parent()->DeprecatedLayoutImmediately();
}
}
std::vector<gfx::Rect> HeaderView::GetVisibleBoundsInScreen() const {
// TODO(pkotwicz): Implement views::View::ConvertRectToScreen().
base::AutoReset<bool> reset(&is_drawn_override_, true);
gfx::Rect visible_bounds(GetVisibleBounds());
gfx::Point visible_origin_in_screen(visible_bounds.origin());
views::View::ConvertPointToScreen(this, &visible_origin_in_screen);
std::vector<gfx::Rect> bounds_in_screen;
bounds_in_screen.push_back(
gfx::Rect(visible_origin_in_screen, visible_bounds.size()));
return bounds_in_screen;
}
void HeaderView::Relayout() {
parent()->DeprecatedLayoutImmediately();
}
void HeaderView::PaintHeaderContent(gfx::Canvas* canvas) {
if (!should_paint_ || !target_widget_)
return;
frame_header_->PaintHeader(canvas);
}
void HeaderView::UpdateBackButton() {
bool has_back_button = caption_button_container_->model()->IsVisible(
views::CAPTION_BUTTON_ICON_BACK);
views::FrameCaptionButton* back_button = frame_header_->GetBackButton();
if (has_back_button) {
if (!back_button) {
back_button = new chromeos::FrameBackButton();
AddChildViewRaw(back_button);
frame_header_->SetBackButton(back_button);
}
back_button->SetEnabled(caption_button_container_->model()->IsEnabled(
views::CAPTION_BUTTON_ICON_BACK));
} else {
delete back_button;
frame_header_->SetBackButton(nullptr);
}
}
void HeaderView::UpdateCenterButton() {
bool is_center_button_visible = caption_button_container_->model()->IsVisible(
views::CAPTION_BUTTON_ICON_CENTER);
auto* center_button = frame_header_->GetCenterButton();
if (!center_button)
return;
if (is_center_button_visible) {
if (!center_button->parent())
AddChildViewRaw(center_button);
center_button->SetVisible(true);
} else {
center_button->SetVisible(false);
}
}
void HeaderView::UpdateCaptionButtonsVisibility() {
if (!target_widget_)
return;
caption_button_container_->SetVisible(should_paint_);
}
BEGIN_METADATA(HeaderView)
END_METADATA
} // namespace chromeos
|