Rect (Area, Rectangle) Example
Demonstrates the Rect geometry primitive and hit-testing patterns.
TUI layouts are composed of rectangles. Understanding how to manipulate Rect objects, reuse them from the layout phase, and use them for mouse interaction is critical for building interactive apps.
Features Demonstrated
-
Rect Attributes: Investigating x, y, width, and height.
-
Edge Accessors: Using
left,right,top,bottominstead of manual math. -
Size Methods: Checking
areaandempty?for guard clauses. -
Geometry Transformations: Computing
inner,offset,union, andclamp. -
Iterators: Traversing
rows,columns, andpositions. -
Cached Layout Pattern: Computing constraints in the render loop and reusing the resulting
Rects in the event loop for logic. -
Hit Testing: Using
Rect#contains?(x, y)to determine if a mouse click happened inside a specific panel.
Hotkeys
-
Arrows (β/β): Expand/Shrink Sidebar Width (Layout Constraint)
-
Arrows (β/β): Navigate Menu Selection (
selected_index) -
Mouse Click: Click anywhere to see which Rect detects the hit (
contains?) -
q: Quit
Usage
ruby examples/widget_rect/app.rb
Learning Outcomes
Use this example if you need toβ¦
-
Handle mouse clicks on specific buttons or areas.
-
Create resizable panes (like a split pane in an IDE).
-
Debug layout issues by inspecting Rect coordinates.
-
Compute inner padding, bounding boxes, or clamped popups.
-
Iterate over rows, columns, or individual positions within a region.
