The editor and preview are named components. Each responsive branch constructs its own instances, whilst both read the same AnnouncementState. Do not construct one Widget and clone it into both branches: retained widget identity and shared application data are not interchangeable.
The breakpoint is a layout choice, not a claim that every iPad is wide. A narrow split-screen window on an iPad should receive the compact arrangement too. The example uses a container query because it must also respond correctly when embedded in the documentation's preview panel.
{"id":"fission-tabs-1","tabs":[{"index":0,"label":"src/app.rs","value":"src-app-rs"},{"index":1,"label":"src/app/state.rs","value":"src-app-state-rs"},{"index":2,"label":"src/app/editor.rs","value":"src-app-editor-rs"},{"index":3,"label":"src/app/preview.rs","value":"src-app-preview-rs"},{"index":4,"label":"src/app/screen.rs","value":"src-app-screen-rs"},{"index":5,"label":"src/i18n.rs","value":"src-i18n-rs"},{"index":6,"label":"src/lib.rs","value":"src-lib-rs"},{"index":7,"label":"preview/lib.rs","value":"preview-lib-rs"},{"index":8,"label":"i18n/en-US.yaml","value":"i18n-en-us-yaml"},{"index":9,"label":"i18n/es-ES.yaml","value":"i18n-es-es-yaml"}]}
{"id":"fission-tabs-1","index":0,"label":"src/app.rs","value":"src-app-rs"}
mod editor;
mod preview;
mod screen;
pub mod state;
pub use screen::AnnouncementScreen;
pub use state::AnnouncementState;
{"id":"fission-tabs-1","index":1,"label":"src/app/state.rs","value":"src-app-state-rs"}
use fission::prelude::*;
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct AnnouncementState {
pub title: String,
pub message: String,
pub locale: Locale,
}
impl Default for AnnouncementState {
fn default() -> Self {
Self {
title: String::new(),
message: String::new(),
locale: Locale::from("en-US"),
}
}
}
impl GlobalState for AnnouncementState {}
{"id":"fission-tabs-1","index":2,"label":"src/app/editor.rs","value":"src-app-editor-rs"}
use super::state::AnnouncementState;
use fission::prelude::*;
fission_component! {
pub AnnouncementEditor<AnnouncementState> {}
view(_editor, state, cx) {
let tokens = &cx.view.env().theme.tokens;
} => Column(gap: tokens.spacing.m, children: [
Text(cx.tr("announcement.edit")).weight(tokens.typography.font_weight_semibold),
FormControl(
label: Some(cx.tr("announcement.title")),
required: true,
child: TextInput(
value: state.title.clone(),
bind(on_input): to(state.title),
),
),
TextInput(
value: state.message.clone(),
bind(on_input): to(state.message),
label: Some(TextContent::Key("announcement.message".into())),
multiline: true,
keyboard_type: TextInputType::Multiline,
),
Button(
child: Text(cx.tr("announcement.clear")), key: "clear-draft",
disabled: state.title.is_empty() && state.message.is_empty(),
on_press: reduce { state.title.clear(); state.message.clear(); },
),
])
}
{"id":"fission-tabs-1","index":3,"label":"src/app/preview.rs","value":"src-app-preview-rs"}
use super::state::AnnouncementState;
use fission::prelude::*;
fission_component! {
pub AnnouncementPreview<AnnouncementState> {}
view(_preview, state, cx) {
let tokens = &cx.view.env().theme.tokens;
} => Container(
Column(gap: tokens.spacing.m, children: [
Text(cx.tr("announcement.preview")).color(tokens.colors.text_secondary),
if state.title.trim().is_empty() && state.message.trim().is_empty() {
Text(cx.tr("announcement.empty"))
} else {
Column(gap: tokens.spacing.s, children: [
Text(state.title.clone()).size(tokens.typography.heading2_size),
Text(state.message.clone()),
])
},
Text(cx.tr("announcement.not_sent")).color(tokens.colors.text_secondary),
]),
)
.padding_all(tokens.spacing.m)
.border(tokens.colors.border, 1.0)
.border_radius(tokens.radii.medium)
}
{"id":"fission-tabs-1","index":4,"label":"src/app/screen.rs","value":"src-app-screen-rs"}
use super::{editor::AnnouncementEditor, preview::AnnouncementPreview, state::AnnouncementState};
use fission::prelude::*;
const TWO_PANE_MIN_WIDTH: f32 = 760.0;
fission_component! {
pub WideAnnouncement<AnnouncementState> {}
view(_layout, _state, cx) {
let tokens = &cx.view.env().theme.tokens;
} => Row(gap: tokens.spacing.l, children: [
Container(AnnouncementEditor).flex_grow(1.0),
Container(AnnouncementPreview).flex_grow(1.0),
])
}
fission_component! {
pub CompactAnnouncement<AnnouncementState> {}
view(_layout, _state, cx) {
let tokens = &cx.view.env().theme.tokens;
} => Column(gap: tokens.spacing.l, children: [
AnnouncementEditor,
AnnouncementPreview,
])
}
fission_component! {
pub AnnouncementScreen<AnnouncementState> {}
view(_screen, state, cx) {
let tokens = &cx.view.env().theme.tokens;
} => SafeArea(
child: Scroll(
child: Container(Column(gap: tokens.spacing.m, children: [
Text(cx.tr("announcement.heading")).size(tokens.typography.heading2_size),
Text(cx.tr("announcement.explanation")),
Row(gap: tokens.spacing.s, children: [
Button(
child: Text(cx.tr("language.english")), key: "english",
on_press: reduce { state.locale = Locale::from("en-US"); },
),
Button(
child: Text(cx.tr("language.spanish")), key: "spanish",
on_press: reduce { state.locale = Locale::from("es-ES"); },
),
]),
Responsive::new_with_id(
WidgetId::explicit("announcement.layout"),
|| WideAnnouncement,
)
.case_max_width_with(TWO_PANE_MIN_WIDTH, || CompactAnnouncement)
.container_query(),
]))
.padding_all(tokens.spacing.l),
),
)
}
{"id":"fission-tabs-1","index":5,"label":"src/i18n.rs","value":"src-i18n-rs"}
use fission::i18n::{Locale, TranslationBundle};
use fission::prelude::*;
use std::collections::HashMap;
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 announcement translations");
env.i18n.add_bundle(TranslationBundle {
locale: Locale::from(locale),
messages,
});
}
env
}
pub fn sync_env(state: &crate::app::AnnouncementState, env: &mut Env) {
env.locale = state.locale.clone();
}
{"id":"fission-tabs-1","index":6,"label":"src/lib.rs","value":"src-lib-rs"}
pub mod app;
mod i18n;
use app::{AnnouncementScreen, AnnouncementState};
use fission::prelude::*;
#[cfg(not(any(target_arch = "wasm32", target_os = "android", target_os = "ios")))]
pub fn run_desktop() -> anyhow::Result<()> {
DesktopApp::<AnnouncementState, _>::new(AnnouncementScreen)
.with_env(i18n::create_env())
.with_sync_env(i18n::sync_env)
.run()
}
#[cfg(any(target_os = "android", target_os = "ios"))]
pub fn run_mobile() -> anyhow::Result<()> {
MobileApp::<AnnouncementState, _>::new(AnnouncementScreen)
.with_env(i18n::create_env())
.with_sync_env(i18n::sync_env)
.run()
}
#[cfg(target_os = "android")]
#[no_mangle]
fn android_main(app_handle: AndroidApp) {
let _ = MobileApp::<AnnouncementState, _>::new(AnnouncementScreen)
.with_env(i18n::create_env())
.with_sync_env(i18n::sync_env)
.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::<AnnouncementState, _>::new(AnnouncementScreen)
.with_env(i18n::create_env())
.with_sync_env(i18n::sync_env)
.run()
.map_err(|error| wasm_bindgen::JsValue::from_str(&error.to_string()))
}
{"id":"fission-tabs-1","index":7,"label":"preview/lib.rs","value":"preview-lib-rs"}
mod app;
mod i18n;
use fission::prelude::*;
use wasm_bindgen::prelude::*;
#[wasm_bindgen(start)]
pub fn run() -> Result<(), JsValue> {
console_error_panic_hook::set_once();
WebApp::<app::AnnouncementState, _>::new(app::AnnouncementScreen)
.with_env(i18n::create_env())
.with_sync_env(i18n::sync_env)
.mount("#fission-web-mount")
.run()
.map_err(|error| JsValue::from_str(&error.to_string()))
}
{"id":"fission-tabs-1","index":8,"label":"i18n/en-US.yaml","value":"i18n-en-us-yaml"}
announcement.heading: "Prepare a team announcement"
announcement.explanation: "Write a message and review it beside the editor when there is room, or below it on a narrow screen."
announcement.edit: "Edit the announcement"
announcement.title: "Announcement title"
announcement.message: "Message"
announcement.clear: "Clear draft"
announcement.preview: "Message preview"
announcement.empty: "Your announcement will appear here as you write."
announcement.not_sent: "This is a preview. Nothing has been sent or saved to disk."
language.english: "English"
language.spanish: "Español"
{"id":"fission-tabs-1","index":9,"label":"i18n/es-ES.yaml","value":"i18n-es-es-yaml"}
announcement.heading: "Preparar un anuncio para el equipo"
announcement.explanation: "Escribe un mensaje y revísalo junto al editor cuando haya espacio, o debajo en una pantalla estrecha."
announcement.edit: "Editar el anuncio"
announcement.title: "Título del anuncio"
announcement.message: "Mensaje"
announcement.clear: "Borrar borrador"
announcement.preview: "Vista previa del mensaje"
announcement.empty: "Tu anuncio aparecerá aquí mientras escribes."
announcement.not_sent: "Esto es una vista previa. No se ha enviado ni guardado nada en disco."
language.english: "Inglés"
language.spanish: "Español"
{"id":"fission-tabs-1"}