Skip to content

Commit 683f0f4

Browse files
dragonfly1033andybalaam
authored andcommitted
feat(multiverse): Add room creation to multiverse
1 parent c783ed8 commit 683f0f4

File tree

6 files changed

+130
-3
lines changed

6 files changed

+130
-3
lines changed

labs/multiverse/src/main.rs

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ use matrix_sdk::{
2525
config::StoreConfig,
2626
encryption::{BackupDownloadStrategy, EncryptionSettings},
2727
reqwest::Url,
28-
ruma::OwnedRoomId,
28+
ruma::{OwnedRoomId, api::client::room::create_room::v3::Request as CreateRoomRequest},
2929
};
3030
use matrix_sdk_common::locks::Mutex;
3131
use matrix_sdk_ui::{
@@ -44,6 +44,7 @@ use widgets::{
4444
};
4545

4646
use crate::widgets::{
47+
create_room::CreateRoomView,
4748
help::HelpView,
4849
room_list::{ExtraRoomInfo, RoomInfos, RoomList, Rooms},
4950
status::Status,
@@ -84,6 +85,8 @@ pub enum GlobalMode {
8485
Settings { view: SettingsView },
8586
/// Mode where we are shutting our tasks down and exiting multiverse.
8687
Exiting { shutdown_task: JoinHandle<()> },
88+
/// Mode where we have opened create room screen
89+
CreateRoom { view: CreateRoomView },
8790
}
8891

8992
/// Helper function to create a centered rect using up certain percentage of the
@@ -350,6 +353,10 @@ impl App {
350353
}
351354
}
352355

356+
Event::Key(KeyEvent { modifiers: KeyModifiers::CONTROL, code: Char('r'), .. }) => {
357+
self.set_global_mode(GlobalMode::CreateRoom { view: CreateRoomView::new() })
358+
}
359+
353360
_ => self.room_view.handle_event(event).await,
354361
}
355362

@@ -360,7 +367,10 @@ impl App {
360367
self.state.throbber_state.calc_next();
361368

362369
match &mut self.state.global_mode {
363-
GlobalMode::Help | GlobalMode::Default | GlobalMode::Exiting { .. } => {}
370+
GlobalMode::Help
371+
| GlobalMode::Default
372+
| GlobalMode::CreateRoom { .. }
373+
| GlobalMode::Exiting { .. } => {}
364374
GlobalMode::Settings { view } => {
365375
view.on_tick();
366376
}
@@ -411,12 +421,41 @@ impl App {
411421
self.set_global_mode(GlobalMode::Default);
412422
}
413423
}
424+
GlobalMode::CreateRoom { view } => {
425+
if let Event::Key(key) = event
426+
&& let KeyModifiers::NONE = key.modifiers
427+
{
428+
match key.code {
429+
Enter => {
430+
if let Some(room_name) = view.get_text() {
431+
let mut request = CreateRoomRequest::new();
432+
request.name = Some(room_name);
433+
if let Err(err) = self
434+
.sync_service
435+
.room_list_service()
436+
.client()
437+
.create_room(request)
438+
.await
439+
{
440+
error!("error while creating room: {err:?}");
441+
}
442+
}
443+
self.set_global_mode(GlobalMode::Default);
444+
}
445+
Esc => self.set_global_mode(GlobalMode::Default),
446+
_ => view.handle_key_press(key),
447+
}
448+
}
449+
}
414450
GlobalMode::Exiting { .. } => {}
415451
}
416452
}
417453

