~eliasnaur/gio

3 3

App size vs constraints

Details
Message ID
<CAARDkYBgrLwveqkKOAk3SEdrb8t5JjtHTHFc7k9s2MmRrHhROA@mail.gmail.com>
DKIM signature
pass
Download raw message
Hi

While maintaining my egg-timer I noticed the previous centered egg now
had moved to the left of center.

The size of the app is hardcoded to 400x600

        w := app.NewWindow(
            app.Title("Egg timer"),
            app.Size(unit.Dp(400), unit.Dp(600)),
        )


But when I later define and investigate the context, it reports 500x750

            case system.FrameEvent:
                gtx := layout.NewContext(&ops, e)
                fmt.Println(gtx.Constraints.Max)


> go run .
> (500,750)


What's missing in my understanding?
A scaling factor between unit.Dp and gtx.Constraints?
A decoupling between app and canvas?
Others?


From https://github.com/jonegil/gui-with-gio/blob/main/egg_timer/code/08_egg_as_circle/main.go



Thanks for any pointers!!

Kind regards
Jon Egil
Details
Message ID
<CANtNKfoajiqM9xxOu74uvsLJEm3fhvZA7HLT9PRyRtMrM=uNgg@mail.gmail.com>
In-Reply-To
<CAARDkYBgrLwveqkKOAk3SEdrb8t5JjtHTHFc7k9s2MmRrHhROA@mail.gmail.com> (view parent)
DKIM signature
pass
Download raw message
https://pkg.go.dev/gioui.org/unit#Metric contains PxPerDp and PxPerSp.
It could be useful to add `DpToPx` and `SpToPx` methods.

+ Egon

On Fri, Sep 2, 2022 at 8:09 PM Jon Egil Strand
<jon.egil.strand@gmail.com> wrote:
>
> Hi
>
> While maintaining my egg-timer I noticed the previous centered egg now
> had moved to the left of center.
>
> The size of the app is hardcoded to 400x600
>
>         w := app.NewWindow(
>             app.Title("Egg timer"),
>             app.Size(unit.Dp(400), unit.Dp(600)),
>         )
>
>
> But when I later define and investigate the context, it reports 500x750
>
>             case system.FrameEvent:
>                 gtx := layout.NewContext(&ops, e)
>                 fmt.Println(gtx.Constraints.Max)
>
>
> > go run .
> > (500,750)
>
>
> What's missing in my understanding?
> A scaling factor between unit.Dp and gtx.Constraints?
> A decoupling between app and canvas?
> Others?
>
>
> >From https://github.com/jonegil/gui-with-gio/blob/main/egg_timer/code/08_egg_as_circle/main.go
>
>
>
> Thanks for any pointers!!
>
> Kind regards
> Jon Egil
Details
Message ID
<CAFcc3FTmqd_7yAXL3TDaCep+8O3nydfKhcDemrZe15BVVv64gw@mail.gmail.com>
In-Reply-To
<CAARDkYBgrLwveqkKOAk3SEdrb8t5JjtHTHFc7k9s2MmRrHhROA@mail.gmail.com> (view parent)
DKIM signature
pass
Download raw message
> What's missing in my understanding?
> A scaling factor between unit.Dp and gtx.Constraints?
> A decoupling between app and canvas?
> Others?

From your description, I suspect that you're running your code on a
display with a 125% scaling factor applied. Gio automatically applies
that number when translating from DP to screen pixels. That's why your
window is 125% larger than you expect. You can print gtx.Metric to
confirm that. You can use the gtx.Dp method to convert DP to screen
pixels in your code.

Cheers,
Chris
Details
Message ID
<CAARDkYCkBezjy07MbhOu5EoMQt0uaLyY__7x2WDkCq2Oz=9obQ@mail.gmail.com>
In-Reply-To
<CAFcc3FTmqd_7yAXL3TDaCep+8O3nydfKhcDemrZe15BVVv64gw@mail.gmail.com> (view parent)
DKIM signature
pass
Download raw message
Ah, thank you, both Egon and Chris!

J

On Sun, 4 Sept 2022 at 00:51, Chris Waldon
<christopher.waldon.dev@gmail.com> wrote:
>
> > What's missing in my understanding?
> > A scaling factor between unit.Dp and gtx.Constraints?
> > A decoupling between app and canvas?
> > Others?
>
> From your description, I suspect that you're running your code on a
> display with a 125% scaling factor applied. Gio automatically applies
> that number when translating from DP to screen pixels. That's why your
> window is 125% larger than you expect. You can print gtx.Metric to
> confirm that. You can use the gtx.Dp method to convert DP to screen
> pixels in your code.
>
> Cheers,
> Chris
Reply to thread Export thread (mbox)