Attach check types.

This commit is contained in:
Nuno Cruces 2022-06-04 13:09:58 +01:00
parent b902eebb1d
commit 52eb3aeda1
6 changed files with 56 additions and 12 deletions

View file

@ -15,7 +15,6 @@ import (
"path/filepath" "path/filepath"
"runtime" "runtime"
"runtime/debug" "runtime/debug"
"strconv"
"strings" "strings"
"syscall" "syscall"
"time" "time"
@ -448,11 +447,7 @@ func loadFlags() []zenity.Option {
} }
if attach != "" { if attach != "" {
if i, err := strconv.ParseUint(attach, 0, 64); err != nil { opts = append(opts, zenity.Attach(zenutil.ParseWindowId(attach)))
opts = append(opts, zenity.Attach(attach))
} else {
opts = append(opts, zenity.Attach(i))
}
} }
// Message options // Message options

View file

@ -1,7 +1,16 @@
package zenutil package zenutil
import "strconv"
// These are internal. // These are internal.
var ( var (
Separator = "\x00" Separator = "\x00"
LineBreak = "\n" LineBreak = "\n"
) )
func ParseWindowId(id string) any {
if pid, err := strconv.ParseUint(id, 0, 64); err == nil {
return int(pid)
}
return id
}

View file

@ -2,8 +2,15 @@
package zenutil package zenutil
import "strconv"
// These are internal. // These are internal.
var ( var (
Separator = "\x1e" Separator = "\x1e"
LineBreak = "\n" LineBreak = "\n"
) )
func ParseWindowId(id string) int {
wid, _ := strconv.ParseUint(id, 0, 64)
return int(wid)
}

View file

@ -1,7 +1,14 @@
package zenutil package zenutil
import "strconv"
// These are internal. // These are internal.
var ( var (
Separator string Separator string
LineBreak = "\r\n" LineBreak = "\r\n"
) )
func ParseWindowId(id string) uintptr {
hwnd, _ := strconv.ParseUint(id, 0, 64)
return uintptr(hwnd)
}

View file

@ -13,6 +13,8 @@ package zenity
import ( import (
"context" "context"
"image/color" "image/color"
"reflect"
"runtime"
"time" "time"
"github.com/ncruces/zenity/internal/zenutil" "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). // The string can be a GTK icon name (Unix), or a path (Windows and macOS).
func Icon(icon any) Option { func Icon(icon any) Option {
switch icon.(type) { switch icon.(type) {
case string: case DialogIcon, string:
case DialogIcon:
default: default:
panic("interface conversion: expected string or DialogIcon") panic("interface conversion: expected string or DialogIcon")
} }
@ -176,8 +177,7 @@ func Icon(icon any) Option {
// WindowIcon accepts a DialogIcon, or a string path. // WindowIcon accepts a DialogIcon, or a string path.
func WindowIcon(icon any) Option { func WindowIcon(icon any) Option {
switch icon.(type) { switch icon.(type) {
case string: case DialogIcon, string:
case DialogIcon:
default: default:
panic("interface conversion: expected string or DialogIcon") 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 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 { 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 }) return funcOption(func(o *options) { o.attach = id })
} }

View file

@ -6,6 +6,8 @@ import (
"reflect" "reflect"
"testing" "testing"
"time" "time"
"github.com/ncruces/zenity/internal/zenutil"
) )
func Test_applyOptions(t *testing.T) { 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: "WindowIcon", args: WindowIcon("error"), want: options{windowIcon: "error"}},
{name: "Icon", args: Icon(ErrorIcon), want: options{icon: ErrorIcon}}, {name: "Icon", args: Icon(ErrorIcon), want: options{icon: ErrorIcon}},
{name: "Icon", args: Icon("error"), want: options{icon: "error"}}, {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: "Modal", args: Modal(), want: options{modal: true}},
{name: "Attach", args: Attach(zenutil.ParseWindowId("12345")), want: options{attach: zenutil.ParseWindowId("12345")}},
// Message options // Message options
{name: "NoWrap", args: NoWrap(), want: options{noWrap: true}}, {name: "NoWrap", args: NoWrap(), want: options{noWrap: true}},