From 7e0d64e8bd231de100d13ea8a026dce06aa23a5b Mon Sep 17 00:00:00 2001 From: Sasha Koshka Date: Sun, 5 Mar 2023 10:58:27 -0500 Subject: [PATCH] TextBox text is now vertically centered --- elements/basic/textbox.go | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/elements/basic/textbox.go b/elements/basic/textbox.go index 1f99460..98296dc 100644 --- a/elements/basic/textbox.go +++ b/elements/basic/textbox.go @@ -92,11 +92,18 @@ func (element *TextBox) HandleMouseMove (x, y int) { } } -func (element *TextBox) atPosition (position image.Point) int { - padding := element.theme.Padding(theme.PatternInput) - offset := element.Bounds().Min.Add (image.Pt ( +func (element *TextBox) textOffset () image.Point { + padding := element.theme.Padding(theme.PatternInput) + bounds := element.Bounds() + innerBounds := padding.Apply(bounds) + textHeight := element.valueDrawer.LineHeight().Round() + return bounds.Min.Add (image.Pt ( padding[artist.SideLeft] - element.scroll, - padding[artist.SideTop])) + padding[artist.SideTop] + (innerBounds.Dy() - textHeight) / 2)) +} + +func (element *TextBox) atPosition (position image.Point) int { + offset := element.textOffset() textBoundsMin := element.valueDrawer.LayoutBounds().Min return element.valueDrawer.AtPosition ( fixedutil.Pt(position.Sub(offset).Add(textBoundsMin))) @@ -115,8 +122,6 @@ func (element *TextBox) HandleKeyDown(key input.Key, modifiers input.Modifiers) return } - // TODO: text selection with shift - scrollMemory := element.scroll altered := true textChanged := false @@ -358,11 +363,7 @@ func (element *TextBox) draw () { padding := element.theme.Padding(theme.PatternInput) innerCanvas := canvas.Cut(element.core, padding.Apply(bounds)) pattern.Draw(element.core, bounds) - - offset := bounds.Min.Add (image.Point { - X: padding[artist.SideLeft] - element.scroll, - Y: padding[artist.SideTop], - }) + offset := element.textOffset() if element.Focused() && !element.dot.Empty() { // draw selection bounds