From 0b2f5d218d20365a4f779742ae89d690cefae9da Mon Sep 17 00:00:00 2001 From: Nuno Cruces Date: Tue, 17 May 2022 15:36:00 +0100 Subject: [PATCH] Fix default date on macOS. --- cmd/zenity/main.go | 6 +++--- date_test.go | 6 +++--- internal/zenutil/osa_generated.go | 2 ++ internal/zenutil/osascripts/date.gojs | 2 ++ msg.go | 5 ----- zenity.go | 7 ++++++- zenity_test.go | 15 ++++++++++----- 7 files changed, 26 insertions(+), 17 deletions(-) diff --git a/cmd/zenity/main.go b/cmd/zenity/main.go index 30bb5d3..2627c6c 100644 --- a/cmd/zenity/main.go +++ b/cmd/zenity/main.go @@ -397,6 +397,9 @@ func loadFlags() []zenity.Option { if extraButton != unspecified { opts = append(opts, zenity.ExtraButton(extraButton)) } + if defaultCancel { + opts = append(opts, zenity.DefaultCancel()) + } var ico zenity.DialogIcon switch icon { @@ -429,9 +432,6 @@ func loadFlags() []zenity.Option { if ellipsize { opts = append(opts, zenity.Ellipsize()) } - if defaultCancel { - opts = append(opts, zenity.DefaultCancel()) - } // Entry options diff --git a/date_test.go b/date_test.go index bad7c0b..0071920 100644 --- a/date_test.go +++ b/date_test.go @@ -55,9 +55,9 @@ func TestCalendar_script(t *testing.T) { err error }{ {name: "Cancel", call: "cancel", err: zenity.ErrCanceled}, - {name: "Black", call: "choose today", want: time.Now()}, - {name: "Rebecca", call: "press OK", want: time.Date(2000, 1, 1, 0, 0, 0, 0, time.Local), - opts: []zenity.Option{zenity.DefaultDate(2000, 1, 1)}}, + {name: "Today", call: "choose today", want: time.Now()}, + {name: "Default", call: "press OK", want: time.Date(2006, 1, 1, 0, 0, 0, 0, time.Local), + opts: []zenity.Option{zenity.DefaultDate(2006, 1, 1)}}, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { diff --git a/internal/zenutil/osa_generated.go b/internal/zenutil/osa_generated.go index 91d4389..598bd94 100644 --- a/internal/zenutil/osa_generated.go +++ b/internal/zenutil/osa_generated.go @@ -33,6 +33,8 @@ date.setDatePickerElements($.NSDatePickerElementFlagYearMonthDay) date.setFrameSize(date.fittingSize) {{- if .Date}} date.setDateValue($.NSDate.dateWithTimeIntervalSince1970({{.Date}})) +{{- else}} +date.setDateValue($.NSDate.date) {{- end}} var alert=$.NSAlert.alloc.init alert.setAccessoryView(date) diff --git a/internal/zenutil/osascripts/date.gojs b/internal/zenutil/osascripts/date.gojs index e2616bb..de791c0 100644 --- a/internal/zenutil/osascripts/date.gojs +++ b/internal/zenutil/osascripts/date.gojs @@ -8,6 +8,8 @@ date.setDatePickerElements($.NSDatePickerElementFlagYearMonthDay) date.setFrameSize(date.fittingSize) {{- if .Date}} date.setDateValue($.NSDate.dateWithTimeIntervalSince1970({{.Date}})) +{{- else}} + date.setDateValue($.NSDate.date) {{- end}} var alert = $.NSAlert.alloc.init diff --git a/msg.go b/msg.go index 7b1a830..592e217 100644 --- a/msg.go +++ b/msg.go @@ -58,8 +58,3 @@ func NoWrap() Option { func Ellipsize() Option { return funcOption(func(o *options) { o.ellipsize = true }) } - -// DefaultCancel returns an Option to give the Cancel button focus by default. -func DefaultCancel() Option { - return funcOption(func(o *options) { o.defaultCancel = true }) -} diff --git a/zenity.go b/zenity.go index f737c62..9d40f43 100644 --- a/zenity.go +++ b/zenity.go @@ -130,6 +130,11 @@ func ExtraButton(extra string) Option { return funcOption(func(o *options) { o.extraButton = &extra }) } +// DefaultCancel returns an Option to give the Cancel button focus by default. +func DefaultCancel() Option { + return funcOption(func(o *options) { o.defaultCancel = true }) +} + // DialogIcon is an Option that sets the dialog icon. type DialogIcon int @@ -154,7 +159,7 @@ const ( // Tip: use DialogIcon directly. func Icon(icon DialogIcon) Option { return icon } -// Icon returns an Option to set a custom dialog icon, loaded from a file. +// CustomIcon returns an Option to set a custom dialog icon, loaded from a file. func CustomIcon(path string) Option { return funcOption(func(o *options) { o.icon = unspecifiedIcon diff --git a/zenity_test.go b/zenity_test.go index b35a78c..9951a75 100644 --- a/zenity_test.go +++ b/zenity_test.go @@ -5,9 +5,11 @@ import ( "image/color" "reflect" "testing" + "time" ) func Test_applyOptions(t *testing.T) { + date := time.Date(2006, 1, 1, 0, 0, 0, 0, time.Local) tests := []struct { name string args Option @@ -20,8 +22,8 @@ func Test_applyOptions(t *testing.T) { {name: "OKLabel", args: OKLabel("OK"), want: options{okLabel: stringPtr("OK")}}, {name: "CancelLabel", args: CancelLabel("Cancel"), want: options{cancelLabel: stringPtr("Cancel")}}, {name: "ExtraButton", args: ExtraButton("Extra"), want: options{extraButton: stringPtr("Extra")}}, - {name: "Icon", args: Icon(ErrorIcon), want: options{icon: ErrorIcon}}, {name: "DefaultCancel", args: DefaultCancel(), want: options{defaultCancel: true}}, + {name: "Icon", args: Icon(ErrorIcon), want: options{icon: ErrorIcon}}, // Message options {name: "NoWrap", args: NoWrap(), want: options{noWrap: true}}, @@ -36,6 +38,9 @@ func Test_applyOptions(t *testing.T) { {name: "DisallowEmpty", args: DisallowEmpty(), want: options{disallowEmpty: true}}, {name: "DefaultItems", args: DefaultItems("a", "b"), want: options{defaultItems: []string{"a", "b"}}}, + // Calendar options + {name: "DefaultDate", args: DefaultDate(2006, time.January, 1), want: options{time: &date}}, + // File selection options {name: "Directory", args: Directory(), want: options{directory: true}}, {name: "ConfirmOverwrite", args: ConfirmOverwrite(), want: options{confirmOverwrite: true}}, @@ -49,16 +54,16 @@ func Test_applyOptions(t *testing.T) { fileFilters: FileFilters{{"Go files", []string{"*.go"}}}, }}, + // Color selection options + {name: "Color", args: Color(color.Black), want: options{color: color.Black}}, + {name: "ShowPalette", args: ShowPalette(), want: options{showPalette: true}}, + // Progress indication options {name: "MaxValue", args: MaxValue(100), want: options{maxValue: 100}}, {name: "Pulsate", args: Pulsate(), want: options{maxValue: -1}}, {name: "NoCancel", args: NoCancel(), want: options{noCancel: true}}, {name: "TimeRemaining", args: TimeRemaining(), want: options{timeRemaining: true}}, - // Color selection options - {name: "Color", args: Color(color.Black), want: options{color: color.Black}}, - {name: "ShowPalette", args: ShowPalette(), want: options{showPalette: true}}, - // Context for timeout {name: "Context", args: Context(context.TODO()), want: options{ctx: context.TODO()}}, }