Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improve title bar in direct messages #66

Merged
merged 8 commits into from
Jan 10, 2025
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/backend/nc_request/nc_req_data_user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ pub struct NCReqDataParticipants {
attendeePermissions: i32,
sessionIds: Vec<String>,
pub status: Option<String>,
statusIcon: Option<String>,
statusMessage: Option<String>,
pub statusIcon: Option<String>,
pub statusMessage: Option<String>,
statusClearAt: Option<i32>,
roomToken: Option<String>,
phoneNumber: Option<String>,
Expand Down
94 changes: 77 additions & 17 deletions src/ui/widget/title_bar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ use style::Styled;

pub struct TitleBar<'a> {
room: String,
status: Option<String>,
status_text: Option<String>,
user_away_style: Style,
user_dnd_style: Style,
user_online_style: Style,
user_offline_style: Style,
mode: String,
unread: usize,
unread_rooms: Text<'a>,
Expand All @@ -24,6 +30,12 @@ impl TitleBar<'_> {
pub fn new(initial_state: CurrentScreen, room: String, config: &Config) -> Self {
TitleBar {
room,
status: None,
status_text: None,
user_away_style: config.theme.user_away_style(),
user_dnd_style: config.theme.user_dnd_style(),
user_online_style: config.theme.user_online_style(),
user_offline_style: config.theme.user_offline_style(),
mode: initial_state.to_string(),
unread: 0,
unread_rooms: Text::raw(""),
Expand All @@ -40,7 +52,25 @@ impl TitleBar<'_> {
current_room: &Token,
) {
self.mode = screen.to_string();
self.room = backend.get_room(current_room).to_string();
let room = backend.get_room(current_room);
self.room = room.to_string();
if room.is_dm() {
let user = room
.get_users()
.iter()
.find(|user| user.displayName == room.get_display_name());
if user.is_none() {
tofubert marked this conversation as resolved.
Show resolved Hide resolved
log::warn!("Could not find user associated with this DM");
}
self.status = user.and_then(|user| user.status.clone());
self.status_text =
user.and_then(|user| match (&user.statusIcon, &user.statusMessage) {
(None, None) => None,
(None, Some(msg)) => Some(msg.to_string()),
(Some(icon), None) => Some(icon.to_string()),
(Some(icon), Some(msg)) => Some(format!("{icon} {msg}")),
});
}
self.unread = backend.get_room(current_room).get_unread();
let unread_array: Vec<String> = backend
.get_unread_rooms()
Expand All @@ -65,19 +95,41 @@ impl TitleBar<'_> {

impl Widget for &TitleBar<'_> {
fn render(self, area: Rect, buf: &mut Buffer) {
let (room_title, room_title_style) = if self.unread > 0 {
(
format!("Current: {}: {}", self.room, self.unread),
self.title_style,
)
let header = if self.unread > 0 {
format!("Current({}): ", self.unread)
} else {
"Current: ".to_string()
};
let room_style = if let Some(status) = &self.status {
jrichardsen marked this conversation as resolved.
Show resolved Hide resolved
match status.as_str() {
"away" => self.user_away_style,
"offline" => self.user_offline_style,
"dnd" => self.user_dnd_style,
"online" => self.user_online_style,
unknown => {
log::debug!("Unknown Status {unknown}");
self.default_style
}
}
} else {
(format!("Current: {}", self.room), self.title_style)
self.title_style
};
let mut title_length = header.len() + self.room.len();
let mut title_spans = vec![
Span::styled(header, self.title_style),
Span::styled(&self.room, room_style),
];

if let Some(status_text) = &self.status_text {
let status_text = format!(" ({status_text})");
title_length += status_text.len();
title_spans.push(Span::styled(status_text, self.title_style));
}

let title_layout = Layout::default()
.direction(Direction::Horizontal)
.constraints([
Constraint::Min(room_title.len().as_()),
Constraint::Min(title_length.as_()),
Constraint::Fill(1),
Constraint::Percentage(20),
])
Expand All @@ -87,7 +139,7 @@ impl Widget for &TitleBar<'_> {
.borders(Borders::BOTTOM)
.style(self.default_style);

Paragraph::new(Text::styled(room_title, room_title_style))
Paragraph::new(Line::from(title_spans))
.block(title_block)
.render(title_layout[0], buf);

Expand Down Expand Up @@ -129,15 +181,21 @@ mod tests {
let config = init("./test/").unwrap();

let mut mock_nc_backend = MockNCTalk::new();
let backend = TestBackend::new(30, 3);
let backend = TestBackend::new(60, 3);
let mut terminal = Terminal::new(backend).unwrap();
let mut bar = TitleBar::new(CurrentScreen::Reading, "General".to_string(), &config);

let mut mock_room = MockNCRoomInterface::new();
let mut dummy_user = NCReqDataParticipants::default();
dummy_user.displayName = "Butz".to_string();
dummy_user.status = Some("online".to_string());
dummy_user.statusMessage = Some("having fun".to_string());
mock_room.expect_get_users().return_const(vec![dummy_user]);
mock_room.expect_get_unread().return_const(false);
mock_room.expect_get_unread().return_const(42_usize);
mock_room.expect_is_dm().return_const(true);
mock_room
.expect_get_display_name()
.return_const("Butz".to_string());
mock_nc_backend
.expect_get_unread_rooms()
.once()
Expand All @@ -149,19 +207,21 @@ mod tests {
bar.update(CurrentScreen::Reading, &mock_nc_backend, &"123".to_string());

terminal
.draw(|frame| bar.render_area(frame, Rect::new(0, 0, 30, 3)))
.draw(|frame| bar.render_area(frame, Rect::new(0, 0, 60, 3)))
.unwrap();

let mut expected = Buffer::with_lines([
"Current: Butz Readin",
" ",
"──────────────────────────────",
"Current(42): Butz (having fun) Reading",
" ",
"────────────────────────────────────────────────────────────",
]);
expected.set_style(Rect::new(0, 0, 30, 3), config.theme.default_style());
expected.set_style(Rect::new(0, 0, 60, 3), config.theme.default_style());

expected.set_style(Rect::new(0, 0, 13, 1), config.theme.title_status_style());
expected.set_style(Rect::new(13, 0, 4, 1), config.theme.user_online_style());
expected.set_style(Rect::new(17, 0, 13, 1), config.theme.title_status_style());

expected.set_style(Rect::new(24, 0, 6, 1), config.theme.title_status_style());
expected.set_style(Rect::new(53, 0, 7, 1), config.theme.title_status_style());

terminal.backend().assert_buffer(&expected);
}
Expand Down
Loading