Skip to main content

FontResource

Struct FontResource 

Source
pub struct FontResource {
    pub primary_font: Option<Handle<Font>>,
}

Fields§

§primary_font: Option<Handle<Font>>

Implementations§

Source§

impl FontResource

Source

pub fn from_handle(font: Handle<Font>) -> Self

Examples found in repository?
examples/content_core.rs (lines 23-25)
21fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
22    commands.spawn(Camera2d);
23    commands.insert_resource(FontResource::from_handle(
24        asset_server.load("fonts/SarasaFixedSC-Regular.ttf"),
25    ));
26    commands
27        .spawn((
28            Node {
29                width: Val::Percent(100.0),
30                height: Val::Percent(100.0),
31                padding: UiRect::axes(Val::Px(40.0), Val::Px(28.0)),
32                row_gap: Val::Px(14.0),
33                flex_direction: FlexDirection::Column,
34                ..default()
35            },
36            BackgroundColor(Color::srgb_u8(248, 250, 252)),
37        ))
38        .with_children(|parent| {
39            text(parent, "Content page essentials", 30.0, Color::srgb_u8(15, 23, 42));
40            text(
41                parent,
42                "Heading, paragraph, helper text, image, link, rule, and list primitives.",
43                15.0,
44                Color::srgb_u8(71, 85, 105),
45            );
46            parent.spawn((
47                Node {
48                    width: Val::Px(120.0),
49                    height: Val::Px(120.0),
50                    ..default()
51                },
52                AddImage {
53                    src: "branding/icon.png".to_string(),
54                    alt: "Project icon".to_string(),
55                    ..default()
56                },
57            ));
58            parent.spawn((
59                Node {
60                    width: Val::Px(220.0),
61                    ..default()
62                },
63                AddLink {
64                    name: "docs".to_string(),
65                    href: "/docs/getting-started".to_string(),
66                    text: "Open getting started docs".to_string(),
67                    ..default()
68                },
69            ));
70            parent.spawn((
71                Node {
72                    width: Val::Percent(100.0),
73                    height: Val::Px(1.0),
74                    ..default()
75                },
76                BackgroundColor(Color::srgb_u8(203, 213, 225)),
77            ));
78            parent
79                .spawn((
80                    Node {
81                        row_gap: Val::Px(8.0),
82                        flex_direction: FlexDirection::Column,
83                        ..default()
84                    },
85                ))
86                .with_children(|list| {
87                    text(list, "• Semantic content tags", 15.0, Color::srgb_u8(30, 41, 59));
88                    text(
89                        list,
90                        "• Fieldset and legend defaults",
91                        15.0,
92                        Color::srgb_u8(30, 41, 59),
93                    );
94                    text(
95                        list,
96                        "• Small / strong / emphasis styling hooks",
97                        15.0,
98                        Color::srgb_u8(30, 41, 59),
99                    );
100                });
101        });
102}
More examples
Hide additional examples
examples/control_events.rs (lines 46-48)
44fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
45    commands.spawn(Camera2d);
46    commands.insert_resource(FontResource::from_handle(
47        asset_server.load("fonts/SarasaFixedSC-Regular.ttf"),
48    ));
49
50    commands
51        .spawn((
52            Node {
53                width: Val::Percent(100.0),
54                height: Val::Percent(100.0),
55                padding: UiRect::all(Val::Px(24.0)),
56                column_gap: Val::Px(20.0),
57                overflow: Overflow::visible(),
58                ..default()
59            },
60            BackgroundColor(Color::srgb_u8(248, 250, 252)),
61        ))
62        .with_children(|parent| {
63            let mut left = parent.spawn(Node {
64                width: Val::Px(420.0),
65                padding: UiRect::all(Val::Px(16.0)),
66                row_gap: Val::Px(12.0),
67                flex_direction: FlexDirection::Column,
68                overflow: Overflow::visible(),
69                border_radius: BorderRadius::all(Val::Px(12.0)),
70                ..default()
71            });
72            left.insert(BorderColor::all(Color::srgb_u8(203, 213, 225)));
73            left.insert(BackgroundColor(Color::WHITE));
74            left.with_children(|parent| {
75                spawn_text(
76                    parent,
77                    "Interact with these controls",
78                    18.0,
79                    Color::srgb_u8(15, 23, 42),
80                );
81                parent.spawn(AddInput {
82                    name: "display_name".to_string(),
83                    placeholder: "Type a display name".to_string(),
84                    size_chars: Some(24),
85                    ..default()
86                });
87                parent.spawn(AddInput {
88                    name: "zoom".to_string(),
89                    input_type: InputType::Range,
90                    value: "35".to_string(),
91                    min: Some(0.0),
92                    max: Some(100.0),
93                    step: Some(5.0),
94                    ..default()
95                });
96                parent.spawn(AddSelect {
97                    name: "theme".to_string(),
98                    value: "light".to_string(),
99                    options: vec![
100                        option("theme_light", "light", "Light"),
101                        option("theme_dark", "dark", "Dark"),
102                        option("theme_hc", "high-contrast", "High Contrast"),
103                    ],
104                    ..default()
105                });
106                parent
107                    .spawn(Node {
108                        column_gap: Val::Px(10.0),
109                        flex_wrap: FlexWrap::Wrap,
110                        ..default()
111                    })
112                    .with_children(|parent| {
113                        parent.spawn(AddButton {
114                            name: "save".to_string(),
115                            text: "Save".to_string(),
116                            class: Some("button-root w-[120px]".to_string()),
117                            ..default()
118                        });
119                        parent.spawn(AddButton {
120                            name: "reset".to_string(),
121                            text: "Reset".to_string(),
122                            class: Some("button-root w-[120px]".to_string()),
123                            ..default()
124                        });
125                    });
126            });
127
128            let mut right = parent.spawn(Node {
129                flex_grow: 1.0,
130                padding: UiRect::all(Val::Px(16.0)),
131                row_gap: Val::Px(10.0),
132                flex_direction: FlexDirection::Column,
133                overflow: Overflow::visible(),
134                border_radius: BorderRadius::all(Val::Px(12.0)),
135                ..default()
136            });
137            right.insert(BorderColor::all(Color::srgb_u8(203, 213, 225)));
138            right.insert(BackgroundColor(Color::WHITE));
139            right.with_children(|parent| {
140                spawn_text(parent, "Event log", 18.0, Color::srgb_u8(15, 23, 42));
141                parent.spawn((
142                    EventLogText,
143                    Node {
144                        width: Val::Percent(100.0),
145                        ..default()
146                    },
147                    TextLayout::default(),
148                    AddText {
149                        text: "No events yet.\nClick, type, drag, or select.".to_string(),
150                        size: 14.0,
151                        color: Color::srgb_u8(71, 85, 105),
152                        ..default()
153                    },
154                ));
155            });
156        });
157}
examples/basic_controls.rs (lines 31-33)
29fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
30    commands.spawn(Camera2d);
31    commands.insert_resource(FontResource::from_handle(
32        asset_server.load("fonts/SarasaFixedSC-Regular.ttf"),
33    ));
34
35    commands
36        .spawn((
37            scroll_container_node(Node {
38                width: Val::Percent(100.0),
39                height: Val::Percent(100.0),
40                padding: UiRect::all(Val::Px(24.0)),
41                column_gap: Val::Px(20.0),
42                align_items: AlignItems::Start,
43                ..default()
44            }),
45            ScrollPosition::default(),
46            BackgroundColor(Color::srgb_u8(245, 247, 250)),
47        ))
48        .with_children(|parent| {
49            spawn_column(parent, |parent| {
50                spawn_panel(parent, "Text", |parent| {
51                    spawn_text(parent, "Display Title", 22.0, Color::srgb_u8(15, 23, 42));
52                    spawn_text(
53                        parent,
54                        "Body copy rendered through the runtime text builder.",
55                        15.0,
56                        Color::srgb_u8(30, 41, 59),
57                    );
58                    spawn_text(
59                        parent,
60                        "Secondary hint text for dense tool UIs.",
61                        13.0,
62                        Color::srgb_u8(100, 116, 139),
63                    );
64                });
65
66                spawn_panel(parent, "Text Inputs", |parent| {
67                    parent.spawn(AddInput {
68                        name: "pilot_name".to_string(),
69                        placeholder: "Pilot name".to_string(),
70                        size_chars: Some(24),
71                        ..default()
72                    });
73                    parent.spawn(AddInput {
74                        name: "callsign".to_string(),
75                        value: "ALPHA-7".to_string(),
76                        size_chars: Some(16),
77                        ..default()
78                    });
79                    parent.spawn(AddInput {
80                        name: "long_note".to_string(),
81                        value: "Long single-line value for caret scrolling and selection checks"
82                            .to_string(),
83                        placeholder: "Long note".to_string(),
84                        size_chars: Some(28),
85                        ..default()
86                    });
87                    parent.spawn(AddInput {
88                        name: "multiline_note".to_string(),
89                        input_type: InputType::Textarea,
90                        value: "Textarea value for native multiline wrapping, caret movement,\nand segmented selection checks."
91                            .to_string(),
92                        placeholder: "Multiline note".to_string(),
93                        size_chars: Some(28),
94                        rows: Some(4),
95                        ..default()
96                    });
97                    parent.spawn(AddInput {
98                        name: "ime_text".to_string(),
99                        placeholder: "中文 IME input".to_string(),
100                        size_chars: Some(20),
101                        ..default()
102                    });
103                    parent.spawn(AddInput {
104                        name: "ime_textarea".to_string(),
105                        input_type: InputType::Textarea,
106                        placeholder: "中文 IME textarea".to_string(),
107                        size_chars: Some(28),
108                        rows: Some(3),
109                        ..default()
110                    });
111                    parent.spawn(AddInput {
112                        name: "disabled_text".to_string(),
113                        value: "Locked field".to_string(),
114                        size_chars: Some(18),
115                        disabled: true,
116                        ..default()
117                    });
118                });
119
120                spawn_panel(parent, "Numeric Inputs", |parent| {
121                    spawn_row(parent, |parent| {
122                        parent.spawn(AddInput {
123                            name: "count".to_string(),
124                            input_type: InputType::Number,
125                            value: "12".to_string(),
126                            min: Some(0.0),
127                            max: Some(64.0),
128                            step: Some(1.0),
129                            size_chars: Some(8),
130                            ..default()
131                        });
132                        parent.spawn(AddInput {
133                            name: "threshold".to_string(),
134                            input_type: InputType::Number,
135                            value: ".".to_string(),
136                            min: Some(0.0),
137                            max: Some(1.0),
138                            step: Some(0.05),
139                            size_chars: Some(8),
140                            ..default()
141                        });
142                    });
143                    parent.spawn((
144                        SliderValueText,
145                        Node {
146                            width: Val::Percent(100.0),
147                            ..default()
148                        },
149                        TextLayout::default(),
150                        AddText {
151                            text: "Volume: 45".to_string(),
152                            size: 14.0,
153                            color: Color::srgb_u8(71, 85, 105),
154                            ..default()
155                        },
156                    ));
157                    parent.spawn(AddInput {
158                        name: "volume".to_string(),
159                        input_type: InputType::Range,
160                        value: "45".to_string(),
161                        min: Some(0.0),
162                        max: Some(100.0),
163                        step: Some(5.0),
164                        ..default()
165                    });
166                });
167
168                spawn_panel(parent, "Form Toggles", |parent| {
169                    spawn_labeled_toggle_row(
170                        parent,
171                        "Checkbox input",
172                        AddInput {
173                            name: "enable_audio".to_string(),
174                            input_type: InputType::Checkbox,
175                            checked: true,
176                            ..default()
177                        },
178                    );
179                    spawn_labeled_toggle_row(
180                        parent,
181                        "Radio input (easy)",
182                        AddInput {
183                            name: "mode".to_string(),
184                            input_type: InputType::Radio,
185                            value: "easy".to_string(),
186                            checked: true,
187                            ..default()
188                        },
189                    );
190                    spawn_labeled_toggle_row(
191                        parent,
192                        "Radio input (hard)",
193                        AddInput {
194                            name: "mode".to_string(),
195                            input_type: InputType::Radio,
196                            value: "hard".to_string(),
197                            ..default()
198                        },
199                    );
200                    spawn_field(parent, "Password input", |parent| {
201                        parent.spawn(AddInput {
202                            name: "secret".to_string(),
203                            input_type: InputType::Password,
204                            value: "hunter2".to_string(),
205                            placeholder: "Password".to_string(),
206                            size_chars: Some(20),
207                            ..default()
208                        });
209                    });
210                });
211            });
212
213            spawn_column(parent, |parent| {
214                spawn_panel(parent, "Selects", |parent| {
215                    parent.spawn(AddSelect {
216                        name: "difficulty".to_string(),
217                        value: "normal".to_string(),
218                        options: vec![
219                            option("difficulty_easy", "easy", "Easy"),
220                            option("difficulty_normal", "normal", "Normal"),
221                            option("difficulty_hard", "hard", "Hard"),
222                        ],
223                        ..default()
224                    });
225                    parent.spawn(AddSelect {
226                        name: "region".to_string(),
227                        value: "us-east".to_string(),
228                        options: vec![
229                            option("region_use1", "us-east", "US East"),
230                            option("region_euw1", "eu-west", "EU West"),
231                            AddSelectOption {
232                                name: "region_apac".to_string(),
233                                value: "apac".to_string(),
234                                text: "APAC (disabled)".to_string(),
235                                localized_text: None,
236                                localized_text_format: None,
237                                disabled: true,
238                            },
239                        ],
240                        ..default()
241                    });
242                });
243
244                spawn_panel(parent, "Buttons", |parent| {
245                    spawn_field_label(parent, "Default");
246                    spawn_row(parent, |parent| {
247                        parent.spawn(AddButton {
248                            name: "default_primary".to_string(),
249                            text: "Primary Action".to_string(),
250                            class: Some("button-root w-[180px]".to_string()),
251                            ..default()
252                        });
253                        parent.spawn(AddButton {
254                            name: "default_secondary".to_string(),
255                            text: "Secondary Action".to_string(),
256                            class: Some("button-root w-[180px]".to_string()),
257                            ..default()
258                        });
259                    });
260
261                    spawn_field_label(parent, "Sizing");
262                    spawn_row(parent, |parent| {
263                        parent.spawn(AddButton {
264                            name: "compact".to_string(),
265                            text: "Compact".to_string(),
266                            class: Some(
267                                "button-root min-h-[30px] w-[120px] px-[8px] py-[4px]".to_string(),
268                            ),
269                            ..default()
270                        });
271                        parent.spawn(AddButton {
272                            name: "wide".to_string(),
273                            text: "Wide Button".to_string(),
274                            class: Some("button-root min-h-[48px] w-[220px]".to_string()),
275                            ..default()
276                        });
277                    });
278
279                    spawn_field_label(parent, "Disabled");
280                    spawn_row(parent, |parent| {
281                        parent.spawn(AddButton {
282                            name: "disabled_default".to_string(),
283                            text: "Disabled".to_string(),
284                            disabled: true,
285                            ..default()
286                        });
287                        parent.spawn(AddButton {
288                            name: "disabled_wide".to_string(),
289                            text: "Disabled Wide".to_string(),
290                            disabled: true,
291                            class: Some("button-root min-h-[48px] w-[220px]".to_string()),
292                            ..default()
293                        });
294                    });
295                });
296            });
297        });
298}

