From 21e96c692595f204b91431a90123419e4a1780c4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?John=20Ankarstr=C3=B6m?= Date: Fri, 22 Jul 2022 23:52:22 +0200 Subject: Cache GetSystemMetrics values with variable template. The variable template could be generalized like this: template const auto cache = F(A...); and instantiated like: cache It would still be limited to constant function arguments, which usually isn't a problem for GetSystemMetrics, but might be for other functions. --- c/common.h | 14 +++++++++----- c/datalistview.cpp | 3 +-- c/episodelistview.cpp | 3 +-- c/main.cpp | 3 --- 4 files changed, 11 insertions(+), 12 deletions(-) diff --git a/c/common.h b/c/common.h index 2c0be13..c6c7dbd 100644 --- a/c/common.h +++ b/c/common.h @@ -54,7 +54,11 @@ std::optional maybe_make(U... xs) } } -/* Call Windows API function, throwing error on NULL. */ +/* Variable template for caching values from GetSystemMetrics. */ +template +const auto Metric = GetSystemMetrics(I); + +/* Check result of Windows API call, throwing error on NULL. */ template inline T require(const T x) { @@ -62,7 +66,7 @@ inline T require(const T x) return x; } -/* Call Windows API function, showing a warning on NULL. */ +/* Check result of Windows API call, showing a warning on NULL. */ template inline T prefer(const T x) { @@ -75,7 +79,7 @@ inline T prefer(const T x) } /* Conditionally choose between two values. This template is necessary - * because the ternary conditional operator chooses only between + * because the ternary conditional operator can choose only between * values of the same type. */ template constexpr std::enable_if_t choose(X x, Y) { return x; } @@ -83,10 +87,10 @@ template constexpr std::enable_if_t choose(X, Y y) { return y; } /* Conditionally choose between ANSI and wide string literal. */ -#define AWTEXT(t, s) choose>(s, L"" s) +#define AWTEXT(t, s) choose>(L"" s, s) /* Conditionally choose between ANSI and wide Windows API function. */ -#define AWFUN(t, f) choose>(f##A, f##W) +#define AWFUN(t, f) choose>(f##W, f##A) /* Return integer scaled for current DPI. */ inline int Dpi(const int i) diff --git a/c/datalistview.cpp b/c/datalistview.cpp index c191901..9ff4659 100644 --- a/c/datalistview.cpp +++ b/c/datalistview.cpp @@ -31,12 +31,11 @@ DataListView::DataListView(const HWND hWndParent) void DataListView::ResizeColumns(RECT& rcParent) { - extern int g_cxVScroll; ListView_SetColumnWidth(hWnd, DLVSIKEY, LVSCW_AUTOSIZE); const int cxColumn = ListView_GetColumnWidth(hWnd, 0)+4; ListView_SetColumnWidth(hWnd, DLVSIKEY, cxColumn); - ListView_SetColumnWidth(hWnd, DLVSIVALUE, rcParent.right-cxColumn-g_cxVScroll-4); + ListView_SetColumnWidth(hWnd, DLVSIVALUE, rcParent.right-cxColumn-Metric-4); } void DataListView::ShowEpisode(const int iEpisode) diff --git a/c/episodelistview.cpp b/c/episodelistview.cpp index 6f12a0f..17ae373 100644 --- a/c/episodelistview.cpp +++ b/c/episodelistview.cpp @@ -143,14 +143,13 @@ void EpisodeListView::Redraw() void EpisodeListView::ResizeColumns(RECT& rcParent) { - extern int g_cxVScroll; ListView_SetColumnWidth(hWnd, ELVSIEPISODE, LVSCW_AUTOSIZE); int cxColumn = ListView_GetColumnWidth(hWnd, ELVSIEPISODE)+4; ListView_SetColumnWidth(hWnd, ELVSIEPISODE, cxColumn); cxColumn += ListView_GetColumnWidth(hWnd, ELVSIRATING); - ListView_SetColumnWidth(hWnd, ELVSITITLE, rcParent.right-cxColumn-g_cxVScroll-4); + ListView_SetColumnWidth(hWnd, ELVSITITLE, rcParent.right-cxColumn-Metric-4); } /* Select previously focused episode. */ diff --git a/c/main.cpp b/c/main.cpp index 5a33405..c04890d 100644 --- a/c/main.cpp +++ b/c/main.cpp @@ -18,7 +18,6 @@ int g_bThread; /* Looked-up constants. */ int g_bThemes; -int g_cxVScroll; int g_iDPI = 96; /* Fonts. */ @@ -166,8 +165,6 @@ static LRESULT CALLBACK CBTProc(const int nCode, const WPARAM wParam, const LPAR g_hWnd = (HWND)wParam; /* Look up constants. */ - g_cxVScroll = GetSystemMetrics(SM_CXVSCROLL); - if (auto opLib = maybe_make(TEXT("User32.dll"))) if (auto GetDpiForWindow = opLib->GetProcAddress("GetDpiForWindow")) g_iDPI = GetDpiForWindow(g_hWnd); -- cgit v1.2.3