Rust图形界面:eGUI的Panel布局

Panel布局

eGUI提供面板堆叠的布局方案,即Panel布局。其布局逻辑是,根据当前面板指定的方向,尽可能地填充空间。

  • CentralPanel 占据屏幕剩余部分的空间
  • SidePanel 占据屏幕两侧的空间,在具体调用时,可指定left或者right
  • TopBottomPanel 占据屏幕顶部或底部的空间,具体调用时,可指定top或者bottom

示例如下

在这里插入图片描述

其压入顺序以及调用的布局函数如下表

顺序标题函数
1Top PanelTopBottomPanel::top
2Left PanelSidePanel::left
3Right PanelSidePanel::right
4Bottom PanelTopBottomPanel::bottom
5Central PanelCentralPanel

尺寸调节

由上图可见,Panel之间的界限是允许用鼠标拖动的,这是因为在创建Panel之后,将其设为尺寸可调节。SidePanel和TopBottomPanel均支持resizable方法,但二者的调节方向不同,前者是水平可调,后者是竖直可调。相应地,前者可以设置水平方向的调节尺寸,例如min_width, max_width以及width_range等,而后者则可设置min_height, max_height以及height_range等。

源码

用于布局的核心代码如下

impl eframe::App for MyApp {
    fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) {
        egui::CentralPanel::default().show(ctx, |ui| {
            egui::TopBottomPanel::top("top_panel")
                .resizable(true)
                .min_height(32.0)
                .show_inside(ui, |ui| {ui.heading("Top Panel");});

            egui::SidePanel::left("left_panel")
                .resizable(true)
                .default_width(150.0)
                .width_range(80.0..=200.0)
                .show_inside(ui, |ui| {
                    ui.vertical_centered(|ui| {ui.heading("Left Panel");});
                });

            egui::SidePanel::right("right_panel")
                .resizable(true)
                .show_inside(ui, |ui| {
                    ui.vertical_centered(|ui| {ui.heading("Right Panel");});
                });

            egui::TopBottomPanel::bottom("bottom_panel")
                .show_inside(ui, |ui| {
                    ui.vertical_centered(|ui| {ui.heading("Bottom Panel");});
                });

            egui::CentralPanel::default()
                .show_inside(ui, |ui| {
                ui.vertical_centered(|ui| {ui.heading("Central Panel");});
            });

        });
    }
}

下面是程序运行的其他代码。

#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")]
use std::fmt::format;

use eframe::egui;

struct MyApp {
}

impl Default for MyApp {
    fn default() -> Self {
        Self { }
    }
}
fn main() -> Result<(), eframe::Error> {
    let options = eframe::NativeOptions {
        initial_window_size: Some(egui::vec2(640.0, 320.0)),
        ..Default::default()
    };
    eframe::run_native(
        "layout",
        options,
        Box::new(|_cc| Box::<MyApp>::default()),
    )
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

微小冷

请我喝杯咖啡

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值