Changed label and text to be generic over static strings and owned strings

This commit is contained in:
Mikayla Maki 2023-02-21 16:46:03 -08:00
parent ebf1da1de8
commit b500ed3171
24 changed files with 60 additions and 72 deletions

View File

@ -78,7 +78,7 @@ impl View for UpdateNotification {
) )
.with_child({ .with_child({
let style = theme.action_message.style_for(state, false); let style = theme.action_message.style_for(state, false);
Text::new("View the release notes".to_string(), style.text.clone()) Text::new("View the release notes", style.text.clone())
.contained() .contained()
.with_style(style.container) .with_style(style.container)
.boxed() .boxed()

View File

@ -47,7 +47,7 @@ impl View for Breadcrumbs {
{ {
Flex::row() Flex::row()
.with_children(Itertools::intersperse_with(breadcrumbs.into_iter(), || { .with_children(Itertools::intersperse_with(breadcrumbs.into_iter(), || {
Label::new("".to_string(), theme.breadcrumbs.text.clone()).boxed() Label::new("", theme.breadcrumbs.text.clone()).boxed()
})) }))
.contained() .contained()
.with_style(theme.breadcrumbs.container) .with_style(theme.breadcrumbs.container)

View File

@ -293,7 +293,7 @@ impl CollabTitlebarItem {
let titlebar = &theme.workspace.titlebar; let titlebar = &theme.workspace.titlebar;
MouseEventHandler::<Share>::new(0, cx, |state, _| { MouseEventHandler::<Share>::new(0, cx, |state, _| {
let style = titlebar.share_button.style_for(state, false); let style = titlebar.share_button.style_for(state, false);
Label::new("Share".into(), style.text.clone()) Label::new("Share", style.text.clone())
.contained() .contained()
.with_style(style.container) .with_style(style.container)
.boxed() .boxed()
@ -378,7 +378,7 @@ impl CollabTitlebarItem {
.titlebar .titlebar
.sign_in_prompt .sign_in_prompt
.style_for(state, false); .style_for(state, false);
Label::new("Sign in".to_string(), style.text.clone()) Label::new("Sign in", style.text.clone())
.contained() .contained()
.with_style(style.container) .with_style(style.container)
.boxed() .boxed()
@ -531,7 +531,7 @@ impl CollabTitlebarItem {
client::Status::UpgradeRequired => Some( client::Status::UpgradeRequired => Some(
MouseEventHandler::<ConnectionStatusButton>::new(0, cx, |_, _| { MouseEventHandler::<ConnectionStatusButton>::new(0, cx, |_, _| {
Label::new( Label::new(
"Please update Zed to collaborate".to_string(), "Please update Zed to collaborate",
theme.workspace.titlebar.outdated_warning.text.clone(), theme.workspace.titlebar.outdated_warning.text.clone(),
) )
.contained() .contained()

View File

@ -749,7 +749,7 @@ impl ContactList {
) )
.with_children(if is_pending { .with_children(if is_pending {
Some( Some(
Label::new("Calling".to_string(), theme.calling_indicator.text.clone()) Label::new("Calling", theme.calling_indicator.text.clone())
.contained() .contained()
.with_style(theme.calling_indicator.container) .with_style(theme.calling_indicator.container)
.aligned() .aligned()
@ -950,7 +950,7 @@ impl ContactList {
.boxed(), .boxed(),
) )
.with_child( .with_child(
Label::new("Screen".into(), row.name.text.clone()) Label::new("Screen", row.name.text.clone())
.aligned() .aligned()
.left() .left()
.contained() .contained()
@ -994,7 +994,7 @@ impl ContactList {
Some( Some(
MouseEventHandler::<LeaveCall>::new(0, cx, |state, _| { MouseEventHandler::<LeaveCall>::new(0, cx, |state, _| {
let style = theme.leave_call.style_for(state, false); let style = theme.leave_call.style_for(state, false);
Label::new("Leave Session".into(), style.text.clone()) Label::new("Leave Session", style.text.clone())
.contained() .contained()
.with_style(style.container) .with_style(style.container)
.boxed() .boxed()
@ -1026,7 +1026,7 @@ impl ContactList {
.boxed(), .boxed(),
) )
.with_child( .with_child(
Label::new(text.to_string(), header_style.text.clone()) Label::new(text, header_style.text.clone())
.aligned() .aligned()
.left() .left()
.contained() .contained()
@ -1126,7 +1126,7 @@ impl ContactList {
) )
.with_children(if calling { .with_children(if calling {
Some( Some(
Label::new("Calling".to_string(), theme.calling_indicator.text.clone()) Label::new("Calling", theme.calling_indicator.text.clone())
.contained() .contained()
.with_style(theme.calling_indicator.container) .with_style(theme.calling_indicator.container)
.aligned() .aligned()

View File

@ -172,7 +172,7 @@ impl IncomingCallNotification {
.with_child( .with_child(
MouseEventHandler::<Accept>::new(0, cx, |_, cx| { MouseEventHandler::<Accept>::new(0, cx, |_, cx| {
let theme = &cx.global::<Settings>().theme.incoming_call_notification; let theme = &cx.global::<Settings>().theme.incoming_call_notification;
Label::new("Accept".to_string(), theme.accept_button.text.clone()) Label::new("Accept", theme.accept_button.text.clone())
.aligned() .aligned()
.contained() .contained()
.with_style(theme.accept_button.container) .with_style(theme.accept_button.container)
@ -188,7 +188,7 @@ impl IncomingCallNotification {
.with_child( .with_child(
MouseEventHandler::<Decline>::new(0, cx, |_, cx| { MouseEventHandler::<Decline>::new(0, cx, |_, cx| {
let theme = &cx.global::<Settings>().theme.incoming_call_notification; let theme = &cx.global::<Settings>().theme.incoming_call_notification;
Label::new("Decline".to_string(), theme.decline_button.text.clone()) Label::new("Decline", theme.decline_button.text.clone())
.aligned() .aligned()
.contained() .contained()
.with_style(theme.decline_button.container) .with_style(theme.decline_button.container)

View File

@ -11,8 +11,8 @@ enum Button {}
pub fn render_user_notification<V: View, A: Action + Clone>( pub fn render_user_notification<V: View, A: Action + Clone>(
user: Arc<User>, user: Arc<User>,
title: &str, title: &'static str,
body: Option<&str>, body: Option<&'static str>,
dismiss_action: A, dismiss_action: A,
buttons: Vec<(&'static str, Box<dyn Action>)>, buttons: Vec<(&'static str, Box<dyn Action>)>,
cx: &mut RenderContext<V>, cx: &mut RenderContext<V>,
@ -83,7 +83,7 @@ pub fn render_user_notification<V: View, A: Action + Clone>(
.named("contact notification header"), .named("contact notification header"),
) )
.with_children(body.map(|body| { .with_children(body.map(|body| {
Label::new(body.to_string(), theme.body_message.text.clone()) Label::new(body, theme.body_message.text.clone())
.contained() .contained()
.with_style(theme.body_message.container) .with_style(theme.body_message.container)
.boxed() .boxed()
@ -97,7 +97,7 @@ pub fn render_user_notification<V: View, A: Action + Clone>(
|(ix, (message, action))| { |(ix, (message, action))| {
MouseEventHandler::<Button>::new(ix, cx, |state, _| { MouseEventHandler::<Button>::new(ix, cx, |state, _| {
let button = theme.button.style_for(state, false); let button = theme.button.style_for(state, false);
Label::new(message.to_string(), button.text.clone()) Label::new(message, button.text.clone())
.contained() .contained()
.with_style(button.container) .with_style(button.container)
.boxed() .boxed()

View File

@ -175,7 +175,7 @@ impl ProjectSharedNotification {
.with_child( .with_child(
MouseEventHandler::<Open>::new(0, cx, |_, cx| { MouseEventHandler::<Open>::new(0, cx, |_, cx| {
let theme = &cx.global::<Settings>().theme.project_shared_notification; let theme = &cx.global::<Settings>().theme.project_shared_notification;
Label::new("Open".to_string(), theme.open_button.text.clone()) Label::new("Open", theme.open_button.text.clone())
.aligned() .aligned()
.contained() .contained()
.with_style(theme.open_button.container) .with_style(theme.open_button.container)
@ -194,7 +194,7 @@ impl ProjectSharedNotification {
.with_child( .with_child(
MouseEventHandler::<Dismiss>::new(0, cx, |_, cx| { MouseEventHandler::<Dismiss>::new(0, cx, |_, cx| {
let theme = &cx.global::<Settings>().theme.project_shared_notification; let theme = &cx.global::<Settings>().theme.project_shared_notification;
Label::new("Dismiss".to_string(), theme.dismiss_button.text.clone()) Label::new("Dismiss", theme.dismiss_button.text.clone())
.aligned() .aligned()
.contained() .contained()
.with_style(theme.dismiss_button.container) .with_style(theme.dismiss_button.container)

View File

@ -257,7 +257,7 @@ impl PickerDelegate for CommandPalette {
.filter_map(|(modifier, label)| { .filter_map(|(modifier, label)| {
if modifier { if modifier {
Some( Some(
Label::new(label.into(), key_style.label.clone()) Label::new(label, key_style.label.clone())
.contained() .contained()
.with_style(key_style.container) .with_style(key_style.container)
.boxed(), .boxed(),

View File

@ -90,14 +90,11 @@ impl View for ProjectDiagnosticsEditor {
fn render(&mut self, cx: &mut RenderContext<Self>) -> ElementBox { fn render(&mut self, cx: &mut RenderContext<Self>) -> ElementBox {
if self.path_states.is_empty() { if self.path_states.is_empty() {
let theme = &cx.global::<Settings>().theme.project_diagnostics; let theme = &cx.global::<Settings>().theme.project_diagnostics;
Label::new( Label::new("No problems in workspace", theme.empty_message.clone())
"No problems in workspace".to_string(), .aligned()
theme.empty_message.clone(), .contained()
) .with_style(theme.container)
.aligned() .boxed()
.contained()
.with_style(theme.container)
.boxed()
} else { } else {
ChildView::new(&self.editor, cx).boxed() ChildView::new(&self.editor, cx).boxed()
} }
@ -697,7 +694,7 @@ pub(crate) fn render_summary(
theme: &theme::ProjectDiagnostics, theme: &theme::ProjectDiagnostics,
) -> ElementBox { ) -> ElementBox {
if summary.error_count == 0 && summary.warning_count == 0 { if summary.error_count == 0 && summary.warning_count == 0 {
Label::new("No problems".to_string(), text_style.clone()).boxed() Label::new("No problems", text_style.clone()).boxed()
} else { } else {
let icon_width = theme.tab_icon_width; let icon_width = theme.tab_icon_width;
let icon_spacing = theme.tab_icon_spacing; let icon_spacing = theme.tab_icon_spacing;

View File

@ -178,14 +178,11 @@ impl View for DiagnosticIndicator {
if in_progress { if in_progress {
element.add_child( element.add_child(
Label::new( Label::new("Checking…", style.diagnostic_message.default.text.clone())
"Checking…".into(), .aligned()
style.diagnostic_message.default.text.clone(), .contained()
) .with_margin_left(item_spacing)
.aligned() .boxed(),
.contained()
.with_margin_left(item_spacing)
.boxed(),
); );
} else if let Some(diagnostic) = &self.current_diagnostic { } else if let Some(diagnostic) = &self.current_diagnostic {
let message_style = style.diagnostic_message.clone(); let message_style = style.diagnostic_message.clone();

View File

@ -1438,7 +1438,7 @@ impl EditorElement {
} else { } else {
let text_style = self.style.text.clone(); let text_style = self.style.text.clone();
Flex::row() Flex::row()
.with_child(Label::new("".to_string(), text_style).boxed()) .with_child(Label::new("", text_style).boxed())
.with_children(jump_icon) .with_children(jump_icon)
.contained() .contained()
.with_padding_left(gutter_padding) .with_padding_left(gutter_padding)

View File

@ -529,7 +529,7 @@ impl Item for Editor {
) -> ElementBox { ) -> ElementBox {
Flex::row() Flex::row()
.with_child( .with_child(
Label::new(self.title(cx).into(), style.label.clone()) Label::new(self.title(cx).to_string(), style.label.clone())
.aligned() .aligned()
.boxed(), .boxed(),
) )

View File

@ -25,11 +25,7 @@ impl View for DeployFeedbackButton {
let theme = &cx.global::<Settings>().theme; let theme = &cx.global::<Settings>().theme;
let theme = &theme.workspace.status_bar.feedback; let theme = &theme.workspace.status_bar.feedback;
Text::new( Text::new("Give Feedback", theme.style_for(state, true).clone()).boxed()
"Give Feedback".to_string(),
theme.style_for(state, true).clone(),
)
.boxed()
}) })
.with_cursor_style(CursorStyle::PointingHand) .with_cursor_style(CursorStyle::PointingHand)
.on_click(MouseButton::Left, |_, cx| cx.dispatch_action(GiveFeedback)) .on_click(MouseButton::Left, |_, cx| cx.dispatch_action(GiveFeedback))

View File

@ -247,7 +247,7 @@ impl Item for FeedbackEditor {
fn tab_content(&self, _: Option<usize>, style: &theme::Tab, _: &AppContext) -> ElementBox { fn tab_content(&self, _: Option<usize>, style: &theme::Tab, _: &AppContext) -> ElementBox {
Flex::row() Flex::row()
.with_child( .with_child(
Label::new("Feedback".to_string(), style.label.clone()) Label::new("Feedback", style.label.clone())
.aligned() .aligned()
.contained() .contained()
.boxed(), .boxed(),

View File

@ -30,7 +30,7 @@ impl View for FeedbackInfoText {
fn render(&mut self, cx: &mut RenderContext<Self>) -> ElementBox { fn render(&mut self, cx: &mut RenderContext<Self>) -> ElementBox {
let theme = cx.global::<Settings>().theme.clone(); let theme = cx.global::<Settings>().theme.clone();
let text = "We read whatever you submit here. For issues and discussions, visit the community repo on GitHub."; let text = "We read whatever you submit here. For issues and discussions, visit the community repo on GitHub.";
Label::new(text.to_string(), theme.feedback.info_text.text.clone()) Label::new(text, theme.feedback.info_text.text.clone())
.contained() .contained()
.aligned() .aligned()
.left() .left()

View File

@ -34,7 +34,7 @@ impl View for SubmitFeedbackButton {
enum SubmitFeedbackButton {} enum SubmitFeedbackButton {}
MouseEventHandler::<SubmitFeedbackButton>::new(0, cx, |state, _| { MouseEventHandler::<SubmitFeedbackButton>::new(0, cx, |state, _| {
let style = theme.feedback.submit_button.style_for(state, false); let style = theme.feedback.submit_button.style_for(state, false);
Label::new("Submit as Markdown".into(), style.text.clone()) Label::new("Submit as Markdown", style.text.clone())
.contained() .contained()
.with_style(style.container) .with_style(style.container)
.boxed() .boxed()

View File

@ -1,4 +1,4 @@
use std::ops::Range; use std::{borrow::Cow, ops::Range};
use crate::{ use crate::{
fonts::TextStyle, fonts::TextStyle,
@ -16,7 +16,7 @@ use serde_json::json;
use smallvec::{smallvec, SmallVec}; use smallvec::{smallvec, SmallVec};
pub struct Label { pub struct Label {
text: String, text: Cow<'static, str>,
style: LabelStyle, style: LabelStyle,
highlight_indices: Vec<usize>, highlight_indices: Vec<usize>,
} }
@ -44,9 +44,9 @@ impl LabelStyle {
} }
impl Label { impl Label {
pub fn new(text: String, style: impl Into<LabelStyle>) -> Self { pub fn new<I: Into<Cow<'static, str>>>(text: I, style: impl Into<LabelStyle>) -> Self {
Self { Self {
text, text: text.into(),
highlight_indices: Default::default(), highlight_indices: Default::default(),
style: style.into(), style: style.into(),
} }
@ -138,11 +138,9 @@ impl Element for Label {
cx: &mut LayoutContext, cx: &mut LayoutContext,
) -> (Vector2F, Self::LayoutState) { ) -> (Vector2F, Self::LayoutState) {
let runs = self.compute_runs(); let runs = self.compute_runs();
let line = cx.text_layout_cache.layout_str( let line =
self.text.as_str(), cx.text_layout_cache
self.style.text.font_size, .layout_str(&self.text, self.style.text.font_size, runs.as_slice());
runs.as_slice(),
);
let size = vec2f( let size = vec2f(
line.width() line.width()

View File

@ -15,7 +15,7 @@ use serde_json::json;
use std::{borrow::Cow, ops::Range, sync::Arc}; use std::{borrow::Cow, ops::Range, sync::Arc};
pub struct Text { pub struct Text {
text: String, text: Cow<'static, str>,
style: TextStyle, style: TextStyle,
soft_wrap: bool, soft_wrap: bool,
highlights: Vec<(Range<usize>, HighlightStyle)>, highlights: Vec<(Range<usize>, HighlightStyle)>,
@ -28,9 +28,9 @@ pub struct LayoutState {
} }
impl Text { impl Text {
pub fn new(text: String, style: TextStyle) -> Self { pub fn new<I: Into<Cow<'static, str>>>(text: I, style: TextStyle) -> Self {
Self { Self {
text, text: text.into(),
style, style,
soft_wrap: true, soft_wrap: true,
highlights: Vec::new(), highlights: Vec::new(),
@ -280,7 +280,7 @@ mod tests {
let (window_id, _) = cx.add_window(Default::default(), |_| TestView); let (window_id, _) = cx.add_window(Default::default(), |_| TestView);
let mut presenter = cx.build_presenter(window_id, Default::default(), Default::default()); let mut presenter = cx.build_presenter(window_id, Default::default(), Default::default());
fonts::with_font_cache(cx.font_cache().clone(), || { fonts::with_font_cache(cx.font_cache().clone(), || {
let mut text = Text::new("Hello\r\n".into(), Default::default()).with_soft_wrap(true); let mut text = Text::new("Hello\r\n", Default::default()).with_soft_wrap(true);
let (_, state) = text.layout( let (_, state) = text.layout(
SizeConstraint::new(Default::default(), vec2f(f32::INFINITY, f32::INFINITY)), SizeConstraint::new(Default::default(), vec2f(f32::INFINITY, f32::INFINITY)),
&mut presenter.build_layout_context(Default::default(), false, cx), &mut presenter.build_layout_context(Default::default(), false, cx),

View File

@ -80,7 +80,7 @@ impl<D: PickerDelegate> View for Picker<D> {
None None
} else { } else {
Some( Some(
Label::new("No matches".into(), theme.no_matches.label.clone()) Label::new("No matches", theme.no_matches.label.clone())
.contained() .contained()
.with_style(theme.no_matches.container) .with_style(theme.no_matches.container)
.boxed(), .boxed(),

View File

@ -319,7 +319,7 @@ impl BufferSearchBar {
fn render_search_option( fn render_search_option(
&self, &self,
option_supported: bool, option_supported: bool,
icon: &str, icon: &'static str,
option: SearchOption, option: SearchOption,
cx: &mut RenderContext<Self>, cx: &mut RenderContext<Self>,
) -> Option<ElementBox> { ) -> Option<ElementBox> {
@ -337,7 +337,7 @@ impl BufferSearchBar {
.search .search
.option_button .option_button
.style_for(state, is_active); .style_for(state, is_active);
Label::new(icon.to_string(), style.text.clone()) Label::new(icon, style.text.clone())
.contained() .contained()
.with_style(style.container) .with_style(style.container)
.boxed() .boxed()
@ -359,7 +359,7 @@ impl BufferSearchBar {
fn render_nav_button( fn render_nav_button(
&self, &self,
icon: &str, icon: &'static str,
direction: Direction, direction: Direction,
cx: &mut RenderContext<Self>, cx: &mut RenderContext<Self>,
) -> ElementBox { ) -> ElementBox {
@ -385,7 +385,7 @@ impl BufferSearchBar {
.search .search
.option_button .option_button
.style_for(state, false); .style_for(state, false);
Label::new(icon.to_string(), style.text.clone()) Label::new(icon, style.text.clone())
.contained() .contained()
.with_style(style.container) .with_style(style.container)
.boxed() .boxed()

View File

@ -189,7 +189,7 @@ impl View for ProjectSearchView {
"No results" "No results"
}; };
MouseEventHandler::<Status>::new(0, cx, |_, _| { MouseEventHandler::<Status>::new(0, cx, |_, _| {
Label::new(text.to_string(), theme.search.results_status.clone()) Label::new(text, theme.search.results_status.clone())
.aligned() .aligned()
.contained() .contained()
.with_background_color(theme.editor.background) .with_background_color(theme.editor.background)
@ -744,7 +744,7 @@ impl ProjectSearchBar {
fn render_nav_button( fn render_nav_button(
&self, &self,
icon: &str, icon: &'static str,
direction: Direction, direction: Direction,
cx: &mut RenderContext<Self>, cx: &mut RenderContext<Self>,
) -> ElementBox { ) -> ElementBox {
@ -770,7 +770,7 @@ impl ProjectSearchBar {
.search .search
.option_button .option_button
.style_for(state, false); .style_for(state, false);
Label::new(icon.to_string(), style.text.clone()) Label::new(icon, style.text.clone())
.contained() .contained()
.with_style(style.container) .with_style(style.container)
.boxed() .boxed()
@ -792,7 +792,7 @@ impl ProjectSearchBar {
fn render_option_button( fn render_option_button(
&self, &self,
icon: &str, icon: &'static str,
option: SearchOption, option: SearchOption,
cx: &mut RenderContext<Self>, cx: &mut RenderContext<Self>,
) -> ElementBox { ) -> ElementBox {
@ -805,7 +805,7 @@ impl ProjectSearchBar {
.search .search
.option_button .option_button
.style_for(state, is_active); .style_for(state, is_active);
Label::new(icon.to_string(), style.text.clone()) Label::new(icon, style.text.clone())
.contained() .contained()
.with_style(style.container) .with_style(style.container)
.boxed() .boxed()

View File

@ -680,7 +680,7 @@ impl Item for TerminalView {
fn breadcrumbs(&self, theme: &theme::Theme, cx: &AppContext) -> Option<Vec<ElementBox>> { fn breadcrumbs(&self, theme: &theme::Theme, cx: &AppContext) -> Option<Vec<ElementBox>> {
Some(vec![Text::new( Some(vec![Text::new(
self.terminal().read(cx).breadcrumb_text.to_string(), self.terminal().read(cx).breadcrumb_text.clone(),
theme.breadcrumbs.text.clone(), theme.breadcrumbs.text.clone(),
) )
.boxed()]) .boxed()])

View File

@ -308,7 +308,7 @@ impl Item for ThemeTestbench {
style: &theme::Tab, style: &theme::Tab,
_: &AppContext, _: &AppContext,
) -> gpui::ElementBox { ) -> gpui::ElementBox {
Label::new("Theme Testbench".into(), style.label.clone()) Label::new("Theme Testbench", style.label.clone())
.aligned() .aligned()
.contained() .contained()
.boxed() .boxed()

View File

@ -1958,7 +1958,7 @@ impl Workspace {
MouseEventHandler::<DisconnectedOverlay>::new(0, cx, |_, cx| { MouseEventHandler::<DisconnectedOverlay>::new(0, cx, |_, cx| {
let theme = &cx.global::<Settings>().theme; let theme = &cx.global::<Settings>().theme;
Label::new( Label::new(
"Your connection to the remote project has been lost.".to_string(), "Your connection to the remote project has been lost.",
theme.workspace.disconnected_overlay.text.clone(), theme.workspace.disconnected_overlay.text.clone(),
) )
.aligned() .aligned()