Files
ai-app/app/examples/phone.rs
T

97 lines
3.1 KiB
Rust

use iris::prelude::*;
use winit::{dpi::PhysicalSize, window::WindowAttributes};
fn ime_argv() -> Option<f32> {
let mut args = std::env::args().skip(1);
while let Some(arg) = args.next() {
if arg == "--ime" {
return args.next()?.parse().ok();
}
}
None
}
fn message_argv() -> Option<String> {
let mut args = std::env::args().skip(1);
while let Some(arg) = args.next() {
if arg == "--message" {
return Some(args.next()?.replace("\\n", "\n"));
}
}
None
}
fn typed_argv() -> Option<String> {
let mut args = std::env::args().skip(1);
while let Some(arg) = args.next() {
if arg == "--typed" {
return Some(args.next()?.replace("\\n", "\n"));
}
}
None
}
fn main() {
DesktopApp::<Client>::run();
}
#[derive(DesktopUiState)]
pub struct Client {
ui_state: DesktopUiState,
#[allow(dead_code)]
screen: Option<ai_app::ui::TranscriptScreen>,
}
impl DesktopAppState for Client {
fn window_attributes() -> WindowAttributes {
WindowAttributes::default()
.with_title("iris transcript (bench fixture)")
.with_inner_size(PhysicalSize::new(
ai_app::ui::fixture::PHONE_WIDTH,
ai_app::ui::fixture::PHONE_HEIGHT,
))
}
fn new(mut ui_state: DesktopUiState, rsc: &mut StdRsc<Self>) -> Self {
let screen = match ai_app::ui::fixture::open(rsc, &mut ui_state) {
Ok(opened) => {
if let Some(message) = message_argv() {
(opened.screen.composer.field)(rsc).set(&message);
}
if let Some(text) = typed_argv() {
let field = opened.screen.composer.field;
rsc.spawn_task(async move |mut ctx| {
for ch in text.chars() {
tokio::time::sleep(std::time::Duration::from_millis(100)).await;
ctx.update(move |state: &mut Client, rsc| {
state.set_focus(Some(field));
let end = rsc[field].text().len();
let edit = field(rsc);
if edit.caret().is_none() {
edit.set_cursor_byte(end);
}
edit.insert(&ch.to_string());
});
}
});
}
if let Some(inset) = ime_argv() {
opened.screen.composer.set_bottom_inset(rsc, inset);
}
Some(opened.screen)
}
Err(message) => {
let text = wtext(format!("Couldn't fold the bench fixture: {message}"))
.color(PaintId::WHITE)
.wrap(true)
.pad(dp(16))
.add_strong(rsc)
.any();
ui_state.set_root(rsc, text);
None
}
};
Self { ui_state, screen }
}
}