WIP: progress (windows).

This commit is contained in:
Nuno Cruces 2021-04-29 16:38:59 +01:00
parent 8db7926394
commit e6ec7f8962
4 changed files with 106 additions and 24 deletions

View File

@ -69,8 +69,8 @@ func entry(text string, opts options) (out string, err error) {
layout(dpi(uint32(wparam) >> 16)) layout(dpi(uint32(wparam) >> 16))
default: default:
ret, _, _ := syscall.Syscall6(defWindowProc, 4, wnd, uintptr(msg), wparam, lparam, 0, 0) res, _, _ := syscall.Syscall6(defWindowProc, 4, wnd, uintptr(msg), wparam, lparam, 0, 0)
return ret return res
} }
return 0 return 0

View File

@ -99,8 +99,8 @@ func listDlg(text string, items []string, multiple bool, opts options) (out []st
layout(dpi(uint32(wparam) >> 16)) layout(dpi(uint32(wparam) >> 16))
default: default:
ret, _, _ := syscall.Syscall6(defWindowProc, 4, wnd, uintptr(msg), wparam, lparam, 0, 0) res, _, _ := syscall.Syscall6(defWindowProc, 4, wnd, uintptr(msg), wparam, lparam, 0, 0)
return ret return res
} }
return 0 return 0

View File

