functora-egui 0.4.2

shadcn/ui-inspired widget library for egui - buttons, inputs, selects, dialogs, and 70+ more components with light/dark theming
Documentation
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum BackOutcome {
    ConsumedNav,
    ConsumedNoop,
}

static CTX: std::sync::Mutex<Option<egui::Context>> = std::sync::Mutex::new(None);

pub fn store_context(ctx: &egui::Context) {
    let _ = CTX
        .lock()
        .unwrap_or_else(std::sync::PoisonError::into_inner)
        .replace(ctx.clone());
}

pub fn wake_via_repaint() {
    if let Some(ctx) = CTX
        .lock()
        .unwrap_or_else(std::sync::PoisonError::into_inner)
        .as_ref()
    {
        ctx.request_repaint();
    }
    #[cfg(all(target_os = "android", feature = "platform"))]
    crate::platform::android::wake_event_loop();
}

#[cfg(all(target_os = "android", feature = "platform"))]
#[unsafe(export_name = "Java_com_functora_Waker_wake")]
pub extern "system" fn java_com_functora_waker_wake(
    _env: jni::JNIEnv<'_>,
    _class: jni::objects::JClass<'_>,
) {
    wake_via_repaint();
}

fn system_back_pressed(ctx: &egui::Context) -> bool {
    let key_back = ctx.input(|i| i.key_pressed(egui::Key::BrowserBack));
    if key_back {
        ctx.input_mut(|i| {
            let _ = i.consume_key(egui::Modifiers::NONE, egui::Key::BrowserBack);
        });
        return true;
    }
    #[cfg(all(target_os = "android", feature = "platform"))]
    {
        if crate::platform::android::poll_back_pressed() {
            return true;
        }
    }
    false
}

#[must_use]
pub fn is_back_pressed(ctx: &egui::Context) -> bool {
    ctx.input(|i| i.key_pressed(egui::Key::BrowserBack)) || {
        #[cfg(all(target_os = "android", feature = "platform"))]
        {
            crate::platform::android::peek_back_pressed()
        }
        #[cfg(not(all(target_os = "android", feature = "platform")))]
        {
            false
        }
    }
}

pub fn handle_system_back(
    ctx: &egui::Context,
    can_go_back: bool,
    on_back: impl FnOnce(),
) -> Option<BackOutcome> {
    store_context(ctx);
    if !system_back_pressed(ctx) {
        return None;
    }
    let wants_keyboard = ctx.egui_wants_keyboard_input();
    if wants_keyboard {
        if let Some(id) = ctx.memory(egui::Memory::focused) {
            ctx.memory_mut(|m| m.surrender_focus(id));
        }
        #[cfg(all(target_os = "android", feature = "platform"))]
        {
            let _hide_outcome = crate::platform::android::hide_soft_input();
        }
        ctx.send_viewport_cmd(egui::ViewportCommand::CancelClose);
        ctx.request_repaint();
        return Some(BackOutcome::ConsumedNoop);
    }
    if can_go_back {
        on_back();
        ctx.send_viewport_cmd(egui::ViewportCommand::CancelClose);
        ctx.request_repaint();
        Some(BackOutcome::ConsumedNav)
    } else {
        None
    }
}