horizontal-resize-grip.cpp 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. #if defined(Hiro_HorizontalResizeGrip)
  2. mHorizontalResizeGrip::mHorizontalResizeGrip() {
  3. image icon;
  4. icon.allocate(5, 15);
  5. for(uint y : range(icon.height())) {
  6. auto data = icon.data() + y * icon.pitch();
  7. icon.write(data, 0x00000000); data += icon.stride();
  8. icon.write(data, 0xff9f9f9f); data += icon.stride();
  9. icon.write(data, 0x00000000); data += icon.stride();
  10. icon.write(data, 0xff9f9f9f); data += icon.stride();
  11. icon.write(data, 0x00000000); data += icon.stride();
  12. }
  13. mCanvas::setIcon(icon);
  14. mCanvas::setMouseCursor(MouseCursor::HorizontalResize);
  15. mCanvas::onMousePress([&](auto button) {
  16. if(button == Mouse::Button::Left && !state.timer.enabled()) {
  17. doActivate();
  18. state.offset = 0;
  19. state.origin = Mouse::position();
  20. state.timer.setEnabled();
  21. }
  22. });
  23. state.timer.setInterval(10).onActivate([&] {
  24. if(!Mouse::pressed(Mouse::Button::Left)) return (void)state.timer.setEnabled(false);
  25. auto position = Mouse::position();
  26. auto offset = position.x() - state.origin.x();
  27. if(offset != state.offset) doResize(offset), offset = state.offset;
  28. });
  29. }
  30. auto mHorizontalResizeGrip::doActivate() const -> void {
  31. if(state.onActivate) state.onActivate();
  32. }
  33. auto mHorizontalResizeGrip::doResize(int offset) const -> void {
  34. if(state.onResize) state.onResize(offset);
  35. }
  36. auto mHorizontalResizeGrip::onActivate(const function<void ()>& callback) -> type& {
  37. state.onActivate = callback;
  38. return *this;
  39. }
  40. auto mHorizontalResizeGrip::onResize(const function<void (int)>& callback) -> type& {
  41. state.onResize = callback;
  42. return *this;
  43. }
  44. #endif