Try to open a Combobox inside a Popup, it closes along with the Popup #7628
Replies: 1 comment
-
|
I think this may be a bug in WorkaroundsBefore getting into the workarounds I want to mention that you're resetting the combo box value every time update is called, so the value selected will not be reflected when the popup is opened again; this is addressed in the final workaround but can be applied to all of them. v0.31.1If you're willing to downgrade to 0.31.1 then the only change needed is: - egui::Popup::context_menu(&response).show(|ui| {
+ response.context_menu(|ui| {This does not work for 0.32+ as the underlying implementation was changed to use Simple manualThe "fine, I'll do it myself" context menustruct MyEguiApp {
context: bool,
pointer: egui::Pos2,
}
impl MyEguiApp {
fn new() -> Self {
MyEguiApp {
context: false,
pointer: [0., 0.].into(),
}
}
}
impl eframe::App for MyEguiApp {
fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) {
egui::CentralPanel::default().show(ctx, |ui| {
let back_ground_size = ui.available_size();
let response = ui.allocate_response(back_ground_size, Sense::click());
let mut value = 0;
if response.secondary_clicked() {
self.context = true;
self.pointer = response.interact_pointer_pos().expect("clicks have positions");
}
if let Some(inner_response) = egui::Popup::from_response(&response)
.open_bool(&mut self.context)
.at_position(self.pointer)
.close_behavior(egui::PopupCloseBehavior::CloseOnClickOutside)
.show(|ui| {
egui::ComboBox::from_label("value")
.selected_text(format!("{value}"))
.show_ui(ui, |ui| {
for i in 0..10u32 {
ui.selectable_value(&mut value, i, format!("{i}"));
}
});
})
&& inner_response.response.should_close()
{
self.context = false;
}
});
}
}We have to use a positioned popup as the pointer-fixed popup does not work (I'm guessing it's opening outside the viewport). The behaviour of right-clicks outside an open popup is not quite the same as the real context menu, as whether the popup just closes or reopens in the new click position seems to depend on the interval between press and release. Fully controlledI'm not sure exactly what you were expecting from the context menu, but if you want the popup to remain open after selecting a value, that is also possible. Closing the popup is accomplished by clicking outside after the combo box is closed. The "let me decide" context menustruct MyEguiApp {
value: u32,
was_closed: bool,
context: bool,
pointer: egui::Pos2,
}
impl MyEguiApp {
fn new() -> Self {
MyEguiApp {
value: 0,
was_closed: false,
context: false,
pointer: [0., 0.].into(),
}
}
}
impl eframe::App for MyEguiApp {
fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) {
egui::CentralPanel::default().show(ctx, |ui| {
let back_ground_size = ui.available_size();
let response = ui.allocate_response(back_ground_size, Sense::click());
self.was_closed = !self.context;
if response.secondary_clicked() {
self.context = true;
self.pointer = response.interact_pointer_pos().expect("clicks have positions");
}
if let Some(inner_response) = egui::Popup::from_response(&response)
.open_bool(&mut self.context)
.at_position(self.pointer)
.close_behavior(egui::PopupCloseBehavior::IgnoreClicks)
.show(|ui| {
let inner_response = egui::ComboBox::from_label("value")
.selected_text(format!("{}", self.value))
.show_ui(ui, |ui| {
let mut everywhere = true;
for i in 0..10u32 {
everywhere &= ui
.selectable_value(&mut self.value, i, format!("{i}"))
.clicked_elsewhere();
}
everywhere
});
!self.was_closed
&& inner_response.response.clicked_elsewhere()
&& (inner_response.inner.is_none() || inner_response.inner.unwrap())
})
&& inner_response.inner
&& inner_response.response.clicked_elsewhere()
{
self.context = false;
}
});
}
}Wrapping upI may have missed some "right way" to get the context menu working, but it would certainly be non-obvious. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
I'm using egui 0.32 to place a Combobox inside a Popup, and when I run the program and try to open the Combobox, it immediately closes along with the Popup.
Is there a way to avoid this?
Below is some rust code that can be reproduced using an eframe.
Beta Was this translation helpful? Give feedback.
All reactions