Skip to content

Commit baebc86

Browse files
authored
Modal fixes (#281) (#284)
1 parent 0deee6d commit baebc86

File tree

5 files changed

+46
-40
lines changed

5 files changed

+46
-40
lines changed

display/display.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ func Init(version, commit, builtBy string) {
241241
}
242242
if i <= len(tabs[tab].page.Links) && i > 0 {
243243
// It's a valid link number
244-
followLink(tabs[tab], tabs[tab].page.URL, tabs[tab].page.Links[i-1])
244+
go followLink(tabs[tab], tabs[tab].page.URL, tabs[tab].page.Links[i-1])
245245
return
246246
}
247247
// Invalid link number, don't do anything

display/handlers.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -352,15 +352,15 @@ func handleURL(t *tab, u string, numRedirects int) (string, bool) {
352352
// Disable read timeout and go back to start
353353
res.SetReadTimeout(0) //nolint: errcheck
354354
res.Body.(*rr.RestartReader).Restart()
355-
go dlChoice("That page is too large. What would you like to do?", u, res)
355+
dlChoice("That page is too large. What would you like to do?", u, res)
356356
return ret("", false)
357357
}
358358
if errors.Is(err, renderer.ErrTimedOut) {
359359
// Downloading now
360360
// Disable read timeout and go back to start
361361
res.SetReadTimeout(0) //nolint: errcheck
362362
res.Body.(*rr.RestartReader).Restart()
363-
go dlChoice("Loading that page timed out. What would you like to do?", u, res)
363+
dlChoice("Loading that page timed out. What would you like to do?", u, res)
364364
return ret("", false)
365365
}
366366
if err != nil {
@@ -493,7 +493,7 @@ func handleURL(t *tab, u string, numRedirects int) (string, bool) {
493493
// Disable read timeout and go back to start
494494
res.SetReadTimeout(0) //nolint: errcheck
495495
res.Body.(*rr.RestartReader).Restart()
496-
go dlChoice("That file could not be displayed. What would you like to do?", u, res)
496+
dlChoice("That file could not be displayed. What would you like to do?", u, res)
497497
}
498498
}()
499499
return ret("", false)
@@ -503,6 +503,6 @@ func handleURL(t *tab, u string, numRedirects int) (string, bool) {
503503
// Disable read timeout and go back to start
504504
res.SetReadTimeout(0) //nolint: errcheck
505505
res.Body.(*rr.RestartReader).Restart()
506-
go dlChoice("That file could not be displayed. What would you like to do?", u, res)
506+
dlChoice("That file could not be displayed. What would you like to do?", u, res)
507507
return ret("", false)
508508
}

display/modals.go

+16-16
Original file line numberDiff line numberDiff line change
@@ -16,24 +16,21 @@ import (
1616
// The bookmark modal is in bookmarks.go
1717

1818
var infoModal = cview.NewModal()
19-
2019
var errorModal = cview.NewModal()
21-
var errorModalDone = make(chan struct{})
22-
2320
var inputModal = cview.NewModal()
24-
var inputCh = make(chan string)
25-
var inputModalText string // The current text of the input field in the modal
26-
2721
var yesNoModal = cview.NewModal()
2822

29-
// Channel to receive yesNo answer on
23+
var inputCh = make(chan string)
3024
var yesNoCh = make(chan bool)
3125

26+
var inputModalText string // The current text of the input field in the modal
27+
28+
// Internal channel used to know when a modal has been dismissed
29+
var modalDone = make(chan struct{})
30+
3231
func modalInit() {
3332
infoModal.AddButtons([]string{"Ok"})
34-
3533
errorModal.AddButtons([]string{"Ok"})
36-
3734
yesNoModal.AddButtons([]string{"Yes", "No"})
3835

3936
panels.AddPanel(PanelInfoModal, infoModal, false, false)
@@ -145,6 +142,7 @@ func modalInit() {
145142
panels.HidePanel(PanelInfoModal)
146143
App.SetFocus(tabs[curTab].view)
147144
App.Draw()
145+
modalDone <- struct{}{}
148146
})
149147

150148
errorModal.SetBorder(true)
@@ -153,7 +151,7 @@ func modalInit() {
153151
panels.HidePanel(PanelErrorModal)
154152
App.SetFocus(tabs[curTab].view)
155153
App.Draw()
156-
errorModalDone <- struct{}{}
154+
modalDone <- struct{}{}
157155
})
158156

159157
inputModal.SetBorder(true)
@@ -183,7 +181,7 @@ func modalInit() {
183181
dlInit()
184182
}
185183

186-
// Error displays an error on the screen in a modal.
184+
// Error displays an error on the screen in a modal, and blocks until dismissed by the user.
187185
func Error(title, text string) {
188186
if text == "" {
189187
text = "No additional information."
@@ -203,19 +201,21 @@ func Error(title, text string) {
203201
App.SetFocus(errorModal)
204202
App.Draw()
205203

206-
<-errorModalDone
204+
<-modalDone
207205
}
208206

209-
// Info displays some info on the screen in a modal.
207+
// Info displays some info on the screen in a modal, and blocks until dismissed by the user.
210208
func Info(s string) {
211209
infoModal.SetText(s)
212210
panels.ShowPanel(PanelInfoModal)
213211
panels.SendToFront(PanelInfoModal)
214212
App.SetFocus(infoModal)
215213
App.Draw()
214+
215+
<-modalDone
216216
}
217217

218-
// Input pulls up a modal that asks for input, and returns the user's input.
218+
// Input pulls up a modal that asks for input, waits for that input, and returns it.
219219
// It returns an bool indicating if the user chose to send input or not.
220220
func Input(prompt string, sensitive bool) (string, bool) {
221221
// Remove elements and re-add them - to clear input text and keep input in focus
@@ -257,7 +257,7 @@ func Input(prompt string, sensitive bool) (string, bool) {
257257
return resp, true
258258
}
259259

260-
// YesNo displays a modal asking a yes-or-no question.
260+
// YesNo displays a modal asking a yes-or-no question, waits for an answer, then returns it as a bool.
261261
func YesNo(prompt string) bool {
262262
if viper.GetBool("a-general.color") {
263263
m := yesNoModal
@@ -289,7 +289,7 @@ func YesNo(prompt string) bool {
289289
}
290290

291291
// Tofu displays the TOFU warning modal.
292-
// It returns a bool indicating whether the user wants to continue.
292+
// It blocks then returns a bool indicating whether the user wants to continue.
293293
func Tofu(host string, expiry time.Time) bool {
294294
// Reuses yesNoModal, with error color
295295

display/private.go

+17-11
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package display
22

3+
// This file contains the functions that aren't part of the public API.
4+
// The funcs are for network and displaying.
5+
36
import (
47
"net/url"
58
"strconv"
@@ -9,17 +12,20 @@ import (
912
"github.com/makeworld-the-better-one/amfora/structs"
1013
)
1114

12-
// This file contains the functions that aren't part of the public API.
13-
// The funcs are for network and displaying.
14-
15-
// followLink should be used when the user "clicks" a link on a page.
16-
// Not when a URL is opened on a new tab for the first time.
17-
// It will handle setting the bottomBar.
15+
// followLink should be used when the user "clicks" a link on a page,
16+
// but not when a URL is opened on a new tab for the first time.
17+
//
18+
// It will handle updating the bottomBar.
19+
//
20+
// It should be called with the `go` keyword to spawn a new goroutine if
21+
// it would otherwise block the UI loop, such as when called from an input
22+
// handler.
23+
//
24+
// It blocks until navigation is finished, and we've completed any user
25+
// interaction related to loading the URL (such as info, error modals)
1826
func followLink(t *tab, prev, next string) {
1927
if strings.HasPrefix(next, "about:") {
20-
if final, ok := handleAbout(t, next); ok {
21-
t.addToHistory(final)
22-
}
28+
goURL(t, next)
2329
return
2430
}
2531

@@ -29,7 +35,7 @@ func followLink(t *tab, prev, next string) {
2935
Error("URL Error", err.Error())
3036
return
3137
}
32-
go goURL(t, nextURL)
38+
goURL(t, nextURL)
3339
return
3440
}
3541
// No content on current tab, so the "prev" URL is not valid.
@@ -39,7 +45,7 @@ func followLink(t *tab, prev, next string) {
3945
Error("URL Error", "Link URL could not be parsed")
4046
return
4147
}
42-
go goURL(t, next)
48+
goURL(t, next)
4349
}
4450

4551
// reformatPage will take the raw page content and reformat it according to the current terminal dimensions.

display/tab.go

+8-8
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ func makeNewTab() *tab {
8686
linkN, _ := strconv.Atoi(currentSelection[0])
8787
tabs[tab].page.Selected = tabs[tab].page.Links[linkN]
8888
tabs[tab].page.SelectedID = currentSelection[0]
89-
followLink(tabs[tab], tabs[tab].page.URL, tabs[tab].page.Links[linkN])
89+
go followLink(tabs[tab], tabs[tab].page.URL, tabs[tab].page.Links[linkN])
9090
return
9191
}
9292
if len(currentSelection) == 0 && (key == tcell.KeyEnter || key == tcell.KeyTab) {
@@ -156,12 +156,12 @@ func makeNewTab() *tab {
156156
if t.hasContent() {
157157
savePath, err := downloadPage(t.page)
158158
if err != nil {
159-
Error("Download Error", fmt.Sprintf("Error saving page content: %v", err))
159+
go Error("Download Error", fmt.Sprintf("Error saving page content: %v", err))
160160
} else {
161-
Info(fmt.Sprintf("Page content saved to %s. ", savePath))
161+
go Info(fmt.Sprintf("Page content saved to %s. ", savePath))
162162
}
163163
} else {
164-
Info("The current page has no content, so it couldn't be downloaded.")
164+
go Info("The current page has no content, so it couldn't be downloaded.")
165165
}
166166
return nil
167167
case config.CmdBack:
@@ -178,7 +178,7 @@ func makeNewTab() *tab {
178178
currentURL := tabs[curTab].page.URL
179179
err := clipboard.WriteAll(currentURL)
180180
if err != nil {
181-
Error("Copy Error", err.Error())
181+
go Error("Copy Error", err.Error())
182182
return nil
183183
}
184184
return nil
@@ -193,14 +193,14 @@ func makeNewTab() *tab {
193193
if err != nil {
194194
err := clipboard.WriteAll(selectedURL)
195195
if err != nil {
196-
Error("Copy Error", err.Error())
196+
go Error("Copy Error", err.Error())
197197
return nil
198198
}
199199
return nil
200200
}
201201
err = clipboard.WriteAll(copiedURL.String())
202202
if err != nil {
203-
Error("Copy Error", err.Error())
203+
go Error("Copy Error", err.Error())
204204
return nil
205205
}
206206
return nil
@@ -209,7 +209,7 @@ func makeNewTab() *tab {
209209
if cmd >= config.CmdLink1 && cmd <= config.CmdLink0 {
210210
if int(cmd) <= len(t.page.Links) {
211211
// It's a valid link number
212-
followLink(&t, t.page.URL, t.page.Links[cmd-1])
212+
go followLink(&t, t.page.URL, t.page.Links[cmd-1])
213213
return nil
214214
}
215215
}

0 commit comments

Comments
 (0)