Repository navigation
Add checkbox + demo #44
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -505,11 +505,14 @@ | |||||||||||||||||||||||||||||||
| if (child == nullptr) { | ||||||||||||||||||||||||||||||||
| return WidgetHandle{WidgetHandle::InvalidId}; | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
| child->mText.assign(text); | ||||||||||||||||||||||||||||||||
| child->mCheckBoxContext = new CheckBoxContext; | ||||||||||||||||||||||||||||||||
|
Check failure on line 509 in src/widgets.cpp
|
||||||||||||||||||||||||||||||||
| child->mCheckBoxContext->mChecked = checked; | ||||||||||||||||||||||||||||||||
|
Comment on lines
+509
to
+510
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🩺 Stability & Availability | 🟠 Major | ⚡ Quick win Release each allocated
Minimal lifetime fix if (mContent != nullptr) {
delete [] mContent;
}
+ if (mCheckBoxContext != nullptr) {
+ delete mCheckBoxContext;
+ }🧰 Tools🪛 GitHub Check: SonarCloud Code Analysis[failure] 509-509: Replace the use of "new" with an operation that automatically manages the memory. 🤖 Prompt for AI AgentsSource: Linters/SAST tools |
||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| return child->mHandle; | ||||||||||||||||||||||||||||||||
|
Comment on lines
+508
to
+512
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🎯 Functional Correctness | 🟠 Major | ⚡ Quick win Wire the The public factory documents Proposed callback wiring child->mCheckBoxContext = new CheckBoxContext;
child->mCheckBoxContext->mChecked = checked;
+ child->mCallback = callback;
+ if (callback != nullptr) {
+ callback->incRef();
+ }
return child->mHandle;📝 Committable suggestion
Suggested change
🧰 Tools🪛 GitHub Check: SonarCloud Code Analysis[failure] 509-509: Replace the use of "new" with an operation that automatically manages the memory. 🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| static void render(Context &ctx, const Widget *currentWidget) { | ||||||||||||||||||||||||||||||||
|
Check failure on line 515 in src/widgets.cpp
|
||||||||||||||||||||||||||||||||
| if (currentWidget == nullptr) { | ||||||||||||||||||||||||||||||||
| return; | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
|
|
@@ -583,6 +586,27 @@ | |||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
| break; | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| case WidgetType::CheckBox: | ||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||
| if (currentWidget->mCheckBoxContext != nullptr) { | ||||||||||||||||||||||||||||||||
| int size = r.height/2; | ||||||||||||||||||||||||||||||||
| Rect checkBoxRect(r.top.x, r.top.y + (r.height - size)/2, size, size); | ||||||||||||||||||||||||||||||||
| if (currentWidget->mCheckBoxContext->mChecked) { | ||||||||||||||||||||||||||||||||
| Renderer::drawRect(ctx, checkBoxRect.top.x, checkBoxRect.top.y, checkBoxRect.width, checkBoxRect.height, true, ctx.mStyle.mFg); | ||||||||||||||||||||||||||||||||
| } else { | ||||||||||||||||||||||||||||||||
| Renderer::drawRect(ctx, checkBoxRect.top.x, checkBoxRect.top.y, checkBoxRect.width, checkBoxRect.height, true, ctx.mStyle.mBg); | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
|
Comment on lines
+594
to
+598
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🎯 Functional Correctness | 🟠 Major | ⚡ Quick win Keep the unchecked box visible. The unchecked branch fills the square with Proposed unchecked rendering- Renderer::drawRect(ctx, checkBoxRect.top.x, checkBoxRect.top.y, checkBoxRect.width, checkBoxRect.height, true, ctx.mStyle.mBg);
+ Renderer::drawRect(ctx, checkBoxRect.top.x, checkBoxRect.top.y, checkBoxRect.width, checkBoxRect.height, false, ctx.mStyle.mFg);📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||
| if (!currentWidget->mText.empty()) { | ||||||||||||||||||||||||||||||||
| const Color4 fg = ctx.mStyle.mTextColor; | ||||||||||||||||||||||||||||||||
| const Color4 bg = ctx.mStyle.mBg; | ||||||||||||||||||||||||||||||||
| Rect textRect(checkBoxRect.top.x + checkBoxRect.width + 5, r.top.y, r.width - checkBoxRect.width - 5, r.height); | ||||||||||||||||||||||||||||||||
| Renderer::drawText(ctx, currentWidget->mText.c_str(), ctx.mDefaultFont, | ||||||||||||||||||||||||||||||||
| textRect, fg, bg, currentWidget->mAlignment); | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
| break; | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| case WidgetType::InputField: | ||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||
| Renderer::drawRect(ctx, r.top.x, r.top.y, r.width, r.height, true, ctx.mStyle.mFg); | ||||||||||||||||||||||||||||||||
|
|
@@ -638,6 +662,12 @@ | |||||||||||||||||||||||||||||||
| Widget *found{nullptr}; | ||||||||||||||||||||||||||||||||
| findSelectedWidget(x, y, ctx.mRoot, &found); | ||||||||||||||||||||||||||||||||
| if (found != nullptr) { | ||||||||||||||||||||||||||||||||
| if (found->mType == WidgetType::CheckBox) { | ||||||||||||||||||||||||||||||||
| if (eventType == Events::MouseButtonDownEvent) { | ||||||||||||||||||||||||||||||||
|
Check warning on line 666 in src/widgets.cpp
|
||||||||||||||||||||||||||||||||
| found->mCheckBoxContext->mChecked = !found->mCheckBoxContext->mChecked; | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| #ifdef _DEBUG | ||||||||||||||||||||||||||||||||
| if (eventType == Events::MouseButtonDownEvent) { | ||||||||||||||||||||||||||||||||
| std::cout << "Clicked " << found->mHandle.mId << "\n"; | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🩺 Stability & Availability | 🔴 Critical | ⚡ Quick win
🧩 Analysis chain
🏁 Script executed:
Repository: kimkulling/tiny_ui
Length of output: 2099
🏁 Script executed:
Repository: kimkulling/tiny_ui
Length of output: 10062
🌐 Web query:
C++ std::string assign const char* nullptr behavior💡 Result:
In modern C++ (C++23 and later), attempting to assign a nullptr to a std::string is ill-formed and will result in a compiler error [1]. This is because the C++23 standard added a deleted assignment operator: std::basic_string& operator=(std::nullptr_t) = delete; [2][1]. For older versions of C++ (C++11 through C++20), assigning nullptr to a std::string was syntactically valid but constituted undefined behavior [3][4][1]. Historically, this occurred because std::string provided constructors and assignment operators that accepted a const char* pointer [1]. Since nullptr implicitly converts to any pointer type, it matched these overloads, causing the string to attempt to read from a null memory address, which resulted in undefined behavior [3][1]. If you are working with an older C++ standard, you must ensure that any pointer passed to std::string::assign or used in an assignment is non-null [3]. You should explicitly check for null pointers before performing the assignment or construction [3].
Citations:
Guard
textbefore assigning the checkbox label.Widgets::checkBox()callschild->mText.assign(text)with aconst char*that can benullptr. Guard invalid text before assignment so other callers cannot crash this factory.Proposed input guard
📝 Committable suggestion
🤖 Prompt for AI Agents
Source: Coding guidelines