Replies: 1 comment
-
hi, have you found a better way to achieve this? I am also troubled by this problem. I searched for a long time and only found this helpful discussion. We can use this tricky way to make the window fit its content when it starts
Here is the code permalink fn main() {
let app = AppGUI::default();
eframe::run_native(
"eframe window size fit content",
eframe::NativeOptions::default(),
Box::new(|_cc| Ok(Box::new(app))),
)
.unwrap();
}
struct AppGUI {
name: String,
age: u8,
content: String,
_first_render: bool,
}
impl Default for AppGUI {
fn default() -> Self {
Self {
name: "John".to_string(),
age: 20,
content: "This demo shows how the eframe window size fit content works.".to_string(),
_first_render: true,
}
}
}
impl AppGUI {
fn pre_render(&mut self, ctx: &eframe::egui::Context) {
egui::Window::new("pre_render")
.title_bar(false)
.fixed_pos((0.0, 0.0))
.show(ctx, |ui| {
self.render(ui);
});
}
fn render(&mut self, ui: &mut egui::Ui) {
ui.add(
egui::Label::new(egui::RichText::new("Hello, world!").heading())
.wrap_mode(egui::TextWrapMode::Extend),
);
ui.label(format!("Name: {}", self.name));
ui.label(format!("Age: {}", self.age));
ui.label(format!("{} ", self.content));
}
}
impl eframe::App for AppGUI {
fn update(&mut self, ctx: &eframe::egui::Context, _frame: &mut eframe::Frame) {
if self._first_render {
self.pre_render(ctx);
self._first_render = false;
let window_size = ctx.used_size();
ctx.send_viewport_cmd(egui::ViewportCommand::InnerSize(window_size));
} else {
egui::CentralPanel::default().show(ctx, |ui| {
self.render(ui);
});
}
}
} Not quite elegant, hope it helps you. |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Here's what I got:
It's not bad, and I like that it wasn't that hard to create, but there's a problem: that extra space at the bottom is driving me crazy, and it changes size on different screens and operating systems and so forth. In fact, on some systems, this overflows. I would love to figure out some way to set the window's initial size based on what's IN the window rather than just setting it by trial and error in a run/edit loop, you know? But I haven't figured out a working way to do that.
This is less of a problem horizontally because I can trust the width of these elements to scale to a degree, but I'll admit that I have a similar problem with X and Y both.
Edit: I just realized that, technically, this isn't a window. It's built around
CentralPanel
instead. I just tried usingWindow
, but that exhibits the same problem except that now there's a perfectly-sizedWindow
inside the (not-so-perfectly-sized) application window, which is what I was referring to before, colloquially, as a "window."Edit 2: It's really not essential that this be automatic. It would be fine to be able to know how much room the ui elements take up and then perform a resize by hand. I tried that at one point, actually, but the results weren't great. The size reported on the first frame is wrong; the size reported on the second frame appears to be correct, but by that time it seems to be too late to resize the window?
Edit the Third: I have now split the UI into two panels. With a footer panel at the bottom, it's definitely less obvious that there is "extra" space in the UI, and I have an element at the bottom of the central panel that can grow a little without looking too funny. This is a relatively satisfying result, albeit not completely, but I'd still like to know if there's some way to add up what the appropriate size for a given UI is. Also, yeah, if there's a smarter way to do this stuff, let me know!
Beta Was this translation helpful? Give feedback.
All reactions