1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
//! Avatar component for user representation
//!
//! Displays user avatars with initials or icons.
//!
//! # Example
//!
//! ```rust,ignore
//! use rnk::prelude::*;
//! use rnk::components::Avatar;
//!
//! fn app() -> Element {
//! Box::new()
//! .flex_direction(FlexDirection::Row)
//! .gap(1.0)
//! .children(vec![
//! Avatar::new("John Doe").into_element(),
//! Avatar::new("AB").size(AvatarSize::Large).into_element(),
//! Avatar::initials("CD").color(Color::Cyan).into_element(),
//! ])
//! .into_element()
//! }
//! ```
use crate::components::Text;
use crate::core::{Color, Element};
/// Avatar size
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum AvatarSize {
Small,
#[default]
Medium,
Large,
}
/// An avatar component for user representation
#[derive(Debug, Clone)]
pub struct Avatar {
initials: String,
color: Color,
background: Color,
size: AvatarSize,
}
impl Avatar {
/// Create an avatar from a name (extracts initials)
pub fn new(name: impl Into<String>) -> Self {
let name = name.into();
let initials = Self::extract_initials(&name);
Self {
initials,
color: Color::White,
background: Color::Blue,
size: AvatarSize::Medium,
}
}
/// Create an avatar with explicit initials
pub fn initials(initials: impl Into<String>) -> Self {
Self {
initials: initials.into(),
color: Color::White,
background: Color::Blue,
size: AvatarSize::Medium,
}
}
/// Set the text color
pub fn color(mut self, color: Color) -> Self {
self.color = color;
self
}
/// Set the background color
pub fn background(mut self, color: Color) -> Self {
self.background = color;
self
}
/// Set the avatar size
pub fn size(mut self, size: AvatarSize) -> Self {
self.size = size;
self
}
/// Extract initials from a name
fn extract_initials(name: &str) -> String {
name.split_whitespace()
.filter_map(|word| word.chars().next())
.take(2)
.collect::<String>()
.to_uppercase()
}
/// Convert to Element
pub fn into_element(self) -> Element {
let (left, right) = match self.size {
AvatarSize::Small => ("(", ")"),
AvatarSize::Medium => ("[", "]"),
AvatarSize::Large => ("【", "】"),
};
let content = format!("{}{}{}", left, self.initials, right);
Text::new(content)
.color(self.color)
.background(self.background)
.bold()
.into_element()
}
}
impl Default for Avatar {
fn default() -> Self {
Self::initials("?")
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_avatar_from_name() {
let av = Avatar::new("John Doe");
assert_eq!(av.initials, "JD");
}
#[test]
fn test_avatar_single_name() {
let av = Avatar::new("Alice");
assert_eq!(av.initials, "A");
}
#[test]
fn test_avatar_initials() {
let av = Avatar::initials("XY");
assert_eq!(av.initials, "XY");
}
#[test]
fn test_avatar_into_element() {
let _ = Avatar::new("Test User").into_element();
let _ = Avatar::initials("TU")
.size(AvatarSize::Large)
.into_element();
}
}