done with text sending widget
This commit is contained in:
parent
9fafc48263
commit
a9144f0ce6
4 changed files with 25 additions and 14 deletions
1
Cargo.lock
generated
1
Cargo.lock
generated
|
@ -1177,6 +1177,7 @@ dependencies = [
|
||||||
"tower-http",
|
"tower-http",
|
||||||
"tui-input",
|
"tui-input",
|
||||||
"tui-logger",
|
"tui-logger",
|
||||||
|
"unicode-segmentation",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
|
|
|
@ -28,3 +28,4 @@ tokio = { version = "1", default-features = false, features = ["time", "macros",
|
||||||
tower-http = { version = "0.6", features = ["limit"] }
|
tower-http = { version = "0.6", features = ["limit"] }
|
||||||
tui-input = "0.14.0"
|
tui-input = "0.14.0"
|
||||||
tui-logger = { version = "0.17", features = ["crossterm"] }
|
tui-logger = { version = "0.17", features = ["crossterm"] }
|
||||||
|
unicode-segmentation = "1.12.0"
|
||||||
|
|
|
@ -148,6 +148,7 @@ impl App {
|
||||||
},
|
},
|
||||||
SendingScreen::Peers => match code {
|
SendingScreen::Peers => match code {
|
||||||
KeyCode::Tab => *sending_screen = SendingScreen::Files,
|
KeyCode::Tab => *sending_screen = SendingScreen::Files,
|
||||||
|
KeyCode::Char('t') => *sending_screen = SendingScreen::Text,
|
||||||
KeyCode::Enter => self.send_content().await,
|
KeyCode::Enter => self.send_content().await,
|
||||||
KeyCode::Up => self.peer_state.select_previous(),
|
KeyCode::Up => self.peer_state.select_previous(),
|
||||||
KeyCode::Down => self.peer_state.select_next(),
|
KeyCode::Down => self.peer_state.select_next(),
|
||||||
|
@ -166,6 +167,7 @@ impl App {
|
||||||
KeyCode::Enter => self.send_text().await,
|
KeyCode::Enter => self.send_text().await,
|
||||||
KeyCode::Esc => {
|
KeyCode::Esc => {
|
||||||
self.text = None;
|
self.text = None;
|
||||||
|
self.input.reset();
|
||||||
*sending_screen = SendingScreen::Files;
|
*sending_screen = SendingScreen::Files;
|
||||||
}
|
}
|
||||||
_ => {
|
_ => {
|
||||||
|
|
|
@ -195,14 +195,18 @@ impl Widget for &mut App {
|
||||||
);
|
);
|
||||||
|
|
||||||
if s == SendingScreen::Text {
|
if s == SendingScreen::Text {
|
||||||
let rect = centered_rect(area, 90, 80);
|
let rect = centered_rect(area, Constraint::Percentage(80), Constraint::Max(10));
|
||||||
let text = if let Some(text) = self.text.as_ref() {
|
let text = if let Some(text) = self.text.as_ref() {
|
||||||
text
|
text
|
||||||
} else {
|
} else {
|
||||||
""
|
""
|
||||||
};
|
};
|
||||||
|
|
||||||
text_input(text, rect, buf);
|
text_popup(text, " Enter Text to Send ", rect, buf);
|
||||||
|
// TODO: add cursor, need to do that in the `draw` method
|
||||||
|
// because we need the frame to do it; add cursor position
|
||||||
|
// fields to `App` and mutate them in the text input
|
||||||
|
// function
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
_ => {
|
_ => {
|
||||||
|
@ -228,15 +232,18 @@ fn outer_frame(screen: &CurrentScreen, menu: &Line, area: Rect, buf: &mut Buffer
|
||||||
.render(area, buf);
|
.render(area, buf);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn text_input(text: &str, area: Rect, buf: &mut Buffer) {
|
fn text_popup(text: &str, title: &str, area: Rect, buf: &mut Buffer) {
|
||||||
let title = Line::from(" Input Text ".bold());
|
let title = Line::from(title.bold());
|
||||||
let block = Block::bordered().title(title.centered());
|
let block = Block::bordered().title(title.centered());
|
||||||
ratatui::widgets::Clear::default().render(area, buf);
|
ratatui::widgets::Clear.render(area, buf);
|
||||||
|
|
||||||
Paragraph::new(text)
|
block.render(area, buf);
|
||||||
.centered()
|
|
||||||
.block(block)
|
let (_, len) = unicode_segmentation::UnicodeSegmentation::graphemes(text, true).size_hint();
|
||||||
.render(area, buf);
|
let len = len.unwrap_or(text.len()) as u16;
|
||||||
|
let area = centered_rect(area, Constraint::Length(len), Constraint::Length(1));
|
||||||
|
|
||||||
|
Paragraph::new(text).centered().yellow().render(area, buf);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn logger(area: Rect, buf: &mut Buffer) {
|
fn logger(area: Rect, buf: &mut Buffer) {
|
||||||
|
@ -385,10 +392,10 @@ impl Widget for NetworkInfoWidget {
|
||||||
}
|
}
|
||||||
|
|
||||||
// helpers
|
// helpers
|
||||||
fn centered_rect(area: Rect, width_pct: u16, height_pct: u16) -> Rect {
|
fn centered_rect(area: Rect, horizontal: Constraint, vertical: Constraint) -> Rect {
|
||||||
let horizontal = Layout::horizontal([Constraint::Percentage(width_pct)]).flex(Flex::Center);
|
let [area] = Layout::horizontal([horizontal])
|
||||||
let vertical = Layout::vertical([Constraint::Percentage(height_pct)]).flex(Flex::Center);
|
.flex(Flex::Center)
|
||||||
let [area] = vertical.areas(area);
|
.areas(area);
|
||||||
let [area] = horizontal.areas(area);
|
let [area] = Layout::vertical([vertical]).flex(Flex::Center).areas(area);
|
||||||
area
|
area
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue