Refactor.

This commit is contained in:
Nuno Cruces 2022-03-24 12:51:28 +00:00
parent 537e3cbab3
commit 418cd141e0
3 changed files with 153 additions and 142 deletions

View File

@ -16,8 +16,26 @@ func entry(text string, opts options) (string, error) {
opts.cancelLabel = stringPtr("Cancel") opts.cancelLabel = stringPtr("Cancel")
} }
dlg := &entryDialog{}
return dlg.setup(text, opts)
}
type entryDialog struct {
out string
err error
wnd uintptr
textCtl uintptr
editCtl uintptr
okBtn uintptr
cancelBtn uintptr
extraBtn uintptr
font font
}
func (dlg *entryDialog) setup(text string, opts options) (string, error) {
defer setup()() defer setup()()
dlg := &entryDialog{font: getFont()} dlg.font = getFont()
defer dlg.font.delete() defer dlg.font.delete()
if opts.ctx != nil && opts.ctx.Err() != nil { if opts.ctx != nil && opts.ctx.Err() != nil {
@ -98,36 +116,23 @@ func entry(text string, opts options) (string, error) {
return dlg.out, dlg.err return dlg.out, dlg.err
} }
type entryDialog struct { func (dlg *entryDialog) layout(dpi dpi) {
out string font := dlg.font.forDPI(dpi)
err error sendMessage.Call(dlg.textCtl, 0x0030 /* WM_SETFONT */, font, 1)
sendMessage.Call(dlg.editCtl, 0x0030 /* WM_SETFONT */, font, 1)
wnd uintptr sendMessage.Call(dlg.okBtn, 0x0030 /* WM_SETFONT */, font, 1)
textCtl uintptr sendMessage.Call(dlg.cancelBtn, 0x0030 /* WM_SETFONT */, font, 1)
editCtl uintptr sendMessage.Call(dlg.extraBtn, 0x0030 /* WM_SETFONT */, font, 1)
okBtn uintptr setWindowPos.Call(dlg.wnd, 0, 0, 0, dpi.scale(281), dpi.scale(141), 0x6) // SWP_NOZORDER|SWP_NOMOVE
cancelBtn uintptr setWindowPos.Call(dlg.textCtl, 0, dpi.scale(12), dpi.scale(10), dpi.scale(241), dpi.scale(16), 0x4) // SWP_NOZORDER
extraBtn uintptr setWindowPos.Call(dlg.editCtl, 0, dpi.scale(12), dpi.scale(30), dpi.scale(241), dpi.scale(24), 0x4) // SWP_NOZORDER
font font if dlg.extraBtn == 0 {
} setWindowPos.Call(dlg.okBtn, 0, dpi.scale(95), dpi.scale(66), dpi.scale(75), dpi.scale(24), 0x4) // SWP_NOZORDER
setWindowPos.Call(dlg.cancelBtn, 0, dpi.scale(178), dpi.scale(66), dpi.scale(75), dpi.scale(24), 0x4) // SWP_NOZORDER
func (d *entryDialog) layout(dpi dpi) {
font := d.font.forDPI(dpi)
sendMessage.Call(d.textCtl, 0x0030 /* WM_SETFONT */, font, 1)
sendMessage.Call(d.editCtl, 0x0030 /* WM_SETFONT */, font, 1)
sendMessage.Call(d.okBtn, 0x0030 /* WM_SETFONT */, font, 1)
sendMessage.Call(d.cancelBtn, 0x0030 /* WM_SETFONT */, font, 1)
sendMessage.Call(d.extraBtn, 0x0030 /* WM_SETFONT */, font, 1)
setWindowPos.Call(d.wnd, 0, 0, 0, dpi.scale(281), dpi.scale(141), 0x6) // SWP_NOZORDER|SWP_NOMOVE
setWindowPos.Call(d.textCtl, 0, dpi.scale(12), dpi.scale(10), dpi.scale(241), dpi.scale(16), 0x4) // SWP_NOZORDER
setWindowPos.Call(d.editCtl, 0, dpi.scale(12), dpi.scale(30), dpi.scale(241), dpi.scale(24), 0x4) // SWP_NOZORDER
if d.extraBtn == 0 {
setWindowPos.Call(d.okBtn, 0, dpi.scale(95), dpi.scale(66), dpi.scale(75), dpi.scale(24), 0x4) // SWP_NOZORDER
setWindowPos.Call(d.cancelBtn, 0, dpi.scale(178), dpi.scale(66), dpi.scale(75), dpi.scale(24), 0x4) // SWP_NOZORDER
} else { } else {
setWindowPos.Call(d.okBtn, 0, dpi.scale(12), dpi.scale(66), dpi.scale(75), dpi.scale(24), 0x4) // SWP_NOZORDER setWindowPos.Call(dlg.okBtn, 0, dpi.scale(12), dpi.scale(66), dpi.scale(75), dpi.scale(24), 0x4) // SWP_NOZORDER
setWindowPos.Call(d.extraBtn, 0, dpi.scale(95), dpi.scale(66), dpi.scale(75), dpi.scale(24), 0x4) // SWP_NOZORDER setWindowPos.Call(dlg.extraBtn, 0, dpi.scale(95), dpi.scale(66), dpi.scale(75), dpi.scale(24), 0x4) // SWP_NOZORDER
setWindowPos.Call(d.cancelBtn, 0, dpi.scale(178), dpi.scale(66), dpi.scale(75), dpi.scale(24), 0x4) // SWP_NOZORDER setWindowPos.Call(dlg.cancelBtn, 0, dpi.scale(178), dpi.scale(66), dpi.scale(75), dpi.scale(24), 0x4) // SWP_NOZORDER
} }
} }

View File

@ -28,12 +28,31 @@ func listDlg(text string, items []string, multiple bool, opts options) ([]string
opts.cancelLabel = stringPtr("Cancel") opts.cancelLabel = stringPtr("Cancel")
} }
defer setup()()
dlg := &listDialog{ dlg := &listDialog{
items: items, items: items,
multiple: multiple, multiple: multiple,
font: getFont(),
} }
return dlg.setup(text, opts)
}
type listDialog struct {
items []string
multiple bool
out []string
err error
wnd uintptr
textCtl uintptr
listCtl uintptr
okBtn uintptr
cancelBtn uintptr
extraBtn uintptr
font font
}
func (dlg *listDialog) setup(text string, opts options) ([]string, error) {
defer setup()()
dlg.font = getFont()
defer dlg.font.delete() defer dlg.font.delete()
if opts.ctx != nil && opts.ctx.Err() != nil { if opts.ctx != nil && opts.ctx.Err() != nil {
@ -64,7 +83,7 @@ func listDlg(text string, items []string, multiple bool, opts options) ([]string
12, 10, 241, 16, dlg.wnd, 0, instance, 0) 12, 10, 241, 16, dlg.wnd, 0, instance, 0)
var flags uintptr = 0x50320000 // WS_CHILD|WS_VISIBLE|WS_VSCROLL|WS_GROUP|WS_TABSTOP var flags uintptr = 0x50320000 // WS_CHILD|WS_VISIBLE|WS_VSCROLL|WS_GROUP|WS_TABSTOP
if multiple { if dlg.multiple {
flags |= 0x0800 // LBS_EXTENDEDSEL flags |= 0x0800 // LBS_EXTENDEDSEL
} }
dlg.listCtl, _, _ = createWindowEx.Call(0x200, // WS_EX_CLIENTEDGE dlg.listCtl, _, _ = createWindowEx.Call(0x200, // WS_EX_CLIENTEDGE
@ -87,7 +106,7 @@ func listDlg(text string, items []string, multiple bool, opts options) ([]string
12, 206, 75, 24, dlg.wnd, 7 /* IDNO */, instance, 0) 12, 206, 75, 24, dlg.wnd, 7 /* IDNO */, instance, 0)
} }
for _, item := range items { for _, item := range dlg.items {
sendMessage.Call(dlg.listCtl, 0x180 /* LB_ADDSTRING */, 0, strptr(item)) sendMessage.Call(dlg.listCtl, 0x180 /* LB_ADDSTRING */, 0, strptr(item))
} }
@ -117,38 +136,23 @@ func listDlg(text string, items []string, multiple bool, opts options) ([]string
return dlg.out, dlg.err return dlg.out, dlg.err
} }
type listDialog struct { func (dlg *listDialog) layout(dpi dpi) {
items []string font := dlg.font.forDPI(dpi)
multiple bool sendMessage.Call(dlg.textCtl, 0x0030 /* WM_SETFONT */, font, 1)
out []string sendMessage.Call(dlg.listCtl, 0x0030 /* WM_SETFONT */, font, 1)
err error sendMessage.Call(dlg.okBtn, 0x0030 /* WM_SETFONT */, font, 1)
sendMessage.Call(dlg.cancelBtn, 0x0030 /* WM_SETFONT */, font, 1)
wnd uintptr sendMessage.Call(dlg.extraBtn, 0x0030 /* WM_SETFONT */, font, 1)
textCtl uintptr setWindowPos.Call(dlg.wnd, 0, 0, 0, dpi.scale(281), dpi.scale(281), 0x6) // SWP_NOZORDER|SWP_NOMOVE
listCtl uintptr setWindowPos.Call(dlg.textCtl, 0, dpi.scale(12), dpi.scale(10), dpi.scale(241), dpi.scale(16), 0x4) // SWP_NOZORDER
okBtn uintptr setWindowPos.Call(dlg.listCtl, 0, dpi.scale(12), dpi.scale(30), dpi.scale(241), dpi.scale(164), 0x4) // SWP_NOZORDER
cancelBtn uintptr if dlg.extraBtn == 0 {
extraBtn uintptr setWindowPos.Call(dlg.okBtn, 0, dpi.scale(95), dpi.scale(206), dpi.scale(75), dpi.scale(24), 0x4) // SWP_NOZORDER
font font setWindowPos.Call(dlg.cancelBtn, 0, dpi.scale(178), dpi.scale(206), dpi.scale(75), dpi.scale(24), 0x4) // SWP_NOZORDER
}
func (d *listDialog) layout(dpi dpi) {
font := d.font.forDPI(dpi)
sendMessage.Call(d.textCtl, 0x0030 /* WM_SETFONT */, font, 1)
sendMessage.Call(d.listCtl, 0x0030 /* WM_SETFONT */, font, 1)
sendMessage.Call(d.okBtn, 0x0030 /* WM_SETFONT */, font, 1)
sendMessage.Call(d.cancelBtn, 0x0030 /* WM_SETFONT */, font, 1)
sendMessage.Call(d.extraBtn, 0x0030 /* WM_SETFONT */, font, 1)
setWindowPos.Call(d.wnd, 0, 0, 0, dpi.scale(281), dpi.scale(281), 0x6) // SWP_NOZORDER|SWP_NOMOVE
setWindowPos.Call(d.textCtl, 0, dpi.scale(12), dpi.scale(10), dpi.scale(241), dpi.scale(16), 0x4) // SWP_NOZORDER
setWindowPos.Call(d.listCtl, 0, dpi.scale(12), dpi.scale(30), dpi.scale(241), dpi.scale(164), 0x4) // SWP_NOZORDER
if d.extraBtn == 0 {
setWindowPos.Call(d.okBtn, 0, dpi.scale(95), dpi.scale(206), dpi.scale(75), dpi.scale(24), 0x4) // SWP_NOZORDER
setWindowPos.Call(d.cancelBtn, 0, dpi.scale(178), dpi.scale(206), dpi.scale(75), dpi.scale(24), 0x4) // SWP_NOZORDER
} else { } else {
setWindowPos.Call(d.okBtn, 0, dpi.scale(12), dpi.scale(206), dpi.scale(75), dpi.scale(24), 0x4) // SWP_NOZORDER setWindowPos.Call(dlg.okBtn, 0, dpi.scale(12), dpi.scale(206), dpi.scale(75), dpi.scale(24), 0x4) // SWP_NOZORDER
setWindowPos.Call(d.extraBtn, 0, dpi.scale(95), dpi.scale(206), dpi.scale(75), dpi.scale(24), 0x4) // SWP_NOZORDER setWindowPos.Call(dlg.extraBtn, 0, dpi.scale(95), dpi.scale(206), dpi.scale(75), dpi.scale(24), 0x4) // SWP_NOZORDER
setWindowPos.Call(d.cancelBtn, 0, dpi.scale(178), dpi.scale(206), dpi.scale(75), dpi.scale(24), 0x4) // SWP_NOZORDER setWindowPos.Call(dlg.cancelBtn, 0, dpi.scale(178), dpi.scale(206), dpi.scale(75), dpi.scale(24), 0x4) // SWP_NOZORDER
} }
} }

