rust gui开发 根据官网例子学习orbtk(3)

多窗口显示

效果图
在这里插入图片描述
依赖

orbtk = "0.3.1-alpha3"
serde_derive = "1.0.106"

代码

/// mutli window will not work properly on web now. But it will be fixed.
use orbtk::prelude::*;

#[derive(AsAny, Default)]
struct MainState {
    show_window: bool,
}

impl MainState {
    fn show_window(&mut self) {
        self.show_window = true;
    }
}

impl State for MainState {
    fn update(&mut self, _: &mut Registry, ctx: &mut Context) {
        if self.show_window {
            button(ctx.child("button")).set_enabled(false);
            ctx.show_window(|ctx| {
                Window::new()
                    .title("Dialog")
                    .position((120.0, 120.0))
                    .size(100.0, 75.0)
                    .child(
                        Stack::new()
                            .child(TextBlock::new().text("New window").margin(4.0).build(ctx))
                            .build(ctx),
                    )
                    .build(ctx)
            });
            self.show_window = false;
        }
    }
}

widget!(MainView<MainState>);

impl Template for MainView {
    fn template(self, id: Entity, ctx: &mut BuildContext) -> Self {
        self.child(
            Stack::new()
                .child(TextBlock::new().text("Window 1").margin(4.0).build(ctx))
                .child(
                    Button::new()
                        .id("button")
                        .on_click(move |states, _| {
                            states.get_mut::<MainState>(id).show_window();
                            true
                        })
                        .margin(4.0)
                        .text("Show window")
                        .build(ctx),
                )
                .build(ctx),
        )
    }
}

fn main() {
    // use this only if you want to run it as web application.
    orbtk::initialize();

    Application::new()
        .window(|ctx| {
            Window::new()
                .title("OrbTk - multi window example window 1")
                .position((100.0, 100.0))
                .size(420.0, 730.0)
                .child(MainView::new().build(ctx))
                .build(ctx)
        })
        .window(|ctx| {
            Window::new()
                .title("OrbTk - multi window example window 2")
                .position((600.0, 100.0))
                .size(420.0, 730.0)
                .child(
                    Stack::new()
                        .child(TextBlock::new().text("Window 2").margin(4.0).build(ctx))
                        .child(Button::new().margin(4.0).text("Click me").build(ctx))
                        .build(ctx),
                )
                .build(ctx)
        })
        .run();
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值