Skip to content

Commit

Permalink
Refresh window title on demand only (#94)
Browse files Browse the repository at this point in the history
  • Loading branch information
TicClick authored Feb 13, 2024
1 parent 3765bd1 commit e3fdcb1
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 27 deletions.
2 changes: 1 addition & 1 deletion crates/steel_core/src/ipc/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ impl CoreClient {
.unwrap();
}

pub fn chat_switch_requested(&self, target: &str, message_id: usize) {
pub fn chat_switch_requested(&self, target: &str, message_id: Option<usize>) {
self.server
.blocking_send(AppMessageIn::UIChatSwitchRequested(
target.to_owned(),
Expand Down
2 changes: 1 addition & 1 deletion crates/steel_core/src/ipc/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ pub enum AppMessageIn {
UIPrivateChatOpened(String),
UIChatClosed(String),
UIChatCleared(String),
UIChatSwitchRequested(String, usize),
UIChatSwitchRequested(String, Option<usize>),
UIChatMessageSent { target: String, text: String },
UIChatActionSent { target: String, text: String },
UISettingsRequested,
Expand Down
2 changes: 1 addition & 1 deletion crates/steel_core/src/ipc/ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ pub enum UIMessageIn {
details: String,
},
NewChatRequested(String, ChatState, bool),
ChatSwitchRequested(String, usize),
ChatSwitchRequested(String, Option<usize>),
ChannelJoined(String),
ChatClosed(String),
ChatCleared(String),
Expand Down
2 changes: 1 addition & 1 deletion src/app/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ impl Application {
self.join_channel(&channel);
}

pub fn ui_handle_chat_switch_requested(&self, chat: String, message_id: usize) {
pub fn ui_handle_chat_switch_requested(&self, chat: String, message_id: Option<usize>) {
self.ui_queue
.blocking_send(UIMessageIn::ChatSwitchRequested(chat, message_id))
.unwrap();
Expand Down
4 changes: 1 addition & 3 deletions src/gui/chat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -529,9 +529,7 @@ fn format_chat_name(ui: &mut egui::Ui, state: &UIState, chat_name: &str, message
}
});
if switch_requested {
state
.core
.chat_switch_requested(chat_name, message.id.unwrap());
state.core.chat_switch_requested(chat_name, message.id);
}
}
}
Expand Down
8 changes: 6 additions & 2 deletions src/gui/chat_tabs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,9 @@ impl ChatTabs {
(interleaved_pos, chat_tab.rect.center().y);

if chat_tab.clicked() {
state.highlights.mark_as_read(&normalized_chat_name);
state
.core
.chat_switch_requested(&state.active_chat_tab_name, None);
}
if matches!(chat_state, ChatState::JoinInProgress) {
ui.spinner();
Expand Down Expand Up @@ -292,7 +294,9 @@ impl ChatTabs {
coloured_label,
);
if chat_tab.clicked() {
state.highlights.mark_as_read(&label);
state
.core
.chat_switch_requested(&state.active_chat_tab_name, None);
}
}
}
Expand Down
12 changes: 11 additions & 1 deletion src/gui/state/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,14 +147,23 @@ impl UIState {
}
}

pub fn push_chat_message(&mut self, target: String, mut message: Message, ctx: &egui::Context) {
pub fn push_chat_message(
&mut self,
target: String,
mut message: Message,
ctx: &egui::Context,
) -> bool {
let normalized = target.to_lowercase();
let tab_inactive = !self.is_active_tab(&normalized);

let mut name_updated = true;

if let Some(pos) = self.name_to_chat.get(&normalized) {
if let Some(ch) = self.chats.get_mut(*pos) {
// If the chat was open with an improper case, fix it!
if ch.name != target {
ch.name = target;
name_updated = true;
}

message.id = Some(ch.messages.len());
Expand Down Expand Up @@ -189,6 +198,7 @@ impl UIState {
}
}
}
name_updated
}

pub fn validate_reference(&self, chat_name: &str, highlight: &Message) -> bool {
Expand Down
48 changes: 31 additions & 17 deletions src/gui/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,20 @@ impl ApplicationWindow {
}
}

fn refresh_window_title(&self, ctx: &egui::Context) {
let new_tab_title = match self.s.active_chat_tab_name.starts_with('$') {
true => format!("steel v{}", crate::VERSION),
false => {
if let Some(chat) = self.s.active_chat() {
format!("{} – steel v{}", chat.name, crate::VERSION)
} else {
format!("steel v{}", crate::VERSION)
}
}
};
ctx.send_viewport_cmd(egui::ViewportCommand::Title(new_tab_title));
}

pub fn process_pending_events(&mut self, ctx: &egui::Context) {
if self.date_announcer.should_announce() {
let text = format!(
Expand Down Expand Up @@ -206,6 +220,9 @@ impl ApplicationWindow {
} else {
self.s.add_new_chat(name, state, switch_to_chat);
}
if switch_to_chat {
self.refresh_window_title(ctx);
}
}

UIMessageIn::NewChatStatusReceived {
Expand All @@ -220,10 +237,14 @@ impl ApplicationWindow {

UIMessageIn::ChatSwitchRequested(name, message_id) => {
if self.s.has_chat(&name) {
self.s.highlights.mark_as_read(&name);
self.s.active_chat_tab_name = name;
self.chat.scroll_to = Some(message_id);
let lowercase_name = name.to_lowercase();
self.s.highlights.mark_as_read(&lowercase_name);
self.s.active_chat_tab_name = lowercase_name;
if message_id.is_some() {
self.chat.scroll_to = message_id;
}
}
self.refresh_window_title(ctx);
}

UIMessageIn::ChannelJoined(name) => {
Expand All @@ -235,8 +256,12 @@ impl ApplicationWindow {
}

UIMessageIn::NewMessageReceived { target, message } => {
self.s
.push_chat_message(target.clone(), message.clone(), ctx);
let name_updated =
self.s
.push_chat_message(target.clone(), message.clone(), ctx);
if name_updated {
self.refresh_window_title(ctx);
}

#[cfg(feature = "glass")]
match message.username == self.s.settings.chat.irc.username {
Expand All @@ -262,6 +287,7 @@ impl ApplicationWindow {

UIMessageIn::ChatClosed(name) => {
self.s.remove_chat(name);
self.refresh_window_title(ctx);
}

UIMessageIn::ChatCleared(name) => {
Expand Down Expand Up @@ -304,18 +330,6 @@ impl eframe::App for ApplicationWindow {
ctx.request_repaint_after(MIN_IDLE_FRAME_TIME);
self.process_pending_events(ctx);

if !self.s.active_chat_tab_name.is_empty() {
let title = match self.s.active_chat_tab_name.starts_with('$') {
true => format!("steel v{}", crate::VERSION),
false => format!(
"{} – steel v{}",
self.s.active_chat_tab_name,
crate::VERSION
),
};
ctx.send_viewport_cmd(egui::ViewportCommand::Title(title));
}

self.set_theme(ctx);

self.usage_window
Expand Down

0 comments on commit e3fdcb1

Please sign in to comment.