Loading...
Searching...
No Matches
BindDialog.hpp
1#ifndef __BINDDIALOG_HPP
2#define __BINDDIALOG_HPP
3#include "BindView.hpp"
4#include "BindUtils.hpp"
5
14 enum DialogType {
15 TEXT_INPUT,
16 PASSWORD_INPUT,
17 NO_INPUT_TEXT,
18};
19
37 class BindDialog : public BindView
38{
39 public:
40
41 BindDialog(DialogType type = NO_INPUT_TEXT)
42 : dialogType(type), title(nullptr), message(nullptr), primaryButton("OK"), secondaryButton("Cancel"), resultText(nullptr)
43 {
44 this->tag = tagIndex++;
45 setTitle("Confirmation");
46 setMessage("Are you sure?");
47 setPrimaryButton("OK");
48 setSecondaryButton("Cancel");
49 setTextResult("", 0);
50 }
51
52 ~BindDialog()
53 {
54 if (resultText != nullptr) {
55 delete[] resultText;
56 }
57 }
58
64 void setTitle(const char *cstr)
65 {
66 title = cstr;
67 }
68
74 void setMessage(const char *cstr)
75 {
76 message = cstr;
77 }
78
84 void setPrimaryButton(const char *cstr)
85 {
86 primaryButton = cstr;
87 }
88
94 void setSecondaryButton(const char *cstr)
95 {
96 secondaryButton = cstr;
97 }
98
113 void setCallback(void (*callback)(bool))
114 {
115 this->callback = callback;
116 }
117
134 void setCallback(void (*callback)(bool, const char *))
135 {
136 this->callbackWithText = callback;
137 }
138
139 void invokeCallback(bool result, const char *text)
140 {
141 this->hasResult = true;
142 this->accepted = result;
143
144 if (dialogType == TEXT_INPUT || dialogType == PASSWORD_INPUT){
145 setTextResult(text, strlen(text));
146 if (callbackWithText != nullptr)
147 {
148 callbackWithText(result, text);
149 }
150 }
151
152 if (callback != nullptr)
153 {
154 callback(result);
155 }
156
157 }
158
159 void invokeCallback(bool result)
160 {
161 this->hasResult = true;
162 this->accepted = result;
163 if (callback != nullptr)
164 {
165 callback(result);
166 }
167 }
168
177 const char* getResultText() const
178 {
179 if (resultText != nullptr) {
180 return resultText;
181 }else{
182 return "";
183 }
184 }
185
186 uint16_t getBytes(uint8_t *out) override
187 {
188 offset = 0;
189 uint8_t dialogTypeU = (uint8_t)dialogType;
190 copyAndOffset(out, &offset, &objID, sizeof(objID));
191 copyAndOffset(out, &offset, &tag, sizeof(tag));
192 copyAndOffset(out, &offset, &cmdId, sizeof(cmdId));
193 copyAndOffset(out, &offset, &dialogTypeU, sizeof(dialogTypeU));
194 copyAndOffset(out, &offset, &singleButton, sizeof(singleButton));
195
196 copyStringWithLength(out, &offset, title);
197 copyStringWithLength(out, &offset, message);
198 copyStringWithLength(out, &offset, primaryButton);
199 copyStringWithLength(out, &offset, secondaryButton);
200
201 return offset;
202 }
203
204 uint8_t cmdId = 0;
205 bool accepted = false;
206 bool hasResult = false;
207 bool singleButton = false;
208
209 private:
210 uint8_t objID = BIND_ID_DIALOG;
211 uint16_t offset = 0;
212 int strLength = 0;
213 const char *title;
214 const char *message;
215 const char *primaryButton = "OK";
216 const char *secondaryButton = "Cancel";
217 DialogType dialogType;
218 static int16_t tagIndex;
219 void (*callback)(bool) = nullptr;
220 void (*callbackWithText)(bool, const char *) = nullptr;
221 char *resultText = nullptr;
222
223 void setTextResult(const char *cstr, int length)
224 {
225 if (cstr == resultText || length < 0) {
226 return; // Handle self-assignment
227 }
228
229 // Allocate memory for the new text
230 if (resultText != nullptr) {
231 delete[] resultText;
232 }
233 resultText = new char[length + 1];
234 strncpy(resultText, cstr, length);
235 resultText[length] = '\0'; // Null-terminate the string
236 }
237};
238
239#endif // __BINDDIALOG_HPP
BindDialog
BindDialog class represents a dialog box for use with BindCanvas app.
Definition BindDialog.hpp:38
BindDialog::singleButton
bool singleButton
Flag to indicate if the dialog has a single button.
Definition BindDialog.hpp:207
BindDialog::setMessage
void setMessage(const char *cstr)
Set the message text for the dialog.
Definition BindDialog.hpp:74
BindDialog::setCallback
void setCallback(void(*callback)(bool, const char *))
Set the callback function for the dialog with text input.
Definition BindDialog.hpp:134
BindDialog::setTitle
void setTitle(const char *cstr)
Set the title text for the dialog.
Definition BindDialog.hpp:64
BindDialog::getResultText
const char * getResultText() const
Get the result text entered by the user.
Definition BindDialog.hpp:177
BindDialog::getBytes
uint16_t getBytes(uint8_t *out) override
Retrieves the bytes representing the BindView for synchronization.
Definition BindDialog.hpp:186
BindDialog::accepted
bool accepted
Result of the user interaction with the dialog.
Definition BindDialog.hpp:205
BindDialog::setSecondaryButton
void setSecondaryButton(const char *cstr)
Set the secondary button text (e.g. "Cancel") for the dialog.
Definition BindDialog.hpp:94
BindDialog::setPrimaryButton
void setPrimaryButton(const char *cstr)
Set the primary button text (e.g. "OK") for the dialog.
Definition BindDialog.hpp:84
BindDialog::cmdId
uint8_t cmdId
Command ID for the dialog. See the notes for possible cmdId values.
Definition BindDialog.hpp:204
BindDialog::hasResult
bool hasResult
Flag to indicate if the dialog has a result. The resets to false after each sync.
Definition BindDialog.hpp:206
BindDialog::setCallback
void setCallback(void(*callback)(bool))
Set the callback function for the dialog.
Definition BindDialog.hpp:113
BindView
Definition BindView.hpp:22