Merge branch 'main' into element-types

This commit is contained in:
Conrad Irwin 2023-11-14 11:36:32 -07:00
commit 5dda105182
31 changed files with 1154 additions and 1029 deletions

View file

@ -87,6 +87,7 @@ pub struct Button<V: 'static> {
label: SharedString,
variant: ButtonVariant,
width: Option<DefiniteLength>,
color: Option<LabelColor>,
}
impl<V: 'static> Button<V> {
@ -99,6 +100,7 @@ impl<V: 'static> Button<V> {
label: label.into(),
variant: Default::default(),
width: Default::default(),
color: None,
}
}
@ -139,25 +141,24 @@ impl<V: 'static> Button<V> {
self
}
fn label_color(&self) -> LabelColor {
pub fn color(mut self, color: Option<LabelColor>) -> Self {
self.color = color;
self
}
pub fn label_color(&self, color: Option<LabelColor>) -> LabelColor {
if self.disabled {
LabelColor::Disabled
} else if let Some(color) = color {
color
} else {
Default::default()
}
}
fn icon_color(&self) -> IconColor {
if self.disabled {
IconColor::Disabled
} else {
Default::default()
}
}
fn render_label(&self) -> Label {
fn render_label(&self, color: LabelColor) -> Label {
Label::new(self.label.clone())
.color(self.label_color())
.color(color)
.line_height_style(LineHeightStyle::UILabel)
}
@ -166,7 +167,11 @@ impl<V: 'static> Button<V> {
}
pub fn render(self, _view: &mut V, cx: &mut ViewContext<V>) -> impl Component<V> {
let icon_color = self.icon_color();
let (icon_color, label_color) = match (self.disabled, self.color) {
(true, _) => (IconColor::Disabled, LabelColor::Disabled),
(_, None) => (IconColor::Default, LabelColor::Default),
(_, Some(color)) => (IconColor::from(color), color),
};
let mut button = h_stack()
.id(SharedString::from(format!("{}", self.label)))
@ -182,16 +187,16 @@ impl<V: 'static> Button<V> {
(Some(_), Some(IconPosition::Left)) => {
button = button
.gap_1()
.child(self.render_label())
.child(self.render_label(label_color))
.children(self.render_icon(icon_color))
}
(Some(_), Some(IconPosition::Right)) => {
button = button
.gap_1()
.children(self.render_icon(icon_color))
.child(self.render_label())
.child(self.render_label(label_color))
}
(_, _) => button = button.child(self.render_label()),
(_, _) => button = button.child(self.render_label(label_color)),
}
if let Some(width) = self.width {

View file

@ -1,7 +1,7 @@
use gpui::{rems, svg, Hsla};
use strum::EnumIter;
use crate::prelude::*;
use crate::{prelude::*, LabelColor};
#[derive(Default, PartialEq, Copy, Clone)]
pub enum IconSize {
@ -14,15 +14,20 @@ pub enum IconSize {
pub enum IconColor {
#[default]
Default,
Muted,
Disabled,
Placeholder,
Accent,
Created,
Deleted,
Disabled,
Error,
Warning,
Success,
Hidden,
Info,
Modified,
Muted,
Placeholder,
Player(u32),
Selected,
Success,
Warning,
}
impl IconColor {
@ -38,6 +43,33 @@ impl IconColor {
IconColor::Success => cx.theme().status().success,
IconColor::Info => cx.theme().status().info,
IconColor::Selected => cx.theme().colors().icon_accent,
IconColor::Player(i) => cx.theme().styles.player.0[i.clone() as usize].cursor,
IconColor::Created => cx.theme().status().created,
IconColor::Modified => cx.theme().status().modified,
IconColor::Deleted => cx.theme().status().deleted,
IconColor::Hidden => cx.theme().status().hidden,
}
}
}
impl From<LabelColor> for IconColor {
fn from(label: LabelColor) -> Self {
match label {
LabelColor::Default => IconColor::Default,
LabelColor::Muted => IconColor::Muted,
LabelColor::Disabled => IconColor::Disabled,
LabelColor::Placeholder => IconColor::Placeholder,
LabelColor::Accent => IconColor::Accent,
LabelColor::Error => IconColor::Error,
LabelColor::Warning => IconColor::Warning,
LabelColor::Success => IconColor::Success,
LabelColor::Info => IconColor::Info,
LabelColor::Selected => IconColor::Selected,
LabelColor::Player(i) => IconColor::Player(i),
LabelColor::Created => IconColor::Created,
LabelColor::Modified => IconColor::Modified,
LabelColor::Deleted => IconColor::Deleted,
LabelColor::Hidden => IconColor::Hidden,
}
}
}

View file

@ -1,6 +1,6 @@
use crate::{h_stack, prelude::*};
use crate::{ClickHandler, Icon, IconColor, IconElement};
use gpui::{prelude::*, rems, MouseButton};
use crate::{ClickHandler, Icon, IconColor, IconElement, TextTooltip};
use gpui::{prelude::*, MouseButton, VisualContext};
use std::sync::Arc;
struct IconButtonHandlers<V: 'static> {
@ -20,6 +20,7 @@ pub struct IconButton<V: 'static> {
color: IconColor,
variant: ButtonVariant,
state: InteractionState,
tooltip: Option<SharedString>,
handlers: IconButtonHandlers<V>,
}
@ -31,6 +32,7 @@ impl<V: 'static> IconButton<V> {
color: IconColor::default(),
variant: ButtonVariant::default(),
state: InteractionState::default(),
tooltip: None,
handlers: IconButtonHandlers::default(),
}
}
@ -55,6 +57,11 @@ impl<V: 'static> IconButton<V> {
self
}
pub fn tooltip(mut self, tooltip: impl Into<SharedString>) -> Self {
self.tooltip = Some(tooltip.into());
self
}
pub fn on_click(
mut self,
handler: impl 'static + Fn(&mut V, &mut ViewContext<V>) + Send + Sync,
@ -86,9 +93,7 @@ impl<V: 'static> IconButton<V> {
.id(self.id.clone())
.justify_center()
.rounded_md()
// todo!("Where do these numbers come from?")
.py(rems(0.21875))
.px(rems(0.375))
.p_1()
.bg(bg_color)
.hover(|style| style.bg(bg_hover_color))
.active(|style| style.bg(bg_active_color))
@ -101,6 +106,11 @@ impl<V: 'static> IconButton<V> {
});
}
if let Some(tooltip) = self.tooltip.clone() {
button =
button.tooltip(move |_, cx| cx.build_view(|cx| TextTooltip::new(tooltip.clone())));
}
button
}
}