@ -1,6 +1,8 @@
package zenity package zenity
import ( import (
"context"
"sync"
"syscall" "syscall"
) )
@ -17,7 +19,30 @@ func progress(opts options) (ProgressDialog, error) {
if opts.maxValue == 0 { if opts.maxValue == 0 {
opts.maxValue = 100 opts.maxValue = 100
} }
if opts.ctx == nil {
opts.ctx = context.Background()
}
dlg := &progressDialog{
done: make(chan struct{}),
max: opts.maxValue,
}
dlg.init.Add(1)
go func() {
err := progressDlg(opts, dlg)
if cerr := opts.ctx.Err(); cerr != nil {
err = cerr
}
dlg.err = err
close(dlg.done)
}()
dlg.init.Wait()
return dlg, nil
}
func progressDlg(opts options, dlg *progressDialog) (err error) {
defer setup()() defer setup()()
font := getFont() font := getFont()
defer font.Delete() defer font.Delete()
@ -51,6 +76,7 @@ func progress(opts options) (ProgressDialog, error) {
postQuitMessage.Call(0) postQuitMessage.Call(0)
case 0x0010: // WM_CLOSE case 0x0010: // WM_CLOSE
err = ErrCanceled
destroyWindow.Call(wnd) destroyWindow.Call(wnd)
case 0x0111: // WM_COMMAND case 0x0111: // WM_COMMAND
@ -58,8 +84,11 @@ func progress(opts options) (ProgressDialog, error) {
default: default:
return 1 return 1
case 1, 6: // IDOK, IDYES case 1, 6: // IDOK, IDYES
//
case 2: // IDCANCEL case 2: // IDCANCEL
err = ErrCanceled
case 7: // IDNO case 7: // IDNO
err = ErrExtraButton
} }
destroyWindow.Call(wnd) destroyWindow.Call(wnd)
@ -67,25 +96,25 @@ func progress(opts options) (ProgressDialog, error) {
layout(dpi(uint32(wparam) >> 16)) layout(dpi(uint32(wparam) >> 16))
default: default:
ret, _, _ := syscall.Syscall6(defWindowProc, 4, wnd, uintptr(msg), wparam, lparam, 0, 0) res, _, _ := syscall.Syscall6(defWindowProc, 4, wnd, uintptr(msg), wparam, lparam, 0, 0)
return ret return res
} }
return 0 return 0
} }
if opts.ctx != nil && opts.ctx.Err() != nil { if opts.ctx != nil && opts.ctx.Err() != nil {
return nil, opts.ctx.Err() return opts.ctx.Err()
} }
instance, _, err := getModuleHandle.Call(0) instance, _, err := getModuleHandle.Call(0)
if instance == 0 { if instance == 0 {
return nil, err return err
} }
cls, err := registerClass(instance, syscall.NewCallback(proc)) cls, err := registerClass(instance, syscall.NewCallback(proc))
if cls == 0 { if cls == 0 {
return nil, err return err
} }
defer unregisterClass.Call(cls, instance) defer unregisterClass.Call(cls, instance)
@ -112,7 +141,7 @@ func progress(opts options) (ProgressDialog, error) {
okBtn, _, _ = createWindowEx.Call(0, okBtn, _, _ = createWindowEx.Call(0,
strptr("BUTTON"), strptr(*opts.okLabel), strptr("BUTTON"), strptr(*opts.okLabel),
0x50030001, // WS_CHILD|WS_VISIBLE|WS_GROUP|WS_TABSTOP|BS_DEFPUSHBUTTON 0x58030001, // WS_CHILD|WS_VISIBLE|WS_DISABLED|WS_GROUP|WS_TABSTOP|BS_DEFPUSHBUTTON
12, 66, 75, 24, wnd, 1 /* IDOK */, instance, 0) 12, 66, 75, 24, wnd, 1 /* IDOK */, instance, 0)
cancelBtn, _, _ = createWindowEx.Call(0, cancelBtn, _, _ = createWindowEx.Call(0,
strptr("BUTTON"), strptr(*opts.cancelLabel), strptr("BUTTON"), strptr(*opts.cancelLabel),
@ -131,9 +160,13 @@ func progress(opts options) (ProgressDialog, error) {
if opts.maxValue < 0 { if opts.maxValue < 0 {
sendMessage.Call(progCtl, 0x40a /* PBM_SETMARQUEE */, 1, 0) sendMessage.Call(progCtl, 0x40a /* PBM_SETMARQUEE */, 1, 0)
} else { } else {
sendMessage.Call(progCtl, 0x402 /* PBM_SETPOS */, 33, 0)
sendMessage.Call(progCtl, 0x406 /* PBM_SETRANGE32 */, 0, uintptr(opts.maxValue)) sendMessage.Call(progCtl, 0x406 /* PBM_SETRANGE32 */, 0, uintptr(opts.maxValue))
} }
dlg.prog = progCtl
dlg.text = textCtl
dlg.ok = okBtn
dlg.wnd = wnd
dlg.init.Done()
if opts.ctx != nil { if opts.ctx != nil {
wait := make(chan struct{}) wait := make(chan struct{})
@ -148,13 +181,61 @@ func progress(opts options) (ProgressDialog, error) {
} }
// set default values // set default values
// out, ok, err = "", false, nil err = nil
if err := messageLoop(wnd); err != nil { if err := messageLoop(wnd); err != nil {
return nil, err return err
} }
if opts.ctx != nil && opts.ctx.Err() != nil { if opts.ctx != nil && opts.ctx.Err() != nil {
return nil, opts.ctx.Err() return opts.ctx.Err()
} }
return nil, err return err
}
type progressDialog struct {
err error
done chan struct{}
init sync.WaitGroup
prog uintptr
text uintptr
wnd uintptr
ok uintptr
max int
}
func (d *progressDialog) Close() error {
sendMessage.Call(d.wnd, 0x0112 /* WM_SYSCOMMAND */, 0xf060 /* SC_CLOSE */, 0)
<-d.done
return d.err
}
func (d *progressDialog) Text(text string) error {
select {
default:
setWindowText.Call(d.text, strptr(text))
return nil
case <-d.done:
return d.err
}
}
func (d *progressDialog) Value(value int) error {
select {
default:
sendMessage.Call(d.prog, 0x402 /* PBM_SETPOS */, uintptr(value), 0)
if value >= d.max {
enableWindow.Call(d.ok, 1)
}
return nil
case <-d.done:
return d.err
}
}
func (d *progressDialog) MaxValue() int {
return d.max
}
func (d *progressDialog) Done() <-chan struct{} {
return d.done
} }

View File

@ -72,6 +72,7 @@ var (
destroyWindow = user32.NewProc("DestroyWindow") destroyWindow = user32.NewProc("DestroyWindow")
createWindowEx = user32.NewProc("CreateWindowExW") createWindowEx = user32.NewProc("CreateWindowExW")
showWindow = user32.NewProc("ShowWindow") showWindow = user32.NewProc("ShowWindow")
enableWindow = user32.NewProc("EnableWindow")
setFocus = user32.NewProc("SetFocus") setFocus = user32.NewProc("SetFocus")
defWindowProc = user32.NewProc("DefWindowProcW") defWindowProc = user32.NewProc("DefWindowProcW")
) )
@ -267,8 +268,8 @@ func (f *font) Delete() {
func centerWindow(wnd uintptr) { func centerWindow(wnd uintptr) {
getMetric := func(i uintptr) int32 { getMetric := func(i uintptr) int32 {
ret, _, _ := getSystemMetrics.Call(i) n, _, _ := getSystemMetrics.Call(i)
return int32(ret) return int32(n)
} }
var rect _RECT var rect _RECT
@ -295,8 +296,8 @@ func registerClass(instance, proc uintptr) (uintptr, error) {
wcx.Background = 5 // COLOR_WINDOW wcx.Background = 5 // COLOR_WINDOW
wcx.ClassName = syscall.StringToUTF16Ptr(name) wcx.ClassName = syscall.StringToUTF16Ptr(name)
ret, _, err := registerClassEx.Call(uintptr(unsafe.Pointer(&wcx))) atom, _, err := registerClassEx.Call(uintptr(unsafe.Pointer(&wcx)))
return ret, err return atom, err
} }
// https://docs.microsoft.com/en-us/windows/win32/winmsg/using-messages-and-message-queues // https://docs.microsoft.com/en-us/windows/win32/winmsg/using-messages-and-message-queues
@ -308,16 +309,16 @@ func messageLoop(wnd uintptr) error {
for { for {
var msg _MSG var msg _MSG
ret, _, err := syscall.Syscall6(getMessage, 4, uintptr(unsafe.Pointer(&msg)), 0, 0, 0, 0, 0) s, _, err := syscall.Syscall6(getMessage, 4, uintptr(unsafe.Pointer(&msg)), 0, 0, 0, 0, 0)
if int32(ret) == -1 { if int32(s) == -1 {
return err return err
} }
if ret == 0 { if s == 0 {
return nil return nil
} }
ret, _, _ = syscall.Syscall(isDialogMessage, 2, wnd, uintptr(unsafe.Pointer(&msg)), 0) s, _, _ = syscall.Syscall(isDialogMessage, 2, wnd, uintptr(unsafe.Pointer(&msg)), 0)
if ret == 0 { if s == 0 {
syscall.Syscall(translateMessage, 1, uintptr(unsafe.Pointer(&msg)), 0, 0) syscall.Syscall(translateMessage, 1, uintptr(unsafe.Pointer(&msg)), 0, 0)
syscall.Syscall(dispatchMessage, 1, uintptr(unsafe.Pointer(&msg)), 0, 0) syscall.Syscall(dispatchMessage, 1, uintptr(unsafe.Pointer(&msg)), 0, 0)
} }