ListScreen.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /*
  2. *
  3. * Copyright (c) 2020 Project CHIP Authors
  4. * All rights reserved.
  5. *
  6. * Licensed under the Apache License, Version 2.0 (the "License");
  7. * you may not use this file except in compliance with the License.
  8. * You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. /**
  19. * @file ListScreen.cpp
  20. *
  21. * Simple list screen.
  22. *
  23. */
  24. #include "ListScreen.h"
  25. #if CONFIG_HAVE_DISPLAY
  26. #include <algorithm>
  27. namespace {
  28. const char * buttonText[] = { "Up", "Down", "Action" };
  29. };
  30. std::string ListScreen::GetButtonText(int id)
  31. {
  32. return buttonText[id - 1];
  33. }
  34. void ListScreen::Display()
  35. {
  36. int i = 0;
  37. int items = (DisplayHeight - ScreenTitleSafeTop - ScreenTitleSafeBottom) / ScreenFontHeight;
  38. if (items < model->GetItemCount())
  39. {
  40. i = std::max(0, focusIndex - items + (focusIndex == model->GetItemCount() - 1 ? 1 : 2));
  41. }
  42. for (int count = 0, y = ScreenTitleSafeTop; i < model->GetItemCount() && count < items; ++i, ++count, y += ScreenFontHeight)
  43. {
  44. tft_fg = focusIndex == i ? ScreenFocusColor : ScreenNormalColor;
  45. TFT_print(model->GetItemText(i).c_str(), ScreenTitleSafeTop, y);
  46. }
  47. }
  48. void ListScreen::Focus(FocusType focus)
  49. {
  50. switch (focus)
  51. {
  52. case FocusType::NONE:
  53. hasFocus = false;
  54. focusIndex = -1;
  55. break;
  56. case FocusType::BLUR:
  57. hasFocus = false;
  58. // leave focus index alone
  59. break;
  60. case FocusType::UNBLUR:
  61. hasFocus = true;
  62. // leave focus index alone
  63. break;
  64. case FocusType::NEXT:
  65. hasFocus = true;
  66. if (focusIndex == -1)
  67. {
  68. focusIndex = 0;
  69. break;
  70. }
  71. focusIndex = (focusIndex + 1) % model->GetItemCount(); // wraparound
  72. if (focusIndex == 0)
  73. {
  74. ScreenManager::FocusBack(); // try focus back if it did wrap
  75. }
  76. break;
  77. case FocusType::PREVIOUS:
  78. hasFocus = true;
  79. if (focusIndex == -1)
  80. {
  81. focusIndex = model->GetItemCount() - 1;
  82. break;
  83. }
  84. focusIndex = (focusIndex + model->GetItemCount() - 1) % model->GetItemCount(); // wraparound
  85. if (focusIndex == model->GetItemCount() - 1)
  86. {
  87. ScreenManager::FocusBack(); // try focus back if it did wrap
  88. }
  89. break;
  90. }
  91. }
  92. #endif // CONFIG_HAVE_DISPLAY