Replies: 2 comments 3 replies
-
I don't think this is the answer that you seek. But the best idea that i have to to do the layout manually. ui.horizontal(|ui| {
let previous_button_width = ui.data(|data| data.get_temp(id)).unwrap_or(egui::vec2(100.0, 0.0)).x;
let input_width = ui.available_width() - ui.spacing().item_spacing.x - previous_button_width;
ui.add(egui::TextEdit::singleline(&mut text).min_size(input_width * egui::Vec2::X));
let res = ui.button("Ping");
if res.clicked() {
// ...
}
ui.data_mut(|data| data.insert_temp(id, res.rect.size()));
}); |
Beta Was this translation helpful? Give feedback.
-
I came up with this, in case someone finds it easier: impl eframe::App for MyApp {
fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) {
egui::CentralPanel::default().show(ctx, |ui| {
ui.heading("Fill available space");
ui.add(egui::Slider::new(&mut self.ratio, 0.1..=0.9).text("ratio"));
let ratio = self.ratio;
ui.horizontal(|ui| {
ui.label("Your name: ");
let spacing_id = Id::new("name_input");
let previous: f32 = ui.memory(|mem| mem.data.get_temp(spacing_id).unwrap_or(0.0));
let available = ui.available_width() - previous;
ui.add(TextEdit::singleline(&mut self.name).desired_width(available * ratio));
let mut next = 0.0;
let cursor = ui.min_rect().right();
ui.label(format!("{} - label", self.name));
next += ui.min_rect().right() - cursor;
ui.add(
TextEdit::singleline(&mut self.name).desired_width(available * (1.0 - ratio)),
);
let cursor = ui.min_rect().right();
ui.button(format!("{} - button", self.name));
next += ui.min_rect().right() - cursor;
next += ui.spacing().item_spacing.x;
ui.memory_mut(|mem| mem.data.insert_temp(spacing_id, next));
});
});
}
} You only need to store one f32 in memory, corresponding to the total width taken by the fixed size widgets, which you can get by adding up the deltas of the layout cursor as you place them. You do have to add the item spacing at the end to account for the right margin. Then you subtract this width from the total available width, and split the remaining space between the variable size widgets. Screencast.from.2024-01-28.21-10-16.webm |
Beta Was this translation helpful? Give feedback.
-
So, I have this button that I want to be to the right of a text field:
It was really annoying to get it here, I had to switch the layout direction twice in order to place something on the right side but still have something on the left take up the remaining space:
The problem now is that the focus order is now incorrect. because the button is technically before the text area.
Is there any better way to do this that has correct focus order?
Beta Was this translation helpful? Give feedback.
All reactions