View file

@ -7,28 +7,40 @@ use crate::styled_ext::StyledExt;
pub enum LabelColor {
#[default]
Default,
Muted,
Accent,
Created,
Modified,
Deleted,
Disabled,
Error,
Hidden,
Info,
Modified,
Muted,
Placeholder,
Accent,
Player(u32),
Selected,
Success,
Warning,
}
impl LabelColor {
pub fn hsla(&self, cx: &WindowContext) -> Hsla {
match self {
Self::Default => cx.theme().colors().text,
Self::Muted => cx.theme().colors().text_muted,
Self::Created => cx.theme().status().created,
Self::Modified => cx.theme().status().modified,
Self::Deleted => cx.theme().status().deleted,
Self::Disabled => cx.theme().colors().text_disabled,
Self::Hidden => cx.theme().status().hidden,
Self::Placeholder => cx.theme().colors().text_placeholder,
Self::Accent => cx.theme().colors().text_accent,
LabelColor::Default => cx.theme().colors().text,
LabelColor::Muted => cx.theme().colors().text_muted,
LabelColor::Created => cx.theme().status().created,
LabelColor::Modified => cx.theme().status().modified,
LabelColor::Deleted => cx.theme().status().deleted,
LabelColor::Disabled => cx.theme().colors().text_disabled,
LabelColor::Hidden => cx.theme().status().hidden,
LabelColor::Info => cx.theme().status().info,
LabelColor::Placeholder => cx.theme().colors().text_placeholder,
LabelColor::Accent => cx.theme().colors().text_accent,
LabelColor::Player(i) => cx.theme().styles.player.0[i.clone() as usize].cursor,
LabelColor::Error => cx.theme().status().error,
LabelColor::Selected => cx.theme().colors().text_accent,
LabelColor::Success => cx.theme().status().success,
LabelColor::Warning => cx.theme().status().warning,
}
}
}

View file

@ -9,8 +9,10 @@ pub struct TextTooltip {
}
impl TextTooltip {
pub fn new(str: SharedString) -> Self {
Self { title: str }
pub fn new(title: impl Into<SharedString>) -> Self {
Self {
title: title.into(),
}
}
}