Skip to content

Commit fece318

Browse files
committed
refactor(app): move render_set_alias into its own module
1 parent b9f2e4b commit fece318

3 files changed

Lines changed: 124 additions & 99 deletions

File tree

src/alias.rs

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
use crate::bluetooth::Controller;
2+
use ratatui::{
3+
Frame,
4+
layout::{Alignment, Constraint, Layout, Rect},
5+
style::{Color, Modifier, Style, Stylize},
6+
text::{Line, Span},
7+
widgets::{Block, BorderType, Borders, Clear, Padding, Paragraph, TableState},
8+
};
9+
use tui_input::Input;
10+
11+
pub fn render_set_alias(
12+
controllers: &[Controller],
13+
controller_state: &TableState,
14+
paired_devices_state: &TableState,
15+
new_alias: &Input,
16+
frame: &mut Frame,
17+
area: Rect,
18+
) {
19+
let block = Layout::vertical([
20+
Constraint::Fill(1),
21+
Constraint::Length(6),
22+
Constraint::Fill(1),
23+
])
24+
.split(area);
25+
26+
let block = Layout::horizontal([
27+
Constraint::Fill(1),
28+
Constraint::Max(70),
29+
Constraint::Fill(1),
30+
])
31+
.split(block[1])[1];
32+
33+
let (text_block, alias_block) = {
34+
let chunks = Layout::vertical(
35+
[
36+
Constraint::Length(1),
37+
Constraint::Length(3),
38+
Constraint::Length(1),
39+
Constraint::Length(2),
40+
]
41+
.as_ref(),
42+
)
43+
.split(block);
44+
45+
let area1 = Layout::horizontal(
46+
[
47+
Constraint::Length(1),
48+
Constraint::Fill(1),
49+
Constraint::Length(1),
50+
]
51+
.as_ref(),
52+
)
53+
.split(chunks[1]);
54+
55+
let area2 = Layout::horizontal(
56+
[
57+
Constraint::Percentage(20),
58+
Constraint::Fill(1),
59+
Constraint::Percentage(20),
60+
]
61+
.as_ref(),
62+
)
63+
.split(chunks[2]);
64+
65+
(area1[1], area2[1])
66+
};
67+
68+
frame.render_widget(Clear, block);
69+
frame.render_widget(
70+
Block::new()
71+
.borders(Borders::ALL)
72+
.border_type(BorderType::Thick)
73+
.style(Style::default().green())
74+
.border_style(Style::default().fg(Color::Green)),
75+
block,
76+
);
77+
78+
if let Some(selected_controller) = controller_state.selected() {
79+
let controller = &controllers[selected_controller];
80+
if let Some(index) = paired_devices_state.selected() {
81+
let name = controller.paired_devices[index].alias.as_str();
82+
83+
let text = Line::from(vec![
84+
Span::from("Enter the new name for "),
85+
Span::styled(
86+
name,
87+
Style::default().add_modifier(Modifier::BOLD | Modifier::ITALIC),
88+
),
89+
]);
90+
91+
let msg = Paragraph::new(text)
92+
.alignment(Alignment::Center)
93+
.style(Style::default().fg(Color::White))
94+
.block(Block::new().padding(Padding::horizontal(2)));
95+
96+
let alias = Paragraph::new(new_alias.value())
97+
.alignment(Alignment::Left)
98+
.style(Style::default().fg(Color::White))
99+
.block(
100+
Block::new()
101+
.bg(Color::DarkGray)
102+
.padding(Padding::horizontal(2)),
103+
);
104+
105+
frame.render_widget(msg, text_block);
106+
frame.render_widget(alias, alias_block);
107+
}
108+
}
109+
}

src/app.rs

