From 48279e879ae15520c02e81c5909bf89f5a302150 Mon Sep 17 00:00:00 2001 From: Joscha Date: Sat, 29 Apr 2023 16:31:23 +0200 Subject: [PATCH] Show key binding hint in link popup --- cove/src/ui/euph/links.rs | 32 +++++++++++++++++++++++++------- cove/src/ui/euph/room.rs | 19 +++++++++++-------- cove/src/ui/rooms.rs | 2 ++ 3 files changed, 38 insertions(+), 15 deletions(-) diff --git a/cove/src/ui/euph/links.rs b/cove/src/ui/euph/links.rs index 00d9d7d..8e3f535 100644 --- a/cove/src/ui/euph/links.rs +++ b/cove/src/ui/euph/links.rs @@ -1,17 +1,18 @@ -use cove_config::Keys; +use cove_config::{Config, Keys}; use cove_input::InputEvent; use crossterm::event::KeyCode; use crossterm::style::Stylize; use linkify::{LinkFinder, LinkKind}; -use toss::widgets::Text; -use toss::{Style, Styled, Widget}; +use toss::widgets::{Join2, Text}; +use toss::{Style, Styled, Widget, WidgetExt}; use crate::ui::widgets::{ListBuilder, ListState, Popup}; -use crate::ui::{util, UiError}; +use crate::ui::{key_bindings, util, UiError}; use super::popup::PopupResult; pub struct LinksState { + config: &'static Config, links: Vec, list: ListState, } @@ -19,7 +20,7 @@ pub struct LinksState { const NUMBER_KEYS: [char; 10] = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '0']; impl LinksState { - pub fn new(content: &str) -> Self { + pub fn new(config: &'static Config, content: &str) -> Self { let links = LinkFinder::new() .url_must_have_scheme(false) .kinds(&[LinkKind::Url]) @@ -28,6 +29,7 @@ impl LinksState { .collect(); Self { + config, links, list: ListState::new(), } @@ -69,7 +71,24 @@ impl LinksState { } } - Popup::new(list_builder.build(&mut self.list), "Links") + let hint_style = Style::new().grey().italic(); + let hint = Styled::new("Open links with ", hint_style) + .and_then(key_bindings::format_binding( + &self.config.keys.general.confirm, + )) + .then(" or the number keys.", hint_style); + + Popup::new( + Join2::vertical( + list_builder.build(&mut self.list).segment(), + Text::new(hint) + .padding() + .with_top(1) + .segment() + .with_fixed(true), + ), + "Links", + ) } fn open_link_by_id(&self, id: usize) -> PopupResult { @@ -110,7 +129,6 @@ impl LinksState { return PopupResult::Handled; } - // TODO Mention that this is possible in the UI if let Some(key_event) = event.key_event() { if key_event.modifiers.is_empty() { match key_event.code { diff --git a/cove/src/ui/euph/room.rs b/cove/src/ui/euph/room.rs index f539a0e..5f26304 100644 --- a/cove/src/ui/euph/room.rs +++ b/cove/src/ui/euph/room.rs @@ -1,6 +1,6 @@ use std::collections::VecDeque; -use cove_config::Keys; +use cove_config::{Config, Keys}; use cove_input::InputEvent; use crossterm::style::Stylize; use euphoxide::api::{Data, Message, MessageId, PacketType, SessionId}; @@ -43,8 +43,9 @@ enum State { type EuphChatState = ChatState; pub struct EuphRoom { + config: &'static Config, server_config: ServerConfig, - config: cove_config::EuphRoom, + room_config: cove_config::EuphRoom, ui_event_tx: mpsc::UnboundedSender, room: Option, @@ -61,14 +62,16 @@ pub struct EuphRoom { impl EuphRoom { pub fn new( + config: &'static Config, server_config: ServerConfig, - config: cove_config::EuphRoom, + room_config: cove_config::EuphRoom, vault: EuphRoomVault, ui_event_tx: mpsc::UnboundedSender, ) -> Self { Self { - server_config, config, + server_config, + room_config, ui_event_tx, room: None, focus: Focus::Chat, @@ -97,9 +100,9 @@ impl EuphRoom { .room(self.vault().room().to_string()) .name(format!("{room}-{}", next_instance_id)) .human(true) - .username(self.config.username.clone()) - .force_username(self.config.force_username) - .password(self.config.password.clone()); + .username(self.room_config.username.clone()) + .force_username(self.room_config.force_username) + .password(self.room_config.password.clone()); *next_instance_id = next_instance_id.wrapping_add(1); let tx = self.ui_event_tx.clone(); @@ -420,7 +423,7 @@ impl EuphRoom { if event.matches(&keys.tree.action.links) { if let Some(id) = self.chat.cursor() { if let Some(msg) = logging_unwrap!(self.vault().msg(*id).await) { - self.state = State::Links(LinksState::new(&msg.content)); + self.state = State::Links(LinksState::new(self.config, &msg.content)); } } return true; diff --git a/cove/src/ui/rooms.rs b/cove/src/ui/rooms.rs index e93b8ba..c692d46 100644 --- a/cove/src/ui/rooms.rs +++ b/cove/src/ui/rooms.rs @@ -93,6 +93,7 @@ impl Rooms { fn get_or_insert_room(&mut self, name: String) -> &mut EuphRoom { self.euph_rooms.entry(name.clone()).or_insert_with(|| { EuphRoom::new( + self.config, self.euph_server_config.clone(), self.config.euph_room(&name), self.vault.euph().room(name), @@ -104,6 +105,7 @@ impl Rooms { fn connect_to_room(&mut self, name: String) { let room = self.euph_rooms.entry(name.clone()).or_insert_with(|| { EuphRoom::new( + self.config, self.euph_server_config.clone(), self.config.euph_room(&name), self.vault.euph().room(name),