/
main.go
123 lines (103 loc) · 3.26 KB
/
main.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
// This is free and unencumbered software released into the public
// domain. For more information, see <http://unlicense.org> or the
// accompanying UNLICENSE file.
package main
import (
"log"
"path/filepath"
"github.com/nelsam/gxui"
"github.com/nelsam/gxui/drivers/gl"
"github.com/nelsam/gxui/math"
"github.com/nelsam/gxui/themes/basic"
"github.com/nelsam/gxui/themes/dark"
"github.com/nelsam/vidar/command"
"github.com/nelsam/vidar/command/focus"
"github.com/nelsam/vidar/command/input"
"github.com/nelsam/vidar/commander"
"github.com/nelsam/vidar/commander/bind"
"github.com/nelsam/vidar/controller"
"github.com/nelsam/vidar/editor"
"github.com/nelsam/vidar/navigator"
"github.com/nelsam/vidar/plugin"
"github.com/nelsam/vidar/setting"
"github.com/nelsam/vidar/theme"
"github.com/spf13/cobra"
)
var (
background = gxui.Gray10
cmd *cobra.Command
files []string
)
func init() {
cmd = &cobra.Command{
Use: "vidar [files...]",
Short: "An experimental Go editor",
Long: "An editor for Go code, still in its infancy. " +
"Basic editing of Go code is mostly complete, but " +
"panics still happen and can result in the loss of " +
"unsaved work.",
Run: func(cmd *cobra.Command, args []string) {
files = args
gl.StartDriver(uiMain, gl.Debug())
},
}
}
func main() {
cmd.Execute()
}
func uiMain(driver gxui.Driver) {
gTheme := dark.CreateTheme(driver).(*basic.Theme)
font := setting.PrefFont(driver)
if font == nil {
font = gTheme.DefaultMonospaceFont()
}
gTheme.SetDefaultMonospaceFont(font)
gTheme.SetDefaultFont(font)
gTheme.WindowBackground = background
// TODO: figure out a better way to get this resolution
window := newWindow(gTheme)
controller := controller.New(driver, gTheme)
// Bindings should be added immediately after creating the commander,
// since other types rely on the bindings having been bound.
cmdr := commander.New(driver, gTheme, window, controller)
window.child = cmdr
bindings := []bind.Bindable{input.New(driver, cmdr)}
bindings = append(bindings, command.Bindables(cmdr, driver, gTheme)...)
bindings = append(bindings, plugin.Bindables(cmdr, driver, gTheme)...)
cmdr.Push(bindings...)
nav := navigator.New(driver, gTheme)
controller.SetNavigator(nav)
editor := editor.New(driver, window, cmdr, gTheme, theme.Default, gTheme.DefaultMonospaceFont())
controller.SetEditor(editor)
projTree := navigator.NewProjectTree(cmdr, driver, window, gTheme)
projects := navigator.NewProjectsPane(cmdr, driver, gTheme, projTree.Frame())
nav.Add(projects)
nav.Add(projTree)
nav.Resize(window.Size().H)
window.OnResize(func() {
nav.Resize(window.Size().H)
})
// TODO: Check the system's DPI settings for this value
window.SetScale(1)
window.AddChild(cmdr)
window.OnKeyDown(func(event gxui.KeyboardEvent) {
if window.Focus() == nil {
cmdr.KeyDown(event)
}
})
window.OnKeyUp(func(event gxui.KeyboardEvent) {
if window.Focus() == nil {
cmdr.KeyPress(event)
}
})
opener := cmdr.Bindable("focus-location").(*focus.Location)
for _, file := range files {
filepath, err := filepath.Abs(file)
if err != nil {
log.Printf("Failed to get path: %s", err)
}
cmdr.Execute(opener.For(focus.Path(filepath)))
}
window.OnClose(driver.Terminate)
window.SetPadding(math.Spacing{L: 10, T: 10, R: 10, B: 10})
}