-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2aad7e4
commit e3e8f16
Showing
3 changed files
with
104 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
use gpui::{ | ||
div, prelude::FluentBuilder, px, relative, AnyElement, Div, IntoElement, ParentElement, | ||
RenderOnce, Styled, WindowContext, | ||
}; | ||
|
||
use crate::{h_flex, red_500, white}; | ||
|
||
#[derive(Default)] | ||
enum BadgeStyle { | ||
Dot, | ||
#[default] | ||
Number, | ||
} | ||
|
||
#[derive(IntoElement)] | ||
pub struct Badge { | ||
base: Div, | ||
count: usize, | ||
max: usize, | ||
style: BadgeStyle, | ||
} | ||
|
||
impl Badge { | ||
pub fn new() -> Self { | ||
Self { | ||
base: div(), | ||
count: 0, | ||
max: 99, | ||
style: Default::default(), | ||
} | ||
} | ||
|
||
pub fn dot(mut self) -> Self { | ||
self.style = BadgeStyle::Dot; | ||
self | ||
} | ||
|
||
pub fn count(mut self, count: usize) -> Self { | ||
self.count = count; | ||
self | ||
} | ||
|
||
pub fn max(mut self, max: usize) -> Self { | ||
self.max = max; | ||
self | ||
} | ||
} | ||
|
||
impl ParentElement for Badge { | ||
fn extend(&mut self, elements: impl IntoIterator<Item = AnyElement>) { | ||
self.base.extend(elements); | ||
} | ||
} | ||
|
||
impl RenderOnce for Badge { | ||
fn render(self, _: &mut WindowContext) -> impl IntoElement { | ||
self.base.relative().when(self.count > 0, |this| { | ||
this.child( | ||
h_flex() | ||
.absolute() | ||
.justify_center() | ||
.rounded_full() | ||
.bg(red_500()) | ||
.map(|this| match self.style { | ||
BadgeStyle::Dot => this.top(px(0.)).right(px(0.)).size(px(6.)), | ||
BadgeStyle::Number => { | ||
let count = if self.count > self.max { | ||
format!("{}+", self.max) | ||
} else { | ||
self.count.to_string() | ||
}; | ||
|
||
this.top(px(-2.)) | ||
.right_neg_1p5() | ||
.py_0p5() | ||
.px_0p5() | ||
.min_w_3p5() | ||
.text_color(white()) | ||
.text_size(px(10.)) | ||
.line_height(relative(1.)) | ||
.child(count) | ||
} | ||
}), | ||
) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters