This version of the starter counter adds checked-in English and Spanish text, a safe increment limit and a focused Rust test. It is not a verbatim copy of the generated template. Both native and Web entrypoints below launch it.
{"id":"fission-tabs-1","tabs":[{"index":0,"label":"src/app.rs","value":"src-app-rs"},{"index":1,"label":"src/translations.rs","value":"src-translations-rs"},{"index":2,"label":"i18n/en-US.yaml","value":"i18n-en-us-yaml"},{"index":3,"label":"i18n/es-ES.yaml","value":"i18n-es-es-yaml"},{"index":4,"label":"src/lib.rs","value":"src-lib-rs"},{"index":5,"label":"preview/lib.rs","value":"preview-lib-rs"}]}
{"id":"fission-tabs-1","index":0,"label":"src/app.rs","value":"src-app-rs"}
use fission::prelude::*;
#[derive(Default, Debug, Clone, PartialEq, Eq)]
pub struct CounterState { pub count: u32 }
impl GlobalState for CounterState {}
impl CounterState {
pub fn increment(&mut self) { self.count = self.count.saturating_add(1); }
}
fission_component! {
pub CounterApp<CounterState> {}
view(_app, state, cx) {
let tokens = &cx.view.env().theme.tokens;
} =>
SafeArea(child: Scroll(child: Container(
child: SemanticsRegion(
identifier: Some("starter-counter".to_string()),
label: Some(cx.tr("counter.title")),
child: Column(gap: tokens.spacing.m, children: [
Text(cx.tr("counter.title")).size(tokens.typography.heading2_size),
Text(cx.tr("counter.context")),
Text(cx.tr("counter.value").replace("{count}", &state.count.to_string())),
Button(
child: Text(cx.tr("counter.increment")),
key: "increment",
disabled: state.count == u32::MAX,
on_press: reduce { state.increment(); },
).semantics_identifier("counter.increment"),
]),
),
).padding_all(tokens.spacing.l)))
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn one_action_adds_one_and_the_limit_does_not_overflow() {
let mut state = CounterState::default();
state.increment();
assert_eq!(state.count, 1);
state.count = u32::MAX;
state.increment();
assert_eq!(state.count, u32::MAX);
}
}
{"id":"fission-tabs-1","index":1,"label":"src/translations.rs","value":"src-translations-rs"}
use std::collections::HashMap;
use fission::prelude::*;
pub fn create_env() -> Env {
let mut env = Env::default();
env.locale = Locale::from("en-US");
for (locale, source) in [
("en-US", include_str!("../i18n/en-US.yaml")),
("es-ES", include_str!("../i18n/es-ES.yaml")),
] {
let messages: HashMap<String, String> =
serde_yaml::from_str(source).expect("valid checked-in translations");
env.i18n.add_bundle(TranslationBundle { locale: Locale::from(locale), messages });
}
env
}
{"id":"fission-tabs-1","index":2,"label":"i18n/en-US.yaml","value":"i18n-en-us-yaml"}
counter.title: "Check the Android launch path"
counter.context: "This small counter checks that this build launches and handles an action. It does not test a device capability."
counter.value: "Count: {count}"
counter.increment: "Increment count"
{"id":"fission-tabs-1","index":3,"label":"i18n/es-ES.yaml","value":"i18n-es-es-yaml"}
counter.title: "Comprobar el arranque de Android"
counter.context: "Este contador comprueba que la compilación arranca y responde a una acción. No prueba capacidades del dispositivo."
counter.value: "Cuenta: {count}"
counter.increment: "Incrementar la cuenta"
{"id":"fission-tabs-1","index":4,"label":"src/lib.rs","value":"src-lib-rs"}
pub mod app;
mod translations;
use app::{CounterApp, CounterState};
use fission::prelude::*;
#[cfg(not(any(target_arch = "wasm32", target_os = "android", target_os = "ios")))]
pub fn run_desktop() -> anyhow::Result<()> {
DesktopApp::<CounterState, _>::new(CounterApp)
.with_env(translations::create_env())
.run()
}
#[cfg(any(target_os = "android", target_os = "ios"))]
fn mobile_app() -> MobileApp<CounterState, CounterApp> {
let app = MobileApp::<CounterState, _>::new(CounterApp)
.with_env(translations::create_env());
#[cfg(all(target_os = "android", debug_assertions))]
let app = app.with_test_control_port(48761);
app
}
#[cfg(any(target_os = "android", target_os = "ios"))]
pub fn run_mobile() -> anyhow::Result<()> { mobile_app().run() }
#[cfg(target_os = "android")]
#[no_mangle]
fn android_main(app_handle: AndroidApp) {
let _ = mobile_app().run_with_android_app(app_handle);
}
#[cfg(target_arch = "wasm32")]
#[wasm_bindgen::prelude::wasm_bindgen(start)]
pub fn run_web() -> Result<(), wasm_bindgen::JsValue> {
console_error_panic_hook::set_once();
WebApp::<CounterState, _>::new(CounterApp)
.with_env(translations::create_env())
.run()
.map_err(|error| wasm_bindgen::JsValue::from_str(&error.to_string()))
}
{"id":"fission-tabs-1","index":5,"label":"preview/lib.rs","value":"preview-lib-rs"}
mod app;
mod translations;
use fission::prelude::*;
use wasm_bindgen::prelude::*;
#[wasm_bindgen(start)]
pub fn run() -> Result<(), JsValue> {
console_error_panic_hook::set_once();
WebApp::<app::CounterState, _>::new(app::CounterApp)
.with_env(translations::create_env())
.mount("#fission-web-mount")
.run()
.map_err(|error| JsValue::from_str(&error.to_string()))
}
{"id":"fission-tabs-1"}