2. Handle a button click
Choose the computer you’ll build on. Your choice applies throughout these six lessons.
Add a button that opens an alert. Start from lesson 1 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:
Append to resource/locales/en/app.ftl:
try_button = Try the button
click_received = Your button click reached the app.
2. Connect the button
Replace src/lib.rs:
Write src/lib.rs:
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 arranges controls vertically.
button creates the button;
.action
registers its zero-argument click callback. id!(try_button) identifies it for automation;
IDs do not replace accessible labels.
day::task 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
day launch -t macos-appkit
Windows
day launch -t windows-winui
Linux
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
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.