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

#include "cru/common/Logger.hpp"

#include <optional>

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

  event_rovoker_guards_.push_back(
      EventRevokerGuard(control->MouseEnterEvent()->Direct()->AddHandler(
          [this](event::MouseEventArgs&) {
            if (this->enable_) {
              if (this->state_ == ClickState::PressInactive) {
                if ((this->button_ & this->trigger_button_)) {
                  this->SetState(ClickState::Press);
                }
              } else {
                this->SetState(ClickState::Hover);
              }
            }
          })));

  event_rovoker_guards_.push_back(
      EventRevokerGuard(control->MouseLeaveEvent()->Direct()->AddHandler(
          [this](event::MouseEventArgs&) {
            if (this->enable_) {
              if (this->state_ == ClickState::Press) {
                if ((this->button_ & this->trigger_button_)) {
                  this->SetState(ClickState::PressInactive);
                }
              } else {
                this->SetState(ClickState::None);
              }
            }
          })));

  event_rovoker_guards_.push_back(
      EventRevokerGuard(control->MouseDownEvent()->Direct()->AddHandler(
          [this](event::MouseButtonEventArgs& args) {
            const auto button = args.GetButton();
            if (this->enable_ && (button & this->trigger_button_) &&
                this->state_ == ClickState::Hover) {
              if (!this->control_->CaptureMouse()) {
                log::TagDebug(log_tag,
                              u"Failed to capture mouse when begin click.");
                return;
              }
              this->down_point_ = args.GetPoint();
              this->button_ = button;
              this->SetState(ClickState::Press);
            }
          })));

  event_rovoker_guards_.push_back(
      EventRevokerGuard(control->MouseUpEvent()->Direct()->AddHandler(
          [this](event::MouseButtonEventArgs& args) {
            const auto button = args.GetButton();
            if (this->enable_ && (button & this->trigger_button_) &&
                button == button_) {
              if (this->state_ == ClickState::Press) {
                this->SetState(ClickState::Hover);
                this->event_.Raise(ClickEventArgs{this->control_,
                                                  this->down_point_,
                                                  args.GetPoint(), button});
                this->control_->ReleaseMouse();
              } else if (this->state_ == ClickState::PressInactive) {
                this->SetState(ClickState::None);
                this->control_->ReleaseMouse();
              }
            }
          })));
}  // namespace cru::ui

void ClickDetector::SetEnabled(bool enable) {
  if (enable == enable_) {
    return;
  }

  enable_ = enable;
  if (enable) {
    SetState(control_->IsMouseOver() ? ClickState::Hover : ClickState::None);
  } else {
    if (state_ == ClickState::Press || state_ == ClickState::PressInactive) {
      SetState(ClickState::None);
      control_->ReleaseMouse();
    } else if (state_ == ClickState::Hover) {
      SetState(ClickState::None);
    }
  }
}

void ClickDetector::SetTriggerButton(MouseButton trigger_button) {
  if (trigger_button == trigger_button_) {
    return;
  }

  trigger_button_ = trigger_button;
  if ((state_ == ClickState::Press || state_ == ClickState::PressInactive) &&
      !(button_ & trigger_button)) {
    SetState(control_->IsMouseOver() ? ClickState::Hover : ClickState::None);
    control_->ReleaseMouse();
  }
}

void ClickDetector::SetState(ClickState state) {
#ifdef CRU_DEBUG
  auto to_string = [](ClickState state) -> std::u16string_view {
    switch (state) {
      case ClickState::None:
        return u"None";
      case ClickState::Hover:
        return u"Hover";
      case ClickState::Press:
        return u"Press";
      case ClickState::PressInactive:
        return u"PressInvactive";
      default:
        UnreachableCode();
    }
  };
  log::TagDebug(log_tag, u"Click state changed, new state: {}.",
                to_string(state));
#endif

  state_ = state;
  state_change_event_.Raise(state);
}
}  // namespace cru::ui