tui/view.go

51 lines
1.6 KiB
Go
Raw Normal View History

2021-01-10 21:52:29 +01:00
package tui
2022-04-02 13:01:41 +02:00
// View defines the behavior of any element displayable on screen.
2022-04-01 20:10:51 +02:00
// To define custom Views, it is recommended to add ViewTmpl
// as the promoted anonymous field for your custom View struct.
// It implements the View interface with useful default behavior
2021-01-10 21:52:29 +01:00
type View interface {
Events
2022-04-02 13:01:41 +02:00
SetStyle(s Style)
2022-04-01 20:10:51 +02:00
Style() Style
2022-04-02 15:09:52 +02:00
// Layout is usually called by the parent view to ask the view's preferred size.
// If the parent view does not care about its preferred size, it might not be called at all.
// Negative values indicate as much space as possible.
2022-04-02 13:01:41 +02:00
Layout() (prefWidth, prefHeight int)
2022-04-02 15:09:52 +02:00
// Draw is called for each view when it should print itself onto the screen.
// The parent view has full control of the ViewBuffer size
// and may or may not use the values returned from Layout() to set the size.
2022-04-02 13:01:41 +02:00
Draw(buf *ViewBuffer)
2022-04-01 20:10:51 +02:00
}
2022-04-02 15:21:17 +02:00
// Layout defines the behavior of a View which can hold multiple sub views
type Layout interface {
2022-04-01 20:10:51 +02:00
View
2022-04-02 13:01:41 +02:00
Views() []View
2022-04-01 20:10:51 +02:00
}
2021-01-10 21:52:29 +01:00
2022-04-02 15:21:17 +02:00
// Wrapper defines the behavior of a Layout which can hold exactly one sub view.
2022-04-01 20:10:51 +02:00
// To define custom Wrappers, it is recommended to add WrapperTmpl
// as the promoted anonymous field for your custom Wrapper struct.
// It implements the Wrapper interface with useful default behavior
type Wrapper interface {
2022-04-02 15:21:17 +02:00
Layout
2021-01-10 21:52:29 +01:00
2022-04-01 20:10:51 +02:00
SetView(View)
View() View
2021-01-10 21:52:29 +01:00
}
2023-04-24 11:41:38 +02:00
// Modal defines the behavior of a Wrapper which captures
// all screen space and all events when opened and can return results.
// It can be used to make dialogs, alert boxes and context menus
type Modal[T any] interface {
Wrapper
Open(s *Screen) <-chan T
Close(result T)
}