From da4dd4e4473dfac00581de09380276b394b33d6f Mon Sep 17 00:00:00 2001 From: Nick Logozzo Date: Mon, 3 Jun 2024 21:04:12 -0400 Subject: [PATCH 1/3] App - Implement WindowGeometry Model --- CMakeLists.txt | 1 + docs/app.md | 55 +++++++++++++++++++++++-- include/app/windowgeometry.h | 80 ++++++++++++++++++++++++++++++++++++ src/app/windowgeometry.cpp | 73 ++++++++++++++++++++++++++++++++ 4 files changed, 205 insertions(+), 4 deletions(-) create mode 100644 include/app/windowgeometry.h create mode 100644 src/app/windowgeometry.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 3eea780c..16bacca3 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -35,6 +35,7 @@ add_library (${PROJECT_NAME} "src/app/aura.cpp" "src/app/configurationbase.cpp" "src/app/interprocesscommunicator.cpp" + "src/app/windowgeometry.cpp" "src/database/sqlcontext.cpp" "src/database/sqldatabase.cpp" "src/database/sqlite3.c" diff --git a/docs/app.md b/docs/app.md index 9b409b92..cd55421d 100644 --- a/docs/app.md +++ b/docs/app.md @@ -7,11 +7,12 @@ This module contains types and functions for creating Nickvision applications. - [Aura](#aura) - [ConfigurationBase](#configurationbase) - [InterProcessCommunicator](#interprocesscommunicator) +- [WindowGeometry](#windowgeometry) ## AppInfo Description: A model for the information about an application. -Interface: [appinfo.h](/include/aura/appinfo.h) +Interface: [appinfo.h](/include/app/appinfo.h) Type: `class` @@ -127,7 +128,7 @@ Path: `Nickvision::App::AppInfo` ## Aura Description: An application base. -Interface: [aura.h](/include/aura/aura.h) +Interface: [aura.h](/include/app/aura.h) Type: `class` @@ -233,7 +234,7 @@ Path: `Nickvision::App::Aura` ## ConfigurationBase Description: A base class for configuration files. -Interface: [configurationbase.h](/include/aura/configurationbase.h) +Interface: [configurationbase.h](/include/app/configurationbase.h) Type: `class` @@ -337,7 +338,7 @@ Type: `file` ## InterProcessCommunicator Description: An inter process communicator (server/client). -Interface: [interprocesscommunicator.h](/include/aura/interprocesscommunicator.h) +Interface: [interprocesscommunicator.h](/include/app/interprocesscommunicator.h) Type: `class` @@ -414,3 +415,49 @@ If this program is ran for the first time, ipc will be the server instance. `han If this program is ran not for the first time, its arguments will be sent to the first instance and this instance itself will close. The first instance's `handleArguments` function will be called as a result of `CommandReceived` being invoked by the ipc server receiving the command. +## WindowGeometry +Description: A model of a window's geometry. + +Interface: [windowgeometry.h](/include/app/windowgeometry.h) + +Type: `class` + +Path: `Nickvision::App::WindowGeometry` + +### Member Variables +- ``` + long Width: get, set + ``` + - The window's width. +- ``` + long Height: get, set + ``` + - The window's height. +- ``` + bool IsMaximized: get, set + ``` + - Whether or not the window is maximized. + +### Methods +- ```cpp + WindowGeometry() + ``` + - Constructs a WindowGeometry. + - Note: Uses default values: 800 width, 600 height, not maximized. +- ```cpp + WindowGeometry(long width, long height, bool isMaximized) + ``` + - Constructs a WindowGeometry. + - Accepts: The window's width, width, the window's height, height, and whether or not the window is maximized, isMaximized. +- ```cpp + WindowGeometry(HWND hwnd) + ``` + - Constructs a WindowGeometry. + - Accepts: The window handle to get the geometry from, hwnd. + - Note: This function is only available on Windows. +- ```cpp + bool apply(HWND hwnd) + ``` + - Accepts: The window handle to apply the geometry to, hwnd. + - Returns: True if successful. + - Returns: False if unsuccessful. \ No newline at end of file diff --git a/include/app/windowgeometry.h b/include/app/windowgeometry.h new file mode 100644 index 00000000..efaf59fc --- /dev/null +++ b/include/app/windowgeometry.h @@ -0,0 +1,80 @@ +#ifndef WINDOWGEOMETRY_H +#define WINDOWGEOMETRY_H + +#ifdef _WIN32 +#include +#endif + +namespace Nickvision::App +{ + /** + * @brief A model of a window's geometry. + */ + class WindowGeometry + { + public: + /** + * @brief Construct a WindowGeometry. + */ + WindowGeometry(); + /** + * @brief Construct a WindowGeometry. + * @param width The width of the window + * @param height The height of the window + * @param isMaximized Whether or not the window is maximized + */ + WindowGeometry(long width, long height, bool isMaximized); +#ifdef _WIN32 + /** + * @brief Construct a WindowGeometry. + * @param hwnd The window handle to get the geometry from + */ + WindowGeometry(HWND hwnd); +#endif + /** + * @brief Gets the width of the window. + * @return The width of the window + */ + long getWidth(); + /** + * @brief Sets the width of the window. + * @param width The new width of the window + */ + void setWidth(long width); + /** + * @brief Gets the height of the window. + * @return The height of the window + */ + long getHeight(); + /** + * @brief Sets the height of the window. + * @param height The new height of the window + */ + void setHeight(long height); + /** + * @brief Gets whether or not the window is maximized. + * @return True if maximized, else false + */ + bool isMaximized(); + /** + * @brief Sets whether or not the window is maximized. + * @param isMaximized True if maximized, else false + */ + void setIsMaximized(bool isMaximized); +#ifdef _WIN32 + /** + * @brief Applies the geometry to a window. + * @param hwnd The window handle to apply the geometry to + * @return True if successful, else false + */ + bool apply(HWND hwnd); +#endif + + private: + long m_width; + long m_height; + bool m_isMaximized; + }; +} + +#endif //WINDOWGEOMETRY_H \ No newline at end of file diff --git a/src/app/windowgeometry.cpp b/src/app/windowgeometry.cpp new file mode 100644 index 00000000..86a4f77d --- /dev/null +++ b/src/app/windowgeometry.cpp @@ -0,0 +1,73 @@ +#include "app/windowgeometry.h" + +namespace Nickvision::App +{ + WindowGeometry::WindowGeometry() + : m_width{ 800 }, + m_height{ 600 }, + m_isMaximized{ false } + { + + } + + WindowGeometry::WindowGeometry(long width, long height, bool isMaximized) + : m_width{ width }, + m_height{ height }, + m_isMaximized{ isMaximized } + { + + } + +#ifdef _WIN32 + WindowGeometry::WindowGeometry(HWND hwnd) + { + WINDOWPLACEMENT placement; + GetWindowPlacement(hwnd, &placement); + m_width = placement.rcNormalPosition.right - placement.rcNormalPosition.left; + m_height = placement.rcNormalPosition.bottom - placement.rcNormalPosition.top; + m_isMaximized = placement.showCmd == SW_SHOWMAXIMIZED; + } +#endif + + long WindowGeometry::getWidth() + { + return m_width; + } + + void WindowGeometry::setWidth(long width) + { + m_width = width; + } + + long WindowGeometry::getHeight() + { + return m_height; + } + + void WindowGeometry::setHeight(long height) + { + m_height = height; + } + + bool WindowGeometry::isMaximized() + { + return m_isMaximized; + } + + void WindowGeometry::setIsMaximized(bool isMaximized) + { + m_isMaximized = isMaximized; + } + +#ifdef _WIN32 + bool WindowGeometry::apply(HWND hwnd) + { + WINDOWPLACEMENT placement; + GetWindowPlacement(hwnd, &placement); + placement.rcNormalPosition.right = placement.rcNormalPosition.left + m_width; + placement.rcNormalPosition.bottom = placement.rcNormalPosition.top + m_height; + placement.showCmd = m_isMaximized ? SW_SHOWMAXIMIZED : SW_SHOWNORMAL; + return SetWindowPlacement(hwnd, &placement); + } +#endif +} \ No newline at end of file From 915c73cd34deda7d36ca834b8a7fae65da99f5ac Mon Sep 17 00:00:00 2001 From: Nick Logozzo Date: Mon, 3 Jun 2024 21:13:56 -0400 Subject: [PATCH 2/3] App - Fix WindowGeometry Const --- include/app/windowgeometry.h | 8 ++++---- src/app/windowgeometry.cpp | 8 ++++---- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/include/app/windowgeometry.h b/include/app/windowgeometry.h index efaf59fc..0d459f3f 100644 --- a/include/app/windowgeometry.h +++ b/include/app/windowgeometry.h @@ -35,7 +35,7 @@ namespace Nickvision::App * @brief Gets the width of the window. * @return The width of the window */ - long getWidth(); + long getWidth() const; /** * @brief Sets the width of the window. * @param width The new width of the window @@ -45,7 +45,7 @@ namespace Nickvision::App * @brief Gets the height of the window. * @return The height of the window */ - long getHeight(); + long getHeight() const; /** * @brief Sets the height of the window. * @param height The new height of the window @@ -55,7 +55,7 @@ namespace Nickvision::App * @brief Gets whether or not the window is maximized. * @return True if maximized, else false */ - bool isMaximized(); + bool isMaximized() const; /** * @brief Sets whether or not the window is maximized. * @param isMaximized True if maximized, else false @@ -67,7 +67,7 @@ namespace Nickvision::App * @param hwnd The window handle to apply the geometry to * @return True if successful, else false */ - bool apply(HWND hwnd); + bool apply(HWND hwnd) const; #endif private: diff --git a/src/app/windowgeometry.cpp b/src/app/windowgeometry.cpp index 86a4f77d..0ca015de 100644 --- a/src/app/windowgeometry.cpp +++ b/src/app/windowgeometry.cpp @@ -29,7 +29,7 @@ namespace Nickvision::App } #endif - long WindowGeometry::getWidth() + long WindowGeometry::getWidth() const { return m_width; } @@ -39,7 +39,7 @@ namespace Nickvision::App m_width = width; } - long WindowGeometry::getHeight() + long WindowGeometry::getHeight() const { return m_height; } @@ -49,7 +49,7 @@ namespace Nickvision::App m_height = height; } - bool WindowGeometry::isMaximized() + bool WindowGeometry::isMaximized() const { return m_isMaximized; } @@ -60,7 +60,7 @@ namespace Nickvision::App } #ifdef _WIN32 - bool WindowGeometry::apply(HWND hwnd) + bool WindowGeometry::apply(HWND hwnd) const { WINDOWPLACEMENT placement; GetWindowPlacement(hwnd, &placement); From 8b3242d19d83a7aaf9dd362f4a927841cdd9f59c Mon Sep 17 00:00:00 2001 From: Nick Logozzo Date: Mon, 3 Jun 2024 21:14:15 -0400 Subject: [PATCH 3/3] AuraTest - Add WindowGeometry to Config --- tests/auratests.cpp | 32 +++++++++++++++++++++++++++++--- 1 file changed, 29 insertions(+), 3 deletions(-) diff --git a/tests/auratests.cpp b/tests/auratests.cpp index 9cd683c6..d29bf739 100644 --- a/tests/auratests.cpp +++ b/tests/auratests.cpp @@ -1,5 +1,6 @@ #include #include "app/aura.h" +#include "app/windowgeometry.h" #include "filesystem/userdirectories.h" #include "notifications/shellnotification.h" @@ -25,12 +26,29 @@ class AppConfig : public ConfigurationBase Theme getTheme() const { - return (Theme)m_json.get("Theme", (int)Theme::System).asInt(); + return static_cast(m_json.get("Theme", (int)Theme::System).asInt()); } void setTheme(Theme theme) { - m_json["Theme"] = (int)theme; + m_json["Theme"] = static_cast(theme); + } + + WindowGeometry getWindowGeometry() + { + WindowGeometry geometry; + const Json::Value json{ m_json["WindowGeometry"] }; + geometry.setWidth(json.get("Width", 800).asInt64()); + geometry.setHeight(json.get("Height", 600).asInt64()); + geometry.setIsMaximized(json.get("IsMaximized", false).asBool()); + return geometry; + } + + void setWindowGeometry(const WindowGeometry& geometry) + { + m_json["WindowGeometry"]["Width"] = static_cast(geometry.getWidth()); + m_json["WindowGeometry"]["Height"] = static_cast(geometry.getHeight()); + m_json["WindowGeometry"]["IsMaximized"] = geometry.isMaximized(); } }; @@ -57,21 +75,29 @@ TEST_F(AuraTest, SetAppInfo) TEST_F(AuraTest, EnsureDefaultAppConfig) { AppConfig& config{ Aura::getActive().getConfig("config") }; + WindowGeometry geometry{ config.getWindowGeometry() }; ASSERT_EQ(config.getTheme(), Theme::System); + ASSERT_EQ(geometry.getWidth(), 800); + ASSERT_EQ(geometry.getHeight(), 600); + ASSERT_EQ(geometry.isMaximized(), false); } TEST_F(AuraTest, ChangeAppConfig) { AppConfig& config{ Aura::getActive().getConfig("config") }; config.setTheme(Theme::Light); + config.setWindowGeometry(WindowGeometry{ 1920, 1080, true }); ASSERT_TRUE(config.save()); - ASSERT_EQ(config.getTheme(), Theme::Light); } TEST_F(AuraTest, EnsureChangeInAppConfig) { AppConfig& config{ Aura::getActive().getConfig("config") }; ASSERT_EQ(config.getTheme(), Theme::Light); + WindowGeometry geometry{ config.getWindowGeometry() }; + ASSERT_EQ(geometry.getWidth(), 1920); + ASSERT_EQ(geometry.getHeight(), 1080); + ASSERT_EQ(geometry.isMaximized(), true); } TEST_F(AuraTest, ResetAppConfig)