Trait Implementations§

Source§

impl Default for FontResource

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl Resource for FontResource
where Self: Send + Sync + 'static,

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T, U> AsBindGroupShaderType<U> for T
where U: ShaderType, &'a T: for<'a> Into<U>,

Source§

fn as_bind_group_shader_type(&self, _images: &RenderAssets<GpuImage>) -> U

Return the T ShaderType for self. When used in AsBindGroup derives, it is safe to assume that all images in self exist.
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> Downcast for T
where T: Any,

Source§

fn into_any(self: Box<T>) -> Box<dyn Any>

Converts Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>, which can then be downcast into Box<dyn ConcreteType> where ConcreteType implements Trait.
Source§

fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>

Converts Rc<Trait> (where Trait: Downcast) to Rc<Any>, which can then be further downcast into Rc<ConcreteType> where ConcreteType implements Trait.
Source§

fn as_any(&self) -> &(dyn Any + 'static)

Converts &Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &Any’s vtable from &Trait’s.
Source§

fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)

Converts &mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &mut Any’s vtable from &mut Trait’s.
Source§

impl<T> Downcast for T
where T: Any,

Source§

fn into_any(self: Box<T>) -> Box<dyn Any>

Convert Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>. Box<dyn Any> can then be further downcast into Box<ConcreteType> where ConcreteType implements Trait.
Source§

fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>

Convert Rc<Trait> (where Trait: Downcast) to Rc<Any>. Rc<Any> can then be further downcast into Rc<ConcreteType> where ConcreteType implements Trait.
Source§

fn as_any(&self) -> &(dyn Any + 'static)

Convert &Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &Any’s vtable from &Trait’s.
Source§

fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)

Convert &mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &mut Any’s vtable from &mut Trait’s.
Source§

impl<T> DowncastSend for T
where T: Any + Send,

Source§

fn into_any_send(self: Box<T>) -> Box<dyn Any + Send>

Converts Box<Trait> (where Trait: DowncastSend) to Box<dyn Any + Send>, which can then be downcast into Box<ConcreteType> where ConcreteType implements Trait.
Source§

impl<T> DowncastSync for T
where T: Any + Send + Sync,

Source§

fn into_any_arc(self: Arc<T>) -> Arc<dyn Any + Sync + Send>

Convert Arc<Trait> (where Trait: Downcast) to Arc<Any>. Arc<Any> can then be further downcast into Arc<ConcreteType> where ConcreteType implements Trait.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<S> FromSample<S> for S

Source§

fn from_sample_(s: S) -> S

Source§

impl<T> FromWorld for T
where T: Default,

Source§

fn from_world(_world: &mut World) -> T

