slint/sixtyfps_runtime/corelib/backend.rs
Simon Hausmann a951e75fdc Move the backend pointer/cell to the corelib
This way we can add window-independent functions to Backend and use it
from items.
2021-01-22 15:24:26 +01:00

47 lines
1.7 KiB
Rust

/* LICENSE BEGIN
This file is part of the SixtyFPS Project -- https://sixtyfps.io
Copyright (c) 2020 Olivier Goffart <olivier.goffart@sixtyfps.io>
Copyright (c) 2020 Simon Hausmann <simon.hausmann@sixtyfps.io>
SPDX-License-Identifier: GPL-3.0-only
This file is also available under commercial licensing terms.
Please contact info@sixtyfps.io for more information.
LICENSE END */
/*!
The backend is the abstraction for crates that need to do the actual drawing and event loop
*/
use crate::window::ComponentWindow;
/// Interface implemented by backends
pub trait Backend: Send + Sync {
/// Instentiate a window for a component.
/// FIXME: should return a Box<dyn PlatformWindow>
fn create_window(&'static self) -> ComponentWindow;
/// Spins an event loop and renders the items of the provided component in this window.
fn run_event_loop(&'static self);
/// This function can be used to register a custom TrueType font with SixtyFPS,
/// for use with the `font-family` property. The provided slice must be a valid TrueType
/// font.
fn register_application_font_from_memory(
&'static self,
data: &'static [u8],
) -> Result<(), Box<dyn std::error::Error>>;
}
static PRIVATE_BACKEND_INSTANCE: once_cell::sync::OnceCell<Box<dyn Backend + 'static>> =
once_cell::sync::OnceCell::new();
pub fn instance() -> Option<&'static dyn Backend> {
use std::ops::Deref;
PRIVATE_BACKEND_INSTANCE.get().map(|backend_box| backend_box.deref())
}
pub fn instance_or_init(
factory_fn: impl FnOnce() -> Box<dyn Backend + 'static>,
) -> &'static dyn Backend {
use std::ops::Deref;
PRIVATE_BACKEND_INSTANCE.get_or_init(factory_fn).deref()
}