Merge branch 'main' into feedback-2

This commit is contained in:
Joseph T. Lyons
2023-12-06 23:16:54 -05:00
114 changed files with 12427 additions and 4903 deletions
@@ -359,11 +359,7 @@ impl RenderOnce for ButtonLike {
},
)
.when_some(self.tooltip, |this, tooltip| {
if !self.selected {
this.tooltip(move |cx| tooltip(cx))
} else {
this
}
this.tooltip(move |cx| tooltip(cx))
})
.children(self.children)
}
@@ -1,4 +1,4 @@
use gpui::{Action, AnyView, DefiniteLength};
use gpui::{AnyView, DefiniteLength};
use crate::prelude::*;
use crate::{ButtonCommon, ButtonLike, ButtonSize, ButtonStyle, Icon, IconSize};
@@ -39,10 +39,6 @@ impl IconButton {
self.selected_icon = icon.into();
self
}
pub fn action(self, action: Box<dyn Action>) -> Self {
self.on_click(move |_event, cx| cx.dispatch_action(action.boxed_clone()))
}
}
impl Disableable for IconButton {
+18 -7
View File
@@ -1,15 +1,26 @@
use gpui::{rems, svg, IntoElement, Svg};
use gpui::{rems, svg, IntoElement, Rems, Svg};
use strum::EnumIter;
use crate::prelude::*;
#[derive(Default, PartialEq, Copy, Clone)]
pub enum IconSize {
XSmall,
Small,
#[default]
Medium,
}
impl IconSize {
pub fn rems(self) -> Rems {
match self {
IconSize::XSmall => rems(12. / 16.),
IconSize::Small => rems(14. / 16.),
IconSize::Medium => rems(16. / 16.),
}
}
}
#[derive(Debug, PartialEq, Copy, Clone, EnumIter)]
pub enum Icon {
Ai,
@@ -81,6 +92,8 @@ pub enum Icon {
Shift,
Option,
Return,
Update,
ZedXCopilot,
}
impl Icon {
@@ -109,6 +122,7 @@ impl Icon {
Icon::Close => "icons/x.svg",
Icon::Collab => "icons/user_group_16.svg",
Icon::Copilot => "icons/copilot.svg",
Icon::CopilotInit => "icons/copilot_init.svg",
Icon::CopilotError => "icons/copilot_error.svg",
Icon::CopilotDisabled => "icons/copilot_disabled.svg",
@@ -155,6 +169,8 @@ impl Icon {
Icon::Shift => "icons/shift.svg",
Icon::Option => "icons/option.svg",
Icon::Return => "icons/return.svg",
Icon::Update => "icons/update.svg",
Icon::ZedXCopilot => "icons/zed_x_copilot.svg",
}
}
}
@@ -170,13 +186,8 @@ impl RenderOnce for IconElement {
type Rendered = Svg;
fn render(self, cx: &mut WindowContext) -> Self::Rendered {
let svg_size = match self.size {
IconSize::Small => rems(14. / 16.),
IconSize::Medium => rems(16. / 16.),
};
svg()
.size(svg_size)
.size(self.size.rems())
.flex_none()
.path(self.path)
.text_color(self.color.color(cx))
+60
View File
@@ -0,0 +1,60 @@
use gpui::{Div, Position};
use crate::prelude::*;
#[derive(Default)]
pub enum IndicatorStyle {
#[default]
Dot,
Bar,
}
#[derive(IntoElement)]
pub struct Indicator {
position: Position,
style: IndicatorStyle,
color: Color,
}
impl Indicator {
pub fn dot() -> Self {
Self {
position: Position::Relative,
style: IndicatorStyle::Dot,
color: Color::Default,
}
}
pub fn bar() -> Self {
Self {
position: Position::Relative,
style: IndicatorStyle::Dot,
color: Color::Default,
}
}
pub fn color(mut self, color: Color) -> Self {
self.color = color;
self
}
pub fn absolute(mut self) -> Self {
self.position = Position::Absolute;
self
}
}
impl RenderOnce for Indicator {
type Rendered = Div;
fn render(self, cx: &mut WindowContext) -> Self::Rendered {
div()
.flex_none()
.map(|this| match self.style {
IndicatorStyle::Dot => this.w_1p5().h_1p5().rounded_full(),
IndicatorStyle::Bar => this.w_full().h_1p5().rounded_t_md(),
})
.when(self.position == Position::Absolute, |this| this.absolute())
.bg(self.color.color(cx))
}
}
+6 -179
View File
@@ -1,180 +1,7 @@
use std::ops::Range;
mod highlighted_label;
mod label;
mod label_like;
use crate::prelude::*;
use crate::styled_ext::StyledExt;
use gpui::{relative, Div, HighlightStyle, IntoElement, StyledText, WindowContext};
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Clone, Copy, Default)]
pub enum LabelSize {
#[default]
Default,
Small,
}
#[derive(Default, PartialEq, Copy, Clone)]
pub enum LineHeightStyle {
#[default]
TextLabel,
/// Sets the line height to 1
UILabel,
}
#[derive(IntoElement, Clone)]
pub struct Label {
label: SharedString,
size: LabelSize,
line_height_style: LineHeightStyle,
color: Color,
strikethrough: bool,
}
impl RenderOnce for Label {
type Rendered = Div;
fn render(self, cx: &mut WindowContext) -> Self::Rendered {
div()
.when(self.strikethrough, |this| {
this.relative().child(
div()
.absolute()
.top_1_2()
.w_full()
.h_px()
.bg(Color::Hidden.color(cx)),
)
})
.map(|this| match self.size {
LabelSize::Default => this.text_ui(),
LabelSize::Small => this.text_ui_sm(),
})
.when(self.line_height_style == LineHeightStyle::UILabel, |this| {
this.line_height(relative(1.))
})
.text_color(self.color.color(cx))
.child(self.label.clone())
}
}
impl Label {
pub fn new(label: impl Into<SharedString>) -> Self {
Self {
label: label.into(),
size: LabelSize::Default,
line_height_style: LineHeightStyle::default(),
color: Color::Default,
strikethrough: false,
}
}
pub fn size(mut self, size: LabelSize) -> Self {
self.size = size;
self
}
pub fn color(mut self, color: Color) -> Self {
self.color = color;
self
}
pub fn line_height_style(mut self, line_height_style: LineHeightStyle) -> Self {
self.line_height_style = line_height_style;
self
}
pub fn set_strikethrough(mut self, strikethrough: bool) -> Self {
self.strikethrough = strikethrough;
self
}
}
#[derive(IntoElement)]
pub struct HighlightedLabel {
label: SharedString,
size: LabelSize,
color: Color,
highlight_indices: Vec<usize>,
strikethrough: bool,
}
impl RenderOnce for HighlightedLabel {
type Rendered = Div;
fn render(self, cx: &mut WindowContext) -> Self::Rendered {
let highlight_color = cx.theme().colors().text_accent;
let mut highlight_indices = self.highlight_indices.iter().copied().peekable();
let mut highlights: Vec<(Range<usize>, HighlightStyle)> = Vec::new();
while let Some(start_ix) = highlight_indices.next() {
let mut end_ix = start_ix;
loop {
end_ix = end_ix + self.label[end_ix..].chars().next().unwrap().len_utf8();
if let Some(&next_ix) = highlight_indices.peek() {
if next_ix == end_ix {
end_ix = next_ix;
highlight_indices.next();
continue;
}
}
break;
}
highlights.push((
start_ix..end_ix,
HighlightStyle {
color: Some(highlight_color),
..Default::default()
},
));
}
div()
.flex()
.when(self.strikethrough, |this| {
this.relative().child(
div()
.absolute()
.top_px()
.my_auto()
.w_full()
.h_px()
.bg(Color::Hidden.color(cx)),
)
})
.map(|this| match self.size {
LabelSize::Default => this.text_ui(),
LabelSize::Small => this.text_ui_sm(),
})
.child(StyledText::new(self.label).with_highlights(&cx.text_style(), highlights))
}
}
impl HighlightedLabel {
/// shows a label with the given characters highlighted.
/// characters are identified by utf8 byte position.
pub fn new(label: impl Into<SharedString>, highlight_indices: Vec<usize>) -> Self {
Self {
label: label.into(),
size: LabelSize::Default,
color: Color::Default,
highlight_indices,
strikethrough: false,
}
}
pub fn size(mut self, size: LabelSize) -> Self {
self.size = size;
self
}
pub fn color(mut self, color: Color) -> Self {
self.color = color;
self
}
pub fn set_strikethrough(mut self, strikethrough: bool) -> Self {
self.strikethrough = strikethrough;
self
}
}
pub use highlighted_label::*;
pub use label::*;
pub use label_like::*;
@@ -0,0 +1,86 @@
use std::ops::Range;
use gpui::{HighlightStyle, StyledText};
use crate::{prelude::*, LabelCommon, LabelLike, LabelSize, LineHeightStyle};
#[derive(IntoElement)]
pub struct HighlightedLabel {
base: LabelLike,
label: SharedString,
highlight_indices: Vec<usize>,
}
impl HighlightedLabel {
/// Constructs a label with the given characters highlighted.
/// Characters are identified by UTF-8 byte position.
pub fn new(label: impl Into<SharedString>, highlight_indices: Vec<usize>) -> Self {
Self {
base: LabelLike::new(),
label: label.into(),
highlight_indices,
}
}
}
impl LabelCommon for HighlightedLabel {
fn size(mut self, size: LabelSize) -> Self {
self.base = self.base.size(size);
self
}
fn line_height_style(mut self, line_height_style: LineHeightStyle) -> Self {
self.base = self.base.line_height_style(line_height_style);
self
}
fn color(mut self, color: Color) -> Self {
self.base = self.base.color(color);
self
}
fn strikethrough(mut self, strikethrough: bool) -> Self {
self.base = self.base.strikethrough(strikethrough);
self
}
}
impl RenderOnce for HighlightedLabel {
type Rendered = LabelLike;
fn render(self, cx: &mut WindowContext) -> Self::Rendered {
let highlight_color = cx.theme().colors().text_accent;
let mut highlight_indices = self.highlight_indices.iter().copied().peekable();
let mut highlights: Vec<(Range<usize>, HighlightStyle)> = Vec::new();
while let Some(start_ix) = highlight_indices.next() {
let mut end_ix = start_ix;
loop {
end_ix = end_ix + self.label[end_ix..].chars().next().unwrap().len_utf8();
if let Some(&next_ix) = highlight_indices.peek() {
if next_ix == end_ix {
end_ix = next_ix;
highlight_indices.next();
continue;
}
}
break;
}
highlights.push((
start_ix..end_ix,
HighlightStyle {
color: Some(highlight_color),
..Default::default()
},
));
}
let mut text_style = cx.text_style().clone();
text_style.color = self.base.color.color(cx);
LabelLike::new().child(StyledText::new(self.label).with_highlights(&text_style, highlights))
}
}
+48
View File
@@ -0,0 +1,48 @@
use gpui::WindowContext;
use crate::{prelude::*, LabelCommon, LabelLike, LabelSize, LineHeightStyle};
#[derive(IntoElement)]
pub struct Label {
base: LabelLike,
label: SharedString,
}
impl Label {
pub fn new(label: impl Into<SharedString>) -> Self {
Self {
base: LabelLike::new(),
label: label.into(),
}
}
}
impl LabelCommon for Label {
fn size(mut self, size: LabelSize) -> Self {
self.base = self.base.size(size);
self
}
fn line_height_style(mut self, line_height_style: LineHeightStyle) -> Self {
self.base = self.base.line_height_style(line_height_style);
self
}
fn color(mut self, color: Color) -> Self {
self.base = self.base.color(color);
self
}
fn strikethrough(mut self, strikethrough: bool) -> Self {
self.base = self.base.strikethrough(strikethrough);
self
}
}
impl RenderOnce for Label {
type Rendered = LabelLike;
fn render(self, _cx: &mut WindowContext) -> Self::Rendered {
self.base.child(self.label)
}
}
@@ -0,0 +1,102 @@
use gpui::{relative, AnyElement, Div, Styled};
use smallvec::SmallVec;
use crate::prelude::*;
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Clone, Copy, Default)]
pub enum LabelSize {
#[default]
Default,
Small,
}
#[derive(Default, PartialEq, Copy, Clone)]
pub enum LineHeightStyle {
#[default]
TextLabel,
/// Sets the line height to 1
UILabel,
}
pub trait LabelCommon {
fn size(self, size: LabelSize) -> Self;
fn line_height_style(self, line_height_style: LineHeightStyle) -> Self;
fn color(self, color: Color) -> Self;
fn strikethrough(self, strikethrough: bool) -> Self;
}
#[derive(IntoElement)]
pub struct LabelLike {
size: LabelSize,
line_height_style: LineHeightStyle,
pub(crate) color: Color,
strikethrough: bool,
children: SmallVec<[AnyElement; 2]>,
}
impl LabelLike {
pub fn new() -> Self {
Self {
size: LabelSize::Default,
line_height_style: LineHeightStyle::default(),
color: Color::Default,
strikethrough: false,
children: SmallVec::new(),
}
}
}
impl LabelCommon for LabelLike {
fn size(mut self, size: LabelSize) -> Self {
self.size = size;
self
}
fn line_height_style(mut self, line_height_style: LineHeightStyle) -> Self {
self.line_height_style = line_height_style;
self
}
fn color(mut self, color: Color) -> Self {
self.color = color;
self
}
fn strikethrough(mut self, strikethrough: bool) -> Self {
self.strikethrough = strikethrough;
self
}
}
impl ParentElement for LabelLike {
fn children_mut(&mut self) -> &mut SmallVec<[AnyElement; 2]> {
&mut self.children
}
}
impl RenderOnce for LabelLike {
type Rendered = Div;
fn render(self, cx: &mut WindowContext) -> Self::Rendered {
div()
.when(self.strikethrough, |this| {
this.relative().child(
div()
.absolute()
.top_1_2()
.w_full()
.h_px()
.bg(Color::Hidden.color(cx)),
)
})
.map(|this| match self.size {
LabelSize::Default => this.text_ui(),
LabelSize::Small => this.text_ui_sm(),
})
.when(self.line_height_style == LineHeightStyle::UILabel, |this| {
this.line_height(relative(1.))
})
.text_color(self.color.color(cx))
.children(self.children)
}
}
@@ -51,5 +51,13 @@ impl Render for IconButtonStory {
.tooltip(|cx| Tooltip::text("Open messages", cx)),
),
)
.child(Story::label("Selected with `tooltip`"))
.child(
div().w_8().child(
IconButton::new("selected_with_tooltip", Icon::InlayHint)
.selected(true)
.tooltip(|cx| Tooltip::text("Toggle inlay hints", cx)),
),
)
}
}
@@ -23,5 +23,9 @@ impl Render for LabelStory {
"Héllo, world!",
vec![0, 1, 3, 8, 9, 13],
))
.child(Story::label("Highlighted with `color`"))
.child(
HighlightedLabel::new("Hello, world!", vec![0, 1, 2, 7, 8, 12]).color(Color::Error),
)
}
}
+1
View File
@@ -84,6 +84,7 @@ impl Render for Tooltip {
.px_2()
.child(
h_stack()
.gap_2()
.child(self.title.clone())
.when_some(self.key_binding.clone(), |this, key_binding| {
this.justify_between().child(key_binding)