Skip to content

Commit 9654150

Browse files
authored
Merge pull request #30 from NickvisionApps/win-geo
`WindowGeometry` Model
2 parents 44dc7ac + 8b3242d commit 9654150

File tree

5 files changed

+234
-7
lines changed

5 files changed

+234
-7
lines changed

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ add_library (${PROJECT_NAME}
3535
"src/app/aura.cpp"
3636
"src/app/configurationbase.cpp"
3737
"src/app/interprocesscommunicator.cpp"
38+
"src/app/windowgeometry.cpp"
3839
"src/database/sqlcontext.cpp"
3940
"src/database/sqldatabase.cpp"
4041
"src/database/sqlite3.c"

docs/app.md

Lines changed: 51 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,12 @@ This module contains types and functions for creating Nickvision applications.
77
- [Aura](#aura)
88
- [ConfigurationBase](#configurationbase)
99
- [InterProcessCommunicator](#interprocesscommunicator)
10+
- [WindowGeometry](#windowgeometry)
1011

1112
## AppInfo
1213
Description: A model for the information about an application.
1314

14-
Interface: [appinfo.h](/include/aura/appinfo.h)
15+
Interface: [appinfo.h](/include/app/appinfo.h)
1516

1617
Type: `class`
1718

@@ -127,7 +128,7 @@ Path: `Nickvision::App::AppInfo`
127128
## Aura
128129
Description: An application base.
129130
130-
Interface: [aura.h](/include/aura/aura.h)
131+
Interface: [aura.h](/include/app/aura.h)
131132
132133
Type: `class`
133134
@@ -233,7 +234,7 @@ Path: `Nickvision::App::Aura`
233234
## ConfigurationBase
234235
Description: A base class for configuration files.
235236

236-
Interface: [configurationbase.h](/include/aura/configurationbase.h)
237+
Interface: [configurationbase.h](/include/app/configurationbase.h)
237238

238239
Type: `class`
239240

@@ -337,7 +338,7 @@ Type: `file`
337338
## InterProcessCommunicator
338339
Description: An inter process communicator (server/client).
339340

340-
Interface: [interprocesscommunicator.h](/include/aura/interprocesscommunicator.h)
341+
Interface: [interprocesscommunicator.h](/include/app/interprocesscommunicator.h)
341342

342343
Type: `class`
343344

@@ -414,3 +415,49 @@ If this program is ran for the first time, ipc will be the server instance. `han
414415

415416
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.
416417

418+
## WindowGeometry
419+
Description: A model of a window's geometry.
420+
421+
Interface: [windowgeometry.h](/include/app/windowgeometry.h)
422+
423+
Type: `class`
424+
425+
Path: `Nickvision::App::WindowGeometry`
426+
427+
### Member Variables
428+
- ```
429+
long Width: get, set
430+
```
431+
- The window's width.
432+
- ```
433+
long Height: get, set
434+
```
435+
- The window's height.
436+
- ```
437+
bool IsMaximized: get, set
438+
```
439+
- Whether or not the window is maximized.
440+
441+
### Methods
442+
- ```cpp
443+
WindowGeometry()
444+
```
445+
- Constructs a WindowGeometry.
446+
- Note: Uses default values: 800 width, 600 height, not maximized.
447+
- ```cpp
448+
WindowGeometry(long width, long height, bool isMaximized)
449+
```
450+
- Constructs a WindowGeometry.
451+
- Accepts: The window's width, width, the window's height, height, and whether or not the window is maximized, isMaximized.
452+
- ```cpp
453+
WindowGeometry(HWND hwnd)
454+
```
455+
- Constructs a WindowGeometry.
456+
- Accepts: The window handle to get the geometry from, hwnd.
457+
- Note: This function is only available on Windows.
458+
- ```cpp
459+
bool apply(HWND hwnd)
460+
```
461+
- Accepts: The window handle to apply the geometry to, hwnd.
462+
- Returns: True if successful.
463+
- Returns: False if unsuccessful.

include/app/windowgeometry.h

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
#ifndef WINDOWGEOMETRY_H
2+
#define WINDOWGEOMETRY_H
3+
4+
#ifdef _WIN32
5+
#include <windows.h>
6+
#endif
7+
8+
namespace Nickvision::App
9+
{
10+
/**
11+
* @brief A model of a window's geometry.
12+
*/
13+
class WindowGeometry
14+
{
15+
public:
16+
/**
17+
* @brief Construct a WindowGeometry.
18+
*/
19+
WindowGeometry();
20+
/**
21+
* @brief Construct a WindowGeometry.
22+
* @param width The width of the window
23+
* @param height The height of the window
24+
* @param isMaximized Whether or not the window is maximized
25+
*/
26+
WindowGeometry(long width, long height, bool isMaximized);
27+
#ifdef _WIN32
28+
/**
29+
* @brief Construct a WindowGeometry.
30+
* @param hwnd The window handle to get the geometry from
31+
*/
32+
WindowGeometry(HWND hwnd);
33+
#endif
34+
/**
35+
* @brief Gets the width of the window.
36+
* @return The width of the window
37+
*/
38+
long getWidth() const;
39+
/**
40+
* @brief Sets the width of the window.
41+
* @param width The new width of the window
42+
*/
43+
void setWidth(long width);
44+
/**
45+
* @brief Gets the height of the window.
46+
* @return The height of the window
47+
*/
48+
long getHeight() const;
49+
/**
50+
* @brief Sets the height of the window.
51+
* @param height The new height of the window
52+
*/
53+
void setHeight(long height);
54+
/**
55+
* @brief Gets whether or not the window is maximized.
56+
* @return True if maximized, else false
57+
*/
58+
bool isMaximized() const;
59+
/**
60+
* @brief Sets whether or not the window is maximized.
61+
* @param isMaximized True if maximized, else false
62+
*/
63+
void setIsMaximized(bool isMaximized);
64+
#ifdef _WIN32
65+
/**
66+
* @brief Applies the geometry to a window.
67+
* @param hwnd The window handle to apply the geometry to
68+
* @return True if successful, else false
69+
*/
70+
bool apply(HWND hwnd) const;
71+
#endif
72+
73+
private:
74+
long m_width;
75+
long m_height;
76+
bool m_isMaximized;
77+
};
78+
}
79+
80+
#endif //WINDOWGEOMETRY_H

src/app/windowgeometry.cpp

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
#include "app/windowgeometry.h"
2+
3+
namespace Nickvision::App
4+
{
5+
WindowGeometry::WindowGeometry()
6+
: m_width{ 800 },
7+
m_height{ 600 },
8+
m_isMaximized{ false }
9+
{
10+
11+
}
12+
13+
WindowGeometry::WindowGeometry(long width, long height, bool isMaximized)
14+
: m_width{ width },
15+
m_height{ height },
16+
m_isMaximized{ isMaximized }
17+
{
18+
19+
}
20+
21+
#ifdef _WIN32
22+
WindowGeometry::WindowGeometry(HWND hwnd)
23+
{
24+
WINDOWPLACEMENT placement;
25+
GetWindowPlacement(hwnd, &placement);
26+
m_width = placement.rcNormalPosition.right - placement.rcNormalPosition.left;
27+
m_height = placement.rcNormalPosition.bottom - placement.rcNormalPosition.top;
28+
m_isMaximized = placement.showCmd == SW_SHOWMAXIMIZED;
29+
}
30+
#endif
31+
32+
long WindowGeometry::getWidth() const
33+
{
34+
return m_width;
35+
}
36+
37+
void WindowGeometry::setWidth(long width)
38+
{
39+
m_width = width;
40+
}
41+
42+
long WindowGeometry::getHeight() const
43+
{
44+
return m_height;
45+
}
46+
47+
void WindowGeometry::setHeight(long height)
48+
{
49+
m_height = height;
50+
}
51+
52+
bool WindowGeometry::isMaximized() const
53+
{
54+
return m_isMaximized;
55+
}
56+
57+
void WindowGeometry::setIsMaximized(bool isMaximized)
58+
{
59+
m_isMaximized = isMaximized;
60+
}
61+
62+
#ifdef _WIN32
63+
bool WindowGeometry::apply(HWND hwnd) const
64+
{
65+
WINDOWPLACEMENT placement;
66+
GetWindowPlacement(hwnd, &placement);
67+
placement.rcNormalPosition.right = placement.rcNormalPosition.left + m_width;
68+
placement.rcNormalPosition.bottom = placement.rcNormalPosition.top + m_height;
69+
placement.showCmd = m_isMaximized ? SW_SHOWMAXIMIZED : SW_SHOWNORMAL;
70+
return SetWindowPlacement(hwnd, &placement);
71+
}
72+
#endif
73+
}

tests/auratests.cpp

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include <gtest/gtest.h>
22
#include "app/aura.h"
3+
#include "app/windowgeometry.h"
34
#include "filesystem/userdirectories.h"
45
#include "notifications/shellnotification.h"
56

@@ -25,12 +26,29 @@ class AppConfig : public ConfigurationBase
2526

2627
Theme getTheme() const
2728
{
28-
return (Theme)m_json.get("Theme", (int)Theme::System).asInt();
29+
return static_cast<Theme>(m_json.get("Theme", (int)Theme::System).asInt());
2930
}
3031

3132
void setTheme(Theme theme)
3233
{
33-
m_json["Theme"] = (int)theme;
34+
m_json["Theme"] = static_cast<int>(theme);
35+
}
36+
37+
WindowGeometry getWindowGeometry()
38+
{
39+
WindowGeometry geometry;
40+
const Json::Value json{ m_json["WindowGeometry"] };
41+
geometry.setWidth(json.get("Width", 800).asInt64());
42+
geometry.setHeight(json.get("Height", 600).asInt64());
43+
geometry.setIsMaximized(json.get("IsMaximized", false).asBool());
44+
return geometry;
45+
}
46+
47+
void setWindowGeometry(const WindowGeometry& geometry)
48+
{
49+
m_json["WindowGeometry"]["Width"] = static_cast<Json::Int64>(geometry.getWidth());
50+
m_json["WindowGeometry"]["Height"] = static_cast<Json::Int64>(geometry.getHeight());
51+
m_json["WindowGeometry"]["IsMaximized"] = geometry.isMaximized();
3452
}
3553
};
3654

@@ -57,21 +75,29 @@ TEST_F(AuraTest, SetAppInfo)
5775
TEST_F(AuraTest, EnsureDefaultAppConfig)
5876
{
5977
AppConfig& config{ Aura::getActive().getConfig<AppConfig>("config") };
78+
WindowGeometry geometry{ config.getWindowGeometry() };
6079
ASSERT_EQ(config.getTheme(), Theme::System);
80+
ASSERT_EQ(geometry.getWidth(), 800);
81+
ASSERT_EQ(geometry.getHeight(), 600);
82+
ASSERT_EQ(geometry.isMaximized(), false);
6183
}
6284

6385
TEST_F(AuraTest, ChangeAppConfig)
6486
{
6587
AppConfig& config{ Aura::getActive().getConfig<AppConfig>("config") };
6688
config.setTheme(Theme::Light);
89+
config.setWindowGeometry(WindowGeometry{ 1920, 1080, true });
6790
ASSERT_TRUE(config.save());
68-
ASSERT_EQ(config.getTheme(), Theme::Light);
6991
}
7092

7193
TEST_F(AuraTest, EnsureChangeInAppConfig)
7294
{
7395
AppConfig& config{ Aura::getActive().getConfig<AppConfig>("config") };
7496
ASSERT_EQ(config.getTheme(), Theme::Light);
97+
WindowGeometry geometry{ config.getWindowGeometry() };
98+
ASSERT_EQ(geometry.getWidth(), 1920);
99+
ASSERT_EQ(geometry.getHeight(), 1080);
100+
ASSERT_EQ(geometry.isMaximized(), true);
75101
}
76102

77103
TEST_F(AuraTest, ResetAppConfig)

0 commit comments

Comments
 (0)