Switch global error menu icon to vectorized MD asset
[chromium-blink-merge.git] / ash / host / transformer_helper.cc
blobaf56d8a8d64fdaa2c38e26e9289ad7d9d8fab353
1 // Copyright 2014 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.
5 #include "ash/host/transformer_helper.h"
7 #include "ash/host/ash_window_tree_host.h"
8 #include "ash/host/root_window_transformer.h"
9 #include "ui/aura/window.h"
10 #include "ui/aura/window_tree_host.h"
11 #include "ui/compositor/dip_util.h"
12 #include "ui/gfx/geometry/insets.h"
13 #include "ui/gfx/geometry/rect.h"
14 #include "ui/gfx/geometry/rect_f.h"
15 #include "ui/gfx/geometry/size.h"
16 #include "ui/gfx/geometry/size_conversions.h"
17 #include "ui/gfx/transform.h"
19 namespace ash {
20 namespace {
22 // A simple RootWindowTransformer without host insets.
23 class SimpleRootWindowTransformer : public RootWindowTransformer {
24 public:
25 SimpleRootWindowTransformer(const aura::Window* root_window,
26 const gfx::Transform& transform)
27 : root_window_(root_window), transform_(transform) {}
29 // RootWindowTransformer overrides:
30 gfx::Transform GetTransform() const override { return transform_; }
32 gfx::Transform GetInverseTransform() const override {
33 gfx::Transform invert;
34 if (!transform_.GetInverse(&invert))
35 return transform_;
36 return invert;
39 gfx::Rect GetRootWindowBounds(const gfx::Size& host_size) const override {
40 gfx::Rect bounds(host_size);
41 gfx::RectF new_bounds(ui::ConvertRectToDIP(root_window_->layer(), bounds));
42 transform_.TransformRect(&new_bounds);
43 return gfx::Rect(gfx::ToFlooredSize(new_bounds.size()));
46 gfx::Insets GetHostInsets() const override { return gfx::Insets(); }
48 private:
49 ~SimpleRootWindowTransformer() override {}
51 const aura::Window* root_window_;
52 const gfx::Transform transform_;
54 DISALLOW_COPY_AND_ASSIGN(SimpleRootWindowTransformer);
57 } // namespace
59 TransformerHelper::TransformerHelper(AshWindowTreeHost* ash_host)
60 : ash_host_(ash_host) {
63 TransformerHelper::~TransformerHelper() {}
65 void TransformerHelper::Init() {
66 SetTransform(gfx::Transform());
69 gfx::Insets TransformerHelper::GetHostInsets() const {
70 return transformer_->GetHostInsets();
73 void TransformerHelper::SetTransform(const gfx::Transform& transform) {
74 scoped_ptr<RootWindowTransformer> transformer(new SimpleRootWindowTransformer(
75 ash_host_->AsWindowTreeHost()->window(), transform));
76 SetRootWindowTransformer(transformer.Pass());
79 void TransformerHelper::SetRootWindowTransformer(
80 scoped_ptr<RootWindowTransformer> transformer) {
81 transformer_ = transformer.Pass();
82 aura::WindowTreeHost* host = ash_host_->AsWindowTreeHost();
83 aura::Window* window = host->window();
84 window->SetTransform(transformer_->GetTransform());
85 // If the layer is not animating, then we need to update the root window
86 // size immediately.
87 if (!window->layer()->GetAnimator()->is_animating())
88 host->UpdateRootWindowSize(host->GetBounds().size());
91 gfx::Transform TransformerHelper::GetTransform() const {
92 float scale = ui::GetDeviceScaleFactor(
93 ash_host_->AsWindowTreeHost()->window()->layer());
94 gfx::Transform transform;
95 transform.Scale(scale, scale);
96 transform *= transformer_->GetTransform();
97 return transform;
100 gfx::Transform TransformerHelper::GetInverseTransform() const {
101 float scale = ui::GetDeviceScaleFactor(
102 ash_host_->AsWindowTreeHost()->window()->layer());
103 gfx::Transform transform;
104 transform.Scale(1.0f / scale, 1.0f / scale);
105 return transformer_->GetInverseTransform() * transform;
108 void TransformerHelper::UpdateWindowSize(const gfx::Size& host_size) {
109 ash_host_->AsWindowTreeHost()->window()->SetBounds(
110 transformer_->GetRootWindowBounds(host_size));
113 } // namespace ash