2019-11-16 05:00:27 +02:00
package gui
2020-08-17 13:58:30 +02:00
type ListContext struct {
2020-08-20 00:52:51 +02:00
GetItemsLength func ( ) int
GetDisplayStrings func ( ) [ ] [ ] string
OnFocus func ( ) error
OnFocusLost func ( ) error
OnClickSelectedItem func ( ) error
2020-08-22 07:56:30 +02:00
2020-08-23 01:42:30 +02:00
// the boolean here tells us whether the item is nil. This is needed because you can't work it out on the calling end once the pointer is wrapped in an interface (unless you want to use reflection)
2020-08-22 07:56:30 +02:00
SelectedItem func ( ) ( ListItem , bool )
GetPanelState func ( ) IListPanelState
2020-08-19 10:06:51 +02:00
2021-10-17 10:01:02 +02:00
Gui * Gui
2021-04-11 05:17:20 +02:00
* BasicContext
2019-11-16 05:00:27 +02:00
}
2021-03-31 15:24:41 +02:00
type IListPanelState interface {
SetSelectedLineIdx ( int )
GetSelectedLineIdx ( ) int
}
2020-08-20 00:24:35 +02:00
type ListItem interface {
2020-08-22 02:14:53 +02:00
// ID is a SHA when the item is a commit, a filename when the item is a file, 'stash@{4}' when it's a stash entry, 'my_branch' when it's a branch
2020-08-20 00:24:35 +02:00
ID ( ) string
2020-08-22 02:14:53 +02:00
// Description is something we would show in a message e.g. '123as14: push blah' for a commit
Description ( ) string
2020-08-20 00:24:35 +02:00
}
2020-08-22 07:56:30 +02:00
func ( lc * ListContext ) GetSelectedItem ( ) ( ListItem , bool ) {
2020-08-22 01:55:49 +02:00
return lc . SelectedItem ( )
}
2020-08-20 00:24:35 +02:00
func ( lc * ListContext ) GetSelectedItemId ( ) string {
2020-08-22 07:56:30 +02:00
item , ok := lc . SelectedItem ( )
2020-08-20 00:24:35 +02:00
2020-08-22 07:56:30 +02:00
if ! ok {
2020-08-20 00:24:35 +02:00
return ""
}
return item . ID ( )
}
2020-08-19 13:51:50 +02:00
// OnFocus assumes that the content of the context has already been rendered to the view. OnRender is the function which actually renders the content to the view
func ( lc * ListContext ) OnRender ( ) error {
view , err := lc . Gui . g . View ( lc . ViewName )
if err != nil {
return nil
}
if lc . GetDisplayStrings != nil {
2020-08-20 00:52:51 +02:00
lc . Gui . refreshSelectedLine ( lc . GetPanelState ( ) , lc . GetItemsLength ( ) )
2020-08-19 13:51:50 +02:00
lc . Gui . renderDisplayStrings ( view , lc . GetDisplayStrings ( ) )
}
return nil
}
2020-08-17 13:58:30 +02:00
func ( lc * ListContext ) HandleFocusLost ( ) error {
if lc . OnFocusLost != nil {
return lc . OnFocusLost ( )
2020-08-16 05:58:29 +02:00
}
return nil
}
2020-08-17 13:58:30 +02:00
func ( lc * ListContext ) HandleFocus ( ) error {
2020-08-19 14:27:31 +02:00
if lc . Gui . popupPanelFocused ( ) {
return nil
}
2020-08-22 00:49:02 +02:00
view , err := lc . Gui . g . View ( lc . ViewName )
if err != nil {
return nil
}
view . FocusPoint ( 0 , lc . GetPanelState ( ) . GetSelectedLineIdx ( ) )
2020-08-22 03:44:03 +02:00
if lc . Gui . State . Modes . Diffing . Active ( ) {
2020-08-19 14:27:31 +02:00
return lc . Gui . renderDiff ( )
}
if lc . OnFocus != nil {
return lc . OnFocus ( )
}
return nil
2020-08-16 05:58:29 +02:00
}
2020-08-19 10:06:51 +02:00
func ( lc * ListContext ) HandleRender ( ) error {
return lc . OnRender ( )
}
2021-04-02 10:20:40 +02:00
func ( lc * ListContext ) handlePrevLine ( ) error {
2020-08-17 13:58:30 +02:00
return lc . handleLineChange ( - 1 )
2019-11-16 05:00:27 +02:00
}
2021-04-02 10:20:40 +02:00
func ( lc * ListContext ) handleNextLine ( ) error {
2020-08-17 13:58:30 +02:00
return lc . handleLineChange ( 1 )
2019-11-16 05:00:27 +02:00
}
2020-08-17 13:58:30 +02:00
func ( lc * ListContext ) handleLineChange ( change int ) error {
if ! lc . Gui . isPopupPanel ( lc . ViewName ) && lc . Gui . popupPanelFocused ( ) {
2019-11-16 05:00:27 +02:00
return nil
}
2020-08-17 13:58:30 +02:00
view , err := lc . Gui . g . View ( lc . ViewName )
2020-08-16 01:18:57 +02:00
if err != nil {
return err
}
2020-10-01 13:40:20 +02:00
selectedLineIdx := lc . GetPanelState ( ) . GetSelectedLineIdx ( )
if ( change < 0 && selectedLineIdx == 0 ) || ( change > 0 && selectedLineIdx == lc . GetItemsLength ( ) - 1 ) {
return nil
}
2020-08-20 00:52:51 +02:00
lc . Gui . changeSelectedLine ( lc . GetPanelState ( ) , lc . GetItemsLength ( ) , change )
view . FocusPoint ( 0 , lc . GetPanelState ( ) . GetSelectedLineIdx ( ) )
2019-11-16 05:00:27 +02:00
2020-08-19 14:27:31 +02:00
return lc . HandleFocus ( )
2019-11-16 05:00:27 +02:00
}
2021-04-02 10:20:40 +02:00
func ( lc * ListContext ) handleNextPage ( ) error {
2020-08-17 13:58:30 +02:00
view , err := lc . Gui . g . View ( lc . ViewName )
2020-03-28 04:44:20 +02:00
if err != nil {
return nil
}
2020-10-01 23:56:14 +02:00
delta := lc . Gui . pageDelta ( view )
2020-08-17 13:58:30 +02:00
return lc . handleLineChange ( delta )
2020-03-28 04:44:20 +02:00
}
2021-04-02 10:20:40 +02:00
func ( lc * ListContext ) handleGotoTop ( ) error {
2020-08-17 13:58:30 +02:00
return lc . handleLineChange ( - lc . GetItemsLength ( ) )
2020-03-28 04:44:20 +02:00
}
2021-04-02 10:20:40 +02:00
func ( lc * ListContext ) handleGotoBottom ( ) error {
2020-08-17 13:58:30 +02:00
return lc . handleLineChange ( lc . GetItemsLength ( ) )
2020-03-28 04:44:20 +02:00
}
2021-04-02 10:20:40 +02:00
func ( lc * ListContext ) handlePrevPage ( ) error {
2020-08-17 13:58:30 +02:00
view , err := lc . Gui . g . View ( lc . ViewName )
2020-03-28 04:44:20 +02:00
if err != nil {
return nil
}
2020-10-01 23:56:14 +02:00
delta := lc . Gui . pageDelta ( view )
2020-08-17 13:58:30 +02:00
return lc . handleLineChange ( - delta )
2020-03-28 04:44:20 +02:00
}
2021-04-02 10:20:40 +02:00
func ( lc * ListContext ) handleClick ( ) error {
2020-08-17 13:58:30 +02:00
if ! lc . Gui . isPopupPanel ( lc . ViewName ) && lc . Gui . popupPanelFocused ( ) {
2019-11-17 08:23:06 +02:00
return nil
}
2021-04-02 10:20:40 +02:00
view , err := lc . Gui . g . View ( lc . ViewName )
if err != nil {
return nil
}
2020-08-20 00:52:51 +02:00
prevSelectedLineIdx := lc . GetPanelState ( ) . GetSelectedLineIdx ( )
2021-04-02 10:20:40 +02:00
newSelectedLineIdx := view . SelectedLineIdx ( )
2019-11-17 08:23:06 +02:00
2020-08-16 01:18:57 +02:00
// we need to focus the view
2020-11-28 04:14:48 +02:00
if err := lc . Gui . pushContext ( lc ) ; err != nil {
2020-08-16 01:18:57 +02:00
return err
}
2020-08-17 13:58:30 +02:00
if newSelectedLineIdx > lc . GetItemsLength ( ) - 1 {
2020-08-16 05:58:29 +02:00
return nil
2019-11-17 08:23:06 +02:00
}
2020-08-20 00:52:51 +02:00
lc . GetPanelState ( ) . SetSelectedLineIdx ( newSelectedLineIdx )
2019-11-17 08:23:06 +02:00
2020-08-17 13:58:30 +02:00
prevViewName := lc . Gui . currentViewName ( )
if prevSelectedLineIdx == newSelectedLineIdx && prevViewName == lc . ViewName && lc . OnClickSelectedItem != nil {
return lc . OnClickSelectedItem ( )
2019-11-17 08:23:06 +02:00
}
2020-08-19 14:27:31 +02:00
return lc . HandleFocus ( )
2020-08-16 01:18:57 +02:00
}
2020-08-17 13:58:30 +02:00
func ( lc * ListContext ) onSearchSelect ( selectedLineIdx int ) error {
2020-08-20 00:52:51 +02:00
lc . GetPanelState ( ) . SetSelectedLineIdx ( selectedLineIdx )
2020-08-19 14:27:31 +02:00
return lc . HandleFocus ( )
2020-08-16 01:18:57 +02:00
}