418454
match &self.state.global_mode {
419-
GlobalMode::Default | GlobalMode::Help | GlobalMode::Settings { .. } => {}
455+
GlobalMode::Default
456+
| GlobalMode::Help
457+
| GlobalMode::CreateRoom { .. }
458+
| GlobalMode::Settings { .. } => {}
420459
GlobalMode::Exiting { shutdown_task } => {
421460
if shutdown_task.is_finished() {
422461
break;
@@ -480,6 +519,9 @@ impl Widget for &mut App {
480519
let mut help_view = HelpView::new();
481520
help_view.render(area, buf);
482521
}
522+
GlobalMode::CreateRoom { view } => {
523+
view.render(area, buf);
524+
}
483525
}
484526
}
485527
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
use crossterm::event::KeyEvent;
2+
use ratatui::{
3+
buffer::Buffer,
4+
layout::Rect,
5+
style::{Stylize, palette::tailwind},
6+
widgets::{Block, Borders, Widget},
7+
};
8+
use tui_textarea::TextArea;
9+
10+
#[derive(Default)]
11+
pub(crate) struct Input {
12+
/// The text area that will keep track of what the user has input.
13+
textarea: TextArea<'static>,
14+
}
15+
16+
impl Input {
17+
/// Create a new empty [`Input`] widget.
18+
pub fn new() -> Self {
19+
let textarea = TextArea::default();
20+
Self { textarea }
21+
}
22+
23+
/// Receive a key press event and handle it.
24+
pub fn handle_key_press(&mut self, key: KeyEvent) {
25+
self.textarea.input(key);
26+
}
27+
28+
/// Get the currently input text.
29+
pub fn get_input(&self) -> String {
30+
self.textarea.lines().join("\n")
31+
}
32+
}
33+
34+
impl Widget for &mut Input {
35+
fn render(self, area: Rect, buf: &mut Buffer) {
36+
// Set the placeholder text.
37+
self.textarea.set_placeholder_text("(Enter new room name)");
38+
39+
// Let's first create a block to set the background color.
40+
let input_block =
41+
Block::new().borders(Borders::ALL).bg(tailwind::BLUE.c400).title("Create Room");
42+
43+
// Now we set the block and we render the textarea.
44+
self.textarea.set_block(input_block);
45+
self.textarea.render(area, buf);
46+
}
47+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
use crossterm::event::KeyEvent;
2+
use ratatui::{layout::Flex, prelude::*};
3+
mod input;
4+
use input::Input;
5+
6+
#[derive(Default)]
7+
pub struct CreateRoomView {
8+
input: Input,
9+
}
10+
11+
impl CreateRoomView {
12+
pub fn new() -> Self {
13+
Self { input: Input::new() }
14+
}
15+
16+
pub fn get_text(&self) -> Option<String> {
17+
let name = self.input.get_input();
18+
if !name.is_empty() { Some(name) } else { None }
19+
}
20+
21+
pub fn handle_key_press(&mut self, key: KeyEvent) {
22+
self.input.handle_key_press(key);
23+
}
24+
}
25+
26+
impl Widget for &mut CreateRoomView {
27+
fn render(self, area: Rect, buf: &mut Buffer) {
28+
let vertical = Layout::vertical([Constraint::Length(3)]).flex(Flex::Center);
29+
let horizontal = Layout::horizontal([Constraint::Percentage(30)]).flex(Flex::Center);
30+
let [area] = vertical.areas(area);
31+
let [area] = horizontal.areas(area);
32+
33+
self.input.render(area, buf);
34+
}
35+
}

labs/multiverse/src/widgets/help.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ impl Widget for &mut HelpView {
6464
Cell::from("Ctrl-t"),
6565
Cell::from("Open a thread on the focused timeline item"),
6666
]),
67+
Row::new(vec![Cell::from("Ctrl-r"), Cell::from("Create a new room")]),
6768
];
6869
let widths = [Constraint::Length(5), Constraint::Length(5)];
6970

labs/multiverse/src/widgets/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use itertools::Itertools;
22
use ratatui::{prelude::*, widgets::WidgetRef};
33

4+
pub mod create_room;
45
pub mod help;
56
pub mod recovery;
67
pub mod room_list;

labs/multiverse/src/widgets/status.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ impl StatefulWidget for &mut Status {
129129
match global_mode {
130130
GlobalMode::Help => "Press q to exit the help screen",
131131
GlobalMode::Settings { .. } => "Press ESC to exit the settings screen",
132+
GlobalMode::CreateRoom { .. } => "Press ESC to exit the create room screen",
132133
GlobalMode::Default => "Press F1 to show the help screen",
133134
GlobalMode::Exiting { .. } => "",
134135
}

0 commit comments

Comments
 (0)