#![windows_subsystem = "windows"]
use anyhow::{Error, Result};
use fui_app::*;
use fui_controls::*;
use fui_core::*;
use fui_macros::ui;
use std::cell::RefCell;
use std::rc::Rc;
use tokio::task::LocalSet;
use typed_builder::TypedBuilder;
use typemap::TypeMap;
struct MainViewModel {
pub counter: Property<i32>,
pub counter2: Property<i32>,
}
impl MainViewModel {
pub fn new() -> Rc<Self> {
Rc::new(MainViewModel {
counter: Property::new(10),
counter2: Property::new(0),
})
}
pub fn increase(self: &Rc<Self>) {
self.counter.change(|c| c + 1);
}
pub fn decrease(self: &Rc<Self>) {
self.counter.change(|c| c - 1);
}
}
#[derive(TypedBuilder)]
pub struct ButtonText {
#[builder(default = Property::new("".to_string()))]
pub text: Property<String>,
#[builder(default = Callback::empty())]
pub clicked: Callback<()>,
}
impl ButtonText {
pub fn to_view(
self,
_style: Option<Box<dyn Style<Self>>>,
_context: ViewContext,
) -> Rc<RefCell<dyn ControlObject>> {
ui! {
Button {
clicked: self.clicked,
Text { text: self.text }
}
}
}
}
impl ViewModel for MainViewModel {
fn create_view(self: &Rc<Self>) -> Rc<RefCell<dyn ControlObject>> {
self.counter2.bind(&self.counter);
self.counter.bind(&self.counter2);
ui!(
Horizontal {
Margin: Thickness::sides(0.0f32, 5.0f32),
Text {
Margin: Thickness::all(5.0f32),
text: (&self.counter, |counter| format!("Counter {}", counter))
},
Button {
clicked: cb!(self, decrease),
Text { text: "Decrease" }
},
ButtonText {
clicked: cb!(self, increase),
text: "Increase"
},
Text {
Margin: Thickness::all(5.0f32),
text: (&self.counter2, |counter| format!("Counter2 {}", counter))
},
}
)
}
}
#[tokio::main(flavor = "current_thread")]
async fn main() -> Result<()> {
LocalSet::new()
.run_until(async {
let app = Application::new("Example: button").await?;
let mut window = Window::create(
WindowOptions::new()
.with_title("Example: button")
.with_size(800, 600),
)
.await?;
window.set_vm(MainViewModel::new());
app.run().await?;
Ok::<(), Error>(())
})
.await
}