Skip to content

Commit

Permalink
Disable form labels for disabled widgets
Browse files Browse the repository at this point in the history
In 2.1 we could return to using Label as RichText would allow colours.
But for now using Text works :).
Fixes fyne-io#1530
  • Loading branch information
andydotxyz committed Jul 22, 2021
1 parent 291e8f9 commit a6357cd
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 17 deletions.
13 changes: 11 additions & 2 deletions layout/formlayout.go
Expand Up @@ -2,6 +2,7 @@ package layout

import (
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/canvas"
"fyne.io/fyne/v2/theme"
)

Expand Down Expand Up @@ -53,6 +54,9 @@ func (f *formLayout) tableCellsSize(objects []fyne.CanvasObject, containerWidth
}

labelCell := currentRow[0].MinSize()
if _, ok := currentRow[0].(*canvas.Text); ok {
labelCell.Width += theme.Padding() * 4
}
labelCellMaxWidth = fyne.Max(labelCellMaxWidth, labelCell.Width)

contentCell := currentRow[1].MinSize()
Expand Down Expand Up @@ -92,8 +96,13 @@ func (f *formLayout) Layout(objects []fyne.CanvasObject, size fyne.Size) {
}

tableRow := table[row]
objects[i].Move(fyne.NewPos(0, y))
objects[i].Resize(fyne.NewSize(tableRow[0].Width, tableRow[0].Height))
if _, ok := objects[i].(*canvas.Text); ok {
objects[i].Move(fyne.NewPos(theme.Padding()*2, y+theme.Padding()*2))
objects[i].Resize(fyne.NewSize(tableRow[0].Width-theme.Padding()*4, objects[i].MinSize().Height))
} else {
objects[i].Move(fyne.NewPos(0, y))
objects[i].Resize(fyne.NewSize(tableRow[0].Width, tableRow[0].Height))
}

if i+1 < len(objects) {
objects[i+1].Move(fyne.NewPos(theme.Padding()+tableRow[0].Width, y))
Expand Down
24 changes: 17 additions & 7 deletions widget/form.go
Expand Up @@ -108,8 +108,11 @@ func (f *Form) createInput(item *FormItem) fyne.CanvasObject {
return fyne.NewContainerWithLayout(layout.NewVBoxLayout(), item.Widget, fyne.NewContainerWithoutLayout(text))
}

func (f *Form) createLabel(text string) *Label {
return NewLabelWithStyle(text, fyne.TextAlignTrailing, fyne.TextStyle{Bold: true})
func (f *Form) createLabel(text string) *canvas.Text {
return &canvas.Text{Text: text,
Alignment: fyne.TextAlignTrailing,
TextSize: theme.TextSize(),
TextStyle: fyne.TextStyle{Bold: true}}
}

func (f *Form) updateButtons() {
Expand Down Expand Up @@ -193,12 +196,18 @@ func (f *Form) updateHelperText(item *FormItem) {

func (f *Form) updateLabels() {
for i, item := range f.Items {
l := f.itemGrid.Objects[i*2].(*Label)
if l.Text == item.Text {
continue
l := f.itemGrid.Objects[i*2].(*canvas.Text)
l.TextSize = theme.TextSize()
if dis, ok := item.Widget.(fyne.Disableable); ok {
if dis.Disabled() {
l.Color = theme.DisabledColor()
} else {
l.Color = theme.ForegroundColor()
}
}

l.SetText(item.Text)
l.Text = item.Text
l.Refresh()
f.updateHelperText(item)
}
}
Expand All @@ -221,7 +230,8 @@ func (f *Form) CreateRenderer() fyne.WidgetRenderer {
f.itemGrid = fyne.NewContainerWithLayout(layout.NewFormLayout(), objects...)

renderer := &simpleRenderer{content: fyne.NewContainerWithLayout(layout.NewVBoxLayout(), f.itemGrid, f.buttonBox)}
f.updateButtons() // will set correct visibility on the submit/cancel btns
f.updateButtons()
f.updateLabels()
f.checkValidation(nil) // will trigger a validation check for correct intial validation status
return renderer
}
Expand Down
22 changes: 20 additions & 2 deletions widget/form_test.go
Expand Up @@ -5,6 +5,7 @@ import (
"testing"

"fyne.io/fyne/v2"
"fyne.io/fyne/v2/canvas"
"fyne.io/fyne/v2/data/validation"
"fyne.io/fyne/v2/test"
"fyne.io/fyne/v2/theme"
Expand Down Expand Up @@ -104,11 +105,11 @@ func TestForm_ChangeText(t *testing.T) {

renderer := test.WidgetRenderer(form)
c := renderer.Objects()[0].(*fyne.Container).Objects[0].(*fyne.Container)
assert.Equal(t, "Test", c.Objects[0].(*Label).Text)
assert.Equal(t, "Test", c.Objects[0].(*canvas.Text).Text)

item.Text = "Changed"
form.Refresh()
assert.Equal(t, "Changed", c.Objects[0].(*Label).Text)
assert.Equal(t, "Changed", c.Objects[0].(*canvas.Text).Text)
}

func TestForm_ChangeTheme(t *testing.T) {
Expand All @@ -133,6 +134,23 @@ func TestForm_ChangeTheme(t *testing.T) {
})
}

func TestForm_Disabled(t *testing.T) {
app := test.NewApp()
defer test.NewApp()
app.Settings().SetTheme(theme.LightTheme())

disabled := NewEntry()
disabled.Disable()
f := NewForm(
NewFormItem("Form Item 1", NewEntry()),
NewFormItem("Form Item 2", disabled))

w := test.NewWindow(f)
defer w.Close()

test.AssertImageMatches(t, "form/disabled.png", w.Canvas().Capture())
}

func TestForm_Hints(t *testing.T) {
app := test.NewApp()
defer test.NewApp()
Expand Down
Binary file added widget/testdata/form/disabled.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 2 additions & 6 deletions widget/testdata/form/layout.xml
Expand Up @@ -3,9 +3,7 @@
<widget pos="4,4" size="206x119" type="*widget.Form">
<container size="206x119">
<container size="206x78">
<widget size="55x37" type="*widget.Label">
<text alignment="trailing" bold pos="8,8" size="39x21">test1</text>
</widget>
<text alignment="trailing" bold pos="8,8" size="39x21">test1</text>
<widget pos="59,0" size="147x37" type="*widget.Entry">
<rectangle fillColor="rgba(102,102,102,255)" pos="0,2" size="147x33"/>
<rectangle fillColor="shadow" pos="0,35" size="147x2"/>
Expand All @@ -20,9 +18,7 @@
</widget>
</widget>
</widget>
<widget pos="0,41" size="55x37" type="*widget.Label">
<text alignment="trailing" bold pos="8,8" size="39x21">test2</text>
</widget>
<text alignment="trailing" bold pos="8,49" size="39x21">test2</text>
<widget pos="59,41" size="147x37" type="*widget.Entry">
<rectangle fillColor="rgba(102,102,102,255)" pos="0,2" size="147x33"/>
<rectangle fillColor="shadow" pos="0,35" size="147x2"/>
Expand Down
Binary file modified widget/testdata/form/theme_changed.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit a6357cd

Please sign in to comment.