/
controller.go
89 lines (74 loc) · 1.84 KB
/
controller.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
// This is free and unencumbered software released into the public
// domain. For more information, see <http://unlicense.org> or the
// accompanying UNLICENSE file.
package controller
import (
"github.com/nelsam/gxui"
"github.com/nelsam/gxui/mixins"
"github.com/nelsam/gxui/themes/basic"
"github.com/nelsam/vidar/commander/text"
)
type Navigator interface {
gxui.Control
}
type MultiEditor interface {
gxui.Control
CurrentFile() string
CurrentEditor() text.Editor
Open(path string) (editor text.Editor, existed bool)
}
type Controller struct {
mixins.LinearLayout
theme *basic.Theme
driver gxui.Driver
font gxui.Font
navigator Navigator
editor MultiEditor
}
func New(driver gxui.Driver, theme *basic.Theme) *Controller {
controller := new(Controller)
controller.Init(driver, theme)
return controller
}
func (c *Controller) Init(driver gxui.Driver, theme *basic.Theme) {
c.LinearLayout.Init(c, theme)
c.driver = driver
c.theme = theme
c.SetDirection(gxui.LeftToRight)
}
// Navigator returns c's Navigator instance.
func (c *Controller) Navigator() Navigator {
return c.navigator
}
func (c *Controller) Elements() []interface{} {
return []interface{}{
c.navigator,
c.editor,
}
}
// SetNavigator sets c's Navigator instance.
func (c *Controller) SetNavigator(navigator Navigator) {
if c.navigator != nil {
c.RemoveChild(c.navigator)
}
c.navigator = navigator
if c.navigator != nil {
c.AddChild(c.navigator)
// Reset the editor, since it should come after the navigator.
c.SetEditor(c.editor)
}
}
// Editor returns c's Editor instance.
func (c *Controller) Editor() MultiEditor {
return c.editor
}
// SetEditor sets c's Editor instance.
func (c *Controller) SetEditor(editor MultiEditor) {
if c.editor != nil {
c.RemoveChild(c.editor)
}
c.editor = editor
if c.editor != nil {
c.AddChild(c.editor)
}
}