Quick Start
This tutorial will guide you through creating a simple counter application with Freya. You’ll learn the fundamentals of components, state management, and event handling.Make sure you’ve completed the installation guide before starting this tutorial.
What We’re Building
We’ll create an interactive counter app with:- A display showing the current count
- Buttons to increase and decrease the count
- Styled UI elements with colors, shadows, and spacing
Step 1: Create a New Project
Step 2: Set Up the Basic App
Replace the contents ofsrc/main.rs with this code:
Understanding the Code
Understanding the Code
#![cfg_attr(...)]- On Windows release builds, prevents console window from appearinguse freya::prelude::*- Imports all common Freya types and functionslaunch()- Starts the Freya runtime with configurationWindowConfig::new(app)- Creates a window with ourappfunction as the root componentrect()- Creates a rectangular container element.expanded()- Makes the rect fill available space.center()- Centers child content both horizontally and vertically.child()- Adds a child element (in this case, text)
Step 3: Add State
Now let’s add state to track the counter value. Freya uses hooks for state management. Theuse_state hook creates reactive state that automatically triggers re-renders when changed.
Update your app function:
Understanding State
Understanding State
use_state(|| 4)- Creates state initialized to4. The closure is only called once.count.read()- Reads the current value. This subscribes the component to updates.count.write()- Gets a mutable reference to update the value (we’ll use this next)- When state changes, Freya automatically re-renders the component
Step 4: Create the Counter Display
Let’s make the counter display more visually appealing with styling:Understanding Styling
Understanding Styling
width(Size::fill())- Fill available widthheight(Size::percent(50.))- Take up 50% of parent heightcolor((255, 255, 255))- White text color (RGB)background((15, 163, 242))- Blue backgroundfont_weight(FontWeight::BOLD)- Bold textfont_size(75.)- Large text sizeshadow((x, y, blur, spread, color))- Drop shadow effect
Step 5: Add Interactive Buttons
Now let’s add buttons to modify the counter:Understanding Events and Buttons
Understanding Events and Buttons
Step 6: Final Touches
Let’s add window configuration for a better initial size:Run Your App
Key Concepts Learned
Components
Functions that return UI elements. The
app function is your root component.State Management
use_state creates reactive state that triggers re-renders on changes.Layout
Use
rect() with properties like .horizontal(), .center(), and sizing to arrange UI.Event Handling
Use
.on_press(), .on_mouse_up(), etc. to respond to user interactions.Next Steps
Enhance Your Counter
Try these modifications to deepen your understanding:Add a Reset Button
Add a Reset Button
Prevent Negative Numbers
Prevent Negative Numbers
Change Colors Based on Value
Change Colors Based on Value
Create a Reusable Component
Create a Reusable Component
Explore More
Component Examples
Browse examples of all built-in components like switches, sliders, and dropdowns
Animation Guide
Learn how to add smooth animations to your UI
State Management
Deep dive into local and global state patterns
API Reference
Complete documentation of all Freya APIs
Common Patterns
Using Multiple State Values
Conditional Rendering
Lists and Iteration
Troubleshooting
Component not re-rendering
Component not re-rendering
Make sure you’re calling
.read() on your state. Components only re-render when they subscribe to state by reading it.Cannot move out of captured variable
Cannot move out of captured variable
Add
move keyword to your closure and ensure the state type is Copy. Freya’s State type is Copy by default.Window doesn't appear on Windows
Window doesn't appear on Windows
Make sure you have the
#![cfg_attr(...)] attribute at the top of your main.rs file.Get Help
If you get stuck:- Check the examples directory for working code
- Ask in the Discord community
- Review the API docs for detailed information
- Open an issue if you find a bug