Skip to content

Commit ef0db38

Browse files
committed
Improve flow of Windows GUI
- Consolidate error checking to before window ever opens
1 parent 0150a09 commit ef0db38

File tree

1 file changed

+23
-23
lines changed

1 file changed

+23
-23
lines changed

_ref/GUI/GUI_Windows.go

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import (
1414

1515
const (
1616
// kanziSFX accelerator
17-
accelerator int64 = 500000
17+
ACCELERATOR int64 = 500000
1818

1919
// Window dimensions
2020

@@ -45,6 +45,9 @@ const (
4545
)
4646

4747
var (
48+
// Default path
49+
defaultPath string
50+
4851
// Path buffer
4952
pathBuffer = make([]uint16, MAX_PATH)
5053

@@ -54,13 +57,16 @@ var (
5457
// Progress tracker provided to kanziSFX
5558
progress [2]int64
5659

57-
// Track if extraction is running or not
58-
running bool
59-
6060
// Errors
6161
err error
6262
)
6363

64+
// Function to report error and exit
65+
func errExit(err error, code int) {
66+
MessageBox(0, CStr(err.Error()), CStr("Error"), MB_ICONERROR)
67+
os.Exit(code)
68+
}
69+
6470
// Window callback function
6571
func proc(hwnd syscall.Handle, msg uint32, wparam, lparam uintptr) (uintptr) {
6672
switch msg {
@@ -75,18 +81,6 @@ func proc(hwnd syscall.Handle, msg uint32, wparam, lparam uintptr) (uintptr) {
7581
uintptr(TEXT),
7682
0, 0,
7783
)
78-
defaultPath, err := os.Executable()
79-
if err != nil {
80-
MessageBox(uintptr(hwnd), CStr(err.Error()), CStr("Error"), MB_ICONERROR)
81-
DestroyWindow(uintptr(hwnd))
82-
} else {
83-
defaultPath, err = filepath.EvalSymlinks(defaultPath)
84-
if err != nil {
85-
MessageBox(uintptr(hwnd), CStr(err.Error()), CStr("Error"), MB_ICONERROR)
86-
DestroyWindow(uintptr(hwnd))
87-
}
88-
}
89-
defaultPath = strings.TrimSuffix(defaultPath, filepath.Ext(defaultPath))
9084
CreateWindowEx(
9185
WS_EX_CLIENTEDGE,
9286
CStr("edit"),
@@ -193,9 +187,9 @@ func proc(hwnd syscall.Handle, msg uint32, wparam, lparam uintptr) (uintptr) {
193187
)
194188
}
195189
pathBufferStr := syscall.UTF16ToString(pathBuffer)
190+
running := true
196191
go func() {
197-
running = true
198-
err = Extract(&pathBufferStr, accelerator, nil, &progress, REWRITE_PATH)
192+
err = Extract(&pathBufferStr, ACCELERATOR, nil, &progress, REWRITE_PATH)
199193
running = false
200194
if err != nil {
201195
PostMessage(
@@ -285,20 +279,26 @@ func main() {
285279
ctx := make(map[string]any)
286280

287281
// Call kanziSFX
288-
err = Extract(outNamePtr, accelerator, ctx, nil, INFO)
282+
err = Extract(outNamePtr, ACCELERATOR, ctx, nil, INFO)
289283

290284
// If there was an error, report it and exit
291-
if err != nil {
292-
MessageBox(0, CStr(err.Error()), CStr("Error"), MB_ICONERROR)
293-
os.Exit(1)
294-
}
285+
if err != nil {errExit(err, 1)}
295286

296287
// Check if Kanzi bit stream contains tar or not
297288
if ctx["tar"].(bool) {tar = true}
298289

299290
// Check if output size is present to use with progress tracking
300291
if value, hasKey := ctx["outputSize"]; hasKey {progress[1] = value.(int64)}
301292

293+
// Generate default path
294+
defaultPath, err = os.Executable()
295+
if err != nil {errExit(err, 2)
296+
} else {
297+
defaultPath, err = filepath.EvalSymlinks(defaultPath)
298+
if err != nil {errExit(err, 3)}
299+
}
300+
defaultPath = strings.TrimSuffix(filepath.Base(defaultPath), filepath.Ext(defaultPath))
301+
302302
// Get screen dimensions
303303

304304
screenWidth := GetSystemMetrics(SM_CXSCREEN)

0 commit comments

Comments
 (0)