github.com/gop9/olt@v0.0.0-20200202132135-d956aad50b08/gio/app/internal/window/window.go (about)

     1  // SPDX-License-Identifier: Unlicense OR MIT
     2  
     3  // Package window implements platform specific windows
     4  // and GPU contexts.
     5  package window
     6  
     7  import (
     8  	"errors"
     9  	"math"
    10  	"time"
    11  
    12  	"github.com/gop9/olt/gio/app/internal/gl"
    13  	"github.com/gop9/olt/gio/io/event"
    14  	"github.com/gop9/olt/gio/io/system"
    15  	"github.com/gop9/olt/gio/unit"
    16  )
    17  
    18  type Options struct {
    19  	Width, Height unit.Value
    20  	Title         string
    21  }
    22  
    23  type FrameEvent struct {
    24  	system.FrameEvent
    25  
    26  	Sync bool
    27  }
    28  
    29  type Callbacks interface {
    30  	SetDriver(d Driver)
    31  	Event(e event.Event)
    32  }
    33  
    34  type Context interface {
    35  	Functions() *gl.Functions
    36  	Present() error
    37  	MakeCurrent() error
    38  	Release()
    39  	Lock()
    40  	Unlock()
    41  }
    42  
    43  // Driver is the interface for the platform implementation
    44  // of a window.
    45  type Driver interface {
    46  	// SetAnimating sets the animation flag. When the window is animating,
    47  	// FrameEvents are delivered as fast as the display can handle them.
    48  	SetAnimating(anim bool)
    49  	// ShowTextInput updates the virtual keyboard state.
    50  	ShowTextInput(show bool)
    51  	NewContext() (Context, error)
    52  }
    53  
    54  type windowRendezvous struct {
    55  	in   chan windowAndOptions
    56  	out  chan windowAndOptions
    57  	errs chan error
    58  }
    59  
    60  type windowAndOptions struct {
    61  	window Callbacks
    62  	opts   *Options
    63  }
    64  
    65  // config implements the system.Config interface.
    66  type config struct {
    67  	// Device pixels per dp.
    68  	pxPerDp float32
    69  	// Device pixels per sp.
    70  	pxPerSp float32
    71  	now     time.Time
    72  }
    73  
    74  func (c *config) Now() time.Time {
    75  	return c.now
    76  }
    77  
    78  func (c *config) Px(v unit.Value) int {
    79  	var r float32
    80  	switch v.U {
    81  	case unit.UnitPx:
    82  		r = v.V
    83  	case unit.UnitDp:
    84  		r = c.pxPerDp * v.V
    85  	case unit.UnitSp:
    86  		r = c.pxPerSp * v.V
    87  	default:
    88  		panic("unknown unit")
    89  	}
    90  	return int(math.Round(float64(r)))
    91  }
    92  
    93  func newWindowRendezvous() *windowRendezvous {
    94  	wr := &windowRendezvous{
    95  		in:   make(chan windowAndOptions),
    96  		out:  make(chan windowAndOptions),
    97  		errs: make(chan error),
    98  	}
    99  	go func() {
   100  		var main windowAndOptions
   101  		var out chan windowAndOptions
   102  		for {
   103  			select {
   104  			case w := <-wr.in:
   105  				var err error
   106  				if main.window != nil {
   107  					err = errors.New("multiple windows are not supported")
   108  				}
   109  				wr.errs <- err
   110  				main = w
   111  				out = wr.out
   112  			case out <- main:
   113  			}
   114  		}
   115  	}()
   116  	return wr
   117  }