Skip to content

Commit

Permalink
Move textSendWindow to bridge package
Browse files Browse the repository at this point in the history
For #134
  • Loading branch information
Jacalz committed Jan 11, 2024
1 parent 5384263 commit ecc67a6
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 74 deletions.
69 changes: 63 additions & 6 deletions internal/transport/bridge/send.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,10 @@ func (s *SendItem) failed() {
type SendData struct {
Client *transport.Client
Window fyne.Window
Canvas fyne.Canvas

items []*SendItem
info sendInfoDialog
items []*SendItem
info sendInfoDialog
textWindow *textSendWindow

deleting atomic.Bool
list *widget.List
Expand Down Expand Up @@ -261,7 +261,7 @@ func (d *SendData) NewSendFromFiles(uris []fyne.URI) {
// SendText sends new text.
func (d *SendData) SendText() {
go func() {
text := d.Client.ShowTextSendWindow()
text := d.showTextSendWindow()
if text == "" {
return
}
Expand Down Expand Up @@ -320,10 +320,10 @@ func (d *SendData) getCustomCode() string {

close(code)
}, d.Window)
form.Resize(fyne.Size{Width: d.Canvas.Size().Width * 0.8})
form.Resize(fyne.Size{Width: d.Window.Canvas().Size().Width * 0.8})
codeEntry.OnSubmitted = func(_ string) { form.Submit() }
form.Show()
d.Canvas.Focus(codeEntry)
d.Window.Canvas().Focus(codeEntry)

return <-code
}
Expand Down Expand Up @@ -394,3 +394,60 @@ type sendInfoDialog struct {
label *widget.Label
image *canvas.Image
}

type textSendWindow struct {
textEntry *widget.Entry
cancelButton, sendButton *widget.Button
window fyne.Window
text chan string
}

func (s *textSendWindow) dismiss() {
s.text <- ""
s.window.Hide()
s.textEntry.SetText("")
}

func (s *textSendWindow) send() {
s.text <- s.textEntry.Text
s.window.Hide()
s.textEntry.SetText("")
}

func createTextSendWindow(app fyne.App) *textSendWindow {
display := &textSendWindow{
window: app.NewWindow("Send Text"),
textEntry: &widget.Entry{MultiLine: true, Wrapping: fyne.TextWrapWord},
cancelButton: &widget.Button{Text: "Cancel", Icon: theme.CancelIcon()},
sendButton: &widget.Button{Text: "Send", Icon: theme.MailSendIcon(), Importance: widget.HighImportance},
text: make(chan string),
}

display.window.SetCloseIntercept(display.dismiss)
display.cancelButton.OnTapped = display.dismiss
display.sendButton.OnTapped = display.send
display.textEntry.OnSubmitted = func(_ string) { display.send() }

actionContainer := container.NewGridWithColumns(2, display.cancelButton, display.sendButton)
display.window.SetContent(container.NewBorder(nil, actionContainer, nil, nil, display.textEntry))
display.window.Resize(fyne.NewSize(400, 300))
return display
}

// showTextSendWindow opens a new window for setting up text to send.
func (d *SendData) showTextSendWindow() string {
if d.textWindow == nil {
d.textWindow = createTextSendWindow(d.Client.App)
} else if d.textWindow.window.Canvas().Content().Visible() {
d.textWindow.window.RequestFocus()
return ""
}

win := d.textWindow.window

win.Show()
win.RequestFocus()
win.Canvas().Focus(d.textWindow.textEntry)

return <-d.textWindow.text
}
60 changes: 1 addition & 59 deletions internal/transport/display.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func createTextRecvWindow(app fyne.App) *textRecvWindow {
// showTextReceiveWindow handles the creation of a window for displaying text content.
func (c *Client) showTextReceiveWindow(received []byte) {
if c.textRecvWindow == nil {
c.textRecvWindow = createTextRecvWindow(c.app)
c.textRecvWindow = createTextRecvWindow(c.App)
}

d := c.textRecvWindow
Expand Down Expand Up @@ -83,61 +83,3 @@ func (c *Client) showTextReceiveWindow(received []byte) {
d.window.RequestFocus()
d.window.Canvas().Focus(d.textEntry)
}

type textSendWindow struct {
textEntry *widget.Entry
cancelButton, sendButton *widget.Button
window fyne.Window
text chan string
}

func (s *textSendWindow) dismiss() {
s.text <- ""
s.window.Hide()
s.textEntry.SetText("")
}

func (s *textSendWindow) send() {
s.text <- s.textEntry.Text
s.window.Hide()
s.textEntry.SetText("")
}

func createTextSendWindow(app fyne.App) *textSendWindow {
display := &textSendWindow{
window: app.NewWindow("Send Text"),
textEntry: &widget.Entry{MultiLine: true, Wrapping: fyne.TextWrapWord},
cancelButton: &widget.Button{Text: "Cancel", Icon: theme.CancelIcon()},
sendButton: &widget.Button{Text: "Send", Icon: theme.MailSendIcon(), Importance: widget.HighImportance},
text: make(chan string),
}

display.window.SetCloseIntercept(display.dismiss)
display.cancelButton.OnTapped = display.dismiss
display.sendButton.OnTapped = display.send
display.textEntry.OnSubmitted = func(_ string) { display.send() }

actionContainer := container.NewGridWithColumns(2, display.cancelButton, display.sendButton)
display.window.SetContent(container.NewBorder(nil, actionContainer, nil, nil, display.textEntry))
display.window.Resize(fyne.NewSize(400, 300))
return display
}

// ShowTextSendWindow opens a new window for setting up text to send.
func (c *Client) ShowTextSendWindow() string {
if c.textSendWindow == nil {
c.textSendWindow = createTextSendWindow(c.app)
} else if c.textSendWindow.window.Canvas().Content().Visible() {
c.textSendWindow.window.RequestFocus()
return ""
}

s := c.textSendWindow
win := s.window

win.Show()
win.RequestFocus()
win.Canvas().Focus(s.textEntry)

return <-s.text
}
8 changes: 4 additions & 4 deletions internal/transport/transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ import (
type Client struct {
wormhole.Client

app fyne.App
// App is a reference to the currently running Fyne applciation.
App fyne.App

// Save a reference to the window to avoid creating a new one when sending and receiving text.
textSendWindow *textSendWindow
textRecvWindow *textRecvWindow

// Notification holds the settings value for if we have notifications enabled or not.
Expand All @@ -32,11 +32,11 @@ type Client struct {
// ShowNotification sends a notification if c.Notifications is true.
func (c *Client) ShowNotification(title, content string) {
if c.Notifications {
c.app.SendNotification(&fyne.Notification{Title: title, Content: content})
c.App.SendNotification(&fyne.Notification{Title: title, Content: content})
}
}

// NewClient returns a new client for sending and receiving using wormhole-william
func NewClient(app fyne.App) *Client {
return &Client{app: app}
return &Client{App: app}
}
2 changes: 1 addition & 1 deletion internal/ui/send.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func (s *send) buildUI(window fyne.Window) *fyne.Container {
choiceContent := container.NewGridWithColumns(1, fileChoice, directoryChoice, textChoice, codeChoice)
s.contentPicker = dialog.NewCustom("Pick a content type", "Cancel", choiceContent, window)

s.data = bridge.SendData{Client: s.client, Window: window, Canvas: s.window.Canvas()}
s.data = bridge.SendData{Client: s.client, Window: window}
contentToSend := &widget.Button{Text: "Add content to send", Icon: theme.ContentAddIcon(), OnTapped: func() {
codeChoice.SetChecked(s.client.CustomCode)
s.contentPicker.Show()
Expand Down
6 changes: 3 additions & 3 deletions internal/ui/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,13 @@ type settings struct {
window fyne.Window
}

func newSettingsTab(a fyne.App, w fyne.Window, c *transport.Client) *container.TabItem {
settings := &settings{window: w, client: c, preferences: a.Preferences()}
func newSettingsTab(w fyne.Window, c *transport.Client) *container.TabItem {
settings := &settings{window: w, client: c, preferences: c.App.Preferences()}

return &container.TabItem{
Text: "Settings",
Icon: theme.SettingsIcon(),
Content: settings.buildUI(a),
Content: settings.buildUI(c.App),
}
}

Expand Down
2 changes: 1 addition & 1 deletion internal/ui/tabs.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func Create(app fyne.App, window fyne.Window) *container.AppTabs {
Items: []*container.TabItem{
send.newSendTab(),
newRecvTab(window, client),
newSettingsTab(app, window, client),
newSettingsTab(window, client),
newAboutTab(app),
},
}
Expand Down

0 comments on commit ecc67a6

Please sign in to comment.