-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdefault_layout.go
More file actions
123 lines (110 loc) · 3.66 KB
/
default_layout.go
File metadata and controls
123 lines (110 loc) · 3.66 KB
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
package main
import (
"fmt"
"path/filepath"
"github.com/gdamore/tcell/v2"
. "github.com/JaMo42/spellcheck_comments/common"
. "github.com/JaMo42/spellcheck_comments/source_file"
"github.com/JaMo42/spellcheck_comments/tui"
"github.com/JaMo42/spellcheck_comments/util"
)
type DefaultLayout struct {
highlight tui.SliceIndex
bottomStatus bool
source tui.TextBufferView
pmenu tui.Menu
menuContainer tui.MenuContainer
globalKeys tui.Dock
statusBar tui.StatusBar
suggestionCount int
}
func (self *DefaultLayout) Configure(cfg *Config) {
self.bottomStatus = cfg.General.BottomStatus
self.suggestionCount = cfg.General.Suggestions
}
func (self *DefaultLayout) SetSource(sf *SourceFile) {
self.source.SetTextBuffer(sf.Text())
self.statusBar.SetLeft(filepath.Clean(sf.Name()))
}
func (self *DefaultLayout) Show(index tui.SliceIndex) {
self.source.ScrollTo(index.Line(), 5, false)
tb := self.source.Text()
slice := tb.GetSlice(index)
x, y := self.source.SlicePosition(index)
if !self.bottomStatus {
y--
}
self.menuContainer.SetMenuPosition(x, y, slice.Width()).Then(func(above int) {
self.source.ScrollTo(index.Line(), above, true)
})
self.highlight = index
}
func (self *DefaultLayout) SetSuggestions(suggestions []string) {
self.pmenu.SetItems(suggestions)
}
func (self *DefaultLayout) ArrowReceiver() tui.ArrowReceiver {
return &self.pmenu
}
func (self *DefaultLayout) MouseReceivers() []tui.MouseReceiver {
// Note: the order is intentional here, if they end up overlapping the last
// element in this list will take "precedence" for motion drawing and click
// order. Note that this is not a layering system but just how the tui is
// implemented.
return []tui.MouseReceiver{&self.globalKeys, &self.pmenu}
}
func (self *DefaultLayout) Create() {
const columnCount = 2
globalControls := globalControls()
self.source = tui.NewTextBufferView()
topGap := 0
if !self.bottomStatus {
topGap = 1
}
rows := util.CeilDiv(self.suggestionCount, columnCount)
self.pmenu = tui.NewMenu(tui.MenuLocation.Below, rows, columnCount, topGap)
self.menuContainer = tui.NewMenuContainer()
self.menuContainer.SetMenu(&self.pmenu)
self.globalKeys = tui.NewDock(tui.Alignment.End, tui.Alignment.End, 1, 0, len(globalControls))
self.globalKeys.SetPermanentItems(globalControls)
self.pmenu.TranslateAction(func(_, item int) any {
return ActionSelectSuggestion{item}
})
self.globalKeys.TranslateAction(func(_, item int) any {
return globalControls[item].Action()
})
self.statusBar = tui.NewStausBar()
self.statusBar.SetRight(fmt.Sprintf("%s %s", appName, appVersion))
}
func (self *DefaultLayout) Layout(width, height int) {
screen := tui.NewRectangle(0, 0, width, height-1)
if !self.bottomStatus {
screen.Y++
self.statusBar.Viewport(0, width)
} else {
self.statusBar.Viewport(height-1, width)
}
self.source.SetViewport(screen)
self.menuContainer.SetViewport(screen)
self.globalKeys.SetViewport(screen)
self.menuContainer.SetEvade(Some(self.globalKeys.Rect())).Then(func(above int) {
self.source.ScrollTo(self.highlight.Line(), above, true)
})
}
func (self *DefaultLayout) Update(scr tcell.Screen, widget any) {
if widget == nil {
// We only highlight the current slice on demand so we don't need to
// worry about any state.
text := self.source.Text()
text.GetSlice(self.highlight).ReverseColors()
self.source.Redraw(scr)
self.globalKeys.Redraw(scr)
self.pmenu.Redraw(scr)
self.source.UpdateSlice(scr, self.highlight)
text.GetSlice(self.highlight).ReverseColors()
self.statusBar.Redraw(scr)
} else if widget == &self.pmenu {
self.pmenu.Redraw(scr)
} else if widget == &self.globalKeys {
self.globalKeys.Redraw(scr)
}
}