Debian Codegraph @

center.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 type FractionalCenterer struct {
15 center *Centerer
16 minWidth int
17 minHeight int
18 fractionWidth float64
19 fractionHeight float64
20 }
21
22 func FractionalCenter(target Component, minWidth, minHeight int, fractionalWidth, fractionalHeight float64) *FractionalCenterer {
23 return &FractionalCenterer{
24 center: Center(target, 0, 0),
25 minWidth: minWidth,
26 minHeight: minHeight,
27 fractionWidth: fractionalWidth,
28 fractionHeight: fractionalHeight,
29 }
30 }
31
32 func (fc *FractionalCenterer) SetAlwaysFocusChild(always bool) *FractionalCenterer {
33 fc.center.alwaysFocusChild = always
34 return fc
35 }
36
37 func (fc *FractionalCenterer) Blur() { fc.center.Blur() }
38 func (fc *FractionalCenterer) Focus() { fc.center.Focus() }
39
40 func (fc *FractionalCenterer) OnMouseEvent(evt MouseEvent) bool { return fc.center.OnMouseEvent(evt) }
41 func (fc *FractionalCenterer) OnKeyEvent(evt KeyEvent) bool { return fc.center.OnKeyEvent(evt) }
42 func (fc *FractionalCenterer) OnPasteEvent(evt PasteEvent) bool { return fc.center.OnPasteEvent(evt) }
43
44 func (fc *FractionalCenterer) Draw(screen Screen) {
45 width, height := screen.Size()
46 width = int(float64(width) * fc.fractionWidth)
47 height = int(float64(height) * fc.fractionHeight)
48 if width < fc.minWidth {
49 width = fc.minWidth
50 }
51 if height < fc.minHeight {
52 height = fc.minHeight
53 }
54 fc.center.SetSize(width, height)
55 fc.center.Draw(screen)
56 }
57
58 type Centerer struct {
59 target Component
60 screen *ProxyScreen
61 childFocused bool
62 alwaysFocusChild bool
63 }
64
65 func Center(target Component, width, height int) *Centerer {
66 return &Centerer{
67 target: target,
68 screen: &ProxyScreen{Style: tcell.StyleDefault, Width: width, Height: height},
69 childFocused: false,
70 alwaysFocusChild: false,
71 }
72 }
73
74 func (center *Centerer) SetHeight(height int) {
75 center.screen.Height = height
76 }
77
78 func (center *Centerer) SetWidth(width int) {
79 center.screen.Width = width
80 }
81
82 func (center *Centerer) SetSize(width, height int) {
83 center.screen.Width = width
84 center.screen.Height = height
85 }
86
87 func (center *Centerer) SetAlwaysFocusChild(always bool) *Centerer {
88 center.alwaysFocusChild = always
89 return center
90 }
91
92 func (center *Centerer) Draw(screen Screen) {
93 totalWidth, totalHeight := screen.Size()
94 paddingX := (totalWidth - center.screen.Width) / 2
95 paddingY := (totalHeight - center.screen.Height) / 2
96 if paddingX >= 0 {
97 center.screen.OffsetX = paddingX
98 }
99 if paddingY >= 0 {
100 center.screen.OffsetY = paddingY
101 }
102 center.screen.Parent = screen
103 center.target.Draw(center.screen)
104 }
105
106 func (center *Centerer) OnKeyEvent(evt KeyEvent) bool {
107 return center.target.OnKeyEvent(evt)
108 }
109
110 func (center *Centerer) Focus() {
111 if center.alwaysFocusChild {
112 center.childFocused = true
113 focusable, ok := center.target.(Focusable)
114 if ok {
115 focusable.Focus()
116 }
117 }
118 }
119
120 func (center *Centerer) Blur() {
121 center.childFocused = false
122 focusable, ok := center.target.(Focusable)
123 if ok {
124 focusable.Blur()
125 }
126 }
127
128 func (center *Centerer) OnMouseEvent(evt MouseEvent) bool {
129 x, y := evt.Position()
130 x -= center.screen.OffsetX
131 y -= center.screen.OffsetY
132 focusable, ok := center.target.(Focusable)
133 if x < 0 || y < 0 || x > center.screen.Width || y > center.screen.Height {
134 if ok && evt.Buttons() == tcell.Button1 && !evt.HasMotion() {
135 if center.alwaysFocusChild && !center.childFocused {
136 focusable.Focus()
137 center.childFocused = true
138 } else if !center.alwaysFocusChild && center.childFocused {
139 center.Blur()
140 }
141 return true
142 }
143 return false
144 }
145 focusChanged := false
146 if ok && !center.childFocused && evt.Buttons() == tcell.Button1 && !evt.HasMotion() {
147 focusable.Focus()
148 center.childFocused = true
149 focusChanged = true
150 }
151 return center.target.OnMouseEvent(OffsetMouseEvent(evt, -center.screen.OffsetX, -center.screen.OffsetY)) || focusChanged
152 }
153
154 func (center *Centerer) OnPasteEvent(evt PasteEvent) bool {
155 return center.target.OnPasteEvent(evt)
156 }