fix: correct text layout and rendering pipeline
- Fix render.go: replace broken material.Label with shaper-based text rendering (LayoutString + Shape + glyph iteration) per Gio's paintGlyph - Fix render.go: only apply clip rects at container boundaries; leaf elements have zero-size regions that were clipping all text out - Fix render.go: call r.drawElement recursively for container children so nested containers work and clips are applied correctly - Fix render.go: add drawLineText/drawLine helpers matching Gio's paintGlyph offset calculation (x + first.X, y + first.Y) - Fix element.go: Button.Draw now uses r.drawText instead of duplicate broken material.Label code; Label defaults to black when color is unset - Fix state.go: container children use relative coordinates instead of mixed screen-space/container-relative positions - Fix main.go: ConfigEvent carries raw pixel dimensions only; ScaleEvent carries scale only; layout is computed once per frame using both, eliminating the infinite Invalidate() loop
This commit is contained in:
+12
-30
@@ -7,10 +7,10 @@ import (
|
||||
)
|
||||
|
||||
// ConfigEvent represents a window configuration change (resize, orientation).
|
||||
// Width and Height are in device-independent pixels (Dp).
|
||||
// PixelWidth and PixelHeight are the raw pixel dimensions from Gio.
|
||||
type ConfigEvent struct {
|
||||
Width ui.Dp
|
||||
Height ui.Dp
|
||||
PixelWidth int
|
||||
PixelHeight int
|
||||
}
|
||||
|
||||
// ScaleEvent represents a metric change (HiDPI scale factor).
|
||||
@@ -25,9 +25,8 @@ type ConfigUpdate interface {
|
||||
}
|
||||
|
||||
func (e ConfigEvent) apply(s *State) {
|
||||
s.ScreenWidth = e.Width
|
||||
s.ScreenHeight = e.Height
|
||||
s.Elems = EditorLayout(e.Width, e.Height)
|
||||
s.PixelWidth = e.PixelWidth
|
||||
s.PixelHeight = e.PixelHeight
|
||||
}
|
||||
|
||||
func (e ScaleEvent) apply(s *State) {
|
||||
@@ -57,10 +56,9 @@ type Logic struct {
|
||||
}
|
||||
|
||||
// NewLogic creates a new Logic instance.
|
||||
// screenWidth and screenHeight are in device-independent pixels (Dp).
|
||||
func NewLogic(screenWidth, screenHeight ui.Dp) *Logic {
|
||||
func NewLogic() *Logic {
|
||||
return &Logic{
|
||||
state: NewState(screenWidth, screenHeight),
|
||||
state: NewState(),
|
||||
configChan: make(chan ConfigUpdate),
|
||||
frameChan: make(chan []ui.Element),
|
||||
inputChan: make(chan []InputEvent),
|
||||
@@ -85,17 +83,10 @@ func (l *Logic) InputChan() chan<- []InputEvent {
|
||||
}
|
||||
|
||||
// ResultChan returns the result channel for the logic goroutine.
|
||||
func (l *Logic) ResultChan() <-chan ResultEvent {
|
||||
func (l *Logic) ResultChan() chan<- ResultEvent {
|
||||
return l.resultChan
|
||||
}
|
||||
|
||||
// ScreenSize returns the current screen dimensions in Dp.
|
||||
func (l *Logic) ScreenSize() (ui.Dp, ui.Dp) {
|
||||
l.mu.Lock()
|
||||
defer l.mu.Unlock()
|
||||
return l.state.ScreenWidth, l.state.ScreenHeight
|
||||
}
|
||||
|
||||
// Scale returns the current scale factor (pixels per DP).
|
||||
func (l *Logic) Scale() float32 {
|
||||
return l.state.Scale()
|
||||
@@ -106,21 +97,12 @@ func (l *Logic) Run() {
|
||||
for {
|
||||
select {
|
||||
case update := <-l.configChan:
|
||||
// Type switch to differentiate ConfigEvent vs ScaleEvent
|
||||
switch e := update.(type) {
|
||||
case ConfigEvent:
|
||||
e.apply(l.state)
|
||||
l.frameChan <- l.state.Elems
|
||||
case ScaleEvent:
|
||||
e.apply(l.state)
|
||||
l.frameChan <- l.state.Elems
|
||||
}
|
||||
update.apply(l.state)
|
||||
l.frameChan <- l.state.layout()
|
||||
case <-l.inputChan:
|
||||
// Process input (not implemented in mockup)
|
||||
l.frameChan <- l.state.Elems
|
||||
l.frameChan <- l.state.layout()
|
||||
case <-l.resultChan:
|
||||
// Handle result (not implemented in mockup)
|
||||
l.frameChan <- l.state.Elems
|
||||
l.frameChan <- l.state.layout()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+30
-28
@@ -1,63 +1,64 @@
|
||||
package editor
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"pad/internal/ui"
|
||||
)
|
||||
|
||||
// State holds all application state owned by the logic goroutine.
|
||||
// All dimensions are in device-independent pixels (Dp).
|
||||
type State struct {
|
||||
ScreenWidth ui.Dp
|
||||
ScreenHeight ui.Dp
|
||||
scale float32 // pixels per DP, default 1.0
|
||||
Elems []ui.Element
|
||||
PixelWidth int // raw pixel width from Gio ConfigEvent
|
||||
PixelHeight int // raw pixel height from Gio ConfigEvent
|
||||
scale float32
|
||||
Elems []ui.Element
|
||||
}
|
||||
|
||||
// NewState creates a new State with initial editor layout.
|
||||
func NewState(screenWidth, screenHeight ui.Dp) *State {
|
||||
return &State{
|
||||
ScreenWidth: screenWidth,
|
||||
ScreenHeight: screenHeight,
|
||||
scale: 1.0,
|
||||
Elems: EditorLayout(screenWidth, screenHeight),
|
||||
}
|
||||
func NewState() *State {
|
||||
return &State{scale: 1.0}
|
||||
}
|
||||
|
||||
// SetScale updates the scale factor and recomputes layout.
|
||||
func (s *State) SetScale(scale float32) {
|
||||
s.scale = scale
|
||||
s.Elems = EditorLayout(s.ScreenWidth, s.ScreenHeight)
|
||||
}
|
||||
|
||||
// Scale returns the current scale factor (pixels per DP).
|
||||
func (s *State) Scale() float32 {
|
||||
return s.scale
|
||||
}
|
||||
|
||||
// layout converts stored pixel dimensions to Dp using the current scale
|
||||
// and computes the element tree. Called only when a frame is needed.
|
||||
func (s *State) layout() []ui.Element {
|
||||
dpW := ui.ToDp(ui.Px(s.PixelWidth), s.scale)
|
||||
dpH := ui.ToDp(ui.Px(s.PixelHeight), s.scale)
|
||||
s.Elems = EditorLayout(dpW, dpH)
|
||||
return s.Elems
|
||||
}
|
||||
|
||||
// EditorLayout computes the element tree for the editor page.
|
||||
// It takes screen dimensions in Dp and returns []Element.
|
||||
func EditorLayout(screenWidth, screenHeight ui.Dp) []ui.Element {
|
||||
margin := ui.Dp(10)
|
||||
|
||||
// --- Top container: status bar ---
|
||||
statusBarHeight := ui.Dp(52)
|
||||
// --- Top bar: filename on row 1, icons on row 2 ---
|
||||
statusBarRegion := ui.Region{
|
||||
X: margin, Y: margin,
|
||||
W: screenWidth - margin*2,
|
||||
H: statusBarHeight,
|
||||
H: ui.Dp(52),
|
||||
}
|
||||
|
||||
statusBarW := statusBarRegion.W
|
||||
statusBar := ui.NewContainer(
|
||||
statusBarRegion,
|
||||
ui.Color{R: 230, G: 230, B: 230, A: 255},
|
||||
[]ui.Element{
|
||||
ui.NewIcon("cut", ui.Region{X: ui.Dp(8), Y: ui.Dp(2), W: ui.Dp(16), H: ui.Dp(16)}, ui.Dp(16)),
|
||||
ui.NewLabel("Ln 47, Col 12", 12, ui.Region{X: ui.Dp(32), Y: ui.Dp(2)}),
|
||||
ui.NewLabel("Ready", 12, ui.Region{X: ui.Dp(8), Y: ui.Dp(28)}),
|
||||
// Row 1: filename centered (relative to container)
|
||||
ui.NewLabel("a very long file name that probably will eventually need to be truncated.txt", 14, ui.Region{X: statusBarW/2 - ui.Dp(40), Y: ui.Dp(2)}),
|
||||
// Row 2: cut icon left (relative to container)
|
||||
ui.NewIcon("cut", ui.Region{X: ui.Dp(8), Y: ui.Dp(28)}, ui.Dp(16)),
|
||||
// Row 2: status icons right (relative to container)
|
||||
ui.NewLabel("Ln 47, Col 12", 12, ui.Region{X: statusBarW - ui.Dp(70), Y: ui.Dp(28)}),
|
||||
},
|
||||
)
|
||||
|
||||
// --- Bottom container: bottom bar ---
|
||||
// --- Bottom bar ---
|
||||
bottomBarHeight := ui.Dp(24)
|
||||
bottomBarY := screenHeight - margin - bottomBarHeight
|
||||
bottomBarRegion := ui.Region{
|
||||
@@ -65,16 +66,17 @@ func EditorLayout(screenWidth, screenHeight ui.Dp) []ui.Element {
|
||||
W: screenWidth - margin*2,
|
||||
H: bottomBarHeight,
|
||||
}
|
||||
|
||||
bottomBarW := bottomBarRegion.W
|
||||
bottomBar := ui.NewContainer(
|
||||
bottomBarRegion,
|
||||
ui.Color{R: 230, G: 230, B: 230, A: 255},
|
||||
[]ui.Element{
|
||||
ui.NewLabel("Ln 47, Col 12", 12, ui.Region{X: ui.Dp(8), Y: ui.Dp(2)}),
|
||||
ui.NewLabel("1024 / 50000", 12, ui.Region{X: ui.Dp(150), Y: ui.Dp(2)}),
|
||||
ui.NewLabel("Wrap: On", 12, ui.Region{X: screenWidth - margin - ui.Dp(60), Y: ui.Dp(2)}),
|
||||
ui.NewLabel("1024 / 50000", 12, ui.Region{X: bottomBarW/2 - ui.Dp(40), Y: ui.Dp(2)}),
|
||||
ui.NewLabel("Wrap: On", 12, ui.Region{X: bottomBarW - ui.Dp(60), Y: ui.Dp(2)}),
|
||||
},
|
||||
)
|
||||
|
||||
fmt.Println("[DEBUG EditorLayout] created", len([]ui.Element{statusBar, bottomBar}), "containers")
|
||||
return []ui.Element{statusBar, bottomBar}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user