egui_ash/
app.rs

1use ash::{
2    extensions::khr::{Surface, Swapchain},
3    vk, Device, Entry, Instance,
4};
5use egui_winit::winit;
6use std::ffi::CString;
7
8#[cfg(feature = "persistence")]
9use crate::storage;
10use crate::{
11    event,
12    renderer::{EguiCommand, ImageRegistry},
13    Allocator, ExitSignal,
14};
15
16/// egui theme type.
17#[derive(Debug, PartialEq, Eq, Clone, Copy)]
18pub enum Theme {
19    Light,
20    Dark,
21}
22
23/// redraw handler type.
24pub type RedrawHandler = Box<dyn FnOnce(winit::dpi::PhysicalSize<u32>, EguiCommand) + Send>;
25
26/// return type of [`App::request_redraw`].
27pub enum HandleRedraw {
28    Auto,
29    Handle(RedrawHandler),
30}
31
32/// main egui-ash app trait.
33pub trait App {
34    /// egui entry point
35    fn ui(&mut self, ctx: &egui::Context);
36
37    /// handle events of the app.
38    fn handle_event(&mut self, _event: event::Event) {}
39
40    /// redraw the app.
41    ///
42    /// If you want to draw only egui, return [`HandleRedraw::Auto`].
43    ///
44    /// If you want to do your own Vulkan drawing in ash,
45    /// return [`HandleRedraw::Handle(RedrawHandle)`] with FnOnce of drawing.
46    /// NOTE: You must call `egui_cmd.update_swapchain` inside render function
47    /// when you first render and when you recreate the swapchain.
48    fn request_redraw(&mut self, _viewport_id: egui::ViewportId) -> HandleRedraw {
49        HandleRedraw::Auto
50    }
51
52    /// time interval for call [`Self::save`].
53    #[cfg(feature = "persistence")]
54    fn auto_save_interval(&self) -> std::time::Duration {
55        std::time::Duration::from_secs(30)
56    }
57
58    /// save app state.
59    #[cfg(feature = "persistence")]
60    fn save(&mut self, _storage: &mut storage::Storage) {}
61}
62
63/// passed to [`AppCreator::create()`] for creating egui-ash app.
64pub struct CreationContext<'a> {
65    /// root window
66    pub main_window: &'a winit::window::Window,
67
68    /// egui context
69    pub context: egui::Context,
70
71    /// required instance extensions for ash vulkan
72    pub required_instance_extensions: Vec<CString>,
73
74    /// required device extensions for ash vulkan
75    pub required_device_extensions: Vec<CString>,
76
77    /// user texture image registry for egui-ash
78    pub image_registry: ImageRegistry,
79
80    /// exit signal sender
81    pub exit_signal: ExitSignal,
82}
83
84/// vulkan objects required for drawing ash.
85/// You should return this struct from [`AppCreator::create()`].
86pub struct AshRenderState<A: Allocator + 'static> {
87    pub entry: Entry,
88    pub instance: Instance,
89    pub physical_device: vk::PhysicalDevice,
90    pub device: Device,
91    pub surface_loader: Surface,
92    pub swapchain_loader: Swapchain,
93    pub queue: vk::Queue,
94    pub queue_family_index: u32,
95    pub command_pool: vk::CommandPool,
96    pub allocator: A,
97}
98
99/// egui-ash app creator trait.
100pub trait AppCreator<A: Allocator + 'static> {
101    type App: App;
102
103    /// create egui-ash app.
104    fn create(&self, cc: CreationContext) -> (Self::App, AshRenderState<A>);
105}