所选的 egui 组合框矢量

问题描述 投票:0回答:2

我正在尝试使用向量而不是文档中指定的枚举,但我不知道如何实现所选部分。我当前的代码是

egui::ComboBox::from_label("Take your pick")
    .selected_text(format!("{}", self.radio[0]))
    .show_ui(ui, |ui| {
        for i in 0..self.radio.len() {
            ui.selectable_value(&mut &self.radio, &self.radio, &self.radio[i]);
        }
    });

任何人都可以给我一个想法吗?我不介意使用枚举,但我不知道里面会有多少东西。

user-interface rust
2个回答
4
投票

我偶然发现了同样的问题,并且找到了解决方案。 解决办法是给 self 添加一个新的变量。 这是有关枚举选择和向量选择的基本示例。 希望这对遇到同样问题的人有所帮助。

#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")]

use eframe::egui;

fn main() {
    let options = eframe::NativeOptions::default();
    eframe::run_native(
        "Test Select with Enum and Vector",
        options,
        Box::new(|_cc| Box::new(MyApp::default())),
    );
}

#[derive(PartialEq)]
#[derive(Debug)]
enum OS_Select {
    First,
    Second,
    Third,
}

struct MyApp {
    selected: usize,
    radio: OS_Select,
    selector_vec: Vec<String>,
}

impl Default for MyApp {
    fn default() -> Self {
        Self {
            selected: 0,
            radio: OS_Select::First,
            selector_vec: get_vec(),
        }
    }
}

fn get_vec() -> Vec<String> {
    let vecs = [
        "1".to_string(),
        "2".to_string(),
        "3".to_string(),
    ].to_vec();
    return vecs;
}

impl eframe::App for MyApp {
    fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) {
        egui::CentralPanel::default().show(ctx, |ui| {
            ui.heading("Select with enum");
            ui.horizontal(|ui| {
                ui.radio_value(&mut self.radio, OS_Select::First, "First");
                ui.radio_value(&mut self.radio, OS_Select::Second, "Second");
                ui.radio_value(&mut self.radio, OS_Select::Third, "Third");
            });
            ui.end_row();

            ui.heading("Select with Vectors");

            egui::ComboBox::from_label("Take your pick")
            .selected_text(format!("{}", &self.selector_vec[self.selected]))
            .show_ui(ui, |ui| {
                
                for i in 0..self.selector_vec.len() {
                    let value = ui.selectable_value(&mut &self.selector_vec[i], &self.selector_vec[self.selected], &self.selector_vec[i]);
                    if (value.clicked()) {
                        self.selected = i;
                    }
                }
            });
            ui.end_row();
        });
    }

    fn save(&mut self, _storage: &mut dyn eframe::Storage) {}

    fn on_close_event(&mut self) -> bool {
        true
    }

    fn on_exit(&mut self, _gl: Option<&eframe::glow::Context>) {}

    fn auto_save_interval(&self) -> std::time::Duration {
        std::time::Duration::from_secs(30)
    }

    fn max_size_points(&self) -> egui::Vec2 {
        egui::Vec2::INFINITY
    }

    fn clear_color(&self, _visuals: &egui::Visuals) -> egui::Rgba {
        // NOTE: a bright gray makes the shadows of the windows look weird.
        // We use a bit of transparency so that if the user switches on the
        // `transparent()` option they get immediate results.
        egui::Color32::from_rgba_unmultiplied(12, 12, 12, 180).into()

        // _visuals.window_fill() would also be a natural choice
    }

    fn persist_native_window(&self) -> bool {
        true
    }

    fn persist_egui_memory(&self) -> bool {
        true
    }

    fn warm_up_enabled(&self) -> bool {
        false
    }

    fn post_rendering(&mut self, _window_size_px: [u32; 2], _frame: &eframe::Frame) {}
}

0
投票

在我看来,一个更简单、更惯用的答案:

egui::ComboBox::from_label("Take your pick")
    .selected_text(self.selected_item.clone())
    .show_ui(ui, |ui| {
        for radio_item in self.ratio_items.iter() {
            ui.selectable_value(
              &mut self.selected_item,
              radio_item.clone(),
              &radio_item
            );
        }
    });

这是在以下背景下假设的

struct

struct MyForm {
  radio_items: Vec<String>,
  selected_item: String,
}

在这种情况下我们可以直接存储

String
,而不需要担心索引。

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.