-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.go
111 lines (100 loc) · 2.42 KB
/
app.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
package component
import (
"strings"
"github.com/gdamore/tcell/v2"
"github.com/kopecmaciej/mongui/config"
"github.com/kopecmaciej/mongui/manager"
"github.com/kopecmaciej/mongui/mongo"
"github.com/kopecmaciej/tview"
"github.com/rs/zerolog/log"
)
type (
// App is a main application struct
App struct {
*tview.Application
Dao *mongo.Dao
Manager *manager.ComponentManager
Root *Root
FullScreenHelp *Help
FooterHelp *Help
Styles *config.Styles
Config *config.Config
Keys *config.KeyBindings
}
)
func NewApp(appConfig *config.Config) App {
styles, err := config.LoadStyles()
if err != nil {
log.Fatal().Err(err).Msg("Failed to load styles")
}
keyBindings, err := config.LoadKeybindings()
if err != nil {
log.Fatal().Err(err).Msg("Failed to load keybindings")
}
app := App{
Application: tview.NewApplication(),
Root: NewRoot(),
FullScreenHelp: NewHelp(),
FooterHelp: NewHelp(),
Manager: manager.NewComponentManager(),
Styles: styles,
Config: appConfig,
Keys: keyBindings,
}
return app
}
// Init initializes app
func (a *App) Init() error {
a.Root.app = a
if err := a.Root.Init(); err != nil {
return err
}
a.SetRoot(a.Root.Pages, true).EnableMouse(true)
err := a.FullScreenHelp.Init(a)
if err != nil {
return err
}
err = a.FooterHelp.Init(a)
if err != nil {
return err
}
a.setKeybindings()
return a.Run()
}
func (a *App) setKeybindings() {
a.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
// TODO: This is temporary solution
if strings.Contains(string(a.Manager.CurrentComponent()), "Input") {
return event
}
switch {
case a.Keys.Contains(a.Keys.Global.ToggleFullScreenHelp, event.Name()):
if a.Root.HasPage(HelpComponent) {
a.Root.RemovePage(HelpComponent)
return nil
}
err := a.FullScreenHelp.Render()
if err != nil {
return event
}
a.Root.AddPage(HelpComponent, a.FullScreenHelp, true, true)
return nil
case a.Keys.Contains(a.Keys.Global.ToggleHelpBarFooter, event.Name()):
if a.Root.innerFlex.HasItem(a.FooterHelp) {
a.Root.innerFlex.RemoveItem(a.FooterHelp)
return nil
}
err := a.FooterHelp.Render()
if err != nil {
return event
}
a.Root.innerFlex.AddItem(a.FooterHelp, 10, 0, false)
err = a.FooterHelp.Render()
if err != nil {
return event
}
return nil
}
return event
})
}