mirror of
https://git.sr.ht/~eliasnaur/gio
synced 2026-07-07 02:15:34 +00:00
ui,ui/app: convert Config to an interface
To keep the interface slim, remove the helper methods and shorten the essential method, Pixels, to Px. Add and use unexported Config implementation in the app package. Signed-off-by: Elias Naur <mail@eliasnaur.com>
This commit is contained in:
+34
-1
@@ -4,8 +4,10 @@ package app
|
||||
|
||||
import (
|
||||
"image"
|
||||
"math"
|
||||
"os"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"gioui.org/ui"
|
||||
)
|
||||
@@ -15,7 +17,7 @@ type Event interface {
|
||||
}
|
||||
|
||||
type DrawEvent struct {
|
||||
Config ui.Config
|
||||
Config Config
|
||||
Size image.Point
|
||||
// Insets is the window space taken up by
|
||||
// system decoration such as translucent
|
||||
@@ -131,3 +133,34 @@ func init() {
|
||||
func DataDir() (string, error) {
|
||||
return dataDir()
|
||||
}
|
||||
|
||||
// Config implements the ui.Config interface.
|
||||
type Config struct {
|
||||
// Device pixels per dp.
|
||||
pxPerDp float32
|
||||
// Device pixels per sp.
|
||||
pxPerSp float32
|
||||
now time.Time
|
||||
}
|
||||
|
||||
func (c *Config) Now() time.Time {
|
||||
return c.now
|
||||
}
|
||||
|
||||
func (c *Config) Px(v ui.Value) int {
|
||||
var r float32
|
||||
switch v.U {
|
||||
case ui.UnitPx:
|
||||
r = v.V
|
||||
case ui.UnitDp:
|
||||
r = c.pxPerDp * v.V
|
||||
case ui.UnitSp:
|
||||
r = c.pxPerSp * v.V
|
||||
default:
|
||||
panic("unknown unit")
|
||||
}
|
||||
if math.IsInf(float64(r), +1) {
|
||||
return ui.Inf
|
||||
}
|
||||
return int(math.Round(float64(r)))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user