Fix for x86_64 build fail
[platform/upstream/connectedhomeip.git] / examples / common / screen-framework / include / ListScreen.h
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 /**
20  * @file ListScreen.h
21  *
22  * Simple list screen.
23  *
24  */
25
26 #pragma once
27
28 #include "Screen.h"
29 #include "ScreenManager.h"
30
31 #if CONFIG_HAVE_DISPLAY
32
33 #include <support/CHIPMem.h>
34
35 #include <functional>
36 #include <string>
37 #include <tuple>
38 #include <vector>
39
40 class ListScreen : public Screen
41 {
42 public:
43     class Model
44     {
45     public:
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) {}
51     };
52
53 private:
54     Model * model  = nullptr;
55     bool hasFocus  = false;
56     int focusIndex = -1;
57
58 public:
59     ListScreen(Model * model) : model(model) {}
60
61     virtual ~ListScreen() { chip::Platform::Delete(model); }
62
63     std::string GetTitle() override { return model->GetTitle(); }
64
65     std::string GetButtonText(int id) override;
66
67     void Display() override;
68
69     bool IsFocusable() override { return model->GetItemCount() > 0; }
70
71     void Focus(FocusType focus) override;
72
73     void Action() override { model->ItemAction(focusIndex); }
74 };
75
76 class SimpleListModel : public ListScreen::Model
77 {
78     std::string title;
79     std::function<void(int)> action;
80     std::vector<std::tuple<std::string, std::function<void()>>> items;
81
82 public:
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]); }
86
87     void ItemAction(int i) override
88     {
89         auto & action = std::get<1>(items[i]);
90         if (action)
91         {
92             action();
93         }
94         else if (this->action)
95         {
96             this->action(i);
97         }
98     }
99
100     // Builder interface.
101
102     SimpleListModel * Title(std::string title)
103     {
104         this->title = std::move(title);
105         return this;
106     }
107
108     SimpleListModel * Action(std::function<void(int)> action)
109     {
110         this->action = std::move(action);
111         return this;
112     }
113
114     SimpleListModel * Item(std::string text)
115     {
116         items.emplace_back(std::move(text), std::move(std::function<void()>()));
117         return this;
118     }
119
120     SimpleListModel * Item(std::string text, std::function<void()> action)
121     {
122         items.emplace_back(std::move(text), std::move(action));
123         return this;
124     }
125 };
126
127 #endif // CONFIG_HAVE_DISPLAY