# Your first application

Version: 0.5.0
Framework commit: d503df2befa756e681fcdc6250e9552dc7388bea
Canonical URL: /docs/0.5.0/start/first-application/

Start from the repository's smallest complete applications. They give you a working platform entry point, theme, and font setup before you add your own features. The primary way to write a FerroUI interface is declarative Rust with `ferroui-view`; XAML compiled to Rust is the second, equal option, shown at the end of this page.

## Run a complete example

```sh
cargo run -p hello-world-hooks
```

The window shows a greeting and a button that counts its clicks. The same window is written four more ways in the repository, so you can compare them side by side:

| Sample | How the view is written |
| --- | --- |
| `hello-world-hooks` | A component that runs again, with its state in `use_signal` |
| `hello-world-builders` | The generated builders (`stack_panel()`, `button()`) and a signal |
| `hello-world-macros` | `rsx!` and a component that runs once, its text following a signal |
| `hello-world-component-host` | A component shown inside a XAML window |
| `hello-world` | XAML compiled to Rust, with an application class |

Read the sources in [`samples`](https://github.com/wieslawsoltes/FerroUI/tree/d503df2befa756e681fcdc6250e9552dc7388bea/samples) at the commit associated with these docs.

## Write the application in Rust

Create a binary crate and add `ferroui-view` with its `desktop` feature at the same version as any other FerroUI dependency ([installation](/docs/0.5.0/start/installation/)). This `main.rs` is a complete application:

```rust
use ferroui_view::prelude::*;

#[component(rerender)]
fn Counter() -> Element {
    let count = use_signal(|| 0);
    rsx! {
        StackPanel { spacing: 8.0, margin: 16.0,
            TextBlock { text: format!("Clicked {} times", count.get()) }
            Button { on_click: move |_| count.update(|c| *c += 1), "Click" }
        }
    }
}

fn main() {
    ferroui_view::launch(|| rsx! { Window { title: "Counter", width: 320.0, height: 160.0, Counter {} } });
}
```

Run it with `cargo run` and click the button. The pieces:

- `rsx!` describes elements: `StackPanel { .. }` is a control, `spacing: 8.0` sets a property, `on_click` attaches a handler, and `"Click"` is the button's content.
- `#[component(rerender)]` makes `Counter` a component that runs again when a signal it read changes. A reconciler then patches the existing controls; the button and the text block stay the same objects.
- `use_signal` keeps the count between runs. `count.get()` reads it and subscribes the component; `count.update` changes it.
- `launch` starts the desktop application with the Fluent theme and makes the `Window` element its main window. `launch_themed` takes another theme, as the hello world samples do with the WinUI 3 theme:

```rust
fn main() {
    launch_themed(
        || ferroui_themes_winui::WinUITheme::new().as_style(),
        || window().title("Hello, World!").width(400.0).height(240.0).child(rsx! { Counter {} }).into(),
        |_| {},
    );
}
```

That form needs `ferroui-themes-winui` as a dependency at the same version.

## The same view with the builders

Every control has a builder function and a setter per property and event. `rsx!` expands to these calls, so the two forms mix freely:

```rust
use ferroui_view::prelude::*;

pub fn counter() -> Element {
    let count = signal(0);
    stack_panel()
        .spacing(8.0)
        .child(text_block().text(move || format!("Clicked {} times", count.get())))
        .child(button().content("Click").on_click(move |_| count.update(|c| *c += 1)))
        .into()
}
```

This function runs once. The text property is given a closure, so it follows the signal by itself: a click updates that one property, and nothing else runs. This is the fine-grained model of SolidJS and Leptos; the component above is the model of React. [Builders, rsx! & signals](/docs/0.5.0/declarative/overview/) compares them.

## Add a field

A two-way value connects an editable property to a signal without a change handler:

```rust
use ferroui_view::prelude::*;

#[component]
pub fn Greeting() -> Element {
    let name = signal(String::new());
    rsx! {
        StackPanel { spacing: 8.0, margin: 16.0,
            TextBox { placeholder_text: "Your name", text: name.two_way() }
            TextBlock { text: move || format!("Hello, {}!", name.get()) }
        }
    }
}
```

Typing writes into `name`; the text block follows it. Continue with [components and hooks](/docs/0.5.0/declarative/components/) for state, effects, context, and lists.

## Write the view in XAML instead

XAML is the other way to write the same view. The build compiles markup to Rust, and the result drives the same controls, themes, and bindings:

```xml
<StackPanel xmlns="https://github.com/ferroui"
            Margin="24" Spacing="12">
  <TextBlock Text="Welcome to FerroUI" FontSize="24" />
  <TextBox PlaceholderText="Your name" />
  <Button Content="Continue" />
</StackPanel>
```

The XAML sample separates three responsibilities: `program.rs` configures platform detection and starts the desktop lifetime; the application class installs resources and a theme; the main window owns its content. Run it with `cargo run -p hello-world` and read [`samples/HelloWorld`](https://github.com/wieslawsoltes/FerroUI/tree/d503df2befa756e681fcdc6250e9552dc7388bea/samples/HelloWorld) for the entry point, registration, theme, fonts, and build script that the fragment above omits.

Set a view model as `DataContext` when the view needs application state. Controls inherit that context through their logical parent, unless you deliberately replace it on a child. [Connect a view model](/docs/0.5.0/guides/mvvm/) and [compile or load XAML](/docs/0.5.0/xaml/compilation/). A XAML view can host a component and a component can host XAML; see [components with XAML](/docs/0.5.0/declarative/xaml-interop/).

## Create controls directly in Rust

Underneath both, controls are framework objects that can also be created and changed imperatively:

```rust
use ferroui_controls::{StackPanel, TextBlock};
use ferroui_base::Thickness;

let heading = TextBlock::new();
heading.set_text(Some("Welcome to FerroUI"));
heading.set_font_size(24.0);
let panel = StackPanel::new();
panel.set_margin(Thickness::uniform(24.0));
panel.set_spacing(12.0);
panel.children().add(heading);
```

A framework object is held in `Ref<T>`, a reference-counted handle with the object model's inheritance and casting behavior. Call `upcast()` when a containing API expects a base control type. Use this form for infrastructure, custom controls, and integration code.

## Troubleshoot the first run

A graphics dependency error usually indicates a missing platform toolchain, not an error in your view. A window with invisible controls usually needs a theme or a usable font. A misspelled property in `rsx!` is a compile error that points at the attribute. A binding that shows nothing requires a data context and correctly registered properties. Start from a known-working sample to isolate each problem.

## Connect the pieces

Use this small progression to keep the first application understandable:

1. Run the unchanged sample and confirm that startup and shutdown work.
2. Replace only the main view's content with one of the examples above.
3. Change a label and rebuild to confirm you are editing the code that runs.
4. Keep the name in a signal (or, in XAML, a view-model property) and [bind the text box](/docs/0.5.0/guides/binding/).
5. Give Continue a [handler or command](/docs/0.5.0/guides/commands/) that reads the name.

## Try the controls


The interactive playground runs XAML in the browser. Run this small view, edit its layout, and download its complete desktop project from the example.

### Arrange a simple flow

```xml
<StackPanel xmlns="https://github.com/ferroui" Orientation="Horizontal" Spacing="12">
  <Button Content="Back" />
  <Button Content="Continue" />
  <Button Content="Cancel" />
</StackPanel>
```

```rust
use ferroui::prelude::*;
use ferroui::base::metadata::from_markup_value;
use ferroui::markup_xaml_loader::FerroRuntimeXamlLoader;

#[repr(C)]
pub struct App {
    base: Application,
}

ferro_class!(App: Application);
ferro_impl_classes!(App: FerroObjectImpl);

impl NewApplication for App {
    fn new_application() -> Ref<Self> {
        instantiate(Self { base: Application::construct() })
    }
}

impl ApplicationImpl for App {
    fn initialize(this: &Self) {
        Self::parent_initialize(this);
        ferroui::controls::register_types();
        FerroRuntimeXamlLoader::register();
        this.styles().add(FluentTheme::new().as_style());
    }

    fn on_framework_initialization_completed(this: &Self) {
        let lifetime = this.application_lifetime();
        if let Some(desktop) = lifetime.as_ref().and_then(|l| l.as_classic_desktop_style_application_lifetime()) {
            let value = FerroRuntimeXamlLoader::load(include_str!("View.xaml"), None, None, None, false)
                .expect("Example XAML should load");
            let view = from_markup_value::<Ref<Control>>(&Some(value)).expect("Example root must be a control");
            let window = Window::new();
            window.set_title(Some("FerroUI documentation example".to_owned()));
            window.set_width(560.0);
            window.set_height(420.0);
            window.set_padding(Thickness::uniform(24.0));
            window.set_content(Some(Control::boxed(view)));
            desktop.set_main_window(Some(window));
        }
        Self::parent_on_framework_initialization_completed(this);
    }
}

fn main() -> std::process::ExitCode {
    let args: Vec<String> = std::env::args().skip(1).collect();
    let result = AppBuilder::configure::<App>().use_platform_detect().start_with_classic_desktop_lifetime(&args);
    std::process::ExitCode::from(result as u8)
}

```

[Download the complete desktop project](/docs/0.5.0/generated/examples/stack-panel.zip). The browser preview runs real FerroUI controls with precompiled Rust support; this desktop application loads the same XAML.


## Public APIs and controls

- [`rsx`](/docs/0.5.0/api/ferroui-view-macros/proc_macro-rsx/), [`component`](/docs/0.5.0/api/ferroui-view-macros/proc_macro-component/), and [`Element`](/docs/0.5.0/api/ferroui-view/element/enum-element/) — declarative composition.
- [`signal`](/docs/0.5.0/api/ferroui-view/reactive/function-signal/) and [`use_signal`](/docs/0.5.0/api/ferroui-view/hooks/function-use_signal/) — state.
- [`AppBuilder`](/docs/0.5.0/api/ferroui-controls/struct-appbuilder/) and [`Application`](/docs/0.5.0/api/ferroui-controls/struct-application/) — the application model behind `launch` and the XAML sample.
- [StackPanel](/docs/0.5.0/controls/reference/stackpanel/), [TextBlock](/docs/0.5.0/controls/reference/textblock/), [TextBox](/docs/0.5.0/controls/reference/textbox/), and [Button](/docs/0.5.0/controls/reference/button/) — the view.
