Skip to content

Commit

Permalink
error handling; code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Ace-Radom committed Jun 28, 2024
1 parent 77a9a6c commit 005bedd
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 29 deletions.
20 changes: 15 additions & 5 deletions LenovoLegionToolkit.Lib.AoTOSD/Utils/DesktopInfos.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,30 +7,40 @@
namespace Utils = LenovoLegionToolkit::Lib::AoTOSD::Utils;

RECT Utils::DesktopInfos::GetPrimaryDesktopWorkingArea() {
RECT out = { 0 };
HMONITOR hMonitor = MonitorFromPoint({ 0 }, MONITOR_DEFAULTTOPRIMARY);
MONITORINFO monitorInfo = { 0 };
monitorInfo.cbSize = sizeof(MONITORINFO);
if (!GetMonitorInfo(hMonitor, &monitorInfo))
{
Log() << L"Failed to get primary monitor info.";
// need to return a default value
return { 0 };
out = GetSystemParameterWorkingArea();
return out;
}
UINT dpiX, dpiY;
if (!SUCCEEDED(GetDpiForMonitor(hMonitor, MDT_EFFECTIVE_DPI, &dpiX, &dpiY)))
{
Log() << L"Failed to get primary monitor dpi info.";
// same, need to return a default value
return { 0 };
out = GetSystemParameterWorkingArea();
return out;
}
RECT workingArea = monitorInfo.rcWork;
double multiplierX = 96.0 / dpiX;
double multiplierY = 96.0 / dpiY;

RECT out = { 0 };
out.left = workingArea.left;
out.right = RECTWIDTH(workingArea) * multiplierX - out.left;
out.top = workingArea.top;
out.bottom = RECTHEIGHT(workingArea) * multiplierY - out.top;
return out;
}