Creates Self using default().

Source§

impl<T, W> HasTypeWitness<W> for T
where W: MakeTypeWitness<Arg = T>, T: ?Sized,

Source§

const WITNESS: W = W::MAKE

A constant of the type witness
Source§

impl<T> Identity for T
where T: ?Sized,

Source§

const TYPE_EQ: TypeEq<T, <T as Identity>::Type> = TypeEq::NEW

Proof that Self is the same type as Self::Type, provides methods for casting between Self and Self::Type.
Source§

type Type = T

The same type as Self, used to emulate type equality bounds (T == U) with associated type equality constraints (T: Identity<Type = U>).
Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> IntoResult<T> for T

Source§

fn into_result(self) -> Result<T, RunSystemError>

Converts this type into the system output type.
Source§

impl<A> Is for A
where A: Any,

Source§

fn is<T>() -> bool
where T: Any,

Checks if the current type “is” another type, using a TypeId equality comparison. This is most useful in the context of generic logic. Read more
Source§

impl<T, U> ToSample<U> for T
where U: FromSample<T>,

Source§

fn to_sample_(self) -> U

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

impl<T> ConditionalSend for T
where T: Send,

Source§

impl<S, T> Duplex<S> for T
where T: FromSample<S> + ToSample<S>,

Source§

impl<T> Settings for T
where T: 'static + Send + Sync,

Source§

impl<T> WasmNotSend for T
where T: Send,

Source§

impl<T> WasmNotSendSync for T

Source§

impl<T> WasmNotSync for T
where T: Sync,