-
Notifications
You must be signed in to change notification settings - Fork 1
/
common.go
77 lines (63 loc) · 2.51 KB
/
common.go
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
package foam
// Package foam provides utilities for styling and managing UI components.
// It uses the Lipgloss library for styling and offers a structured way to
// handle styles for focused, blurred, and no-border states of UI elements.
import (
"github.com/charmbracelet/lipgloss"
)
// Styles defines the styles for focused, blurred, and no-border
// states of UI elements.
type Styles struct {
Focused lipgloss.Style
Blurred lipgloss.Style
NoBorder lipgloss.Style
}
// Common encapsulates common properties for UI components, including
// width, height, and styles.
type Common struct {
width, height int
styles *Styles
}
// DefaultStyles returns a new Styles struct with default styles for
// focused, blurred, and no-border states, leveraging Lipgloss for
// styling.
func DefaultStyles() *Styles {
return &Styles{
Focused: lipgloss.NewStyle().Border(lipgloss.RoundedBorder()).BorderForeground(lipgloss.Color("5")),
Blurred: lipgloss.NewStyle().Border(lipgloss.RoundedBorder()),
NoBorder: lipgloss.NewStyle(),
}
}
// GetWidth returns the current width of the UI component.
func (c *Common) GetWidth() int { return c.width }
// GetHeight returns the current height of the UI component.
func (c *Common) GetHeight() int { return c.height }
// SetWidth sets the width of the UI component.
func (c *Common) SetWidth(width int) {
c.width = width - c.styles.Focused.GetHorizontalFrameSize()
c.styles.Focused = c.styles.Focused.Width(c.width)
c.styles.Blurred = c.styles.Blurred.Width(c.width)
}
// SetHeight sets the height of the UI component.
func (c *Common) SetHeight(height int) {
// c.height = height
c.height = height - c.styles.Focused.GetHorizontalFrameSize()
c.styles.Focused = c.styles.Focused.Height(c.height)
c.styles.Blurred = c.styles.Blurred.Height(c.height)
}
// SetSize sets the width and height of the UI component, adjusting
// the focused style width to account for the border size.
func (c *Common) SetSize(width int, height int) {
c.width = width - c.styles.Focused.GetHorizontalFrameSize()
c.height = height - c.styles.Focused.GetVerticalFrameSize()
c.styles.Focused = c.styles.Focused.Width(c.width)
c.styles.Focused = c.styles.Focused.Height(c.height)
c.styles.Blurred = c.styles.Blurred.Width(c.width)
c.styles.Blurred = c.styles.Blurred.Height(c.height)
}
// GetStyles returns the current styles of the UI component.
func (c *Common) GetStyles() *Styles { return c.styles }
// SetStyles sets the styles of the UI component.
func (c *Common) SetStyles(styles *Styles) {
c.styles = styles
}