[Toast] Support for native toast notifications

This commit is contained in:
momo5502 2017-02-18 17:25:54 +01:00
parent 2f3e665c89
commit 1b7aea6780
8 changed files with 92 additions and 5 deletions

3
.gitmodules vendored
View File

@ -35,3 +35,6 @@
path = deps/protobuf path = deps/protobuf
url = https://github.com/google/protobuf.git url = https://github.com/google/protobuf.git
branch = 3.2.x branch = 3.2.x
[submodule "deps/WinToast"]
path = deps/WinToast
url = https://github.com/mohabouje/WinToast.git

1
deps/WinToast vendored Submodule

@ -0,0 +1 @@
Subproject commit a9771a2b43d93e18227fae5ecee92d6b9eb6f2bb

45
premake/WinToast.lua Normal file
View File

@ -0,0 +1,45 @@
WinToast = {
settings = nil,
}
function WinToast.setup(settings)
if not settings.source then error("Missing source.") end
WinToast.settings = settings
end
function WinToast.import()
if not WinToast.settings then error("Run WinToast.setup first") end
links { "WinToast" }
WinToast.includes()
end
function WinToast.includes()
if not WinToast.settings then error("Run WinToast.setup first") end
includedirs { path.join(WinToast.settings.source, "src"), }
end
function WinToast.project()
if not WinToast.settings then error("Run WinToast.setup first") end
project "WinToast"
language "C++"
includedirs
{
WinToast.settings.source,
}
files
{
path.join(WinToast.settings.source, "src/*.cpp"),
path.join(WinToast.settings.source, "src/*.h"),
}
-- not our code, ignore POSIX usage warnings for now
warnings "Off"
kind "StaticLib"
end

View File

@ -199,7 +199,7 @@ require "premake/pdcurses"
require "premake/protobuf" require "premake/protobuf"
require "premake/sqlite3" require "premake/sqlite3"
require "premake/zlib" require "premake/zlib"
require "premake/boost" require "premake/WinToast"
base128.setup base128.setup
{ {
@ -256,9 +256,9 @@ zlib.setup
}, },
source = path.join(depsBasePath, "zlib"), source = path.join(depsBasePath, "zlib"),
} }
boost.setup WinToast.setup
{ {
source = path.join(depsBasePath, "boost"), source = path.join(depsBasePath, "WinToast"),
} }
workspace "iw4x" workspace "iw4x"
@ -366,7 +366,7 @@ workspace "iw4x"
pdcurses.import() pdcurses.import()
protobuf.import() protobuf.import()
zlib.import() zlib.import()
boost.import() WinToast.import()
-- fix vpaths for protobuf sources -- fix vpaths for protobuf sources
vpaths vpaths
@ -485,7 +485,7 @@ workspace "iw4x"
pdcurses.project() pdcurses.project()
protobuf.project() protobuf.project()
zlib.project() zlib.project()
boost.project() WinToast.project()
rule "ProtobufCompiler" rule "ProtobufCompiler"
display "Protobuf compiler" display "Protobuf compiler"

View File

@ -18,6 +18,8 @@ namespace Components
Loader::Postgame = false; Loader::Postgame = false;
Loader::MemAllocator.clear(); Loader::MemAllocator.clear();
CoInitialize(nullptr);
Loader::Register(new Flags()); Loader::Register(new Flags());
Loader::Register(new Singleton()); Loader::Register(new Singleton());
Loader::Register(new Exception()); // install our exception handler as early as posssible to get better debug dumps from startup crashes Loader::Register(new Exception()); // install our exception handler as early as posssible to get better debug dumps from startup crashes
@ -107,6 +109,8 @@ namespace Components
Loader::Components.clear(); Loader::Components.clear();
Loader::MemAllocator.clear(); Loader::MemAllocator.clear();
CoUninitialize();
} }
void Loader::PreDestroy() void Loader::PreDestroy()

View File

