Skip to content

Commit

Permalink
Add chat commands menu (#74)
Browse files Browse the repository at this point in the history
  • Loading branch information
TicClick authored Jan 23, 2024
1 parent 168c61a commit 418787b
Show file tree
Hide file tree
Showing 9 changed files with 354 additions and 3 deletions.
15 changes: 15 additions & 0 deletions crates/steel_core/src/ipc/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,12 @@ impl CoreClient {
.unwrap();
}

pub fn chat_tab_cleared(&self, normalized_name: &str) {
self.server
.blocking_send(AppMessageIn::UIChatCleared(normalized_name.to_owned()))
.unwrap();
}

pub fn chat_message_sent(&self, target: &str, text: &str) {
self.server
.blocking_send(AppMessageIn::UIChatMessageSent {
Expand All @@ -60,6 +66,15 @@ impl CoreClient {
.unwrap();
}

pub fn chat_action_sent(&self, target: &str, text: &str) {
self.server
.blocking_send(AppMessageIn::UIChatActionSent {
target: target.to_owned(),
text: text.to_owned(),
})
.unwrap();
}

pub fn connect_requested(&self) {
self.server
.blocking_send(AppMessageIn::UIConnectRequested)
Expand Down
2 changes: 2 additions & 0 deletions crates/steel_core/src/ipc/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@ pub enum AppMessageIn {
UIChannelJoinRequested(String),
UIPrivateChatOpened(String),
UIChatClosed(String),
UIChatCleared(String),
UIChatSwitchRequested(String, usize),
UIChatMessageSent { target: String, text: String },
UIChatActionSent { target: String, text: String },
UISettingsRequested,
UISettingsUpdated(Settings),

Expand Down
1 change: 1 addition & 0 deletions crates/steel_core/src/ipc/ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,6 @@ pub enum UIMessageIn {
ChatSwitchRequested(String, usize),
ChannelJoined(String),
ChatClosed(String),
ChatCleared(String),
ChatModeratorAdded(String),
}
24 changes: 24 additions & 0 deletions src/app/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,15 @@ impl Application {
AppMessageIn::UIChatClosed(target) => {
self.ui_handle_close_chat(&target);
}
AppMessageIn::UIChatCleared(target) => {
self.ui_handle_clear_chat(&target);
}
AppMessageIn::UIChatMessageSent { target, text } => {
self.send_text_message(&target, &text);
}
AppMessageIn::UIChatActionSent { target, text } => {
self.send_action(&target, &text);
}
AppMessageIn::UISettingsRequested => {
self.ui_handle_settings_requested();
}
Expand Down Expand Up @@ -296,6 +302,13 @@ impl Application {
.unwrap();
}

pub fn ui_handle_clear_chat(&mut self, name: &str) {
let normalized = name.to_lowercase();
self.ui_queue
.blocking_send(UIMessageIn::ChatCleared(normalized))
.unwrap();
}

pub fn send_text_message(&mut self, target: &str, text: &str) {
self.irc.send_message(target, text);
let message = Message::new_text(&self.state.settings.chat.irc.username, text);
Expand All @@ -307,6 +320,17 @@ impl Application {
.unwrap();
}

pub fn send_action(&mut self, target: &str, text: &str) {
self.irc.send_action(target, text);
let message = Message::new_action(&self.state.settings.chat.irc.username, text);
self.ui_queue
.blocking_send(UIMessageIn::NewMessageReceived {
target: target.to_owned(),
message,
})
.unwrap();
}

pub fn join_channel(&self, channel: &str) {
self.irc.join_channel(channel);
}
Expand Down
4 changes: 2 additions & 2 deletions src/core/irc/actor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,8 +183,8 @@ impl IRCActor {
// This fixes #49 -- either Bancho loses an extra prefix colon due to its similarity with the IRC
// command separator, or https://github.com/aatxe/irc around the transport level, and I can't be
// bothered to figure out who is at fault.
if content.starts_with(":") {
content.insert_str(0, " ");
if content.starts_with(':') {
content.insert(0, ' ');
}
sender.send_privmsg(destination, content).unwrap();
}
Expand Down
33 changes: 32 additions & 1 deletion src/gui/chat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ use crate::core::chat::{Chat, ChatLike, Message, MessageChunk, MessageType};
use crate::gui::state::UIState;
use crate::gui::DecoratedText;

use crate::gui::command;

const MAX_MESSAGE_LENGTH: usize = 450;

trait WithInnerShadow {
Expand Down Expand Up @@ -61,6 +63,8 @@ pub struct ChatWindow {

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

command_helper: command::CommandHelper,
}

impl ChatWindow {
Expand Down Expand Up @@ -96,7 +100,12 @@ impl ChatWindow {
ui.add_space(2.);

if let Some(ch) = state.active_chat() {
if response.lost_focus() && ui.input(|i| i.key_pressed(egui::Key::Enter)) {
if response.lost_focus()
&& ui.input(|i| i.key_pressed(egui::Key::Enter))
&& !self
.command_helper
.detect_and_run(state, &mut self.chat_input)
{
state.core.chat_message_sent(&ch.name, &self.chat_input);
self.chat_input.clear();
response.request_focus();
Expand All @@ -118,6 +127,25 @@ impl ChatWindow {
// Source of wisdom: https://github.com/emilk/egui/blob/c86bfb6e67abf208dccd7e006ccd9c3675edcc2f/crates/egui_demo_lib/src/demo/table_demo.rs

egui::CentralPanel::default().show(ctx, |ui| {
if self
.command_helper
.has_applicable_commands(&self.chat_input)
{
egui::Window::new("chat-command-hint-layer")
.title_bar(false)
.resizable(false)
.pivot(egui::Align2::LEFT_BOTTOM)
.fixed_pos(ui.available_rect_before_wrap().left_bottom())
.show(ctx, |ui| {
self.command_helper.show(
ui,
state,
&mut self.chat_input,
&self.response_widget_id,
);
});
}

// Default spacing, which is by default zero for table rows.
ui.spacing_mut().item_spacing.y = 4.;
self.widget_width = ui.available_width();
Expand Down Expand Up @@ -225,6 +253,7 @@ impl ChatWindow {
) {
let msg = &ch.messages[message_index];

#[allow(unused_mut)] // glass
let mut username_styles = BTreeSet::<TextStyle>::new();
let mut message_styles = BTreeSet::<TextStyle>::new();

Expand Down Expand Up @@ -340,6 +369,7 @@ impl ChatWindow {
}
.with_styles(styles, &state.settings);

#[allow(unused_mut)] // glass
let mut resp = ui.button(username_text);

#[cfg(feature = "glass")]
Expand Down Expand Up @@ -416,6 +446,7 @@ fn show_datetime(
})
}

#[allow(unused_variables)] // glass
fn show_username_menu(ui: &mut egui::Ui, state: &UIState, chat_name: &str, message: &Message) {
if state.is_connected() && ui.button("💬 Open chat").clicked() {
state.core.private_chat_opened(&message.username);
Expand Down
Loading

0 comments on commit 418787b

Please sign in to comment.