Completed the refactor and upgrade to Ratatui v0.26. Next up: Refactoring all of the "draw_" functions into custom widgets for more ergonomic and extensible DevX

This commit is contained in:
2024-02-08 13:58:45 -07:00
parent c6f51ab9b6
commit 9b0c272e76
20 changed files with 529 additions and 870 deletions
@@ -1,6 +1,5 @@
use ratatui::layout::{Constraint, Rect};
use ratatui::layout::{Constraint, Flex, Layout, Rect};
use ratatui::Frame;
use std::iter;
use crate::app::App;
use crate::models::servarr_data::radarr::radarr_data::{
@@ -8,10 +7,7 @@ use crate::models::servarr_data::radarr::radarr_data::{
};
use crate::models::Route;
use crate::ui::radarr_ui::indexers::draw_indexers;
use crate::ui::utils::{
horizontal_chunks, horizontal_chunks_with_margin, title_block_centered, vertical_chunks,
vertical_chunks_with_margin,
};
use crate::ui::utils::title_block_centered;
use crate::ui::{
draw_button, draw_checkbox_with_label, draw_popup_over, draw_text_box_with_label, loading,
DrawUi, LabeledTextBoxProps,
@@ -32,11 +28,11 @@ impl DrawUi for IndexerSettingsUi {
false
}
fn draw(f: &mut Frame<'_>, app: &mut App<'_>, content_rect: Rect) {
fn draw(f: &mut Frame<'_>, app: &mut App<'_>, area: Rect) {
draw_popup_over(
f,
app,
content_rect,
area,
draw_indexers,
draw_edit_indexer_settings_prompt,
70,
@@ -45,7 +41,7 @@ impl DrawUi for IndexerSettingsUi {
}
}
fn draw_edit_indexer_settings_prompt(f: &mut Frame<'_>, app: &mut App<'_>, prompt_area: Rect) {
fn draw_edit_indexer_settings_prompt(f: &mut Frame<'_>, app: &mut App<'_>, area: Rect) {
let block = title_block_centered("Configure All Indexer Settings");
let yes_no_value = app.data.radarr_data.prompt_confirm;
let selected_block = app.data.radarr_data.selected_block.get_active_block();
@@ -54,46 +50,41 @@ fn draw_edit_indexer_settings_prompt(f: &mut Frame<'_>, app: &mut App<'_>, promp
if indexer_settings_option.is_some() {
let indexer_settings = indexer_settings_option.as_ref().unwrap();
f.render_widget(block, prompt_area);
f.render_widget(block, area);
let chunks = vertical_chunks_with_margin(
vec![Constraint::Min(0), Constraint::Length(3)],
prompt_area,
1,
);
let [settings_area, buttons_area] =
Layout::vertical([Constraint::Fill(0), Constraint::Length(3)])
.margin(1)
.areas(area);
let split_chunks = horizontal_chunks_with_margin(
vec![Constraint::Ratio(1, 2), Constraint::Ratio(1, 2)],
chunks[0],
1,
);
let [left_side_area, right_side_area] =
Layout::horizontal([Constraint::Ratio(1, 2), Constraint::Ratio(1, 2)])
.margin(1)
.areas(settings_area);
let left_chunks = vertical_chunks(
vec![
let [min_age_area, retention_area, max_size_area, prefer_flags_area, _] = Layout::vertical([
Constraint::Length(3),
Constraint::Length(3),
Constraint::Length(3),
Constraint::Length(3),
Constraint::Fill(0),
])
.areas(left_side_area);
let [availability_delay_area, rss_sync_interval_area, whitelisted_sub_tags_area, allow_hardcoded_subs_area, _] =
Layout::vertical([
Constraint::Length(3),
Constraint::Length(3),
Constraint::Length(3),
Constraint::Length(3),
Constraint::Min(0),
],
split_chunks[0],
);
let right_chunks = vertical_chunks(
vec![
Constraint::Length(3),
Constraint::Length(3),
Constraint::Length(3),
Constraint::Length(3),
Constraint::Min(0),
],
split_chunks[1],
);
Constraint::Fill(0),
])
.areas(right_side_area);
if let Route::Radarr(active_radarr_block, _) = *app.get_current_route() {
draw_text_box_with_label(
f,
LabeledTextBoxProps {
area: left_chunks[0],
area: min_age_area,
label: "Minimum Age (minutes) ▴▾",
text: &indexer_settings.minimum_age.to_string(),
offset: 0,
@@ -106,7 +97,7 @@ fn draw_edit_indexer_settings_prompt(f: &mut Frame<'_>, app: &mut App<'_>, promp
draw_text_box_with_label(
f,
LabeledTextBoxProps {
area: left_chunks[1],
area: retention_area,
label: "Retention (days) ▴▾",
text: &indexer_settings.retention.to_string(),
offset: 0,
@@ -119,7 +110,7 @@ fn draw_edit_indexer_settings_prompt(f: &mut Frame<'_>, app: &mut App<'_>, promp
draw_text_box_with_label(
f,
LabeledTextBoxProps {
area: left_chunks[2],
area: max_size_area,
label: "Maximum Size (MB) ▴▾",
text: &indexer_settings.maximum_size.to_string(),
offset: 0,
@@ -132,7 +123,7 @@ fn draw_edit_indexer_settings_prompt(f: &mut Frame<'_>, app: &mut App<'_>, promp
draw_text_box_with_label(
f,
LabeledTextBoxProps {
area: right_chunks[0],
area: availability_delay_area,
label: "Availability Delay (days) ▴▾",
text: &indexer_settings.availability_delay.to_string(),
offset: 0,
@@ -145,7 +136,7 @@ fn draw_edit_indexer_settings_prompt(f: &mut Frame<'_>, app: &mut App<'_>, promp
draw_text_box_with_label(
f,
LabeledTextBoxProps {
area: right_chunks[1],
area: rss_sync_interval_area,
label: "RSS Sync Interval (minutes) ▴▾",
text: &indexer_settings.rss_sync_interval.to_string(),
offset: 0,
@@ -158,7 +149,7 @@ fn draw_edit_indexer_settings_prompt(f: &mut Frame<'_>, app: &mut App<'_>, promp
draw_text_box_with_label(
f,
LabeledTextBoxProps {
area: right_chunks[2],
area: whitelisted_sub_tags_area,
label: "Whitelisted Subtitle Tags",
text: &indexer_settings.whitelisted_hardcoded_subs.text,
offset: *indexer_settings.whitelisted_hardcoded_subs.offset.borrow(),
@@ -173,7 +164,7 @@ fn draw_edit_indexer_settings_prompt(f: &mut Frame<'_>, app: &mut App<'_>, promp
draw_checkbox_with_label(
f,
left_chunks[3],
prefer_flags_area,
"Prefer Indexer Flags",
indexer_settings.prefer_indexer_flags,
selected_block == &ActiveRadarrBlock::IndexerSettingsTogglePreferIndexerFlags,
@@ -181,30 +172,20 @@ fn draw_edit_indexer_settings_prompt(f: &mut Frame<'_>, app: &mut App<'_>, promp
draw_checkbox_with_label(
f,
right_chunks[3],
allow_hardcoded_subs_area,
"Allow Hardcoded Subs",
indexer_settings.allow_hardcoded_subs,
selected_block == &ActiveRadarrBlock::IndexerSettingsToggleAllowHardcodedSubs,
);
let button_chunks = horizontal_chunks(
iter::repeat(Constraint::Ratio(1, 4)).take(4).collect(),
chunks[1],
);
let [save_area, cancel_area] =
Layout::horizontal([Constraint::Percentage(25), Constraint::Percentage(25)])
.flex(Flex::Center)
.areas(buttons_area);
draw_button(
f,
button_chunks[1],
"Save",
yes_no_value && highlight_yes_no,
);
draw_button(
f,
button_chunks[2],
"Cancel",
!yes_no_value && highlight_yes_no,
);
draw_button(f, save_area, "Save", yes_no_value && highlight_yes_no);
draw_button(f, cancel_area, "Cancel", !yes_no_value && highlight_yes_no);
} else {
loading(f, block, prompt_area, app.is_loading);
loading(f, block, area, app.is_loading);
}
}