implementation.hpp 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. #ifndef IMPLEMENTATION_HPP
  2. #define IMPLEMENTATION_HPP
  3. #include "implementation.h"
  4. template <typename Range>
  5. bool focus_group<Range>::focused_in_range() const noexcept
  6. {
  7. using std::end;
  8. return focused_element != end(elements);
  9. }
  10. template <typename Range>
  11. bool focus_group<Range>::focus() const noexcept
  12. {
  13. using std::end;
  14. using ::focus;
  15. return focused_in_range() &&
  16. focus(*focused_element);
  17. }
  18. template <typename Range>
  19. void focus_group<Range>::drop_focus() noexcept
  20. {
  21. using std::end;
  22. using ::drop_focus;
  23. if( focused_in_range() )
  24. drop_focus(*focused_element);
  25. }
  26. template <typename Range>
  27. bool focus_group<Range>::focus_on(const i_focusable& target) noexcept
  28. {
  29. using std::begin;
  30. using std::end;
  31. using ::drop_focus;
  32. using ::focus_on;
  33. if(focused_in_range())
  34. drop_focus(*focused_element);
  35. auto focused = std::find_if(begin(elements), end(elements),
  36. [&target](auto& x) {return focus_on(x,target);});
  37. if(focused != end(elements))
  38. {
  39. focused_element = focused;
  40. return true;
  41. }
  42. else return false;
  43. }
  44. template <typename Range>
  45. bool focus_group<Range>::focus(direction dir)
  46. {
  47. using std::begin;
  48. using std::end;
  49. using ::focus;
  50. using ::drop_focus;
  51. bool forward = dir >= 0;
  52. if(!this->focus())
  53. {
  54. focused_element = forward
  55. ? begin(elements)
  56. : end(elements) - 1
  57. ;
  58. focus(*focused_element, dir);
  59. return true;
  60. }
  61. if(focus(*focused_element, dir))
  62. return true;
  63. if(forward
  64. ? focused_element + dir != end(elements)
  65. : focused_element != begin(elements))
  66. {
  67. drop_focus(*focused_element);
  68. focused_element += dir;
  69. focus(*focused_element, dir);
  70. return true;
  71. }
  72. return false;
  73. }
  74. template <typename Range>
  75. focus_group<Range>::focus_group(Range range) :
  76. elements(std::move(range)),
  77. focused_element(std::end(elements))
  78. {}
  79. template <typename Range>
  80. focus_group<Range>::focus_group() :
  81. elements(),
  82. focused_element(std::end(elements))
  83. {}
  84. #endif /* end of include guard */