Skip to content

Commit

Permalink
Added session
Browse files Browse the repository at this point in the history
  • Loading branch information
asticode committed Nov 25, 2017
1 parent 8b98638 commit 4688888
Show file tree
Hide file tree
Showing 9 changed files with 75 additions and 9 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
.DS_Store
Thumbs.db
.idea/
cover.*
cover*
testdata/tmp/*
10 changes: 8 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,13 @@ And that's it!

NOTE: needless to say that the message can be something other than a string. A custom struct for instance!

## Play with the window's session

```go
// Clear window's HTTP cache
w.Session.ClearCache()
```

## Handle several screens/displays

```go
Expand Down Expand Up @@ -376,15 +383,14 @@ In your webserver add one of the following javascript to achieve any kind of dia
- [x] dialogs (open or save file, alerts, ...)
- [x] tray
- [x] bundler
- [x] session
- [ ] loader
- [ ] accelerators (shortcuts)
- [ ] file methods (drag & drop, ...)
- [ ] clipboard methods
- [ ] power monitor events (suspend, resume, ...)
- [ ] notifications (macosx)
- [ ] desktop capturer (audio and video)
- [ ] session methods
- [ ] session events
- [ ] window advanced options (add missing ones)
- [ ] window advanced methods (add missing ones)
- [ ] window advanced events (add missing ones)
Expand Down
2 changes: 1 addition & 1 deletion astilectron.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (

// Versions
const (
VersionAstilectron = "0.12.0"
VersionAstilectron = "0.13.0"
VersionElectron = "1.8.1"
)

Expand Down
1 change: 1 addition & 0 deletions event.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ type Event struct {
MenuItemPosition *int `json:"menuItemPosition,omitempty"`
MenuPopupOptions *MenuPopupOptions `json:"menuPopupOptions,omitempty"`
Message *EventMessage `json:"message,omitempty"`
SessionID string `json:"sessionId,omitempty"`
TrayOptions *TrayOptions `json:"trayOptions,omitempty"`
URL string `json:"url,omitempty"`
WindowID string `json:"windowId,omitempty"`
Expand Down
35 changes: 35 additions & 0 deletions session.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package astilectron

import (
"context"

"github.com/asticode/go-astitools/context"
)

// Session event names
const (
EventNameSessionCmdClearCache = "session.cmd.clear.cache"
EventNameSessionEventClearedCache = "session.event.cleared.cache"
EventNameSessionEventWillDownload = "session.event.will.download"
)

// Session represents a session
// TODO Add missing session methods
// TODO Add missing session events
type Session struct {
*object
}

// newSession creates a new session
func newSession(parentCtx context.Context, c *asticontext.Canceller, d *Dispatcher, i *identifier, w *writer) *Session {
return &Session{object: newObject(parentCtx, c, d, i, w)}
}

// ClearCache clears the Session's HTTP cache
func (s *Session) ClearCache() (err error) {
if err = s.isActionable(); err != nil {
return
}
_, err = synchronousEvent(s.c, s, s.w, Event{Name: EventNameSessionCmdClearCache, TargetID: s.id}, EventNameSessionEventClearedCache)
return
}
22 changes: 22 additions & 0 deletions session_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package astilectron

import (
"context"
"testing"

"github.com/asticode/go-astitools/context"
)

func TestSession_Actions(t *testing.T) {
// Init
var c = asticontext.NewCanceller()
var d = newDispatcher()
go d.start()
var i = newIdentifier()
var wrt = &mockedWriter{}
var w = newWriter(wrt)
var s = newSession(context.Background(), c, d, i, w)

// Actions
testObjectAction(t, func() error { return s.ClearCache() }, s.object, wrt, "{\"name\":\"session.cmd.clear.cache\",\"targetID\":\"1\"}\n", EventNameSessionEventClearedCache)
}
2 changes: 1 addition & 1 deletion sub_menu_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func TestSubMenu_ToEvent(t *testing.T) {
assert.NoError(t, err)
s = newSubMenu(nil, w.id, []*MenuItemOptions{{Label: PtrStr("1")}, {Label: PtrStr("2")}}, asticontext.NewCanceller(), newDispatcher(), i, nil)
e = s.toEvent()
assert.Equal(t, &EventSubMenu{ID: "2", Items: []*EventMenuItem{{ID: "3", Options: &MenuItemOptions{Label: PtrStr("1")}, RootID: "1"}, {ID: "4", Options: &MenuItemOptions{Label: PtrStr("2")}, RootID: "1"}}, RootID: "1"}, e)
assert.Equal(t, &EventSubMenu{ID: "3", Items: []*EventMenuItem{{ID: "4", Options: &MenuItemOptions{Label: PtrStr("1")}, RootID: "1"}, {ID: "5", Options: &MenuItemOptions{Label: PtrStr("2")}, RootID: "1"}}, RootID: "1"}, e)
}

func TestSubMenu_SubMenu(t *testing.T) {
Expand Down
8 changes: 5 additions & 3 deletions window.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,9 @@ var (
// TODO Add missing window events
type Window struct {
*object
o *WindowOptions
url *url.URL
o *WindowOptions
Session *Session
url *url.URL
}

// WindowOptions represents window options
Expand Down Expand Up @@ -151,6 +152,7 @@ func newWindow(o Options, url string, wo *WindowOptions, c *asticontext.Cancelle
o: wo,
object: newObject(nil, c, d, i, wrt),
}
w.Session = newSession(w.ctx, c, d, i, wrt)

// Check app details
if wo.Icon == nil && o.AppIconDefaultPath != "" {
Expand Down Expand Up @@ -221,7 +223,7 @@ func (w *Window) Create() (err error) {
if err = w.isActionable(); err != nil {
return
}
_, err = synchronousEvent(w.c, w, w.w, Event{Name: EventNameWindowCmdCreate, TargetID: w.id, URL: w.url.String(), WindowOptions: w.o}, EventNameWindowEventDidFinishLoad)
_, err = synchronousEvent(w.c, w, w.w, Event{Name: EventNameWindowCmdCreate, SessionID: w.Session.id, TargetID: w.id, URL: w.url.String(), WindowOptions: w.o}, EventNameWindowEventDidFinishLoad)
return
}

Expand Down
2 changes: 1 addition & 1 deletion window_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func TestWindow_Actions(t *testing.T) {
w, err = a.NewWindow("http://test.com", &WindowOptions{Center: PtrBool(true)})
assert.NoError(t, err)
testObjectAction(t, func() error { return w.CloseDevTools() }, w.object, wrt, "{\"name\":\""+EventNameWindowCmdWebContentsCloseDevTools+"\",\"targetID\":\""+w.id+"\"}\n", "")
testObjectAction(t, func() error { return w.Create() }, w.object, wrt, "{\"name\":\""+EventNameWindowCmdCreate+"\",\"targetID\":\""+w.id+"\",\"url\":\"http://test.com\",\"windowOptions\":{\"center\":true}}\n", EventNameWindowEventDidFinishLoad)
testObjectAction(t, func() error { return w.Create() }, w.object, wrt, "{\"name\":\""+EventNameWindowCmdCreate+"\",\"targetID\":\""+w.id+"\",\"sessionId\":\"4\",\"url\":\"http://test.com\",\"windowOptions\":{\"center\":true}}\n", EventNameWindowEventDidFinishLoad)
testObjectAction(t, func() error { return w.Destroy() }, w.object, wrt, "{\"name\":\""+EventNameWindowCmdDestroy+"\",\"targetID\":\""+w.id+"\"}\n", EventNameWindowEventClosed)
assert.True(t, w.IsDestroyed())
w, err = a.NewWindow("http://test.com", &WindowOptions{})
Expand Down

0 comments on commit 4688888

Please sign in to comment.