zenity/progress_windows.go

277 lines
6.8 KiB
Go
Raw Permalink Normal View History

2021-04-27 09:05:04 -04:00
package zenity
import (
2021-04-29 11:38:59 -04:00
"context"
"sync"
2021-04-27 09:05:04 -04:00
"syscall"
"unsafe"
2022-06-18 07:37:39 -04:00
"github.com/ncruces/zenity/internal/win"
2021-04-27 09:05:04 -04:00
)
func progress(opts options) (ProgressDialog, error) {
if opts.title == nil {
opts.title = stringPtr("")
}
if opts.okLabel == nil {
opts.okLabel = stringPtr("OK")
}
if opts.cancelLabel == nil {
opts.cancelLabel = stringPtr("Cancel")
}
if opts.maxValue == 0 {
opts.maxValue = 100
}
2021-04-29 11:38:59 -04:00
if opts.ctx == nil {
opts.ctx = context.Background()
2021-07-12 13:13:08 -04:00
} else if cerr := opts.ctx.Err(); cerr != nil {
return nil, cerr
2021-04-29 11:38:59 -04:00
}
dlg := &progressDialog{
done: make(chan struct{}),
max: opts.maxValue,
}
dlg.init.Add(1)
2021-04-27 09:05:04 -04:00
2021-04-29 11:38:59 -04:00
go func() {
2022-03-24 08:51:28 -04:00
dlg.err = dlg.setup(opts)
2021-04-29 11:38:59 -04:00
close(dlg.done)
}()
dlg.init.Wait()
return dlg, nil
}
2022-03-24 08:51:28 -04:00
type progressDialog struct {
init sync.WaitGroup
done chan struct{}
max int
err error
2022-06-20 11:05:11 -04:00
wnd win.HWND
textCtl win.HWND
progCtl win.HWND
okBtn win.HWND
cancelBtn win.HWND
extraBtn win.HWND
2022-03-24 08:51:28 -04:00
font font
}
func (d *progressDialog) Text(text string) error {
select {
default:
2022-06-20 11:05:11 -04:00
win.SetWindowText(d.textCtl, strptr(text))
2022-03-24 08:51:28 -04:00
return nil
case <-d.done:
return d.err
}
}
func (d *progressDialog) Value(value int) error {
select {
default:
2022-06-20 11:05:11 -04:00
win.SendMessage(d.progCtl, win.PBM_SETPOS, uintptr(value), 0)
2022-03-24 08:51:28 -04:00
if value >= d.max {
2022-06-20 11:05:11 -04:00
win.EnableWindow(d.okBtn, true)
2022-03-24 08:51:28 -04:00
}
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
}
func (d *progressDialog) Complete() error {
select {
default:
2022-06-21 07:28:21 -04:00
win.SetWindowLong(d.progCtl, win.GWL_STYLE, win.WS_CHILD|win.WS_VISIBLE|win.PBS_SMOOTH)
2022-06-20 11:05:11 -04:00
win.SendMessage(d.progCtl, win.PBM_SETRANGE32, 0, 1)
win.SendMessage(d.progCtl, win.PBM_SETPOS, 1, 0)
win.EnableWindow(d.okBtn, true)
win.EnableWindow(d.cancelBtn, false)
2022-03-24 08:51:28 -04:00
return nil
case <-d.done:
return d.err
}
}
func (d *progressDialog) Close() error {
2022-06-21 07:28:21 -04:00
win.SendMessage(d.wnd, win.WM_SYSCOMMAND, win.SC_CLOSE, 0)
2022-03-24 08:51:28 -04:00
<-d.done
if d.err == ErrCanceled {
return nil
}
return d.err
}
func (dlg *progressDialog) setup(opts options) error {
2022-05-23 19:12:38 -04:00
var once sync.Once
defer once.Do(dlg.init.Done)
2022-03-24 08:51:28 -04:00
2022-06-28 09:52:02 -04:00
owner, _ := opts.attach.(win.HWND)
defer setup(owner)()
dlg.font = getFont()
defer dlg.font.delete()
2022-07-26 11:56:43 -04:00
icon, _ := getIcon(opts.windowIcon)
2022-05-23 19:12:38 -04:00
defer icon.delete()
2021-04-27 09:05:04 -04:00
if opts.ctx != nil && opts.ctx.Err() != nil {
2021-04-29 11:38:59 -04:00
return opts.ctx.Err()
2021-04-27 09:05:04 -04:00
}
2022-06-20 08:37:28 -04:00
instance, err := win.GetModuleHandle(nil)
if err != nil {
2021-04-29 11:38:59 -04:00
return err
2021-04-27 09:05:04 -04:00
}
2022-05-23 19:12:38 -04:00
cls, err := registerClass(instance, icon.handle, syscall.NewCallback(progressProc))
2022-06-20 08:37:28 -04:00
if err != nil {
2021-04-29 11:38:59 -04:00
return err
2021-04-27 09:05:04 -04:00
}
2022-06-20 08:37:28 -04:00
defer win.UnregisterClass(cls, instance)
2021-04-27 09:05:04 -04:00
2022-06-21 07:28:21 -04:00
dlg.wnd, _ = win.CreateWindowEx(_WS_EX_ZEN_DIALOG,
cls, strptr(*opts.title), _WS_ZEN_DIALOG,
win.CW_USEDEFAULT, win.CW_USEDEFAULT,
2022-06-20 11:05:11 -04:00
281, 133, owner, 0, instance, unsafe.Pointer(dlg))
2021-04-27 09:05:04 -04:00
2022-06-20 11:05:11 -04:00
dlg.textCtl, _ = win.CreateWindowEx(0,
2022-06-21 07:28:21 -04:00
strptr("STATIC"), nil, _WS_ZEN_LABEL,
2022-06-20 11:05:11 -04:00
12, 10, 241, 16, dlg.wnd, 0, instance, nil)
2021-04-27 09:05:04 -04:00
2022-06-21 07:28:21 -04:00
var flags uint32 = win.WS_CHILD | win.WS_VISIBLE | win.PBS_SMOOTH
2021-04-27 09:05:04 -04:00
if opts.maxValue < 0 {
2022-06-21 07:28:21 -04:00
flags |= win.PBS_MARQUEE
2021-04-27 09:05:04 -04:00
}
2022-06-20 11:05:11 -04:00
dlg.progCtl, _ = win.CreateWindowEx(0,
2022-06-21 07:28:21 -04:00
strptr(win.PROGRESS_CLASS),
2022-06-20 11:05:11 -04:00
nil, flags,
12, 30, 241, 16, dlg.wnd, 0, instance, nil)
2021-04-27 09:05:04 -04:00
2022-06-20 11:05:11 -04:00
dlg.okBtn, _ = win.CreateWindowEx(0,
2021-04-27 09:05:04 -04:00
strptr("BUTTON"), strptr(*opts.okLabel),
2022-06-21 07:28:21 -04:00
_WS_ZEN_BUTTON|win.BS_DEFPUSHBUTTON|win.WS_DISABLED,
2022-06-20 11:05:11 -04:00
12, 58, 75, 24, dlg.wnd, win.IDOK, instance, nil)
2021-04-30 14:19:14 -04:00
if !opts.noCancel {
2022-06-20 11:05:11 -04:00
dlg.cancelBtn, _ = win.CreateWindowEx(0,
2021-04-30 14:19:14 -04:00
strptr("BUTTON"), strptr(*opts.cancelLabel),
2022-06-21 07:28:21 -04:00
_WS_ZEN_BUTTON,
2022-06-20 11:05:11 -04:00
12, 58, 75, 24, dlg.wnd, win.IDCANCEL, instance, nil)
2021-04-30 14:19:14 -04:00
}
2021-04-27 09:05:04 -04:00
if opts.extraButton != nil {
2022-06-20 11:05:11 -04:00
dlg.extraBtn, _ = win.CreateWindowEx(0,
2021-04-27 09:05:04 -04:00
strptr("BUTTON"), strptr(*opts.extraButton),
2022-06-21 07:28:21 -04:00
_WS_ZEN_BUTTON,
2022-06-20 11:05:11 -04:00
12, 58, 75, 24, dlg.wnd, win.IDNO, instance, nil)
2021-04-27 09:05:04 -04:00
}
dlg.layout(getDPI(dlg.wnd))
2021-04-30 14:19:14 -04:00
centerWindow(dlg.wnd)
2022-06-21 07:28:21 -04:00
win.ShowWindow(dlg.wnd, win.SW_NORMAL)
2021-04-27 09:05:04 -04:00
if opts.maxValue < 0 {
2022-06-20 11:05:11 -04:00
win.SendMessage(dlg.progCtl, win.PBM_SETMARQUEE, 1, 0)
2021-04-27 09:05:04 -04:00
} else {
2022-06-20 11:05:11 -04:00
win.SendMessage(dlg.progCtl, win.PBM_SETRANGE32, 0, uintptr(opts.maxValue))
2021-04-27 09:05:04 -04:00
}
2022-05-23 19:12:38 -04:00
once.Do(dlg.init.Done)
2021-04-27 09:05:04 -04:00
if opts.ctx != nil {
wait := make(chan struct{})
defer close(wait)
go func() {
select {
case <-opts.ctx.Done():
2022-06-21 07:28:21 -04:00
win.SendMessage(dlg.wnd, win.WM_SYSCOMMAND, win.SC_CLOSE, 0)
2021-04-27 09:05:04 -04:00
case <-wait:
}
}()
}
2022-06-19 21:14:08 -04:00
if err := win.MessageLoop(win.HWND(dlg.wnd)); err != nil {
2021-04-29 11:38:59 -04:00
return err
2021-04-27 09:05:04 -04:00
}
if opts.ctx != nil && opts.ctx.Err() != nil {
2021-04-29 11:38:59 -04:00
return opts.ctx.Err()
}
return dlg.err
2021-04-29 11:38:59 -04:00
}
func (d *progressDialog) layout(dpi dpi) {
font := d.font.forDPI(dpi)
2022-06-20 11:05:11 -04:00
win.SendMessage(d.textCtl, win.WM_SETFONT, font, 1)
win.SendMessage(d.okBtn, win.WM_SETFONT, font, 1)
win.SendMessage(d.cancelBtn, win.WM_SETFONT, font, 1)
win.SendMessage(d.extraBtn, win.WM_SETFONT, font, 1)
2022-06-21 07:28:21 -04:00
win.SetWindowPos(d.wnd, 0, 0, 0, dpi.scale(281), dpi.scale(133), win.SWP_NOMOVE|win.SWP_NOZORDER)
win.SetWindowPos(d.textCtl, 0, dpi.scale(12), dpi.scale(10), dpi.scale(241), dpi.scale(16), win.SWP_NOZORDER)
win.SetWindowPos(d.progCtl, 0, dpi.scale(12), dpi.scale(30), dpi.scale(241), dpi.scale(16), win.SWP_NOZORDER)
if d.extraBtn == 0 {
if d.cancelBtn == 0 {
2022-06-21 07:28:21 -04:00
win.SetWindowPos(d.okBtn, 0, dpi.scale(178), dpi.scale(58), dpi.scale(75), dpi.scale(24), win.SWP_NOZORDER)
} else {
2022-06-21 07:28:21 -04:00
win.SetWindowPos(d.okBtn, 0, dpi.scale(95), dpi.scale(58), dpi.scale(75), dpi.scale(24), win.SWP_NOZORDER)
win.SetWindowPos(d.cancelBtn, 0, dpi.scale(178), dpi.scale(58), dpi.scale(75), dpi.scale(24), win.SWP_NOZORDER)
}
} else {
if d.cancelBtn == 0 {
2022-06-21 07:28:21 -04:00
win.SetWindowPos(d.okBtn, 0, dpi.scale(95), dpi.scale(58), dpi.scale(75), dpi.scale(24), win.SWP_NOZORDER)
win.SetWindowPos(d.extraBtn, 0, dpi.scale(178), dpi.scale(58), dpi.scale(75), dpi.scale(24), win.SWP_NOZORDER)
} else {
2022-06-21 07:28:21 -04:00
win.SetWindowPos(d.okBtn, 0, dpi.scale(12), dpi.scale(58), dpi.scale(75), dpi.scale(24), win.SWP_NOZORDER)
win.SetWindowPos(d.extraBtn, 0, dpi.scale(95), dpi.scale(58), dpi.scale(75), dpi.scale(24), win.SWP_NOZORDER)
win.SetWindowPos(d.cancelBtn, 0, dpi.scale(178), dpi.scale(58), dpi.scale(75), dpi.scale(24), win.SWP_NOZORDER)
}
}
}
2022-06-20 20:26:03 -04:00
func progressProc(wnd win.HWND, msg uint32, wparam uintptr, lparam *unsafe.Pointer) uintptr {
var dlg *progressDialog
switch msg {
2022-06-18 19:48:38 -04:00
case win.WM_NCCREATE:
2022-06-20 20:26:03 -04:00
saveBackRef(uintptr(wnd), *lparam)
dlg = (*progressDialog)(*lparam)
2022-06-18 19:48:38 -04:00
case win.WM_NCDESTROY:
2022-06-20 20:26:03 -04:00
deleteBackRef(uintptr(wnd))
default:
2022-06-20 20:26:03 -04:00
dlg = (*progressDialog)(loadBackRef(uintptr(wnd)))
}
switch msg {
2022-06-18 19:48:38 -04:00
case win.WM_DESTROY:
2022-06-20 20:26:03 -04:00
win.PostQuitMessage(0)
2022-06-18 19:48:38 -04:00
case win.WM_CLOSE:
dlg.err = ErrCanceled
2022-06-20 20:26:03 -04:00
win.DestroyWindow(wnd)
2022-06-18 19:48:38 -04:00
case win.WM_COMMAND:
switch wparam {
default:
return 1
2022-06-18 07:37:39 -04:00
case win.IDOK, win.IDYES:
//
2022-06-18 07:37:39 -04:00
case win.IDCANCEL:
dlg.err = ErrCanceled
2022-06-18 07:37:39 -04:00
case win.IDNO:
dlg.err = ErrExtraButton
}
2022-06-20 20:26:03 -04:00
win.DestroyWindow(wnd)
2022-06-18 19:48:38 -04:00
case win.WM_DPICHANGED:
dlg.layout(dpi(uint32(wparam) >> 16))
default:
2022-06-20 20:26:03 -04:00
return win.DefWindowProc(wnd, msg, wparam, unsafe.Pointer(lparam))
}
return 0
}