use std::sync::atomic::{AtomicI64, AtomicUsize, Ordering};
use swiftui_native::prelude::*;
use swiftui_native::state;
use swiftui_native::views::{ButtonStyle, TextStyle};
const ACTIVITIES: &[&str] = &[
"Archery", "Baseball", "Basketball", "Bowling", "Boxing", "Cricket", "Curling", "Fencing",
"Golf", "Hiking", "Lacrosse", "Rugby", "Squash",
];
const COLORS: &[SystemColor] = &[
SystemColor::Blue,
SystemColor::Cyan,
SystemColor::Gray,
SystemColor::Green,
SystemColor::Indigo,
SystemColor::Mint,
SystemColor::Orange,
SystemColor::Pink,
SystemColor::Purple,
SystemColor::Red,
];
static SELECTED: AtomicUsize = AtomicUsize::new(1);
static ID: AtomicI64 = AtomicI64::new(1);
static COLOR: AtomicUsize = AtomicUsize::new(0);
fn selected() -> &'static str {
ACTIVITIES[SELECTED.load(Ordering::Relaxed) % ACTIVITIES.len()]
}
fn random(upper: usize) -> usize {
static SEED: AtomicUsize = AtomicUsize::new(0x2545_F491_4F6C_DD1D);
let mut x = SEED.load(Ordering::Relaxed);
x ^= x << 13;
x ^= x >> 7;
x ^= x << 17;
SEED.store(x, Ordering::Relaxed);
x % upper
}
fn body() -> View {
let colour = COLORS[COLOR.load(Ordering::Relaxed) % COLORS.len()];
vstack![
text("Why not try…").text_style(TextStyle::LargeTitle).bold(),
vstack![
circle_shape().fill_system(colour).padding(16.0).overlay(
image!("figure.{}", selected().to_lowercase())
.font_size(144.0)
.fg(Color::WHITE),
),
text!("{}!", selected()).text_style(TextStyle::Title),
]
.transition(Transition::Slide)
.id(ID.load(Ordering::Relaxed)),
spacer(),
button("Try again", try_again).button_style(ButtonStyle::BorderedProminent),
]
}
fn try_again() {
SELECTED.store(random(ACTIVITIES.len()), Ordering::Relaxed);
COLOR.store(random(COLORS.len()), Ordering::Relaxed);
ID.fetch_add(1, Ordering::Relaxed);
state::refresh_animated(state::ease_in_out(1.0));
}
fn content_view() -> swiftui_native::ViewHandle {
state::root_view(|_generation| body().into_handle())
}
#[cfg(target_os = "macos")]
#[path = "common/simulator.rs"]
mod simulator;
#[cfg(target_os = "macos")]
fn main() {
if std::env::args().any(|a| a == "--sim" || a == "device" || a == "ipa") {
return simulator::main(&simulator::Bundle {
example: "swift_org_tutorial",
bundle_id: "dev.rswift.WhyNotTry",
executable: "WhyNotTry",
display_name: "Why not try…",
launch_args: &[],
});
}
if std::env::args().any(|a| a == "--render") {
let out = concat!(env!("CARGO_MANIFEST_DIR"), "/../../target/tutorial.png");
let frame = swiftui_native::render::render(&content_view(), 420, 640);
frame.write_png(out);
eprintln!("wrote {out} ({} colours)", frame.distinct());
return;
}
show_window(&content_view(), "Why not try…", 420.0, 640.0);
}
#[cfg(not(target_os = "macos"))]
fn main() {
swiftui_native::app::run(content_view)
}