Skip to content

Commit

Permalink
Merge pull request #67 from TicClick/text-wrap
Browse files Browse the repository at this point in the history
  • Loading branch information
TicClick authored Jan 19, 2024
2 parents bcc5404 + 4c3fc93 commit aa67293
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 25 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ winresource = "0.1"
steel_core = { version = "0.1.3", path = "./crates/steel_core" }
glass = { version = "*", path = "./crates/glass", optional = true}

eframe = "0.24.1"
egui_extras = "0.24.2"
eframe = "0.25.0"
egui_extras = "0.25.0"

chrono = { version = "0.4.31", features = ["serde"] }
irc = "0.15.0"
Expand Down
2 changes: 1 addition & 1 deletion crates/glass
Submodule glass updated from 23cfda to 967074
3 changes: 1 addition & 2 deletions crates/steel_core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
# See also: https://github.com/emilk/egui/issues/2898
ecolor = "0.24.1"
ecolor = "0.25.0"
chrono = { version = "0.4.31", features = ["serde"] }
irc-proto = "0.15.0"
serde = { version = "1.0.194", features = ["serde_derive"] }
Expand Down
67 changes: 47 additions & 20 deletions src/gui/chat.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use eframe::egui;
use egui_extras::{Column, TableBuilder};
use std::collections::BTreeSet;
use std::collections::{BTreeMap, BTreeSet};

use steel_core::TextStyle;

Expand Down Expand Up @@ -48,14 +48,17 @@ pub struct ChatWindow {
pub scroll_to: Option<usize>,

chat_row_height: Option<f32>,
cached_row_heights: Vec<f32>,
cached_row_heights: BTreeMap<String, Vec<f32>>,

// FIXME: This is a hack to prevent the context menu from re-sticking to other chat buttons (and therefore messages)
// when the chat keeps scrolling to bottom. The menu seems to not care about that and stick to whichever is beneath, which is changing.
context_menu_target: Option<Message>,

// Draw the hinting shadow at the bottom of the chat in the next frame.
shadow_next_frame: bool,

// Chat space width -- longer lines will wrap around the window.
widget_width: f32,
}

impl ChatWindow {
Expand Down Expand Up @@ -103,11 +106,15 @@ impl ChatWindow {
egui::CentralPanel::default().show(ctx, |ui| {
// Default spacing, which is by default zero for table rows.
ui.spacing_mut().item_spacing.y = 4.;
self.widget_width = ui.available_width();

let chat_row_height = *self
.chat_row_height
.get_or_insert_with(|| ui.text_style_height(&egui::TextStyle::Body));

self.cached_row_heights
.entry(state.active_chat_tab_name.clone())
.or_default()
.resize(state.chat_message_count(), chat_row_height);

ui.push_id(&state.active_chat_tab_name, |ui| {
Expand All @@ -120,7 +127,9 @@ impl ChatWindow {
builder = builder.stick_to_bottom(true);
}

let heights = self.cached_row_heights.clone().into_iter();
let heights = self.cached_row_heights[&state.active_chat_tab_name]
.clone()
.into_iter();
let mut last_visible_row = 0;

builder
Expand All @@ -129,7 +138,8 @@ impl ChatWindow {
.auto_shrink([false; 2])
.body(|body| {
if let Some(ch) = state.active_chat() {
body.heterogeneous_rows(heights, |row_index, mut row| {
body.heterogeneous_rows(heights, |mut row| {
let row_index = row.index();
last_visible_row = row_index;
row.col(|ui| {
self.show_regular_chat_single_message(ui, state, ch, row_index);
Expand All @@ -143,7 +153,8 @@ impl ChatWindow {
st.insert(TextStyle::Monospace);
st
});
body.heterogeneous_rows(heights, |row_index, mut row| {
body.heterogeneous_rows(heights, |mut row| {
let row_index = row.index();
last_visible_row = row_index;
row.col(|ui| {
self.show_server_tab_single_message(
Expand All @@ -156,7 +167,8 @@ impl ChatWindow {
});
}
super::HIGHLIGHTS_TAB_NAME => {
body.heterogeneous_rows(heights, |row_index, mut row| {
body.heterogeneous_rows(heights, |mut row| {
let row_index = row.index();
last_visible_row = row_index;
row.col(|ui| {
self.show_highlights_tab_single_message(
Expand All @@ -171,7 +183,13 @@ impl ChatWindow {
});

// FIXME: the shadow is removed as soon as the last row becomes PARTIALLY, NOT FULLY visible.
if last_visible_row + 1 < self.cached_row_heights.len() {
if last_visible_row + 1
< self
.cached_row_heights
.get_mut(&state.active_chat_tab_name)
.unwrap()
.len()
{
if self.shadow_next_frame {
ui.inner_shadow_bottom(20);
} else {
Expand Down Expand Up @@ -214,20 +232,25 @@ impl ChatWindow {
}

let updated_height = ui
.horizontal_wrapped(|ui| {
ui.spacing_mut().item_spacing.x /= 2.;
ui.style_mut().wrap = Some(true);
show_datetime(ui, state, msg, &None);
match msg.r#type {
MessageType::Action | MessageType::Text => {
self.format_username(ui, state, &ch.name, msg, &Some(username_styles));
format_chat_message_text(ui, state, msg, &Some(message_styles))
.push_id(format!("{}_row_{}", ch.name, message_index), |ui| {
ui.horizontal_wrapped(|ui| {
ui.spacing_mut().item_spacing.x /= 2.;
ui.set_max_width(self.widget_width);
show_datetime(ui, state, msg, &None);
match msg.r#type {
MessageType::Action | MessageType::Text => {
self.format_username(ui, state, &ch.name, msg, &Some(username_styles));
format_chat_message_text(ui, state, msg, &Some(message_styles))
}
MessageType::System => format_system_message(ui, msg),
}
MessageType::System => format_system_message(ui, msg),
}
})
})
.inner
.inner;
self.cached_row_heights[message_index] = updated_height;
self.cached_row_heights
.get_mut(&state.active_chat_tab_name)
.unwrap()[message_index] = updated_height;
}

fn show_highlights_tab_single_message(
Expand All @@ -246,7 +269,9 @@ impl ChatWindow {
format_chat_message_text(ui, state, msg, &None)
})
.inner;
self.cached_row_heights[message_index] = updated_height;
self.cached_row_heights
.get_mut(&state.active_chat_tab_name)
.unwrap()[message_index] = updated_height;
}

fn show_server_tab_single_message(
Expand All @@ -264,7 +289,9 @@ impl ChatWindow {
format_chat_message_text(ui, state, msg, styles)
})
.inner;
self.cached_row_heights[message_index] = updated_height;
self.cached_row_heights
.get_mut(&state.active_chat_tab_name)
.unwrap()[message_index] = updated_height;
}

pub fn return_focus(&mut self, ctx: &egui::Context, state: &UIState) {
Expand Down

0 comments on commit aa67293

Please sign in to comment.