diff --git a/ui/commands.go b/ui/commands.go index e05076c..dc7f35c 100644 --- a/ui/commands.go +++ b/ui/commands.go @@ -302,12 +302,12 @@ func (tv *TutView) PreferencesCommand() { func (tv *TutView) ListPlacementCommand(lp config.ListPlacement) { tv.tut.Config.General.ListPlacement = lp - tv.MainView.be_ForceUpdate(tv) + tv.MainView.Be_ForceUpdate(tv) } func (tv *TutView) ListSplitCommand(ls config.ListSplit) { tv.tut.Config.General.ListSplit = ls - tv.MainView.be_ForceUpdate(tv) + tv.MainView.Be_ForceUpdate(tv) } func (tv *TutView) ProportionsCommand(lp string, cp string) { @@ -323,7 +323,7 @@ func (tv *TutView) ProportionsCommand(lp string, cp string) { } tv.tut.Config.General.ListProportion = lpi tv.tut.Config.General.ContentProportion = cpi - tv.MainView.be_ForceUpdate(tv) + tv.MainView.Be_ForceUpdate(tv) } func (tv *TutView) LoadNewerCommand() { diff --git a/ui/input.go b/ui/input.go index 39a3fbc..53609b5 100644 --- a/ui/input.go +++ b/ui/input.go @@ -277,7 +277,8 @@ func (tv *TutView) InputMainViewFeed(event *tcell.EventKey) *tcell.EventKey { if tv.tut.Config.Input.GlobalExit.Match(event.Key(), event.Rune()) { exiting := tv.Timeline.RemoveCurrent(false) if exiting && tv.Timeline.FeedFocusIndex == 0 { - tv.ModalView.Run("Do you want to exit tut?", + tv.ModalView.Be_OpenConfirm(tv, + "Do you want to exit tut?", func() { tv.Timeline.RemoveCurrent(true) }) @@ -447,7 +448,8 @@ func (tv *TutView) InputStatus(event *tcell.EventKey, item api.Item, status *mas if boosted { txt = "unboost" } - tv.ModalView.Run( + tv.ModalView.Be_OpenConfirm(tv, + fmt.Sprintf("Do you want to %s this toot?", txt), func() { ns, err := tv.tut.Client.BoostToggle(status) if err != nil { @@ -465,23 +467,24 @@ func (tv *TutView) InputStatus(event *tcell.EventKey, item api.Item, status *mas if !isMine { return nil } - tv.ModalView.Run("Do you want to delete this toot?", func() { - err := tv.tut.Client.DeleteStatus(sr) - if err != nil { - tv.ShowError( - fmt.Sprintf("Couldn't delete toot. Error: %v\n", err), - ) - return - } - status.Card = nil - status.Sensitive = false - status.SpoilerText = "" - status.Favourited = false - status.MediaAttachments = nil - status.Reblogged = false - status.Content = "Deleted" - tv.RedrawContent() - }) + tv.ModalView.Be_OpenConfirm(tv, + "Do you want to delete this toot?", func() { + err := tv.tut.Client.DeleteStatus(sr) + if err != nil { + tv.ShowError( + fmt.Sprintf("Couldn't delete toot. Error: %v\n", err), + ) + return + } + status.Card = nil + status.Sensitive = false + status.SpoilerText = "" + status.Favourited = false + status.MediaAttachments = nil + status.Reblogged = false + status.Content = "Deleted" + tv.RedrawContent() + }) return nil } if tv.tut.Config.Input.StatusEdit.Match(event.Key(), event.Rune()) { @@ -493,7 +496,8 @@ func (tv *TutView) InputStatus(event *tcell.EventKey, item api.Item, status *mas if favorited { txt = "unfavorite" } - tv.ModalView.Run(fmt.Sprintf("Do you want to %s this toot?", txt), + tv.ModalView.Be_OpenConfirm(tv, + fmt.Sprintf("Do you want to %s this toot?", txt), func() { ns, err := tv.tut.Client.FavoriteToogle(status) if err != nil { @@ -534,7 +538,8 @@ func (tv *TutView) InputStatus(event *tcell.EventKey, item api.Item, status *mas if bookmarked { txt = "unsave" } - tv.ModalView.Run(fmt.Sprintf("Do you want to %s this toot?", txt), + tv.ModalView.Be_OpenConfirm(tv, + fmt.Sprintf("Do you want to %s this toot?", txt), func() { ns, err := tv.tut.Client.BookmarkToogle(status) if err != nil { @@ -700,7 +705,8 @@ func (tv *TutView) InputUser(event *tcell.EventKey, user *api.User, ut InputUser } if ut == InputUserFollowRequest && tv.tut.Config.Input.UserFollowRequestDecide.Match(event.Key(), event.Rune()) { - tv.ModalView.RunDecide("Do you want accept the follow request?", + tv.ModalView.Be_Open(tv, + "Do you want accept the follow request?", func() { err := tv.tut.Client.FollowRequestAccept(user.Data) if err != nil { @@ -736,7 +742,8 @@ func (tv *TutView) InputUser(event *tcell.EventKey, user *api.User, ut InputUser if blocking { txt = "unblock" } - tv.ModalView.Run(fmt.Sprintf("Do you want to %s this user?", txt), + tv.ModalView.Be_OpenConfirm(tv, + fmt.Sprintf("Do you want to %s this user?", txt), func() { rel, err := tv.tut.Client.BlockToggle(user) if err != nil { @@ -755,7 +762,8 @@ func (tv *TutView) InputUser(event *tcell.EventKey, user *api.User, ut InputUser if following { txt = "unfollow" } - tv.ModalView.Run(fmt.Sprintf("Do you want to %s this user?", txt), + tv.ModalView.Be_OpenConfirm(tv, + fmt.Sprintf("Do you want to %s this user?", txt), func() { rel, err := tv.tut.Client.FollowToggle(user) if err != nil { @@ -774,7 +782,8 @@ func (tv *TutView) InputUser(event *tcell.EventKey, user *api.User, ut InputUser if muting { txt = "unmute" } - tv.ModalView.Run(fmt.Sprintf("Do you want to %s this user?", txt), + tv.ModalView.Be_OpenConfirm(tv, + fmt.Sprintf("Do you want to %s this user?", txt), func() { rel, err := tv.tut.Client.MuteToggle(user) if err != nil { @@ -852,7 +861,8 @@ func (tv *TutView) InputTag(event *tcell.EventKey, tag *mastodon.Tag) *tcell.Eve if tag.Following != nil && tag.Following == true { txt = "unfollow" } - tv.ModalView.Run(fmt.Sprintf("Do you want to %s #%s?", txt, tag.Name), + tv.ModalView.Be_OpenConfirm(tv, + fmt.Sprintf("Do you want to %s #%s?", txt, tag.Name), func() { nt, err := tv.tut.Client.TagToggleFollow(tag) if err != nil { @@ -948,7 +958,8 @@ func (tv *TutView) InputComposeView(event *tcell.EventKey) *tcell.EventKey { } if tv.tut.Config.Input.GlobalBack.Match(event.Key(), event.Rune()) || tv.tut.Config.Input.GlobalExit.Match(event.Key(), event.Rune()) { - tv.ModalView.Run( + tv.ModalView.Be_OpenConfirm(tv, + "Do you want exit the compose view?", func() { tv.FocusMainNoHistory() }) @@ -1102,7 +1113,8 @@ func (tv *TutView) InputPreference(event *tcell.EventKey) *tcell.EventKey { } if tv.tut.Config.Input.GlobalBack.Match(event.Key(), event.Rune()) || tv.tut.Config.Input.GlobalExit.Match(event.Key(), event.Rune()) { - tv.ModalView.Run( + tv.ModalView.Be_OpenConfirm(tv, + "Do you want exit the preference view?", func() { tv.FocusMainNoHistory() }) diff --git a/ui/item.go b/ui/item.go index d7ba959..8a77889 100644 --- a/ui/item.go +++ b/ui/item.go @@ -107,7 +107,7 @@ func DrawItem(tv *TutView, item api.Item, main *tview.TextView, controls *tview. drawUser(tv, item.Raw().(*api.User), main, controls, "", InputUserNormal) } case api.NotificationType: - drawNotification(tv, item, item.Raw().(*api.NotificationData), main, controls) + drawNotification(tv, item.Raw().(*api.NotificationData), main, controls) case api.ListsType: drawList(tv, item.Raw().(*mastodon.List), main, controls) case api.TagType: @@ -139,7 +139,7 @@ func DrawItemControls(tv *TutView, item api.Item, controls *tview.Flex, ft confi drawUser(tv, item.Raw().(*api.User), nil, controls, "", InputUserNormal) } case api.NotificationType: - drawNotification(tv, item, item.Raw().(*api.NotificationData), nil, controls) + drawNotification(tv, item.Raw().(*api.NotificationData), nil, controls) case api.ListsType: drawList(tv, item.Raw().(*mastodon.List), nil, controls) case api.TagType: diff --git a/ui/item_notification.go b/ui/item_notification.go index d6b29f4..c7f4de2 100644 --- a/ui/item_notification.go +++ b/ui/item_notification.go @@ -9,7 +9,7 @@ import ( "github.com/rivo/tview" ) -func drawNotification(tv *TutView, item api.Item, notification *api.NotificationData, main *tview.TextView, controls *tview.Flex) { +func drawNotification(tv *TutView, notification *api.NotificationData, main *tview.TextView, controls *tview.Flex) { switch notification.Item.Type { case "follow": drawUser(tv, notification.User.Raw().(*api.User), main, controls, diff --git a/ui/item_user.go b/ui/item_user.go index 5bdd280..b1ad18b 100644 --- a/ui/item_user.go +++ b/ui/item_user.go @@ -109,9 +109,10 @@ func drawUser(tv *TutView, data *api.User, main *tview.TextView, controls *tview controlItems = append(controlItems, NewControl(tv.tut.Config, tv.tut.Config.Input.UserYank, true)) // Clear controls and only have add and delete for lists. - if ut == InputUserListAdd { + switch ut { + case InputUserListAdd: controlItems = []Control{NewControl(tv.tut.Config, tv.tut.Config.Input.ListUserAdd, true)} - } else if ut == InputUserListDelete { + case InputUserListDelete: controlItems = []Control{NewControl(tv.tut.Config, tv.tut.Config.Input.ListUserDelete, true)} } diff --git a/ui/mainview.go b/ui/mainview.go index af0a3d7..fec4059 100644 --- a/ui/mainview.go +++ b/ui/mainview.go @@ -10,21 +10,21 @@ import ( type MainView struct { phony.Inbox - tv *TutView + tv *TutView View *tview.Flex accView *tview.Flex } func NewMainView(tv *TutView) *MainView { mv := &MainView{ - tv: tv, + tv: tv, accView: NewControlView(tv.tut.Config), } mv.View = mv.mainViewUI(tv) return mv } -func (mv *MainView) be_ForceUpdate(from phony.Actor) { +func (mv *MainView) Be_ForceUpdate(from phony.Actor) { mv.Act(from, func() { tv := mv.tv tv.tut.App.QueueUpdateDraw(func() { @@ -109,19 +109,20 @@ func (mv *MainView) mainViewUI(tv *TutView) *tview.Flex { if tv.tut.Config.General.TerminalTitle < 2 { r.AddItem(tv.Shared.Top.View, 1, 0, false) } - if tv.tut.Config.General.ListPlacement == config.ListPlacementTop { + switch tv.tut.Config.General.ListPlacement { + case config.ListPlacementTop: r.AddItem(list, 0, lp, false). AddItem(hl, 1, 0, false). AddItem(content, 0, cp, false). AddItem(controls, 1, 0, false). AddItem(tv.Shared.Bottom.View, 2, 0, false) - } else if tv.tut.Config.General.ListPlacement == config.ListPlacementBottom { + case config.ListPlacementBottom: r.AddItem(content, 0, cp, false). AddItem(controls, 1, 0, false). AddItem(hl, 1, 0, false). AddItem(list, 0, lp, false). AddItem(tv.Shared.Bottom.View, 2, 0, false) - } else if tv.tut.Config.General.ListPlacement == config.ListPlacementLeft { + case config.ListPlacementLeft: r.AddItem(tview.NewFlex().SetDirection(tview.FlexColumn). AddItem(list, 0, lp, false). AddItem(vl, 1, 0, false). @@ -129,7 +130,7 @@ func (mv *MainView) mainViewUI(tv *TutView) *tview.Flex { AddItem(content, 0, 1, false). AddItem(controls, 1, 0, false), 0, cp, false), 0, 1, false). AddItem(tv.Shared.Bottom.View, 2, 0, false) - } else if tv.tut.Config.General.ListPlacement == config.ListPlacementRight { + case config.ListPlacementRight: r.AddItem(tview.NewFlex().SetDirection(tview.FlexColumn). AddItem(tview.NewFlex().SetDirection(tview.FlexRow). AddItem(content, 0, 1, false). diff --git a/ui/modalview.go b/ui/modalview.go index e268be2..2406e7a 100644 --- a/ui/modalview.go +++ b/ui/modalview.go @@ -1,69 +1,75 @@ package ui import ( + "github.com/Arceliar/phony" "github.com/rivo/tview" ) +type ModelViewOnceAction struct { + fnYes func() + fnNo func() +} + type ModalView struct { - tutView *TutView - View *tview.Modal - res chan bool + phony.Inbox + onceAction *ModelViewOnceAction + tutView *TutView + View *tview.Modal } func NewModalView(tv *TutView) *ModalView { mv := &ModalView{ tutView: tv, View: NewModal(tv.tut.Config), - res: make(chan bool, 1), } mv.View.SetText("Are you sure?"). AddButtons([]string{"Yes", "No"}). SetDoneFunc(func(buttonIndex int, buttonLabel string) { if buttonLabel == "Yes" { - mv.res <- true + mv.Be_Done(mv, true) } else { - mv.res <- false + mv.Be_Done(mv, false) } }) return mv } -func (mv *ModalView) run(text string) (chan bool, func()) { - mv.View.SetFocus(0) - mv.View.SetText(text) - mv.tutView.SetPage(ModalFocus) - return mv.res, func() { +func (mv *ModalView) Be_Done(from phony.Actor, res bool) { + mv.Act(from, func() { + action := mv.onceAction + mv.onceAction = nil + if res { + action.fnYes() + } else { + action.fnNo() + } mv.tutView.tut.App.QueueUpdateDraw(func() { mv.tutView.PrevFocus() }) - } + }) } -func (mv *ModalView) Run(text string, fn func()) { + +func (mv *ModalView) Be_OpenConfirm(from phony.Actor, text string, fn func()) { if !mv.tutView.tut.Config.General.Confirmation { fn() return } - r, f := mv.run(text) - go func() { - if <-r { - fn() - } - f() - }() + + mv.Be_Open(from, text, fn, func() {}) } -func (mv *ModalView) Stop(fn func()) { - fn() +func (mv *ModalView) Be_Open(from phony.Actor, text string, fnYes func(), fnNo func()) { + mv.Act(from, func() { + mv.onceAction = &ModelViewOnceAction{fnYes, fnNo} + mv.View.SetFocus(0) + mv.View.SetText(text) + mv.tutView.tut.App.QueueUpdateDraw(func() { + mv.tutView.SetPage(ModalFocus) + }) + }) } -func (mv *ModalView) RunDecide(text string, fnYes func(), fnNo func()) { - r, f := mv.run(text) - go func() { - if <-r { - fnYes() - } else { - fnNo() - } - f() - }() +// no idea what this is for +func (mv *ModalView) Stop(fn func()) { + fn() } diff --git a/ui/timeline.go b/ui/timeline.go index a047d07..976046a 100644 --- a/ui/timeline.go +++ b/ui/timeline.go @@ -161,7 +161,7 @@ func (tl *Timeline) AddFeed(f *Feed, newPane bool) { fh.FeedIndex = fh.FeedIndex + 1 } tl.tutView.Shared.Top.SetText(tl.GetTitle()) - tl.tutView.MainView.be_ForceUpdate(tl.tutView) + tl.tutView.MainView.Be_ForceUpdate(tl.tutView) } func (tl *Timeline) RemoveCurrent(quit bool) bool { @@ -182,7 +182,7 @@ func (tl *Timeline) RemoveCurrent(quit bool) bool { } f.FeedIndex = ni tl.tutView.Shared.Top.SetText(tl.GetTitle()) - tl.tutView.MainView.be_ForceUpdate(tl.tutView) + tl.tutView.MainView.Be_ForceUpdate(tl.tutView) return false } @@ -257,7 +257,7 @@ func (tl *Timeline) NextFeed() { } f.FeedIndex = ni tl.tutView.Shared.Top.SetText(tl.GetTitle()) - tl.tutView.MainView.be_ForceUpdate(tl.tutView) + tl.tutView.MainView.Be_ForceUpdate(tl.tutView) } func (tl *Timeline) PrevFeed() { @@ -268,7 +268,7 @@ func (tl *Timeline) PrevFeed() { } f.FeedIndex = ni tl.tutView.Shared.Top.SetText(tl.GetTitle()) - tl.tutView.MainView.be_ForceUpdate(tl.tutView) + tl.tutView.MainView.Be_ForceUpdate(tl.tutView) } func (tl *Timeline) FindAndGoTo(ft config.FeedType, data string, hideBoosts, hideReplies bool) bool { @@ -281,7 +281,7 @@ func (tl *Timeline) FindAndGoTo(ft config.FeedType, data string, hideBoosts, hid tl.tutView.FocusFeed(i, nil) fh.FeedIndex = j tl.tutView.Shared.Top.SetText(tl.GetTitle()) - tl.tutView.MainView.be_ForceUpdate(tl.tutView) + tl.tutView.MainView.Be_ForceUpdate(tl.tutView) return true } } diff --git a/ui/tutview.go b/ui/tutview.go index 7cf172a..77d1cb2 100644 --- a/ui/tutview.go +++ b/ui/tutview.go @@ -179,7 +179,7 @@ func (tvh *TutViewsHolder) SetFocusedTutView(index int) { App.SetMouseCapture(curr.MouseInput) } if curr.MainView != nil { - curr.MainView.be_ForceUpdate(curr) + curr.MainView.Be_ForceUpdate(curr) } } @@ -295,7 +295,7 @@ func (tv *TutView) FocusFeed(index int, ct *config.Timeline) { } } tv.Shared.Top.SetText(tv.Timeline.GetTitle()) - tv.MainView.be_ForceUpdate(tv) + tv.MainView.Be_ForceUpdate(tv) } func (tv *TutView) NextFeed() {