Repository navigation
Expand file tree
/
Copy pathwindow_drag_session.cpp
More file actions
110 lines (92 loc) · 2.87 KB
/
Copy pathwindow_drag_session.cpp
File metadata and controls
110 lines (92 loc) · 2.87 KB
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
#include "window_drag_session.h"
#include <cmath>
namespace nativeapi {
bool WindowDragSession::Start(std::shared_ptr<Window> window, Point anchor) {
Point cursor_position{0, 0};
bool primary_button_down = false;
if (!QueryPointer(cursor_position, primary_button_down)) {
return false;
}
// Retargeting an active session releases the previous window silently.
window_ = std::move(window);
window_id_ = window_ ? window_->GetId() : 0;
anchor_ = anchor;
last_cursor_position_ = cursor_position;
active_ = true;
// A gesture can be recognized after the button was already released; the
// first tick reports the end then, and the window must not jump to wherever
// the cursor has gone meanwhile.
if (window_ && primary_button_down) {
MoveWindow(*window_, cursor_position);
}
StartTicking();
return true;
}
void WindowDragSession::Cancel() {
if (!active_) {
return;
}
Point cursor_position = last_cursor_position_;
bool primary_button_down = false;
QueryPointer(cursor_position, primary_button_down);
Finish(true, cursor_position);
}
bool WindowDragSession::IsActive() const {
return active_;
}
WindowId WindowDragSession::GetWindowId() const {
return active_ ? window_id_ : 0;
}
Point WindowDragSession::GetAnchor() const {
return anchor_;
}
void WindowDragSession::HandleTick() {
if (!active_) {
StopTicking();
return;
}
Point cursor_position = last_cursor_position_;
bool primary_button_down = false;
if (!QueryPointer(cursor_position, primary_button_down)) {
Finish(true, cursor_position);
return;
}
// The window was closed out from under the session.
if (window_ && window_->GetNativeObject() == nullptr) {
Finish(true, cursor_position);
return;
}
// Released: the drag is over where the cursor was last followed. Moving now
// would carry the window to wherever the cursor went after the release.
if (!primary_button_down) {
Finish(false, cursor_position);
return;
}
const bool moved = std::fabs(cursor_position.x - last_cursor_position_.x) >= 0.5 ||
std::fabs(cursor_position.y - last_cursor_position_.y) >= 0.5;
if (moved) {
last_cursor_position_ = cursor_position;
if (window_) {
MoveWindow(*window_, cursor_position);
}
Emit<WindowDragMovedEvent>(window_id_, cursor_position);
}
}
void WindowDragSession::Finish(bool cancelled, Point cursor_position) {
if (!active_) {
return;
}
active_ = false;
StopTicking();
const WindowId window_id = window_id_;
// Keep the window alive until listeners have seen the final event.
const std::shared_ptr<Window> window = std::move(window_);
window_id_ = 0;
last_cursor_position_ = cursor_position;
if (cancelled) {
Emit<WindowDragCancelledEvent>(window_id, cursor_position);
} else {
Emit<WindowDragEndedEvent>(window_id, cursor_position);
}
}
} // namespace nativeapi