ListScreen.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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.h
  20. *
  21. * Simple list screen.
  22. *
  23. */
  24. #pragma once
  25. #include "Screen.h"
  26. #include "ScreenManager.h"
  27. #if CONFIG_HAVE_DISPLAY
  28. #include <lib/support/CHIPMem.h>
  29. #include <functional>
  30. #include <string>
  31. #include <tuple>
  32. #include <vector>
  33. class ListScreen : public Screen
  34. {
  35. public:
  36. class Model
  37. {
  38. public:
  39. virtual ~Model() = default;
  40. virtual std::string GetTitle() { return std::string(); }
  41. virtual int GetItemCount() { return 0; }
  42. virtual std::string GetItemText(int i) { return std::string(); }
  43. virtual void ItemAction(int i) {}
  44. };
  45. private:
  46. Model * model = nullptr;
  47. bool hasFocus = false;
  48. int focusIndex = -1;
  49. public:
  50. ListScreen(Model * model) : model(model) {}
  51. virtual ~ListScreen() { chip::Platform::Delete(model); }
  52. std::string GetTitle() override { return model->GetTitle(); }
  53. std::string GetButtonText(int id) override;
  54. void Display() override;
  55. bool IsFocusable() override { return model->GetItemCount() > 0; }
  56. void Focus(FocusType focus) override;
  57. void Action() override { model->ItemAction(focusIndex); }
  58. };
  59. class SimpleListModel : public ListScreen::Model
  60. {
  61. std::string title;
  62. std::function<void(int)> action;
  63. std::vector<std::tuple<std::string, std::function<void()>>> items;
  64. public:
  65. std::string GetTitle() override { return title; }
  66. int GetItemCount() override { return items.size(); }
  67. std::string GetItemText(int i) override { return std::get<0>(items[i]); }
  68. void ItemAction(int i) override
  69. {
  70. auto & action = std::get<1>(items[i]);
  71. if (action)
  72. {
  73. action();
  74. }
  75. else if (this->action)
  76. {
  77. this->action(i);
  78. }
  79. }
  80. // Builder interface.
  81. SimpleListModel * Title(std::string title)
  82. {
  83. this->title = std::move(title);
  84. return this;
  85. }
  86. SimpleListModel * Action(std::function<void(int)> action)
  87. {
  88. this->action = std::move(action);
  89. return this;
  90. }
  91. SimpleListModel * Item(std::string text)
  92. {
  93. items.emplace_back(std::move(text), std::move(std::function<void()>()));
  94. return this;
  95. }
  96. SimpleListModel * Item(std::string text, std::function<void()> action)
  97. {
  98. items.emplace_back(std::move(text), std::move(action));
  99. return this;
  100. }
  101. };
  102. #endif // CONFIG_HAVE_DISPLAY