Debian Codegraph @

screen.go

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
1 // mauview - A Go TUI library based on tcell.
2 // Copyright © 2019 Tulir Asokan
3 //
4 // This Source Code Form is subject to the terms of the Mozilla Public
5 // License, v. 2.0. If a copy of the MPL was not distributed with this
6 // file, You can obtain one at http://mozilla.org/MPL/2.0/.
7
8 package mauview
9
10 import (
•11 "github.com/gdamore/tcell/v2"
12 )
13
14 // Screen is a subset of the tcell Screen.
15 // See https://godoc.org/maunium.net/go/tcell#Screen for documentation.
16 type Screen interface {
17 Clear()
18 Fill(rune, tcell.Style)
19 SetStyle(style tcell.Style)
20 SetCell(x, y int, style tcell.Style, ch ...rune)
21 GetContent(x, y int) (mainc rune, combc []rune, style tcell.Style, width int)
22 SetContent(x int, y int, mainc rune, combc []rune, style tcell.Style)
23 ShowCursor(x int, y int)
24 HideCursor()
25 Size() (int, int)
26 Colors() int
27 CharacterSet() string
28 CanDisplay(r rune, checkFallbacks bool) bool
29 HasKey(tcell.Key) bool
30 }
31
32 // ProxyScreen is a proxy to a tcell Screen with a specific allowed drawing area.
33 type ProxyScreen struct {
34 Parent Screen
35 OffsetX, OffsetY int
36 Width, Height int
37 Style tcell.Style
38 }
39
40 func NewProxyScreen(parent Screen, offsetX, offsetY, width, height int) Screen {
41 return &ProxyScreen{
42 Parent: parent,
43 OffsetX: offsetX,
44 OffsetY: offsetY,
45 Width: width,
46 Height: height,
47 Style: tcell.StyleDefault,
48 }
49 }
50
51 func (ss *ProxyScreen) IsInArea(x, y int) bool {
52 return x >= ss.OffsetX && x <= ss.OffsetX+ss.Width &&
53 y >= ss.OffsetY && y <= ss.OffsetY+ss.Height
54 }
55
56 func (ss *ProxyScreen) YEnd() int {
57 return ss.OffsetY + ss.Height
58 }
59
60 func (ss *ProxyScreen) XEnd() int {
61 return ss.OffsetX + ss.Width
62 }
63
64 func (ss *ProxyScreen) OffsetMouseEvent(event MouseEvent) MouseEvent {
65 return OffsetMouseEvent(event, -ss.OffsetX, -ss.OffsetY)
66 }
67
68 func (ss *ProxyScreen) Clear() {
69 ss.Fill(' ', ss.Style)
70 }
71
72 func (ss *ProxyScreen) Fill(r rune, style tcell.Style) {
73 for x := ss.OffsetX; x < ss.XEnd(); x++ {
74 for y := ss.OffsetY; y < ss.YEnd(); y++ {
75 ss.Parent.SetCell(x, y, style, r)
76 }
77 }
78 }
79
80 func (ss *ProxyScreen) SetStyle(style tcell.Style) {
81 ss.Style = style
82 }
83
84 func (ss *ProxyScreen) adjustCoordinates(x, y int) (int, int, bool) {
85 if x < 0 || y < 0 || (ss.Width >= 0 && x >= ss.Width) || (ss.Height >= 0 && y >= ss.Height) {
86 return -1, -1, false
87 }
88
89 x += ss.OffsetX
90 y += ss.OffsetY
91 return x, y, true
92 }
93
94 func (ss *ProxyScreen) SetCell(x, y int, style tcell.Style, ch ...rune) {
95 x, y, ok := ss.adjustCoordinates(x, y)
96 if ok {
97 ss.Parent.SetCell(x, y, style, ch...)
98 }
99 }
100
101 func (ss *ProxyScreen) GetContent(x, y int) (mainc rune, combc []rune, style tcell.Style, width int) {
102 x, y, ok := ss.adjustCoordinates(x, y)
103 if ok {
104 return ss.Parent.GetContent(x, y)
105 }
106 return 0, nil, tcell.StyleDefault, 0
107 }
108
109 func (ss *ProxyScreen) SetContent(x int, y int, mainc rune, combc []rune, style tcell.Style) {
110 x, y, ok := ss.adjustCoordinates(x, y)
111 if ok {
112 ss.Parent.SetContent(x, y, mainc, combc, style)
113 }
114 }
115
116 func (ss *ProxyScreen) ShowCursor(x, y int) {
117 x, y, ok := ss.adjustCoordinates(x, y)
118 if ok {
119 ss.Parent.ShowCursor(x, y)
120 }
121 }
122
123 func (ss *ProxyScreen) HideCursor() {
124 ss.Parent.HideCursor()
125 }
126
127 // Size returns the size of this subscreen.
128 //
129 // If the subscreen doesn't fit in the parent with the set offset and size,
130 // the returned size is whatever can actually be rendered.
131 func (ss *ProxyScreen) Size() (width int, height int) {
132 width, height = ss.Parent.Size()
133 width -= ss.OffsetX
134 height -= ss.OffsetY
135 if width > ss.Width {
136 width = ss.Width
137 }
138 if height > ss.Height {
139 height = ss.Height
140 }
141 return
142 }
143
144 func (ss *ProxyScreen) Colors() int {
145 return ss.Parent.Colors()
146 }
147
148 func (ss *ProxyScreen) CharacterSet() string {
149 return ss.Parent.CharacterSet()
150 }
151
152 func (ss *ProxyScreen) CanDisplay(r rune, checkFallbacks bool) bool {
153 return ss.Parent.CanDisplay(r, checkFallbacks)
154 }
155
156 func (ss *ProxyScreen) HasKey(key tcell.Key) bool {
157 return ss.Parent.HasKey(key)
158 }