View File

@ -33,11 +33,7 @@ func progress(opts options) (ProgressDialog, error) {
dlg.init.Add(1) dlg.init.Add(1)
go func() { go func() {
err := progressDlg(opts, dlg) dlg.err = dlg.setup(opts)
if cerr := opts.ctx.Err(); cerr != nil {
err = cerr
}
dlg.err = err
close(dlg.done) close(dlg.done)
}() }()
@ -45,8 +41,83 @@ func progress(opts options) (ProgressDialog, error) {
return dlg, nil return dlg, nil
} }
func progressDlg(opts options, dlg *progressDialog) error { type progressDialog struct {
defer dlg.init.Done() init sync.WaitGroup
done chan struct{}
max int
err error
wnd uintptr
textCtl uintptr
progCtl uintptr
okBtn uintptr
cancelBtn uintptr
extraBtn uintptr
font font
}
func (d *progressDialog) Text(text string) error {
select {
default:
setWindowText.Call(d.textCtl, strptr(text))
return nil
case <-d.done:
return d.err
}
}
func (d *progressDialog) Value(value int) error {
select {
default:
sendMessage.Call(d.progCtl, 0x402 /* PBM_SETPOS */, uintptr(value), 0)
if value >= d.max {
enableWindow.Call(d.okBtn, 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
}
func (d *progressDialog) Complete() error {
select {
default:
setWindowLong.Call(d.progCtl, intptr(-16) /* GWL_STYLE */, 0x50000001 /* WS_CHILD|WS_VISIBLE|PBS_SMOOTH */)
sendMessage.Call(d.progCtl, 0x406 /* PBM_SETRANGE32 */, 0, 1)
sendMessage.Call(d.progCtl, 0x402 /* PBM_SETPOS */, 1, 0)
enableWindow.Call(d.okBtn, 1)
enableWindow.Call(d.cancelBtn, 0)
return nil
case <-d.done:
return d.err
}
}
func (d *progressDialog) Close() error {
sendMessage.Call(d.wnd, 0x0112 /* WM_SYSCOMMAND */, 0xf060 /* SC_CLOSE */, 0)
<-d.done
if d.err == ErrCanceled {
return nil
}
return d.err
}
func (dlg *progressDialog) setup(opts options) error {
done := false
defer func() {
if !done {
dlg.init.Done()
}
}()
defer setup()() defer setup()()
dlg.font = getFont() dlg.font = getFont()
defer dlg.font.delete() defer dlg.font.delete()
@ -112,8 +183,8 @@ func progressDlg(opts options, dlg *progressDialog) error {
} else { } else {
sendMessage.Call(dlg.progCtl, 0x406 /* PBM_SETRANGE32 */, 0, uintptr(opts.maxValue)) sendMessage.Call(dlg.progCtl, 0x406 /* PBM_SETRANGE32 */, 0, uintptr(opts.maxValue))
} }
defer dlg.init.Add(1)
dlg.init.Done() dlg.init.Done()
done = true
if opts.ctx != nil { if opts.ctx != nil {
wait := make(chan struct{}) wait := make(chan struct{})
@ -136,75 +207,6 @@ func progressDlg(opts options, dlg *progressDialog) error {
return dlg.err return dlg.err
} }
type progressDialog struct {
done chan struct{}
init sync.WaitGroup
max int
err error
wnd uintptr
textCtl uintptr
progCtl uintptr
okBtn uintptr
cancelBtn uintptr
extraBtn uintptr
font font
}
func (d *progressDialog) Text(text string) error {
select {
default:
setWindowText.Call(d.textCtl, strptr(text))
return nil
case <-d.done:
return d.err
}
}
func (d *progressDialog) Value(value int) error {
select {
default:
sendMessage.Call(d.progCtl, 0x402 /* PBM_SETPOS */, uintptr(value), 0)
if value >= d.max {
enableWindow.Call(d.okBtn, 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
}
func (d *progressDialog) Complete() error {
select {
default:
setWindowLong.Call(d.progCtl, intptr(-16) /* GWL_STYLE */, 0x50000001 /* WS_CHILD|WS_VISIBLE|PBS_SMOOTH */)
sendMessage.Call(d.progCtl, 0x406 /* PBM_SETRANGE32 */, 0, 1)
sendMessage.Call(d.progCtl, 0x402 /* PBM_SETPOS */, 1, 0)
enableWindow.Call(d.okBtn, 1)
enableWindow.Call(d.cancelBtn, 0)
return nil
case <-d.done:
return d.err
}
}
func (d *progressDialog) Close() error {
sendMessage.Call(d.wnd, 0x0112 /* WM_SYSCOMMAND */, 0xf060 /* SC_CLOSE */, 0)
<-d.done
if d.err == ErrCanceled {
return nil
}
return d.err
}
func (d *progressDialog) layout(dpi dpi) { func (d *progressDialog) layout(dpi dpi) {
font := d.font.forDPI(dpi) font := d.font.forDPI(dpi)
sendMessage.Call(d.textCtl, 0x0030 /* WM_SETFONT */, font, 1) sendMessage.Call(d.textCtl, 0x0030 /* WM_SETFONT */, font, 1)