1// Copyright © SixtyFPS GmbH <info@slint.dev>
2// SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-Slint-Royalty-free-1.1 OR LicenseRef-Slint-commercial
3
4//! Module containing the private api that is used by the generated code.
5//!
6//! This is internal API that shouldn't be used because compatibility is not
7//! guaranteed
8#![doc(hidden)]
9
10use core::pin::Pin;
11use re_exports::*;
12
13// Helper functions called from generated code to reduce code bloat from
14// extra copies of the original functions for each call site due to
15// the impl Fn() they are taking.
16
17pub trait StrongItemTreeRef: Sized {
18 type Weak: Clone + 'static;
19 fn to_weak(&self) -> Self::Weak;
20 fn from_weak(weak: &Self::Weak) -> Option<Self>;
21}
22
23impl<C: 'static> StrongItemTreeRef for VRc<ItemTreeVTable, C> {
24 type Weak = VWeak<ItemTreeVTable, C>;
25 fn to_weak(&self) -> Self::Weak {
26 VRc::downgrade(self)
27 }
28 fn from_weak(weak: &Self::Weak) -> Option<Self> {
29 weak.upgrade()
30 }
31}
32
33impl<C: 'static> StrongItemTreeRef for VRcMapped<ItemTreeVTable, C> {
34 type Weak = VWeakMapped<ItemTreeVTable, C>;
35 fn to_weak(&self) -> Self::Weak {
36 VRcMapped::downgrade(self)
37 }
38 fn from_weak(weak: &Self::Weak) -> Option<Self> {
39 weak.upgrade()
40 }
41}
42
43impl<C: 'static> StrongItemTreeRef for Pin<Rc<C>> {
44 type Weak = PinWeak<C>;
45 fn to_weak(&self) -> Self::Weak {
46 PinWeak::downgrade(self.clone())
47 }
48 fn from_weak(weak: &Self::Weak) -> Option<Self> {
49 weak.upgrade()
50 }
51}
52
53pub fn set_property_binding<T: Clone + 'static, StrongRef: StrongItemTreeRef + 'static>(
54 property: Pin<&Property<T>>,
55 component_strong: &StrongRef,
56 binding: fn(StrongRef) -> T,
57) {
58 let weak: ::Weak = component_strong.to_weak();
59 propertyPin<&Property>
60 .set_binding(move || binding(<StrongRef as StrongItemTreeRef>::from_weak(&weak).unwrap()))
61}
62
63pub fn set_animated_property_binding<
64 T: Clone + i_slint_core::properties::InterpolatedPropertyValue + 'static,
65 StrongRef: StrongItemTreeRef + 'static,
66>(
67 property: Pin<&Property<T>>,
68 component_strong: &StrongRef,
69 binding: fn(StrongRef) -> T,
70 animation_data: PropertyAnimation,
71) {
72 let weak: ::Weak = component_strong.to_weak();
73 property.set_animated_binding(
74 binding:move || binding(<StrongRef as StrongItemTreeRef>::from_weak(&weak).unwrap()),
75 animation_data,
76 )
77}
78
79pub fn set_animated_property_binding_for_transition<
80 T: Clone + i_slint_core::properties::InterpolatedPropertyValue + 'static,
81 StrongRef: StrongItemTreeRef + 'static,
82>(
83 property: Pin<&Property<T>>,
84 component_strong: &StrongRef,
85 binding: fn(StrongRef) -> T,
86 compute_animation_details: fn(
87 StrongRef,
88 ) -> (PropertyAnimation, i_slint_core::animations::Instant),
89) {
90 let weak_1: ::Weak = component_strong.to_weak();
91 let weak_2: ::Weak = weak_1.clone();
92 property.set_animated_binding_for_transition(
93 binding:move || binding(<StrongRef as StrongItemTreeRef>::from_weak(&weak_1).unwrap()),
94 compute_animation_details:move || {
95 compute_animation_details(<StrongRef as StrongItemTreeRef>::from_weak(&weak_2).unwrap())
96 },
97 )
98}
99
100pub fn set_property_state_binding<StrongRef: StrongItemTreeRef + 'static>(
101 property: Pin<&Property<StateInfo>>,
102 component_strong: &StrongRef,
103 binding: fn(StrongRef) -> i32,
104) {
105 let weak: ::Weak = component_strong.to_weak();
106 re_exports::set_state_binding(property, binding:move || {
107 binding(<StrongRef as StrongItemTreeRef>::from_weak(&weak).unwrap())
108 })
109}
110
111pub fn set_callback_handler<
112 Arg: ?Sized + 'static,
113 Ret: Default + 'static,
114 StrongRef: StrongItemTreeRef + 'static,
115>(
116 callback: Pin<&Callback<Arg, Ret>>,
117 component_strong: &StrongRef,
118 handler: fn(StrongRef, &Arg) -> Ret,
119) {
120 let weak: ::Weak = component_strong.to_weak();
121 callback.set_handler(move |arg: &Arg| {
122 handler(<StrongRef as StrongItemTreeRef>::from_weak(&weak).unwrap(), arg)
123 })
124}
125
126pub fn debug(s: SharedString) {
127 #[cfg(feature = "log")]
128 log::debug!("{s}");
129 #[cfg(not(feature = "log"))]
130 {
131 #[cfg(all(feature = "std", not(target_arch = "wasm32")))]
132 println!("{s}");
133 #[cfg(any(not(feature = "std"), target_arch = "wasm32"))]
134 i_slint_core::debug_log!("{s}");
135 }
136}
137
138pub fn ensure_backend() -> Result<(), crate::PlatformError> {
139 i_slint_backend_selector::with_platform(|_b: &dyn Platform| {
140 // Nothing to do, just make sure a backend was created
141 Ok(())
142 })
143}
144
145/// Creates a new window to render components in.
146pub fn create_window_adapter(
147) -> Result<alloc::rc::Rc<dyn i_slint_core::window::WindowAdapter>, crate::PlatformError> {
148 i_slint_backend_selector::with_platform(|b: &dyn Platform| b.create_window_adapter())
149}
150
151/// Wrapper around i_slint_core::translations::translate for the generated code
152pub fn translate(
153 origin: SharedString,
154 context: SharedString,
155 domain: SharedString,
156 args: Slice<SharedString>,
157 n: i32,
158 plural: SharedString,
159) -> SharedString {
160 i_slint_core::translations::translate(&origin, &context, &domain, arguments:args.as_slice(), n, &plural)
161}
162
163#[cfg(feature = "gettext")]
164pub fn init_translations(domain: &str, dirname: impl Into<std::path::PathBuf>) {
165 i_slint_core::translations::gettext_bindtextdomain(domain, _dirname:dirname.into()).unwrap()
166}
167
168/// internal re_exports used by the macro generated
169pub mod re_exports {
170 pub use alloc::boxed::Box;
171 pub use alloc::format;
172 pub use alloc::rc::{Rc, Weak};
173 pub use alloc::string::String;
174 pub use alloc::{vec, vec::Vec};
175 pub use const_field_offset::{self, FieldOffsets, PinnedDrop};
176 pub use core::iter::FromIterator;
177 pub use core::option::{Option, Option::*};
178 pub use core::result::{Result, Result::*};
179 // This one is empty when Qt is not available, which triggers a warning
180 #[allow(unused_imports)]
181 pub use i_slint_backend_selector::native_widgets::*;
182 pub use i_slint_core::accessibility::AccessibleStringProperty;
183 pub use i_slint_core::animations::{animation_tick, EasingCurve};
184 pub use i_slint_core::callbacks::Callback;
185 pub use i_slint_core::graphics::*;
186 pub use i_slint_core::input::{
187 key_codes::Key, FocusEvent, InputEventResult, KeyEvent, KeyEventResult, KeyboardModifiers,
188 MouseEvent,
189 };
190 pub use i_slint_core::item_tree::{
191 register_item_tree, unregister_item_tree, IndexRange, ItemTree, ItemTreeRefPin,
192 ItemTreeVTable, ItemTreeWeak,
193 };
194 pub use i_slint_core::item_tree::{
195 visit_item_tree, ItemTreeNode, ItemVisitorRefMut, ItemVisitorVTable, ItemWeak,
196 TraversalOrder, VisitChildrenResult,
197 };
198 pub use i_slint_core::items::*;
199 pub use i_slint_core::layout::*;
200 pub use i_slint_core::lengths::{
201 logical_position_to_api, LogicalLength, LogicalPoint, LogicalRect,
202 };
203 pub use i_slint_core::model::*;
204 pub use i_slint_core::properties::{set_state_binding, Property, PropertyTracker, StateInfo};
205 pub use i_slint_core::slice::Slice;
206 pub use i_slint_core::window::{
207 InputMethodRequest, WindowAdapter, WindowAdapterRc, WindowInner,
208 };
209 pub use i_slint_core::Color;
210 pub use i_slint_core::Coord;
211 pub use i_slint_core::ItemTreeVTable_static;
212 pub use i_slint_core::SharedString;
213 pub use i_slint_core::SharedVector;
214 pub use num_traits::float::Float;
215 pub use once_cell::race::OnceBox;
216 pub use once_cell::unsync::OnceCell;
217 pub use pin_weak::rc::PinWeak;
218 pub use vtable::{self, *};
219}
220