changeset 178:bc8d2ccff09c

win32/dark: use existing STL classes for dwmapi
author Paper <mrpapersonic@gmail.com>
date Mon, 04 Dec 2023 11:51:30 -0500
parents 122fad646f81
children 9c4645100fec
files .clang-format include/core/strings.h src/core/anime.cc src/gui/pages/anime_list.cc src/gui/pages/now_playing.cc src/gui/pages/statistics.cc src/gui/pages/torrents.cc src/sys/win32/dark_theme.cc
diffstat 8 files changed, 49 insertions(+), 65 deletions(-) [+]
line wrap: on
line diff
--- a/.clang-format	Fri Dec 01 13:32:29 2023 -0500
+++ b/.clang-format	Mon Dec 04 11:51:30 2023 -0500
@@ -29,4 +29,4 @@
 
 ---
 Language: Cpp
-Standard: Cpp11
+Standard: Cpp17
--- a/include/core/strings.h	Fri Dec 01 13:32:29 2023 -0500
+++ b/include/core/strings.h	Mon Dec 04 11:51:30 2023 -0500
@@ -53,7 +53,7 @@
       } else if constexpr (std::is_unsigned<T>::value) {
          return clamp(std::stoull(str), std::numeric_limits<T>::max(), std::numeric_limits<T>::max());
       } else {
-         throw std::invalid_argument("it no worky");
+         throw std::invalid_argument("Invalid input to Strings::ToInt()!");
       }
    } catch (std::invalid_argument const& ex) {
       return def;
--- a/src/core/anime.cc	Fri Dec 01 13:32:29 2023 -0500
+++ b/src/core/anime.cc	Mon Dec 04 11:51:30 2023 -0500
@@ -5,9 +5,8 @@
 #include "core/anime.h"
 #include "core/date.h"
 #include "core/session.h"
+
 #include <algorithm>
-#include <chrono>
-#include <cmath>
 #include <string>
 #include <vector>
 
--- a/src/gui/pages/anime_list.cc	Fri Dec 01 13:32:29 2023 -0500
+++ b/src/gui/pages/anime_list.cc	Mon Dec 04 11:51:30 2023 -0500
@@ -17,6 +17,7 @@
 #include "gui/dialog/information.h"
 #include "gui/translate/anime.h"
 #include "services/services.h"
+
 #include <QDebug>
 #include <QHBoxLayout>
 #include <QHeaderView>
@@ -27,6 +28,7 @@
 #include <QStylePainter>
 #include <QStyledItemDelegate>
 #include <QThreadPool>
+
 #include <set>
 
 AnimeListPageSortFilter::AnimeListPageSortFilter(QObject* parent) : QSortFilterProxyModel(parent) {
@@ -342,8 +344,8 @@
 
 void AnimeListPage::RefreshTabs() {
 	for (unsigned int i = 0; i < sort_models.size(); i++)
-		tab_bar->setTabText(i, Strings::ToQString(Translate::ToString(Anime::ListStatuses[i])) + " (" +
-								   QString::number(Anime::db.GetListsAnimeAmount(Anime::ListStatuses[i])) + ")");
+		tab_bar->setTabText(i, Strings::ToQString(Translate::ToString(Anime::ListStatuses[i]) + " (" +
+								   std::to_string(Anime::db.GetListsAnimeAmount(Anime::ListStatuses[i])) + ")"));
 }
 
 void AnimeListPage::Refresh() {
--- a/src/gui/pages/now_playing.cc	Fri Dec 01 13:32:29 2023 -0500
+++ b/src/gui/pages/now_playing.cc	Mon Dec 04 11:51:30 2023 -0500
@@ -4,6 +4,7 @@
 #include "gui/widgets/anime_info.h"
 #include "gui/widgets/text.h"
 #include "gui/widgets/poster.h"
+
 #include <QLabel>
 #include <QStackedWidget>
 #include <QHBoxLayout>
--- a/src/gui/pages/statistics.cc	Fri Dec 01 13:32:29 2023 -0500
+++ b/src/gui/pages/statistics.cc	Mon Dec 04 11:51:30 2023 -0500
@@ -4,12 +4,14 @@
 #include "gui/pages/anime_list.h"
 #include "gui/widgets/text.h"
 #include "gui/widgets/graph.h"
+
 #include <QString>
 #include <QTextDocument>
 #include <QTextStream>
 #include <QTimer>
 #include <QVBoxLayout>
 #include <QWidget>
+
 #include <sstream>
 
 StatisticsPage::StatisticsPage(QWidget* parent) : QFrame(parent) {
--- a/src/gui/pages/torrents.cc	Fri Dec 01 13:32:29 2023 -0500
+++ b/src/gui/pages/torrents.cc	Mon Dec 04 11:51:30 2023 -0500
@@ -4,7 +4,7 @@
 #include "core/session.h"
 #include "gui/widgets/text.h"
 #include "track/media.h"
-#include "pugixml.hpp"
+
 #include <QVBoxLayout>
 #include <QToolBar>
 #include <QTreeView>
@@ -13,10 +13,12 @@
 #include <QDataStream>
 #include <QThreadPool>
 #include <QDebug>
+
 #include <iostream>
 #include <sstream>
 #include <algorithm>
 
+#include "pugixml.hpp"
 #include "anitomy/anitomy.h"
 
 /* This file is very, very similar to the anime list page.
--- a/src/sys/win32/dark_theme.cc	Fri Dec 01 13:32:29 2023 -0500
+++ b/src/sys/win32/dark_theme.cc	Mon Dec 04 11:51:30 2023 -0500
@@ -1,84 +1,62 @@
 #include "sys/win32/dark_theme.h"
+
 #include <QApplication>
-#include <QDebug>
 #include <QOperatingSystemVersion>
 #include <QSettings>
 #include <QWidget>
+
 #include <iostream>
+#include <memory>
+
 #include <dwmapi.h>
 
-/* let's make a class wrapper around HINSTANCE, 
-   so we don't fuck anything up :). */
-class Library {
-	public:
-		Library() {}
-		~Library() {
-			Unload();
-		}
-		void Unload() {
-			if (hInstance) {
-				FreeLibrary(hInstance);
-				hInstance = nullptr;
-			}
-			loaded = false;
-		}
-		void Load(LPCWSTR name) {
-			if (loaded)
-				Unload();
-			hInstance = LoadLibraryW(name);
-			if (hInstance)
-				loaded = true;
-		}
-		HINSTANCE GetInstance() {
-			return hInstance;
-		}
-		bool IsLoaded() {
-			return loaded;
-		}
-	private:
-		HINSTANCE hInstance = nullptr;
-		bool loaded = false;
+struct LibraryDeconstructor {
+		using pointer = HINSTANCE;
+		void operator()(pointer t) const { ::FreeLibrary(t); };
 };
 
-Library dwmapi;
+using Library = std::unique_ptr<HINSTANCE, LibraryDeconstructor>;
+
+class Dwmapi {
+	public:
+		Dwmapi() { library.reset( ::LoadLibraryW(L"dwmapi.dll")); }
+
+		HRESULT SetWindowAttribute(HWND hWnd, DWORD key, LPCVOID data, DWORD sz_data) {
+			if (!library.get())
+				return E_POINTER;
+
+			/* GCC throws a fit here because C/C++ lacks a "generic" function pointer type.
+			   Ignore. */
+			auto set_wind_attrib = reinterpret_cast<decltype(::DwmSetWindowAttribute)*>(GetProcAddress(library.get(), "DwmSetWindowAttribute"));
+			if (!set_wind_attrib)
+				return E_POINTER;
+
+			return set_wind_attrib(hWnd, key, data, sz_data);
+		}
+
+	protected:
+		Library library = nullptr;
+};
+
+Dwmapi dwmapi;
 
 namespace win32 {
 
-#define GET_FUNCTION(f, i) \
-	reinterpret_cast<decltype(::f)*>(GetProcAddress(i, #f))
-
-static HRESULT SetWindowAttribute(HWND hWnd, DWORD key, LPCVOID data, DWORD sz_data) {
-	if (!dwmapi.IsLoaded()) {
-		dwmapi.Load(L"dwmapi.dll");
-		if (!dwmapi.IsLoaded())
-			return false;
-	}
-
-	HINSTANCE hInstance = dwmapi.GetInstance();
-	if (!hInstance)
-		return false;
-
-	auto set_wind_attrib = GET_FUNCTION(DwmSetWindowAttribute, hInstance);
-	if (!set_wind_attrib)
-		return false;
-
-	return set_wind_attrib(hWnd, key, data, sz_data);
-}
-
+/* NOTE: explicit conversion from builtin `bool` and win32 `BOOL` IS allowed. */
 bool SetTitleBarToBlack(QWidget* win, bool enabled) {
 	BOOL b = enabled;
 
 	/* MAGIC NUMBERS: 19 and 20 are both DWMWA_USE_IMMERSIVE_DARK_MODE.
-	   clarification: it's 20 on newer versions of windows (i.e. win11 and late win10),
-	   but it's 19 on very old versions of win10 nobody ought to be using. */
+	   For clarification: it's 20 on newer versions of windows (i.e. win11 and late win10),
+	   but it's 19 on very old versions of win10 nobody ought to be using anymore. */
 	{
-		HRESULT result = SetWindowAttribute(reinterpret_cast<HWND>(win->winId()), 20, &b, sizeof(b));
+		HRESULT result = dwmapi.SetWindowAttribute(reinterpret_cast<HWND>(win->winId()), 20, &b, sizeof(b));
 		if (result == S_OK)
 			return b;
 	}
 
 	{
-		HRESULT result = SetWindowAttribute(reinterpret_cast<HWND>(win->winId()), 19, &b, sizeof(b));
+		HRESULT result = dwmapi.SetWindowAttribute(reinterpret_cast<HWND>(win->winId()), 19, &b, sizeof(b));
 		if (result == S_OK)
 			return b;
 	}