#![warn(missing_docs)]
#![deny(unsafe_code)]
#![doc(html_logo_url = "https://sixtyfps.io/resources/logo.drawio.svg")]
#![cfg_attr(not(feature = "std"), no_std)]
extern crate alloc;
pub use sixtyfps_macros::sixtyfps;
pub use sixtyfps_corelib::graphics::{
Brush, Color, Image, LoadImageError, Rgb8Pixel, Rgba8Pixel, RgbaColor, SharedPixelBuffer,
};
pub use sixtyfps_corelib::model::{
Model, ModelHandle, ModelNotify, ModelPeer, ModelTracker, StandardListViewItem, VecModel,
};
pub use sixtyfps_corelib::sharedvector::SharedVector;
pub use sixtyfps_corelib::string::SharedString;
pub use sixtyfps_corelib::timers::{Timer, TimerMode};
#[doc(hidden)]
#[cfg(feature = "std")]
pub fn register_font_from_memory(data: &'static [u8]) -> Result<(), Box<dyn std::error::Error>> {
sixtyfps_rendering_backend_default::backend().register_font_from_memory(data)
}
#[doc(hidden)]
#[cfg(feature = "std")]
pub fn register_font_from_path<P: AsRef<std::path::Path>>(
path: P,
) -> Result<(), Box<dyn std::error::Error>> {
sixtyfps_rendering_backend_default::backend().register_font_from_path(path.as_ref())
}
#[doc(hidden)]
pub mod re_exports {
pub use alloc::boxed::Box;
pub use alloc::format;
pub use alloc::rc::{Rc, Weak};
pub use alloc::string::String;
pub use alloc::{vec, vec::Vec};
pub use const_field_offset::{self, FieldOffsets, PinnedDrop};
pub use core::iter::FromIterator;
pub use once_cell::race::OnceBox;
pub use once_cell::unsync::OnceCell;
pub use pin_weak::rc::PinWeak;
pub use sixtyfps_corelib::animations::EasingCurve;
pub use sixtyfps_corelib::callbacks::Callback;
pub use sixtyfps_corelib::component::{
free_component_item_graphics_resources, init_component_items, Component, ComponentRefPin,
ComponentVTable,
};
pub use sixtyfps_corelib::graphics::*;
pub use sixtyfps_corelib::input::{
FocusEvent, InputEventResult, KeyEvent, KeyEventResult, KeyboardModifiers, MouseEvent,
};
pub use sixtyfps_corelib::item_tree::{
visit_item_tree, ItemTreeNode, ItemVisitorRefMut, ItemVisitorVTable, TraversalOrder,
VisitChildrenResult,
};
pub use sixtyfps_corelib::items::*;
pub use sixtyfps_corelib::layout::*;
pub use sixtyfps_corelib::model::*;
pub use sixtyfps_corelib::properties::{
set_state_binding, Property, PropertyTracker, StateInfo,
};
pub use sixtyfps_corelib::slice::Slice;
pub use sixtyfps_corelib::window::{Window, WindowHandleAccess, WindowRc};
pub use sixtyfps_corelib::Color;
pub use sixtyfps_corelib::ComponentVTable_static;
pub use sixtyfps_corelib::SharedString;
pub use sixtyfps_corelib::SharedVector;
pub use sixtyfps_rendering_backend_default::native_widgets::*;
pub use vtable::{self, *};
}
#[doc(hidden)]
pub mod internal {
use crate::re_exports::*;
use alloc::rc::Rc;
use core::pin::Pin;
pub trait StrongComponentRef: Sized {
type Weak: Clone + 'static;
fn to_weak(&self) -> Self::Weak;
fn from_weak(weak: &Self::Weak) -> Option<Self>;
}
impl<C: 'static> StrongComponentRef for VRc<ComponentVTable, C> {
type Weak = VWeak<ComponentVTable, C>;
fn to_weak(&self) -> Self::Weak {
VRc::downgrade(self)
}
fn from_weak(weak: &Self::Weak) -> Option<Self> {
weak.upgrade()
}
}
impl<C: 'static> StrongComponentRef for VRcMapped<ComponentVTable, C> {
type Weak = VWeakMapped<ComponentVTable, C>;
fn to_weak(&self) -> Self::Weak {
VRcMapped::downgrade(self)
}
fn from_weak(weak: &Self::Weak) -> Option<Self> {
weak.upgrade()
}
}
impl<C: 'static> StrongComponentRef for Pin<Rc<C>> {
type Weak = PinWeak<C>;
fn to_weak(&self) -> Self::Weak {
PinWeak::downgrade(self.clone())
}
fn from_weak(weak: &Self::Weak) -> Option<Self> {
weak.upgrade()
}
}
pub fn set_property_binding<T: Clone + 'static, StrongRef: StrongComponentRef + 'static>(
property: Pin<&Property<T>>,
component_strong: &StrongRef,
binding: fn(StrongRef) -> T,
) {
let weak = component_strong.to_weak();
property.set_binding(move || {
binding(<StrongRef as StrongComponentRef>::from_weak(&weak).unwrap())
})
}
pub fn set_animated_property_binding<
T: Clone + sixtyfps_corelib::properties::InterpolatedPropertyValue + 'static,
StrongRef: StrongComponentRef + 'static,
>(
property: Pin<&Property<T>>,
component_strong: &StrongRef,
binding: fn(StrongRef) -> T,
animation_data: PropertyAnimation,
) {
let weak = component_strong.to_weak();
property.set_animated_binding(
move || binding(<StrongRef as StrongComponentRef>::from_weak(&weak).unwrap()),
animation_data,
)
}
pub fn set_animated_property_binding_for_transition<
T: Clone + sixtyfps_corelib::properties::InterpolatedPropertyValue + 'static,
StrongRef: StrongComponentRef + 'static,
>(
property: Pin<&Property<T>>,
component_strong: &StrongRef,
binding: fn(StrongRef) -> T,
compute_animation_details: fn(
StrongRef,
)
-> (PropertyAnimation, sixtyfps_corelib::animations::Instant),
) {
let weak_1 = component_strong.to_weak();
let weak_2 = weak_1.clone();
property.set_animated_binding_for_transition(
move || binding(<StrongRef as StrongComponentRef>::from_weak(&weak_1).unwrap()),
move || {
compute_animation_details(
<StrongRef as StrongComponentRef>::from_weak(&weak_2).unwrap(),
)
},
)
}
pub fn set_property_state_binding<StrongRef: StrongComponentRef + 'static>(
property: Pin<&Property<StateInfo>>,
component_strong: &StrongRef,
binding: fn(StrongRef) -> i32,
) {
let weak = component_strong.to_weak();
crate::re_exports::set_state_binding(property, move || {
binding(<StrongRef as StrongComponentRef>::from_weak(&weak).unwrap())
})
}
pub fn set_callback_handler<
Arg: ?Sized + 'static,
Ret: Default + 'static,
StrongRef: StrongComponentRef + 'static,
>(
callback: Pin<&Callback<Arg, Ret>>,
component_strong: &StrongRef,
handler: fn(StrongRef, &Arg) -> Ret,
) {
let weak = component_strong.to_weak();
callback.set_handler(move |arg| {
handler(<StrongRef as StrongComponentRef>::from_weak(&weak).unwrap(), arg)
})
}
}
#[doc(hidden)]
pub fn create_window() -> re_exports::WindowRc {
sixtyfps_rendering_backend_default::backend().create_window()
}
pub fn run_event_loop() {
sixtyfps_rendering_backend_default::backend()
.run_event_loop(sixtyfps_corelib::backend::EventLoopQuitBehavior::QuitOnLastWindowClosed);
}
pub fn quit_event_loop() {
sixtyfps_rendering_backend_default::backend().quit_event_loop();
}
pub fn invoke_from_event_loop(func: impl FnOnce() + Send + 'static) {
sixtyfps_rendering_backend_default::backend().post_event(alloc::boxed::Box::new(func))
}
pub trait Global<'a, Component> {
fn get(component: &'a Component) -> Self;
}
pub trait ComponentHandle {
#[doc(hidden)]
type Inner;
fn as_weak(&self) -> Weak<Self>
where
Self: Sized;
#[must_use]
fn clone_strong(&self) -> Self;
#[doc(hidden)]
fn from_inner(_: vtable::VRc<re_exports::ComponentVTable, Self::Inner>) -> Self;
fn show(&self);
fn hide(&self);
fn window(&self) -> &Window;
fn run(&self);
fn global<'a, T: Global<'a, Self>>(&'a self) -> T
where
Self: Sized;
}
mod weak_handle {
use super::*;
pub struct Weak<T: ComponentHandle> {
inner: vtable::VWeak<re_exports::ComponentVTable, T::Inner>,
#[cfg(feature = "std")]
thread: std::thread::ThreadId,
}
impl<T: ComponentHandle> Clone for Weak<T> {
fn clone(&self) -> Self {
Self {
inner: self.inner.clone(),
#[cfg(feature = "std")]
thread: self.thread,
}
}
}
impl<T: ComponentHandle> Weak<T> {
#[doc(hidden)]
pub fn new(rc: &vtable::VRc<re_exports::ComponentVTable, T::Inner>) -> Self {
Self {
inner: vtable::VRc::downgrade(rc),
#[cfg(feature = "std")]
thread: std::thread::current().id(),
}
}
pub fn upgrade(&self) -> Option<T>
where
T: ComponentHandle,
{
#[cfg(feature = "std")]
if std::thread::current().id() != self.thread {
return None;
}
self.inner.upgrade().map(T::from_inner)
}
pub fn unwrap(&self) -> T {
self.upgrade().unwrap()
}
#[cfg(feature = "std")]
pub fn upgrade_in_event_loop(self, func: impl FnOnce(T) + Send + 'static)
where
T: 'static,
{
crate::invoke_from_event_loop(move || {
if let Some(h) = self.upgrade() {
func(h);
}
})
}
}
#[allow(unsafe_code)]
#[cfg(feature = "std")]
unsafe impl<T: ComponentHandle> Send for Weak<T> {}
}
pub use weak_handle::*;
pub use sixtyfps_corelib::window::api::Window;
#[cfg(feature = "std")]
pub mod testing {
use core::cell::Cell;
thread_local!(static KEYBOARD_MODIFIERS : Cell<crate::re_exports::KeyboardModifiers> = Default::default());
use super::ComponentHandle;
pub use sixtyfps_corelib::tests::sixtyfps_mock_elapsed_time as mock_elapsed_time;
pub fn send_mouse_click<
X: vtable::HasStaticVTable<sixtyfps_corelib::component::ComponentVTable>
+ crate::re_exports::WindowHandleAccess
+ 'static,
Component: Into<vtable::VRc<sixtyfps_corelib::component::ComponentVTable, X>> + ComponentHandle,
>(
component: &Component,
x: f32,
y: f32,
) {
let rc = component.clone_strong().into();
let dyn_rc = vtable::VRc::into_dyn(rc.clone());
sixtyfps_corelib::tests::sixtyfps_send_mouse_click(
&dyn_rc,
x,
y,
&rc.window_handle().clone(),
);
}
pub fn set_current_keyboard_modifiers<
X: vtable::HasStaticVTable<sixtyfps_corelib::component::ComponentVTable>
+ crate::re_exports::WindowHandleAccess,
Component: Into<vtable::VRc<sixtyfps_corelib::component::ComponentVTable, X>> + ComponentHandle,
>(
_component: &Component,
modifiers: crate::re_exports::KeyboardModifiers,
) {
KEYBOARD_MODIFIERS.with(|x| x.set(modifiers))
}
pub fn send_keyboard_string_sequence<
X: vtable::HasStaticVTable<sixtyfps_corelib::component::ComponentVTable>
+ crate::re_exports::WindowHandleAccess,
Component: Into<vtable::VRc<sixtyfps_corelib::component::ComponentVTable, X>> + ComponentHandle,
>(
component: &Component,
sequence: &str,
) {
let component = component.clone_strong().into();
sixtyfps_corelib::tests::send_keyboard_string_sequence(
&super::SharedString::from(sequence),
KEYBOARD_MODIFIERS.with(|x| x.get()),
&component.window_handle().clone(),
)
}
pub fn set_window_scale_factor<
X: vtable::HasStaticVTable<sixtyfps_corelib::component::ComponentVTable>
+ crate::re_exports::WindowHandleAccess,
Component: Into<vtable::VRc<sixtyfps_corelib::component::ComponentVTable, X>> + ComponentHandle,
>(
component: &Component,
factor: f32,
) {
let component = component.clone_strong().into();
component.window_handle().set_scale_factor(factor)
}
}
#[macro_export]
macro_rules! include_modules {
() => {
include!(env!("SIXTYFPS_INCLUDE_GENERATED"));
};
}
#[doc(hidden)]
#[allow(non_camel_case_types)]
pub struct VersionCheck_0_1_6;
#[cfg(doctest)]
mod compile_fail_tests;
#[cfg(doc)]
pub mod docs;