restructure-config #8

Merged
sashakoshka merged 15 commits from restructure-config into main 2023-02-08 19:07:08 -07:00
18 changed files with 333 additions and 193 deletions
Showing only changes of commit a0e57921a4 - Show all commits

View File

@ -165,6 +165,7 @@ func (drawer *TextDrawer) LineHeight () (height fixed.Int26_6) {
// have its maximum width set to the given width. This does not alter the // have its maximum width set to the given width. This does not alter the
// drawer's state. // drawer's state.
func (drawer *TextDrawer) ReccomendedHeightFor (width int) (height int) { func (drawer *TextDrawer) ReccomendedHeightFor (width int) (height int) {
if drawer.face == nil { return }
if !drawer.layoutClean { drawer.recalculate() } if !drawer.layoutClean { drawer.recalculate() }
metrics := drawer.face.Metrics() metrics := drawer.face.Metrics()
dot := fixed.Point26_6 { 0, metrics.Height } dot := fixed.Point26_6 { 0, metrics.Height }

View File

@ -50,3 +50,44 @@ func (Default) ScrollVelocity () int {
func (Default) ThemePath () (string) { func (Default) ThemePath () (string) {
return "" return ""
} }
// Wrapped wraps a configuration and uses Default if it is nil.
type Wrapped struct {
Config
}
// Padding returns the amount of internal padding elements should have.
// An element's inner content (such as text) should be inset by this
// amount, in addition to the inset returned by the pattern of its
// background.
func (wrapped Wrapped) Padding () int {
return wrapped.ensure().Padding()
}
// Margin returns how much space should be put in between elements.
func (wrapped Wrapped) Margin () int {
return wrapped.ensure().Margin()
}
// HandleWidth returns how large grab handles should typically be. This
// is important for accessibility reasons.
func (wrapped Wrapped) HandleWidth () int {
return wrapped.ensure().HandleWidth()
}
// ScrollVelocity returns how many pixels should be scrolled every time
// a scroll button is pressed.
func (wrapped Wrapped) ScrollVelocity () int {
return wrapped.ensure().ScrollVelocity()
}
// ThemePath returns the directory path to the theme.
func (wrapped Wrapped) ThemePath () string {
return wrapped.ensure().ThemePath()
}
func (wrapped Wrapped) ensure () (real Config) {
real = wrapped.Config
if real == nil { real = Default { } }
return
}

View File

@ -18,18 +18,16 @@ type Button struct {
pressed bool pressed bool
text string text string
config config.Config config config.Wrapped
theme theme.Theme theme theme.Wrapped
c theme.Case
onClick func () onClick func ()
} }
// NewButton creates a new button with the specified label text. // NewButton creates a new button with the specified label text.
func NewButton (text string) (element *Button) { func NewButton (text string) (element *Button) {
element = &Button { element = &Button { }
c: theme.C("basic", "button"), element.theme.Case = theme.C("basic", "button")
}
element.Core, element.core = core.NewCore(element.draw) element.Core, element.core = core.NewCore(element.draw)
element.FocusableCore, element.FocusableCore,
element.focusableControl = core.NewFocusableCore(element.redo) element.focusableControl = core.NewFocusableCore(element.redo)
@ -103,18 +101,19 @@ func (element *Button) SetText (text string) {
// SetTheme sets the element's theme. // SetTheme sets the element's theme.
func (element *Button) SetTheme (new theme.Theme) { func (element *Button) SetTheme (new theme.Theme) {
element.theme = new if new == element.theme.Theme { return }
element.theme.Theme = new
element.drawer.SetFace (element.theme.FontFace ( element.drawer.SetFace (element.theme.FontFace (
theme.FontStyleRegular, theme.FontStyleRegular,
theme.FontSizeNormal, theme.FontSizeNormal))
element.c))
element.updateMinimumSize() element.updateMinimumSize()
element.redo() element.redo()
} }
// SetConfig sets the element's configuration. // SetConfig sets the element's configuration.
func (element *Button) SetConfig (new config.Config) { func (element *Button) SetConfig (new config.Config) {
element.config = new if new == element.config.Config { return }
element.config.Config = new
element.updateMinimumSize() element.updateMinimumSize()
element.redo() element.redo()
} }
@ -141,7 +140,7 @@ func (element *Button) draw () {
Pressed: element.pressed, Pressed: element.pressed,
} }
pattern := element.theme.Pattern(theme.PatternButton, element.c, state) pattern := element.theme.Pattern(theme.PatternButton, state)
artist.FillRectangle(element, pattern, bounds) artist.FillRectangle(element, pattern, bounds)