From bc55617f81b880b5eb82f7f30ab6c15c86602986 Mon Sep 17 00:00:00 2001 From: Jaidyn Ann Date: Wed, 14 Jul 2021 15:13:36 -0500 Subject: [PATCH] (librunview) URL highlighting --- libs/librunview/RunView.cpp | 43 +++++++++++++++++++++++++++++++++++++ libs/librunview/RunView.h | 10 ++++++--- 2 files changed, 50 insertions(+), 3 deletions(-) diff --git a/libs/librunview/RunView.cpp b/libs/librunview/RunView.cpp index 4ca7221..0b8ec6f 100644 --- a/libs/librunview/RunView.cpp +++ b/libs/librunview/RunView.cpp @@ -17,6 +17,11 @@ RunView::RunView(const char* name) text_run run = { 0, BFont(), ui_color(B_PANEL_TEXT_COLOR) }; fDefaultRun = { 1, {run} }; + + BFont urlFont; + urlFont.SetFace(B_UNDERSCORE_FACE); + text_run urlRun = { 0, urlFont, ui_color(B_LINK_TEXT_COLOR) }; + fUrlRun = { 1, {urlRun} }; } @@ -42,3 +47,41 @@ RunView::Append(const char* text) Insert(text, &fDefaultRun); fLastStyled = false; } + + +void +RunView::Insert(const char* text, const text_run_array* runs) +{ + BString buf(text); + + int32 urlOffset = 0; + int32 lastEnd = 0; + while (lastEnd < buf.CountChars() && urlOffset != B_ERROR) + { + urlOffset = buf.FindFirst("://", lastEnd); + + int32 urlStart = buf.FindLast(" ", urlOffset) + 1; + int32 urlEnd = buf.FindFirst(" ", urlOffset); + if (urlStart == B_ERROR || urlOffset == B_ERROR) + urlStart = lastEnd; + if (urlEnd == B_ERROR || urlOffset == B_ERROR) + urlEnd = buf.CountChars(); + + BString url; + BString nonurl; + if (urlOffset == B_ERROR) + buf.CopyCharsInto(nonurl, urlStart, urlEnd - urlStart); + else { + buf.CopyCharsInto(nonurl, lastEnd, urlStart - lastEnd); + buf.CopyCharsInto(url, urlStart, urlEnd - urlStart); + } + + // Actually insert the text + if (nonurl.IsEmpty() == false) + BTextView::Insert(nonurl.String(), runs); + if (url.IsEmpty() == false) + BTextView::Insert(url.String(), &fUrlRun); + + lastEnd = urlEnd; + } +} diff --git a/libs/librunview/RunView.h b/libs/librunview/RunView.h index c376759..4e5c6ac 100644 --- a/libs/librunview/RunView.h +++ b/libs/librunview/RunView.h @@ -12,13 +12,17 @@ class RunView : public BTextView { public: RunView(const char* name); - void Append(const char* text, rgb_color color, - uint16 fontFace = B_REGULAR_FACE); - void Append(const char* text); + void Append(const char* text, rgb_color color, + uint16 fontFace = B_REGULAR_FACE); + void Append(const char* text); + + // Only differs in that it changes font face and color of any URLs + virtual void Insert(const char* text, const text_run_array* runs = NULL); private: bool fLastStyled; text_run_array fDefaultRun; + text_run_array fUrlRun; }; #endif // _RUN_VIEW_H