@ -5,6 +5,8 @@ namespace Components
std::queue<Toast::UIToast> Toast::Queue; std::queue<Toast::UIToast> Toast::Queue;
std::mutex Toast::Mutex; std::mutex Toast::Mutex;
WinToastLib::WinToastHandler* Toast::ToastHandler = nullptr;
void Toast::Show(std::string image, std::string title, std::string description, int length) void Toast::Show(std::string image, std::string title, std::string description, int length)
{ {
Toast::Mutex.lock(); Toast::Mutex.lock();
@ -12,6 +14,12 @@ namespace Components
Toast::Mutex.unlock(); Toast::Mutex.unlock();
} }
bool Toast::ShowNative(const WinToastLib::WinToastTemplate& toast)
{
if (!Toast::ToastHandler) return false;
return WinToastLib::WinToast::instance()->showToast(toast, Toast::ToastHandler);
}
void Toast::Draw(UIToast* toast) void Toast::Draw(UIToast* toast)
{ {
if (!toast) return; if (!toast) return;
@ -125,6 +133,12 @@ namespace Components
Toast::Toast() Toast::Toast()
{ {
Toast::ToastHandler = new WinToastLib::WinToastHandler;
WinToastLib::WinToast::instance()->setAppName(L"IW4x");
WinToastLib::WinToast::instance()->setAppUserModelId(WinToastLib::WinToast::configureAUMI(L"IW4x", L"IW4x", L"IW4x", L"0"));
WinToastLib::WinToast::instance()->initialize();
QuickPatch::OnReady([]() QuickPatch::OnReady([]()
{ {
Renderer::OnFrame(Toast::Handler); Renderer::OnFrame(Toast::Handler);
@ -140,4 +154,16 @@ namespace Components
{ {
Toast::Queue = std::queue<Toast::UIToast>(); Toast::Queue = std::queue<Toast::UIToast>();
} }
void Toast::preDestroy()
{
// Destroying that on the main thread deadlocks, for whatever reason.
// I did not write the library, so whatever.
std::thread([]()
{
delete WinToastLib::WinToast::instance();
delete Toast::ToastHandler;
Toast::ToastHandler = nullptr;
}).join();
}
} }

View File

@ -7,12 +7,16 @@ namespace Components
public: public:
Toast(); Toast();
~Toast(); ~Toast();
void preDestroy() override;
typedef WinToastLib::WinToastTemplate Template;
#if defined(DEBUG) || defined(FORCE_UNIT_TESTS) #if defined(DEBUG) || defined(FORCE_UNIT_TESTS)
const char* getName() override { return "Toast"; }; const char* getName() override { return "Toast"; };
#endif #endif
static void Show(std::string image, std::string title, std::string description, int length); static void Show(std::string image, std::string title, std::string description, int length);
static bool ShowNative(const WinToastLib::WinToastTemplate& toast);
private: private:
class UIToast class UIToast
@ -30,5 +34,7 @@ namespace Components
static std::queue<UIToast> Queue; static std::queue<UIToast> Queue;
static std::mutex Mutex; static std::mutex Mutex;
static WinToastLib::WinToastHandler* ToastHandler;
}; };
} }

View File

@ -47,6 +47,7 @@ template <size_t S> class Sizer { };
// Ignore the warnings, it's no our code! // Ignore the warnings, it's no our code!
#pragma warning(push) #pragma warning(push)
#pragma warning(disable: 4005) #pragma warning(disable: 4005)
#pragma warning(disable: 4091)
#pragma warning(disable: 4100) #pragma warning(disable: 4100)
#pragma warning(disable: 4389) #pragma warning(disable: 4389)
#pragma warning(disable: 4702) #pragma warning(disable: 4702)
@ -67,6 +68,7 @@ template <size_t S> class Sizer { };
#ifndef DISABLE_BITMESSAGE #ifndef DISABLE_BITMESSAGE
#include <BitMRC.h> #include <BitMRC.h>
#endif #endif
#include <wintoastlib.h>
#ifdef max #ifdef max
#undef max #undef max