From 37c10c7c389c0b557dfffb5841ebf7f5be90df83 Mon Sep 17 00:00:00 2001 From: Ross Andrews Date: Sat, 16 May 2026 01:18:02 -0500 Subject: [PATCH] Size window to fit board including panel margin; center board on resize Accounts for egui's 8px CentralPanel margin in the minimum window size. When the window is larger than the minimum, the board is centered in the available area. Co-Authored-By: Claude Sonnet 4.6 --- src/game.rs | 7 +++++-- src/main.rs | 19 ++++++++++++++++--- 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/src/game.rs b/src/game.rs index 96e97f0..60498fb 100644 --- a/src/game.rs +++ b/src/game.rs @@ -1,5 +1,8 @@ use eframe::egui::Color32; +pub const BOARD_W: usize = 60; +pub const BOARD_H: usize = 25; + pub struct Glyph { pub ch: char, pub fg: Color32, @@ -64,8 +67,8 @@ pub struct GameState { impl GameState { pub fn new() -> Self { - let w = 60; - let h = 25; + let w = BOARD_W; + let h = BOARD_H; let mut board = Board { width: w, diff --git a/src/main.rs b/src/main.rs index ff38176..d87e073 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,13 +2,23 @@ use eframe::egui; use egui::{Align2, FontId, Key, Pos2, Rect, vec2}; mod game; -use game::{GameState, Glyph}; +use game::{GameState, Glyph, BOARD_W, BOARD_H}; const CELL_W: f32 = 14.0; const CELL_H: f32 = 20.0; +const MENU_H: f32 = 24.0; +const PANEL_MARGIN: f32 = 8.0; + fn main() -> eframe::Result<()> { - let options = eframe::NativeOptions::default(); + let board_px_w = BOARD_W as f32 * CELL_W + 2.0 * PANEL_MARGIN; + let board_px_h = BOARD_H as f32 * CELL_H + MENU_H + 2.0 * PANEL_MARGIN; + let options = eframe::NativeOptions { + viewport: egui::ViewportBuilder::default() + .with_inner_size([board_px_w, board_px_h]) + .with_min_inner_size([board_px_w, board_px_h]), + ..Default::default() + }; eframe::run_native( "Viberogue", options, @@ -47,7 +57,10 @@ impl eframe::App for App { egui::CentralPanel::default().show(ctx, |ui| { let board = &self.state.board; - let origin = ui.available_rect_before_wrap().min; + let available = ui.available_rect_before_wrap(); + let board_px = vec2(board.width as f32 * CELL_W, board.height as f32 * CELL_H); + let offset = ((available.size() - board_px) / 2.0).max(egui::Vec2::ZERO); + let origin = available.min + offset; let painter = ui.painter(); for y in 0..board.height {