From 52eb3aeda19e5ea1046b121f4112c21331890564 Mon Sep 17 00:00:00 2001 From: Nuno Cruces Date: Sat, 4 Jun 2022 13:09:58 +0100 Subject: [PATCH] Attach check types. --- cmd/zenity/main.go | 7 +------ internal/zenutil/env_darwin.go | 9 +++++++++ internal/zenutil/env_unix.go | 7 +++++++ internal/zenutil/env_windows.go | 7 +++++++ zenity.go | 33 +++++++++++++++++++++++++++++---- zenity_test.go | 5 +++-- 6 files changed, 56 insertions(+), 12 deletions(-) diff --git a/cmd/zenity/main.go b/cmd/zenity/main.go index 774343c..fa6feaf 100644 --- a/cmd/zenity/main.go +++ b/cmd/zenity/main.go @@ -15,7 +15,6 @@ import ( "path/filepath" "runtime" "runtime/debug" - "strconv" "strings" "syscall" "time" @@ -448,11 +447,7 @@ func loadFlags() []zenity.Option { } if attach != "" { - if i, err := strconv.ParseUint(attach, 0, 64); err != nil { - opts = append(opts, zenity.Attach(attach)) - } else { - opts = append(opts, zenity.Attach(i)) - } + opts = append(opts, zenity.Attach(zenutil.ParseWindowId(attach))) } // Message options diff --git a/internal/zenutil/env_darwin.go b/internal/zenutil/env_darwin.go index 39b99f8..5b4a557 100644 --- a/internal/zenutil/env_darwin.go +++ b/internal/zenutil/env_darwin.go @@ -1,7 +1,16 @@ package zenutil +import "strconv" + // These are internal. var ( Separator = "\x00" LineBreak = "\n" ) + +func ParseWindowId(id string) any { + if pid, err := strconv.ParseUint(id, 0, 64); err == nil { + return int(pid) + } + return id +} diff --git a/internal/zenutil/env_unix.go b/internal/zenutil/env_unix.go index ad4003b..79bbd6d 100644 --- a/internal/zenutil/env_unix.go +++ b/internal/zenutil/env_unix.go @@ -2,8 +2,15 @@ package zenutil +import "strconv" + // These are internal. var ( Separator = "\x1e" LineBreak = "\n" ) + +func ParseWindowId(id string) int { + wid, _ := strconv.ParseUint(id, 0, 64) + return int(wid) +} diff --git a/internal/zenutil/env_windows.go b/internal/zenutil/env_windows.go index e8e1894..168e6c3 100644 --- a/internal/zenutil/env_windows.go +++ b/internal/zenutil/env_windows.go @@ -1,7 +1,14 @@ package zenutil +import "strconv" + // These are internal. var ( Separator string LineBreak = "\r\n" ) + +func ParseWindowId(id string) uintptr { + hwnd, _ := strconv.ParseUint(id, 0, 64) + return uintptr(hwnd) +} diff --git a/zenity.go b/zenity.go index 7b4e6bf..b37a095 100644 --- a/zenity.go +++ b/zenity.go @@ -13,6 +13,8 @@ package zenity import ( "context" "image/color" + "reflect" + "runtime" "time" "github.com/ncruces/zenity/internal/zenutil" @@ -163,8 +165,7 @@ const ( // The string can be a GTK icon name (Unix), or a path (Windows and macOS). func Icon(icon any) Option { switch icon.(type) { - case string: - case DialogIcon: + case DialogIcon, string: default: panic("interface conversion: expected string or DialogIcon") } @@ -176,8 +177,7 @@ func Icon(icon any) Option { // WindowIcon accepts a DialogIcon, or a string path. func WindowIcon(icon any) Option { switch icon.(type) { - case string: - case DialogIcon: + case DialogIcon, string: default: panic("interface conversion: expected string or DialogIcon") } @@ -192,7 +192,32 @@ func CustomIcon(path string) Option { } // Attach returns an Option to set the parent window to attach to. +// +// Attach accepts: +// - a window id (int) on Unix +// - a window handle (~uintptr) on Windows +// - an application name (string) or process id (int) on macOS func Attach(id any) Option { + switch runtime.GOOS { + case "windows": + if v := reflect.ValueOf(id); v.Kind() == reflect.Uintptr { + id = uintptr(v.Uint()) + } else { + panic("interface conversion: expected uintptr") + } + + case "darwin": + switch id.(type) { + case int, string: + default: + panic("interface conversion: expected int or string") + } + + default: + if _, ok := id.(int); !ok { + panic("interface conversion: expected int") + } + } return funcOption(func(o *options) { o.attach = id }) } diff --git a/zenity_test.go b/zenity_test.go index 629e27d..5b57d47 100644 --- a/zenity_test.go +++ b/zenity_test.go @@ -6,6 +6,8 @@ import ( "reflect" "testing" "time" + + "github.com/ncruces/zenity/internal/zenutil" ) func Test_applyOptions(t *testing.T) { @@ -27,9 +29,8 @@ func Test_applyOptions(t *testing.T) { {name: "WindowIcon", args: WindowIcon("error"), want: options{windowIcon: "error"}}, {name: "Icon", args: Icon(ErrorIcon), want: options{icon: ErrorIcon}}, {name: "Icon", args: Icon("error"), want: options{icon: "error"}}, - {name: "Attach", args: Attach(12345), want: options{attach: 12345}}, - {name: "Attach", args: Attach("Terminal"), want: options{attach: "Terminal"}}, {name: "Modal", args: Modal(), want: options{modal: true}}, + {name: "Attach", args: Attach(zenutil.ParseWindowId("12345")), want: options{attach: zenutil.ParseWindowId("12345")}}, // Message options {name: "NoWrap", args: NoWrap(), want: options{noWrap: true}},