fixed_window.rs (1462B)
1 use egui::{Rect, Response, RichText, Sense, Window}; 2 3 #[derive(Default)] 4 pub struct FixedWindow { 5 title: Option<RichText>, 6 } 7 8 #[derive(PartialEq)] 9 pub enum FixedWindowResponse { 10 Opened, 11 Closed, 12 } 13 14 impl FixedWindow { 15 #[allow(dead_code)] 16 pub fn new() -> Self { 17 FixedWindow::default() 18 } 19 20 pub fn maybe_with_title(maybe_title: Option<RichText>) -> Self { 21 Self { title: maybe_title } 22 } 23 24 #[allow(dead_code)] 25 pub fn with_title(mut self, title: RichText) -> Self { 26 self.title = Some(title); 27 self 28 } 29 30 pub fn show( 31 self, 32 ui: &mut egui::Ui, 33 rect: Rect, 34 add_contents: impl FnOnce(&mut egui::Ui) -> Response, 35 ) -> FixedWindowResponse { 36 let mut is_open = true; 37 38 let use_title_bar = self.title.is_some(); 39 let title = if let Some(title) = self.title { 40 title 41 } else { 42 RichText::new("") 43 }; 44 45 Window::new(title) 46 .open(&mut is_open) 47 .fixed_rect(rect) 48 .collapsible(false) 49 .movable(false) 50 .resizable(false) 51 .title_bar(use_title_bar) 52 .show(ui.ctx(), |ui| { 53 let resp = add_contents(ui); 54 ui.allocate_rect(resp.rect, Sense::hover()) 55 }); 56 57 if !is_open { 58 FixedWindowResponse::Closed 59 } else { 60 FixedWindowResponse::Opened 61 } 62 } 63 }