Lines changed: 14 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,20 @@ use bluer::{
1212
use futures::FutureExt;
1313
use ratatui::{
1414
Frame,
15-
layout::{Alignment, Constraint, Layout, Margin, Rect},
16-
style::{Color, Modifier, Style, Stylize},
17-
text::{Line, Span},
15+
layout::{Constraint, Layout, Margin, Rect},
16+
style::{Color, Style, Stylize},
17+
text::Line,
1818
widgets::{
19-
Block, BorderType, Borders, Cell, Clear, Padding, Paragraph, Row, Scrollbar,
20-
ScrollbarOrientation, ScrollbarState, Table, TableState,
19+
Block, BorderType, Borders, Cell, Padding, Row, Scrollbar, ScrollbarOrientation,
20+
ScrollbarState, Table, TableState,
2121
},
2222
};
2323
use tokio::sync::mpsc::UnboundedSender;
2424
use tui_input::Input;
2525

2626
use crate::{
2727
agent::AuthAgent,
28+
alias::render_set_alias,
2829
bluetooth::Controller,
2930
config::{Config, Width},
3031
favorite::{read_favorite_devices_from_disk, save_favorite_devices_to_disk},
@@ -166,99 +167,6 @@ impl App {
166167
}
167168
}
168169

169-
pub fn render_set_alias(&mut self, frame: &mut Frame, area: Rect) {
170-
let block = Layout::vertical([
171-
Constraint::Fill(1),
172-
Constraint::Length(6),
173-
Constraint::Fill(1),
174-
])
175-
.split(area);
176-
177-
let block = Layout::horizontal([
178-
Constraint::Fill(1),
179-
Constraint::Max(70),
180-
Constraint::Fill(1),
181-
])
182-
.split(block[1])[1];
183-
184-
let (text_block, alias_block) = {
185-
let chunks = Layout::vertical(
186-
[
187-
Constraint::Length(1),
188-
Constraint::Length(3),
189-
Constraint::Length(1),
190-
Constraint::Length(2),
191-
]
192-
.as_ref(),
193-
)
194-
.split(block);
195-
196-
let area1 = Layout::horizontal(
197-
[
198-
Constraint::Length(1),
199-
Constraint::Fill(1),
200-
Constraint::Length(1),
201-
]
202-
.as_ref(),
203-
)
204-
.split(chunks[1]);
205-
206-
let area2 = Layout::horizontal(
207-
[
208-
Constraint::Percentage(20),
209-
Constraint::Fill(1),
210-
Constraint::Percentage(20),
211-
]
212-
.as_ref(),
213-
)
214-
.split(chunks[2]);
215-
216-
(area1[1], area2[1])
217-
};
218-
219-
frame.render_widget(Clear, block);
220-
frame.render_widget(
221-
Block::new()
222-
.borders(Borders::ALL)
223-
.border_type(BorderType::Thick)
224-
.style(Style::default().green())
225-
.border_style(Style::default().fg(Color::Green)),
226-
block,
227-
);
228-
229-
if let Some(selected_controller) = self.controller_state.selected() {
230-
let controller = &self.controllers[selected_controller];
231-
if let Some(index) = self.paired_devices_state.selected() {
232-
let name = controller.paired_devices[index].alias.as_str();
233-
234-
let text = Line::from(vec![
235-
Span::from("Enter the new name for "),
236-
Span::styled(
237-
name,
238-
Style::default().add_modifier(Modifier::BOLD | Modifier::ITALIC),
239-
),
240-
]);
241-
242-
let msg = Paragraph::new(text)
243-
.alignment(Alignment::Center)
244-
.style(Style::default().fg(Color::White))
245-
.block(Block::new().padding(Padding::horizontal(2)));
246-
247-
let alias = Paragraph::new(self.new_alias.value())
248-
.alignment(Alignment::Left)
249-
.style(Style::default().fg(Color::White))
250-
.block(
251-
Block::new()
252-
.bg(Color::DarkGray)
253-
.padding(Padding::horizontal(2)),
254-
);
255-
256-
frame.render_widget(msg, text_block);
257-
frame.render_widget(alias, alias_block);
258-
}
259-
}
260-
}
261-
262170
pub fn render(&mut self, frame: &mut Frame) {
263171
if let Some(selected_controller_index) = self.controller_state.selected() {
264172
let selected_controller = &self.controllers[selected_controller_index];
@@ -685,7 +593,14 @@ impl App {
685593

686594
// Set alias popup
687595
if self.focused_block == FocusedBlock::SetDeviceAliasBox {
688-
self.render_set_alias(frame, area);
596+
render_set_alias(
597+
&self.controllers,
598+
&self.controller_state,
599+
&self.paired_devices_state,
600+
&self.new_alias,
601+
frame,
602+
area,
603+
);
689604
}
690605

691606
// Request Confirmation

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
pub mod agent;
2+
mod alias;
23
pub mod app;
34
pub mod bluetooth;
45
pub mod cli;

0 commit comments

Comments
 (0)