Skip to content

Commit

Permalink
Adapt window size to menubar height
Browse files Browse the repository at this point in the history
  • Loading branch information
vilhub committed May 31, 2022
1 parent 68672b7 commit 3ce7ba6
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 9 deletions.
37 changes: 29 additions & 8 deletions examples/minimal-egui/src/gui.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
use egui::{ClippedMesh, Context, TextureId, TexturesDelta, Vec2};
use egui_wgpu_backend::{BackendError, RenderPass, ScreenDescriptor, wgpu::TextureViewDescriptor};
use egui_wgpu_backend::{wgpu::TextureViewDescriptor, BackendError, RenderPass, ScreenDescriptor};
use pixels::{wgpu, PixelsContext};
use winit::window::Window;

use crate::{WIDTH, HEIGHT};

/// Manages all state required for rendering egui over `Pixels`.
pub(crate) struct Framework {
// State for egui.
Expand Down Expand Up @@ -41,8 +43,19 @@ impl Framework {
let textures = TexturesDelta::default();
let texture = pixels.texture();
let texture_view = texture.create_view(&TextureViewDescriptor::default());
let egui_texture = RenderPass::egui_texture_from_wgpu_texture(&mut rpass, pixels.device(), &texture_view, wgpu::FilterMode::Nearest);
let gui = Gui::new(egui_texture, Vec2 { x: width as f32 / scale_factor, y: height as f32 / scale_factor });
let egui_texture = RenderPass::egui_texture_from_wgpu_texture(
&mut rpass,
pixels.device(),
&texture_view,
wgpu::FilterMode::Nearest,
);
let gui = Gui::new(
egui_texture,
Vec2 {
x: WIDTH as f32,
y: HEIGHT as f32,
},
);

Self {
egui_ctx,
Expand Down Expand Up @@ -74,18 +87,20 @@ impl Framework {
}

/// Prepare egui.
pub(crate) fn prepare(&mut self, window: &Window) {
pub(crate) fn prepare(&mut self, window: &Window) -> f32 {
// Run the egui frame and create all paint jobs to prepare for rendering.
let raw_input = self.egui_state.take_egui_input(window);
let mut menubar_height: f32 = 0.;
let output = self.egui_ctx.run(raw_input, |egui_ctx| {
// Draw the demo application.
self.gui.ui(egui_ctx);
menubar_height = self.gui.ui(egui_ctx);
});

self.textures.append(output.textures_delta);
self.egui_state
.handle_platform_output(window, &self.egui_ctx, output.platform_output);
self.paint_jobs = self.egui_ctx.tessellate(output.shapes);
menubar_height
}

/// Render egui.
Expand Down Expand Up @@ -131,8 +146,8 @@ impl Gui {
}

/// Create the UI using egui.
fn ui(&mut self, ctx: &Context) {
egui::TopBottomPanel::top("menubar_container").show(ctx, |ui| {
fn ui(&mut self, ctx: &Context) -> f32 {
let inner_response = egui::TopBottomPanel::top("menubar_container").show(ctx, |ui| {
egui::menu::bar(ui, |ui| {
ui.menu_button("File", |ui| {
if ui.button("About...").clicked() {
Expand All @@ -142,8 +157,13 @@ impl Gui {
})
});
});
let menubar_height = inner_response.response.rect.height();

egui::CentralPanel::default().show(ctx, |ui| {
let frame = egui::Frame {
fill: ctx.style().visuals.window_fill(),
..egui::Frame::default()
};
egui::CentralPanel::default().frame(frame).show(ctx, |ui| {
ui.image(self.texture_id, self.texture_size);
});

Expand All @@ -161,5 +181,6 @@ impl Gui {
ui.hyperlink("https://docs.rs/egui");
});
});
menubar_height
}
}
13 changes: 12 additions & 1 deletion examples/minimal-egui/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ fn main() -> Result<(), Error> {
(pixels, framework)
};
let mut world = World::new();
let mut menubar_height = 0.;

event_loop.run(move |event, _, control_flow| {
// Handle input events
Expand Down Expand Up @@ -86,7 +87,17 @@ fn main() -> Result<(), Error> {
world.draw(pixels.get_frame());

// Prepare egui
framework.prepare(&window);
let new_menubar_height = framework.prepare(&window);
if (new_menubar_height - menubar_height).abs() > f32::EPSILON {
menubar_height = new_menubar_height;

// You should probably set your window size to account for the menubar height.
// In this example, we only adjust the minimum size, and allow the user to
// resize the window however they want.
let size = LogicalSize::new(WIDTH as f32, HEIGHT as f32 + menubar_height);
window.set_inner_size(size);
window.set_min_inner_size(Some(size));
}

// Render everything together
let render_result = pixels.render_with(|encoder, render_target, context| {
Expand Down

0 comments on commit 3ce7ba6

Please sign in to comment.