From 9105063938b3272bab39e76074398fec8a097bb7 Mon Sep 17 00:00:00 2001 From: John Scipione Date: Mon, 9 Sep 2024 12:44:20 -0400 Subject: [PATCH] BTextView: Use document colors on AdoptSystemColors() AdoptSystemColors() tints document background color to match panel color if uneditable. Calling MakeEditable() will automatically apply or unapply uneditable background tint if you have previously called AdoptSystemColors(). Parent BView::AdoptSystemColors() sets panel colors, we want document colors here. Do not alter text color - only view, low and high colors are changed. Document AdoptSystemColors() and MakeEditable() in BTextView docs. Change-Id: Ib215735f27bb01fc2f95fcf2fee0185e5fc83f70 Reviewed-on: https://review.haiku-os.org/c/haiku/+/8263 Haiku-Format: Haiku-format Bot Reviewed-by: waddlesplash Tested-by: Commit checker robot --- docs/user/interface/TextView.dox | 37 +++++++++++++++++++++++++++++- headers/os/interface/TextView.h | 5 ++++ src/kits/interface/TextView.cpp | 39 ++++++++++++++++++++++++++++++-- 3 files changed, 78 insertions(+), 3 deletions(-) diff --git a/docs/user/interface/TextView.dox b/docs/user/interface/TextView.dox index b149605389..de9f7e2d5b 100644 --- a/docs/user/interface/TextView.dox +++ b/docs/user/interface/TextView.dox @@ -183,6 +183,8 @@ to be selectable or editable off. A non-editable, non-selectable text view can be useful as a BStringView that wraps. + Call AdoptSystemColors() to set default colors. + Call MakeResizable() to make the view width track with text width, this can be useful for short single-line text views. @@ -1226,9 +1228,14 @@ \fn void BTextView::MakeEditable(bool editable) \brief Sets whether or not the text is editable. + Will automatically tint document colors to indicate uneditable + if you have previously called AdoptSystemColors(). + \param editable Pass in \c true to set the text to be editable, \c false to set the object to be not editable. + \see AdoptSystemColors() + \since BeOS R3 */ @@ -1341,13 +1348,41 @@ /*! - \name Word Wrap + \name Color Adoption */ //! @{ +/*! + \fn BTextView::AdoptSystemColors() + \brief Adopts document colors tinted to match panel background if uneditable. + + - \c B_DOCUMENT_BACKGROUND_COLOR for ViewUIColor() + - \c B_DOCUMENT_BACKGROUND_COLOR for LowUIColor() + - \c B_DOCUMENT_TEXT_COLOR for HighUIColor() + + View and low colors tinted \c B_DARKEN_1_TINT (inverse on dark) if uneditable. + + Does not alter text color. + + \see BView::AdoptSystemColors(), MakeEditable() + + \since Haiku R1 +*/ + + +//! @} + + +/*! + \name Word Wrap +*/ + + +//! @{ + /*! \fn void BTextView::SetWordWrap(bool wrap) diff --git a/headers/os/interface/TextView.h b/headers/os/interface/TextView.h index 3e02971d80..ec9e109c31 100644 --- a/headers/os/interface/TextView.h +++ b/headers/os/interface/TextView.h @@ -131,6 +131,8 @@ public: void SelectAll(); void GetSelection(int32* _start, int32* _end) const; + void AdoptSystemColors(); + void SetFontAndColor(const BFont* font, uint32 mode = B_FONT_ALL, const rgb_color* color = NULL); @@ -426,6 +428,9 @@ private: float _TextHeight(); BRect _TextRect(); + float _UneditableTint(); + bool _UsesSystemColors(); + private: BPrivate::TextGapBuffer* fText; LineBuffer* fLines; diff --git a/src/kits/interface/TextView.cpp b/src/kits/interface/TextView.cpp index 878fab540d..d010df2686 100644 --- a/src/kits/interface/TextView.cpp +++ b/src/kits/interface/TextView.cpp @@ -1589,8 +1589,20 @@ BTextView::GetSelection(int32* _start, int32* _end) const void -BTextView::SetFontAndColor(const BFont* font, uint32 mode, - const rgb_color* color) +BTextView::AdoptSystemColors() +{ + if (IsEditable()) + SetViewUIColor(B_DOCUMENT_BACKGROUND_COLOR); + else + SetViewUIColor(B_DOCUMENT_BACKGROUND_COLOR, _UneditableTint()); + + SetLowUIColor(ViewUIColor()); + SetHighUIColor(B_DOCUMENT_TEXT_COLOR); +} + + +void +BTextView::SetFontAndColor(const BFont* font, uint32 mode, const rgb_color* color) { SetFontAndColor(fSelStart, fSelEnd, font, mode, color); } @@ -2371,6 +2383,11 @@ BTextView::MakeEditable(bool editable) return; fEditable = editable; + + // apply uneditable colors or unapply them + if (_UsesSystemColors()) + AdoptSystemColors(); + // TextControls change the color of the text when // they are made editable, so we need to invalidate // the NULL style here @@ -6093,6 +6110,24 @@ BTextView::_TextRect() } +float +BTextView::_UneditableTint() +{ + return ui_color(B_DOCUMENT_BACKGROUND_COLOR).IsLight() ? B_DARKEN_1_TINT : 0.853; +} + + +bool +BTextView::_UsesSystemColors() +{ + float tint = B_NO_TINT; + + return ViewUIColor(&tint) == B_DOCUMENT_BACKGROUND_COLOR + && LowUIColor(&tint) == B_DOCUMENT_BACKGROUND_COLOR + && HighUIColor() == B_DOCUMENT_TEXT_COLOR; +} + + // #pragma mark - BTextView::TextTrackState