File: partial_magnifier_controller.cc

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (129 lines) | stat: -rw-r--r-- 3,873 bytes parent folder | download | duplicates (6)
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
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "ash/accessibility/magnifier/partial_magnifier_controller.h"

#include "ash/accessibility/magnifier/magnifier_glass.h"
#include "ash/shell.h"
#include "ash/system/palette/palette_utils.h"
#include "ui/aura/window_event_dispatcher.h"
#include "ui/aura/window_tree_host.h"
#include "ui/events/event.h"
#include "ui/events/event_constants.h"
#include "ui/events/types/event_type.h"
#include "ui/wm/core/coordinate_conversion.h"

namespace ash {
namespace {

// Ratio of magnifier scale.
constexpr float kMagnificationScale = 2.f;
// Radius of the magnifying glass in DIP. This does not include the thickness
// of the magnifying glass shadow and border.
constexpr int kMagnifierRadius = 188;

}  // namespace

PartialMagnifierController::PartialMagnifierController() {
  Shell::Get()->AddPreTargetHandler(this);

  MagnifierGlass::Params params = {kMagnificationScale, kMagnifierRadius};
  magnifier_glass_ = std::make_unique<MagnifierGlass>(std::move(params));
}

PartialMagnifierController::~PartialMagnifierController() {
  Shell::Get()->RemovePreTargetHandler(this);
}

void PartialMagnifierController::SetEnabled(bool enabled) {
  if (is_enabled_ == enabled)
    return;

  is_enabled_ = enabled;
  SetActive(false);
}

void PartialMagnifierController::SwitchTargetRootWindowIfNeeded(
    aura::Window* new_root_window) {
  if (new_root_window == current_root_window_)
    return;
  current_root_window_ = new_root_window;

  // |new_root_window| could be null, e.g. current mouse/touch point is beyond
  // the bounds of the displays. Deactivate and return.
  if (!current_root_window_) {
    SetActive(false);
    return;
  }

  DCHECK(current_root_window_->IsRootWindow());

  // Arbitrarily place the magnifier at the center of the new root window.
  // This function's caller is expected to call |ShowFor| afterwards with
  // more appropriate location if possible.
  if (is_enabled_ && is_active_) {
    magnifier_glass_->ShowFor(current_root_window_,
                              current_root_window_->bounds().CenterPoint());
  }
}

void PartialMagnifierController::OnTouchEvent(ui::TouchEvent* event) {
  OnLocatedEvent(event, event->pointer_details());
}

void PartialMagnifierController::SetActive(bool active) {
  // Fail if we're trying to activate while disabled.
  DCHECK(is_enabled_ || !active);

  if (is_active_ == active)
    return;

  is_active_ = active;
  if (!is_active_)
    magnifier_glass_->Close();
}

void PartialMagnifierController::OnLocatedEvent(
    ui::LocatedEvent* event,
    const ui::PointerDetails& pointer_details) {
  if (!is_enabled_)
    return;

  if (pointer_details.pointer_type != ui::EventPointerType::kPen) {
    return;
  }

  // Compute the event location in screen space.
  aura::Window* target = static_cast<aura::Window*>(event->target());
  aura::Window* event_root = target->GetRootWindow();
  gfx::Point screen_point = event->root_location();
  wm::ConvertPointToScreen(event_root, &screen_point);

  SwitchTargetRootWindowIfNeeded(event_root);

  const bool palette_contains_point =
      palette_utils::PaletteContainsPointInScreen(screen_point);
  // If the stylus is pressed on the palette icon or widget, do not activate.
  if (event->type() == ui::EventType::kTouchPressed &&
      !palette_contains_point) {
    SetActive(true);
  }

  if (event->type() == ui::EventType::kTouchReleased ||
      event->type() == ui::EventType::kTouchCancelled) {
    SetActive(false);
  }

  if (!is_active_)
    return;

  magnifier_glass_->ShowFor(current_root_window_, event->root_location());

  // If the stylus is over the palette icon or widget, do not consume the event.
  if (!palette_contains_point) {
    event->StopPropagation();
  }
}

}  // namespace ash