/
view.go
132 lines (107 loc) · 2.89 KB
/
view.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package app
import (
"fmt"
"sync"
"github.com/gdamore/tcell/v2"
"github.com/rivo/tview"
)
// view : ページの表示域
type view struct {
flex *tview.Flex
pages *tview.Pages
pageItems map[string]page
tabBar *tview.TextView
tabs []string
currentTabIndex int
modal *tview.Modal
mu sync.Mutex
}
func newView() *view {
v := &view{
flex: tview.NewFlex(),
pages: tview.NewPages(),
pageItems: map[string]page{},
tabBar: tview.NewTextView(),
tabs: []string{},
currentTabIndex: 0,
modal: tview.NewModal(),
}
v.flex.
SetDirection(tview.FlexRow).
AddItem(v.pages, 0, 1, true)
tabBgColor := global.conf.Style.Tab.BackgroundColor.ToColor()
v.tabBar.
SetDynamicColors(true).
SetRegions(true).
SetTextAlign(tview.AlignLeft).
SetHighlightedFunc(v.handleTabHighlight).
SetTextStyle(tcell.StyleDefault.Background(tabBgColor))
v.modal.
AddButtons([]string{"No", "Yes"}).
SetInputCapture(v.handleModalKeyEvent)
return v
}
// SetInputCapture : キーハンドラを設定
func (v *view) SetInputCapture(f func(*tcell.EventKey) *tcell.EventKey) {
v.flex.SetInputCapture(f)
}
// AddPage : ページを追加
func (v *view) AddPage(p page, focus bool) error {
v.mu.Lock()
defer v.mu.Unlock()
newTab := p.GetName()
// ページが重複する場合、既にあるページに移動
if _, ok := v.pageItems[newTab]; ok {
tabIndex, found := find(v.tabs, func(tab string) bool { return tab == newTab })
if !found {
return fmt.Errorf("Failed to add page (%s)", newTab)
}
v.tabBar.Highlight(newTab)
v.currentTabIndex = tabIndex
return fmt.Errorf("This page already exists (%s)", newTab)
}
// ページ・タブを追加
v.pageItems[newTab] = p
v.pages.AddPage(newTab, p.GetPrimitive(), true, focus)
v.addTab(newTab)
// フォーカスが当たっているならタブをハイライト
if focus {
v.tabBar.Highlight(newTab)
v.currentTabIndex = v.pages.GetPageCount() - 1
}
return nil
}
// Reset : リセット
func (v *view) Reset() {
// ページを削除
for id := range v.pageItems {
v.pages.RemovePage(id)
}
v.pageItems = map[string]page{}
// タブを削除
v.removeTab("")
v.tabBar.SetText("")
v.currentTabIndex = 0
}
// CloseCurrentPage : 現在のページを削除
func (v *view) CloseCurrentPage() {
// ページが1つのみなら削除しない
if v.pages.GetPageCount() == 1 {
global.SetErrorStatus("App", "last page cannot be closed")
return
}
id, _ := v.pages.GetFrontPage()
name := v.pageItems[id].GetName()
// タブを削除
v.removeTab(name)
v.drawTab()
// ページを削除
v.pages.RemovePage(id)
v.pageItems[id].OnDelete()
delete(v.pageItems, id)
// 前のタブを選択
if v.currentTabIndex--; v.currentTabIndex < 0 {
v.currentTabIndex = 0
}
v.tabBar.Highlight(v.tabs[v.currentTabIndex])
}