Skip to content

Commit

Permalink
add color to VirtWndText; parse promote
Browse files Browse the repository at this point in the history
  • Loading branch information
kjk committed Aug 11, 2024
1 parent ad251b2 commit a26873b
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 13 deletions.
72 changes: 65 additions & 7 deletions src/HomePage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "utils/Dpi.h"
#include "utils/FileUtil.h"
#include "utils/WinUtil.h"
#include "utils/SquareTreeParser.h"

#include "wingui/UIModels.h"
#include "wingui/Layout.h"
Expand Down Expand Up @@ -37,12 +38,17 @@
#endif
#define ABOUT_LINE_SEP_SIZE 1

constexpr const char* builtIn = R"(
constexpr const char* promoteBuiltIn = R"(
[
Name = Edna
URL = https://edna.arslexis.io
Info = note taking app for develelopers
]
[
Name = "ArsLexis"
URL = https://arslexis.io
Info = Various web tools
]
)";

constexpr COLORREF kAboutBorderCol = RGB(0, 0, 0);
Expand Down Expand Up @@ -612,31 +618,83 @@ struct ThumbnailLayout {
StaticLinkInfo* sl = nullptr;
};

struct Promote {
struct Promote* next = nullptr;
const char* name = nullptr;
const char* url = nullptr;
const char* info = nullptr;
Promote() = default;
~Promote();
};

Promote::~Promote() {
str::Free(name);
str::Free(url);
str::Free(info);
}

struct HomePageLayout {
// args in
HWND hwnd;
HDC hdc;
HWND hwnd = nullptr;
HDC hdc = nullptr;
Rect rc;
MainWindow* win;
MainWindow* win = nullptr;

Promote* promote = nullptr;

Rect rcAppWithVer; // SumatraPDF colorful text + version
Rect rcLine; // line under bApp
Rect rcIconOpen;

HIMAGELIST himlOpen;
VirtWndText* freqRead;
VirtWndText* openDoc;
HIMAGELIST himlOpen = nullptr;
VirtWndText* freqRead = nullptr;
VirtWndText* openDoc = nullptr;
VirtWndText* hideShowFreqRead = nullptr;
Vec<ThumbnailLayout> thumbnails; // info for each thumbnail
~HomePageLayout();
};

HomePageLayout::~HomePageLayout() {
delete freqRead;
delete openDoc;
ListDelete(promote);
}

static Promote* ParsePromote(const char* s) {
if (str::IsEmptyOrWhiteSpace(s)) {
return nullptr;
}
SquareTreeNode* root = ParseSquareTree(s);
if (!root) {
return nullptr;
}
SquareTreeNode* node;
Promote* first = nullptr;
for (auto& i : root->data) {
node = i.child;
if (!node || node->data.Size() != 3) {
continue;
}
Promote* p = new Promote();
p->name = str::Dup(node->GetValue("Name"));
p->url = str::Dup(node->GetValue("URL"));
p->info = str::Dup(node->GetValue("Info"));
bool ok = !str::IsEmptyOrWhiteSpace(p->name) && !str::IsEmptyOrWhiteSpace(p->url) && !str::IsEmptyOrWhiteSpace(p->info);
if (!ok) {
delete p;
continue;
}
ListInsertEnd(&first, p);
}
delete root;
return first;
}

// layout homepage in r
void LayoutHomePage(HomePageLayout& l) {

l.promote = ParsePromote(promoteBuiltIn);

Vec<FileState*> fileStates;
gFileHistory.GetFrequencyOrder(fileStates);
auto hwnd = l.hwnd;
Expand Down
16 changes: 10 additions & 6 deletions src/utils/SquareTreeParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,11 +99,13 @@ SquareTreeNode::~SquareTreeNode() {
}

const char* SquareTreeNode::GetValue(const char* key, size_t* startIdx) const {
for (size_t i = startIdx ? *startIdx : 0; i < data.size(); i++) {
DataItem& item = data.at(i);
int start = startIdx ? (int)*startIdx : 0;
int n = data.Size();
for (int i = start; i < n; i++) {
DataItem& item = data.At(i);
if (str::EqI(key, item.key) && !item.child) {
if (startIdx) {
*startIdx = i + 1;
*startIdx = (size_t)(i + 1);
}
return item.str;
}
Expand All @@ -112,11 +114,13 @@ const char* SquareTreeNode::GetValue(const char* key, size_t* startIdx) const {
}

SquareTreeNode* SquareTreeNode::GetChild(const char* key, size_t* startIdx) const {
for (size_t i = startIdx ? *startIdx : 0; i < data.size(); i++) {
DataItem& item = data.at(i);
int start = startIdx ? (int)*startIdx : 0;
int n = data.Size();
for (int i = start; i < n; i++) {
DataItem& item = data.At(i);
if (str::EqI(key, item.key) && item.child) {
if (startIdx) {
*startIdx = i + 1;
*startIdx = (size_t)(i + 1);
}
return item.child;
}
Expand Down
14 changes: 14 additions & 0 deletions src/wingui/VirtWnd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,12 @@ Size VirtWndText::GetIdealSize(bool onlyIfEmpty) {

void VirtWndText::Draw(HDC hdc) {
ReportIf(lastBounds.IsEmpty());
COLORREF prevCol = kColorUnset;
int prevBkMode = 0;
if (textColor != kColorUnset) {
prevCol = SetTextColor(hdc, textColor);
}
prevBkMode = SetBkMode(hdc, TRANSPARENT);
UINT fmt = DT_NOCLIP | DT_NOPREFIX;
if (isRtl) {
fmt = fmt | DT_RTLREADING;
Expand All @@ -94,6 +100,14 @@ void VirtWndText::Draw(HDC hdc) {
if (withUnderline) {
auto& r = lastBounds;
Rect lineRect = {r.x, r.y + sz.dy, sz.dx, 0};
auto col = GetTextColor(hdc);
ScopedSelectObject pen(hdc, CreatePen(PS_SOLID, 1, col), true);
DrawLine(hdc, lineRect);
}
if (textColor != kColorUnset) {
SetTextColor(hdc, prevCol);
}
if (prevBkMode != 0) {
SetBkMode(hdc, prevBkMode);
}
}
1 change: 1 addition & 0 deletions src/wingui/VirtWnd.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ struct VirtWndText : VirtWnd {
HFONT font = nullptr;
bool withUnderline = false;
bool isRtl = false;
COLORREF textColor = kColorUnset;

Size sz = {0, 0};

Expand Down

1 comment on commit a26873b

@GitHubRulesOK
Copy link
Collaborator

@GitHubRulesOK GitHubRulesOK commented on a26873b Aug 12, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting you may want to edit last entry in https://arslexis.io landing page to remove docs from https://www.sumatrapdfreader.org/docs/img/homepage.png as this works SumatraPDF screenshot but its a bit tall :-)

Please sign in to comment.