|
16 | 16 | m.top.observeField("minWidth", "onSizeChanged") |
17 | 17 | m.top.observeField("minHeight", "onSizeChanged") |
18 | 18 |
|
| 19 | + ' Enable render tracking on parent to know when component has rendered |
| 20 | + m.top.enableRenderTracking = true |
| 21 | + m.top.observeField("renderTracking", "onRenderComplete") |
| 22 | + |
19 | 23 | applyTheme() |
20 | 24 | end sub |
21 | 25 |
|
|
67 | 71 |
|
68 | 72 | sub onTextChanged() |
69 | 73 | m.buttonText.text = m.top.text |
| 74 | + ' If already rendered, resize immediately. Otherwise wait for onRenderComplete |
| 75 | + if m.top.renderTracking = "full" |
| 76 | + setSizeAndCenterText() |
| 77 | + end if |
| 78 | +end sub |
| 79 | + |
| 80 | +' Called when the component has rendered and we can measure the label accurately |
| 81 | +sub onRenderComplete() |
| 82 | + if m.top.renderTracking <> "full" then return |
70 | 83 | setSizeAndCenterText() |
71 | 84 | end sub |
72 | 85 |
|
73 | 86 | sub setSizeAndCenterText() |
74 | 87 | if not isValid(m.buttonText) then return |
75 | 88 | if m.buttonText.text.Len() = 0 then return |
76 | 89 |
|
77 | | - ' Store the current text to detect if we've already sized for this text |
78 | | - if not isValid(m.lastSizedText) then m.lastSizedText = "" |
79 | | - if m.lastSizedText = m.buttonText.text and isValid(m.top.width) and m.top.width > 0 |
80 | | - ' We've already sized this text, just update the focus border and return |
81 | | - setFocusBorderSize() |
82 | | - return |
83 | | - end if |
84 | | - m.lastSizedText = m.buttonText.text |
| 90 | + ' Reset label size to allow accurate measurement of text content |
| 91 | + m.buttonText.width = 0 |
| 92 | + m.buttonText.height = 0 |
85 | 93 |
|
86 | | - ' Always use font-based calculation to avoid stale localBoundingRect() issues |
87 | | - textLength = m.buttonText.text.Len() |
88 | | - fontSize = m.buttonText.font.size |
89 | | - charWidth = fontSize * 0.64 |
90 | | - textWidth = textLength * charWidth |
| 94 | + ' Get accurate text dimensions from rendered label |
| 95 | + boundingRect = m.buttonText.localBoundingRect() |
| 96 | + textWidth = boundingRect.width |
91 | 97 |
|
92 | | - ' Use font size for height to avoid stale localBoundingRect() issues |
93 | | - textHeight = fontSize |
| 98 | + ' If not rendered yet, wait for onRenderComplete |
| 99 | + if textWidth = 0 then return |
94 | 100 |
|
95 | | - ' print "[TextButton] text='"; m.buttonText.text; "', length="; textLength; ", charWidth="; charWidth; ", textWidth="; textWidth; ", textHeight="; textHeight |
| 101 | + ' Calculate visible text height for equal padding |
| 102 | + ' localBoundingRect height includes line spacing, adjust to match horizontal padding |
| 103 | + fontSize = m.buttonText.font.size |
| 104 | + visibleTextHeight = fontSize * 0.7 |
96 | 105 |
|
97 | 106 | ' Calculate button dimensions with padding |
98 | 107 | buttonWidth = textWidth + (m.top.padding * 2) |
99 | | - buttonHeight = textHeight + (m.top.padding * 2) |
100 | | - |
101 | | - ' print "[TextButton] size with padding: width=", buttonWidth, ", height=", buttonHeight |
| 108 | + buttonHeight = visibleTextHeight + (m.top.padding * 2) |
102 | 109 |
|
103 | 110 | ' Ensure minimum dimensions (this is the ONLY place minWidth should be enforced) |
104 | 111 | if buttonWidth < m.top.minWidth |
|
108 | 115 | buttonHeight = m.top.minHeight |
109 | 116 | end if |
110 | 117 |
|
111 | | - ' print "[TextButton] ACTUAL size (using minWidth): width=", buttonWidth, ", height=", buttonHeight |
112 | | - |
113 | 118 | ' Set background size |
114 | 119 | m.buttonBackground.width = buttonWidth |
115 | 120 | m.buttonBackground.height = buttonHeight |
|
120 | 125 | m.buttonText.translation = [0, 0] |
121 | 126 |
|
122 | 127 | setFocusBorderSize() |
| 128 | + |
| 129 | + ' Show the button now that it's properly sized |
| 130 | + m.buttonBackground.visible = true |
| 131 | + m.buttonBorder.visible = true |
| 132 | + m.buttonText.visible = true |
| 133 | + |
| 134 | + ' Signal that the button is ready (sized and visible) |
| 135 | + m.top.ready = true |
123 | 136 | end sub |
124 | 137 |
|
125 | 138 | sub onPaddingChanged() |
|
0 commit comments