1313def demo_basic_elements ():
1414 """Demo basic elements using the builders API."""
1515 print ("=== Basic Elements Demo ===" )
16-
17- # Create basic shapes with styles
16+
1817 rect = (duc .ElementBuilder ()
1918 .at_position (0 , 0 )
2019 .with_size (100 , 50 )
@@ -27,7 +26,7 @@ def demo_basic_elements():
2726 ))
2827 .build_rectangle ()
2928 .build ())
30-
29+
3130 ellipse = (duc .ElementBuilder ()
3231 .at_position (120 , 0 )
3332 .with_size (60 , 40 )
@@ -39,7 +38,7 @@ def demo_basic_elements():
3938 ))
4039 .build_ellipse ()
4140 .build ())
42-
41+
4342 poly = (duc .ElementBuilder ()
4443 .at_position (200 , 0 )
4544 .with_size (50 , 50 )
@@ -53,22 +52,21 @@ def demo_basic_elements():
5352 .build_polygon ()
5453 .with_sides (6 )
5554 .build ())
56-
55+
5756 print (f"Rectangle ID: { rect .element .base .id } " )
58- print (f"Ellipse ID: { ellipse .element .base .id } " )
57+ print (f"Ellipse ID: { ellipse .element .base .id } " )
5958 print (f"Polygon sides: { poly .element .sides } " )
60-
59+
6160 # Demonstrate mutation with random versioning
62- original_version = rect .element .base .version
6361 duc .mutate_element (rect , x = 10 , label = "Moved Rectangle" )
64- print (f"Version changed: { original_version } -> { rect .element .base .version } " )
62+
63+ return [rect , ellipse , poly ]
6564
6665
6766def demo_linear_elements ():
6867 """Demo linear and arrow elements with styles."""
6968 print ("\n === Linear Elements Demo ===" )
70-
71- # Create a styled line
69+
7270 line_points = [(0 , 0 ), (50 , 25 ), (100 , 0 )]
7371 line = (duc .ElementBuilder ()
7472 .with_label ("Sample Line" )
@@ -79,8 +77,7 @@ def demo_linear_elements():
7977 .with_points (line_points )
8078 .build ())
8179 print (f"Line has { len (line .element .linear_base .points )} points" )
82-
83- # Create a styled arrow
80+
8481 arrow_points = [(0 , 50 ), (75 , 100 )]
8582 arrow = (duc .ElementBuilder ()
8683 .with_label ("Sample Arrow" )
@@ -92,11 +89,13 @@ def demo_linear_elements():
9289 .build ())
9390 print (f"Arrow element type: { type (arrow .element ).__name__ } " )
9491
92+ return [line , arrow ]
93+
9594
9695def demo_text_elements ():
9796 """Demo text elements with styles and document formatting."""
9897 print ("\n === Text Elements Demo ===" )
99-
98+
10099 text = (duc .ElementBuilder ()
101100 .at_position (0 , 100 )
102101 .with_size (150 , 25 )
@@ -106,14 +105,14 @@ def demo_text_elements():
106105 .with_text ("Hello, DucPy!" )
107106 .build ())
108107 print (f"Text content: '{ text .element .text } '" )
109- print (f"Text uses random versioning: { text .element .base .version > 0 } " )
108+
109+ return [text ]
110110
111111
112112def demo_stack_elements ():
113113 """Demo new stack-based elements with styles."""
114114 print ("\n === Stack Elements Demo ===" )
115-
116- # Create a styled frame
115+
117116 frame = (duc .ElementBuilder ()
118117 .at_position (0 , 150 )
119118 .with_size (200 , 100 )
@@ -127,8 +126,7 @@ def demo_stack_elements():
127126 .build_frame_element ()
128127 .build ())
129128 print (f"Frame stack label: { frame .element .stack_element_base .stack_base .label } " )
130-
131- # Create a styled plot with margins
129+
132130 plot = (duc .ElementBuilder ()
133131 .at_position (220 , 150 )
134132 .with_size (180 , 120 )
@@ -141,48 +139,48 @@ def demo_stack_elements():
141139 .build_plot_element ()
142140 .with_margins (duc .Margins (top = 5 , right = 5 , bottom = 5 , left = 5 ))
143141 .build ())
144- print (f"Plot is marked as plot: { plot .element .stack_element_base .stack_base .is_plot } " )
145- print (f"Plot margins: { plot .element .layout .margins .top } mm" )
146-
147142
143+ return [frame , plot ]
148144
149145
150146def demo_custom_stack_base ():
151147 """Demo custom stack base creation."""
152148 print ("\n === Custom Stack Base Demo ===" )
153-
154- # Use it in a frame
149+
155150 custom_frame = (duc .ElementBuilder ()
156151 .at_position (50 , 280 )
157152 .with_size (150 , 80 )
158- .with_label ("Custom Container" ) # Moved label to ElementBuilder
153+ .with_label ("Custom Container" )
159154 .build_frame_element ()
160155 .with_stack_base (duc .StateBuilder ().build_stack_base ()
161156 .with_is_collapsed (False )
162157 .with_styles (duc .DucStackLikeStyles (opacity = 0.8 ))
163158 .build ())
164159 .build ())
165-
166- print ( f"Custom stack opacity: { custom_frame . element . stack_element_base . stack_base . styles . opacity } " )
160+
161+ return [ custom_frame ]
167162
168163
169164def main ():
170165 """Run all element creation demos."""
171166 print ("DucPy Element Creation Demo" )
172167 print ("=" * 40 )
173-
174- demo_basic_elements ()
175- demo_linear_elements ()
176- demo_text_elements ()
177- demo_stack_elements ()
178- demo_custom_stack_base ()
179-
180- print ("\n ✅ All demos completed successfully!" )
181- print ("The refactored code provides:" )
182- print ("- Reduced code duplication" )
183- print ("- Consistent random versioning" )
184- print ("- New stack-based element support" )
185- print ("- Improved maintainability" )
168+
169+ elements = []
170+ elements .extend (demo_basic_elements ())
171+ elements .extend (demo_linear_elements ())
172+ elements .extend (demo_text_elements ())
173+ elements .extend (demo_stack_elements ())
174+ elements .extend (demo_custom_stack_base ())
175+
176+ duc_bytes = duc .serialize_duc (
177+ name = "element_creation_example" ,
178+ elements = elements ,
179+ )
180+
181+ print (f"\n Created { len (elements )} elements → serialized { len (duc_bytes )} bytes." )
182+ print ("✅ Element creation demo complete!" )
183+ return duc_bytes
186184
187185
188186if __name__ == "__main__" :
0 commit comments