Skip to content

feat: Badge#148

Merged
lesleysin merged 1 commit intomainfrom
feat/badge
Nov 22, 2025
Merged

feat: Badge#148
lesleysin merged 1 commit intomainfrom
feat/badge

Conversation

@lesleysin
Copy link
Copy Markdown
Member

@lesleysin lesleysin commented Nov 22, 2025

Summary by CodeRabbit

Release Notes

New Features

  • Added Badge widget component with configurable properties including variant types, colors, sizes, padding, alignment, offset positioning, label visibility, and count display. Supports custom child elements and labels with full theming capabilities.

✏️ Tip: You can customize this high-level summary in your review settings.

@lesleysin lesleysin self-assigned this Nov 22, 2025
@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Nov 22, 2025

Walkthrough

Introduces a new Badge widget component to the duit UI framework. This includes the BadgeAttributes configuration struct with variant types, color theming, sizing, spacing, and count properties; a Badge element type constant; wrapper methods for theming and reference tracking; and a Badge widget constructor function that creates composite elements with child and label content.

Changes

Cohort / File(s) Summary
BadgeAttributes Core Definition
pkg/duit_attributes/badge_attributes.go
New struct with variant, colors, sizes, padding, alignment, offset, visibility, and count fields. Includes NewBadgeAttributes constructor and fluent setters for all fields. Validate method delegates to embedded types and enforces that BadgeCount variant requires a non-nil Count.
BadgeAttributes Delegation
pkg/duit_attributes/zz_embedded_structs_delegates.go
Adds five wrapper methods (SetIgnoreTheme, SetOverrideRule, SetTheme, AddRef, SetRefs) that lazily initialize embedded holders and delegate to ThemeConsumer and ValueReferenceHolder.
Element Type
pkg/duit_core/element_type.go
Adds Badge constant to DuitElementType enum.
Badge Widget
pkg/duit_widget/badge.go
New exported Badge function that constructs a composite element from BadgeAttributes, id, control flag, and child/label elements. Validates attributes and returns a DuitElementModel.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~12 minutes

  • Validation logic: Verify the conditional check that BadgeCount variant requires Count to be non-nil
  • Delegation pattern: Confirm wrapper methods properly initialize embedded fields before delegating
  • Consistency: Ensure Badge widget function follows the same pattern as other widget constructors in the codebase

Pre-merge checks

✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title 'feat: Badge' clearly and concisely summarizes the main change: introducing a new Badge feature with its attributes, element type, and widget implementation.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

📜 Review details

Configuration used: CodeRabbit UI

Review profile: ASSERTIVE

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between e1f2376 and aaad841.

