This document outlines the architectural design principles and implementation details of DearCyGui.
DearCyGui organizes all UI elements in a hierarchical tree structure:
- Each DCG Context is associated with a single Viewport (system window)
- The Viewport serves as the root node of the rendering tree
- Rendering traverses the tree depth-first, starting from the root
- Each item is responsible for rendering itself and orchestrating its children
The initial of DearCyGui was that unlike traditional GUI frameworks:
- Parents don't directly render their children
- Each item points to its LAST child, creating a linked structure
- Each child renders its previous sibling first, forming a recursive chain
This made it complicated however to implement some functionalities, such as Tables.
Thus DearCyGui moved to a more traditional tree traversal:
- Each parent is rendered, iterating on each child to get it rendered.
All this traversal and rendering is performed with efficient C++ code generated through Cython's nogil capabilities.
DearCyGui uses a lock-per-item approach for thread safety, rather than a global lock:
- Each UI element has its own lock
- Locks must be acquired in parent-to-child order (topmost first)
- Multiple thread-safe patterns are implemented for item manipulation
- The viewport uses three distinct locks to enable concurrent operations
To prevent deadlocks with Python's GIL:
- Entry points first attempt to acquire locks with a non-blocking try
- If acquisition fails, the GIL is released before blocking
- This pattern prevents deadlocks between the GIL and item locks
DearCyGui implements a flexible component model through specialized base classes:
-
baseItem: Core parent class for most elements
- Provides tagging, parent-child relationships, and sibling management
-
Major Subclasses:
- uiItem: Interactive elements with state, theme support, and callbacks
- drawingItem: Lightweight drawing primitives with no state
- baseTheme: Theme components that can be bound to items
- baseHandler: Event handling components
- plotElement: Specialized elements for data visualization
-
Child Slots:
- Each parent defines compatible child types
- Children must match the parent's supported base classes
- Siblings must share compatible base classes
DearCyGui uses Python's attribute model for all item configuration:
- No required positional parameters in constructors, except for the context, mandatory for all items
- All properties are implemented as Python item properties
- Fast attribute access through Cython's property system
- Full docstring support for IDE integration
- PYI type-stub generation for autocompletion
# All parameters are passed as attributes
button = dcg.Button(context, label="Click Me", width=100)
# Equivalent to:
button = dcg.Button(context)
button.label = "Click Me"
button.width = 100- All items require a Context instance at creation
- Python's
withstatement creates parent-child relationships - Layout containers automatically manage child positioning
with dcg.Window(context, label="My Window"):
dcg.Text(context, value="Hello World") # Automatically becomes child of WindowDearCyGui provides comprehensive support for asynchronous programming:
- Coroutines are accepted as callbacks
- Various helpers such as
AsyncPoolExecutorare provided for background task management - Viewport rendering loop can be integrated into existing event loops
Themes are first-class objects that can be:
- Created and modified at runtime
- Applied to specific items or item hierarchies
- Combined through the ThemeList aggregator
with dcg.ThemeList(context) as theme:
dcg.ThemeColorImGui(context, button=(100, 50, 200))
dcg.ThemeStyleImGui(context, frame_rounding=5.0)
button.theme = theme # Apply to a specific itemDearCyGui offers multiple layout options:
- Absolute and dynamic positioning (x, y coordinates)
- Auto-layout with content-aware spacing
- Alignment controls (LEFT, RIGHT, CENTER)
- Responsive sizing using expressions (width="0.5*viewport.width")
- Specialized containers (HorizontalLayout, VerticalLayout, etc.)
The viewport provides direct access to its framebuffer for:
- Screenshot capabilities
- Custom rendering effects
- Image capture for recording/sharing
DearCyGui leverages Cython to create true Python extension types:
- Item classes are real Python classes, not just wrapped C++ objects
- Full support for Python introspection (dir(), help(), etc.)
- Support for subclassing in both Python and Cython
- Python garbage collection for all items
- Automatic cleanup of children when parents are destroyed
- Thread-safe reference counting for shared resources
- Rendering code runs without the GIL for maximum performance
- Items track their visibility state to avoid unnecessary drawing
- Context.viewport.wait_for_input enables efficient CPU usage
- ImGui's immediate-mode architecture limits the need for state synchronization