diff --git a/README.md b/README.md index d4d9fd9..50c5f7e 100644 --- a/README.md +++ b/README.md @@ -15,6 +15,8 @@ For now, these are the only implemented dialogs: * [message](https://github.com/ncruces/zenity/wiki/Message-dialog) (error, info, question, warning) * [file selection](https://github.com/ncruces/zenity/wiki/File-Selection-dialog) * [color selection](https://github.com/ncruces/zenity/wiki/Color-Selection-dialog) +* [text entry](https://github.com/ncruces/zenity/wiki/Text-Entry-dialog) +* [password](https://github.com/ncruces/zenity/wiki/Password-dialog) * [notification](https://github.com/ncruces/zenity/wiki/Notification) Behavior on Windows, macOS and other Unixes might differ slightly. diff --git a/entry_test.go b/entry_test.go new file mode 100644 index 0000000..698d312 --- /dev/null +++ b/entry_test.go @@ -0,0 +1,64 @@ +package zenity_test + +import ( + "context" + "errors" + "os" + "testing" + "time" + + "github.com/ncruces/zenity" +) + +func ExampleEntry() { + zenity.Entry("Enter new text:", + zenity.Title("Add a new entry")) + // Output: +} + +func ExamplePassword() { + zenity.Password(zenity.Title("Type your password")) + // Output: +} + +func TestEntryTimeout(t *testing.T) { + ctx, cancel := context.WithTimeout(context.Background(), time.Second/10) + + _, _, err := zenity.Entry("", zenity.Context(ctx)) + if !os.IsTimeout(err) { + t.Error("did not timeout:", err) + } + + cancel() +} + +func TestEntryCancel(t *testing.T) { + ctx, cancel := context.WithCancel(context.Background()) + cancel() + + _, _, err := zenity.Entry("", zenity.Context(ctx)) + if !errors.Is(err, context.Canceled) { + t.Error("was not canceled:", err) + } +} + +func TestPasswordTimeout(t *testing.T) { + ctx, cancel := context.WithTimeout(context.Background(), time.Second/10) + + _, _, _, err := zenity.Password(zenity.Context(ctx)) + if !os.IsTimeout(err) { + t.Error("did not timeout:", err) + } + + cancel() +} + +func TestPasswordCancel(t *testing.T) { + ctx, cancel := context.WithCancel(context.Background()) + cancel() + + _, _, _, err := zenity.Password(zenity.Context(ctx)) + if !errors.Is(err, context.Canceled) { + t.Error("was not canceled:", err) + } +} diff --git a/entry_windows.go b/entry_windows.go index 8927f4a..8a1c21f 100644 --- a/entry_windows.go +++ b/entry_windows.go @@ -245,6 +245,8 @@ func editBox(title, text string, opts options) (out string, ok bool, err error) var okBtn, cancelBtn, extraBtn uintptr defWindowProc := defWindowProc.Addr() + defer setup()() + font := getFont() defer font.Delete() @@ -300,7 +302,9 @@ func editBox(title, text string, opts options) (out string, ok bool, err error) return 0 } - defer setup()() + if opts.ctx != nil && opts.ctx.Err() != nil { + return "", false, opts.ctx.Err() + } instance, _, err := getModuleHandle.Call(0) if instance == 0 { @@ -355,9 +359,26 @@ func editBox(title, text string, opts options) (out string, ok bool, err error) showWindow.Call(wnd, 1 /* SW_SHOWNORMAL */, 0) sendMessage.Call(editCtl, 0xb1 /* EM_SETSEL */, 0, intptr(-1)) - err = nil + if opts.ctx != nil { + wait := make(chan struct{}) + defer close(wait) + go func() { + select { + case <-opts.ctx.Done(): + sendMessage.Call(wnd, 0x0112 /* WM_SYSCOMMAND */, 0xf060 /* SC_CLOSE */, 0) + case <-wait: + } + }() + } + + // set default values + out, ok, err = "", false, nil + if err := messageLoop(wnd); err != nil { return "", false, err } + if opts.ctx != nil && opts.ctx.Err() != nil { + return "", false, opts.ctx.Err() + } return out, ok, err } diff --git a/go.mod b/go.mod index b55b5a3..7df380c 100644 --- a/go.mod +++ b/go.mod @@ -3,8 +3,8 @@ module github.com/ncruces/zenity go 1.15 require ( - github.com/dchest/jsmin v0.0.0-20160823214000-faeced883947 // indirect - github.com/josephspurrier/goversioninfo v1.2.0 // indirect + github.com/dchest/jsmin v0.0.0-20160823214000-faeced883947 + github.com/josephspurrier/goversioninfo v1.2.0 github.com/stretchr/testify v1.7.0 // indirect go.uber.org/goleak v1.1.10 // test golang.org/x/image v0.0.0-20210220032944-ac19c3e999fb diff --git a/go.sum b/go.sum index d70573e..f452467 100644 --- a/go.sum +++ b/go.sum @@ -13,28 +13,20 @@ github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= -github.com/stretchr/objx v0.1.0 h1:4G4v2dO3VZwixGIRoQ5Lfboy6nUhCyYzaqnIAPPhYs4= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= -github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0= github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY= github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= -go.uber.org/goleak v1.0.0 h1:qsup4IcBdlmsnGfqyLl4Ntn3C2XCCuKAE7DwHpScyUo= -go.uber.org/goleak v1.0.0/go.mod h1:8a7PlsEVH3e/a/GLqe5IIrQx6GzcnRmZEufDUTk4A7A= go.uber.org/goleak v1.1.10 h1:z+mqJhf6ss6BSfSM671tgKyZBFPTTJM+HLxnhPC3wu0= go.uber.org/goleak v1.1.10/go.mod h1:8a7PlsEVH3e/a/GLqe5IIrQx6GzcnRmZEufDUTk4A7A= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= -golang.org/x/image v0.0.0-20201208152932-35266b937fa6 h1:nfeHNc1nAqecKCy2FCy4HY+soOOe5sDLJ/gZLbx6GYI= -golang.org/x/image v0.0.0-20201208152932-35266b937fa6/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0= golang.org/x/image v0.0.0-20210220032944-ac19c3e999fb h1:fqpd0EBDzlHRCjiphRR5Zo/RSWWQlWv34418dnEixWk= golang.org/x/image v0.0.0-20210220032944-ac19c3e999fb/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0= golang.org/x/lint v0.0.0-20190930215403-16217165b5de/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc= -golang.org/x/lint v0.0.0-20191125180803-fdd1cda4f05f h1:J5lckAjkw6qYlOZNj90mLYNTEKDvWeuc1yieZ8qUzUE= -golang.org/x/lint v0.0.0-20191125180803-fdd1cda4f05f/go.mod h1:5qLYkcX4OjUUV8bRuDixDT3tpyyb+LUpUlRWLxfhWrs= golang.org/x/lint v0.0.0-20201208152925-83fdc39ff7b5 h1:2M3HP5CCK1Si9FQhwnzYhXdG6DXeebvUHFpre8QvbyI= golang.org/x/lint v0.0.0-20201208152925-83fdc39ff7b5/go.mod h1:3xt1FjdF8hUf6vQPIChWIBhFzV8gjjsPE/fR3IyQdNY= golang.org/x/mod v0.1.1-0.20191105210325-c90efee705ee/go.mod h1:QqPTAvyqsEbceGzBzNggFXnrqF1CaUcvgkdR5Ot7KZg= @@ -57,7 +49,6 @@ golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGm golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs= golang.org/x/tools v0.0.0-20191108193012-7d206e10da11/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= -golang.org/x/tools v0.0.0-20191125144606-a911d9008d1f/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= golang.org/x/tools v0.0.0-20200130002326-2f3ba24bd6e7/go.mod h1:TB2adYChydJhpapKDTa4BR/hXlZSLoq2Wpct/0txZ28= golang.org/x/tools v0.1.0 h1:po9/4sTYwZU9lPhi1tOrb4hCv3qrhiQ77LZfGa2OjwY= golang.org/x/tools v0.1.0/go.mod h1:xkSsbof2nBLbhDlRMhhhyNLN/zl3eTqcnHD5viDpcZ0= diff --git a/internal/zenutil/osa_generator.go b/internal/zenutil/osa_generator.go index 4245bd2..6e3f2e4 100644 --- a/internal/zenutil/osa_generator.go +++ b/internal/zenutil/osa_generator.go @@ -1,4 +1,4 @@ -// +build ignore +// +build tools package main diff --git a/internal/zenutil/tools.go b/internal/zenutil/tools.go new file mode 100644 index 0000000..d55a895 --- /dev/null +++ b/internal/zenutil/tools.go @@ -0,0 +1,7 @@ +// +build tools + +package tools + +import ( + _ "github.com/josephspurrier/goversioninfo" +)