Repository navigation
Expand file tree
/
Copy pathfit.go
More file actions
57 lines (52 loc) · 1.39 KB
/
Copy pathfit.go
File metadata and controls
57 lines (52 loc) · 1.39 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
package ntvideo
import (
tea "charm.land/bubbletea/v2"
"nimbleterminal.dev/ntpicture"
)
// FitMode controls how video occupies its cell rectangle. Crops are centered.
type FitMode int8
const (
FitContain FitMode = iota // Preserve aspect ratio, with letterboxing (default).
FitCover // Preserve aspect ratio, cropping to fill the rectangle.
FitFill // Stretch to fill the rectangle.
)
func normalizeFit(f FitMode) FitMode {
if f < FitContain || f > FitFill {
return FitContain
}
return f
}
func (f FitMode) String() string {
switch normalizeFit(f) {
case FitCover:
return "cover"
case FitFill:
return "fill"
default:
return "contain"
}
}
func (f FitMode) pictureFit() ntpicture.FitMode {
switch normalizeFit(f) {
case FitCover:
return ntpicture.FitCover
case FitFill:
return ntpicture.FitFill
default:
return ntpicture.FitContain
}
}
func WithFit(f FitMode) Option { return func(c *config) { c.fit = normalizeFit(f) } }
func (m *Model) Fit() FitMode { return m.cfg.fit }
// SetFit changes presentation without restarting playback. Run its command to
// redraw an existing native frame, including while paused.
func (m *Model) SetFit(f FitMode) tea.Cmd {
f = normalizeFit(f)
if m.closed || f == m.cfg.fit {
return nil
}
m.cfg.fit = f
m.epoch++
m.engine.enqueue(func(b backend) error { return b.SetFit(f) })
return m.pic.SetFit(f.pictureFit())
}