From ab70326452dd0cef77cd315450f581c790757f55 Mon Sep 17 00:00:00 2001 From: "Chris (ChrisJr404)" <11917633+ChrisJr404@users.noreply.github.com> Date: Tue, 25 Aug 2026 08:35:05 -0400 Subject: [PATCH] Add TextArea::set_style and style to customize text style --- CHANGELOG.md | 1 + cursive-core/src/views/text_area.rs | 66 ++++++++++++++++++++++++++--- 2 files changed, 61 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e50eb5b7..01f06d5f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ## Unreleased +- Add `TextArea::set_style` and `TextArea::style` to customize the text style. - Support tuple structs in `wrap_impl!`. - `Cursive::cb_sink` now returns a `CbSink` instead of a reference to one. - Fix `ProgressBar` to inherit the background color instead of hard-coding it. diff --git a/cursive-core/src/views/text_area.rs b/cursive-core/src/views/text_area.rs index 5da23516..013d3c83 100644 --- a/cursive-core/src/views/text_area.rs +++ b/cursive-core/src/views/text_area.rs @@ -4,7 +4,7 @@ use crate::{ direction::Direction, event::{Event, EventResult, Key, MouseButton, MouseEvent}, rect::Rect, - style::PaletteStyle, + style::{PaletteStyle, StyleType}, utils::lines::simple::{LinesIterator, Row, prefix, simple_prefix}, view::{CannotFocus, ScrollBase, SizeCache, View}, {Printer, With, XY}, @@ -54,6 +54,15 @@ pub struct TextArea { /// Byte offset of the currently selected grapheme. cursor: usize, + + /// Style used for the text when the view is enabled. + regular_style: StyleType, + + /// Style used for the text when the view is disabled. + inactive_style: StyleType, + + /// Style used for the cursor. + cursor_style: StyleType, } fn make_rows(text: &str, width: usize) -> Vec { @@ -76,6 +85,9 @@ impl TextArea { size_cache: None, last_size: Vec2::zero(), cursor: 0, + regular_style: PaletteStyle::EditableText.into(), + inactive_style: PaletteStyle::EditableTextInactive.into(), + cursor_style: PaletteStyle::EditableTextCursor.into(), } .with(|area| area.compute_rows(Vec2::new(1, 1))) // Make sure we have valid rows, even for empty text. @@ -140,6 +152,29 @@ impl TextArea { self.with(|s| s.set_content(content)) } + /// Sets the style used for the text. + /// + /// Defaults to `PaletteStyle::EditableText`. + pub fn set_style>(&mut self, style: S) { + self.regular_style = style.into(); + } + + /// Sets the style used for the text. + /// + /// Chainable variant. + /// + /// # Examples + /// + /// ```rust + /// # use cursive_core::views::TextArea; + /// # use cursive_core::style::PaletteColor; + /// let text_area = TextArea::new().style(PaletteColor::Primary); + /// ``` + #[must_use] + pub fn style>(self, style: S) -> Self { + self.with(|s| s.set_style(style)) + } + /// Disables this view. /// /// A disabled view cannot be selected. @@ -489,12 +524,9 @@ impl View for TextArea { fn draw(&self, printer: &Printer) { let (style, cursor_style) = if self.enabled && printer.enabled { - (PaletteStyle::EditableText, PaletteStyle::EditableTextCursor) + (self.regular_style, self.cursor_style) } else { - ( - PaletteStyle::EditableTextInactive, - PaletteStyle::EditableTextInactive, - ) + (self.inactive_style, self.inactive_style) }; let w = if self.scrollbase.scrollable() { @@ -658,3 +690,25 @@ impl View for TextArea { struct Blueprint { content: Option, } + +#[cfg(test)] +mod tests { + use super::*; + use crate::style::PaletteColor; + + #[test] + fn set_style_overrides_default() { + let mut area = TextArea::new(); + assert_eq!(area.regular_style, PaletteStyle::EditableText.into()); + + area.set_style(PaletteColor::Primary); + assert_eq!(area.regular_style, PaletteColor::Primary.into()); + + // Setting the text style leaves the cursor and inactive styles alone. + assert_eq!(area.cursor_style, PaletteStyle::EditableTextCursor.into()); + assert_eq!( + area.inactive_style, + PaletteStyle::EditableTextInactive.into() + ); + } +}