File: mako_bubble_event_handler.cc

package info (click to toggle)
chromium 138.0.7204.183-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 6,080,960 kB
  • sloc: cpp: 34,937,079; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,954; 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,811; 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 (309 lines) | stat: -rw-r--r-- 10,383 bytes parent folder | download | duplicates (5)
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
// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/ui/webui/ash/mako/mako_bubble_event_handler.h"

#include <variant>

#include "ash/constants/ash_features.h"
#include "third_party/skia/include/core/SkRegion.h"
#include "ui/display/screen.h"
#include "ui/events/event.h"
#include "ui/gfx/geometry/rect.h"
#include "ui/gfx/geometry/vector2d.h"

namespace ash {

namespace {

using State = MakoBubbleEventHandler::State;
using InitialState = MakoBubbleEventHandler::InitialState;
using DraggingState = MakoBubbleEventHandler::DraggingState;
using ResizingDirection = MakoBubbleEventHandler::ResizingDirection;
using ResizingState = MakoBubbleEventHandler::ResizingState;

constexpr int kResizingRegionWidth = 4;
constexpr int kMinWidgetHeight = 343;
constexpr int kMinWidgetWidth = 440;
constexpr int kWidgetCornerRadius = 20;
constexpr int kWidgetPadding = 4;

bool IsInSameDisplay(const gfx::Rect& original_bounds,
                     const gfx::Rect& new_bounds) {
  display::Screen* screen = display::Screen::GetScreen();
  if (screen == nullptr) {
    return false;
  }
  display::Display original_display =
      screen->GetDisplayMatching(original_bounds);
  display::Display new_display = screen->GetDisplayMatching(new_bounds);
  return new_display.id() == original_display.id();
}

// Converts resizing direction to unit vector.
gfx::Vector2d MapResizingDirectionToVector(ResizingDirection direction) {
  switch (direction) {
    case ResizingDirection::kTop:
      return gfx::Vector2d(0, -1);
    case ResizingDirection::kBottom:
      return gfx::Vector2d(0, +1);
    case ResizingDirection::kLeft:
      return gfx::Vector2d(-1, 0);
    case ResizingDirection::kRight:
      return gfx::Vector2d(+1, 0);
    case ResizingDirection::kTopLeft:
      return gfx::Vector2d(-1, -1);
    case ResizingDirection::kTopRight:
      return gfx::Vector2d(+1, -1);
    case ResizingDirection::kBottomLeft:
      return gfx::Vector2d(-1, +1);
    case ResizingDirection::kBottomRight:
      return gfx::Vector2d(+1, +1);
    default:
      return gfx::Vector2d(0, 0);
  }
}

ui::mojom::CursorType MapResizingDirectionToCursorType(
    ResizingDirection direction) {
  switch (direction) {
    case ResizingDirection::kTop:
      return ui::mojom::CursorType::kNorthResize;
    case ResizingDirection::kBottom:
      return ui::mojom::CursorType::kSouthResize;
    case ResizingDirection::kLeft:
      return ui::mojom::CursorType::kWestResize;
    case ResizingDirection::kRight:
      return ui::mojom::CursorType::kEastResize;
    case ResizingDirection::kTopLeft:
      return ui::mojom::CursorType::kNorthWestResize;
    case ResizingDirection::kTopRight:
      return ui::mojom::CursorType::kNorthEastResize;
    case ResizingDirection::kBottomLeft:
      return ui::mojom::CursorType::kSouthWestResize;
    case ResizingDirection::kBottomRight:
      return ui::mojom::CursorType::kSouthEastResize;
    default:
      return ui::mojom::CursorType::kPointer;
  }
}

ResizingDirection ComputeResizingDirection(gfx::Rect bounds,
                                           gfx::Vector2d pointer_pos) {
  // Detect resizing on corners.
  if (pointer_pos.x() < (kWidgetCornerRadius + kWidgetPadding) &&
      pointer_pos.y() < (kWidgetCornerRadius + kWidgetPadding)) {
    return ResizingDirection::kTopLeft;
  }
  if (pointer_pos.x() >
          (bounds.width() - kWidgetCornerRadius - kWidgetPadding) &&
      pointer_pos.y() < (kWidgetCornerRadius + kWidgetPadding)) {
    return ResizingDirection::kTopRight;
  }
  if (pointer_pos.x() < (kWidgetCornerRadius + kWidgetPadding) &&
      pointer_pos.y() >
          (bounds.height() - kWidgetCornerRadius - kWidgetPadding)) {
    return ResizingDirection::kBottomLeft;
  }
  if (pointer_pos.x() >
          (bounds.width() - kWidgetCornerRadius - kWidgetPadding) &&
      pointer_pos.y() >
          (bounds.height() - kWidgetCornerRadius - kWidgetPadding)) {
    return ResizingDirection::kBottomRight;
  }

  // Detect resizing on edges.
  if (pointer_pos.x() < kResizingRegionWidth) {
    return ResizingDirection::kLeft;
  }
  if (pointer_pos.x() > bounds.width() - kResizingRegionWidth) {
    return ResizingDirection::kRight;
  }
  if (pointer_pos.y() < kResizingRegionWidth) {
    return ResizingDirection::kTop;
  }
  if (pointer_pos.y() > bounds.height() - kResizingRegionWidth) {
    return ResizingDirection::kBottom;
  }

  // Otherwise, it's not resizing.
  return ResizingDirection::kNone;
}

class StateProcessFunction {
 public:
  StateProcessFunction(ui::LocatedEvent* event,
                       MakoBubbleEventHandler::Delegate* delegate)
      : event_(event), delegate_(delegate) {}