⛔ Files ignored due to path filters (1)
  • cmd/gen/embedded_sturct_delegate_gen.go is excluded by !**/gen/**
📒 Files selected for processing (4)
  • pkg/duit_attributes/badge_attributes.go (1 hunks)
  • pkg/duit_attributes/zz_embedded_structs_delegates.go (1 hunks)
  • pkg/duit_core/element_type.go (1 hunks)
  • pkg/duit_widget/badge.go (1 hunks)
🧰 Additional context used
🧬 Code graph analysis (3)
pkg/duit_widget/badge.go (3)
pkg/duit_core/element_type.go (1)
  • Badge (93-93)
pkg/duit_attributes/badge_attributes.go (1)
  • BadgeAttributes (17-31)
pkg/duit_core/element.go (1)
  • DuitElementModel (12-39)
pkg/duit_attributes/badge_attributes.go (3)
pkg/duit_attributes/value_reference_holder.go (1)
  • ValueReferenceHolder (15-17)
pkg/duit_attributes/theme.go (1)
  • ThemeConsumer (12-16)
pkg/duit_utils/tristate.go (6)
  • Tristate (30-30)
  • TBool (32-32)
  • TristateFrom (54-68)
  • Float32Value (118-120)
  • BoolValue (74-76)
  • UintValue (98-100)
pkg/duit_attributes/zz_embedded_structs_delegates.go (3)
pkg/duit_attributes/badge_attributes.go (1)
  • BadgeAttributes (17-31)
pkg/duit_attributes/theme.go (2)
  • ThemeConsumer (12-16)
  • ThemeOverrideRule (5-5)
pkg/duit_attributes/value_reference_holder.go (2)
  • ValueRef (5-9)
  • ValueReferenceHolder (15-17)
🔇 Additional comments (5)
pkg/duit_core/element_type.go (1)

93-93: LGTM - Badge element type added correctly.

The new Badge element type follows the established pattern and naming conventions.

pkg/duit_attributes/zz_embedded_structs_delegates.go (1)

4403-4443: LGTM - Generated wrapper methods are consistent.

The wrapper methods for BadgeAttributes follow the established lazy-initialization and delegation pattern used throughout this generated file.

pkg/duit_attributes/badge_attributes.go (2)

10-15: LGTM - Badge variant constants defined correctly.

The BadgeVariant type alias and constants (BadgeCommon, BadgeCount) follow Go conventions.


63-125: LGTM - Constructor and setter methods are well-implemented.

The fluent API methods follow the established pattern and correctly use utility functions from duit_utils for type conversions.

pkg/duit_widget/badge.go (1)

8-11: No nil checks needed—nil children are expected behavior in this framework.

The Badge function correctly follows the established pattern across all multi-child widgets in the codebase. Similar widgets (SliverAppBar, AppBar, FlexibleSpaceBar, SliverVisibility) all pass nullable parameters directly to the children array without filtering. As documented in the codebase's design, the client/renderer is responsible for handling nil values appropriately, so defensive nil checks would be inconsistent with framework conventions.

Comment on lines +34 to +60
func (r *BadgeAttributes) Validate() error {
if err := r.ThemeConsumer.Validate(); err != nil {
return err
}

if err := r.ValueReferenceHolder.Validate(); err != nil {
return err
}

if err := r.AnimatedPropertyOwner.Validate(); err != nil {
return err
}

if r.Padding != nil {
if err := r.Padding.Validate(); err != nil {
return err
}
}

if r.Variant != nil {
if *r.Variant == BadgeCount && r.Count == nil {
return errors.New("count property can`t be ")
}
}

return nil
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🔴 Critical

Fix incomplete error message and add nil checks.

Two issues identified:

  1. Line 55: The error message is incomplete: "count property cant be "` - missing the rest of the message. Also uses a backtick instead of an apostrophe in "can't".

  2. Lines 35-45: Potential nil pointer dereference. The embedded pointers (ThemeConsumer, ValueReferenceHolder, AnimatedPropertyOwner) may be nil, and calling Validate() on them will panic. Add nil checks before validation.

Apply this diff to fix both issues:

 func (r *BadgeAttributes) Validate() error {
-	if err := r.ThemeConsumer.Validate(); err != nil {
-		return err
+	if r.ThemeConsumer != nil {
+		if err := r.ThemeConsumer.Validate(); err != nil {
+			return err
+		}
 	}
 
-	if err := r.ValueReferenceHolder.Validate(); err != nil {
-		return err
+	if r.ValueReferenceHolder != nil {
+		if err := r.ValueReferenceHolder.Validate(); err != nil {
+			return err
+		}
 	}
 
-	if err := r.AnimatedPropertyOwner.Validate(); err != nil {
-		return err
+	if r.AnimatedPropertyOwner != nil {
+		if err := r.AnimatedPropertyOwner.Validate(); err != nil {
+			return err
+		}
 	}
 
 	if r.Padding != nil {
@@ -52,7 +58,7 @@
 
 	if r.Variant != nil {
 		if *r.Variant == BadgeCount && r.Count == nil {
-			return errors.New("count property can`t be ")
+			return errors.New("count property cannot be nil when variant is BadgeCount")
 		}
 	}
🤖 Prompt for AI Agents
In pkg/duit_attributes/badge_attributes.go around lines 34 to 60, the Validate
method currently calls Validate() on embedded fields without nil checks and
returns an incomplete error string for the count check; add nil checks before
calling ThemeConsumer.Validate(), ValueReferenceHolder.Validate(), and
AnimatedPropertyOwner.Validate() (only call Validate if the receiver pointer is
non-nil) and update the count error to a complete, clear message using an
apostrophe (e.g. "count property can't be nil when variant is BadgeCount").

@lesleysin lesleysin merged commit f869281 into main Nov 22, 2025
2 checks passed
@lesleysin lesleysin deleted the feat/badge branch November 22, 2025 09:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant