~eliasnaur/gio

1

Re-using Layout

Details
Message ID
<MK0HrAa--3-2@tutanota.com>
DKIM signature
fail
Download raw message
DKIM signature: fail
For example I have form where editor expect some number as user input. 
If entered text is not a number I want set background to red (but text still should be visible).
I can do it, if copying code for layout in both branch:

if !isNumeric {
    bg := Background{
      Color:  color.RGBA{A: 0xff, R: 255, G: 0, B: 0},      Radius: unit.Dp(0),   }   return bg.Layout(gtx, func(gtx layout.Context) layout.Dimensions {      return layout.W.Layout(gtx, func(gtx layout.Context) layout.Dimensions {         return layout.UniformInset(unit.Dp(8)).Layout(gtx, material.Editor(th, input, "").Layout)         })   })}
return layout.W.Layout(gtx, func(gtx layout.Context) layout.Dimensions {
   return layout.UniformInset(unit.Dp(8)).Layout(gtx, material.Editor(th, input, "").Layout)})

But I want to get rid of duplication so I write this one:

usualLayout := layout.W.Layout(gtx, func(gtx layout.Context) layout.Dimensions {
return layout.UniformInset(unit.Dp(8)).Layout(gtx, material.Editor(th, input, "").Layout)
})
if !isNumeric {
   bg := Background{      Color:  color.RGBA{A: 0xff, R: 255, G: 0, B: 0},      Radius: unit.Dp(0),   }   return bg.Layout(gtx, func(gtx layout.Context) layout.Dimensions {      return usualLayout // doesn't work
   })}
return usualLayout

My question does it even possible and if not - why?

Full code:
https://pastebin.com/uhqVfmNA
Details
Message ID
<CAE_4BPCOWkNSkv2Xq3Xm13WDRW4kaA92p-93s=CzwZA-fVOE5g@mail.gmail.com>
In-Reply-To
<MK0HrAa--3-2@tutanota.com> (view parent)
DKIM signature
missing
Download raw message
[ Repost, replying to the list too this time. ]

Here's the bit of the pastebin that I think you meant:

    usualLayout := layout.W.Layout(gtx, func(gtx layout.Context)
layout.Dimensions {
        return layout.UniformInset(unit.Dp(8)).Layout(gtx,
material.Editor(th, input, "").Layout)
    })
    if !isNumeric {
        bg := Background{
            Color:  color.RGBA{A: 0xff, R: 255, G: 0, B: 0},
            Radius: unit.Dp(0),
        }
        return bg.Layout(gtx, func(gtx layout.Context) layout.Dimensions {
            //return usualLayout // doesn't work
            return layout.W.Layout(gtx, func(gtx layout.Context)
layout.Dimensions {
                return layout.UniformInset(unit.Dp(8)).Layout(gtx,
material.Editor(th, input, "").Layout)
            })
        })
    }
    return usualLayout

and the problem is that layout.W.Layout is a function call that
changes the state of your app, so where you call it matters just as
much as where you return its value.

I think you want something more like

    usualLayout := func(gtx layout.Context) layout.Dimension {
        return layout.W.Layout(gtx, func(gtx layout.Context) layout.Dimensions {
            return layout.UniformInset(unit.Dp(8)).Layout(gtx,
material.Editor(th, input, "").Layout)
        })
    }
    if !isNumeric {
        bg := Background{
            Color:  color.RGBA{A: 0xff, R: 255, G: 0, B: 0},
            Radius: unit.Dp(0),
        }
        return bg.Layout(gtx, usualLayout)
    }
    return usualLayout(gtx)

Hope that helps.

-- Larry

On Mon, Oct 19, 2020 at 10:37 AM <rimebeliskner@tutanota.com> wrote:
>
> For example I have form where editor expect some number as user input.
> If entered text is not a number I want set background to red (but text still should be visible).
> I can do it, if copying code for layout in both branch:
>
> if !isNumeric {
>     bg := Background{
>       Color:  color.RGBA{A: 0xff, R: 255, G: 0, B: 0},      Radius: unit.Dp(0),   }   return bg.Layout(gtx, func(gtx layout.Context) layout.Dimensions {      return layout.W.Layout(gtx, func(gtx layout.Context) layout.Dimensions {         return layout.UniformInset(unit.Dp(8)).Layout(gtx, material.Editor(th, input, "").Layout)         })   })}
> return layout.W.Layout(gtx, func(gtx layout.Context) layout.Dimensions {
>    return layout.UniformInset(unit.Dp(8)).Layout(gtx, material.Editor(th, input, "").Layout)})
>
> But I want to get rid of duplication so I write this one:
>
> usualLayout := layout.W.Layout(gtx, func(gtx layout.Context) layout.Dimensions {
> return layout.UniformInset(unit.Dp(8)).Layout(gtx, material.Editor(th, input, "").Layout)
> })
> if !isNumeric {
>    bg := Background{      Color:  color.RGBA{A: 0xff, R: 255, G: 0, B: 0},      Radius: unit.Dp(0),   }   return bg.Layout(gtx, func(gtx layout.Context) layout.Dimensions {      return usualLayout // doesn't work
>    })}
> return usualLayout
>
> My question does it even possible and if not - why?
>
> Full code:
> https://pastebin.com/uhqVfmNA
Reply to thread Export thread (mbox)