Text
There are multiple different types in GPUI that allow you to render text, to learn how to style text read the styling text chapter.
&'static str
use gpui::{
AppContext, Application, Context, IntoElement, ParentElement, Render, Styled, Window,
WindowOptions, div, rgb,
};
struct RootView;
impl Render for RootView {
fn render(&mut self, _window: &mut Window, _cx: &mut Context<Self>) -> impl IntoElement {
div()
.size_full()
.bg(rgb(0xFFFFFF))
.flex()
.justify_center()
.items_center()
.text_3xl()
.child("Hello")
}
}
fn main() {
Application::new().run(|app| {
app.open_window(WindowOptions::default(), |_window, app| {
app.new(|_cx| RootView)
})
.unwrap();
});
}
String
Using a String
should be used sparingly as it will cause a heap allocation on every re-render, read on about SharedString to learn how to avoid this.
use gpui::{
AppContext, Application, Context, IntoElement, ParentElement, Render, Styled, Window,
WindowOptions, div, rgb,
};
struct RootView;
impl Render for RootView {
fn render(&mut self, _window: &mut Window, _cx: &mut Context<Self>) -> impl IntoElement {
div()
.size_full()
.bg(rgb(0xFFFFFF))
.flex()
.justify_center()
.items_center()
.text_3xl()
.child(String::from("Hello"))
}
}
fn main() {
Application::new().run(|app| {
app.open_window(WindowOptions::default(), |_window, app| {
app.new(|_cx| RootView)
})
.unwrap();
});
}
SharedString
SharedString
is an immutable string that can be cheaply cloned, this is especially useful in the render
function as it avoids a heap allocation on every re-render which occurs when using String
.
The SharedString
is typically stored in the Entity
state where it can be accessed by the render
function.
use gpui::{
AppContext, Application, Context, IntoElement, ParentElement, Render, SharedString, Styled,
Window, WindowOptions, div, rgb,
};
struct RootView {
text: SharedString,
}
impl Render for RootView {
fn render(&mut self, _window: &mut Window, _cx: &mut Context<Self>) -> impl IntoElement {
div()
.size_full()
.bg(rgb(0xFFFFFF))
.flex()
.justify_center()
.items_center()
.text_3xl()
.child(self.text.clone())
}
}
fn main() {
Application::new().run(|app| {
app.open_window(WindowOptions::default(), |_window, app| {
app.new(|_cx| RootView {
text: SharedString::new_static("Hello"),
})
})
.unwrap();
});
}
StyledText
The StyledText
component allows you style specific ranges of the text differently, the text ranges are called TextRun
's. It is unnecessary to use StyleText
if the whole range of your text uses the same style.
use gpui::{
AppContext, Application, Context, Font, FontFeatures, FontStyle, FontWeight, IntoElement,
ParentElement, Render, SharedString, StrikethroughStyle, Styled, StyledText, TextRun,
UnderlineStyle, Window, WindowOptions, div, hsla, px, rgb,
};
struct RootView;
impl Render for RootView {
fn render(&mut self, _window: &mut Window, _cx: &mut Context<Self>) -> impl IntoElement {
div()
.size_full()
.bg(rgb(0xFFFFFF))
.flex()
.justify_center()
.items_center()
.text_3xl()
.child(StyledText::new("Text").with_runs(vec![
TextRun {
len: 2,
font: Font {
family: SharedString::new_static(".SystemUIFont"),
features: FontFeatures::default(),
fallbacks: None,
weight: FontWeight::default(),
style: FontStyle::default(),
},
color: hsla(0., 1., 0.5, 1.),
background_color: None,
underline: None,
strikethrough: Some(StrikethroughStyle {
thickness: px(1.),
color: Some(hsla(0., 1., 0.5, 1.)),
}),
},
TextRun {
len: 3,
font: Font {
family: SharedString::new_static(".SystemUIFont"),
features: FontFeatures::default(),
fallbacks: None,
weight: FontWeight::default(),
style: FontStyle::default(),
},
color: hsla(240. / 360., 1., 0.5, 1.),
background_color: None,
underline: Some(UnderlineStyle {
thickness: px(1.),
color: Some(hsla(240. / 360., 1., 0.5, 1.)),
wavy: true,
}),
strikethrough: None,
},
]))
}
}
fn main() {
Application::new().run(|app| {
app.open_window(WindowOptions::default(), |_window, app| {
app.new(|_cx| RootView)
})
.unwrap();
});
}
InteractiveText
The InteractiveText
component allows you to make specific ranges of the text interactive, it allows you to add click and hover listeners for specific text ranges. The component takes a ElementId
that must be identical on every frame and a StyledText.
use gpui::{
AppContext, Application, Context, InteractiveText, IntoElement, ParentElement, Render, Styled,
StyledText, Window, WindowOptions, div, rgb,
};
struct RootView;
impl Render for RootView {
fn render(&mut self, _window: &mut Window, _cx: &mut Context<Self>) -> impl IntoElement {
div()
.size_full()
.bg(rgb(0xFFFFFF))
.flex()
.justify_center()
.items_center()
.text_3xl()
.child(
InteractiveText::new("interactive_text_id", StyledText::new("Text")).on_click(
vec![1..3],
|_range_index, _window, _app| {
println!("Clicked");
},
),
)
}
}
fn main() {
Application::new().run(|app| {
app.open_window(WindowOptions::default(), |_window, app| {
app.new(|_cx| RootView)
})
.unwrap();
});
}