Skip to content

Refactor NewWindow by using struct #55

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 22, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion examples/chat/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ func main() {
thrust.InitLogger()
thrust.Start()

thrustWindow := thrust.NewWindow(fmt.Sprintf("http://127.0.0.1:%d", *port), nil)
thrustWindow := thrust.NewWindow(thrust.WindowOptions{
RootUrl: fmt.Sprintf("http://127.0.0.1:%d", *port),
})
thrustWindow.Show()
thrustWindow.Focus()
// BLOCKING - Dont run before youve excuted all commands you want first.
Expand Down
4 changes: 3 additions & 1 deletion examples/chat/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,9 @@ func main() {

thrust.InitLogger()
thrust.Start()
thrustWindow := thrust.NewWindow(fmt.Sprintf("http://127.0.0.1:%d", *port), nil)
thrustWindow := thrust.NewWindow(thrust.WindowOptions{
RootUrl: fmt.Sprintf("http://127.0.0.1:%d", *port),
})
thrustWindow.Show()
thrustWindow.Focus()

Expand Down
4 changes: 3 additions & 1 deletion examples/jankybrowser/browser.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ func main() {
thrust.InitLogger()
thrust.Start()

thrustWindow := thrust.NewWindow(fmt.Sprintf("http://127.0.0.1:%d", *port), nil)
thrustWindow := thrust.NewWindow(thrust.WindowOptions{
RootUrl: fmt.Sprintf("http://127.0.0.1:%d", *port),
})
thrustWindow.Show()
thrustWindow.Focus()

Expand Down
57 changes: 40 additions & 17 deletions lib/bindings/window/window.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,15 @@ type Window struct {
SendChannel *connection.In `json:"-"`
}

type Options struct {
RootUrl string
Size SizeHW
Title string
IconPath string
HasFrame bool
Session *session.Session
}

func checkUrl(s string) (string, error) {
u, err := url.Parse(s)
if err != nil {
Expand All @@ -47,39 +56,40 @@ func checkUrl(s string) (string, error) {
return u.String(), err
}

func NewWindow(s string, sess *session.Session) *Window {
u, _ := checkUrl(s)

func NewWindow(options Options) *Window {
w := Window{}
w.Url = u
if len(w.Url) == 0 {
w.Url = "http://google.com"
}
w.setOptions(options)
_, sendChannel := connection.GetCommunicationChannels()

size := options.Size
if options.Size == (SizeHW{}) {
size = SizeHW{
Width: 1024,
Height: 768,
}
}

windowCreate := Command{
Action: "create",
ObjectType: "window",
Args: CommandArguments{
RootUrl: w.Url,
Title: spawn.ApplicationName,
Size: SizeHW{
Width: 1024,
Height: 768,
},
RootUrl: w.Url,
Title: spawn.ApplicationName,
Size: size,
HasFrame: !options.HasFrame,
},
}
dispatcher.RegisterHandler(w.DispatchResponse)
if sess == nil {
if options.Session == nil {
w.SetSendChannel(sendChannel)
w.WaitingResponses = append(w.WaitingResponses, &windowCreate)
w.Send(&windowCreate)
} else {
go func() {
for {
if sess.TargetID != 0 {
fmt.Println("sess", sess.TargetID)
windowCreate.Args.SessionID = sess.TargetID
if options.Session.TargetID != 0 {
fmt.Println("sess", options.Session.TargetID)
windowCreate.Args.SessionID = options.Session.TargetID
w.SetSendChannel(sendChannel)
w.WaitingResponses = append(w.WaitingResponses, &windowCreate)
w.Send(&windowCreate)
Expand All @@ -92,6 +102,19 @@ func NewWindow(s string, sess *session.Session) *Window {
return &w
}

func (w *Window) setOptions(options Options) {
u, _ := checkUrl(options.RootUrl)
w.Url = u
if len(w.Url) == 0 {
w.Url = "http://google.com"
}

w.Title = options.Title
if len(w.Title) == 0 {
w.Title = spawn.ApplicationName
}
}

func (w *Window) SetSendChannel(sendChannel *connection.In) {
w.SendChannel = sendChannel
}
Expand Down
1 change: 1 addition & 0 deletions lib/commands/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ type CommandArguments struct {
Fullscreen bool `json:"fullscreen"`
Kiosk bool `json:"kiosk"`
Focus bool `json:"focus"`
HasFrame bool `json:"has_frame"`
Path string `json:"path,omitempty"`
Message RemoteMessage `json:"message,omitempty"`
}
Expand Down
6 changes: 4 additions & 2 deletions thrust/thrust.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,11 @@ Begin Generic Access and Binding Management Section.
Bindings
*/

type WindowOptions window.Options

/* NewWindow creates a new Window Binding */
func NewWindow(url string, sess *session.Session) *window.Window {
return window.NewWindow(url, sess)
func NewWindow(options WindowOptions) *window.Window {
return window.NewWindow(window.Options(options))
}

/* NewSession creates a new Session Binding */
Expand Down
5 changes: 4 additions & 1 deletion tutorials/advanced_session/advanced_session.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,10 @@ func main() {
Modified basic_window, where we provide, a session argument
to NewWindow.
*/
thrustWindow := thrust.NewWindow("http://breach.cc/", mysession)
thrustWindow := thrust.NewWindow(thrust.WindowOptions{
RootUrl: "http://breach.cc/",
Session: mysession,
})
thrustWindow.Show()
thrustWindow.Maximize()
thrustWindow.Focus()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ func main() {
thrust.SetProvisioner(tutorial.NewTutorialProvisioner())
// thrust.Start() must always come before any bindings are created.
thrust.Start()
thrustWindow := thrust.NewWindow("http://breach.cc/", nil)
thrustWindow := thrust.NewWindow(thrust.WindowOptions{
RootUrl: "http://breach.cc/",
})
thrustWindow.Show()
thrustWindow.Maximize()
thrustWindow.Focus()
Expand Down
4 changes: 3 additions & 1 deletion tutorials/basic_menu/basic_menu.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ func main() {
thrust.SetProvisioner(tutorial.NewTutorialProvisioner())
// thrust.Start() must always come before any bindings are created.
thrust.Start()
thrustWindow := thrust.NewWindow("http://breach.cc/", nil)
thrustWindow := thrust.NewWindow(thrust.WindowOptions{
RootUrl: "http://breach.cc/",
})
thrustWindow.Show()
thrustWindow.Maximize()
thrustWindow.Focus()
Expand Down
4 changes: 3 additions & 1 deletion tutorials/basic_menu_events/basic_menu_events.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ func main() {
thrust.SetProvisioner(tutorial.NewTutorialProvisioner())
// thrust.Start() must always come before any bindings are created.
thrust.Start()
thrustWindow := thrust.NewWindow("http://breach.cc/", nil)
thrustWindow := thrust.NewWindow(thrust.WindowOptions{
RootUrl: "http://breach.cc/",
})
thrustWindow.Show()
thrustWindow.Maximize()
thrustWindow.Focus()
Expand Down
8 changes: 6 additions & 2 deletions tutorials/basic_multiple_windows/basic_multiple_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,16 @@ func main() {
// thrust.Start() must always come before any bindings are created.
thrust.Start()

thrustWindow := thrust.NewWindow("http://breach.cc/", nil)
thrustWindow := thrust.NewWindow(thrust.WindowOptions{
RootUrl: "http://breach.cc/",
})
thrustWindow.Show()
thrustWindow.Maximize()
thrustWindow.Focus()

thrustWindow2 := thrust.NewWindow("http://google.com/", nil)
thrustWindow2 := thrust.NewWindow(thrust.WindowOptions{
RootUrl: "http://google.com/",
})
thrustWindow2.Show()
thrustWindow2.Focus()

Expand Down
4 changes: 3 additions & 1 deletion tutorials/basic_remote_messaging/basic_remote_messaging.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ func main() {
// thrust.Start() must always come before any bindings are created.
thrust.Start()

thrustWindow := thrust.NewWindow("http://localhost:8080/", nil)
thrustWindow := thrust.NewWindow(thrust.WindowOptions{
RootUrl: "http://localhost:8080/",
})
thrustWindow.Show()
thrustWindow.Maximize()
thrustWindow.Focus()
Expand Down
5 changes: 4 additions & 1 deletion tutorials/basic_session/basic_session.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@ func main() {
Modified basic_window, where we provide, a session argument
to NewWindow.
*/
thrustWindow := thrust.NewWindow("http://breach.cc/", mysession)
thrustWindow := thrust.NewWindow(thrust.WindowOptions{
RootUrl: "http://breach.cc/",
Session: mysession,
})
thrustWindow.Show()
thrustWindow.Maximize()
thrustWindow.Focus()
Expand Down
5 changes: 4 additions & 1 deletion tutorials/basic_webserver_app/basic_webserver_app.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@ func main() {

mysession := thrust.NewSession(false, false, "cache")

thrustWindow := thrust.NewWindow("http://localhost:8080/", mysession)
thrustWindow := thrust.NewWindow(thrust.WindowOptions{
RootUrl: "http://localhost:8080/",
Session: mysession,
})
thrustWindow.Show()
thrustWindow.Maximize()
thrustWindow.Focus()
Expand Down
4 changes: 3 additions & 1 deletion tutorials/basic_window/basic_window.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ func main() {
// thrust.Start() must always come before any bindings are created.
thrust.Start()

thrustWindow := thrust.NewWindow("http://breach.cc/", nil)
thrustWindow := thrust.NewWindow(thrust.WindowOptions{
RootUrl: "http://breach.cc/",
})
thrustWindow.Show()
thrustWindow.Maximize()
thrustWindow.Focus()
Expand Down
4 changes: 3 additions & 1 deletion tutorials/basic_window_events/basic_window_events.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ func main() {
// thrust.Start() must always come before any bindings are created.
thrust.Start()

thrustWindow := thrust.NewWindow("http://breach.cc/", nil)
thrustWindow := thrust.NewWindow(thrust.WindowOptions{
RootUrl: "http://breach.cc/",
})
thrustWindow.Show()

/*
Expand Down