3 * Copyright (c) 2020 Project CHIP Authors
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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
29 #include "ScreenManager.h"
31 #if CONFIG_HAVE_DISPLAY
33 #include <support/CHIPMem.h>
40 class ListScreen : public Screen
46 virtual ~Model() = default;
47 virtual std::string GetTitle() { return std::string(); }
48 virtual int GetItemCount() { return 0; }
49 virtual std::string GetItemText(int i) { return std::string(); }
50 virtual void ItemAction(int i) {}
54 Model * model = nullptr;
55 bool hasFocus = false;
59 ListScreen(Model * model) : model(model) {}
61 virtual ~ListScreen() { chip::Platform::Delete(model); }
63 std::string GetTitle() override { return model->GetTitle(); }
65 std::string GetButtonText(int id) override;
67 void Display() override;
69 bool IsFocusable() override { return model->GetItemCount() > 0; }
71 void Focus(FocusType focus) override;
73 void Action() override { model->ItemAction(focusIndex); }
76 class SimpleListModel : public ListScreen::Model
79 std::function<void(int)> action;
80 std::vector<std::tuple<std::string, std::function<void()>>> items;
83 std::string GetTitle() override { return title; }
84 int GetItemCount() override { return items.size(); }
85 std::string GetItemText(int i) override { return std::get<0>(items[i]); }
87 void ItemAction(int i) override
89 auto & action = std::get<1>(items[i]);
94 else if (this->action)
100 // Builder interface.
102 SimpleListModel * Title(std::string title)
104 this->title = std::move(title);
108 SimpleListModel * Action(std::function<void(int)> action)
110 this->action = std::move(action);
114 SimpleListModel * Item(std::string text)
116 items.emplace_back(std::move(text), std::move(std::function<void()>()));
120 SimpleListModel * Item(std::string text, std::function<void()> action)
122 items.emplace_back(std::move(text), std::move(action));
127 #endif // CONFIG_HAVE_DISPLAY