  State operator()(const InitialState& s) {
    // Re-computes cursor type when touch point moves.
    if (event_->type() == ui::EventType::kTouchMoved ||
        event_->type() == ui::EventType::kMouseMoved) {
      if (delegate_->IsResizingEnabled()) {
        ResizingDirection direction = ComputeResizingDirection(
            /*bounds=*/GetBoundsInScreen(),
            /*pointer_pos=*/GetPointerPos());
        delegate_->SetCursor(
            ui::Cursor(MapResizingDirectionToCursorType(direction)));
      }

      return InitialState{};
    }

    if (event_->type() == ui::EventType::kTouchPressed ||
        event_->type() == ui::EventType::kMousePressed) {
      if (delegate_->IsResizingEnabled()) {
        ResizingDirection direction = ComputeResizingDirection(
            /*bounds=*/GetBoundsInScreen(),
            /*pointer_pos=*/GetPointerPos());

        // Starts resizing.
        if (direction != ResizingDirection::kNone) {
          MarkEventHandled();
          return ResizingState{
              .resizing_direction = direction,
              .original_bounds_in_screen = GetBoundsInScreen(),
              .original_pointer_pos =
                  GetBoundsInScreen().OffsetFromOrigin() + GetPointerPos(),
          };
        }
      }

      // Starts dragging.
      if (delegate_->IsDraggingEnabled() && InDraggableRegion()) {
        MarkEventHandled();
        return DraggingState{
            .original_bounds_in_screen = GetBoundsInScreen(),
            .original_pointer_pos =
                GetBoundsInScreen().OffsetFromOrigin() + GetPointerPos(),
        };
      }
    }

    return InitialState{};
  }

  State operator()(const DraggingState& s) {
    // Keeps dragging.
    if (event_->type() == ui::EventType::kMouseDragged ||
        event_->type() == ui::EventType::kTouchMoved) {
      gfx::Rect new_bounds = s.original_bounds_in_screen +
                             GetBoundsInScreen().OffsetFromOrigin() +
                             GetPointerPos() - s.original_pointer_pos;

      // If user moves mouse to another display while dragging, we see that
      // as a completion of dragging.
      if (!IsInSameDisplay(s.original_bounds_in_screen, new_bounds)) {
        return InitialState{};
      }

      SetWidgetBoundsConstrained(new_bounds);
      MarkEventHandled();

      return s;
    }

    return InitialState{};
  }

  State operator()(const ResizingState& s) {
    // Keeps resizing.
    if (event_->type() == ui::EventType::kMouseDragged ||
        event_->type() == ui::EventType::kTouchMoved) {
      gfx::Vector2d resizing_unit =
          MapResizingDirectionToVector(s.resizing_direction);
      gfx::Vector2d pointer_delta = GetBoundsInScreen().OffsetFromOrigin() +
                                    GetPointerPos() - s.original_pointer_pos;

      int new_width = std::max(s.original_bounds_in_screen.width() +
                                   pointer_delta.x() * resizing_unit.x(),
                               kMinWidgetWidth);
      int new_height = std::max(s.original_bounds_in_screen.height() +
                                    pointer_delta.y() * resizing_unit.y(),
                                kMinWidgetHeight);

      int new_x = s.original_bounds_in_screen.x();
      int new_y = s.original_bounds_in_screen.y();

      if (resizing_unit.x() < 0) {
        new_x -= new_width - s.original_bounds_in_screen.width();
      }

      if (resizing_unit.y() < 0) {
        new_y -= new_height - s.original_bounds_in_screen.height();
      }

      SetWidgetBoundsConstrained(gfx::Rect(/*x=*/new_x, /*y=*/new_y,
                                           /*width=*/new_width,
                                           /*height=*/new_height));
      MarkEventHandled();

      return s;
    }

    return InitialState{};
  }

 private:
  gfx::Vector2d GetPointerPos() const {
    return gfx::Vector2d(base::ClampFloor(event_->x()),
                         base::ClampFloor(event_->y()));
  }

  gfx::Rect GetBoundsInScreen() const {
    return delegate_->GetWidgetBoundsInScreen();
  }

  void SetWidgetBoundsConstrained(const gfx::Rect bounds) {
    delegate_->SetWidgetBoundsConstrained(bounds);
  }

  void MarkEventHandled() { event_->SetHandled(); }

  bool InDraggableRegion() {
    std::optional<SkRegion> draggable_region = delegate_->GetDraggableRegion();
    // The pointer pos here is based on MakoRewriteView window (padding
    // included) but the draggable region here is based on web contents. So we
    // need to add padding on pointer pos.
    gfx::Vector2d pointer_pos =
        GetPointerPos() + gfx::Vector2d(kWidgetPadding, kWidgetPadding);
    return draggable_region.has_value() &&
           draggable_region->contains(pointer_pos.x(), pointer_pos.y());
  }

  raw_ptr<ui::LocatedEvent> event_;
  raw_ptr<MakoBubbleEventHandler::Delegate> delegate_;
};

}  // namespace

MakoBubbleEventHandler::MakoBubbleEventHandler(Delegate* delegate)
    : delegate_(delegate) {}

void MakoBubbleEventHandler::OnMouseEvent(ui::MouseEvent* event) {
  ProcessPointerEvent(*event);
}

void MakoBubbleEventHandler::OnTouchEvent(ui::TouchEvent* event) {
  ProcessPointerEvent(*event);
}

MakoBubbleEventHandler::State MakoBubbleEventHandler::get_state_for_testing() {
  return state_;
}

void MakoBubbleEventHandler::set_state_for_testing(State s) {
  state_ = s;
}

void MakoBubbleEventHandler::ProcessPointerEvent(ui::LocatedEvent& event) {
  if (!delegate_) {
    return;
  }
  state_ = std::visit(
      StateProcessFunction(/*event=*/&event, /*delegate=*/delegate_), state_);
}

}  // namespace ash