use crate::{
components::{Button, ButtonSize, ButtonVariant},
icons::{CarbonIcon, Icon},
t3::{
base::{visually_hidden, warning_glyph},
craft::{rem, sans},
},
theme::color,
tokens::{self, ColorRole},
u0::rows as row,
};
use gpui_kit::{
AnyElement, App, ElementId, Hsla, IntoElement, ParentElement, RenderOnce, SharedString, Styled,
Window, div, prelude::FluentBuilder as _,
};
use std::rc::Rc;
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub enum LogLevel {
#[default]
Error,
Warning,
Info,
}
impl LogLevel {
pub const ALL: [Self; 3] = [Self::Error, Self::Warning, Self::Info];
pub const fn name(self) -> &'static str {
match self {
Self::Error => "error",
Self::Warning => "warning",
Self::Info => "info",
}
}
pub fn ink(self) -> Hsla {
color(match self {
Self::Error => ColorRole::SupportError,
Self::Warning => ColorRole::SupportWarning,
Self::Info => ColorRole::SupportInfo,
})
}
}
#[derive(Clone)]
pub struct LogTroubleRowTexts {
pub open: SharedString,
pub copy: SharedString,
}
impl Default for LogTroubleRowTexts {
fn default() -> Self {
Self {
open: "Open log".into(),
copy: "Copy details".into(),
}
}
}
pub fn count_badge(count: u32) -> Option<String> {
(count > 1).then(|| format!("×{count}"))
}
type Handler = Rc<dyn Fn(&mut Window, &mut App)>;
#[derive(IntoElement)]
pub struct LogTroubleRow {
id: ElementId,
level: LogLevel,
source: Option<SharedString>,
message: SharedString,
count: u32,
time: Option<SharedString>,
texts: LogTroubleRowTexts,
on_open: Option<Handler>,
on_copy: Option<Handler>,
}
impl LogTroubleRow {
pub fn new(id: impl Into<ElementId>, message: impl Into<SharedString>) -> Self {
Self {
id: id.into(),
level: LogLevel::Error,
source: None,
message: message.into(),
count: 1,
time: None,
texts: LogTroubleRowTexts::default(),
on_open: None,
on_copy: None,
}
}
pub fn level(mut self, level: LogLevel) -> Self {
self.level = level;
self
}
pub fn source(mut self, source: impl Into<SharedString>) -> Self {
self.source = Some(source.into());
self
}
pub fn count(mut self, count: u32) -> Self {
self.count = count;
self
}
pub fn time(mut self, time: impl Into<SharedString>) -> Self {
self.time = Some(time.into());
self
}
pub fn texts(mut self, texts: LogTroubleRowTexts) -> Self {
self.texts = texts;
self
}
pub fn on_open(mut self, f: impl Fn(&mut Window, &mut App) + 'static) -> Self {
self.on_open = Some(Rc::new(f));
self
}
pub fn on_copy(mut self, f: impl Fn(&mut Window, &mut App) + 'static) -> Self {
self.on_copy = Some(Rc::new(f));
self
}
}
impl RenderOnce for LogTroubleRow {
fn render(self, _: &mut Window, _: &mut App) -> impl IntoElement {
let id = self.id.clone();
let glyph: AnyElement = match self.level {
LogLevel::Warning => warning_glyph(16.).into_any_element(),
LogLevel::Error => Icon::carbon(CarbonIcon::ErrorFilled)
.size_length(rem(16.))
.color(self.level.ink())
.into_any_element(),
LogLevel::Info => Icon::carbon(CarbonIcon::InformationFilled)
.size_length(rem(16.))
.color(self.level.ink())
.into_any_element(),
};
let mut meta: Vec<AnyElement> = Vec::new();
if let Some(s) = &self.source {
meta.push(row::mono_text(s.clone()).into_any_element());
}
if let Some(t) = &self.time {
meta.push(div().child(t.clone()).into_any_element());
}
if let Some(c) = count_badge(self.count) {
meta.push(
div()
.px(tokens::SPACING_02)
.rounded(rem(16.))
.bg(color(ColorRole::LayerAccent01))
.child(c)
.into_any_element(),
);
}
let (on_open, on_copy) = (self.on_open.clone(), self.on_copy.clone());
row::frame()
.child(
div()
.relative()
.flex()
.flex_none()
.child(glyph)
.child(visually_hidden(self.level.name())),
)
.child(
div()
.flex()
.flex_col()
.flex_1()
.min_w_0()
.gap(gpui_kit::px(2.))
.child(
sans(div(), 14., 400)
.min_w_0()
.overflow_hidden()
.whitespace_nowrap()
.text_ellipsis()
.child(self.message.clone()),
)
.child(row::meta(meta)),
)
.child(row::tail(
Button::new((id.clone(), "open"), self.texts.open.clone())
.variant(ButtonVariant::Ghost)
.size(ButtonSize::Sm)
.when_some(on_open, |b, f| b.on_click(move |_, w, cx| f(w, cx))),
))
.child(row::tail(
Button::new((id.clone(), "copy"), self.texts.copy.clone())
.variant(ButtonVariant::Ghost)
.size(ButtonSize::Sm)
.icon(CarbonIcon::Copy)
.icon_only(true)
.when_some(on_copy, |b, f| b.on_click(move |_, w, cx| f(w, cx))),
))
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::CarbonTheme;
#[test]
fn levels_follow_g_log() {
crate::theme::tests_set(CarbonTheme::G100);
assert_eq!(LogLevel::Error.ink(), color(ColorRole::SupportError));
assert_eq!(LogLevel::Info.ink(), color(ColorRole::SupportInfo));
assert_eq!(LogLevel::Warning.ink(), color(ColorRole::SupportWarning));
}
#[test]
fn count_shows_past_one() {
assert_eq!(count_badge(1), None);
assert_eq!(count_badge(12).as_deref(), Some("×12"));
}
}