# 2. Handle a button click

Source: https://daybrite.dev/docs/tutorials/events

Add a button that opens an alert. Start from [lesson 1](https://daybrite.dev/docs/tutorials/first-app) in the same
app directory. Keep `Cargo.toml`, `Day.toml`, `build.rs`, and `src/main.rs`.

## 1. Add the messages

Append to `resource/locales/en/app.ftl`; retain existing keys:

**All platforms: Append to `resource/locales/en/app.ftl`**

```ftl
try_button = Try the button
click_received = Your button click reached the app.
```

## 2. Connect the button

Replace `src/lib.rs`:

**All platforms: Write `src/lib.rs`**

```rust
use day::prelude::*;

day::resources!();
day::day_start!(options: res::window_options(), root);

pub fn root() -> impl Piece {
    column((
        label(res::str::welcome()),
        button(res::str::try_button())
            .action(|| {
                day::task(async {
                    alert(res::str::click_received()).await;
                });
            })
            .id(id!(try_button)),
    ))
    .spacing(Space::M)
    .padding(Space::L)
}
```

[`column`](https://docs.rs/day-pieces/latest/day_pieces/fn.column.html) arranges controls vertically.
[`button`](https://docs.rs/day-pieces/latest/day_pieces/fn.button.html) creates the button;
[`.action`](https://docs.rs/day-pieces/latest/day_pieces/trait.ButtonBuilder.html#tymethod.action)
registers its zero-argument click callback. `id!(try_button)` identifies it for automation;
IDs do not replace accessible labels.

[`day::task`](https://docs.rs/day-app/latest/day/fn.task.html) runs the async UI operation on the UI
thread; `await` waits for alert dismissal without blocking the event loop. Construct localized
messages inside this task. Run blocking network or file work on a worker thread.

## 3. Try the event

**macOS: Run in `first-app`**

```sh
day launch -t macos-appkit
```

**Windows: Run in `first-app`**

```powershell
day launch -t windows-winui
```

**Linux: Run in `first-app`**

```sh
day launch -t linux-gtk
```

Press **Try the button**. Expect **Your button click reached the app.** Dismiss the alert and
repeat to verify the callback runs on each click.

**macOS:** Quit with Command-Q before continuing.

**Windows:** Close the app window before continuing.

**Linux:** Close the app window before continuing.

## Troubleshooting

If the alert is absent, check the button's
[`.action`](https://docs.rs/day-pieces/latest/day_pieces/trait.ButtonBuilder.html#tymethod.action)
callback, save, quit, and relaunch. Missing `try_button` or `click_received` accessors indicate
missing Fluent keys; add them and rebuild.

## Next step

Continue to [3. Update state](https://daybrite.dev/docs/tutorials/state).
