Browser page: scroll + click coexist via register order fix
- Added Page state (BrowserPage/EditorPage) and SortMode - BrowserLayout with 30 static entries, sort toggle, search placeholder - ListView rows clickable via per-row RegisterClick - Scroll gesture registered at START of ListView.Draw (before clicks) so click gestures registered later are checked first during hit test - Unique IDs for interactive elements: editor_text, browser_list, search_bar - ListView.Interactions() filters out Scroll (registered in Draw, not registerInteraction) - RegisterScroll added to Renderer, called from element Draw methods - lastLineYChan frame loop fixed: only re-layout if value changed - Back icon added, editor status bar shows active filename
This commit is contained in:
+132
-7
@@ -1,7 +1,11 @@
|
||||
package ui
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"image"
|
||||
|
||||
"gioui.org/layout"
|
||||
"gioui.org/op/clip"
|
||||
"gioui.org/unit"
|
||||
)
|
||||
|
||||
@@ -107,6 +111,15 @@ func (i Icon) Draw(gtx layout.Context, r *Renderer) {
|
||||
if img == nil {
|
||||
return
|
||||
}
|
||||
// Register click area if this icon has interactions
|
||||
if cr, ok := r.clicks[i.id]; ok && cr.click != nil {
|
||||
clickClip := clip.Rect{
|
||||
Min: image.Point{X: int(r.toPx(i.region.X)), Y: int(r.toPx(i.region.Y))},
|
||||
Max: image.Point{X: int(r.toPx(i.region.X+i.region.W)), Y: int(r.toPx(i.region.Y+i.region.H))},
|
||||
}.Push(gtx.Ops)
|
||||
cr.click.Add(gtx.Ops)
|
||||
clickClip.Pop()
|
||||
}
|
||||
// Auto-scale: if Size is 0, use region dimensions so the icon fills its region
|
||||
w, h := Dp(0), Dp(0)
|
||||
if i.Size == 0 {
|
||||
@@ -117,12 +130,14 @@ func (i Icon) Draw(gtx layout.Context, r *Renderer) {
|
||||
func (i Icon) ID() string { return i.id }
|
||||
|
||||
// NewIcon creates a visible Icon element.
|
||||
func NewIcon(name string, region Region, size Dp) Icon {
|
||||
func NewIcon(name string, region Region, size Dp, interactions []Interaction) Icon {
|
||||
return Icon{
|
||||
region: region,
|
||||
visible: true,
|
||||
Name: name,
|
||||
Size: size,
|
||||
id: name,
|
||||
region: region,
|
||||
visible: true,
|
||||
interactions: interactions,
|
||||
Name: name,
|
||||
Size: size,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -154,8 +169,9 @@ func (tf TextField) Draw(gtx layout.Context, r *Renderer) {
|
||||
}
|
||||
|
||||
// NewTextField creates a visible multiline TextField.
|
||||
func NewTextField(value string, region Region, wrapWidth Dp, scrollOffset Dp, interactions []Interaction) TextField {
|
||||
func NewTextField(id string, value string, region Region, wrapWidth Dp, scrollOffset Dp, interactions []Interaction) TextField {
|
||||
return TextField{
|
||||
id: id,
|
||||
region: region,
|
||||
visible: true,
|
||||
interactions: interactions,
|
||||
@@ -182,12 +198,99 @@ type ListView struct {
|
||||
Items []ListItem
|
||||
ScrollOffset int
|
||||
Selected int
|
||||
RowFilenames []string // filenames for click navigation
|
||||
}
|
||||
|
||||
func (lv ListView) Region() Region { return lv.region }
|
||||
func (lv ListView) Visible() bool { return lv.visible }
|
||||
func (lv ListView) ID() string { return lv.id }
|
||||
func (lv ListView) Interactions() []Interaction { return lv.interactions }
|
||||
func (lv ListView) Interactions() []Interaction {
|
||||
// ListView registers its scroll in Draw, not via registerInteraction.
|
||||
// Filter out Scroll so registerInteraction only handles Tap.
|
||||
var filtered []Interaction
|
||||
for _, interaction := range lv.interactions {
|
||||
if interaction.Gesture != Scroll {
|
||||
filtered = append(filtered, interaction)
|
||||
}
|
||||
}
|
||||
return filtered
|
||||
}
|
||||
|
||||
// Draw renders each list item as a row of text.
|
||||
func (lv ListView) Draw(gtx layout.Context, r *Renderer) {
|
||||
// Register scroll gesture first, so click gestures registered later are checked first
|
||||
if len(lv.interactions) > 0 {
|
||||
var scrollHandler func(any)
|
||||
for _, interaction := range lv.interactions {
|
||||
if interaction.Gesture == Scroll {
|
||||
scrollHandler = interaction.Handler
|
||||
break
|
||||
}
|
||||
}
|
||||
if scrollHandler != nil {
|
||||
r.RegisterScroll(gtx, lv.id, lv.region, scrollHandler)
|
||||
}
|
||||
}
|
||||
rowHeight := Dp(48)
|
||||
for i, item := range lv.Items {
|
||||
// Only draw visible rows (virtualized)
|
||||
y := lv.region.Y + Dp(i)*rowHeight
|
||||
if y+rowHeight < lv.region.Y || y > lv.region.Y+lv.region.H {
|
||||
continue
|
||||
}
|
||||
// Draw background for selected item
|
||||
if item.Selected || (lv.Selected == i) {
|
||||
r.drawBg(gtx, Region{
|
||||
X: lv.region.X, Y: lv.region.Y + Dp(i)*rowHeight,
|
||||
W: lv.region.W, H: rowHeight,
|
||||
}, Color{R: 200, G: 220, B: 255, A: 255})
|
||||
}
|
||||
// Register click area for this row
|
||||
rowGlobalIndex := lv.ScrollOffset + i
|
||||
rowID := fmt.Sprintf("list_row_%d", rowGlobalIndex)
|
||||
var filename string
|
||||
if rowGlobalIndex < len(lv.RowFilenames) {
|
||||
filename = lv.RowFilenames[rowGlobalIndex]
|
||||
}
|
||||
r.RegisterClick(gtx, rowID, Region{
|
||||
X: lv.region.X, Y: y,
|
||||
W: lv.region.W, H: rowHeight,
|
||||
}, func(data any) {
|
||||
OpenFile(filename)
|
||||
})
|
||||
// Draw main text
|
||||
textRegion := Region{
|
||||
X: lv.region.X + Dp(8),
|
||||
Y: lv.region.Y + Dp(i)*rowHeight + Dp(4),
|
||||
W: lv.region.W - Dp(32),
|
||||
H: rowHeight - Dp(8),
|
||||
}
|
||||
r.drawText(gtx, item.Text, 14, textRegion, AlignStart, Color{R: 0, G: 0, B: 0, A: 255}, "")
|
||||
// Draw subtext
|
||||
if item.Subtext != "" {
|
||||
subRegion := Region{
|
||||
X: lv.region.X + Dp(8),
|
||||
Y: lv.region.Y + Dp(i)*rowHeight + rowHeight - Dp(22),
|
||||
W: lv.region.W - Dp(32),
|
||||
H: Dp(16),
|
||||
}
|
||||
r.drawText(gtx, item.Subtext, 12, subRegion, AlignStart, Color{R: 128, G: 128, B: 128, A: 255}, "")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// NewListView creates a visible ListView element.
|
||||
func NewListView(id string, items []ListItem, region Region, scrollOffset, selected int, interactions []Interaction) ListView {
|
||||
return ListView{
|
||||
id: id,
|
||||
region: region,
|
||||
visible: true,
|
||||
interactions: interactions,
|
||||
Items: items,
|
||||
ScrollOffset: scrollOffset,
|
||||
Selected: selected,
|
||||
}
|
||||
}
|
||||
|
||||
// ListItem is a single entry in a ListView.
|
||||
type ListItem struct {
|
||||
@@ -211,6 +314,24 @@ func (ai AlphaIndex) Visible() bool { return ai.visible }
|
||||
func (ai AlphaIndex) ID() string { return ai.id }
|
||||
func (ai AlphaIndex) Interactions() []Interaction { return ai.interactions }
|
||||
|
||||
// Draw renders letters vertically along the right edge.
|
||||
func (ai AlphaIndex) Draw(gtx layout.Context, r *Renderer) {
|
||||
letterHeight := ai.region.H / Dp(len(ai.Letters))
|
||||
for i, letter := range ai.Letters {
|
||||
region := Region{
|
||||
X: ai.region.X,
|
||||
Y: ai.region.Y + Dp(i)*letterHeight,
|
||||
W: ai.region.W,
|
||||
H: letterHeight,
|
||||
}
|
||||
col := Color{R: 100, G: 100, B: 100, A: 255}
|
||||
if letter == ai.ActiveLetter {
|
||||
col = Color{R: 0, G: 100, B: 200, A: 255}
|
||||
}
|
||||
r.drawText(gtx, letter, 10, region, AlignCenter, col, "")
|
||||
}
|
||||
}
|
||||
|
||||
// NewAlphaIndex creates a visible AlphaIndex element.
|
||||
func NewAlphaIndex(region Region, letters []string) AlphaIndex {
|
||||
return AlphaIndex{
|
||||
@@ -381,6 +502,10 @@ func NewSpacer(height Dp) Spacer {
|
||||
|
||||
// --- Utility types ---
|
||||
|
||||
// OpenFile is called by the browser list when a file row is tapped.
|
||||
// Set by the editor package after initialization.
|
||||
var OpenFile func(any)
|
||||
|
||||
// Color is an RGBA color.
|
||||
type Color struct {
|
||||
R, G, B, A uint8
|
||||
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 166 B |
@@ -0,0 +1,3 @@
|
||||
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" width="24" height="24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
|
||||
<polyline points="15 18 9 12 15 6"/>
|
||||
</svg>
|
||||
|
After Width: | Height: | Size: 228 B |
@@ -28,7 +28,7 @@ var iconTemplate = template.Must(template.New("").Parse(`// Code generated by ge
|
||||
package icons
|
||||
|
||||
import (
|
||||
_ "embed"
|
||||
"embed"
|
||||
)
|
||||
|
||||
//go:embed {{.Embeds}}
|
||||
|
||||
+13
-11
@@ -6,9 +6,15 @@ import (
|
||||
"embed"
|
||||
)
|
||||
|
||||
//go:embed copy.svg cut.svg paste.svg
|
||||
//go:embed back.svg copy.svg cut.svg paste.svg
|
||||
var svgFS embed.FS
|
||||
|
||||
// backSVG contains the back icon SVG source.
|
||||
var backSVG = `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" width="24" height="24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
|
||||
<polyline points="15 18 9 12 15 6"/>
|
||||
</svg>
|
||||
`
|
||||
|
||||
// copySVG contains the copy icon SVG source.
|
||||
var copySVG = `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" width="24" height="24">
|
||||
<rect x="7" y="7" width="12" height="14" rx="1" fill="none" stroke="currentColor" stroke-width="1.5"/>
|
||||
@@ -17,16 +23,12 @@ var copySVG = `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" width
|
||||
`
|
||||
|
||||
// cutSVG contains the cut icon SVG source.
|
||||
var cutSVG = `<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
|
||||
<!-- Left blade -->
|
||||
<circle cx="7" cy="7" r="4" />
|
||||
<line x1="9.5" y1="9.5" x2="14" y2="14" />
|
||||
<!-- Right blade -->
|
||||
<circle cx="17" cy="7" r="4" />
|
||||
<line x1="14.5" y1="9.5" x2="10" y2="14" />
|
||||
<!-- Handles -->
|
||||
<line x1="7" y1="3" x2="7" y2="11" />
|
||||
<line x1="17" y1="3" x2="17" y2="11" />
|
||||
var cutSVG = `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" width="24" height="24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
|
||||
<circle cx="6" cy="6" r="3"/>
|
||||
<circle cx="6" cy="18" r="3"/>
|
||||
<path d="M20 4 8.12 15.12"/>
|
||||
<path d="M14.8 14.8 20 20"/>
|
||||
<path d="M8.12 8.12 12 12"/>
|
||||
</svg>
|
||||
`
|
||||
|
||||
|
||||
+46
-13
@@ -72,7 +72,7 @@ func New(th Theme, shp *text.Shaper, scale ScaleProvider) *Renderer {
|
||||
|
||||
// loadIcons loads PNG icons from the embedded filesystem.
|
||||
func (r *Renderer) loadIcons() {
|
||||
for _, name := range []string{"cut", "copy", "paste"} {
|
||||
for _, name := range []string{"back", "cut", "copy", "paste"} {
|
||||
data, err := iconFS.ReadFile("icons/" + name + ".png")
|
||||
if err != nil {
|
||||
continue
|
||||
@@ -151,6 +151,39 @@ func (r *Renderer) registerInteraction(id string, interaction Interaction, gtx l
|
||||
}
|
||||
}
|
||||
|
||||
// RegisterClick registers a click gesture for a rectangular region.
|
||||
// Used by ListView to register per-row click areas. The click.Add() call
|
||||
// is made within a clip so only the region is clickable.
|
||||
func (r *Renderer) RegisterClick(gtx layout.Context, id string, region Region, handler func(any)) {
|
||||
reg, ok := r.clicks[id]
|
||||
if !ok {
|
||||
reg = clickReg{click: &gesture.Click{}}
|
||||
}
|
||||
reg.handler = handler
|
||||
r.clicks[id] = reg
|
||||
// Clip click to row region
|
||||
clickClip := clip.Rect{
|
||||
Min: image.Point{X: int(r.toPx(region.X)), Y: int(r.toPx(region.Y))},
|
||||
Max: image.Point{X: int(r.toPx(region.X + region.W)), Y: int(r.toPx(region.Y + region.H))},
|
||||
}.Push(gtx.Ops)
|
||||
reg.click.Add(gtx.Ops)
|
||||
clickClip.Pop()
|
||||
}
|
||||
|
||||
// RegisterScroll registers a scroll gesture for a rectangular region.
|
||||
// Called by elements during their Draw method, after paint operations.
|
||||
// No clip is pushed — the scroll gesture is registered at the current area,
|
||||
// similar to Gio's layout.List.
|
||||
func (r *Renderer) RegisterScroll(gtx layout.Context, id string, region Region, handler func(any)) {
|
||||
reg, ok := r.scrolls[id]
|
||||
if !ok {
|
||||
reg = scrollReg{scroll: &gesture.Scroll{}}
|
||||
}
|
||||
reg.handler = handler
|
||||
r.scrolls[id] = reg
|
||||
reg.scroll.Add(gtx.Ops)
|
||||
}
|
||||
|
||||
// CheckGestures checks all registered gestures and returns any events.
|
||||
func (r *Renderer) CheckGestures(q input.Source, m unit.Metric) []InputEvent {
|
||||
var events []InputEvent
|
||||
@@ -163,19 +196,19 @@ func (r *Renderer) CheckGestures(q input.Source, m unit.Metric) []InputEvent {
|
||||
})
|
||||
}
|
||||
}
|
||||
for _, reg := range r.scrolls {
|
||||
// gesture.Scroll.Update returns scroll delta in pixels.
|
||||
// ScrollY range: Min = -scrollOffset (remaining above), Max = large (content height unknown yet).
|
||||
// With Min==Max==0, clampSplit consumes zero scroll.
|
||||
delta := reg.scroll.Update(m, q, time.Now(), gesture.Vertical,
|
||||
pointer.ScrollRange{}, pointer.ScrollRange{Min: -(1<<30), Max: 1<<30})
|
||||
if delta != 0 {
|
||||
events = append(events, InputEvent{
|
||||
Handler: reg.handler,
|
||||
Data: delta,
|
||||
})
|
||||
for _, reg := range r.scrolls {
|
||||
// gesture.Scroll.Update returns scroll delta in pixels.
|
||||
// ScrollY range: Min = -scrollOffset (remaining above), Max = large (content height unknown yet).
|
||||
// With Min==Max==0, clampSplit consumes zero scroll.
|
||||
delta := reg.scroll.Update(m, q, time.Now(), gesture.Vertical,
|
||||
pointer.ScrollRange{}, pointer.ScrollRange{Min: -(1<<30), Max: 1<<30})
|
||||
if delta != 0 {
|
||||
events = append(events, InputEvent{
|
||||
Handler: reg.handler,
|
||||
Data: delta,
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
return events
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user