aboutsummaryrefslogtreecommitdiff
path: root/src/ui/click_detector.cpp
blob: b335f3b5d97cfd964981abace69dbb3d4986262f (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
#include "cru/ui/click_detector.hpp"

#include <cassert>
#include <optional>

namespace cru::ui {
ClickDetector::ClickDetector(Control* control) {
  assert(control);
  control_ = control;

  event_rovoker_guards_.push_front(
      EventRevokerGuard(control->MouseDownEvent()->Direct()->AddHandler(
          [this, control](event::MouseButtonEventArgs& args) {
            if (!control->CaptureMouse()) return;  // capture failed
            const auto button = args.GetMouseButton();
            FromButton(button) = args.GetPoint();  // save mouse down point
            begin_event_.Raise(button);
          })));

  event_rovoker_guards_.push_front(
      EventRevokerGuard(control->MouseUpEvent()->Direct()->AddHandler(
          [this, control](event::MouseButtonEventArgs& args) {
            if (!control->IsMouseCaptured()) return;
            const auto button = args.GetMouseButton();
            auto& down_point = FromButton(button);
            if (down_point.has_value()) {
              event_.Raise(ClickEventArgs(control, down_point.value(),
                                          args.GetPoint(), button));
              end_event_.Raise(button);
              down_point = std::nullopt;
            }
            control->ReleaseMouse();
          })));
}
}  // namespace cru::ui