Skip to content

Commit

Permalink
Added Window.IsShown
Browse files Browse the repository at this point in the history
  • Loading branch information
asticode committed Jun 9, 2018
1 parent cc047f5 commit a7eaf12
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 5 deletions.
20 changes: 18 additions & 2 deletions window.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,16 @@ func newWindow(o Options, url string, wo *WindowOptions, c *asticontext.Cancelle
return true
})

// Show
w.On(EventNameWindowEventHide, func(e Event) (deleteListener bool) {
w.o.Show = PtrBool(false)
return
})
w.On(EventNameWindowEventShow, func(e Event) (deleteListener bool) {
w.o.Show = PtrBool(true)
return
})

// Parse url
if w.url, err = astiurl.Parse(url); err != nil {
err = errors.Wrapf(err, "parsing url %s failed", url)
Expand Down Expand Up @@ -261,11 +271,18 @@ func (w *Window) Hide() (err error) {
if err = w.isActionable(); err != nil {
return
}
w.o.Show = PtrBool(false)
_, err = synchronousEvent(w.c, w, w.w, Event{Name: EventNameWindowCmdHide, TargetID: w.id}, EventNameWindowEventHide)
return
}

// IsShown returns whether the window is shown
func (w *Window) IsShown() bool {
if err := w.isActionable(); err != nil {
return false
}
return w.o.Show != nil && *w.o.Show
}

// Log logs a message in the JS console of the window
func (w *Window) Log(message string) (err error) {
if err = w.isActionable(); err != nil {
Expand Down Expand Up @@ -412,7 +429,6 @@ func (w *Window) Show() (err error) {
if err = w.isActionable(); err != nil {
return
}
w.o.Show = PtrBool(true)
_, err = synchronousEvent(w.c, w, w.w, Event{Name: EventNameWindowCmdShow, TargetID: w.id}, EventNameWindowEventShow)
return
}
Expand Down
6 changes: 3 additions & 3 deletions window_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ func TestWindow_Actions(t *testing.T) {
a.writer = newWriter(wrt)
w, err := a.NewWindow("http://test.com", &WindowOptions{})
assert.NoError(t, err)
assert.Equal(t, false, w.IsShown())

// Actions
testObjectAction(t, func() error { return w.Blur() }, w.object, wrt, "{\"name\":\""+EventNameWindowCmdBlur+"\",\"targetID\":\""+w.id+"\"}\n", EventNameWindowEventBlur)
Expand All @@ -54,7 +55,7 @@ func TestWindow_Actions(t *testing.T) {
assert.NoError(t, err)
testObjectAction(t, func() error { return w.Focus() }, w.object, wrt, "{\"name\":\""+EventNameWindowCmdFocus+"\",\"targetID\":\""+w.id+"\"}\n", EventNameWindowEventFocus)
testObjectAction(t, func() error { return w.Hide() }, w.object, wrt, "{\"name\":\""+EventNameWindowCmdHide+"\",\"targetID\":\""+w.id+"\"}\n", EventNameWindowEventHide)
assert.Equal(t, false, *w.o.Show)
assert.Equal(t, false, w.IsShown())
testObjectAction(t, func() error { return w.Log("message") }, w.object, wrt, "{\"name\":\""+EventNameWindowCmdLog+"\",\"targetID\":\""+w.id+"\",\"message\":\"message\"}\n", "")
testObjectAction(t, func() error { return w.Maximize() }, w.object, wrt, "{\"name\":\""+EventNameWindowCmdMaximize+"\",\"targetID\":\""+w.id+"\"}\n", EventNameWindowEventMaximize)
testObjectAction(t, func() error { return w.Minimize() }, w.object, wrt, "{\"name\":\""+EventNameWindowCmdMinimize+"\",\"targetID\":\""+w.id+"\"}\n", EventNameWindowEventMinimize)
Expand All @@ -64,9 +65,8 @@ func TestWindow_Actions(t *testing.T) {
testObjectAction(t, func() error { return w.MoveInDisplay(d, 3, 4) }, w.object, wrt, "{\"name\":\""+EventNameWindowCmdMove+"\",\"targetID\":\""+w.id+"\",\"windowOptions\":{\"x\":4,\"y\":6}}\n", EventNameWindowEventMove)
testObjectAction(t, func() error { return w.Resize(1, 2) }, w.object, wrt, "{\"name\":\""+EventNameWindowCmdResize+"\",\"targetID\":\""+w.id+"\",\"windowOptions\":{\"height\":2,\"width\":1}}\n", EventNameWindowEventResize)
testObjectAction(t, func() error { return w.Restore() }, w.object, wrt, "{\"name\":\""+EventNameWindowCmdRestore+"\",\"targetID\":\""+w.id+"\"}\n", EventNameWindowEventRestore)
assert.Equal(t, false, *w.o.Show)
testObjectAction(t, func() error { return w.Show() }, w.object, wrt, "{\"name\":\""+EventNameWindowCmdShow+"\",\"targetID\":\""+w.id+"\"}\n", EventNameWindowEventShow)
assert.Equal(t, true, *w.o.Show)
assert.Equal(t, true, w.IsShown())
testObjectAction(t, func() error { return w.Unmaximize() }, w.object, wrt, "{\"name\":\""+EventNameWindowCmdUnmaximize+"\",\"targetID\":\""+w.id+"\"}\n", EventNameWindowEventUnmaximize)
}

Expand Down

0 comments on commit a7eaf12

Please sign in to comment.