aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorYuutaW <17158086+Trumeet@users.noreply.github.com>2019-06-30 18:34:20 -0700
committerTrumeet <17158086+Trumeet@users.noreply.github.com>2019-06-30 18:34:20 -0700
commit41b0c65a084619330db2ead75cffe7fbe6db9ad8 (patch)
tree9b7578c31fe5d3cbed52b08aa9989e8747b4a0d7
parent9f5c437a12b90088e61bd88f91b4430dab036a7c (diff)
downloadAero-41b0c65a084619330db2ead75cffe7fbe6db9ad8.tar
Aero-41b0c65a084619330db2ead75cffe7fbe6db9ad8.tar.gz
Aero-41b0c65a084619330db2ead75cffe7fbe6db9ad8.tar.bz2
Aero-41b0c65a084619330db2ead75cffe7fbe6db9ad8.zip
refactor: trim the main file
Signed-off-by: Trumeet <17158086+Trumeet@users.noreply.github.com>
-rw-r--r--src/mingwMain/kotlin/moe/yuuta/aero/Aero.kt69
-rw-r--r--src/mingwMain/kotlin/moe/yuuta/aero/WinApi.kt56
-rw-r--r--src/mingwMain/kotlin/moe/yuuta/aero/WinCompat.kt19
3 files changed, 82 insertions, 62 deletions
diff --git a/src/mingwMain/kotlin/moe/yuuta/aero/Aero.kt b/src/mingwMain/kotlin/moe/yuuta/aero/Aero.kt
index f1040ff..44c184e 100644
--- a/src/mingwMain/kotlin/moe/yuuta/aero/Aero.kt
+++ b/src/mingwMain/kotlin/moe/yuuta/aero/Aero.kt
@@ -1,13 +1,10 @@
package moe.yuuta.aero
-import aero.*
import kotlinx.cinterop.*
import platform.windows.*
fun main() {
SetProcessDPIAware()
- val hInstance = GetModuleHandleA(null)
-
val className = "Main Window Class"
memScoped {
@@ -28,20 +25,11 @@ fun main() {
CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
null,
null,
- hInstance,
+ WinCompat.hInstance,
null
)
- val forceDialogEnv = memScoped {
- val buffer = nativeHeap.allocArray<CHARVar>(MAX_PATH)
- GetEnvironmentVariableA("AERO_DEMO_FORCE_DIALOG",
- buffer,
- MAX_PATH
- )
- val env = buffer.toKString()
- nativeHeap.free(buffer)
- return@memScoped env
- }
+ val forceDialogEnv = WinApi.getenv("AERO_DEMO_FORCE_DIALOG")
if (hwnd == null || (forceDialogEnv == "true")) {
if (hwnd == null) {
@@ -63,37 +51,15 @@ fun main() {
cx = 400
cy = 200
}
- return@memScoped DialogBoxIndirectParamW(hInstance,
+ return@memScoped DialogBoxIndirectParamW(WinCompat.hInstance,
dlgImpl.ptr,
null,
staticCFunction(::dlgProc),
0)
}
} else {
- val nCmdShow = memScoped {
- val startUpInfo = nativeHeap.alloc<_STARTUPINFOA>()
- GetStartupInfoA(startUpInfo.ptr)
- val nCmdShow = startUpInfo.wShowWindow
- nativeHeap.free(startUpInfo)
- return@memScoped nCmdShow.convert<Int>()
- }
- ShowWindow(hwnd, nCmdShow)
- msgLoop()
- }
-}
-
-fun msgLoop() {
- memScoped {
- val msg = nativeHeap.alloc<MSG>()
- while (GetMessageA(msg.ptr,
- null,
- 0.convert(),
- 0.convert()) != 0) {
- TranslateMessage(msg.ptr)
- DispatchMessageA(msg.ptr)
- }
- // The queue quits
- nativeHeap.free(msg)
+ ShowWindow(hwnd, WinCompat.nCmdShow)
+ WinApi.msgLoop()
}
}
@@ -103,7 +69,7 @@ fun windowProc(hwnd: HWND?,
lParam: LPARAM): LRESULT {
return when (uMsg.convert<Int>()) {
WM_CREATE -> {
- setAero(hwnd)
+ WinApi.setAero(hwnd)
1
}
WM_DESTROY -> {
@@ -132,7 +98,7 @@ fun dlgProc(hDlg: HWND?,
lParam: LPARAM): INT_PTR {
when (message.convert<Int>()) {
WM_INITDIALOG -> {
- setAero(hDlg)
+ WinApi.setAero(hDlg)
return true.toByte().toLong()
}
WM_COMMAND -> {
@@ -146,25 +112,4 @@ fun dlgProc(hDlg: HWND?,
return false.toByte().toLong()
}
}
-}
-
-fun setAero(hwnd: HWND?) {
- val hUser = GetModuleHandleW("user32.dll") ?: return
- val setWindowCompositionAttribute = GetProcAddress(hUser,
- "SetWindowCompositionAttribute")
- as pfnSetWindowCompositionAttribute? ?: return
- val accent = cValue<ACCENT_POLICY> {
- AccentState = ACCENT_ENABLE_BLURBEHIND
- AccentFlags = 0.convert()
- GradientColor = 0.convert()
- AnimationId = 0.convert()
- }
- memScoped {
- val data = cValue<WINDOWCOMPOSITIONATTRIBDATA> {
- Attrib = WCA_ACCENT_POLICY
- pvData = accent.ptr
- cbData = accent.size.convert()
- }
- setWindowCompositionAttribute(hwnd, data.ptr)
- }
} \ No newline at end of file
diff --git a/src/mingwMain/kotlin/moe/yuuta/aero/WinApi.kt b/src/mingwMain/kotlin/moe/yuuta/aero/WinApi.kt
new file mode 100644
index 0000000..381f002
--- /dev/null
+++ b/src/mingwMain/kotlin/moe/yuuta/aero/WinApi.kt
@@ -0,0 +1,56 @@
+package moe.yuuta.aero
+
+import aero.ACCENT_POLICY
+import aero.WINDOWCOMPOSITIONATTRIBDATA
+import aero.pfnSetWindowCompositionAttribute
+import kotlinx.cinterop.*
+import platform.windows.*
+
+object WinApi {
+ fun getenv(name: String): String = memScoped {
+ val buffer = nativeHeap.allocArray<CHARVar>(MAX_PATH)
+ GetEnvironmentVariableA(name,
+ buffer,
+ MAX_PATH
+ )
+ val env = buffer.toKString()
+ nativeHeap.free(buffer)
+ return@memScoped env
+ }
+
+ fun msgLoop() {
+ memScoped {
+ val msg = nativeHeap.alloc<MSG>()
+ while (GetMessageA(msg.ptr,
+ null,
+ 0.convert(),
+ 0.convert()) != 0) {
+ TranslateMessage(msg.ptr)
+ DispatchMessageA(msg.ptr)
+ }
+ // The queue quits
+ nativeHeap.free(msg)
+ }
+ }
+
+ fun setAero(hwnd: HWND?) {
+ val hUser = GetModuleHandleW("user32.dll") ?: return
+ val setWindowCompositionAttribute = GetProcAddress(hUser,
+ "SetWindowCompositionAttribute")
+ as pfnSetWindowCompositionAttribute? ?: return
+ val accent = cValue<ACCENT_POLICY> {
+ AccentState = aero.ACCENT_ENABLE_BLURBEHIND
+ AccentFlags = 0.convert()
+ GradientColor = 0.convert()
+ AnimationId = 0.convert()
+ }
+ memScoped {
+ val data = cValue<WINDOWCOMPOSITIONATTRIBDATA> {
+ Attrib = aero.WCA_ACCENT_POLICY
+ pvData = accent.ptr
+ cbData = accent.size.convert()
+ }
+ setWindowCompositionAttribute(hwnd, data.ptr)
+ }
+ }
+} \ No newline at end of file
diff --git a/src/mingwMain/kotlin/moe/yuuta/aero/WinCompat.kt b/src/mingwMain/kotlin/moe/yuuta/aero/WinCompat.kt
new file mode 100644
index 0000000..29c78fb
--- /dev/null
+++ b/src/mingwMain/kotlin/moe/yuuta/aero/WinCompat.kt
@@ -0,0 +1,19 @@
+package moe.yuuta.aero
+
+import kotlinx.cinterop.*
+import platform.windows.GetModuleHandleA
+import platform.windows.GetStartupInfoA
+import platform.windows._STARTUPINFOA
+
+object WinCompat {
+ val nCmdShow
+ get() = memScoped {
+ val startUpInfo = nativeHeap.alloc<_STARTUPINFOA>()
+ GetStartupInfoA(startUpInfo.ptr)
+ val nCmdShow = startUpInfo.wShowWindow
+ nativeHeap.free(startUpInfo)
+ return@memScoped nCmdShow.convert<Int>()
+ }
+ val hInstance
+ get() = GetModuleHandleA(null)
+} \ No newline at end of file