use crate::{
Application, CommandController, CommandCtx, CommandKey, EmbeddedSvgImageResource, FontHandle,
ImageHandle, RegisteredFont, RegisteredImage, Result, Runtime, UrlPolicy, Widget,
WindowBuilder, WindowIcon, WindowRenderOptions,
};
#[cfg(any(feature = "desktop", feature = "web", feature = "mobile"))]
use crate::{CommandSender, CommandTarget};
#[cfg(all(target_os = "android", feature = "mobile"))]
use crate::AndroidApp;
pub struct App {
application: Application,
}
impl Default for App {
fn default() -> Self {
Self {
application: Application::new(),
}
}
}
impl App {
pub fn new() -> Self {
Self::default()
}
pub fn window(mut self, window: Window) -> Self {
self.application = self.application.window(window.into_window_builder());
self
}
pub fn controller(mut self, controller: impl CommandController + 'static) -> Self {
self.application = self.application.controller(controller);
self
}
pub fn url_policy(mut self, policy: UrlPolicy) -> Self {
self.application = self.application.url_policy(policy);
self
}
pub fn on_command<T, F>(mut self, key: CommandKey<T>, handler: F) -> Self
where
T: Send + Sync + 'static,
F: FnMut(&mut CommandCtx, &T) + 'static,
{
self.application = self.application.on_command(key, handler);
self
}
pub fn main_window<W>(self, title: impl Into<String>, root: W) -> Self
where
W: Widget + 'static,
{
self.window(Window::new(title).root(root))
}
pub fn resources(&mut self) -> ResourceRegistry<'_> {
ResourceRegistry {
application: &mut self.application,
}
}
pub fn with_resources(
mut self,
configure: impl FnOnce(&mut ResourceRegistry<'_>) -> Result<()>,
) -> Result<Self> {
configure(&mut self.resources())?;
Ok(self)
}
pub fn render_options(mut self, options: WindowRenderOptions) -> Self {
self.application = self.application.with_window_render_options(options);
self
}
#[cfg(feature = "wgpu")]
pub fn feathering(mut self, enabled: bool) -> Self {
self.application = self.application.with_feathering_enabled(enabled);
self
}
#[cfg(feature = "wgpu")]
pub fn feather_width(mut self, width: f32) -> Self {
self.application = self.application.with_feather_width(width);
self
}
#[cfg(feature = "wgpu")]
pub fn external_texture_registry(
mut self,
registry: crate::WgpuExternalTextureRegistry,
) -> Self {
self.application = self.application.with_external_texture_registry(registry);
self
}
#[cfg(feature = "wgpu")]
pub fn configured_external_texture_registry(
&self,
) -> Option<&crate::WgpuExternalTextureRegistry> {
self.application.external_texture_registry()
}
pub fn build(self) -> Result<Runtime> {
self.application.build()
}
#[cfg(any(feature = "desktop", feature = "web"))]
pub fn run(self) -> Result<()> {
self.application.run()
}
#[cfg(any(feature = "desktop", feature = "web"))]
pub fn run_with_platform(self, platform: crate::DesktopPlatform) -> Result<()> {
self.application.run_with_platform(platform)
}
#[cfg(any(feature = "desktop", feature = "web"))]
pub fn run_with_handle(self, on_ready: impl FnOnce(UiHandle)) -> Result<()> {
self.application
.run_with(|commands| on_ready(UiHandle::new(commands)))
}
#[cfg(all(
feature = "desktop",
any(target_os = "windows", target_os = "macos", target_os = "linux")
))]
pub fn start_pumped_with_handle(
self,
on_ready: impl FnOnce(UiHandle),
) -> Result<crate::PumpedDesktop> {
self.application
.start_pumped_with(|commands| on_ready(UiHandle::new(commands)))
}
#[cfg(all(feature = "web", target_arch = "wasm32"))]
pub fn start_web_with_handle(
self,
canvas: Option<crate::WebCanvas>,
on_ready: impl FnOnce(UiHandle),
on_exit: impl FnOnce(Result<()>) + 'static,
) -> Result<()> {
self.application.start_web_with(
canvas,
|commands| on_ready(UiHandle::new(commands)),
on_exit,
)
}
#[cfg(any(feature = "desktop", feature = "web"))]
pub fn run_with_platform_and_handle(
self,
platform: crate::DesktopPlatform,
on_ready: impl FnOnce(UiHandle),
) -> Result<()> {
self.application
.run_with_platform_and_handle(platform, |commands| {
on_ready(UiHandle::new(commands));
})
}
#[cfg(all(target_os = "android", feature = "mobile"))]
pub fn run_android(self, android_app: AndroidApp) -> Result<()> {
self.application.run_android(android_app)
}
#[cfg(all(target_os = "android", feature = "mobile"))]
pub fn run_android_with_handle(
self,
android_app: AndroidApp,
on_ready: impl FnOnce(UiHandle),
) -> Result<()> {
self.application
.run_android_with(android_app, |commands| on_ready(UiHandle::new(commands)))
}
pub fn into_application(self) -> Application {
self.application
}
}
impl From<App> for Application {
fn from(app: App) -> Self {
app.into_application()
}
}
pub struct Window {
builder: WindowBuilder,
}
impl Window {
pub fn new(title: impl Into<String>) -> Self {
Self {
builder: WindowBuilder::new().title(title),
}
}
pub fn root<W>(mut self, root: W) -> Self
where
W: Widget + 'static,
{
self.builder = self.builder.root(root);
self
}
pub fn controller(mut self, controller: impl CommandController + 'static) -> Self {
self.builder = self.builder.controller(controller);
self
}
pub fn on_command<T, F>(mut self, key: CommandKey<T>, handler: F) -> Self
where
T: Send + Sync + 'static,
F: FnMut(&mut CommandCtx, &T) + 'static,
{
self.builder = self.builder.on_command(key, handler);
self
}
pub fn icon(mut self, icon: WindowIcon) -> Self {
self.builder = self.builder.icon(icon);
self
}
pub fn icon_svg(mut self, svg: impl Into<Vec<u8>>) -> Self {
self.builder = self.builder.icon_svg(svg);
self
}
pub fn without_icon(mut self) -> Self {
self.builder = self.builder.without_icon();
self
}
pub fn initial_size(mut self, size: crate::Size) -> Self {
self.builder = self.builder.initial_size(size);
self
}
pub fn initial_position(mut self, position: crate::Point) -> Self {
self.builder = self.builder.initial_position(position);
self
}
pub(crate) fn into_window_builder(self) -> WindowBuilder {
self.builder
}
}
pub struct ResourceRegistry<'a> {
application: &'a mut Application,
}
impl ResourceRegistry<'_> {
pub fn register_font(&mut self, handle: FontHandle, font: RegisteredFont) -> Result<()> {
self.application.register_font(handle, font)
}
pub fn font_bytes(&mut self, data: impl Into<Vec<u8>>) -> Result<FontHandle> {
self.application.register_font_bytes(data)
}
pub fn image(&mut self, handle: ImageHandle, image: RegisteredImage) -> Result<()> {
self.application.register_image(handle, image)
}
pub fn rgba_image(
&mut self,
width: u32,
height: u32,
data: impl Into<Vec<u8>>,
) -> Result<ImageHandle> {
self.application.register_rgba_image(width, height, data)
}
pub fn svg_image(&mut self, data: impl AsRef<[u8]>) -> Result<ImageHandle> {
self.application.register_svg_image(data)
}
pub fn svg_image_with_handle(
&mut self,
handle: ImageHandle,
data: impl AsRef<[u8]>,
) -> Result<()> {
self.application
.register_svg_image_with_handle(handle, data)
}
pub fn svg_image_at_size(
&mut self,
width: u32,
height: u32,
data: impl AsRef<[u8]>,
) -> Result<ImageHandle> {
self.application
.register_svg_image_at_size(width, height, data)
}
pub fn svg_image_at_size_with_handle(
&mut self,
handle: ImageHandle,
width: u32,
height: u32,
data: impl AsRef<[u8]>,
) -> Result<()> {
self.application
.register_svg_image_at_size_with_handle(handle, width, height, data)
}
pub fn embedded_svg_image(&mut self, resource: EmbeddedSvgImageResource) -> Result<()> {
self.application.register_embedded_svg_image(resource)
}
pub fn embedded_svg_images(
&mut self,
resources: impl IntoIterator<Item = EmbeddedSvgImageResource>,
) -> Result<()> {
self.application.register_embedded_svg_images(resources)
}
}
#[cfg(any(feature = "desktop", feature = "web", feature = "mobile"))]
#[derive(Clone)]
pub struct UiHandle {
commands: CommandSender,
}
#[cfg(any(feature = "desktop", feature = "web", feature = "mobile"))]
impl UiHandle {
#[cfg_attr(
not(any(
feature = "desktop",
feature = "web",
all(target_os = "android", feature = "mobile")
)),
allow(dead_code)
)]
fn new(commands: CommandSender) -> Self {
Self { commands }
}
pub fn wake(&self) {
self.commands.wake();
}
pub fn request_exit(&self) {
self.commands.request_exit();
}
pub fn send<T>(&self, target: CommandTarget, key: CommandKey<T>, payload: T) -> u64
where
T: Send + Sync + 'static,
{
self.commands.send(target, key, payload)
}
pub fn send_widget<T>(
&self,
window_id: crate::WindowId,
widget_id: crate::WidgetId,
key: CommandKey<T>,
payload: T,
) -> u64
where
T: Send + Sync + 'static,
{
self.commands
.send_widget(window_id, widget_id, key, payload)
}
pub fn send_focused<T>(&self, window_id: crate::WindowId, key: CommandKey<T>, payload: T) -> u64
where
T: Send + Sync + 'static,
{
self.commands.send_focused(window_id, key, payload)
}
pub fn send_window<T>(&self, window_id: crate::WindowId, key: CommandKey<T>, payload: T) -> u64
where
T: Send + Sync + 'static,
{
self.commands.send_window(window_id, key, payload)
}
pub fn send_application<T>(&self, key: CommandKey<T>, payload: T) -> u64
where
T: Send + Sync + 'static,
{
self.commands.send_application(key, payload)
}
pub fn broadcast_window<T>(
&self,
window_id: crate::WindowId,
key: CommandKey<T>,
payload: T,
) -> u64
where
T: Send + Sync + 'static,
{
self.commands.broadcast_window(window_id, key, payload)
}
pub fn broadcast_application<T>(&self, key: CommandKey<T>, payload: T) -> u64
where
T: Send + Sync + 'static,
{
self.commands.broadcast_application(key, payload)
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::{Constraints, MeasureCtx, Size};
struct TestWidget;
impl Widget for TestWidget {
fn measure(&mut self, _ctx: &mut MeasureCtx, _constraints: Constraints) -> Size {
Size::new(32.0, 24.0)
}
}
#[test]
#[cfg(feature = "wgpu")]
fn application_uses_plain_renderer_configuration_defaults() {
let app = Application::new();
let options = sui_render_wgpu::FeatheringOptions::default();
assert_eq!(app.feathering_enabled(), options.enabled);
assert_eq!(app.feather_width(), options.width);
}
#[test]
fn app_builds_window_after_registering_resources() -> Result<()> {
let mut app = App::new();
let image = {
let mut resources = app.resources();
resources.rgba_image(1, 1, vec![255, 0, 0, 255])?
};
let runtime = app
.main_window(format!("Image {image:?}"), TestWidget)
.build()?;
assert_eq!(runtime.window_ids().len(), 1);
Ok(())
}
#[test]
fn app_with_resources_preserves_builder_style() -> Result<()> {
let runtime = App::new()
.with_resources(|resources| {
resources.rgba_image(1, 1, vec![0, 255, 0, 255])?;
Ok(())
})?
.window(Window::new("Resource setup").root(TestWidget))
.build()?;
assert_eq!(runtime.window_ids().len(), 1);
Ok(())
}
}