您现在的位置是:首页 > 文章详情

Masonry —— Rust GUI 框架

日期:2023-02-08点击:357

Masonry 是一个 Rust GUI 框架。

Masonry 提供了用于创建窗口的平台(使用 Glazier 作为后端),每个窗口都包含一个 Widget 树。它还提供了用于在 runtime 检测 Widget 树的工具,可编写单元测试,方便开发者调试和维护应用程序。

基于 Masonry,开发者可实现即时模式 (immediate-mode) GUI、Elm 架构、函数式响应 GUI 等。

示例代码

use masonry::widget::{ Button, CrossAxisAlignment, Flex, Label, Portal, SizedBox, TextBox, WidgetMut, }; use masonry::{ Action, AppDelegate, AppLauncher, Color, DelegateCtx, Env, WidgetId, WindowDescription, WindowId, }; struct Delegate { next_task: String, } impl AppDelegate for Delegate { fn on_action( &mut self, ctx: &mut DelegateCtx, action: Action, ) { match action { Action::ButtonPressed | Action::TextEntered(_) => { let mut root: WidgetMut<Portal<Flex>> = ctx.get_root(); if self.next_task != "" { let mut flex = root.child_mut(); flex.add_child(Label::new(self.next_task.clone())); } } Action::TextChanged(new_text) => { self.next_task = new_text.clone(); } _ => {} } } } fn main() { // The main button and text box with some space below, // all inside a scrollable area. let root_widget = Portal::new( Flex::column() .with_child( Flex::row() .with_child(TextBox::new("")) .with_child(Button::new("Add task")), ) .with_spacer(VERTICAL_WIDGET_SPACING), ) .constrain_horizontal(true); let main_window = WindowDescription::new(root_widget) .title("To-do list") .window_size((400.0, 400.0)); AppLauncher::with_window(main_window) .with_delegate(Delegate { next_task: String::new(), }) .launch() .expect("Failed to launch application"); }
原文链接:https://www.oschina.net/p/masonry-rs
关注公众号

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。

持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。

转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。

文章评论

共有0条评论来说两句吧...

文章二维码

扫描即可查看该文章

点击排行

推荐阅读

最新文章