Skip to content

Commit 8936ade

Browse files
committed
Refactor NewWindow by using struct
1 parent 99246f6 commit 8936ade

File tree

3 files changed

+45
-19
lines changed

3 files changed

+45
-19
lines changed

lib/bindings/window/window.go

Lines changed: 40 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,15 @@ type Window struct {
2929
SendChannel *connection.In `json:"-"`
3030
}
3131

32+
type Options struct {
33+
RootUrl string
34+
Size SizeHW
35+
Title string
36+
IconPath string
37+
HasFrame bool
38+
Session *session.Session
39+
}
40+
3241
func checkUrl(s string) (string, error) {
3342
u, err := url.Parse(s)
3443
if err != nil {
@@ -47,39 +56,40 @@ func checkUrl(s string) (string, error) {
4756
return u.String(), err
4857
}
4958

50-
func NewWindow(s string, sess *session.Session) *Window {
51-
u, _ := checkUrl(s)
52-
59+
func NewWindow(options Options) *Window {
5360
w := Window{}
54-
w.Url = u
55-
if len(w.Url) == 0 {
56-
w.Url = "http://google.com"
57-
}
61+
w.setOptions(options)
5862
_, sendChannel := connection.GetCommunicationChannels()
5963

64+
size := options.Size
65+
if options.Size == (SizeHW{}) {
66+
size = SizeHW{
67+
Width: 1024,
68+
Height: 768,
69+
}
70+
}
71+
6072
windowCreate := Command{
6173
Action: "create",
6274
ObjectType: "window",
6375
Args: CommandArguments{
64-
RootUrl: w.Url,
65-
Title: spawn.ApplicationName,
66-
Size: SizeHW{
67-
Width: 1024,
68-
Height: 768,
69-
},
76+
RootUrl: w.Url,
77+
Title: spawn.ApplicationName,
78+
Size: size,
79+
HasFrame: options.HasFrame,
7080
},
7181
}
7282
dispatcher.RegisterHandler(w.DispatchResponse)
73-
if sess == nil {
83+
if options.Session == nil {
7484
w.SetSendChannel(sendChannel)
7585
w.WaitingResponses = append(w.WaitingResponses, &windowCreate)
7686
w.Send(&windowCreate)
7787
} else {
7888
go func() {
7989
for {
80-
if sess.TargetID != 0 {
81-
fmt.Println("sess", sess.TargetID)
82-
windowCreate.Args.SessionID = sess.TargetID
90+
if options.Session.TargetID != 0 {
91+
fmt.Println("sess", options.Session.TargetID)
92+
windowCreate.Args.SessionID = options.Session.TargetID
8393
w.SetSendChannel(sendChannel)
8494
w.WaitingResponses = append(w.WaitingResponses, &windowCreate)
8595
w.Send(&windowCreate)
@@ -92,6 +102,19 @@ func NewWindow(s string, sess *session.Session) *Window {
92102
return &w
93103
}
94104

105+
func (w *Window) setOptions(options Options) {
106+
u, _ := checkUrl(options.RootUrl)
107+
w.Url = u
108+
if len(w.Url) == 0 {
109+
w.Url = "http://google.com"
110+
}
111+
112+
w.Title = options.Title
113+
if len(w.Title) == 0 {
114+
w.Title = spawn.ApplicationName
115+
}
116+
}
117+
95118
func (w *Window) SetSendChannel(sendChannel *connection.In) {
96119
w.SendChannel = sendChannel
97120
}

lib/commands/commands.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ type CommandArguments struct {
4646
Fullscreen bool `json:"fullscreen"`
4747
Kiosk bool `json:"kiosk"`
4848
Focus bool `json:"focus"`
49+
HasFrame bool `json:"has_frame"`
4950
Path string `json:"path,omitempty"`
5051
Message RemoteMessage `json:"message,omitempty"`
5152
}

thrust/thrust.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,11 @@ Begin Generic Access and Binding Management Section.
2121
Bindings
2222
*/
2323

24+
type WindowOptions window.Options
25+
2426
/* NewWindow creates a new Window Binding */
25-
func NewWindow(url string, sess *session.Session) *window.Window {
26-
return window.NewWindow(url, sess)
27+
func NewWindow(options WindowOptions) *window.Window {
28+
return window.NewWindow(window.Options(options))
2729
}
2830

2931
/* NewSession creates a new Session Binding */

0 commit comments

Comments
 (0)