forked from alperdrsnn/clime
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathresponsive.go
More file actions
309 lines (262 loc) · 6.68 KB
/
responsive.go
File metadata and controls
309 lines (262 loc) · 6.68 KB
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
package clime
import (
"sync"
)
type BreakpointSize int
const (
BreakpointXS BreakpointSize = iota // < 60 chars
BreakpointSM // 60-79 chars
BreakpointMD // 80 - 119 chars
BreakpointLG // 120 - 159 chars
BreakpointXL // >= 160 chars
)
type Breakpoint struct {
Size BreakpointSize
MinWidth int
MaxWidth int
Name string
IsActive bool
}
var (
Breakpoints = []Breakpoint{
{BreakpointXS, 0, 59, "xs", false},
{BreakpointSM, 60, 79, "sm", false},
{BreakpointMD, 80, 119, "md", false},
{BreakpointLG, 120, 159, "lg", false},
{BreakpointXL, 160, 999, "xl", false},
}
)
// ResponsiveManager handles responsive behavior
type ResponsiveManager struct {
terminal *Terminal
currentBreakpoint BreakpointSize
mu sync.RWMutex
}
var globalResponsiveManager *ResponsiveManager
var responsiveOnce sync.Once
// GetResponsiveManager returns the global responsive manager singleton
func GetResponsiveManager() *ResponsiveManager {
responsiveOnce.Do(func() {
globalResponsiveManager = NewResponsiveManager()
})
return globalResponsiveManager
}
// NewResponsiveManager creates a new responsive manager
func NewResponsiveManager() *ResponsiveManager {
rm := &ResponsiveManager{
terminal: NewTerminal(),
}
rm.updateBreakpoint()
return rm
}
// GetCurrentBreakpoint returns the current active breakpoint
func (rm *ResponsiveManager) GetCurrentBreakpoint() BreakpointSize {
rm.mu.RLock()
defer rm.mu.RUnlock()
return rm.currentBreakpoint
}
// GetCurrentBreakpointName returns the current breakpoint name
func (rm *ResponsiveManager) GetCurrentBreakpointName() string {
bp := rm.GetCurrentBreakpoint()
return Breakpoints[bp].Name
}
// IsBreakpoint checks if current breakpoint matches given size
func (rm *ResponsiveManager) IsBreakpoint(size BreakpointSize) bool {
return rm.GetCurrentBreakpoint() == size
}
// IsBreakpointOrLarger checks if current breakpoint is size or larger
func (rm *ResponsiveManager) IsBreakpointOrLarger(size BreakpointSize) bool {
return rm.GetCurrentBreakpoint() >= size
}
// IsBreakpointOrSmaller checks if current breakpoint is size or smaller
func (rm *ResponsiveManager) IsBreakpointOrSmaller(size BreakpointSize) bool {
return rm.GetCurrentBreakpoint() <= size
}
// RefreshBreakpoint manually refreshes the current breakpoint
func (rm *ResponsiveManager) RefreshBreakpoint() {
rm.terminal = NewTerminal()
rm.updateBreakpoint()
}
// updateBreakpoint updates the current breakpoint based on terminal width
func (rm *ResponsiveManager) updateBreakpoint() {
width := rm.terminal.Width()
var newBreakpoint BreakpointSize
for i, bp := range Breakpoints {
if width >= bp.MinWidth && width <= bp.MaxWidth {
newBreakpoint = BreakpointSize(i)
break
}
}
rm.mu.Lock()
rm.currentBreakpoint = newBreakpoint
for i := range Breakpoints {
Breakpoints[i].IsActive = i == int(newBreakpoint)
}
rm.mu.Unlock()
}
// ResponsiveConfig holds responsive configuration for elements
type ResponsiveConfig struct {
XS *ElementConfig
SM *ElementConfig
MD *ElementConfig
LG *ElementConfig
XL *ElementConfig
}
// ElementConfig defines element configuration per breakpoint
type ElementConfig struct {
Width *int
Height *int
Padding *int
Margin *int
ShowFull bool
Compact bool
}
// GetConfigForBreakpoint returns the appropriate config for current breakpoint
func (rc *ResponsiveConfig) GetConfigForBreakpoint(bp BreakpointSize) *ElementConfig {
switch bp {
case BreakpointXS:
if rc.XS != nil {
return rc.XS
}
case BreakpointSM:
if rc.SM != nil {
return rc.SM
}
if rc.XS != nil {
return rc.XS
}
case BreakpointMD:
if rc.MD != nil {
return rc.MD
}
if rc.SM != nil {
return rc.SM
}
if rc.XS != nil {
return rc.XS
}
case BreakpointLG:
if rc.LG != nil {
return rc.LG
}
if rc.MD != nil {
return rc.MD
}
if rc.SM != nil {
return rc.SM
}
if rc.XS != nil {
return rc.XS
}
case BreakpointXL:
if rc.XL != nil {
return rc.XL
}
if rc.LG != nil {
return rc.LG
}
if rc.MD != nil {
return rc.MD
}
if rc.SM != nil {
return rc.SM
}
if rc.XS != nil {
return rc.XS
}
}
return nil
}
// SmartWidth sizing functions
func SmartWidth(percentage float64) int {
rm := GetResponsiveManager()
terminalWidth := rm.terminal.Width()
baseWidth := int(float64(terminalWidth) * percentage)
switch rm.GetCurrentBreakpoint() {
case BreakpointXS:
return min(baseWidth, terminalWidth-2)
case BreakpointSM:
return min(baseWidth, terminalWidth-4)
case BreakpointMD:
return min(baseWidth, terminalWidth-8)
case BreakpointLG:
return min(baseWidth, terminalWidth-12)
case BreakpointXL:
maxWidth := min(terminalWidth-16, 120)
return min(baseWidth, maxWidth)
}
return baseWidth
}
// SmartPadding returns appropriate padding based on screen size
func SmartPadding() int {
rm := GetResponsiveManager()
switch rm.GetCurrentBreakpoint() {
case BreakpointXS:
return 0
case BreakpointSM:
return 1
case BreakpointMD:
return 1
case BreakpointLG:
return 2
case BreakpointXL:
return 2
}
return 1
}
// SmartMargin returns appropriate margin based on screen size
func SmartMargin() int {
rm := GetResponsiveManager()
switch rm.GetCurrentBreakpoint() {
case BreakpointXS:
return 1
case BreakpointSM:
return 2
case BreakpointMD:
return 4
case BreakpointLG:
return 6
case BreakpointXL:
return 8
}
return 4
}
func IsXS() bool { return GetResponsiveManager().IsBreakpoint(BreakpointXS) }
func IsSM() bool { return GetResponsiveManager().IsBreakpoint(BreakpointSM) }
func IsMD() bool { return GetResponsiveManager().IsBreakpoint(BreakpointMD) }
func IsLG() bool { return GetResponsiveManager().IsBreakpoint(BreakpointLG) }
func IsXL() bool { return GetResponsiveManager().IsBreakpoint(BreakpointXL) }
func IsMDOrLarger() bool { return GetResponsiveManager().IsBreakpointOrLarger(BreakpointMD) }
func IsSMOrSmaller() bool { return GetResponsiveManager().IsBreakpointOrSmaller(BreakpointSM) }
// Utility functions
func min(a, b int) int {
if a < b {
return a
}
return b
}
// GetOptimalColumns returns optimal number of columns for current screen size
func GetOptimalColumns(contentWidth int) int {
rm := GetResponsiveManager()
availableWidth := rm.terminal.Width() - SmartMargin()*2
if contentWidth <= 0 {
contentWidth = 20
}
columns := availableWidth / (contentWidth + 2)
if columns < 1 {
columns = 1
}
switch rm.GetCurrentBreakpoint() {
case BreakpointXS:
return min(columns, 1)
case BreakpointSM:
return min(columns, 2)
case BreakpointMD:
return min(columns, 3)
case BreakpointLG:
return min(columns, 4)
case BreakpointXL:
return min(columns, 6)
}
return columns
}