RECT Utils::DesktopInfos::GetSystemParameterWorkingArea() {
RECT out = { 0 };
if (!SystemParametersInfo(SPI_GETWORKAREA, 0, &out, 0))
{
Log() << L"Cannot get system-wide working area. [LastError=" << GetLastError() << L"]";
out = { 0 };
}
return out;
}
3 changes: 3 additions & 0 deletions LenovoLegionToolkit.Lib.AoTOSD/Utils/DesktopInfos.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ namespace LenovoLegionToolkit::Lib::AoTOSD::Utils {
public:
static RECT GetPrimaryDesktopWorkingArea();

private:
static RECT GetSystemParameterWorkingArea();

}; // class DesktopInfos

} // namespace LenovoLegionToolkit::Lib::AoTOSD::Utils
9 changes: 8 additions & 1 deletion LenovoLegionToolkit.Lib.AoTOSD/Window/BasicWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ Window::BasicWindow::BasicWindow(LPCWSTR className, LPCWSTR title, HINSTANCE hIn
int x, int y, int width, int height,
UINT classStyle, DWORD style, DWORD exStyle) :
_className(className),
_title(title)
_title(title),
_ok(true)
{
if (hInstance == NULL)
{
Expand All @@ -34,6 +35,7 @@ Window::BasicWindow::BasicWindow(LPCWSTR className, LPCWSTR title, HINSTANCE hIn
if (!RegisterClassEx(&wcex))
{
Log() << L"Failed to register AoT OSD window class. [LastError=" << GetLastError() << L"]";
this->_ok = false;
return;
}

Expand All @@ -50,6 +52,7 @@ Window::BasicWindow::BasicWindow(LPCWSTR className, LPCWSTR title, HINSTANCE hIn
if (this->_hWnd == NULL)
{
Log() << L"Failed to create AoT OSD window. [LastError=" << GetLastError() << L"]";
this->_ok = false;
return;
}

Expand All @@ -61,6 +64,10 @@ Window::BasicWindow::~BasicWindow() {
UnregisterClass(this->_className.c_str(), this->_hInstance);
}

bool Window::BasicWindow::IsOk() const noexcept {
return this->_ok;
}

std::wstring Window::BasicWindow::GetClassName() const noexcept {
return this->_className;
}
Expand Down
3 changes: 3 additions & 0 deletions LenovoLegionToolkit.Lib.AoTOSD/Window/BasicWindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ namespace LenovoLegionToolkit::Lib::AoTOSD::Window {
UINT classStyle = NULL, DWORD style = NULL, DWORD exStyle = NULL);
virtual ~BasicWindow();

bool IsOk() const noexcept;

protected:
std::wstring GetClassName() const noexcept;
std::wstring GetTitle() const noexcept;
Expand All @@ -27,6 +29,7 @@ namespace LenovoLegionToolkit::Lib::AoTOSD::Window {
std::wstring _title;
HINSTANCE _hInstance;
HWND _hWnd;
bool _ok;

}; // class BasicWindow

Expand Down
18 changes: 8 additions & 10 deletions LenovoLegionToolkit.Lib.AoTOSD/Window/LayeredWindow.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include"LayeredWindow.h"
#include"../Utils/GlobalLogger.h"

namespace Window = LenovoLegionToolkit::Lib::AoTOSD::Window;

Expand All @@ -11,7 +12,6 @@ Window::LayeredWindow::LayeredWindow(LPCWSTR className, LPCWSTR title, HINSTANCE
_bitmap(NULL)
{
this->SetWindowTopMost();
return;
}

Window::LayeredWindow::~LayeredWindow() {
Expand All @@ -26,7 +26,6 @@ void Window::LayeredWindow::Show() {
this->UpdateWindow();
ShowWindow(BasicWindow::GetHandle(), SW_SHOW);
this->_visible = true;
return;
}

void Window::LayeredWindow::Hide() {
Expand All @@ -36,7 +35,6 @@ void Window::LayeredWindow::Hide() {

ShowWindow(BasicWindow::GetHandle(), SW_HIDE);
this->_visible = false;
return;
}

Gdiplus::Bitmap* Window::LayeredWindow::GetBitmap() const noexcept {
Expand All @@ -46,9 +44,9 @@ Gdiplus::Bitmap* Window::LayeredWindow::GetBitmap() const noexcept {
void Window::LayeredWindow::SetBitmap(Gdiplus::Bitmap* bitmap) {
if (bitmap == NULL)
{
Log() << "Null bitmap given.";
return;
}

if (this->_bitmap)
{
delete this->_bitmap;
Expand All @@ -58,7 +56,6 @@ void Window::LayeredWindow::SetBitmap(Gdiplus::Bitmap* bitmap) {
this->_size.cx = bitmap->GetWidth();
this->_size.cy = bitmap->GetHeight();
this->UpdateWindow();
return;
}

byte Window::LayeredWindow::GetTransparency() const noexcept {
Expand All @@ -68,7 +65,6 @@ byte Window::LayeredWindow::GetTransparency() const noexcept {
void Window::LayeredWindow::SetTransparency(byte transparency) {
this->_transparency = transparency;
this->UpdateTransparency();
return;
}

POINT Window::LayeredWindow::GetPosition() const noexcept {
Expand All @@ -86,17 +82,14 @@ int Window::LayeredWindow::GetPositionY() const noexcept {
void Window::LayeredWindow::SetPosition(POINT pos) {
this->_pos = pos;
this->UpdateWindowPosition();
return;
}

void Window::LayeredWindow::SetPositionX(int x) {
this->_pos.x = x;
return;
}

void Window::LayeredWindow::SetPositionY(int y) {
this->_pos.y = y;
return;
}

int Window::LayeredWindow::GetSizeWidth() const noexcept {
Expand All @@ -108,6 +101,12 @@ int Window::LayeredWindow::GetSizeHeight() const noexcept {
}

void Window::LayeredWindow::UpdateWindow() {
if (this->_bitmap == NULL)
{
Log() << "Bitmap hasn't been set.";
return;
}

BLENDFUNCTION bFunc = { 0 };
bFunc.AlphaFormat = AC_SRC_ALPHA;
bFunc.BlendFlags = 0;
Expand Down Expand Up @@ -145,7 +144,6 @@ void Window::LayeredWindow::UpdateWindow() {
DeleteDC(sourceDc);
DeleteObject(hBmp);
ReleaseDC(GetDesktopWindow(), screenDc);
return;
}

void Window::LayeredWindow::UpdateTransparency() {
Expand Down
15 changes: 2 additions & 13 deletions LenovoLegionToolkit.Lib.AoTOSD/Window/OSDWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,7 @@ void Window::OSDWindow::Show() {
{
SetTimer(BasicWindow::GetHandle(), this->TIMER_HIDE_ID, this->_visibleDuration, NULL);
KillTimer(BasicWindow::GetHandle(), this->TIMER_OUT_ID);
if (this->_hideAnimation)
{
this->_hideAnimation->Reset(this);
}
this->_hideAnimation->Reset(this);
}
}

Expand All @@ -40,15 +37,7 @@ void Window::OSDWindow::Hide() {
return;
}

if (this->_hideAnimation)
{
SetTimer(BasicWindow::GetHandle(), this->TIMER_OUT_ID, this->_hideAnimation->GetUpdateInterval(), NULL);
}
else
{
ShowWindow(BasicWindow::GetHandle(), SW_HIDE);
this->_visible = false;
}
SetTimer(BasicWindow::GetHandle(), this->TIMER_OUT_ID, this->_hideAnimation->GetUpdateInterval(), NULL);
}

void Window::OSDWindow::SetVisibleDuration(int duration) {
Expand Down

0 comments on commit 005bedd

Please sign in to comment.