aboutsummaryrefslogtreecommitdiff
path: root/ui/tcell/window.go
blob: d27808257c3e26ca7cd20f153c97016837895233 (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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
package uitcell

import (
	"fmt"
	"strings"

	"github.com/gdamore/tcell"
	"github.com/prodhe/poe/editor"
)

// Window is a tagline and a body with an optional underlying file on disk. It is the main component and handles all events apart from the system wide shortcuts.
type Window struct {
	x, y, w, h int
	bufid      int64
	body       *View
	tagline    *View
	col        *Column // reference to the column where in
	collapsed  bool    // not implemented
	qcnt       int     // quit count
}

// NewWindow returns a fresh window associated with the given filename. For special case filenames, look at the package constants.
func NewWindow(id int64) *Window {
	win := &Window{
		bufid: id,
		body: &View{
			text:         ed.Buffer(id),
			what:         ViewBody,
			style:        bodyStyle,
			cursorStyle:  bodyCursorStyle,
			hilightStyle: bodyHilightStyle,
			tabstop:      4,
			focused:      true,
		},
		tagline: &View{
			text:         &editor.Buffer{},
			what:         ViewTagline,
			style:        tagStyle,
			cursorStyle:  tagCursorStyle,
			hilightStyle: tagHilightStyle,
			tabstop:      4,
		},
	}

	fmt.Fprintf(win.tagline, "%s ",
		win.TagName(),
	)

	return win
}

func AllWindows() []*Window {
	var ws []*Window
	for _, col := range workspace.cols {
		ws = append(ws, col.windows...)
	}
	return ws
}

// FindWindow searches for the given name in current windows and returns a pointer if one already exists. Nil otherwise. Name assumes to be an absolute path.
func FindWindow(name string) *Window {
	for _, win := range AllWindows() {
		if win.Name() == name {
			return win
		}
	}
	return nil
}

// Resize will set new values for position and width height. Meant to be used on a resize event for proper recalculation during the Draw().
func (win *Window) Resize(x, y, w, h int) {
	win.x, win.y, win.w, win.h = x, y, w, h

	win.tagline.Resize(win.x+3, win.y, win.w-3, 1) // 3 for tagbox
	win.body.Resize(win.x, win.y+1, win.w, win.h-1)
}

func (win *Window) Focus() {
	win.body.focused = true
	//win.tagline.focused = true
}

func (win *Window) UnFocus() {
	win.body.focused = false
	win.tagline.focused = false
}

func (win *Window) Name() string {
	return win.body.text.Name()
}

func (win *Window) TagName() string {
	return strings.TrimPrefix(win.body.text.Name(), ed.WorkDir())
}

func (win *Window) Dir() string {
	d := win.body.text.WorkDir()
	if d == "." {
		return ed.WorkDir() // base path for program
	}
	return d
}

func (win *Window) Flags() [2]rune {
	flags := [2]rune{' ', '-'}
	if win.body.Dirty() {
		flags[0] = '\''
	}
	if win.collapsed {
		flags[1] = '+'
	}
	return flags
}

func (win *Window) HandleEvent(ev tcell.Event) {
	switch ev := ev.(type) {
	case *tcell.EventMouse:
		_, my := ev.Position()

		// Set focus to either tagline or body
		if my > win.tagline.y+win.tagline.h-1 {
			win.tagline.focused = false
		} else {
			win.tagline.focused = true
		}
	case *tcell.EventKey:
		switch ev.Key() {
		case tcell.KeyCtrlS: // save
			_, err := win.body.text.SaveFile()
			if err != nil {
				printMsg("%s\n", err)
			}
			return
		}
	}

	// Pass along the event down to current view, if we have not already done something and returned in the switch above.
	if win.tagline.focused {
		win.tagline.HandleEvent(ev)
	} else {
		win.body.HandleEvent(ev)
	}
}

func (win *Window) Draw() {
	flags := win.Flags()
	screen.SetContent(win.x, win.y, flags[0], nil, win.tagline.style)
	screen.SetContent(win.x+1, win.y, flags[1], nil, win.tagline.style)
	screen.SetContent(win.x+2, win.y, ' ', nil, win.tagline.style)

	// Tagline
	win.tagline.Draw()

	// Main text buffer
	win.body.Draw()
}

func (win *Window) CanClose() bool {
	if win.body.what == ViewScratch {
		return true
	}
	ok := (!win.body.Dirty() || win.qcnt > 0)
	if !ok {
		name := win.Name()
		if name == FnEmptyWin {
			name = "unnamed buffer"
		}
		printMsg("%s modified\n", name)
		win.qcnt++
	}
	return ok
}

func (win *Window) Close() {
	if !win.CanClose() {
		return
	}
	ed.CloseBuffer(win.bufid)
	win.col.CloseWindow(win)
}