summaryrefslogtreecommitdiffstats
path: root/pkg/gui/basic_context.go
blob: 1db80ee4ac41d3789e995b46e08ba7f69321c8e3 (plain)
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
package gui

type BasicContext struct {
	OnFocus     func(opts ...OnFocusOpts) error
	OnFocusLost func() error
	OnRender    func() error
	// this is for pushing some content to the main view
	OnRenderToMain  func(opts ...OnFocusOpts) error
	Kind            ContextKind
	Key             ContextKey
	ViewName        string
	WindowName      string
	OnGetOptionsMap func() map[string]string

	ParentContext Context
	// we can't know on the calling end whether a Context is actually a nil value without reflection, so we're storing this flag here to tell us. There has got to be a better way around this
	hasParent bool
}

func (self *BasicContext) GetOptionsMap() map[string]string {
	if self.OnGetOptionsMap != nil {
		return self.OnGetOptionsMap()
	}
	return nil
}

func (self *BasicContext) SetParentContext(context Context) {
	self.ParentContext = context
	self.hasParent = true
}

func (self *BasicContext) GetParentContext() (Context, bool) {
	return self.ParentContext, self.hasParent
}

func (self *BasicContext) SetWindowName(windowName string) {
	self.WindowName = windowName
}

func (self *BasicContext) GetWindowName() string {
	windowName := self.WindowName

	if windowName != "" {
		return windowName
	}

	// TODO: actually set this for everything so we don't default to the view name
	return self.ViewName
}

func (self *BasicContext) HandleRender() error {
	if self.OnRender != nil {
		return self.OnRender()
	}
	return nil
}

func (self *BasicContext) GetViewName() string {
	return self.ViewName
}

func (self *BasicContext) HandleFocus(opts ...OnFocusOpts) error {
	if self.OnFocus != nil {
		if err := self.OnFocus(opts...); err != nil {
			return err
		}
	}

	if self.OnRenderToMain != nil {
		if err := self.OnRenderToMain(opts...); err != nil {
			return err
		}
	}

	return nil
}

func (self *BasicContext) HandleFocusLost() error {
	if self.OnFocusLost != nil {
		return self.OnFocusLost()
	}
	return nil
}

func (self *BasicContext) HandleRenderToMain() error {
	if self.OnRenderToMain != nil {
		return self.OnRenderToMain()
	}

	return nil
}

func (self *BasicContext) GetKind() ContextKind {
	return self.Kind
}

func (self *BasicContext) GetKey() ContextKey {
	return self.Key
}