app,io/system,layout: [API] move FrameEvent and Insets to package app

In the early days of Gio, FrameEvent was part of package app. It was
moved to package system to enable layout.NewContext be a convenient
short-hand for constructing a layout.

However, it seems the better design to leave FrameEvent (and Insets) in
package app, and move layout.NewContext there as well. More importantly,
the move allows us to replace the event.Queue interface with a concrete
type.

Signed-off-by: Elias Naur <mail@eliasnaur.com>
This commit is contained in:
Elias Naur
2023-10-08 12:21:41 -05:00
parent 60bfb9e064
commit cb1e605203
16 changed files with 103 additions and 107 deletions
+80 -6
View File
@@ -3,9 +3,16 @@
package app
import (
"image"
"os"
"path/filepath"
"strings"
"time"
"gioui.org/io/event"
"gioui.org/layout"
"gioui.org/op"
"gioui.org/unit"
)
// extraArgs contains extra arguments to append to
@@ -30,13 +37,68 @@ var extraArgs string
// is not supported. Default value of ID is filepath.Base(os.Args[0]).
var ID = ""
func init() {
if extraArgs != "" {
args := strings.Split(extraArgs, "|")
os.Args = append(os.Args, args...)
// A FrameEvent requests a new frame in the form of a list of
// operations that describes what to display and how to handle
// input.
type FrameEvent struct {
// Now is the current animation. Use Now instead of time.Now to
// synchronize animation and to avoid the time.Now call overhead.
Now time.Time
// Metric converts device independent dp and sp to device pixels.
Metric unit.Metric
// Size is the dimensions of the window.
Size image.Point
// Insets represent the space occupied by system decorations and controls.
Insets Insets
// Frame completes the FrameEvent by drawing the graphical operations
// from ops into the window.
Frame func(frame *op.Ops)
// Queue supplies the events for event handlers.
Queue event.Queue
}
// Insets is the space taken up by
// system decoration such as translucent
// system bars and software keyboards.
type Insets struct {
// Values are in pixels.
Top, Bottom, Left, Right unit.Dp
}
// NewContext is a shorthand for
//
// layout.Context{
// Ops: ops,
// Now: e.Now,
// Queue: e.Queue,
// Config: e.Config,
// Constraints: layout.Exact(e.Size),
// }
//
// NewContext calls ops.Reset and adjusts ops for e.Insets.
func NewContext(ops *op.Ops, e FrameEvent) layout.Context {
ops.Reset()
size := e.Size
if e.Insets != (Insets{}) {
left := e.Metric.Dp(e.Insets.Left)
top := e.Metric.Dp(e.Insets.Top)
op.Offset(image.Point{
X: left,
Y: top,
}).Add(ops)
size.X -= left + e.Metric.Dp(e.Insets.Right)
size.Y -= top + e.Metric.Dp(e.Insets.Bottom)
}
if ID == "" {
ID = filepath.Base(os.Args[0])
return layout.Context{
Ops: ops,
Now: e.Now,
Queue: e.Queue,
Metric: e.Metric,
Constraints: layout.Exact(size),
}
}
@@ -63,3 +125,15 @@ func DataDir() (string, error) {
func Main() {
osMain()
}
func (FrameEvent) ImplementsEvent() {}
func init() {
if extraArgs != "" {
args := strings.Split(extraArgs, "|")
os.Args = append(os.Args, args...)
}
if ID == "" {
ID = filepath.Base(os.Args[0])
}
}
+1 -3
View File
@@ -17,12 +17,10 @@ FrameEvent that prompts an update of the window contents.
For example:
import "gioui.org/unit"
w := app.NewWindow()
for {
e := w.NextEvent()
if e, ok := e.(system.FrameEvent); ok {
if e, ok := e.(app.FrameEvent); ok {
ops.Reset()
// Add operations to ops.
...
+1 -1
View File
@@ -132,7 +132,7 @@ func (o Orientation) String() string {
}
type frameEvent struct {
system.FrameEvent
FrameEvent
Sync bool
}
+2 -2
View File
@@ -836,14 +836,14 @@ func (w *window) draw(env *C.JNIEnv, sync bool) {
const inchPrDp = 1.0 / 160
ppdp := float32(w.dpi) * inchPrDp
dppp := unit.Dp(1.0 / ppdp)
insets := system.Insets{
insets := Insets{
Top: unit.Dp(w.insets.top) * dppp,
Bottom: unit.Dp(w.insets.bottom) * dppp,
Left: unit.Dp(w.insets.left) * dppp,
Right: unit.Dp(w.insets.right) * dppp,
}
w.callbacks.Event(frameEvent{
FrameEvent: system.FrameEvent{
FrameEvent: FrameEvent{
Now: time.Now(),
Size: w.config.Size,
Insets: insets,
+2 -2
View File
@@ -156,13 +156,13 @@ func (w *window) draw(sync bool) {
}
dppp := unit.Dp(1. / m.PxPerDp)
w.w.Event(frameEvent{
FrameEvent: system.FrameEvent{
FrameEvent: FrameEvent{
Now: time.Now(),
Size: image.Point{
X: int(params.width + .5),
Y: int(params.height + .5),
},
Insets: system.Insets{
Insets: Insets{
Top: unit.Dp(params.top) * dppp,
Bottom: unit.Dp(params.bottom) * dppp,
Left: unit.Dp(params.left) * dppp,
+3 -3
View File
@@ -666,7 +666,7 @@ func (w *window) draw(sync bool) {
}
w.w.Event(frameEvent{
FrameEvent: system.FrameEvent{
FrameEvent: FrameEvent{
Now: time.Now(),
Size: size,
Insets: insets,
@@ -676,10 +676,10 @@ func (w *window) draw(sync bool) {
})
}
func (w *window) getConfig() (image.Point, system.Insets, unit.Metric) {
func (w *window) getConfig() (image.Point, Insets, unit.Metric) {
invscale := unit.Dp(1. / w.scale)
return image.Pt(w.config.Size.X, w.config.Size.Y),
system.Insets{
Insets{
Bottom: unit.Dp(w.inset.Y) * invscale,
Right: unit.Dp(w.inset.X) * invscale,
}, unit.Metric{
+1 -1
View File
@@ -777,7 +777,7 @@ func (w *window) draw() {
cfg := configFor(w.scale)
w.setStage(system.StageRunning)
w.w.Event(frameEvent{
FrameEvent: system.FrameEvent{
FrameEvent: FrameEvent{
Now: time.Now(),
Size: w.config.Size,
Metric: cfg,
+1 -1
View File
@@ -1712,7 +1712,7 @@ func (w *window) draw() {
C.wl_callback_add_listener(w.lastFrameCallback, &C.gio_callback_listener, unsafe.Pointer(w.surf))
}
w.w.Event(frameEvent{
FrameEvent: system.FrameEvent{
FrameEvent: FrameEvent{
Now: time.Now(),
Size: w.config.Size,
Metric: cfg,
+1 -1
View File
@@ -621,7 +621,7 @@ func (w *window) draw(sync bool) {
dpi := windows.GetWindowDPI(w.hwnd)
cfg := configForDPI(dpi)
w.w.Event(frameEvent{
FrameEvent: system.FrameEvent{
FrameEvent: FrameEvent{
Now: time.Now(),
Size: w.config.Size,
Metric: cfg,
+1 -1
View File
@@ -458,7 +458,7 @@ loop:
if (anim || syn) && w.config.Size.X != 0 && w.config.Size.Y != 0 {
w.w.Event(frameEvent{
FrameEvent: system.FrameEvent{
FrameEvent: FrameEvent{
Now: time.Now(),
Size: w.config.Size,
Metric: w.metric,
+1 -1
View File
@@ -994,7 +994,7 @@ func (w *Window) fallbackDecorate() bool {
}
// decorate the window if enabled and returns the corresponding Insets.
func (w *Window) decorate(d driver, e system.FrameEvent, o *op.Ops) (size, offset image.Point) {
func (w *Window) decorate(d driver, e FrameEvent, o *op.Ops) (size, offset image.Point) {
if !w.fallbackDecorate() {
return e.Size, image.Pt(0, 0)
}