App trait and app function pattern are used to define the root component of a Freya application.
App Trait
TheApp trait is designed for root-level application components. Types implementing App automatically implement Component and have a blanket PartialEq implementation that always returns true.
Trait Definition
The render method that returns the root element tree of your application.
Characteristics
- Automatically implements
Componenttrait - Has a blanket
PartialEqimplementation that always returnstrue - Designed for root-level components that don’t need to track changes
- Can hold application-level state as struct fields
Struct-based App Example
WindowConfig::new_app
An instance of a type that implements the App trait.
App implementation directly.
app Function Pattern
The more common pattern is to define anapp function that returns impl IntoElement. This function is passed to WindowConfig::new().
Function Signature
IntoElement, typically an Element created by calling element functions like rect(), label(), or component constructors.
Basic Example
With Hooks
Theapp function creates a reactive scope, allowing you to use hooks:
With Components
AppComponent
AppComponent is an internal wrapper for App components that enables conversion from closures.
From Closure
AppComponent, which is why you can pass app directly to WindowConfig::new().
App vs Component
| Feature | App | Component |
|---|---|---|
| Purpose | Root-level application entry point | Reusable UI pieces |
| PartialEq | Always returns true | Must be manually implemented |
| Typical usage | Once per window | Multiple instances |
| State in struct | Common for app-level config | Common for component props |
| Hooks | Can use in render() | Can use in render() |
When to Use App vs app function
Use the App trait when:
- You need to pass initial configuration to your root component
- You want to encapsulate application-level state in a struct
- You’re building a multi-window application with different root components
Use an app function when:
- You have a simple single-window application
- You don’t need to pass initial configuration
- This is the most common pattern
Complete Example
Location
Defined in:/home/daytona/workspace/source/crates/freya-core/src/element.rs:227-270
Related
- launch - Launch the application
- Component trait - Create reusable components
- Element trait - Core element interface
- IntoElement trait - Convert types to elements