Skip to content

Commit

Permalink
adds bold property on label & button (#74)
Browse files Browse the repository at this point in the history
  • Loading branch information
qxb3 authored Feb 7, 2025
1 parent 9dc8516 commit e6e7fec
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 7 deletions.
7 changes: 7 additions & 0 deletions src/config/defaults.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,15 @@ pub fn layout() -> Vec<FumWidget> {
text: "$title".to_string(),
align: LabelAlignment::Center,
truncate: true,
bold: false,
bg: None,
fg: None
},
FumWidget::Label {
text: "$artists".to_string(),
align: LabelAlignment::Center,
truncate: true,
bold: false,
bg: None,
fg: None
},
Expand All @@ -89,6 +91,7 @@ pub fn layout() -> Vec<FumWidget> {
text: "󰒮".to_string(),
action: Some(Action::Prev),
exec: None,
bold: false,
bg: None,
fg: None
},
Expand All @@ -97,6 +100,7 @@ pub fn layout() -> Vec<FumWidget> {
text: "$status_icon".to_string(),
action: Some(Action::PlayPause),
exec: None,
bold: false,
bg: None,
fg: None
},
Expand All @@ -105,6 +109,7 @@ pub fn layout() -> Vec<FumWidget> {
text: "󰒭".to_string(),
action: Some(Action::Next),
exec: None,
bold: false,
bg: None,
fg: None
}
Expand Down Expand Up @@ -138,13 +143,15 @@ pub fn layout() -> Vec<FumWidget> {
text: "$position".to_string(),
align: LabelAlignment::Left,
truncate: false,
bold: false,
bg: None,
fg: None
},
FumWidget::Label {
text: "$length".to_string(),
align: LabelAlignment::Right,
truncate: false,
bold: false,
bg: None,
fg: None
}
Expand Down
11 changes: 9 additions & 2 deletions src/widget/button.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use ratatui::{buffer::Buffer, layout::Rect, style::Stylize, widgets::{Block, Paragraph, Widget}};
use ratatui::{buffer::Buffer, layout::Rect, style::{Modifier, Stylize}, widgets::{Block, Paragraph, Widget}};

use crate::{get_color, state::FumState, text::replace_text};

use super::FumWidget;

pub fn render(widget: &FumWidget, area: Rect, buf: &mut Buffer, state: &mut FumState) {
if let FumWidget::Button { id, text, action, exec, bg, fg } = widget {
if let FumWidget::Button { id, text, action, exec, bold, bg, fg } = widget {
let text = replace_text(text, state).to_string();

state.buttons.insert(
Expand All @@ -15,12 +15,19 @@ pub fn render(widget: &FumWidget, area: Rect, buf: &mut Buffer, state: &mut FumS

let (bg, fg) = get_color!(bg, fg, &state.parent_bg, &state.parent_fg);

// Whether the text is bold
let bold = match bold {
true => Modifier::BOLD,
false => Modifier::default()
};

// Render bg
Block::new()
.bg(*bg)
.render(area, buf);

Paragraph::new(text)
.add_modifier(bold)
.fg(*fg)
.render(area, buf);
}
Expand Down
16 changes: 11 additions & 5 deletions src/widget/label.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,28 @@
use ratatui::{buffer::Buffer, layout::Rect, style::Stylize, widgets::{Block, Paragraph, Widget}};
use ratatui::{buffer::Buffer, layout::Rect, style::{Modifier, Stylize}, widgets::{Block, Paragraph, Widget}};

use crate::{get_color, state::FumState, text::replace_text, utils};

use super::{FumWidget, LabelAlignment};

pub fn render(widget: &FumWidget, area: Rect, buf: &mut Buffer, state: &mut FumState) {
if let FumWidget::Label { text, truncate, align, bg, fg } = widget {
if let FumWidget::Label { text, truncate, align, bold, bg, fg } = widget {
let text = match truncate {
true => utils::etc::truncate(&replace_text(text, state), area.width.into()),
false => replace_text(text, state)
};

let (bg, fg) = get_color!(bg, fg, &state.parent_bg, &state.parent_fg);

// Whether the text is bold
let bold = match bold {
true => Modifier::BOLD,
false => Modifier::default()
};

let widget = match align {
LabelAlignment::Left => Paragraph::new(text).left_aligned().fg(*fg),
LabelAlignment::Center => Paragraph::new(text).centered().fg(*fg),
LabelAlignment::Right => Paragraph::new(text).right_aligned().fg(*fg),
LabelAlignment::Left => Paragraph::new(text).left_aligned().fg(*fg).add_modifier(bold),
LabelAlignment::Center => Paragraph::new(text).centered().fg(*fg).add_modifier(bold),
LabelAlignment::Right => Paragraph::new(text).right_aligned().fg(*fg).add_modifier(bold),
};

// Render bg
Expand Down
5 changes: 5 additions & 0 deletions src/widget/widget.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use super::{button, container, cover_art, empty, label, progress, volume};

fn default_truncate() -> bool { true }
fn default_border() -> bool { false }
fn default_bold() -> bool { false }

#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "lowercase")]
Expand Down Expand Up @@ -146,6 +147,8 @@ pub enum FumWidget {
align: LabelAlignment,
#[serde(default = "default_truncate")]
truncate: bool,
#[serde(default = "default_bold")]
bold: bool,
bg: Option<Color>,
fg: Option<Color>
},
Expand All @@ -155,6 +158,8 @@ pub enum FumWidget {
text: String,
action: Option<Action>,
exec: Option<String>,
#[serde(default = "default_bold")]
bold: bool,
bg: Option<Color>,
fg: Option<Color>
},
Expand Down

0 comments on commit e6e7fec

Please sign in to comment.