Files
simple-rust-tests/__gui/druid/src/main.rs
2021-05-01 13:20:30 +08:00

52 lines
1.6 KiB
Rust

use druid::widget::{Button, Flex, Label};
use druid::{AppLauncher, LocalizedString, PlatformError, Widget, WidgetExt, WindowDesc, Data, Point};
#[derive(Clone, Data)]
struct Counter(i32);
fn main() -> Result<(), PlatformError> {
// let menu = MenuDesc::new(LocalizedString::new("test"));
// Window builder. We set title and size
let main_window = WindowDesc::new(ui_builder)
.title("Hello, Druid!")
.window_size((600.0, 400.0))
// .menu(menu)
.set_position(Point::new(300.0, 200.0));
// Data to be used in the app (=state)
let data: Counter = Counter(0);
// Run the app
AppLauncher::with_window(main_window)
.use_simple_logger() // Neat!
.launch(data)
}
fn ui_builder() -> impl Widget<Counter> {
// The label text will be computed dynamically based on the current locale and count
let text = LocalizedString::new("hello-counter")
.with_arg("count", |data: &Counter, _env| (*data).0.into());
let label = Label::new(text).padding(5.0).center();
// Two buttons with on_click callback
let button_plus = Button::new("+1")
.fix_size(80.0, 30.0)
.on_click(|_ctx, data: &mut Counter, _env| (*data).0 += 1)
.padding(5.0);
let button_minus = Button::new("-1")
.fix_size(80.0, 30.0)
.on_click(|_ctx, data: &mut Counter, _env| (*data).0 -= 1)
.padding(5.0);
// Container for the two buttons
let flex = Flex::row()
.with_child(button_plus)
.with_spacer(1.0)
.with_child(button_minus);
// Container for the whole UI
Flex::column()
.with_child(label)
.with_child(flex)
}