1#![doc(hidden)]
9
10use core::pin::Pin;
11use re_exports::*;
12
13pub trait StrongItemTreeRef: Sized {
22 type Weak: Clone + 'static;
23 fn to_weak(&self) -> Self::Weak;
24 fn from_weak(weak: &Self::Weak) -> Option<Self>;
25}
26
27impl<C: 'static> StrongItemTreeRef for Pin<Rc<C>> {
28 type Weak = PinWeak<C>;
29 fn to_weak(&self) -> Self::Weak {
30 PinWeak::downgrade(self.clone())
31 }
32 fn from_weak(weak: &Self::Weak) -> Option<Self> {
33 weak.upgrade()
34 }
35}
36
37pub fn set_property_binding<
38 T: Clone + Default + 'static,
39 StrongRef: StrongItemTreeRef + 'static,
40>(
41 property: Pin<&Property<T>>,
42 component_strong: &StrongRef,
43 binding: fn(StrongRef) -> T,
44) {
45 let weak = component_strong.to_weak();
46 property.set_binding(move || {
47 <StrongRef as StrongItemTreeRef>::from_weak(&weak).map(binding).unwrap_or_default()
48 })
49}
50
51pub fn set_animated_property_binding<
52 T: Clone + i_slint_core::properties::InterpolatedPropertyValue + 'static,
53 StrongRef: StrongItemTreeRef + 'static,
54>(
55 property: Pin<&Property<T>>,
56 component_strong: &StrongRef,
57 binding: fn(StrongRef) -> T,
58 compute_animation_details: fn(
59 StrongRef,
60 )
61 -> (PropertyAnimation, Option<i_slint_core::animations::Instant>),
62) {
63 let weak_1 = component_strong.to_weak();
64 let weak_2 = weak_1.clone();
65 property.set_animated_binding(
66 move || binding(<StrongRef as StrongItemTreeRef>::from_weak(&weak_1).unwrap()),
67 move || {
68 compute_animation_details(<StrongRef as StrongItemTreeRef>::from_weak(&weak_2).unwrap())
69 },
70 )
71}
72
73pub fn set_property_state_binding<StrongRef: StrongItemTreeRef + 'static>(
74 property: Pin<&Property<StateInfo>>,
75 component_strong: &StrongRef,
76 binding: fn(StrongRef) -> i32,
77) {
78 let weak = component_strong.to_weak();
79 re_exports::set_state_binding(property, move || {
80 binding(<StrongRef as StrongItemTreeRef>::from_weak(&weak).unwrap())
81 })
82}
83
84pub fn set_callback_handler<
85 Arg: ?Sized + 'static,
86 Ret: Default + 'static,
87 StrongRef: StrongItemTreeRef + 'static,
88>(
89 callback: Pin<&Callback<Arg, Ret>>,
90 component_strong: &StrongRef,
91 handler: fn(StrongRef, &Arg) -> Ret,
92) {
93 let weak = component_strong.to_weak();
94 callback.set_handler(move |arg| {
95 handler(<StrongRef as StrongItemTreeRef>::from_weak(&weak).unwrap(), arg)
96 })
97}
98
99pub fn debug(ctx: &i_slint_core::SlintContext, s: SharedString) {
100 ctx.dispatch_log_message(i_slint_core::debug_log::LogMessage::new(
101 i_slint_core::debug_log::LogMessageSource::SlintCode,
102 None,
103 format_args!("{s}"),
104 ));
105}
106
107pub fn ensure_context() -> Result<i_slint_core::SlintContext, crate::PlatformError> {
109 i_slint_backend_selector::with_global_context(|ctx| ctx.clone())
110}
111
112pub fn translate(
114 ctx: &i_slint_core::SlintContext,
115 origin: SharedString,
116 context: SharedString,
117 domain: SharedString,
118 args: Slice<SharedString>,
119 n: i32,
120 plural: SharedString,
121) -> SharedString {
122 ctx.translate(&origin, &context, &domain, args.as_slice(), n, &plural)
123}
124
125#[cfg(feature = "gettext")]
126pub fn init_translations(domain: &str, dirname: impl Into<std::path::PathBuf>) {
127 i_slint_core::translations::gettext_bindtextdomain(domain, dirname.into()).unwrap()
128}
129
130pub fn use_24_hour_format() -> bool {
131 i_slint_core::date_time::use_24_hour_format()
132}
133
134#[inline(never)]
138pub fn build_array_chunk(chunk: impl FnOnce()) {
139 chunk()
140}
141
142pub mod re_exports {
144 pub use alloc::boxed::Box;
145 pub use alloc::rc::{Rc, Weak};
146 pub use alloc::string::String;
147 pub use alloc::{vec, vec::Vec};
148 pub use const_field_offset::{self, FieldOffsets, PinnedDrop};
149 pub use core::iter::FromIterator;
150 pub use core::option::{Option, Option::*};
151 pub use core::result::{Result, Result::*};
152 pub use i_slint_core::styled_text::{
153 StyledText, color_to_styled_text, parse_markdown, string_to_styled_text,
154 };
155 pub use euclid::approxeq::ApproxEq;
157 #[allow(unused_imports)]
158 pub use i_slint_backend_selector::native_widgets::*;
159 pub use i_slint_core::accessibility::{
160 AccessibilityAction, AccessibleStringProperty, SupportedAccessibilityAction,
161 };
162 pub use i_slint_core::animations::{EasingCurve, animation_tick, current_tick};
163 pub use i_slint_core::api::LogicalPosition;
164 pub use i_slint_core::callbacks::Callback;
165 pub use i_slint_core::context::SlintContext;
166 pub use i_slint_core::cursor::MouseCursorInner;
167 pub use i_slint_core::data_transfer::DataTransfer;
168 pub use i_slint_core::date_time::*;
169 pub use i_slint_core::detect_operating_system;
170 pub use i_slint_core::graphics::*;
171 pub use i_slint_core::input::{
172 FocusEvent, FocusReason, InputEventResult, KeyEvent, KeyEventResult, KeyboardModifiers,
173 Keys, MouseEvent, key_codes::Key, make_keys,
174 };
175 pub use i_slint_core::item_tree::{
176 IndexRange, ItemTree, ItemTreeRc, ItemTreeRefPin, ItemTreeVTable, ItemTreeWeak,
177 ensure_item_tree_instantiated, register_item_tree, unregister_item_tree,
178 };
179 pub use i_slint_core::item_tree::{
180 ItemTreeNode, ItemVisitorRefMut, ItemVisitorVTable, ItemWeak, TraversalOrder,
181 VisitChildrenResult, visit_item_tree, visit_item_tree_z_sorted,
182 };
183 pub use i_slint_core::items::{Transform, *};
184 pub use i_slint_core::layout::*;
185 pub use i_slint_core::lengths::{
186 LogicalLength, LogicalPoint, LogicalRect, logical_position_to_api,
187 };
188 pub use i_slint_core::macos_bring_all_windows_to_front;
189 pub use i_slint_core::menus::{Menu, MenuFromItemTree, MenuVTable};
190 pub use i_slint_core::model::*;
191 pub use i_slint_core::open_url;
192 pub use i_slint_core::properties::{
193 ChangeTracker, Property, PropertyTracker, StateInfo, change_tracker_init_erased,
194 set_animated_property_binding_erased, set_callback_handler_erased,
195 set_property_binding_erased, set_property_state_binding_erased, set_state_binding,
196 };
197 pub use i_slint_core::slice::Slice;
198 pub use i_slint_core::string::shared_string_from_number_unlocalized;
199 pub use i_slint_core::string::shared_string_replace_all;
200 pub use i_slint_core::timers::{Timer, TimerMode};
201 pub use i_slint_core::translations::TranslationsBundled;
202 pub use i_slint_core::window::{
203 InputMethodRequest, WindowAdapter, WindowAdapterRc, WindowInner, WindowKind,
204 default_window_title,
205 };
206 pub use i_slint_core::{
207 Color, Coord, SharedString, SharedVector, format, string::ToSharedString,
208 };
209 pub use i_slint_core::{ItemTreeVTable_static, MenuVTable_static};
210 pub use num_traits::float::Float;
211 pub use num_traits::ops::euclid::Euclid;
212 pub use once_cell::race::OnceBox;
213 pub use once_cell::unsync::OnceCell;
214 pub use pin_weak::rc::PinWeak;
215 pub use unicode_segmentation::UnicodeSegmentation;
216 pub use vtable::{self, *};
217
218 #[cfg(feature = "live-preview")]
219 pub use i_slint_live_preview::live_component as live_preview;
220}