use crate::frame::app::App;
use crate::imports::*;
pub struct Core<T>
where
T: App,
{
app: Box<T>,
is_shutdown_pending: bool,
_settings_storage_requested: bool,
_last_settings_storage_request: Instant,
#[allow(dead_code)]
runtime: Runtime,
events: ApplicationEventsChannel,
}
impl<T> Core<T>
where
T: App,
{
pub fn try_new(
cc: &eframe::CreationContext<'_>,
runtime: Runtime,
app_creator: crate::frame::AppCreator<T>,
) -> Result<Self> {
let mut app = app_creator(cc, runtime.clone())?;
app.init(&runtime, cc);
let events = runtime.events().clone();
Ok(Self {
runtime,
app,
is_shutdown_pending: false,
_settings_storage_requested: false,
_last_settings_storage_request: Instant::now(),
events,
})
}
}
impl<T> eframe::App for Core<T>
where
T: App,
{
#[cfg(not(target_arch = "wasm32"))]
fn on_exit(&mut self, _gl: Option<&eframe::glow::Context>) {
self.is_shutdown_pending = true;
Runtime::halt();
println!("bye!");
}
fn clear_color(&self, _visuals: &egui::Visuals) -> [f32; 4] {
egui::Rgba::TRANSPARENT.to_array()
}
fn ui(&mut self, ui: &mut egui::Ui, frame: &mut eframe::Frame) {
let ctx = &ui.ctx().clone();
log_info!("--- update ---");
for event in self.events.iter() {
if let Err(err) = self.handle_events(event.clone(), ctx, frame) {
log_error!("error processing wallet runtime event: {}", err);
}
}
if self.is_shutdown_pending {
return;
}
ctx.input(|input| {
input.events.iter().for_each(|event| {
if let egui::Event::Key {
key,
pressed,
modifiers,
repeat,
physical_key: _,
} = event
{
self.handle_keyboard_events(*key, *pressed, modifiers, *repeat);
}
});
});
if let Some(device) = self.app.device() {
device.set_screen_size(&ctx.content_rect())
}
self.render(ctx, frame);
}
}
impl<T> Core<T>
where
T: App,
{
fn render(&mut self, ctx: &egui::Context, frame: &mut eframe::Frame) {
self.app.render(ctx, frame);
}
pub fn handle_events(
&mut self,
event: RuntimeEvent,
ctx: &egui::Context,
_frame: &mut eframe::Frame,
) -> Result<()> {
if matches!(event, RuntimeEvent::Exit) {
self.is_shutdown_pending = true;
}
self.app.handle_event(ctx, event);
Ok(())
}
fn handle_keyboard_events(
&mut self,
key: egui::Key,
pressed: bool,
modifiers: &egui::Modifiers,
_repeat: bool,
) {
self.app
.handle_keyboard_events(key, pressed, modifiers, false);
}
}