Split ui.rs into modules based on UI element #200

Merged
wojtek merged 13 commits from 135---split-ui-rs-into-modules-based-on-ui-element into main 2024-08-29 17:21:52 +02:00
3 changed files with 50 additions and 44 deletions
Showing only changes of commit 4a22c29eb8 - Show all commits

View File

@ -9,9 +9,10 @@ use ratatui::{
widgets::{List, ListItem, Paragraph},
};
use crate::tui::app::WidgetState;
use super::{UiColor, UiDisplay};
use crate::tui::{
app::WidgetState,
ui::{display::UiDisplay, style::UiColor},
};
pub struct ArtistArea {
pub list: Rect,

View File

@ -5,6 +5,7 @@ mod matches;
mod minibuffer;
mod overlay;
mod reload;
mod style;
use browse::{AlbumArea, AlbumState, ArtistArea, ArtistState, FrameArea, TrackArea, TrackState};
use display::UiDisplay;
@ -15,28 +16,16 @@ use musichoard::collection::{album::Album, track::Track, Collection};
use overlay::{OverlayBuilder, OverlaySize};
use ratatui::{
layout::{Alignment, Rect},
style::{Color, Style},
widgets::{Block, BorderType, Borders, Clear, List, Paragraph, Wrap},
Frame,
};
use reload::ReloadMenu;
use crate::tui::{
app::{AppPublicState, AppState, Category, IAppAccess, Selection, WidgetState},
lib::interface::musicbrainz::Match,
ui::{reload::ReloadMenu, style::UiStyle},
};
pub struct UiColor;
impl UiColor {
const BG: Color = Color::Black;
const BG_HL: Color = Color::DarkGray;
const FG: Color = Color::White;
const FG_ERR: Color = Color::Red;
const FG_WARN: Color = Color::LightYellow;
const FG_GOOD: Color = Color::LightGreen;
}
pub trait IUi {
fn render<APP: IAppAccess>(app: &mut APP, frame: &mut Frame);
}
@ -49,30 +38,8 @@ struct Column<'a> {
pub struct Ui;
impl Ui {
fn style(_active: bool, error: bool) -> Style {
let style = Style::default().bg(UiColor::BG);
if error {
style.fg(UiColor::FG_ERR)
} else {
style.fg(UiColor::FG)
}
}
fn block_style(active: bool, error: bool) -> Style {
Self::style(active, error)
}
fn highlight_style(active: bool) -> Style {
// Do not set the fg color here as it will overwrite any list-specific customisation.
if active {
Style::default().bg(UiColor::BG_HL)
} else {
Style::default().bg(UiColor::BG)
}
}
fn block<'a>(active: bool, error: bool) -> Block<'a> {
Block::default().style(Self::block_style(active, error))
Block::default().style(UiStyle::block_style(active, error))
}
fn block_with_borders<'a>(title: &str, active: bool, error: bool) -> Block<'a> {
@ -92,9 +59,9 @@ impl Ui {
frame: &mut Frame,
) {
frame.render_stateful_widget(
list.highlight_style(Self::highlight_style(active))
list.highlight_style(UiStyle::highlight_style(active))
.highlight_symbol(">> ")
.style(Self::style(active, false))
.style(UiStyle::style(active, false))
.block(Self::block_with_borders(title, active, false)),
area,
&mut state.list,
@ -123,7 +90,7 @@ impl Ui {
) {
frame.render_widget(
paragraph
.style(Self::style(active, false))
.style(UiStyle::style(active, false))
.block(Self::block_with_borders(title, active, false)),
area,
);
@ -139,7 +106,7 @@ impl Ui {
frame.render_widget(Clear, area);
frame.render_widget(
paragraph
.style(Self::style(true, error))
.style(UiStyle::style(true, error))
.block(Self::block_with_borders(title, true, error)),
area,
);
@ -186,7 +153,7 @@ impl Ui {
frame.render_widget(
column
.paragraph
.style(Self::style(active, false))
.style(UiStyle::style(active, false))
.block(Self::block(active, false)),
column.area,
);

38
src/tui/ui/style.rs Normal file
View File

@ -0,0 +1,38 @@
use ratatui::style::{Color, Style};
pub struct UiColor;
impl UiColor {
pub const BG: Color = Color::Black;
pub const BG_HL: Color = Color::DarkGray;
pub const FG: Color = Color::White;
pub const FG_ERR: Color = Color::Red;
pub const FG_WARN: Color = Color::LightYellow;
pub const FG_GOOD: Color = Color::LightGreen;
}
pub struct UiStyle;
impl UiStyle {
pub fn style(_active: bool, error: bool) -> Style {
let style = Style::default().bg(UiColor::BG);
if error {
style.fg(UiColor::FG_ERR)
} else {
style.fg(UiColor::FG)
}
}
pub fn block_style(active: bool, error: bool) -> Style {
Self::style(active, error)
}
pub fn highlight_style(active: bool) -> Style {
// Do not set the fg color here as it will overwrite any list-specific customisation.
if active {
Style::default().bg(UiColor::BG_HL)
} else {
Style::default().bg(UiColor::BG)
}
}
}