chore: use Cow instead of String for tooltips (#2838)

A QoL change to align `Tooltip` with other elements like `Label`
Release Notes:

- N/A
This commit is contained in:
Piotr Osiewicz 2023-08-11 15:29:55 +02:00 committed by GitHub
parent 268f4b1939
commit ffffbbea1f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
13 changed files with 33 additions and 43 deletions

View file

@ -362,7 +362,7 @@ impl AssistantPanel {
this.set_active_editor_index(this.prev_active_editor_index, cx); this.set_active_editor_index(this.prev_active_editor_index, cx);
} }
}) })
.with_tooltip::<History>(1, "History".into(), None, tooltip_style, cx) .with_tooltip::<History>(1, "History", None, tooltip_style, cx)
} }
fn render_editor_tools(&self, cx: &mut ViewContext<Self>) -> Vec<AnyElement<Self>> { fn render_editor_tools(&self, cx: &mut ViewContext<Self>) -> Vec<AnyElement<Self>> {
@ -394,7 +394,7 @@ impl AssistantPanel {
}) })
.with_tooltip::<Split>( .with_tooltip::<Split>(
1, 1,
"Split Message".into(), "Split Message",
Some(Box::new(Split)), Some(Box::new(Split)),
tooltip_style, tooltip_style,
cx, cx,
@ -416,13 +416,7 @@ impl AssistantPanel {
active_editor.update(cx, |editor, cx| editor.assist(&Default::default(), cx)); active_editor.update(cx, |editor, cx| editor.assist(&Default::default(), cx));
} }
}) })
.with_tooltip::<Assist>( .with_tooltip::<Assist>(1, "Assist", Some(Box::new(Assist)), tooltip_style, cx)
1,
"Assist".into(),
Some(Box::new(Assist)),
tooltip_style,
cx,
)
} }
fn render_quote_button(cx: &mut ViewContext<Self>) -> impl Element<Self> { fn render_quote_button(cx: &mut ViewContext<Self>) -> impl Element<Self> {
@ -446,7 +440,7 @@ impl AssistantPanel {
}) })
.with_tooltip::<QuoteSelection>( .with_tooltip::<QuoteSelection>(
1, 1,
"Quote Selection".into(), "Quote Selection",
Some(Box::new(QuoteSelection)), Some(Box::new(QuoteSelection)),
tooltip_style, tooltip_style,
cx, cx,
@ -468,7 +462,7 @@ impl AssistantPanel {
}) })
.with_tooltip::<NewConversation>( .with_tooltip::<NewConversation>(
1, 1,
"New Conversation".into(), "New Conversation",
Some(Box::new(NewConversation)), Some(Box::new(NewConversation)),
tooltip_style, tooltip_style,
cx, cx,
@ -498,11 +492,7 @@ impl AssistantPanel {
}) })
.with_tooltip::<ToggleZoom>( .with_tooltip::<ToggleZoom>(
0, 0,
if self.zoomed { if self.zoomed { "Zoom Out" } else { "Zoom In" },
"Zoom Out".into()
} else {
"Zoom In".into()
},
Some(Box::new(ToggleZoom)), Some(Box::new(ToggleZoom)),
tooltip_style, tooltip_style,
cx, cx,

View file

@ -238,7 +238,7 @@ impl CollabTitlebarItem {
.left() .left()
.with_tooltip::<RecentProjectsTooltip>( .with_tooltip::<RecentProjectsTooltip>(
0, 0,
"Recent projects".into(), "Recent projects",
Some(Box::new(recent_projects::OpenRecent)), Some(Box::new(recent_projects::OpenRecent)),
theme.tooltip.clone(), theme.tooltip.clone(),
cx, cx,
@ -282,7 +282,7 @@ impl CollabTitlebarItem {
.left() .left()
.with_tooltip::<BranchPopoverTooltip>( .with_tooltip::<BranchPopoverTooltip>(
0, 0,
"Recent branches".into(), "Recent branches",
Some(Box::new(ToggleVcsMenu)), Some(Box::new(ToggleVcsMenu)),
theme.tooltip.clone(), theme.tooltip.clone(),
cx, cx,
@ -582,7 +582,7 @@ impl CollabTitlebarItem {
}) })
.with_tooltip::<ToggleContactsMenu>( .with_tooltip::<ToggleContactsMenu>(
0, 0,
"Show contacts menu".into(), "Show contacts menu",
Some(Box::new(ToggleContactsMenu)), Some(Box::new(ToggleContactsMenu)),
theme.tooltip.clone(), theme.tooltip.clone(),
cx, cx,
@ -633,7 +633,7 @@ impl CollabTitlebarItem {
}) })
.with_tooltip::<ToggleScreenSharing>( .with_tooltip::<ToggleScreenSharing>(
0, 0,
tooltip.into(), tooltip,
Some(Box::new(ToggleScreenSharing)), Some(Box::new(ToggleScreenSharing)),
theme.tooltip.clone(), theme.tooltip.clone(),
cx, cx,
@ -686,7 +686,7 @@ impl CollabTitlebarItem {
}) })
.with_tooltip::<ToggleMute>( .with_tooltip::<ToggleMute>(
0, 0,
tooltip.into(), tooltip,
Some(Box::new(ToggleMute)), Some(Box::new(ToggleMute)),
theme.tooltip.clone(), theme.tooltip.clone(),
cx, cx,
@ -734,7 +734,7 @@ impl CollabTitlebarItem {
}) })
.with_tooltip::<ToggleDeafen>( .with_tooltip::<ToggleDeafen>(
0, 0,
tooltip.into(), tooltip,
Some(Box::new(ToggleDeafen)), Some(Box::new(ToggleDeafen)),
theme.tooltip.clone(), theme.tooltip.clone(),
cx, cx,
@ -768,7 +768,7 @@ impl CollabTitlebarItem {
}) })
.with_tooltip::<LeaveCall>( .with_tooltip::<LeaveCall>(
0, 0,
tooltip.into(), tooltip,
Some(Box::new(LeaveCall)), Some(Box::new(LeaveCall)),
theme.tooltip.clone(), theme.tooltip.clone(),
cx, cx,

View file

@ -1345,7 +1345,7 @@ impl View for ContactList {
}) })
.with_tooltip::<AddContact>( .with_tooltip::<AddContact>(
0, 0,
"Search for new contact".into(), "Search for new contact",
None, None,
theme.tooltip.clone(), theme.tooltip.clone(),
cx, cx,

View file

@ -140,7 +140,7 @@ impl View for CopilotButton {
}) })
.with_tooltip::<Self>( .with_tooltip::<Self>(
0, 0,
"GitHub Copilot".into(), "GitHub Copilot",
None, None,
theme.tooltip.clone(), theme.tooltip.clone(),
cx, cx,

View file

@ -173,7 +173,7 @@ impl View for DiagnosticIndicator {
}) })
.with_tooltip::<Summary>( .with_tooltip::<Summary>(
0, 0,
"Project Diagnostics".to_string(), "Project Diagnostics",
Some(Box::new(crate::Deploy)), Some(Box::new(crate::Deploy)),
tooltip_style, tooltip_style,
cx, cx,

View file

@ -8685,7 +8685,7 @@ pub fn diagnostic_block_renderer(diagnostic: Diagnostic, is_valid: bool) -> Rend
// We really need to rethink this ID system... // We really need to rethink this ID system...
.with_tooltip::<BlockContextToolip>( .with_tooltip::<BlockContextToolip>(
cx.block_id, cx.block_id,
"Copy diagnostic message".to_string(), "Copy diagnostic message",
None, None,
tooltip_style, tooltip_style,
cx, cx,

View file

@ -66,7 +66,7 @@ impl View for DeployFeedbackButton {
}) })
.with_tooltip::<Self>( .with_tooltip::<Self>(
0, 0,
"Send Feedback".into(), "Send Feedback",
Some(Box::new(GiveFeedback)), Some(Box::new(GiveFeedback)),
theme.tooltip.clone(), theme.tooltip.clone(),
cx, cx,

View file

@ -80,7 +80,7 @@ impl View for SubmitFeedbackButton {
.with_margin_left(theme.feedback.button_margin) .with_margin_left(theme.feedback.button_margin)
.with_tooltip::<Self>( .with_tooltip::<Self>(
0, 0,
"cmd-s".into(), "cmd-s",
Some(Box::new(SubmitFeedback)), Some(Box::new(SubmitFeedback)),
theme.tooltip.clone(), theme.tooltip.clone(),
cx, cx,

View file

@ -170,7 +170,7 @@ pub trait Element<V: View>: 'static {
fn with_tooltip<Tag: 'static>( fn with_tooltip<Tag: 'static>(
self, self,
id: usize, id: usize,
text: String, text: impl Into<Cow<'static, str>>,
action: Option<Box<dyn Action>>, action: Option<Box<dyn Action>>,
style: TooltipStyle, style: TooltipStyle,
cx: &mut ViewContext<V>, cx: &mut ViewContext<V>,
@ -178,7 +178,7 @@ pub trait Element<V: View>: 'static {
where where
Self: 'static + Sized, Self: 'static + Sized,
{ {
Tooltip::new::<Tag, V>(id, text, action, style, self.into_any(), cx) Tooltip::new::<Tag>(id, text, action, style, self.into_any(), cx)
} }
fn resizable( fn resizable(

View file

@ -12,6 +12,7 @@ use crate::{
use schemars::JsonSchema; use schemars::JsonSchema;
use serde::Deserialize; use serde::Deserialize;
use std::{ use std::{
borrow::Cow,
cell::{Cell, RefCell}, cell::{Cell, RefCell},
ops::Range, ops::Range,
rc::Rc, rc::Rc,
@ -52,9 +53,9 @@ pub struct KeystrokeStyle {
} }
impl<V: View> Tooltip<V> { impl<V: View> Tooltip<V> {
pub fn new<Tag: 'static, T: View>( pub fn new<Tag: 'static>(
id: usize, id: usize,
text: String, text: impl Into<Cow<'static, str>>,
action: Option<Box<dyn Action>>, action: Option<Box<dyn Action>>,
style: TooltipStyle, style: TooltipStyle,
child: AnyElement<V>, child: AnyElement<V>,
@ -66,6 +67,8 @@ impl<V: View> Tooltip<V> {
let state_handle = cx.default_element_state::<ElementState<Tag>, Rc<TooltipState>>(id); let state_handle = cx.default_element_state::<ElementState<Tag>, Rc<TooltipState>>(id);
let state = state_handle.read(cx).clone(); let state = state_handle.read(cx).clone();
let text = text.into();
let tooltip = if state.visible.get() { let tooltip = if state.visible.get() {
let mut collapsed_tooltip = Self::render_tooltip( let mut collapsed_tooltip = Self::render_tooltip(
focused_view_id, focused_view_id,
@ -127,7 +130,7 @@ impl<V: View> Tooltip<V> {
pub fn render_tooltip( pub fn render_tooltip(
focused_view_id: Option<usize>, focused_view_id: Option<usize>,
text: String, text: impl Into<Cow<'static, str>>,
style: TooltipStyle, style: TooltipStyle,
action: Option<Box<dyn Action>>, action: Option<Box<dyn Action>>,
measure: bool, measure: bool,

View file

@ -72,10 +72,7 @@ impl TerminalPanel {
0, 0,
"icons/plus_12.svg", "icons/plus_12.svg",
false, false,
Some(( Some(("New Terminal", Some(Box::new(workspace::NewTerminal)))),
"New Terminal".into(),
Some(Box::new(workspace::NewTerminal)),
)),
cx, cx,
move |_, cx| { move |_, cx| {
let this = this.clone(); let this = this.clone();

View file

@ -303,10 +303,10 @@ impl Pane {
let tooltip_label; let tooltip_label;
if pane.is_zoomed() { if pane.is_zoomed() {
icon_path = "icons/minimize_8.svg"; icon_path = "icons/minimize_8.svg";
tooltip_label = "Zoom In".into(); tooltip_label = "Zoom In";
} else { } else {
icon_path = "icons/maximize_8.svg"; icon_path = "icons/maximize_8.svg";
tooltip_label = "Zoom In".into(); tooltip_label = "Zoom In";
} }
Pane::render_tab_bar_button( Pane::render_tab_bar_button(
@ -1477,7 +1477,7 @@ impl Pane {
index: usize, index: usize,
icon: &'static str, icon: &'static str,
is_active: bool, is_active: bool,
tooltip: Option<(String, Option<Box<dyn Action>>)>, tooltip: Option<(&'static str, Option<Box<dyn Action>>)>,
cx: &mut ViewContext<Pane>, cx: &mut ViewContext<Pane>,
on_click: F1, on_click: F1,
on_down: F2, on_down: F2,

View file

@ -220,7 +220,7 @@ fn nav_button<A: Action, F: 'static + Fn(&mut Toolbar, &mut ViewContext<Toolbar>
spacing: f32, spacing: f32,
on_click: F, on_click: F,
tooltip_action: A, tooltip_action: A,
action_name: &str, action_name: &'static str,
cx: &mut ViewContext<Toolbar>, cx: &mut ViewContext<Toolbar>,
) -> AnyElement<Toolbar> { ) -> AnyElement<Toolbar> {
MouseEventHandler::<A, _>::new(0, cx, |state, _| { MouseEventHandler::<A, _>::new(0, cx, |state, _| {
@ -252,7 +252,7 @@ fn nav_button<A: Action, F: 'static + Fn(&mut Toolbar, &mut ViewContext<Toolbar>
}) })
.with_tooltip::<A>( .with_tooltip::<A>(
0, 0,
action_name.to_string(), action_name,
Some(Box::new(tooltip_action)), Some(Box::new(tooltip_action)),
tooltip_style, tooltip_style,
cx, cx,