summaryrefslogtreecommitdiff
path: root/chromium/ash/magnifier/partial_magnification_controller.cc
blob: a24ba51d4e95e50b7489dda343f12007af598da8 (plain)
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
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "ash/magnifier/partial_magnification_controller.h"

#include "ash/shell.h"
#include "ash/shell_window_ids.h"
#include "ui/aura/root_window.h"
#include "ui/views/corewm/compound_event_filter.h"
#include "ui/aura/window.h"
#include "ui/aura/window_property.h"
#include "ui/gfx/screen.h"
#include "ui/compositor/layer.h"
#include "ui/views/layout/fill_layout.h"
#include "ui/views/widget/widget.h"
#include "ui/views/widget/widget_delegate.h"

namespace {

const float kMinPartialMagnifiedScaleThreshold = 1.1f;

// Number of pixels to make the border of the magnified area.
const int kZoomInset = 16;

// Width of the magnified area.
const int kMagnifierWidth = 200;

// Height of the magnified area.
const int kMagnifierHeight = 200;

// Name of the magnifier window.
const char kPartialMagniferWindowName[] = "PartialMagnifierWindow";

}  // namespace

namespace ash {

PartialMagnificationController::PartialMagnificationController()
    : is_on_zooming_(false),
      is_enabled_(false),
      scale_(kNonPartialMagnifiedScale),
      zoom_widget_(NULL) {
  Shell::GetInstance()->AddPreTargetHandler(this);
}

PartialMagnificationController::~PartialMagnificationController() {
  CloseMagnifierWindow();

  Shell::GetInstance()->RemovePreTargetHandler(this);
}

void PartialMagnificationController::SetScale(float scale) {
  if (!is_enabled_)
    return;

  scale_ = scale;

  if (IsPartialMagnified()) {
    CreateMagnifierWindow();
  } else {
    CloseMagnifierWindow();
  }
}

void PartialMagnificationController::SetEnabled(bool enabled) {
  if (enabled) {
    is_enabled_ = enabled;
    SetScale(kDefaultPartialMagnifiedScale);
  } else {
    SetScale(kNonPartialMagnifiedScale);
    is_enabled_ = enabled;
  }
}

////////////////////////////////////////////////////////////////////////////////
// PartialMagnificationController: ui::EventHandler implementation

void PartialMagnificationController::OnMouseEvent(ui::MouseEvent* event) {
  if (IsPartialMagnified() && event->type() == ui::ET_MOUSE_MOVED) {
    aura::Window* target = static_cast<aura::Window*>(event->target());
    aura::Window* current_root = target->GetRootWindow();
    // TODO(zork): Handle the case where the event is captured on a different
    // display, such as when a menu is opened.
    gfx::Rect root_bounds = current_root->bounds();

    if (root_bounds.Contains(event->root_location())) {
      SwitchTargetRootWindow(current_root);

      OnMouseMove(event->root_location());
    }
  }
}

////////////////////////////////////////////////////////////////////////////////
// PartialMagnificationController: aura::WindowObserver implementation

void PartialMagnificationController::OnWindowDestroying(
    aura::Window* window) {
  CloseMagnifierWindow();

  aura::Window* new_root_window = GetCurrentRootWindow();
  if (new_root_window != window)
    SwitchTargetRootWindow(new_root_window);
}

void PartialMagnificationController::OnWidgetDestroying(
    views::Widget* widget) {
  DCHECK_EQ(widget, zoom_widget_);
  RemoveZoomWidgetObservers();
  zoom_widget_ = NULL;
}

void PartialMagnificationController::OnMouseMove(
    const gfx::Point& location_in_root) {
  gfx::Point origin(location_in_root);

  origin.Offset(-kMagnifierWidth / 2, -kMagnifierHeight / 2);

  if (zoom_widget_) {
    zoom_widget_->SetBounds(gfx::Rect(origin.x(), origin.y(),
                                      kMagnifierWidth, kMagnifierHeight));
  }
}

bool PartialMagnificationController::IsPartialMagnified() const {
  return scale_ >= kMinPartialMagnifiedScaleThreshold;
}

void PartialMagnificationController::CreateMagnifierWindow() {
  if (zoom_widget_)
    return;

  aura::Window* root_window = GetCurrentRootWindow();
  if (!root_window)
    return;

  root_window->AddObserver(this);

  gfx::Point mouse(root_window->GetDispatcher()->GetLastMouseLocationInRoot());

  zoom_widget_ = new views::Widget;
  views::Widget::InitParams params(
      views::Widget::InitParams::TYPE_WINDOW_FRAMELESS);
  params.can_activate = false;
  params.accept_events = false;
  params.opacity = views::Widget::InitParams::TRANSLUCENT_WINDOW;
  params.parent = root_window;
  zoom_widget_->Init(params);
  zoom_widget_->SetBounds(gfx::Rect(mouse.x() - kMagnifierWidth / 2,
                                    mouse.y() - kMagnifierHeight / 2,
                                    kMagnifierWidth, kMagnifierHeight));
  zoom_widget_->set_focus_on_creation(false);
  zoom_widget_->Show();

  aura::Window* window = zoom_widget_->GetNativeView();
  window->SetName(kPartialMagniferWindowName);

  zoom_widget_->GetNativeView()->layer()->SetBounds(
      gfx::Rect(0, 0,
                kMagnifierWidth,
                kMagnifierHeight));
  zoom_widget_->GetNativeView()->layer()->SetBackgroundZoom(
      scale_,
      kZoomInset);

  zoom_widget_->AddObserver(this);
}

void PartialMagnificationController::CloseMagnifierWindow() {
  if (zoom_widget_) {
    RemoveZoomWidgetObservers();
    zoom_widget_->Close();
    zoom_widget_ = NULL;
  }
}

void PartialMagnificationController::RemoveZoomWidgetObservers() {
  DCHECK(zoom_widget_);
  zoom_widget_->RemoveObserver(this);
  aura::Window* root_window =
      zoom_widget_->GetNativeView()->GetRootWindow();
  DCHECK(root_window);
  root_window->RemoveObserver(this);
}

void PartialMagnificationController::SwitchTargetRootWindow(
    aura::Window* new_root_window) {
  if (zoom_widget_ &&
      new_root_window == zoom_widget_->GetNativeView()->GetRootWindow())
    return;

  CloseMagnifierWindow();

  // Recreate the magnifier window by updating the scale factor.
  SetScale(GetScale());
}

aura::Window* PartialMagnificationController::GetCurrentRootWindow() {
  aura::Window::Windows root_windows = Shell::GetAllRootWindows();
  for (aura::Window::Windows::const_iterator iter = root_windows.begin();
       iter != root_windows.end(); ++iter) {
    aura::Window* root_window = *iter;
    if (root_window->ContainsPointInRoot(
            root_window->GetDispatcher()->GetLastMouseLocationInRoot()))
      return root_window;
  }
  return NULL;
}

}  // namespace ash