~eliasnaur/gio

0f05231c356f917981c2e5afebe5e40e0ffea39c — Elias Naur 1 year, 7 months ago
all: initial import

Signed-off-by: Elias Naur <mail@eliasnaur.com>
102 files changed, 17926 insertions(+), 0 deletions(-)

A .gitignore
A COPYING
A LICENSE-MIT
A README.md
A UNLICENSE
A apps/go.mod
A apps/go.sum
A apps/gophers/android/build.gradle
A apps/gophers/android/gradle/wrapper/gradle-wrapper.jar
A apps/gophers/android/gradle/wrapper/gradle-wrapper.properties
A apps/gophers/android/gradlew
A apps/gophers/android/gradlew.bat
A apps/gophers/android/src/main/AndroidManifest.xml
A apps/gophers/android/src/main/res/values/strings.xml
A apps/gophers/ios/gophers.xcodeproj/project.pbxproj
A apps/gophers/ios/gophers.xcodeproj/project.xcworkspace/contents.xcworkspacedata
A apps/gophers/ios/gophers.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist
A apps/gophers/ios/gophers.xcodeproj/project.xcworkspace/xcshareddata/WorkspaceSettings.xcsettings
A apps/gophers/ios/gophers/Assets.xcassets/Contents.json
A apps/gophers/ios/gophers/Info.plist
A apps/gophers/ios/gophers/main.m
A apps/gophers/main.go
A cmd/assets/assets.go
A cmd/gio/gio.go
A cmd/go.mod
A cmd/go.sum
A ui/app/GioActivity.java
A ui/app/GioView.java
A ui/app/app.go
A ui/app/egl.go
A ui/app/egl_android.go
A ui/app/egl_linux.go
A ui/app/egl_wayland.go
A ui/app/egl_win.go
A ui/app/egl_windows.go
A ui/app/framework_ios.h
A ui/app/gl_ios.go
A ui/app/gl_ios.h
A ui/app/gl_ios.m
A ui/app/gl_macos.go
A ui/app/gl_macos.h
A ui/app/gl_macos.m
A ui/app/internal/gl/functions.go
A ui/app/internal/gl/gl.go
A ui/app/internal/gl/gl_windows.go
A ui/app/internal/gl/srgb.go
A ui/app/internal/gl/util.go
A ui/app/internal/gpu/arealut.go
A ui/app/internal/gpu/gpu.go
A ui/app/internal/gpu/pack.go
A ui/app/internal/gpu/path.go
A ui/app/internal/gpu/timer.go
A ui/app/log_android.go
A ui/app/os_android.c
A ui/app/os_android.go
A ui/app/os_android.h
A ui/app/os_ios.go
A ui/app/os_ios.h
A ui/app/os_ios.m
A ui/app/os_macos.go
A ui/app/os_macos.h
A ui/app/os_macos.m
A ui/app/os_wayland.c
A ui/app/os_wayland.go
A ui/app/os_wayland.h
A ui/app/os_windows.go
A ui/app/wayland_text_input.c
A ui/app/wayland_text_input.h
A ui/app/wayland_xdg_decoration.c
A ui/app/wayland_xdg_decoration.h
A ui/app/wayland_xdg_shell.c
A ui/app/wayland_xdg_shell.h
A ui/app/window.go
A ui/draw/draw.go
A ui/draw/path.go
A ui/f32/f32.go
A ui/gesture/estimator.go
A ui/gesture/estimator_test.go
A ui/gesture/gestures.go
A ui/go.mod
A ui/go.sum
A ui/internal/path/path.go
A ui/key/key.go
A ui/key/queue.go
A ui/layout/flex.go
A ui/layout/list.go
A ui/layout/simple.go
A ui/layout/stack.go
A ui/measure/measure.go
A ui/measure/opentype.go
A ui/pointer/pointer.go
A ui/pointer/queue.go
A ui/text/buffer.go
A ui/text/editor.go
A ui/text/label.go
A ui/text/measure.go
A ui/ui.go
A ui/unit.go
A ui/widget/image.go
A website/app.yaml
A website/go.mod
A website/main.go
A  => .gitignore +2 -0
@@ 1,2 @@
.gradle
**/android/build

A  => COPYING +3 -0
@@ 1,3 @@
This project is dual-licensed under the Unlicense and MIT licenses.

You may use this code under the terms of either license.

A  => LICENSE-MIT +21 -0
@@ 1,21 @@
The MIT License (MIT)

Copyright (c) 2019 Elias Naur

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

A  => README.md +60 -0
@@ 1,60 @@
# Gio

Gio implements portable immediate mode GUI programs in Go. Gio programs run on all the major platforms:
iOS/tvOS, Android, Linux (Wayland), macOS and Windows.

## Quickstart

Gio is designed to work with very few dependencies. It depends only on the platform libraries for
window management, input and GPU drawing.

For Linux you need Wayland and the `wayland-client`, `wayland-egl`, `wayland-cursor`, and `xkbcommon`
development packages.

Xcode is required for macOS and iOS.

For Windows you need the ANGLE drivers for emulating OpenGL ES. You can build ANGLE yourself or use
[mine](https://drive.google.com/file/d/1k2950mHNtR2iwhweHS1rJ7reChTa3rki/view?usp=sharing).

With Go 1.12 or newer,

	$ go run gioui.org/apps/gophers

should display a simple (nonsense) demo.

## Android

For Android you need the Android SDK with the NDK installed. Point the ANDROID_HOME to the SDK root
directory.

To build a Gio program as an .aar package, use the gio tool. For example,

	$ go run gioui.org/cmd/gio -target android gioui.org/apps/gophers

to produce gophers.aar, ready to use in an Android project. To run
the demo on an Android device:

	$ git clone https://git.sr.ht/~eliasnaur/gio
	$ cd gio/apps/gophers/android
	$ go run gioui.org/cmd/gio -target android ..
	$ ./gradlew installDebug          # gradlew.bat on Windows

The gio tool passes command line arguments to os.Args at runtime:

	$ go run gioui.org/cmd/gio -target android .. -token <github token>

## License

Dual-licensed under MIT or the [UNLICENSE](http://unlicense.org).

## Contributing

Discussion and patches: [~eliasnaur/gio-dev@lists.sr.ht](mailto:~eliasnaur/gio-dev@lists.sr.ht).
[Instructions](https://man.sr.ht/git.sr.ht/send-email.md). for using git-send-email for sending patches.

Contributors must agree to the [developer certificate og origin](https://developercertificate.org/),
to ensure their work is compatible with the MIT and the UNLICENSE. Sign your commits with Signed-off-by
statements to show your agreement. For convenience, the `git commit --sign` signs a commit with the
name and email from your `user.name` and `user.email` settings.

Bugs and TODOs go in the [issue tracker](https://todo.sr.ht/~eliasnaur/gio).

A  => UNLICENSE +25 -0
@@ 1,25 @@

This is free and unencumbered software released into the public domain.

Anyone is free to copy, modify, publish, use, compile, sell, or
distribute this software, either in source code form or as a compiled
binary, for any purpose, commercial or non-commercial, and by any
means.

In jurisdictions that recognize copyright laws, the author or authors
of this software dedicate any and all copyright interest in the
software to the public domain. We make this dedication for the benefit
of the public at large and to the detriment of our heirs and
successors. We intend this dedication to be an overt act of
relinquishment in perpetuity of all present and future rights to this
software under copyright law.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.

For more information, please refer to <http://unlicense.org/>

A  => apps/go.mod +11 -0
@@ 1,11 @@
module gioui.org/apps

go 1.13

require (
	gioui.org/ui v0.0.0-20190330124410-f25b44831f2b
	github.com/google/go-github/v24 v24.0.1
	golang.org/x/exp v0.0.0-20190321205749-f0864edee7f3
	golang.org/x/image v0.0.0-20190321063152-3fc05d484e9f
	golang.org/x/oauth2 v0.0.0-20190319182350-c85d3e98c914
)

A  => apps/go.sum +41 -0
@@ 1,41 @@
cloud.google.com/go v0.34.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
gioui.org/ui v0.0.0-20190330124410-f25b44831f2b h1:fY5FJRK/vwDZiNeJOQwGbr3QJd4ihJh6OFVvrqC4Djk=
gioui.org/ui v0.0.0-20190330124410-f25b44831f2b/go.mod h1:Nsy5gLRWhMMNMmed9+KjrQ8XXT7a8u8s21zgn/er6d4=
github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo=
github.com/golang/protobuf v1.2.0 h1:P3YflyNX/ehuJFLhxviNdFxQPkGK5cDcApsge1SqnvM=
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/google/go-github v17.0.0+incompatible h1:N0LgJ1j65A7kfXrZnUDaYCs/Sf4rEjNlfyDHW9dolSY=
github.com/google/go-github v17.0.0+incompatible/go.mod h1:zLgOLi98H3fifZn+44m+umXrS52loVEgC2AApnigrVQ=
github.com/google/go-github/v24 v24.0.1 h1:KCt1LjMJEey1qvPXxa9SjaWxwTsCWSq6p2Ju57UR4Q4=
github.com/google/go-github/v24 v24.0.1/go.mod h1:CRqaW1Uns1TCkP0wqTpxYyRxRjxwvKU/XSS44u6X74M=
github.com/google/go-querystring v1.0.0 h1:Xkwi/a1rcvNg1PPYe5vI8GbeBY/jrVuDX5ASuANWTrk=
github.com/google/go-querystring v1.0.0/go.mod h1:odCYkC5MyYFN7vkCjXpyrEuKhc/BUO6wN/zVPAxq5ck=
golang.org/x/crypto v0.0.0-20180820150726-614d502a4dac/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/exp v0.0.0-20190321205749-f0864edee7f3 h1:Ep4L2ibjtJcW6IP73KbcJAU0cpNKsLNSSP2jE1xlCys=
golang.org/x/exp v0.0.0-20190321205749-f0864edee7f3/go.mod h1:ZjyILWgesfNpC6sMxTJOJm9Kp84zZh5NQWvqDGG3Qr8=
golang.org/x/image v0.0.0-20190227222117-0694c2d4d067/go.mod h1:kZ7UVZpmo3dzQBMxlp+ypCbDeSB+sBbTgSJuh5dn5js=
golang.org/x/image v0.0.0-20190321063152-3fc05d484e9f h1:FO4MZ3N56GnxbqxGKqh+YTzUWQ2sDwtFQEZgLOxh9Jc=
golang.org/x/image v0.0.0-20190321063152-3fc05d484e9f/go.mod h1:kZ7UVZpmo3dzQBMxlp+ypCbDeSB+sBbTgSJuh5dn5js=
golang.org/x/mobile v0.0.0-20190312151609-d3739f865fa6/go.mod h1:z+o9i4GpDbdi3rU15maQ/Ox0txvL9dWGYEHz965HBQE=
golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20190108225652-1e06a53dbb7e/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20190311183353-d8887717615a h1:oWX7TPOiFAMXLq8o0ikBYfCJVlRHBcsciT5bXOrH628=
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
golang.org/x/oauth2 v0.0.0-20190319182350-c85d3e98c914 h1:jIOcLT9BZzyJ9ce+IwwZ+aF9yeCqzrR+NrD68a/SHKw=
golang.org/x/oauth2 v0.0.0-20190319182350-c85d3e98c914/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4 h1:YUO/7uOKsKeq9UokNS62b8FYywz3ker1l1vDZRCRefw=
golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20180824143301-4910a1d54f87/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190312061237-fead79001313/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20190329044733-9eb1bfa1ce65 h1:hOY+O8MxdkPV10pNf7/XEHaySCiPKxixMKUshfHsGn0=
golang.org/x/sys v0.0.0-20190329044733-9eb1bfa1ce65/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/tools v0.0.0-20190312151545-0bb0c0a6e846/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM=
google.golang.org/appengine v1.4.0 h1:/wp5JvzpHIxhs/dumFmF7BXTf3Z+dd4uXta4kVyO508=
google.golang.org/appengine v1.4.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4=

A  => apps/gophers/android/build.gradle +32 -0
@@ 1,32 @@
buildscript {
    repositories {
		google()
		jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.3.1'
    }
}

apply plugin: 'com.android.application'

android {
    buildToolsVersion "28.0.3"
    compileSdkVersion 27

	defaultConfig {
		targetSdkVersion 27
		minSdkVersion 16
		versionCode 1
		versionName "0.1"
	}
}

dependencies {
	implementation fileTree(dir: '.', include: ['*.aar'])
}

repositories {
	google()
	jcenter()
}

A  => apps/gophers/android/gradle/wrapper/gradle-wrapper.jar +0 -0

A  => apps/gophers/android/gradle/wrapper/gradle-wrapper.properties +5 -0
@@ 1,5 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-4.10.1-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists

A  => apps/gophers/android/gradlew +172 -0
@@ 1,172 @@
#!/usr/bin/env sh

##############################################################################
##
##  Gradle start up script for UN*X
##
##############################################################################

# Attempt to set APP_HOME
# Resolve links: $0 may be a link
PRG="$0"
# Need this for relative symlinks.
while [ -h "$PRG" ] ; do
    ls=`ls -ld "$PRG"`
    link=`expr "$ls" : '.*-> \(.*\)$'`
    if expr "$link" : '/.*' > /dev/null; then
        PRG="$link"
    else
        PRG=`dirname "$PRG"`"/$link"
    fi
done
SAVED="`pwd`"
cd "`dirname \"$PRG\"`/" >/dev/null
APP_HOME="`pwd -P`"
cd "$SAVED" >/dev/null

APP_NAME="Gradle"
APP_BASE_NAME=`basename "$0"`

# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
DEFAULT_JVM_OPTS=""

# Use the maximum available, or set MAX_FD != -1 to use that value.
MAX_FD="maximum"

warn () {
    echo "$*"
}

die () {
    echo
    echo "$*"
    echo
    exit 1
}

# OS specific support (must be 'true' or 'false').
cygwin=false
msys=false
darwin=false
nonstop=false
case "`uname`" in
  CYGWIN* )
    cygwin=true
    ;;
  Darwin* )
    darwin=true
    ;;
  MINGW* )
    msys=true
    ;;
  NONSTOP* )
    nonstop=true
    ;;
esac

CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar

# Determine the Java command to use to start the JVM.
if [ -n "$JAVA_HOME" ] ; then
    if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
        # IBM's JDK on AIX uses strange locations for the executables
        JAVACMD="$JAVA_HOME/jre/sh/java"
    else
        JAVACMD="$JAVA_HOME/bin/java"
    fi
    if [ ! -x "$JAVACMD" ] ; then
        die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME

Please set the JAVA_HOME variable in your environment to match the
location of your Java installation."
    fi
else
    JAVACMD="java"
    which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.

Please set the JAVA_HOME variable in your environment to match the
location of your Java installation."
fi

# Increase the maximum file descriptors if we can.
if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then
    MAX_FD_LIMIT=`ulimit -H -n`
    if [ $? -eq 0 ] ; then
        if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
            MAX_FD="$MAX_FD_LIMIT"
        fi
        ulimit -n $MAX_FD
        if [ $? -ne 0 ] ; then
            warn "Could not set maximum file descriptor limit: $MAX_FD"
        fi
    else
        warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
    fi
fi

# For Darwin, add options to specify how the application appears in the dock
if $darwin; then
    GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
fi

# For Cygwin, switch paths to Windows format before running java
if $cygwin ; then
    APP_HOME=`cygpath --path --mixed "$APP_HOME"`
    CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
    JAVACMD=`cygpath --unix "$JAVACMD"`

    # We build the pattern for arguments to be converted via cygpath
    ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
    SEP=""
    for dir in $ROOTDIRSRAW ; do
        ROOTDIRS="$ROOTDIRS$SEP$dir"
        SEP="|"
    done
    OURCYGPATTERN="(^($ROOTDIRS))"
    # Add a user-defined pattern to the cygpath arguments
    if [ "$GRADLE_CYGPATTERN" != "" ] ; then
        OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
    fi
    # Now convert the arguments - kludge to limit ourselves to /bin/sh
    i=0
    for arg in "$@" ; do
        CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
        CHECK2=`echo "$arg"|egrep -c "^-"`                                 ### Determine if an option

        if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then                    ### Added a condition
            eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
        else
            eval `echo args$i`="\"$arg\""
        fi
        i=$((i+1))
    done
    case $i in
        (0) set -- ;;
        (1) set -- "$args0" ;;
        (2) set -- "$args0" "$args1" ;;
        (3) set -- "$args0" "$args1" "$args2" ;;
        (4) set -- "$args0" "$args1" "$args2" "$args3" ;;
        (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
        (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
        (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
        (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
        (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
    esac
fi

# Escape application args
save () {
    for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done
    echo " "
}
APP_ARGS=$(save "$@")

# Collect all arguments for the java command, following the shell quoting and substitution rules
eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS"

# by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong
if [ "$(uname)" = "Darwin" ] && [ "$HOME" = "$PWD" ]; then
  cd "$(dirname "$0")"
fi

exec "$JAVACMD" "$@"

A  => apps/gophers/android/gradlew.bat +84 -0
@@ 1,84 @@
@if "%DEBUG%" == "" @echo off
@rem ##########################################################################
@rem
@rem  Gradle startup script for Windows
@rem
@rem ##########################################################################

@rem Set local scope for the variables with windows NT shell
if "%OS%"=="Windows_NT" setlocal

set DIRNAME=%~dp0
if "%DIRNAME%" == "" set DIRNAME=.
set APP_BASE_NAME=%~n0
set APP_HOME=%DIRNAME%

@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
set DEFAULT_JVM_OPTS=

@rem Find java.exe
if defined JAVA_HOME goto findJavaFromJavaHome

set JAVA_EXE=java.exe
%JAVA_EXE% -version >NUL 2>&1
if "%ERRORLEVEL%" == "0" goto init

echo.
echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
echo.
echo Please set the JAVA_HOME variable in your environment to match the
echo location of your Java installation.

goto fail

:findJavaFromJavaHome
set JAVA_HOME=%JAVA_HOME:"=%
set JAVA_EXE=%JAVA_HOME%/bin/java.exe

if exist "%JAVA_EXE%" goto init

echo.
echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
echo.
echo Please set the JAVA_HOME variable in your environment to match the
echo location of your Java installation.

goto fail

:init
@rem Get command-line arguments, handling Windows variants

if not "%OS%" == "Windows_NT" goto win9xME_args

:win9xME_args
@rem Slurp the command line arguments.
set CMD_LINE_ARGS=
set _SKIP=2

:win9xME_args_slurp
if "x%~1" == "x" goto execute

set CMD_LINE_ARGS=%*

:execute
@rem Setup the command line

set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar

@rem Execute Gradle
"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS%

:end
@rem End local scope for the variables with windows NT shell
if "%ERRORLEVEL%"=="0" goto mainEnd

:fail
rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
rem the _cmd.exe /c_ return code!
if  not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
exit /b 1

:mainEnd
if "%OS%"=="Windows_NT" endlocal

:omega

A  => apps/gophers/android/src/main/AndroidManifest.xml +20 -0
@@ 1,20 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
	package="org.gioui.apps.demo"
	android:versionCode="1"
	android:versionName="1.0">
	<uses-permission android:name="android.permission.INTERNET" />
	<uses-feature android:glEsVersion="0x00020000"/>

	<application android:label="Gopher Chat">
		<activity android:name="org.gioui.GioActivity"
			android:label="@string/app_name"
			android:theme="@android:style/Theme.NoTitleBar"
			android:configChanges="orientation|keyboardHidden">
			<intent-filter>
				<action android:name="android.intent.action.MAIN" />
				<category android:name="android.intent.category.LAUNCHER" />
			</intent-filter>
		</activity>
	</application>
</manifest>

A  => apps/gophers/android/src/main/res/values/strings.xml +4 -0
@@ 1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
	<string name="app_name">Gio Demo</string>
</resources>

A  => apps/gophers/ios/gophers.xcodeproj/project.pbxproj +330 -0
@@ 1,330 @@
// !$*UTF8*$!
{
	archiveVersion = 1;
	classes = {
	};
	objectVersion = 50;
	objects = {

/* Begin PBXBuildFile section */
		644702662225DDD70022507C /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = 644702652225DDD70022507C /* main.m */; };
		6447028A2225E97B0022507C /* Gophers.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 644702892225E97B0022507C /* Gophers.framework */; };
		64CCFF572121A32B00B48E05 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 64CCFF562121A32B00B48E05 /* Assets.xcassets */; };
		64CCFF5A2121A32B00B48E05 /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 64CCFF582121A32B00B48E05 /* LaunchScreen.storyboard */; };
/* End PBXBuildFile section */

/* Begin PBXFileReference section */
		644702652225DDD70022507C /* main.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = main.m; sourceTree = "<group>"; };
		644702892225E97B0022507C /* Gophers.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Gophers.framework; path = gophers/Gophers.framework; sourceTree = "<group>"; };
		64CCFF432121A32800B48E05 /* gophers.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = gophers.app; sourceTree = BUILT_PRODUCTS_DIR; };
		64CCFF562121A32B00B48E05 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = "<group>"; };
		64CCFF592121A32B00B48E05 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = "<group>"; };
		64CCFF5B2121A32B00B48E05 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
		64CCFF402121A32800B48E05 /* Frameworks */ = {
			isa = PBXFrameworksBuildPhase;
			buildActionMask = 2147483647;
			files = (
				6447028A2225E97B0022507C /* Gophers.framework in Frameworks */,
			);
			runOnlyForDeploymentPostprocessing = 0;
		};
/* End PBXFrameworksBuildPhase section */

/* Begin PBXGroup section */
		64CCFF3A2121A32700B48E05 = {
			isa = PBXGroup;
			children = (
				64CCFF452121A32800B48E05 /* gophers */,
				64CCFF442121A32800B48E05 /* Products */,
				644702892225E97B0022507C /* Gophers.framework */,
			);
			sourceTree = "<group>";
		};
		64CCFF442121A32800B48E05 /* Products */ = {
			isa = PBXGroup;
			children = (
				64CCFF432121A32800B48E05 /* gophers.app */,
			);
			name = Products;
			sourceTree = "<group>";
		};
		64CCFF452121A32800B48E05 /* gophers */ = {
			isa = PBXGroup;
			children = (
				64CCFF562121A32B00B48E05 /* Assets.xcassets */,
				64CCFF582121A32B00B48E05 /* LaunchScreen.storyboard */,
				64CCFF5B2121A32B00B48E05 /* Info.plist */,
				644702652225DDD70022507C /* main.m */,
			);
			path = gophers;
			sourceTree = "<group>";
		};
/* End PBXGroup section */

/* Begin PBXNativeTarget section */
		64CCFF422121A32800B48E05 /* gophers */ = {
			isa = PBXNativeTarget;
			buildConfigurationList = 64CCFF602121A32B00B48E05 /* Build configuration list for PBXNativeTarget "gophers" */;
			buildPhases = (
				64CCFF3F2121A32800B48E05 /* Sources */,
				64CCFF402121A32800B48E05 /* Frameworks */,
				64CCFF412121A32800B48E05 /* Resources */,
			);
			buildRules = (
			);
			dependencies = (
			);
			name = gophers;
			productName = gophers;
			productReference = 64CCFF432121A32800B48E05 /* gophers.app */;
			productType = "com.apple.product-type.application";
		};
/* End PBXNativeTarget section */

/* Begin PBXProject section */
		64CCFF3B2121A32800B48E05 /* Project object */ = {
			isa = PBXProject;
			attributes = {
				LastUpgradeCheck = 0940;
				ORGANIZATIONNAME = eliasnaur.com;
				TargetAttributes = {
					64CCFF422121A32800B48E05 = {
						CreatedOnToolsVersion = 9.4.1;
					};
				};
			};
			buildConfigurationList = 64CCFF3E2121A32800B48E05 /* Build configuration list for PBXProject "gophers" */;
			compatibilityVersion = "Xcode 9.3";
			developmentRegion = en;
			hasScannedForEncodings = 0;
			knownRegions = (
				en,
				Base,
			);
			mainGroup = 64CCFF3A2121A32700B48E05;
			productRefGroup = 64CCFF442121A32800B48E05 /* Products */;
			projectDirPath = "";
			projectRoot = "";
			targets = (
				64CCFF422121A32800B48E05 /* gophers */,
			);
		};
/* End PBXProject section */

/* Begin PBXResourcesBuildPhase section */
		64CCFF412121A32800B48E05 /* Resources */ = {
			isa = PBXResourcesBuildPhase;
			buildActionMask = 2147483647;
			files = (
				64CCFF572121A32B00B48E05 /* Assets.xcassets in Resources */,
				64CCFF5A2121A32B00B48E05 /* LaunchScreen.storyboard in Resources */,
			);
			runOnlyForDeploymentPostprocessing = 0;
		};
/* End PBXResourcesBuildPhase section */

/* Begin PBXSourcesBuildPhase section */
		64CCFF3F2121A32800B48E05 /* Sources */ = {
			isa = PBXSourcesBuildPhase;
			buildActionMask = 2147483647;
			files = (
				644702662225DDD70022507C /* main.m in Sources */,
			);
			runOnlyForDeploymentPostprocessing = 0;
		};
/* End PBXSourcesBuildPhase section */

/* Begin PBXVariantGroup section */
		64CCFF582121A32B00B48E05 /* LaunchScreen.storyboard */ = {
			isa = PBXVariantGroup;
			children = (
				64CCFF592121A32B00B48E05 /* Base */,
			);
			name = LaunchScreen.storyboard;
			sourceTree = "<group>";
		};
/* End PBXVariantGroup section */

/* Begin XCBuildConfiguration section */
		64CCFF5E2121A32B00B48E05 /* Debug */ = {
			isa = XCBuildConfiguration;
			buildSettings = {
				ALWAYS_SEARCH_USER_PATHS = NO;
				CLANG_ANALYZER_NONNULL = YES;
				CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE;
				CLANG_CXX_LANGUAGE_STANDARD = "gnu++14";
				CLANG_CXX_LIBRARY = "libc++";
				CLANG_ENABLE_MODULES = YES;
				CLANG_ENABLE_OBJC_ARC = YES;
				CLANG_ENABLE_OBJC_WEAK = YES;
				CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES;
				CLANG_WARN_BOOL_CONVERSION = YES;
				CLANG_WARN_COMMA = YES;
				CLANG_WARN_CONSTANT_CONVERSION = YES;
				CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES;
				CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
				CLANG_WARN_DOCUMENTATION_COMMENTS = YES;
				CLANG_WARN_EMPTY_BODY = YES;
				CLANG_WARN_ENUM_CONVERSION = YES;
				CLANG_WARN_INFINITE_RECURSION = YES;
				CLANG_WARN_INT_CONVERSION = YES;
				CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES;
				CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES;
				CLANG_WARN_OBJC_LITERAL_CONVERSION = YES;
				CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
				CLANG_WARN_RANGE_LOOP_ANALYSIS = YES;
				CLANG_WARN_STRICT_PROTOTYPES = YES;
				CLANG_WARN_SUSPICIOUS_MOVE = YES;
				CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE;
				CLANG_WARN_UNREACHABLE_CODE = YES;
				CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
				CODE_SIGN_IDENTITY = "iPhone Developer";
				COPY_PHASE_STRIP = NO;
				DEBUG_INFORMATION_FORMAT = dwarf;
				ENABLE_STRICT_OBJC_MSGSEND = YES;
				ENABLE_TESTABILITY = YES;
				GCC_C_LANGUAGE_STANDARD = gnu11;
				GCC_DYNAMIC_NO_PIC = NO;
				GCC_NO_COMMON_BLOCKS = YES;
				GCC_OPTIMIZATION_LEVEL = 0;
				GCC_PREPROCESSOR_DEFINITIONS = (
					"DEBUG=1",
					"$(inherited)",
				);
				GCC_WARN_64_TO_32_BIT_CONVERSION = YES;
				GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR;
				GCC_WARN_UNDECLARED_SELECTOR = YES;
				GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
				GCC_WARN_UNUSED_FUNCTION = YES;
				GCC_WARN_UNUSED_VARIABLE = YES;
				IPHONEOS_DEPLOYMENT_TARGET = 11.4;
				MTL_ENABLE_DEBUG_INFO = YES;
				ONLY_ACTIVE_ARCH = YES;
				SDKROOT = iphoneos;
			};
			name = Debug;
		};
		64CCFF5F2121A32B00B48E05 /* Release */ = {
			isa = XCBuildConfiguration;
			buildSettings = {
				ALWAYS_SEARCH_USER_PATHS = NO;
				CLANG_ANALYZER_NONNULL = YES;
				CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE;
				CLANG_CXX_LANGUAGE_STANDARD = "gnu++14";
				CLANG_CXX_LIBRARY = "libc++";
				CLANG_ENABLE_MODULES = YES;
				CLANG_ENABLE_OBJC_ARC = YES;
				CLANG_ENABLE_OBJC_WEAK = YES;
				CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES;
				CLANG_WARN_BOOL_CONVERSION = YES;
				CLANG_WARN_COMMA = YES;
				CLANG_WARN_CONSTANT_CONVERSION = YES;
				CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES;
				CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
				CLANG_WARN_DOCUMENTATION_COMMENTS = YES;
				CLANG_WARN_EMPTY_BODY = YES;
				CLANG_WARN_ENUM_CONVERSION = YES;
				CLANG_WARN_INFINITE_RECURSION = YES;
				CLANG_WARN_INT_CONVERSION = YES;
				CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES;
				CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES;
				CLANG_WARN_OBJC_LITERAL_CONVERSION = YES;
				CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
				CLANG_WARN_RANGE_LOOP_ANALYSIS = YES;
				CLANG_WARN_STRICT_PROTOTYPES = YES;
				CLANG_WARN_SUSPICIOUS_MOVE = YES;
				CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE;
				CLANG_WARN_UNREACHABLE_CODE = YES;
				CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
				CODE_SIGN_IDENTITY = "iPhone Developer";
				COPY_PHASE_STRIP = NO;
				DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym";
				ENABLE_NS_ASSERTIONS = NO;
				ENABLE_STRICT_OBJC_MSGSEND = YES;
				GCC_C_LANGUAGE_STANDARD = gnu11;
				GCC_NO_COMMON_BLOCKS = YES;
				GCC_WARN_64_TO_32_BIT_CONVERSION = YES;
				GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR;
				GCC_WARN_UNDECLARED_SELECTOR = YES;
				GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
				GCC_WARN_UNUSED_FUNCTION = YES;
				GCC_WARN_UNUSED_VARIABLE = YES;
				IPHONEOS_DEPLOYMENT_TARGET = 11.4;
				MTL_ENABLE_DEBUG_INFO = NO;
				SDKROOT = iphoneos;
				VALIDATE_PRODUCT = YES;
			};
			name = Release;
		};
		64CCFF612121A32B00B48E05 /* Debug */ = {
			isa = XCBuildConfiguration;
			buildSettings = {
				ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
				CODE_SIGN_STYLE = Automatic;
				DEVELOPMENT_TEAM = 9NFTYP4MQ3;
				FRAMEWORK_SEARCH_PATHS = (
					"$(inherited)",
					"$(PROJECT_DIR)/gophers",
				);
				INFOPLIST_FILE = gophers/Info.plist;
				IPHONEOS_DEPLOYMENT_TARGET = 9.0;
				LD_RUNPATH_SEARCH_PATHS = (
					"$(inherited)",
					"@executable_path/Frameworks",
				);
				PRODUCT_BUNDLE_IDENTIFIER = com.eliasnaur.gophers;
				PRODUCT_NAME = "$(TARGET_NAME)";
				TARGETED_DEVICE_FAMILY = "1,2";
			};
			name = Debug;
		};
		64CCFF622121A32B00B48E05 /* Release */ = {
			isa = XCBuildConfiguration;
			buildSettings = {
				ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
				CODE_SIGN_STYLE = Automatic;
				DEVELOPMENT_TEAM = 9NFTYP4MQ3;
				FRAMEWORK_SEARCH_PATHS = (
					"$(inherited)",
					"$(PROJECT_DIR)/gophers",
				);
				INFOPLIST_FILE = gophers/Info.plist;
				IPHONEOS_DEPLOYMENT_TARGET = 9.0;
				LD_RUNPATH_SEARCH_PATHS = (
					"$(inherited)",
					"@executable_path/Frameworks",
				);
				PRODUCT_BUNDLE_IDENTIFIER = com.eliasnaur.gophers;
				PRODUCT_NAME = "$(TARGET_NAME)";
				TARGETED_DEVICE_FAMILY = "1,2";
			};
			name = Release;
		};
/* End XCBuildConfiguration section */

/* Begin XCConfigurationList section */
		64CCFF3E2121A32800B48E05 /* Build configuration list for PBXProject "gophers" */ = {
			isa = XCConfigurationList;
			buildConfigurations = (
				64CCFF5E2121A32B00B48E05 /* Debug */,
				64CCFF5F2121A32B00B48E05 /* Release */,
			);
			defaultConfigurationIsVisible = 0;
			defaultConfigurationName = Release;
		};
		64CCFF602121A32B00B48E05 /* Build configuration list for PBXNativeTarget "gophers" */ = {
			isa = XCConfigurationList;
			buildConfigurations = (
				64CCFF612121A32B00B48E05 /* Debug */,
				64CCFF622121A32B00B48E05 /* Release */,
			);
			defaultConfigurationIsVisible = 0;
			defaultConfigurationName = Release;
		};
/* End XCConfigurationList section */
	};
	rootObject = 64CCFF3B2121A32800B48E05 /* Project object */;
}

A  => apps/gophers/ios/gophers.xcodeproj/project.xcworkspace/contents.xcworkspacedata +10 -0
@@ 1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<Workspace
   version = "1.0">
   <FileRef
      location = "group:../gophers/Gophers.framework">
   </FileRef>
   <FileRef
      location = "self:">
   </FileRef>
</Workspace>

A  => apps/gophers/ios/gophers.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist +8 -0
@@ 1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
	<key>IDEDidComputeMac32BitWarning</key>
	<true/>
</dict>
</plist>

A  => apps/gophers/ios/gophers.xcodeproj/project.xcworkspace/xcshareddata/WorkspaceSettings.xcsettings +5 -0
@@ 1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict/>
</plist>

A  => apps/gophers/ios/gophers/Assets.xcassets/Contents.json +6 -0
@@ 1,6 @@
{
  "info" : {
    "version" : 1,
    "author" : "xcode"
  }
}
\ No newline at end of file

A  => apps/gophers/ios/gophers/Info.plist +37 -0
@@ 1,37 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
	<key>CFBundleDevelopmentRegion</key>
	<string>$(DEVELOPMENT_LANGUAGE)</string>
	<key>CFBundleExecutable</key>
	<string>$(EXECUTABLE_NAME)</string>
	<key>CFBundleIdentifier</key>
	<string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
	<key>CFBundleInfoDictionaryVersion</key>
	<string>6.0</string>
	<key>CFBundleName</key>
	<string>$(PRODUCT_NAME)</string>
	<key>CFBundlePackageType</key>
	<string>APPL</string>
	<key>CFBundleShortVersionString</key>
	<string>1.0</string>
	<key>CFBundleVersion</key>
	<string>1</string>
	<key>UILaunchStoryboardName</key>
	<string>LaunchScreen</string>
	<key>UISupportedInterfaceOrientations</key>
	<array>
		<string>UIInterfaceOrientationPortrait</string>
		<string>UIInterfaceOrientationLandscapeLeft</string>
		<string>UIInterfaceOrientationLandscapeRight</string>
	</array>
	<key>UISupportedInterfaceOrientations~ipad</key>
	<array>
		<string>UIInterfaceOrientationPortrait</string>
		<string>UIInterfaceOrientationPortraitUpsideDown</string>
		<string>UIInterfaceOrientationLandscapeLeft</string>
		<string>UIInterfaceOrientationLandscapeRight</string>
	</array>
</dict>
</plist>

A  => apps/gophers/ios/gophers/main.m +8 -0
@@ 1,8 @@
@import UIKit;
@import Gophers;

int main(int argc, char * argv[]) {
    @autoreleasepool {
        return UIApplicationMain(argc, argv, nil, NSStringFromClass([GioAppDelegate class]));
    }
}

A  => apps/gophers/main.go +726 -0
@@ 1,726 @@
// SPDX-License-Identifier: Unlicense OR MIT

package main

import (
	"context"
	"flag"
	"fmt"
	"image"
	"image/color"
	"log"
	"net/http"
	"os"

	"golang.org/x/image/draw"
	"golang.org/x/oauth2"

	_ "image/jpeg"
	_ "image/png"

	_ "net/http/pprof"

	"gioui.org/ui"
	"gioui.org/ui/app"
	gdraw "gioui.org/ui/draw"
	"gioui.org/ui/f32"
	"gioui.org/ui/gesture"
	"gioui.org/ui/key"
	"gioui.org/ui/layout"
	"gioui.org/ui/measure"
	"gioui.org/ui/pointer"
	"gioui.org/ui/text"
	"gioui.org/ui/widget"
	"golang.org/x/exp/shiny/iconvg"

	"github.com/google/go-github/v24/github"
	"golang.org/x/image/font/gofont/gobold"
	"golang.org/x/image/font/gofont/goitalic"
	"golang.org/x/image/font/gofont/gomono"
	"golang.org/x/image/font/gofont/goregular"
	"golang.org/x/image/font/sfnt"

	"golang.org/x/exp/shiny/materialdesign/icons"
)

type App struct {
	w     *app.Window
	cfg   *ui.Config
	faces measure.Faces

	pqueue *pointer.Queue
	kqueue *key.Queue

	fab *ActionButton

	usersList   *layout.List
	edit, edit2 *text.Editor

	users        []*user
	userClicks   []gesture.Click
	selectedUser *userPage

	updateUsers chan []*user
}

type userPage struct {
	cfg           *ui.Config
	faces         measure.Faces
	redraw        redrawer
	user          *user
	commitsList   *layout.List
	commits       []*github.Commit
	commitsResult chan []*github.Commit
}

type user struct {
	name    string
	login   string
	company string
	avatar  image.Image
}

type icon struct {
	src  []byte
	size ui.Value

	// Cached values.
	img     image.Image
	imgSize float32
}

type redrawer func()

type ActionButton struct {
	face        text.Face
	cfg         *ui.Config
	Open        bool
	icons       []*icon
	sendIco     *icon
	btnClicker  *gesture.Click
	btnsClicker *gesture.Click
}

var (
	profile = flag.Bool("profile", false, "serve profiling data at http://localhost:6060")
	stats   = flag.Bool("stats", false, "show rendering statistics")
	token   = flag.String("token", "", "Github authentication token")
)

var fonts struct {
	regular *sfnt.Font
	bold    *sfnt.Font
	italic  *sfnt.Font
	mono    *sfnt.Font
}

func main() {
	if *token == "" {
		fmt.Println("The quota for anonymous GitHub API access is very low. Specify a token with -token to avoid quota errors.")
		fmt.Println("See https://help.github.com/en/articles/creating-a-personal-access-token-for-the-command-line.")
	}
	err := app.CreateWindow(app.WindowOptions{
		Width:  ui.Dp(400),
		Height: ui.Dp(800),
		Title:  "Gopher Chat",
	})
	if err != nil {
		log.Fatal(err)
	}
	app.Main()
}

func initProfiling() {
	if !*profile {
		return
	}
	go func() {
		log.Println(http.ListenAndServe("localhost:6060", nil))
	}()
}

func init() {
	flag.Parse()
	initProfiling()
	fonts.regular = mustLoadFont(goregular.TTF)
	fonts.bold = mustLoadFont(gobold.TTF)
	fonts.italic = mustLoadFont(goitalic.TTF)
	fonts.mono = mustLoadFont(gomono.TTF)
	go func() {
		for w := range app.Windows() {
			w := w
			go func() {
				if err := newApp(w).run(); err != nil {
					log.Fatal(err)
				}
			}()
		}
	}()
}

func (a *App) run() error {
	a.w.Profiling = *stats
	for a.w.IsAlive() {
		select {
		case users := <-a.updateUsers:
			a.users = users
			a.userClicks = make([]gesture.Click, len(users))
			a.w.Redraw()
		case e := <-a.w.Events():
			switch e := e.(type) {
			case pointer.Event:
				a.pqueue.Push(e)
			case key.Event:
				a.kqueue.Push(e)
				if e, ok := e.(key.Chord); ok {
					switch e.Name {
					case key.NameEscape:
						os.Exit(0)
					case 'P':
						if e.Modifiers&key.ModCommand != 0 {
							a.w.Profiling = !a.w.Profiling
						}
					}
				}
			case app.ChangeStage:
			case app.Draw:
				a.cfg = e.Config
				a.faces.Cfg = a.cfg
				cs := layout.ExactConstraints(a.w.Size())
				root, _ := a.Layout(cs)
				if a.w.Profiling {
					op, _ := layout.Align(
						layout.NE,
						layout.Margin(a.cfg,
							layout.Margins{Top: ui.Dp(16)},
							text.Label{Src: textColor, Face: a.face(fonts.mono, 8), Text: a.w.Timings()},
						),
					).Layout(cs)
					root = ui.Ops{root, op}
				}
				a.w.Draw(root)
				a.w.SetTextInput(a.kqueue.Frame(root))
				a.pqueue.Frame(root)
				a.faces.Frame()
			}
			a.w.Ack()
		}
	}
	return a.w.Err()
}

func newApp(w *app.Window) *App {
	a := &App{
		w:           w,
		updateUsers: make(chan []*user),
		pqueue:      new(pointer.Queue),
		kqueue:      new(key.Queue),
	}
	a.usersList = &layout.List{Axis: layout.Vertical}
	a.fab = &ActionButton{
		face:        a.face(fonts.regular, 9),
		sendIco:     &icon{src: icons.ContentSend, size: ui.Dp(24)},
		icons:       []*icon{},
		btnClicker:  new(gesture.Click),
		btnsClicker: new(gesture.Click),
	}
	a.edit2 = &text.Editor{
		Src:  textColor,
		Face: a.face(fonts.italic, 14),
		//Alignment: text.End,
		SingleLine: true,
	}
	a.edit2.SetText("Single line editor. Edit me!")
	a.edit = &text.Editor{
		Src:  textColor,
		Face: a.face(fonts.regular, 14),
		//Alignment: text.End,
		//SingleLine: true,
	}
	a.edit.SetText(longTextSample)
	go a.fetchContributors()
	return a
}

func githubClient(ctx context.Context) *github.Client {
	var tc *http.Client
	if *token != "" {
		ts := oauth2.StaticTokenSource(
			&oauth2.Token{AccessToken: *token},
		)
		tc = oauth2.NewClient(ctx, ts)
	}
	return github.NewClient(tc)
}

func (a *App) fetchContributors() {
	ctx := context.Background()
	client := githubClient(ctx)
	cons, _, err := client.Repositories.ListContributors(ctx, "golang", "go", nil)
	if err != nil {
		fmt.Fprintf(os.Stderr, "github: failed to fetch contributors: %v\n", err)
		return
	}
	var users []*user
	userErrs := make(chan error, len(cons))
	avatarErrs := make(chan error, len(cons))
	for _, con := range cons {
		con := con
		avatar := con.GetAvatarURL()
		if avatar == "" {
			continue
		}
		u := &user{
			login: con.GetLogin(),
		}
		users = append(users, u)
		go func() {
			guser, _, err := client.Users.Get(ctx, u.login)
			if err != nil {
				avatarErrs <- err
				return
			}
			u.name = guser.GetName()
			u.company = guser.GetCompany()
			avatarErrs <- nil
		}()
		go func() {
			a, err := fetchImage(avatar)
			u.avatar = a
			userErrs <- err
		}()
	}
	for i := 0; i < len(cons); i++ {
		if err := <-userErrs; err != nil {
			fmt.Fprintf(os.Stderr, "github: failed to fetch user: %v\n", err)
		}
		if err := <-avatarErrs; err != nil {
			fmt.Fprintf(os.Stderr, "github: failed to fetch avatar: %v\n", err)
		}
	}
	// Drop users with no avatar or name.
	for i := len(users) - 1; i >= 0; i-- {
		if u := users[i]; u.name == "" || u.avatar == nil {
			users = append(users[:i], users[i+1:]...)
		}
	}
	a.updateUsers <- users
}

func fetchImage(url string) (image.Image, error) {
	resp, err := http.Get(url)
	if err != nil {
		return nil, fmt.Errorf("fetchImage: http.Get(%q): %v", url, err)
	}
	defer resp.Body.Close()
	img, _, err := image.Decode(resp.Body)
	if err != nil {
		return nil, fmt.Errorf("fetchImage: image decode failed: %v", err)
	}
	return img, nil
}

func mustLoadFont(fontData []byte) *sfnt.Font {
	fnt, err := sfnt.Parse(fontData)
	if err != nil {
		panic("failed to load font")
	}
	return fnt
}

var (
	backgroundColor = rgb(0xfbfbfb)
	brandColor      = rgb(0x62798c)
	divColor        = rgb(0xecedef)
	textColor       = rgb(0x333333)
	secTextColor    = rgb(0xe0e4e8)
	tertTextColor   = rgb(0xbbbbbb)
	whiteColor      = rgb(0xffffff)
	accentColor     = rgb(0x00c28c)
)

func rgb(c uint32) *image.Uniform {
	return argb((0xff << 24) | c)
}

func argb(c uint32) *image.Uniform {
	col := color.NRGBA{A: uint8(c >> 24), R: uint8(c >> 16), G: uint8(c >> 8), B: uint8(c)}
	return &image.Uniform{col}
}

func (a *App) face(f *sfnt.Font, size float32) text.Face {
	return a.faces.For(f, ui.Sp(size))
}

func (a *App) Layout(cs layout.Constraints) (ui.Op, layout.Dimens) {
	if a.selectedUser == nil {
		return a.layoutUsers(cs)
	} else {
		a.selectedUser.Update(a.pqueue)
		return a.selectedUser.Layout(cs)
	}
}

func newUserPage(cfg *ui.Config, user *user, redraw redrawer, faces measure.Faces) *userPage {
	up := &userPage{
		cfg:           cfg,
		faces:         faces,
		redraw:        redraw,
		user:          user,
		commitsList:   &layout.List{Axis: layout.Vertical},
		commitsResult: make(chan []*github.Commit, 1),
	}
	up.fetchCommits()
	return up
}

func (up *userPage) Update(pqueue pointer.Events) {
	up.commitsList.Scroll(up.cfg, pqueue)
}

func (up *userPage) Layout(cs layout.Constraints) (ui.Op, layout.Dimens) {
	l := up.commitsList
	var ops ui.Ops
	if l.Dragging() {
		ops = append(ops, key.OpHideInput{})
	}
	select {
	case commits := <-up.commitsResult:
		up.commits = commits
	default:
	}
	for i, ok := l.Init(cs, len(up.commits)); ok; i, ok = l.Index() {
		l.Elem(up.commit(i))
	}
	op, dims := l.Layout()
	return append(ops, op), dims
}

func (up *userPage) commit(index int) layout.Widget {
	sz := ui.Dp(48)
	u := up.user
	c := up.cfg
	avatar := clipCircle(layout.Sized(c, sz, sz, widget.Image{Src: u.avatar, Rect: u.avatar.Bounds()}))
	msg := up.commits[index].GetMessage()
	label := text.Label{Src: textColor, Face: up.faces.For(fonts.regular, ui.Sp(12)), Text: msg}
	return layout.Margin(c,
		layout.Margins{Top: ui.Dp(16), Right: ui.Dp(8), Left: ui.Dp(8)},
		layout.F(func(cs layout.Constraints) (ui.Op, layout.Dimens) {
			return (&layout.Flex{Axis: layout.Horizontal, MainAxisAlignment: layout.Start, CrossAxisAlignment: layout.Start}).
				Init(cs).
				Rigid(avatar).
				Flexible(-1, 1, layout.Fit, layout.Margin(c, layout.Margins{Left: ui.Dp(8)}, label)).
				Layout()
		}),
	)
}

func (up *userPage) fetchCommits() {
	go func() {
		ctx := context.Background()
		gh := githubClient(ctx)
		repoCommits, _, err := gh.Repositories.ListCommits(ctx, "golang", "go", &github.CommitsListOptions{
			Author: up.user.login,
		})
		if err != nil {
			log.Fatal(err)
		}
		var commits []*github.Commit
		for _, commit := range repoCommits {
			if c := commit.GetCommit(); c != nil {
				commits = append(commits, c)
			}
		}
		up.commitsResult <- commits
		up.redraw()
	}()
}

func (a *App) layoutUsers(cs layout.Constraints) (ui.Op, layout.Dimens) {
	c := a.cfg
	a.fab.Update(c, a.pqueue)
	st := (&layout.Stack{Alignment: layout.Center}).Init(cs).
		Rigid(layout.Align(
			layout.SE,
			layout.Margin(c,
				layout.EqualMargins(ui.Dp(16)),
				a.fab,
			),
		))
	a.edit.Update(c, a.pqueue, a.kqueue)
	a.edit2.Update(c, a.pqueue, a.kqueue)
	return st.Expand(0, layout.F(func(cs layout.Constraints) (ui.Op, layout.Dimens) {
		return (&layout.Flex{Axis: layout.Vertical, MainAxisAlignment: layout.Start, CrossAxisAlignment: layout.Stretch}).Init(cs).
			Rigid(layout.Margin(c,
				layout.EqualMargins(ui.Dp(16)),
				layout.Sized(c, ui.Dp(0), ui.Dp(200), a.edit),
			)).
			Rigid(layout.Margin(c,
				layout.Margins{Bottom: ui.Dp(16), Left: ui.Dp(16), Right: ui.Dp(16)},
				a.edit2,
			)).
			Rigid(layout.F(func(cs layout.Constraints) (ui.Op, layout.Dimens) {
				return (&layout.Stack{Alignment: layout.Center}).Init(cs).
					Rigid(layout.Margin(c,
						layout.Margins{Top: ui.Dp(16), Right: ui.Dp(8), Bottom: ui.Dp(8), Left: ui.Dp(8)},
						text.Label{Src: rgb(0x888888), Face: a.face(fonts.regular, 9), Text: "GOPHERS"},
					)).
					Expand(0, fill(rgb(0xf2f2f2))).
					Layout()
			})).
			Flexible(-1, 1, layout.Fit, a.layoutContributors()).
			Layout()
	})).
		Layout()
}

func (a *ActionButton) Update(c *ui.Config, q pointer.Events) {
	a.cfg = c
	a.btnsClicker.Update(q)
	a.btnClicker.Update(q)
}

func (a *ActionButton) Layout(cs layout.Constraints) (ui.Op, layout.Dimens) {
	c := a.cfg
	fl := (&layout.Flex{Axis: layout.Vertical, MainAxisAlignment: layout.Start, CrossAxisAlignment: layout.End, MainAxisSize: layout.Min}).Init(cs)
	fabCol := brandColor
	fl.Rigid(layout.Margin(c,
		layout.Margins{Top: ui.Dp(4)},
		layout.F(func(cs layout.Constraints) (ui.Op, layout.Dimens) {
			op, dims := fab(c, a.sendIco.image(c), fabCol, ui.Dp(56)).Layout(cs)
			ops := ui.Ops{op, a.btnClicker.Op(gesture.Ellipse(dims.Size))}
			return ops, dims
		}),
	))
	return fl.Layout()
}

func (a *App) layoutContributors() layout.Widget {
	return layout.F(func(cs layout.Constraints) (ui.Op, layout.Dimens) {
		c := a.cfg
		l := a.usersList
		l.Scroll(c, a.pqueue)
		var ops ui.Ops
		if l.Dragging() {
			ops = append(ops, key.OpHideInput{})
		}
		for i, ok := l.Init(cs, len(a.users)); ok; i, ok = l.Index() {
			l.Elem(a.user(c, i))
		}
		op, dims := l.Layout()
		return append(ops, op), dims
	})
}

func (a *App) user(c *ui.Config, index int) layout.Widget {
	u := a.users[index]
	click := &a.userClicks[index]
	sz := ui.Dp(48)
	for _, r := range click.Update(a.pqueue) {
		if r.Type == gesture.TypeClick {
			a.selectedUser = newUserPage(a.cfg, u, a.w.Redraw, a.faces)
		}
	}
	avatar := clipCircle(layout.Sized(a.cfg, sz, sz, widget.Image{Src: u.avatar, Rect: u.avatar.Bounds()}))
	return layout.F(func(cs layout.Constraints) (ui.Op, layout.Dimens) {
		elem := (&layout.Flex{Axis: layout.Vertical, MainAxisAlignment: layout.Start, CrossAxisAlignment: layout.Start}).Init(cs)
		elem.Rigid(layout.F(func(cs layout.Constraints) (ui.Op, layout.Dimens) {
			op, dims := layout.Margin(c,
				layout.EqualMargins(ui.Dp(8)),
				layout.F(func(cs layout.Constraints) (ui.Op, layout.Dimens) {
					return centerRowOpts().Init(cs).
						Rigid(layout.Margin(c, layout.Margins{Right: ui.Dp(8)}, avatar)).
						Rigid(column(
							baseline(
								text.Label{Src: textColor, Face: a.face(fonts.regular, 11), Text: u.name},
								layout.Align(layout.E, layout.Margin(c,
									layout.Margins{Left: ui.Dp(2)},
									text.Label{Src: textColor, Face: a.face(fonts.regular, 8), Text: "3 hours ago"},
								)),
							),
							layout.Margin(c,
								layout.Margins{Top: ui.Dp(4)},
								text.Label{Src: tertTextColor, Face: a.face(fonts.regular, 10), Text: u.company},
							),
						)).
						Layout()
				}),
			).Layout(cs)
			ops := ui.Ops{op, click.Op(gesture.Rect(dims.Size))}
			return ops, dims
		}))
		return elem.Layout()
	})
}

func fill(img image.Image) layout.Widget {
	return widget.Image{Src: img, Rect: image.Rectangle{Max: image.Point{X: 1, Y: 1}}}
}

func column(widgets ...layout.Widget) layout.Widget {
	return flex(&layout.Flex{Axis: layout.Vertical, MainAxisAlignment: layout.Start, CrossAxisAlignment: layout.Start}, widgets...)
}

func centerColumn(widgets ...layout.Widget) layout.Widget {
	return flex(&layout.Flex{Axis: layout.Vertical, MainAxisAlignment: layout.Start, CrossAxisAlignment: layout.Center, MainAxisSize: layout.Min}, widgets...)
}

func centerRowOpts(widgets ...layout.Widget) *layout.Flex {
	return &layout.Flex{Axis: layout.Horizontal, MainAxisAlignment: layout.Start, CrossAxisAlignment: layout.Center, MainAxisSize: layout.Min}
}

func centerRow(widgets ...layout.Widget) layout.Widget {
	return flex(centerRowOpts(), widgets...)
}

func baseline(widgets ...layout.Widget) layout.Widget {
	return flex(&layout.Flex{Axis: layout.Horizontal, CrossAxisAlignment: layout.Baseline, MainAxisSize: layout.Min}, widgets...)
}

func flex(f *layout.Flex, widgets ...layout.Widget) layout.Widget {
	return layout.F(func(cs layout.Constraints) (ui.Op, layout.Dimens) {
		f.Init(cs)
		for _, w := range widgets {
			f.Rigid(w)
		}
		return f.Layout()
	})
}

func clipCircle(w layout.Widget) layout.Widget {
	return layout.F(func(cs layout.Constraints) (ui.Op, layout.Dimens) {
		op, dims := w.Layout(cs)
		max := dims.Size.X
		if dy := dims.Size.Y; dy > max {
			max = dy
		}
		szf := float32(max)
		rr := szf * .5
		op = gdraw.OpClip{
			Path: rrect(szf, szf, rr, rr, rr, rr),
			Op:   op,
		}
		return op, dims
	})
}

func fab(c *ui.Config, ico, col image.Image, size ui.Value) layout.Widget {
	return layout.F(func(cs layout.Constraints) (ui.Op, layout.Dimens) {
		szf := c.Pixels(size)
		sz := int(szf + .5)
		rr := szf * .5
		dp := image.Point{X: (sz - ico.Bounds().Dx()) / 2, Y: (sz - ico.Bounds().Dy()) / 2}
		dims := image.Point{X: sz, Y: sz}
		op := gdraw.OpClip{
			Path: rrect(szf, szf, rr, rr, rr, rr),
			Op: ui.Ops{
				gdraw.OpImage{Rect: f32.Rectangle{Max: f32.Point{X: float32(sz), Y: float32(sz)}}, Src: col, SrcRect: col.Bounds()},
				gdraw.OpImage{
					Rect:    toRectF(ico.Bounds().Add(dp)),
					Src:     ico,
					SrcRect: ico.Bounds(),
				},
			},
		}
		return op, layout.Dimens{Size: dims}
	})
}

func toRectF(r image.Rectangle) f32.Rectangle {
	return f32.Rectangle{
		Min: f32.Point{X: float32(r.Min.X), Y: float32(r.Min.Y)},
		Max: f32.Point{X: float32(r.Max.X), Y: float32(r.Max.Y)},
	}
}

func (ic *icon) image(cfg *ui.Config) image.Image {
	sz := cfg.Pixels(ic.size)
	if sz == ic.imgSize {
		return ic.img
	}
	m, _ := iconvg.DecodeMetadata(ic.src)
	dx, dy := m.ViewBox.AspectRatio()
	img := image.NewNRGBA(image.Rectangle{Max: image.Point{X: int(sz), Y: int(sz * dy / dx)}})
	var ico iconvg.Rasterizer
	ico.SetDstImage(img, img.Bounds(), draw.Src)
	// Use white for icons.
	m.Palette[0] = color.RGBA{A: 0xff, R: 0xff, G: 0xff, B: 0xff}
	iconvg.Decode(&ico, ic.src, &iconvg.DecodeOptions{
		Palette: &m.Palette,
	})
	ic.img = img
	ic.imgSize = sz
	return img
}

// https://pomax.github.io/bezierinfo/#circles_cubic.
func rrect(width, height, se, sw, nw, ne float32) *gdraw.Path {
	w, h := float32(width), float32(height)
	const c = 0.55228475 // 4*(sqrt(2)-1)/3
	var b gdraw.PathBuilder
	b.Move(f32.Point{X: w, Y: h - se})
	b.Cube(f32.Point{X: 0, Y: se * c}, f32.Point{X: -se + se*c, Y: se}, f32.Point{X: -se, Y: se}) // SE
	b.Line(f32.Point{X: sw - w + se, Y: 0})
	b.Cube(f32.Point{X: -sw * c, Y: 0}, f32.Point{X: -sw, Y: -sw + sw*c}, f32.Point{X: -sw, Y: -sw}) // SW
	b.Line(f32.Point{X: 0, Y: nw - h + sw})
	b.Cube(f32.Point{X: 0, Y: -nw * c}, f32.Point{X: nw - nw*c, Y: -nw}, f32.Point{X: nw, Y: -nw}) // NW
	b.Line(f32.Point{X: w - ne - nw, Y: 0})
	b.Cube(f32.Point{X: ne * c, Y: 0}, f32.Point{X: ne, Y: ne - ne*c}, f32.Point{X: ne, Y: ne}) // NE
	return b.Path()
}

const longTextSample = `1. I learned from my grandfather, Verus, to use good manners, and to
put restraint on anger. 2. In the famous memory of my father I had a
pattern of modesty and manliness. 3. Of my mother I learned to be
pious and generous; to keep myself not only from evil deeds, but even
from evil thoughts; and to live with a simplicity which is far from
customary among the rich. 4. I owe it to my great-grandfather that I
did not attend public lectures and discussions, but had good and able
teachers at home; and I owe him also the knowledge that for things of
this nature a man should count no expense too great.

5. My tutor taught me not to favour either green or blue at the
chariot races, nor, in the contests of gladiators, to be a supporter
either of light or heavy armed. He taught me also to endure labour;
not to need many things; to serve myself without troubling others; not
to intermeddle in the affairs of others, and not easily to listen to
slanders against them.

6. Of Diognetus I had the lesson not to busy myself about vain things;
not to credit the great professions of such as pretend to work
wonders, or of sorcerers about their charms, and their expelling of
Demons and the like; not to keep quails (for fighting or divination),
nor to run after such things; to suffer freedom of speech in others,
and to apply myself heartily to philosophy. Him also I must thank for
my hearing first Bacchius, then Tandasis and Marcianus; that I wrote
dialogues in my youth, and took a liking to the philosopher's pallet
and skins, and to the other things which, by the Grecian discipline,
belong to that profession.

7. To Rusticus I owe my first apprehensions that my nature needed
reform and cure; and that I did not fall into the ambition of the
common Sophists, either by composing speculative writings or by
declaiming harangues of exhortation in public; further, that I never
strove to be admired by ostentation of great patience in an ascetic
life, or by display of activity and application; that I gave over the
study of rhetoric, poetry, and the graces of language; and that I did
not pace my house in my senatorial robes, or practise any similar
affectation. I observed also the simplicity of style in his letters,
particularly in that which he wrote to my mother from Sinuessa. I
learned from him to be easily appeased, and to be readily reconciled
with those who had displeased me or given cause of offence, so soon as
they inclined to make their peace; to read with care; not to rest
satisfied with a slight and superficial knowledge; nor quickly to
assent to great talkers. I have him to thank that I met with the
discourses of Epictetus, which he furnished me from his own library.

8. From Apollonius I learned true liberty, and tenacity of purpose; to
regard nothing else, even in the smallest degree, but reason always;
and always to remain unaltered in the agonies of pain, in the losses
of children, or in long diseases. He afforded me a living example of
how the same man can, upon occasion, be most yielding and most
inflexible. He was patient in exposition; and, as might well be seen,
esteemed his fine skill and ability in teaching others the principles
of philosophy as the least of his endowments. It was from him that I
learned how to receive from friends what are thought favours without
seeming humbled by the giver or insensible to the gift.`

A  => cmd/assets/assets.go +57 -0
@@ 1,57 @@
// SPDX-License-Identifier: Unlicense OR MIT

// Command assets converts data files to Go source code.
package main

import (
	"bytes"
	"fmt"
	"io/ioutil"
	"os"
	"path/filepath"
	"strings"
)

func main() {
	if len(os.Args) != 2 {
		fmt.Fprintln(os.Stderr, "please specify a directory to convert")
		os.Exit(1)
	}
	var w bytes.Buffer
	w.WriteString("// Code generated by command assets DO NOT EDIT.\n\n")
	w.WriteString("package assets\n\n")
	w.WriteString("import (\n")
	w.WriteString("\t\"io\"\n")
	w.WriteString("\t\"strings\"\n")
	w.WriteString(")\n\n")
	dir := os.Args[1]
	filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
		if err != nil {
			return err
		}
		if info.IsDir() {
			return nil
		}
		if filepath.Ext(path) == ".go" {
			return nil
		}
		name := path[len(dir)+1:]
		name = strings.ReplaceAll(name, "/", "_")
		name = strings.ReplaceAll(name, ".", "_")
		name = strings.Title(name)
		w.WriteString(fmt.Sprintf("func %s() io.Reader {\n", name))
		content, err := ioutil.ReadFile(path)
		if err != nil {
			return err
		}
		w.WriteString(fmt.Sprintf("\tcontent := %q\n", content))
		w.WriteString("\treturn strings.NewReader(content)\n")
		w.WriteString("}\n")
		return nil
	})
	err := ioutil.WriteFile(filepath.Join(dir, "assets.go"), w.Bytes(), 0644)
	if err != nil {
		fmt.Fprintf(os.Stderr, "%v\n", err)
		os.Exit(1)
	}
}

A  => cmd/gio/gio.go +571 -0
@@ 1,571 @@
// SPDX-License-Identifier: Unlicense OR MIT

package main

import (
	"archive/zip"
	"bytes"
	"errors"
	"flag"
	"fmt"
	"io"
	"io/ioutil"
	"os"
	"os/exec"
	"path/filepath"
	"runtime"
	"strconv"
	"strings"

	"golang.org/x/sync/errgroup"
)

// zip.Writer with a sticky error.
type zipWriter struct {
	err error
	w   *zip.Writer
}

// Writer that saves any errors.
type errWriter struct {
	w   io.Writer
	err *error
}

var (
	target    = flag.String("target", "", "specify target (ios or android)")
	archNames = flag.String("arch", "", "specify architecture(s) to include")
	destPath  = flag.String("o", "", "output file (for Android .aar) or directory (for iOS .framework)")
	verbose   = flag.Bool("v", false, "verbose output")
)

func main() {
	flag.Usage = func() {
		fmt.Fprintf(os.Stderr, "Usage: %s [flags] <pkg>\n", os.Args[0])
		flag.PrintDefaults()
		os.Exit(2)
	}
	flag.Parse()
	pkg := flag.Arg(0)
	if pkg == "" {
		flag.Usage()
	}
	if *target == "" {
		fmt.Fprintf(os.Stderr, "Please specify -target\n\n")
		flag.PrintDefaults()
		os.Exit(2)
	}
	// Expand relative package paths.
	out, err := exec.Command("go", "list", pkg).CombinedOutput()
	out = bytes.TrimSpace(out)
	if err != nil {
		errorf("gio: %s", out)
	}
	pkg = string(out)
	appArgs := flag.Args()[1:]
	if err := run(pkg, appArgs); err != nil {
		errorf("gio: %v", err)
	}
}

func run(pkg string, appArgs []string) error {
	tmpDir, err := ioutil.TempDir("", "gio-")
	if err != nil {
		return err
	}
	defer os.RemoveAll(tmpDir)
	var ldflags string
	if len(appArgs) > 0 {
		// Pass along arguments to the app.
		ldflags = fmt.Sprintf("-X gioui.org/ui/app.extraArgs=%s", strings.Join(appArgs, "|"))
	}
	var archs []string
	switch *target {
	case "tvos":
		// Only 64-bit support.
		archs = []string{"arm64", "amd64"}
	default:
		archs = []string{"arm", "arm64", "386", "amd64"}
	}
	if *archNames != "" {
		archs = strings.Split(*archNames, ",")
	}
	switch *target {
	case "ios", "tvos":
		return runIOS(tmpDir, *target, pkg, archs, ldflags)
	case "android":
		return runAndroid(tmpDir, pkg, archs, ldflags)
	default:
		return fmt.Errorf("invalid -target %s\n", *target)
	}
}

func runIOS(tmpDir, target, pkg string, archs []string, ldflags string) error {
	frameworkRoot := *destPath
	if frameworkRoot == "" {
		appName := filepath.Base(pkg)
		frameworkRoot = fmt.Sprintf("%s.framework", strings.Title(appName))
	}
	framework := filepath.Base(frameworkRoot)
	suf := ".framework"
	if !strings.HasSuffix(framework, suf) {
		return fmt.Errorf("the specified output %q does not end in '.framework'", frameworkRoot)
	}
	framework = framework[:len(framework)-len(suf)]
	if err := os.RemoveAll(frameworkRoot); err != nil {
		return err
	}
	frameworkDir := filepath.Join(frameworkRoot, "Versions", "A")
	for _, dir := range []string{"Headers", "Modules"} {
		p := filepath.Join(frameworkDir, dir)
		if err := os.MkdirAll(p, 0755); err != nil {
			return err
		}
	}
	symlinks := [][2]string{
		{"Versions/Current/Headers", "Headers"},
		{"Versions/Current/Modules", "Modules"},
		{"Versions/Current/" + framework, framework},
		{"A", filepath.Join("Versions", "Current")},
	}
	for _, l := range symlinks {
		if err := os.Symlink(l[0], filepath.Join(frameworkRoot, l[1])); err != nil && !os.IsExist(err) {
			return err
		}
	}
	exe := filepath.Join(frameworkDir, framework)
	lipo := exec.Command("xcrun", "lipo", "-o", exe, "-create")
	var builds errgroup.Group
	for _, a := range archs {
		arch := allArchs[a]
		var platformSDK string
		var platformOS string
		switch target {
		case "ios":
			platformOS = "ios"
			platformSDK = "iphone"
		case "tvos":
			platformOS = "tvos"
			platformSDK = "appletv"
		}
		switch a {
		case "arm", "arm64":
			platformSDK += "os"
		case "386", "amd64":
			platformOS += "-simulator"
			platformSDK += "simulator"
		default:
			return fmt.Errorf("unsupported -arch: %s", a)
		}
		sdkPathOut, err := runCmd(exec.Command("xcrun", "--sdk", platformSDK, "--show-sdk-path"))
		if err != nil {
			return err
		}
		sdkPath := string(bytes.TrimSpace(sdkPathOut))
		clangOut, err := runCmd(exec.Command("xcrun", "--sdk", platformSDK, "--find", "clang"))
		if err != nil {
			return err
		}
		clang := string(bytes.TrimSpace(clangOut))
		cflags := fmt.Sprintf("-fmodules -fobjc-arc -fembed-bitcode -Werror -arch %s -isysroot %s -m%s-version-min=9.0", arch.iosArch, sdkPath, platformOS)
		lib := filepath.Join(tmpDir, "gio-"+a)
		cmd := exec.Command(
			"go",
			"build",
			"-ldflags=-s -w "+ldflags,
			"-buildmode=c-archive",
			"-o", lib,
			"-tags", "ios",
			pkg,
		)
		lipo.Args = append(lipo.Args, lib)
		cmd.Env = append(
			os.Environ(),
			"GOOS=darwin",
			"GOARCH="+a,
			"CGO_ENABLED=1",
			"CC="+clang,
			"CGO_CFLAGS="+cflags,
			"CGO_LDFLAGS="+cflags,
		)
		builds.Go(func() error {
			_, err := runCmd(cmd)
			return err
		})
	}
	if err := builds.Wait(); err != nil {
		return err
	}
	if _, err := runCmd(lipo); err != nil {
		return err
	}
	appDir, err := appDir()
	if err != nil {
		return err
	}
	headerDst := filepath.Join(frameworkDir, "Headers", framework+".h")
	headerSrc := filepath.Join(appDir, "framework_ios.h")
	if err := copyFile(headerDst, headerSrc); err != nil {
		return err
	}
	module := fmt.Sprintf(`framework module "%s" {
    header "%[1]s.h"

    export *
}`, framework)
	moduleFile := filepath.Join(frameworkDir, "Modules", "module.modulemap")
	return ioutil.WriteFile(moduleFile, []byte(module), 0644)
}

func runAndroid(tmpDir, pkg string, archs []string, ldflags string) (err error) {
	androidHome := os.Getenv("ANDROID_HOME")
	if androidHome == "" {
		return errors.New("ANDROID_HOME is not set. Please point it to the root of the Android SDK.")
	}
	ndkRoot := filepath.Join(androidHome, "ndk-bundle")
	if _, err := os.Stat(ndkRoot); err != nil {
		return fmt.Errorf("No NDK found in $ANDROID_HOME/ndk-bundle (%s). Use `sdkmanager ndk-bundle` to install it.", ndkRoot)
	}
	tcRoot := filepath.Join(ndkRoot, "toolchains", "llvm", "prebuilt", archNDK())
	sdk := os.Getenv("ANDROID_HOME")
	if sdk == "" {
		return errors.New("Please set ANDROID_HOME to the Android SDK path")
	}
	if _, err := os.Stat(sdk); err != nil {
		return err
	}
	platform, err := latestPlatform(sdk)
	if err != nil {
		return err
	}
	var builds errgroup.Group
	for _, a := range archs {
		arch := allArchs[a]
		clang := filepath.Join(tcRoot, "bin", arch.clang)
		if _, err := os.Stat(clang); err != nil {
			return fmt.Errorf("No NDK compiler found. Please make sure you have NDK >= r19c installed. Use the command `sdkmanager ndk-bundle` to install it. Path %s", clang)
		}
		if runtime.GOOS == "windows" {
			// Because of https://github.com/android-ndk/ndk/issues/920,
			// we need NDK r19c, not just r19b. Check for the presence of
			// clang++.cmd which is only available in r19c.
			clangpp := filepath.Join(tcRoot, "bin", arch.clang+"++.cmd")
			if _, err := os.Stat(clangpp); err != nil {
				return fmt.Errorf("NDK version r19b detected, but >= r19c is required. Use the command `sdkmanager ndk-bundle` to install it.")
			}
		}
		archDir := filepath.Join(tmpDir, "jni", arch.jniArch)
		if err := os.MkdirAll(archDir, 0755); err != nil {
			return fmt.Errorf("failed to create %q: %v", archDir, err)
		}
		libFile := filepath.Join(archDir, "libgio.so")
		cmd := exec.Command(
			"go",
			"build",
			"-ldflags=-w -s "+ldflags,
			"-buildmode=c-shared",
			"-o", libFile,
			pkg,
		)
		cmd.Env = append(
			os.Environ(),
			"GOOS=android",
			"GOARCH="+a,
			"CGO_ENABLED=1",
			"CC="+clang,
			"CGO_CFLAGS=-Werror",
		)
		builds.Go(func() error {
			_, err := runCmd(cmd)
			return err
		})
	}
	if err := builds.Wait(); err != nil {
		return err
	}
	aarFile := *destPath
	if aarFile == "" {
		aarFile = fmt.Sprintf("%s.aar", filepath.Base(pkg))
	}
	if filepath.Ext(aarFile) != ".aar" {
		return fmt.Errorf("the specified output %q does not end in '.aar'", aarFile)
	}
	aar, err := os.Create(aarFile)
	if err != nil {
		return err
	}
	defer func() {
		if cerr := aar.Close(); err == nil {
			err = cerr
		}
	}()
	aarw := newZipWriter(aar)
	defer aarw.Close()
	aarw.Create("R.txt")
	aarw.Create("res/")
	manifest := aarw.Create("AndroidManifest.xml")
	manifest.Write([]byte(`<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="org.gioui.app">
	<uses-sdk android:minSdkVersion="16"/>
	<uses-feature android:glEsVersion="0x00030000" android:required="true" />
</manifest>`))
	proguard := aarw.Create("proguard.txt")
	proguard.Write([]byte(`-keep class org.gioui.** { *; }`))

	for _, a := range archs {
		arch := allArchs[a]
		libFile := filepath.Join("jni", arch.jniArch, "libgio.so")
		aarw.Add(filepath.ToSlash(libFile), filepath.Join(tmpDir, libFile))
	}
	appDir, err := appDir()
	if err != nil {
		return err
	}
	javaFiles, err := filepath.Glob(filepath.Join(appDir, "*.java"))
	if err != nil {
		return err
	}
	if len(javaFiles) > 0 {
		clsPath := filepath.Join(platform, "android.jar")
		classes := filepath.Join(tmpDir, "classes")
		if err := os.MkdirAll(classes, 0755); err != nil {
			return err
		}
		javac := exec.Command(
			"javac",
			"-target", "1.8",
			"-source", "1.8",
			"-sourcepath", appDir,
			"-bootclasspath", clsPath,
			"-d", classes,
		)
		javac.Args = append(javac.Args, javaFiles...)
		if _, err := runCmd(javac); err != nil {
			return err
		}
		jarFile := filepath.Join(tmpDir, "classes.jar")
		if err := writeJar(jarFile, classes); err != nil {
			return err
		}
		aarw.Add("classes.jar", jarFile)
	}
	return aarw.Close()
}

func newZipWriter(w io.Writer) *zipWriter {
	return &zipWriter{
		w: zip.NewWriter(w),
	}
}

func (z *zipWriter) Close() error {
	err := z.w.Close()
	if z.err == nil {
		z.err = err
	}
	return z.err
}

func (z *zipWriter) Create(name string) io.Writer {
	if z.err != nil {
		return ioutil.Discard
	}
	w, err := z.w.Create(name)
	if err != nil {
		z.err = err
		return ioutil.Discard
	}
	return &errWriter{w: w, err: &z.err}
}

func (z *zipWriter) Add(name, file string) {
	if z.err != nil {
		return
	}
	w := z.Create(name)
	f, err := os.Open(file)
	if err != nil {
		z.err = err
		return
	}
	defer f.Close()
	if _, err := io.Copy(w, f); err != nil {
		z.err = err
		return
	}
}

func (w *errWriter) Write(p []byte) (n int, err error) {
	if err := *w.err; err != nil {
		return 0, err
	}
	n, err = w.w.Write(p)
	*w.err = err
	return
}

func errorf(format string, args ...interface{}) {
	fmt.Fprintf(os.Stderr, format+"\n", args...)
	os.Exit(2)
}

func runCmd(cmd *exec.Cmd) ([]byte, error) {
	if *verbose {
		fmt.Printf("%s\n", strings.Join(cmd.Args, " "))
	}
	out, err := cmd.Output()
	if err == nil {
		return out, nil
	}
	if err, ok := err.(*exec.ExitError); ok {
		return nil, fmt.Errorf("%s failed: %s%s", strings.Join(cmd.Args, " "), out, err.Stderr)
	}
	return nil, err
}

func copyFile(dst, src string) (err error) {
	r, err := os.Open(src)
	if err != nil {
		return err
	}
	defer r.Close()
	w, err := os.Create(dst)
	if err != nil {
		return err
	}
	defer func() {
		if cerr := w.Close(); err == nil {
			err = cerr
		}
	}()
	_, err = io.Copy(w, r)
	return err
}

func appDir() (string, error) {
	cmd := exec.Command("go", "list", "-f", "{{.Dir}}", "gioui.org/ui/app")
	cmd.Env = append(
		os.Environ(),
		"GOOS=android",
	)
	out, err := runCmd(cmd)
	if err != nil {
		return "", err
	}
	appDir := string(bytes.TrimSpace(out))
	return appDir, nil
}

func writeJar(jarFile, dir string) (err error) {
	jar, err := os.Create(jarFile)
	if err != nil {
		return err
	}
	defer func() {
		if cerr := jar.Close(); err == nil {
			err = cerr
		}
	}()
	jarw := newZipWriter(jar)
	const manifestHeader = `Manifest-Version: 1.0
Created-By: 1.0 (Go)

`
	jarw.Create("META-INF/MANIFEST.MF").Write([]byte(manifestHeader))
	err = filepath.Walk(dir, func(path string, f os.FileInfo, err error) error {
		if err != nil {
			return err
		}
		if f.IsDir() {
			return nil
		}
		if filepath.Ext(path) == ".class" {
			rel := filepath.ToSlash(path[len(dir)+1:])
			jarw.Add(rel, path)
		}
		return nil
	})
	if err != nil {
		return err
	}
	return jarw.Close()
}

type arch struct {
	iosArch string
	jniArch string
	clang   string
	// TODO: Remove when https://github.com/android-ndk/ndk/issues/920
	// is solved and released.
	r19bWindowsClangArgs string
}

var allArchs = map[string]arch{
	"arm": arch{
		iosArch:              "armv7",
		jniArch:              "armeabi-v7a",
		clang:                "armv7a-linux-androideabi16-clang",
		r19bWindowsClangArgs: "--target=armv7a-linux-androideabi16 -fno-addrsig",
	},
	"arm64": arch{
		iosArch:              "arm64",
		jniArch:              "arm64-v8a",
		clang:                "aarch64-linux-android21-clang",
		r19bWindowsClangArgs: "--target=aarch64-linux-androideabi21 -fno-addrsig",
	},
	"386": arch{
		iosArch:              "i386",
		jniArch:              "x86",
		clang:                "i686-linux-android16-clang",
		r19bWindowsClangArgs: "--target=i686-linux-androideabi16 -fno-addrsig",
	},
	"amd64": arch{
		iosArch:              "x86_64",
		jniArch:              "x86_64",
		clang:                "x86_64-linux-android21-clang",
		r19bWindowsClangArgs: "--target=x86_64-linux-androideabi21 -fno-addrsig",
	},
}

func archNDK() string {
	if runtime.GOOS == "windows" && runtime.GOARCH == "386" {
		return "windows"
	} else {
		var arch string
		switch runtime.GOARCH {
		case "386":
			arch = "x86"
		case "amd64":
			arch = "x86_64"
		default:
			panic("unsupported GOARCH: " + runtime.GOARCH)
		}
		return runtime.GOOS + "-" + arch
	}
}

func latestPlatform(sdk string) (string, error) {
	allPlats, err := filepath.Glob(filepath.Join(sdk, "platforms", "android-*"))
	if err != nil {
		return "", err
	}
	var bestVer int
	var bestPlat string
	for _, platform := range allPlats {
		_, name := filepath.Split(platform)
		// The glob above guarantees the "android-" prefix.
		verStr := name[len("android-"):]
		ver, err := strconv.Atoi(verStr)
		if err != nil {
			continue
		}
		if ver < bestVer {
			continue
		}
		bestVer = ver
		bestPlat = platform
	}
	if bestPlat == "" {
		return "", fmt.Errorf("no platforms found in %q", sdk)
	}
	return bestPlat, nil
}

A  => cmd/go.mod +5 -0
@@ 1,5 @@
module gioui.org/cmd

go 1.12

require golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4

A  => cmd/go.sum +2 -0
@@ 1,2 @@
golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4 h1:YUO/7uOKsKeq9UokNS62b8FYywz3ker1l1vDZRCRefw=
golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=

A  => ui/app/GioActivity.java +53 -0
@@ 1,53 @@
// SPDX-License-Identifier: Unlicense OR MIT

package org.gioui;

import android.app.Activity;
import android.content.res.Configuration;
import android.os.Build;
import android.os.Bundle;
import android.view.Window;
import android.view.WindowManager;

public class GioActivity extends Activity {
	private GioView view;

	static {
		System.loadLibrary("gio");
	}

	@Override public void onCreate(Bundle state) {
		super.onCreate(state);
		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
			Window w = getWindow();
			w.setFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS, WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
		}
		this.view = new GioView(this);
		setContentView(view);
	}

	@Override public void onDestroy() {
		view.destroy();
		super.onDestroy();
	}

	@Override public void onStart() {
		super.onStart();
		view.start();
	}

	@Override public void onStop() {
		view.stop();
		super.onStop();
	}

	@Override public void onConfigurationChanged(Configuration c) {
		super.onConfigurationChanged(c);
		view.configurationChanged();
	}

	@Override public void onLowMemory() {
		super.onLowMemory();
		view.lowMemory();
	}
}

A  => ui/app/GioView.java +183 -0
@@ 1,183 @@
// SPDX-License-Identifier: Unlicense OR MIT

package org.gioui;

import android.content.Context;
import android.os.Handler;
import android.util.AttributeSet;
import android.text.Editable;
import android.view.Choreographer;
import android.view.KeyCharacterMap;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.Surface;
import android.view.SurfaceView;
import android.view.SurfaceHolder;
import android.view.inputmethod.BaseInputConnection;
import android.view.inputmethod.InputConnection;
import android.view.inputmethod.InputMethodManager;
import android.view.inputmethod.EditorInfo;

public class GioView extends SurfaceView implements Choreographer.FrameCallback {
	private final SurfaceHolder.Callback callbacks;
	private final InputMethodManager imm;
	private final Handler handler;
	private long nhandle;

	public GioView(Context context) {
		this(context, null);
	}

	public GioView(Context context, AttributeSet attrs) {
		super(context, attrs);
		handler = new Handler();
		imm = (InputMethodManager)context.getSystemService(Context.INPUT_METHOD_SERVICE);
		setFocusable(true);
		setFocusableInTouchMode(true);
		callbacks = new SurfaceHolder.Callback() {
			@Override public void surfaceCreated(SurfaceHolder holder) {
				// Ignore; surfaceChanged is guaranteed to be called immediately after this.
			}
			@Override public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
				onSurfaceChanged(nhandle, getHolder().getSurface());
			}
			@Override public void surfaceDestroyed(SurfaceHolder holder) {
				onSurfaceDestroyed(nhandle);
			}
		};
		getHolder().addCallback(callbacks);
		nhandle = onCreateView(this);
	}

	@Override public boolean onKeyDown(int keyCode, KeyEvent event) {
		onKeyEvent(nhandle, keyCode, event.getUnicodeChar(), event.getEventTime());
		return false;
	}

	@Override public boolean onTouchEvent(MotionEvent event) {
		for (int j = 0; j < event.getHistorySize(); j++) {
			long time = event.getHistoricalEventTime(j);
			for (int i = 0; i < event.getPointerCount(); i++) {
				onTouchEvent(
						nhandle,
						event.ACTION_MOVE,
						event.getPointerId(i),
						event.getToolType(i),
						event.getHistoricalX(i, j),
						event.getHistoricalY(i, j),
						time);
			}
		}
		int act = event.getActionMasked();
		int idx = event.getActionIndex();
		for (int i = 0; i < event.getPointerCount(); i++) {
			int pact = event.ACTION_MOVE;
			if (i == idx) {
				pact = act;
			}
			onTouchEvent(
					nhandle,
					act,
					event.getPointerId(i),
					event.getToolType(i),
					event.getX(i),
					event.getY(i),
					event.getEventTime());
		}
		return true;
	}

	@Override public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
		return new InputConnection(this);
	}

	void showTextInput() {
		post(new Runnable() {
			@Override public void run() {
				GioView.this.requestFocus();
				imm.showSoftInput(GioView.this, 0);
			}
		});
	}

	void hideTextInput() {
		post(new Runnable() {
			@Override public void run() {
				imm.hideSoftInputFromWindow(getWindowToken(), 0);
			}
		});
	}

	void postFrameCallbackOnMainThread() {
		handler.post(new Runnable() {
			@Override public void run() {
				postFrameCallback();
			}
		});
	}

	void postFrameCallback() {
		Choreographer.getInstance().removeFrameCallback(this);
		Choreographer.getInstance().postFrameCallback(this);
	}

	@Override public void doFrame(long nanos) {
		onFrameCallback(nhandle, nanos);
	}

	int getDensity() {
		return getResources().getDisplayMetrics().densityDpi;
	}

	float getFontScale() {
		return getResources().getConfiguration().fontScale;
	}

	void start() {
		onStartView(nhandle);
	}

	void stop() {
		onStopView(nhandle);
	}

	void destroy() {
		getHolder().removeCallback(callbacks);
		onDestroyView(nhandle);
		nhandle = 0;
	}

	void configurationChanged() {
		onConfigurationChanged(nhandle);
	}

	void lowMemory() {
		onLowMemory();
	}

	static private native long onCreateView(GioView view);
	static private native void onDestroyView(long handle);
	static private native void onStartView(long handle);
	static private native void onStopView(long handle);
	static private native void onSurfaceDestroyed(long handle);
	static private native void onSurfaceChanged(long handle, Surface surface);
	static private native void onConfigurationChanged(long handle);
	static private native void onLowMemory();
	static private native void onTouchEvent(long handle, int action, int pointerID, int tool, float x, float y, long time);
	static private native void onKeyEvent(long handle, int code, int character, long time);
	static private native void onFrameCallback(long handle, long nanos);

	private static class InputConnection extends BaseInputConnection {
		private final Editable editable;

		InputConnection(View view) {
			super(view, true);
			editable = Editable.Factory.getInstance().newEditable("");
		}

		@Override public Editable getEditable() {
			return editable;
		}
	}
}

A  => ui/app/app.go +94 -0
@@ 1,94 @@
// SPDX-License-Identifier: Unlicense OR MIT

package app

import (
	"image"
	"os"
	"strings"

	"gioui.org/ui"
)

type Draw struct {
	Config *ui.Config
	Size   image.Point
	// Whether this draw is system generated
	// and needs to a complete frame before
	// proceeding.
	sync bool
}

type ChangeStage struct {
	Stage Stage
}

type Stage uint8

type Event interface {
	ImplementsEvent()
}

type Input interface {
	ImplementsInput()
}

const (
	StageDead Stage = iota
	StageInvisible
	StageVisible
)

const (
	inchPrDp = 1.0 / 160
	mmPrDp   = 25.4 / 160
	// monitorScale is the extra scale applied to
	// monitor outputs to compensate for the extra
	// viewing distance compared to phone and tables.
	monitorScale = 1.50
	// minDensity is the minimum pixels per dp to
	// ensure font and ui legibility on low-dpi
	// screens.
	minDensity = 1.25
)

// extraArgs contains extra arguments to append to
// os.Args. The arguments are separated with |.
// Useful for running programs on mobiles where the
// command line is not available.
// Set it with the go tool linker flag -X.
var extraArgs string

var windows = make(chan *Window)

func CreateWindow(opts WindowOptions) error {
	if opts.Width.V <= 0 || opts.Height.V <= 0 {
		panic("window width and height must be larger than 0")
	}
	return createWindow(opts)
}

func Windows() <-chan *Window {
	return windows
}

func (l Stage) String() string {
	switch l {
	case StageDead:
		return "StageDead"
	case StageInvisible:
		return "StageInvisible"
	case StageVisible:
		return "StageVisible"
	default:
		panic("unexpected Stage value")
	}
}

func (_ Draw) ImplementsEvent()        {}
func (_ ChangeStage) ImplementsEvent() {}

func init() {
	args := strings.Split(extraArgs, "|")
	os.Args = append(os.Args, args...)
}

A  => ui/app/egl.go +254 -0
@@ 1,254 @@
// SPDX-License-Identifier: Unlicense OR MIT

// +build linux windows

package app

import (
	"errors"
	"fmt"
	"runtime"
	"strings"

	"gioui.org/ui/app/internal/gl"
)

type context struct {
	c             *gl.Functions
	driver        *window
	eglCtx        *eglContext
	nwindow       _EGLNativeWindowType
	eglWin        *eglWindow
	eglSurf       _EGLSurface
	width, height int
	// For sRGB emulation.
	srgbFBO *gl.SRGBFBO
}

type eglContext struct {
	disp     _EGLDisplay
	config   _EGLConfig
	ctx      _EGLContext
	visualID int
	srgb     bool
}

const (
	_EGL_ALPHA_SIZE             = 0x3021
	_EGL_BLUE_SIZE              = 0x3022
	_EGL_CONFIG_CAVEAT          = 0x3027
	_EGL_CONTEXT_CLIENT_VERSION = 0x3098
	_EGL_DEPTH_SIZE             = 0x3025
	_EGL_GL_COLORSPACE_KHR      = 0x309d
	_EGL_GL_COLORSPACE_SRGB_KHR = 0x3089
	_EGL_GREEN_SIZE             = 0x3023
	_EGL_EXTENSIONS             = 0x3055
	_EGL_NATIVE_VISUAL_ID       = 0x302e
	_EGL_NONE                   = 0x3038
	_EGL_OPENGL_ES2_BIT         = 0x4
	_EGL_RED_SIZE               = 0x3024
	_EGL_RENDERABLE_TYPE        = 0x3040
	_EGL_SURFACE_TYPE           = 0x3033
	_EGL_WINDOW_BIT             = 0x4
)

func (c *context) Release() {
	if c.srgbFBO != nil {
		c.srgbFBO.Release()
	}
	if c.eglSurf != nilEGLSurface {
		eglMakeCurrent(c.eglCtx.disp, nilEGLSurface, nilEGLSurface, nilEGLContext)
		eglDestroySurface(c.eglCtx.disp, c.eglSurf)
		c.eglSurf = nilEGLSurface
	}
	if c.eglWin != nil {
		c.eglWin.destroy()
		c.eglWin = nil
	}
	if c.eglCtx != nil {
		eglDestroyContext(c.eglCtx.disp, c.eglCtx.ctx)
		eglTerminate(c.eglCtx.disp)
		eglReleaseThread()
		c.eglCtx = nil
	}
	c.driver = nil
}

func (c *context) Present() error {
	if c.eglWin == nil {
		panic("context is not active")
	}
	if c.srgbFBO != nil {
		c.srgbFBO.Blit()
	}
	if !eglSwapBuffers(c.eglCtx.disp, c.eglSurf) {
		return fmt.Errorf("eglSwapBuffers failed (%x%x)", eglGetError())
	}
	if c.srgbFBO != nil {
		c.srgbFBO.AfterPresent()
	}
	return nil
}

func newContext(w *window) (*context, error) {
	eglCtx, err := createContext(_EGLNativeDisplayType(w.display()))
	if err != nil {
		return nil, err
	}
	c := &context{
		driver: w,
		eglCtx: eglCtx,
	}
	return c, nil
}

func (c *context) Functions() *gl.Functions {
	return c.c
}

func (c *context) MakeCurrent() error {
	w, width, height := c.driver.nativeWindow(int(c.eglCtx.visualID))
	win := _EGLNativeWindowType(w)
	if c.nwindow == win && width == c.width && height == c.height {
		return nil
	}
	if win == nilEGLNativeWindowType {
		if c.srgbFBO != nil {
			c.srgbFBO.Release()
			c.srgbFBO = nil
		}
	}
	if c.eglSurf != nilEGLSurface {
		// Make sure any in-flight GL commands are complete.
		c.c.Finish()
		eglMakeCurrent(c.eglCtx.disp, nilEGLSurface, nilEGLSurface, nilEGLContext)
		eglDestroySurface(c.eglCtx.disp, c.eglSurf)
		c.eglSurf = nilEGLSurface
	}
	c.width, c.height = width, height
	c.nwindow = win
	if c.nwindow == nilEGLNativeWindowType {
		if c.eglWin != nil {
			c.eglWin.destroy()
			c.eglWin = nil
		}
		return nil
	}
	if c.eglWin == nil {
		var err error
		c.eglWin, err = newEGLWindow(win, width, height)
		if err != nil {
			return err
		}
	} else {
		c.eglWin.resize(width, height)
	}
	eglSurf, err := createSurfaceAndMakeCurrent(c.eglCtx, c.eglWin.window())
	c.eglSurf = eglSurf
	if err != nil {
		c.eglWin.destroy()
		c.eglWin = nil
		c.nwindow = nilEGLNativeWindowType
		return err
	}
	if c.eglCtx.srgb {
		return nil
	}
	if c.srgbFBO == nil {
		var err error
		c.srgbFBO, err = gl.NewSRGBFBO(c.c)
		if err != nil {
			c.Release()
			return err
		}
	}
	if err := c.srgbFBO.Refresh(c.width, c.height); err != nil {
		c.Release()
		return err
	}
	return nil
}

func createContext(disp _EGLNativeDisplayType) (*eglContext, error) {
	eglDisp := eglGetDisplay(disp)
	if eglDisp == 0 {
		return nil, fmt.Errorf("eglGetDisplay(_EGL_DEFAULT_DISPLAY) failed: 0x%x", eglGetError())
	}
	_, minor, ret := eglInitialize(eglDisp)
	if !ret {
		return nil, fmt.Errorf("eglInitialize failed: 0x%x", eglGetError())
	}
	// sRGB framebuffer support on EGL 1.5 or if EGL_KHR_gl_colorspace is supported.
	exts := eglQueryString(eglDisp, _EGL_EXTENSIONS)
	srgb := minor >= 5 || strings.Contains(exts, "EGL_KHR_gl_colorspace")
	attribs := []_EGLint{
		_EGL_RENDERABLE_TYPE, _EGL_OPENGL_ES2_BIT,
		_EGL_SURFACE_TYPE, _EGL_WINDOW_BIT,
		_EGL_BLUE_SIZE, 8,
		_EGL_GREEN_SIZE, 8,
		_EGL_RED_SIZE, 8,
		_EGL_CONFIG_CAVEAT, _EGL_NONE,
	}
	if srgb {
		if runtime.GOOS == "linux" {
			// Some Mesa drivers crash if an sRGB framebuffer is requested without alpha.
			// https://bugs.freedesktop.org/show_bug.cgi?id=107782.
			attribs = append(attribs, _EGL_ALPHA_SIZE, 1)
		}
		// Only request a depth buffer if we're going to render directly to the framebuffer.
		attribs = append(attribs, _EGL_DEPTH_SIZE, 16)
	}
	attribs = append(attribs, _EGL_NONE)
	eglCfg, ret := eglChooseConfig(eglDisp, attribs)
	if !ret {
		return nil, fmt.Errorf("eglChooseConfig failed: 0x%x", eglGetError())
	}
	if eglCfg == nilEGLConfig {
		return nil, errors.New("eglChooseConfig returned 0 configs")
	}
	var eglCtx _EGLContext
	ctxAttribs := []_EGLint{
		_EGL_CONTEXT_CLIENT_VERSION, 3,
		_EGL_NONE,
	}
	eglCtx = eglCreateContext(eglDisp, eglCfg, nilEGLContext, ctxAttribs)
	if eglCtx == nilEGLContext {
		return nil, fmt.Errorf("eglCreateContext failed: 0x%x", eglGetError())
	}
	visID, ret := eglGetConfigAttrib(eglDisp, eglCfg, _EGL_NATIVE_VISUAL_ID)
	if !ret {
		return nil, errors.New("newContext: eglGetConfigAttrib for _EGL_NATIVE_VISUAL_ID failed")
	}
	return &eglContext{
		disp:     eglDisp,
		config:   _EGLConfig(eglCfg),
		ctx:      _EGLContext(eglCtx),
		visualID: int(visID),
		srgb:     srgb,
	}, nil
}

func createSurfaceAndMakeCurrent(eglCtx *eglContext, win _EGLNativeWindowType) (_EGLSurface, error) {
	var surfAttribs []_EGLint
	if eglCtx.srgb {
		surfAttribs = append(surfAttribs, _EGL_GL_COLORSPACE_KHR, _EGL_GL_COLORSPACE_SRGB_KHR)
	}
	surfAttribs = append(surfAttribs, _EGL_NONE)
	eglSurf := eglCreateWindowSurface(eglCtx.disp, eglCtx.config, win, surfAttribs)
	if eglSurf == nilEGLSurface {
		return nilEGLSurface, fmt.Errorf("newContext: eglCreateWindowSurface failed 0x%x", eglGetError())
	}
	if !eglMakeCurrent(eglCtx.disp, eglSurf, eglSurf, eglCtx.ctx) {
		eglDestroySurface(eglCtx.disp, eglSurf)
		return nilEGLSurface, fmt.Errorf("eglMakeCurrent error 0x%x", eglGetError())
	}
	// eglSwapInterval 1 leads to erratic frame rates and unnecessary blocking.
	// We rely on platform specific frame rate limiting instead, except on Windows
	// where eglSwapInterval is all there is.
	if runtime.GOOS != "windows" {
		eglSwapInterval(eglCtx.disp, 0)
	} else {
		eglSwapInterval(eglCtx.disp, 1)
	}
	return eglSurf, nil
}

A  => ui/app/egl_android.go +22 -0
@@ 1,22 @@
// SPDX-License-Identifier: Unlicense OR MIT

package app

/*
#include <EGL/egl.h>
*/
import "C"

type (
	_EGLNativeDisplayType = C.EGLNativeDisplayType
	_EGLNativeWindowType  = C.EGLNativeWindowType
)

func eglGetDisplay(disp _EGLNativeDisplayType) _EGLDisplay {
	return C.eglGetDisplay(disp)
}

func eglCreateWindowSurface(disp _EGLDisplay, conf _EGLConfig, win _EGLNativeWindowType, attribs []_EGLint) _EGLSurface {
	eglSurf := C.eglCreateWindowSurface(disp, conf, win, &attribs[0])
	return eglSurf
}

A  => ui/app/egl_linux.go +90 -0
@@ 1,90 @@
// SPDX-License-Identifier: Unlicense OR MIT

package app

/*
#cgo LDFLAGS: -lEGL

#include <EGL/egl.h>
#include <EGL/eglext.h>
#include <GLES2/gl2.h>
#include <GLES3/gl3.h>
*/
import "C"

type (
	_EGLint     = C.EGLint
	_EGLDisplay = C.EGLDisplay
	_EGLConfig  = C.EGLConfig
	_EGLContext = C.EGLContext
	_EGLSurface = C.EGLSurface
)

var (
	nilEGLSurface          _EGLSurface
	nilEGLContext          _EGLContext
	nilEGLConfig           _EGLConfig
	nilEGLNativeWindowType _EGLNativeWindowType
)

func eglChooseConfig(disp _EGLDisplay, attribs []_EGLint) (_EGLConfig, bool) {
	var cfg C.EGLConfig
	var ncfg C.EGLint
	if C.eglChooseConfig(disp, &attribs[0], &cfg, 1, &ncfg) != C.EGL_TRUE {
		return nil, false
	}
	return _EGLConfig(cfg), true
}

func eglCreateContext(disp _EGLDisplay, cfg _EGLConfig, shareCtx _EGLContext, attribs []_EGLint) _EGLContext {
	ctx := C.eglCreateContext(disp, cfg, shareCtx, &attribs[0])
	return _EGLContext(ctx)
}

func eglDestroySurface(disp _EGLDisplay, surf _EGLSurface) bool {
	return C.eglDestroySurface(disp, surf) == C.EGL_TRUE
}

func eglDestroyContext(disp _EGLDisplay, ctx _EGLContext) bool {
	return C.eglDestroyContext(disp, ctx) == C.EGL_TRUE
}

func eglGetConfigAttrib(disp _EGLDisplay, cfg _EGLConfig, attr _EGLint) (_EGLint, bool) {
	var val _EGLint
	ret := C.eglGetConfigAttrib(disp, cfg, attr, &val)
	return val, ret == C.EGL_TRUE
}

func eglGetError() _EGLint {
	return C.eglGetError()
}

func eglInitialize(disp _EGLDisplay) (_EGLint, _EGLint, bool) {
	var maj, min _EGLint
	ret := C.eglInitialize(disp, &maj, &min)
	return maj, min, ret == C.EGL_TRUE
}

func eglMakeCurrent(disp _EGLDisplay, draw, read _EGLSurface, ctx _EGLContext) bool {
	return C.eglMakeCurrent(disp, draw, read, ctx) == C.EGL_TRUE
}

func eglReleaseThread() bool {
	return C.eglReleaseThread() == C.EGL_TRUE
}

func eglSwapBuffers(disp _EGLDisplay, surf _EGLSurface) bool {
	return C.eglSwapBuffers(disp, surf) == C.EGL_TRUE
}

func eglSwapInterval(disp _EGLDisplay, interval _EGLint) bool {
	return C.eglSwapInterval(disp, interval) == C.EGL_TRUE
}

func eglTerminate(disp _EGLDisplay) bool {
	return C.eglTerminate(disp) == C.EGL_TRUE
}

func eglQueryString(disp _EGLDisplay, name _EGLint) string {
	return C.GoString(C.eglQueryString(disp, name))
}

A  => ui/app/egl_wayland.go +58 -0
@@ 1,58 @@
// SPDX-License-Identifier: Unlicense OR MIT

// +build linux,!android

package app

import (
	"errors"
	"unsafe"
)

/*
#cgo LDFLAGS: -lwayland-egl

#include <wayland-client.h>
#include <wayland-egl.h>
#include <EGL/egl.h>
*/
import "C"

type (
	_EGLNativeDisplayType = C.EGLNativeDisplayType
	_EGLNativeWindowType  = C.EGLNativeWindowType
)

type eglWindow struct {
	w *C.struct_wl_egl_window
}

func newEGLWindow(w _EGLNativeWindowType, width, height int) (*eglWindow, error) {
	surf := (*C.struct_wl_surface)(unsafe.Pointer(w))
	win := C.wl_egl_window_create(surf, C.int(width), C.int(height))
	if win == nil {
		return nil, errors.New("wl_egl_create_window failed")
	}
	return &eglWindow{win}, nil
}

func (w *eglWindow) window() _EGLNativeWindowType {
	return w.w
}

func (w *eglWindow) resize(width, height int) {
	C.wl_egl_window_resize(w.w, C.int(width), C.int(height), 0, 0)
}

func (w *eglWindow) destroy() {
	C.wl_egl_window_destroy(w.w)
}

func eglGetDisplay(disp _EGLNativeDisplayType) _EGLDisplay {
	return C.eglGetDisplay(disp)
}

func eglCreateWindowSurface(disp _EGLDisplay, conf _EGLConfig, win _EGLNativeWindowType, attribs []_EGLint) _EGLSurface {
	eglSurf := C.eglCreateWindowSurface(disp, conf, win, &attribs[0])
	return eglSurf
}

A  => ui/app/egl_win.go +20 -0
@@ 1,20 @@
// SPDX-License-Identifier: Unlicense OR MIT

// +build android windows

package app

type eglWindow struct {
	w _EGLNativeWindowType
}

func newEGLWindow(w _EGLNativeWindowType, width, height int) (*eglWindow, error) {
	return &eglWindow{w}, nil
}

func (w *eglWindow) window() _EGLNativeWindowType {
	return w.w
}

func (w *eglWindow) resize(width, height int) {}
func (w *eglWindow) destroy()                 {}

A  => ui/app/egl_windows.go +175 -0
@@ 1,175 @@
// SPDX-License-Identifier: Unlicense OR MIT

package app

import (
	"os"
	"reflect"
	"unsafe"

	syscall "golang.org/x/sys/windows"

	"gioui.org/ui/app/internal/gl"
)

type (
	_EGLint               int32
	_EGLDisplay           uintptr
	_EGLConfig            uintptr
	_EGLContext           uintptr
	_EGLSurface           uintptr
	_EGLNativeDisplayType uintptr
	_EGLNativeWindowType  uintptr
)

var (
	libEGL                  = syscall.NewLazyDLL("libEGL.dll")
	_eglChooseConfig        = libEGL.NewProc("eglChooseConfig")
	_eglCreateContext       = libEGL.NewProc("eglCreateContext")
	_eglCreateWindowSurface = libEGL.NewProc("eglCreateWindowSurface")
	_eglDestroyContext      = libEGL.NewProc("eglDestroyContext")
	_eglDestroySurface      = libEGL.NewProc("eglDestroySurface")
	_eglGetConfigAttrib     = libEGL.NewProc("eglGetConfigAttrib")
	_eglGetDisplay          = libEGL.NewProc("eglGetDisplay")
	_eglGetError            = libEGL.NewProc("eglGetError")
	_eglInitialize          = libEGL.NewProc("eglInitialize")
	_eglMakeCurrent         = libEGL.NewProc("eglMakeCurrent")
	_eglReleaseThread       = libEGL.NewProc("eglReleaseThread")
	_eglSwapInterval        = libEGL.NewProc("eglSwapInterval")
	_eglSwapBuffers         = libEGL.NewProc("eglSwapBuffers")
	_eglTerminate           = libEGL.NewProc("eglTerminate")
	_eglQueryString         = libEGL.NewProc("eglQueryString")
)

const (
	nilEGLSurface          _EGLSurface          = 0
	nilEGLContext          _EGLContext          = 0
	nilEGLConfig           _EGLConfig           = 0
	nilEGLNativeWindowType _EGLNativeWindowType = 0
)

func init() {
	mustLoadDLL(libEGL, "libEGL.dll")
	mustLoadDLL(gl.LibGLESv2, "libGLESv2.dll")
	// d3dcompiler_47.dll is needed internally for shader compilation to function.
	mustLoadDLL(syscall.NewLazyDLL("d3dcompiler_47.dll"), "d3dcompiler_47.dll")
}

func mustLoadDLL(dll *syscall.LazyDLL, name string) {
	loadErr := dll.Load()
	if loadErr == nil {
		return
	}
	user32 := syscall.NewLazySystemDLL("user32.dll")
	messageBox := user32.NewProc("MessageBoxW")
	if err := messageBox.Find(); err != nil {
		panic(loadErr)
	}
	pmsg := syscall.StringToUTF16Ptr("Failed to load " + name)
	ptitle := syscall.StringToUTF16Ptr("Error")
	const MB_ICONERROR = 0x00000010
	const MB_SYSTEMMODAL = 0x00001000
	messageBox.Call(0 /* HWND */, uintptr(unsafe.Pointer(pmsg)), uintptr(unsafe.Pointer(ptitle)), MB_ICONERROR|MB_SYSTEMMODAL)
	os.Exit(1)
}

func eglChooseConfig(disp _EGLDisplay, attribs []_EGLint) (_EGLConfig, bool) {
	var cfg _EGLConfig
	var ncfg _EGLint
	r, _, _ := _eglChooseConfig.Call(uintptr(disp), uintptr(unsafe.Pointer(&attribs[0])), uintptr(unsafe.Pointer(&cfg)), 1, uintptr(unsafe.Pointer(&ncfg)))
	return cfg, r != 0 && ncfg > 0
}

func eglCreateContext(disp _EGLDisplay, cfg _EGLConfig, shareCtx _EGLContext, attribs []_EGLint) _EGLContext {
	c, _, _ := _eglCreateContext.Call(uintptr(disp), uintptr(cfg), uintptr(shareCtx), uintptr(unsafe.Pointer(&attribs[0])))
	return _EGLContext(c)
}

func eglCreateWindowSurface(disp _EGLDisplay, cfg _EGLConfig, win _EGLNativeWindowType, attribs []_EGLint) _EGLSurface {
	s, _, _ := _eglCreateWindowSurface.Call(uintptr(disp), uintptr(cfg), uintptr(win), uintptr(unsafe.Pointer(&attribs[0])))
	return _EGLSurface(s)
}

func eglDestroySurface(disp _EGLDisplay, surf _EGLSurface) bool {
	r, _, _ := _eglDestroySurface.Call(uintptr(disp), uintptr(surf))
	return r != 0
}

func eglDestroyContext(disp _EGLDisplay, ctx _EGLContext) bool {
	r, _, _ := _eglDestroyContext.Call(uintptr(disp), uintptr(ctx))
	return r != 0
}

func eglGetConfigAttrib(disp _EGLDisplay, cfg _EGLConfig, attr _EGLint) (_EGLint, bool) {
	var val uintptr
	r, _, _ := _eglGetConfigAttrib.Call(uintptr(disp), uintptr(cfg), uintptr(attr), uintptr(unsafe.Pointer(&val)))
	return _EGLint(val), r != 0
}

func eglGetDisplay(disp _EGLNativeDisplayType) _EGLDisplay {
	d, _, _ := _eglGetDisplay.Call(uintptr(disp))
	return _EGLDisplay(d)
}

func eglGetError() _EGLint {
	e, _, _ := _eglGetError.Call()
	return _EGLint(e)
}

func eglInitialize(disp _EGLDisplay) (_EGLint, _EGLint, bool) {
	var maj, min uintptr
	r, _, _ := _eglInitialize.Call(uintptr(disp), uintptr(unsafe.Pointer(&maj)), uintptr(unsafe.Pointer(&min)))
	return _EGLint(maj), _EGLint(min), r != 0
}

func eglMakeCurrent(disp _EGLDisplay, draw, read _EGLSurface, ctx _EGLContext) bool {
	r, _, _ := _eglMakeCurrent.Call(uintptr(disp), uintptr(draw), uintptr(read), uintptr(ctx))
	return r != 0
}

func eglReleaseThread() bool {
	r, _, _ := _eglReleaseThread.Call()
	return r != 0
}

func eglSwapInterval(disp _EGLDisplay, interval _EGLint) bool {
	r, _, _ := _eglSwapInterval.Call(uintptr(disp), uintptr(interval))
	return r != 0
}

func eglSwapBuffers(disp _EGLDisplay, surf _EGLSurface) bool {
	r, _, _ := _eglSwapBuffers.Call(uintptr(disp), uintptr(surf))
	return r != 0
}

func eglTerminate(disp _EGLDisplay) bool {
	r, _, _ := _eglTerminate.Call(uintptr(disp))
	return r != 0
}

func eglQueryString(disp _EGLDisplay, name _EGLint) string {
	r, _, _ := _eglQueryString.Call(uintptr(disp), uintptr(name))
	return goString(r)
}

func goString(s uintptr) string {
	if s == 0 {
		return ""
	}
	sh := reflect.SliceHeader{
		Data: s,
		Len:  1 << 30,
		Cap:  1 << 30,
	}
	sl := *(*[]byte)(unsafe.Pointer(&sh))
	var v string
	for i, c := range sl {
		if c == 0 {
			if i > 0 {
				v = string(sl[:i-1])
			}
			break
		}
	}
	return v
}

A  => ui/app/framework_ios.h +8 -0
@@ 1,8 @@
// SPDX-License-Identifier: Unlicense OR MIT

@import UIKit;

@interface GioAppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@end


A  => ui/app/gl_ios.go +119 -0
@@ 1,119 @@
// SPDX-License-Identifier: Unlicense OR MIT

//+build darwin,ios

package app

/*
#cgo CFLAGS: -fmodules -fobjc-arc -x objective-c

#include <CoreFoundation/CoreFoundation.h>
#include <OpenGLES/ES2/gl.h>
#include <OpenGLES/ES2/glext.h>
#include "gl_ios.h"
*/
import "C"

import (
	"errors"
	"fmt"

	"gioui.org/ui/app/internal/gl"
)

type context struct {
	owner                    *window
	c                        *gl.Functions
	ctx                      C.CFTypeRef
	layer                    C.CFTypeRef
	init                     bool
	frameBuffer              gl.Framebuffer
	colorBuffer, depthBuffer gl.Renderbuffer
}

func init() {
	layerFactory = func() uintptr {
		return uintptr(C.gio_createGLLayer())
	}
}

func newContext(w *window) (*context, error) {
	ctx := C.gio_createContext()
	if ctx == 0 {
		return nil, fmt.Errorf("failed to create EAGLContext")
	}
	c := &context{
		ctx:   ctx,
		owner: w,
		layer: C.CFTypeRef(w.contextLayer()),
	}
	return c, nil
}

func (c *context) Functions() *gl.Functions {
	return c.c
}

func (c *context) Release() {
	if c.ctx == 0 {
		return
	}
	C.gio_renderbufferStorage(c.ctx, 0, C.GLenum(gl.RENDERBUFFER))
	c.c.DeleteFramebuffer(c.frameBuffer)
	c.c.DeleteRenderbuffer(c.colorBuffer)
	c.c.DeleteRenderbuffer(c.depthBuffer)
	C.gio_makeCurrent(0)
	C.CFRelease(c.ctx)
	c.ctx = 0
}

func (c *context) Present() error {
	if c.layer == 0 {
		panic("context is not active")
	}
	// Discard depth buffer as recommended in
	// https://developer.apple.com/library/archive/documentation/3DDrawing/Conceptual/OpenGLES_ProgrammingGuide/WorkingwithEAGLContexts/WorkingwithEAGLContexts.html
	c.c.BindFramebuffer(gl.FRAMEBUFFER, c.frameBuffer)
	c.c.InvalidateFramebuffer(gl.FRAMEBUFFER, gl.DEPTH_ATTACHMENT)
	c.c.BindRenderbuffer(gl.RENDERBUFFER, c.colorBuffer)
	if C.gio_presentRenderbuffer(c.ctx, C.GLenum(gl.RENDERBUFFER)) == 0 {
		return errors.New("presentRenderBuffer failed")
	}
	return nil
}

func (c *context) MakeCurrent() error {
	if C.gio_makeCurrent(c.ctx) == 0 {
		C.CFRelease(c.ctx)
		c.ctx = 0
		return errors.New("[EAGLContext setCurrentContext] failed")
	}
	if !c.init {
		c.init = true
		c.frameBuffer = c.c.CreateFramebuffer()
		c.colorBuffer = c.c.CreateRenderbuffer()
		c.depthBuffer = c.c.CreateRenderbuffer()
	}
	if !c.owner.isVisible() {
		// Make sure any in-flight GL commands are complete.
		c.c.Finish()
		return nil
	}
	currentRB := gl.Renderbuffer(c.c.GetInteger(gl.RENDERBUFFER_BINDING))
	c.c.BindRenderbuffer(gl.RENDERBUFFER, c.colorBuffer)
	if C.gio_renderbufferStorage(c.ctx, c.layer, C.GLenum(gl.RENDERBUFFER)) == 0 {
		return errors.New("renderbufferStorage failed")
	}
	w := c.c.GetRenderbufferParameteri(gl.RENDERBUFFER, gl.RENDERBUFFER_WIDTH)
	h := c.c.GetRenderbufferParameteri(gl.RENDERBUFFER, gl.RENDERBUFFER_HEIGHT)
	c.c.BindRenderbuffer(gl.RENDERBUFFER, c.depthBuffer)
	c.c.RenderbufferStorage(gl.RENDERBUFFER, gl.DEPTH_COMPONENT16, w, h)
	c.c.BindRenderbuffer(gl.RENDERBUFFER, currentRB)
	c.c.BindFramebuffer(gl.FRAMEBUFFER, c.frameBuffer)
	c.c.FramebufferRenderbuffer(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.RENDERBUFFER, c.colorBuffer)
	c.c.FramebufferRenderbuffer(gl.FRAMEBUFFER, gl.DEPTH_ATTACHMENT, gl.RENDERBUFFER, c.depthBuffer)
	if st := c.c.CheckFramebufferStatus(gl.FRAMEBUFFER); st != gl.FRAMEBUFFER_COMPLETE {
		return fmt.Errorf("framebuffer incomplete, status: %#x\n", st)
	}
	return nil
}

A  => ui/app/gl_ios.h +7 -0
@@ 1,7 @@
// SPDX-License-Identifier: Unlicense OR MIT

__attribute__ ((visibility ("hidden"))) int gio_renderbufferStorage(CFTypeRef ctx, CFTypeRef layer, GLenum buffer);
__attribute__ ((visibility ("hidden"))) int gio_presentRenderbuffer(CFTypeRef ctx, GLenum buffer);
__attribute__ ((visibility ("hidden"))) int gio_makeCurrent(CFTypeRef ctx);
__attribute__ ((visibility ("hidden"))) CFTypeRef gio_createContext(void);
__attribute__ ((visibility ("hidden"))) CFTypeRef gio_createGLLayer();

A  => ui/app/gl_ios.m +44 -0
@@ 1,44 @@
// SPDX-License-Identifier: Unlicense OR MIT

//+build darwin,ios

@import UIKit;
@import OpenGLES;

#include "gl_ios.h"

int gio_renderbufferStorage(CFTypeRef ctxRef, CFTypeRef layerRef, GLenum buffer) {
	EAGLContext *ctx = (__bridge EAGLContext *)ctxRef;
	CAEAGLLayer *layer = (__bridge CAEAGLLayer *)layerRef;
	return (int)[ctx renderbufferStorage:buffer fromDrawable:layer];
}

int gio_presentRenderbuffer(CFTypeRef ctxRef, GLenum buffer) {
	EAGLContext *ctx = (__bridge EAGLContext *)ctxRef;
	return (int)[ctx presentRenderbuffer:buffer];
}

int gio_makeCurrent(CFTypeRef ctxRef) {
	EAGLContext *ctx = (__bridge EAGLContext *)ctxRef;
	return (int)[EAGLContext setCurrentContext:ctx];
}

CFTypeRef gio_createContext(void) {
	EAGLContext *ctx = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES3];
	if (ctx == nil) {
		return nil;
	}
	return CFBridgingRetain(ctx);
}

CFTypeRef gio_createGLLayer() {
	CAEAGLLayer *layer = [[CAEAGLLayer layer] init];
	if (layer == nil) {
		return nil;
	}
	layer.drawableProperties = @{kEAGLDrawablePropertyColorFormat: kEAGLColorFormatSRGBA8};
	//layer.presentsWithTransaction = YES;
	layer.opaque = YES;
	layer.anchorPoint = CGPointMake(0, 0);
	return CFBridgingRetain(layer);
}

A  => ui/app/gl_macos.go +59 -0
@@ 1,59 @@
// SPDX-License-Identifier: Unlicense OR MIT

// +build darwin,!ios

package app

import (
	"gioui.org/ui/app/internal/gl"
)

/*
#cgo CFLAGS: -DGL_SILENCE_DEPRECATION -fmodules -fobjc-arc -x objective-c

#include <CoreFoundation/CoreFoundation.h>
#include <CoreGraphics/CoreGraphics.h>
#include <AppKit/AppKit.h>
#include <OpenGL/gl3.h>
#include "gl_macos.h"
*/
import "C"

type context struct {
	c   *gl.Functions
	ctx C.CFTypeRef
}

func init() {
	viewFactory = func() uintptr {
		return uintptr(C.gio_createGLView())
	}
}

func newContext(w *window) (*context, error) {
	ctx := C.gio_contextForView(w.contextView())
	c := &context{
		ctx: ctx,
	}
	return c, nil
}

func (c *context) Functions() *gl.Functions {
	return c.c
}

func (c *context) Release() {
	C.gio_clearCurrentContext()
	C.CFRelease(c.ctx)
	c.ctx = 0
}

func (c *context) Present() error {
	C.glFlush()
	return nil
}

func (c *context) MakeCurrent() error {
	C.gio_makeCurrentContext(c.ctx)
	return nil
}

A  => ui/app/gl_macos.h +7 -0
@@ 1,7 @@
// SPDX-License-Identifier: Unlicense OR MIT

__attribute__ ((visibility ("hidden"))) CFTypeRef gio_createGLView();
__attribute__ ((visibility ("hidden"))) CFTypeRef gio_contextForView(CFTypeRef viewRef);
__attribute__ ((visibility ("hidden"))) void gio_makeCurrentContext(CFTypeRef ctx);
__attribute__ ((visibility ("hidden"))) void gio_flushContextBuffer(CFTypeRef ctx);
__attribute__ ((visibility ("hidden"))) void gio_clearCurrentContext();

A  => ui/app/gl_macos.m +148 -0
@@ 1,148 @@
// SPDX-License-Identifier: Unlicense OR MIT

//+build darwin,!ios

@import AppKit;

#include <CoreFoundation/CoreFoundation.h>
#include <OpenGL/OpenGL.h>
#include <OpenGL/gl3.h>
#include "os_macos.h"
#include "gl_macos.h"
#include "_cgo_export.h"

static void handleMouse(NSView *view, NSEvent *event, int typ, CGFloat dx, CGFloat dy) {
	NSPoint p = [view convertPoint:[event locationInWindow] fromView:nil];
	CGFloat scale = view.layer.contentsScale;
	if (!event.hasPreciseScrollingDeltas) {
		// dx and dy are in rows and columns.
		dx *= 10;
		dy *= 10;
	}
	gio_onMouse((__bridge CFTypeRef)view, typ, p.x*scale, p.y*scale, dx*scale, dy*scale, [event timestamp]);
}

static CVReturn displayLinkCallback(CVDisplayLinkRef displayLink, const CVTimeStamp *inNow, const CVTimeStamp *inOutputTime, CVOptionFlags flagsIn, CVOptionFlags *flagsOut, void *displayLinkContext) {
	CFTypeRef view = (CFTypeRef *)displayLinkContext;
	gio_onFrameCallback(view);
	return COREVIDEO_TRUE;
}

@interface GioView : NSOpenGLView 
@end

@implementation GioView {
CVDisplayLinkRef displayLink;
}
- (instancetype)initWithFrame:(NSRect)frameRect
				  pixelFormat:(NSOpenGLPixelFormat *)format {
	self = [super initWithFrame:frameRect pixelFormat:format];
	if (self) {
		CVDisplayLinkCreateWithActiveCGDisplays(&displayLink);
		CVDisplayLinkSetOutputCallback(displayLink, displayLinkCallback, (__bridge void*)self);
	}
	return self;
}
- (void)dealloc {
	CVDisplayLinkRelease(displayLink);
}
- (void)setAnimating:(BOOL)anim {
	if (anim) {
		CVDisplayLinkStart(displayLink);
	} else {
		CVDisplayLinkStop(displayLink);
	}
}
- (void)updateDisplay:(CGDirectDisplayID)dispID {
	CVDisplayLinkSetCurrentCGDisplay(displayLink, dispID);
}
- (void)prepareOpenGL {
	// Bind a default VBA to emulate OpenGL ES 2.
	GLuint defVBA;
	glGenVertexArrays(1, &defVBA);
	glBindVertexArray(defVBA);
	glEnable(GL_FRAMEBUFFER_SRGB);
}
- (BOOL)isFlipped {
	return YES;
}
- (void)drawRect:(NSRect)r {
	gio_onDraw((__bridge CFTypeRef)self);
}
- (void)mouseDown:(NSEvent *)event {
	handleMouse(self, event, GIO_MOUSE_DOWN, 0, 0);
}
- (void)mouseUp:(NSEvent *)event {
	handleMouse(self, event, GIO_MOUSE_UP, 0, 0);
}
- (void)mouseMoved:(NSEvent *)event {
	handleMouse(self, event, GIO_MOUSE_MOVE, 0, 0);
}
- (void)mouseDragged:(NSEvent *)event {
	handleMouse(self, event, GIO_MOUSE_MOVE, 0, 0);
}
- (void)scrollWheel:(NSEvent *)event {
	CGFloat dx = -event.scrollingDeltaX;
	CGFloat dy = -event.scrollingDeltaY;
	handleMouse(self, event, GIO_MOUSE_MOVE, dx, dy);
}
- (void)keyDown:(NSEvent *)event {
	NSString *keys = [event charactersIgnoringModifiers];
	gio_onKeys((__bridge CFTypeRef)self, (char *)[keys UTF8String], [event timestamp], [event modifierFlags]);
	[self interpretKeyEvents:[NSArray arrayWithObject:event]];
}
- (void)insertText:(id)string {
	const char *utf8 = [string UTF8String];
	gio_onText((__bridge CFTypeRef)self, (char *)utf8);
}
@end

CFTypeRef gio_createGLView() {
	@autoreleasepool {
		NSOpenGLPixelFormatAttribute attr[] = {
			NSOpenGLPFAOpenGLProfile, NSOpenGLProfileVersion3_2Core,
			NSOpenGLPFAColorSize,     24,
			NSOpenGLPFADepthSize,     16,
			NSOpenGLPFAAccelerated,
			// Opt-in to automatic GPU switching. CGL-only property.
			kCGLPFASupportsAutomaticGraphicsSwitching,
			NSOpenGLPFAAllowOfflineRenderers,
			0
		};
		id pixFormat = [[NSOpenGLPixelFormat alloc] initWithAttributes:attr];

		NSRect frame = NSMakeRect(0, 0, 0, 0);
		GioView* view = [[GioView alloc] initWithFrame:frame pixelFormat:pixFormat];

		[view setWantsBestResolutionOpenGLSurface:YES];
		[view setWantsLayer:YES]; // The default in Mojave.

		return CFBridgingRetain(view);
	}
}

void gio_updateDisplayLink(CFTypeRef viewRef, CGDirectDisplayID dispID) {
	GioView *view = (__bridge GioView *)viewRef;
	[view updateDisplay:dispID];
}

void gio_setAnimating(CFTypeRef viewRef, BOOL anim) {
	GioView *view = (__bridge GioView *)viewRef;
	dispatch_async(dispatch_get_main_queue(), ^{
		[view setAnimating:anim];
	});
}

CFTypeRef gio_contextForView(CFTypeRef viewRef) {
	NSOpenGLView *view = (__bridge NSOpenGLView *)viewRef;
	return (__bridge CFTypeRef)view.openGLContext;
}

void gio_clearCurrentContext(void) {
	[NSOpenGLContext clearCurrentContext];
}

void gio_makeCurrentContext(CFTypeRef ctxRef) {
	NSOpenGLContext *ctx = (__bridge NSOpenGLContext *)ctxRef;
	return [ctx makeCurrentContext];
}

A  => ui/app/internal/gl/functions.go +473 -0
@@ 1,473 @@
// SPDX-License-Identifier: Unlicense OR MIT

// +build darwin linux

package gl

import (
	"unsafe"
)

/*
#cgo linux LDFLAGS: -lGLESv2 -ldl
#cgo darwin,!ios LDFLAGS: -framework OpenGL

#include <stdlib.h>

#ifdef __APPLE__
#cgo CFLAGS: -DGL_SILENCE_DEPRECATION
	#include "TargetConditionals.h"
	#if TARGET_OS_IPHONE
	#include <OpenGLES/ES3/gl.h>
	#else
	#include <OpenGL/gl3.h>
	#endif
#else
#define __USE_GNU
#include <dlfcn.h>
#include <GLES2/gl2.h>
#include <GLES3/gl3.h>
#endif

static void (*_glInvalidateFramebuffer)(GLenum target, GLsizei numAttachments, const GLenum *attachments);

static void (*_glBeginQuery)(GLenum target, GLuint id);
static void (*_glDeleteQueries)(GLsizei n, const GLuint *ids);
static void (*_glEndQuery)(GLenum target);
static void (*_glGenQueries)(GLsizei n, GLuint *ids);
static void (*_glGetQueryObjectuiv)(GLuint id, GLenum pname, GLuint *params);

// The pointer-free version of glVertexAttribPointer, to avoid the Cgo pointer checks.
__attribute__ ((visibility ("hidden"))) void gio_glVertexAttribPointer(GLuint index, GLint size, GLenum type, GLboolean normalized, GLsizei stride, uintptr_t offset) {
	glVertexAttribPointer(index, size, type, normalized, stride, (const GLvoid *)offset);
}

// The pointer-free version of glDrawElements, to avoid the Cgo pointer checks.
__attribute__ ((visibility ("hidden"))) void gio_glDrawElements(GLenum mode, GLsizei count, GLenum type, const uintptr_t offset) {
	glDrawElements(mode, count, type, (const GLvoid *)offset);
}

__attribute__ ((visibility ("hidden"))) void gio_glInvalidateFramebuffer(GLenum target, GLenum attachment) {
	// Framebuffer invalidation is just a hint and can safely be ignored.
	if (_glInvalidateFramebuffer != NULL) {
		_glInvalidateFramebuffer(target, 1, &attachment);
	}
}

__attribute__ ((visibility ("hidden"))) void gio_glBeginQuery(GLenum target, GLenum attachment) {
	_glBeginQuery(target, attachment);
}

__attribute__ ((visibility ("hidden"))) void gio_glDeleteQueries(GLsizei n, const GLuint *ids) {
	_glDeleteQueries(n, ids);
}

__attribute__ ((visibility ("hidden"))) void gio_glEndQuery(GLenum target) {
	_glEndQuery(target);
}

__attribute__ ((visibility ("hidden"))) void gio_glGenQueries(GLsizei n, GLuint *ids) {
	_glGenQueries(n, ids);
}

__attribute__ ((visibility ("hidden"))) void gio_glGetQueryObjectuiv(GLuint id, GLenum pname, GLuint *params) {
	_glGetQueryObjectuiv(id, pname, params);
}

__attribute__((constructor)) static void gio_loadGLFunctions() {
#ifdef __APPLE__
	#if TARGET_OS_IPHONE
	_glInvalidateFramebuffer = glInvalidateFramebuffer;
	_glBeginQuery = glBeginQuery;
	_glDeleteQueries = glDeleteQueries;
	_glEndQuery = glEndQuery;
	_glGenQueries = glGenQueries;
	_glGetQueryObjectuiv = glGetQueryObjectuiv;
	#endif
#else
	// Load libGLESv3 if available.
	dlopen("libGLESv3.so", RTLD_NOW | RTLD_GLOBAL);
	_glInvalidateFramebuffer = dlsym(RTLD_DEFAULT, "glInvalidateFramebuffer");
	// Fall back to EXT_invalidate_framebuffer if available.
	if (_glInvalidateFramebuffer == NULL) {
		_glInvalidateFramebuffer = dlsym(RTLD_DEFAULT, "glDiscardFramebufferEXT");
	}

	_glBeginQuery = dlsym(RTLD_DEFAULT, "glBeginQuery");
	if (_glBeginQuery == NULL)
		_glBeginQuery = dlsym(RTLD_DEFAULT, "glBeginQueryEXT");
	_glDeleteQueries = dlsym(RTLD_DEFAULT, "glDeleteQueries");
	if (_glDeleteQueries == NULL)
		_glDeleteQueries = dlsym(RTLD_DEFAULT, "glDeleteQueriesEXT");
	_glEndQuery = dlsym(RTLD_DEFAULT, "glEndQuery");
	if (_glEndQuery == NULL)
		_glEndQuery = dlsym(RTLD_DEFAULT, "glEndQueryEXT");
	_glGenQueries = dlsym(RTLD_DEFAULT, "glGenQueries");
	if (_glGenQueries == NULL)
		_glGenQueries = dlsym(RTLD_DEFAULT, "glGenQueriesEXT");
	_glGetQueryObjectuiv = dlsym(RTLD_DEFAULT, "glGetQueryObjectuiv");
	if (_glGetQueryObjectuiv == NULL)
		_glGetQueryObjectuiv = dlsym(RTLD_DEFAULT, "glGetQueryObjectuivEXT");
#endif
}
*/
import "C"

type Functions struct{}

func (f *Functions) ActiveTexture(texture Enum) {
	C.glActiveTexture(C.GLenum(texture))
}

func (f *Functions) AttachShader(p Program, s Shader) {
	C.glAttachShader(C.GLuint(p), C.GLuint(s))
}

func (f *Functions) BeginQuery(target Enum, query Query) {
	C.gio_glBeginQuery(C.GLenum(target), C.GLenum(query))
}

func (f *Functions) BindAttribLocation(p Program, a Attrib, name string) {
	cname := C.CString(name)
	defer C.free(unsafe.Pointer(cname))
	C.glBindAttribLocation(C.GLuint(p), C.GLuint(a), cname)
}

func (f *Functions) BindBuffer(target Enum, b Buffer) {
	C.glBindBuffer(C.GLenum(target), C.GLuint(b))
}

func (f *Functions) BindFramebuffer(target Enum, fb Framebuffer) {
	C.glBindFramebuffer(C.GLenum(target), C.GLuint(fb))
}

func (f *Functions) BindRenderbuffer(target Enum, fb Renderbuffer) {
	C.glBindRenderbuffer(C.GLenum(target), C.GLuint(fb))
}

func (f *Functions) BindTexture(target Enum, t Texture) {
	C.glBindTexture(C.GLenum(target), C.GLuint(t))
}

func (f *Functions) BlendEquation(mode Enum) {
	C.glBlendEquation(C.GLenum(mode))
}

func (f *Functions) BlendFunc(sfactor, dfactor Enum) {
	C.glBlendFunc(C.GLenum(sfactor), C.GLenum(dfactor))
}

func (f *Functions) BufferData(target Enum, src []byte, usage Enum) {
	var p unsafe.Pointer
	if len(src) > 0 {
		p = unsafe.Pointer(&src[0])
	}
	C.glBufferData(C.GLenum(target), C.GLsizeiptr(len(src)), p, C.GLenum(usage))
}

func (f *Functions) CheckFramebufferStatus(target Enum) Enum {
	return Enum(C.glCheckFramebufferStatus(C.GLenum(target)))
}

func (f *Functions) Clear(mask Enum) {
	C.glClear(C.GLbitfield(mask))
}

func (f *Functions) ClearColor(red float32, green float32, blue float32, alpha float32) {
	C.glClearColor(C.GLfloat(red), C.GLfloat(green), C.GLfloat(blue), C.GLfloat(alpha))
}

func (f *Functions) ClearDepthf(d float32) {
	C.glClearDepthf(C.GLfloat(d))
}

func (f *Functions) CompileShader(s Shader) {
	C.glCompileShader(C.GLuint(s))
}

func (f *Functions) CreateBuffer() Buffer {
	var handle C.GLuint
	C.glGenBuffers(1, &handle)
	return Buffer(handle)
}

func (f *Functions) CreateFramebuffer() Framebuffer {
	var handle C.GLuint
	C.glGenFramebuffers(1, &handle)
	return Framebuffer(handle)
}

func (f *Functions) CreateProgram() Program {
	return Program(C.glCreateProgram())
}

func (f *Functions) CreateQuery() Query {
	var handle C.GLuint
	C.gio_glGenQueries(1, &handle)
	return Query(handle)
}

func (f *Functions) CreateRenderbuffer() Renderbuffer {
	var handle C.GLuint
	C.glGenRenderbuffers(1, &handle)
	return Renderbuffer(handle)
}

func (f *Functions) CreateShader(ty Enum) Shader {
	return Shader(C.glCreateShader(C.GLenum(ty)))
}

func (f *Functions) CreateTexture() Texture {
	var handle C.GLuint
	C.glGenTextures(1, &handle)
	return Texture(handle)
}

func (f *Functions) DeleteBuffer(v Buffer) {
	handle := C.GLuint(v)
	C.glDeleteBuffers(1, &handle)
}

func (f *Functions) DeleteFramebuffer(v Framebuffer) {
	handle := C.GLuint(v)
	C.glDeleteFramebuffers(1, &handle)
}

func (f *Functions) DeleteProgram(p Program) {
	C.glDeleteProgram(C.GLuint(p))
}

func (f *Functions) DeleteQuery(query Query) {
	handle := C.GLuint(query)
	C.gio_glDeleteQueries(1, &handle)
}

func (f *Functions) DeleteRenderbuffer(v Renderbuffer) {
	handle := C.GLuint(v)
	C.glDeleteRenderbuffers(1, &handle)
}

func (f *Functions) DeleteShader(s Shader) {
	C.glDeleteShader(C.GLuint(s))
}

func (f *Functions) DeleteTexture(v Texture) {
	handle := C.GLuint(v)
	C.glDeleteTextures(1, &handle)
}

func (f *Functions) DepthFunc(v Enum) {
	C.glDepthFunc(C.GLenum(v))
}

func (f *Functions) DepthMask(mask bool) {
	m := C.GLboolean(C.GL_FALSE)
	if mask {
		m = C.GLboolean(C.GL_TRUE)
	}
	C.glDepthMask(m)
}

func (f *Functions) DisableVertexAttribArray(a Attrib) {
	C.glDisableVertexAttribArray(C.GLuint(a))
}

func (f *Functions) Disable(cap Enum) {
	C.glDisable(C.GLenum(cap))
}

func (f *Functions) DrawArrays(mode Enum, first int, count int) {
	C.glDrawArrays(C.GLenum(mode), C.GLint(first), C.GLsizei(count))
}

func (f *Functions) DrawElements(mode Enum, count int, ty Enum, offset int) {
	C.gio_glDrawElements(C.GLenum(mode), C.GLsizei(count), C.GLenum(ty), C.uintptr_t(offset))
}

func (f *Functions) Enable(cap Enum) {
	C.glEnable(C.GLenum(cap))
}

func (f *Functions) EndQuery(target Enum) {
	C.gio_glEndQuery(C.GLenum(target))
}

func (f *Functions) EnableVertexAttribArray(a Attrib) {
	C.glEnableVertexAttribArray(C.GLuint(a))
}

func (f *Functions) Finish() {
	C.glFinish()
}

func (f *Functions) FramebufferRenderbuffer(target, attachment, renderbuffertarget Enum, renderbuffer Renderbuffer) {
	C.glFramebufferRenderbuffer(C.GLenum(target), C.GLenum(attachment), C.GLenum(renderbuffertarget), C.GLuint(renderbuffer))
}

func (f *Functions) FramebufferTexture2D(target, attachment, texTarget Enum, t Texture, level int) {
	C.glFramebufferTexture2D(C.GLenum(target), C.GLenum(attachment), C.GLenum(texTarget), C.GLuint(t), C.GLint(level))
}

func (f *Functions) GetError() Enum {
	return Enum(C.glGetError())
}

func (f *Functions) GetRenderbufferParameteri(target, pname Enum) int {
	// Hope this is enough room.
	var buf [100]C.GLint
	C.glGetRenderbufferParameteriv(C.GLenum(target), C.GLenum(pname), &buf[0])
	return int(buf[0])
}

func (f *Functions) GetFramebufferAttachmentParameteri(target, attachment, pname Enum) int {
	// Hope this is enough room.
	var buf [100]C.GLint
	C.glGetFramebufferAttachmentParameteriv(C.GLenum(target), C.GLenum(attachment), C.GLenum(pname), &buf[0])
	return int(buf[0])
}

func (f *Functions) GetInteger(pname Enum) int {
	// Hope this is enough room.
	var buf [100]C.GLint
	C.glGetIntegerv(C.GLenum(pname), &buf[0])
	return int(buf[0])
}

func (f *Functions) GetProgrami(p Program, pname Enum) int {
	// Hope this is enough room.
	var buf [100]C.GLint
	C.glGetProgramiv(C.GLuint(p), C.GLenum(pname), &buf[0])
	return int(buf[0])
}

func (f *Functions) GetProgramInfoLog(p Program) string {
	var plen C.GLsizei
	C.glGetProgramInfoLog(C.GLuint(p), 0, &plen, nil)
	if plen == 0 {
		return ""
	}
	// Make room for the string and the null terminator.
	buf := make([]byte, plen+1)
	C.glGetProgramInfoLog(C.GLuint(p), C.GLsizei(len(buf)), &plen, (*C.GLchar)(unsafe.Pointer(&buf[0])))
	return string(buf[:len(buf)-1])
}

func (f *Functions) GetQueryObjectuiv(query Query, pname Enum) uint {
	// Hope this is enough room.
	var buf [100]C.GLuint
	C.gio_glGetQueryObjectuiv(C.GLuint(query), C.GLenum(pname), &buf[0])
	return uint(buf[0])
}

func (f *Functions) GetShaderi(s Shader, pname Enum) int {
	// Hope this is enough room.
	var buf [100]C.GLint
	C.glGetShaderiv(C.GLuint(s), C.GLenum(pname), &buf[0])
	return int(buf[0])
}

func (f *Functions) GetShaderInfoLog(s Shader) string {
	var plen C.GLsizei
	C.glGetShaderInfoLog(C.GLuint(s), 0, &plen, nil)
	if plen == 0 {
		return ""
	}
	// Make room for the string and the null terminator.
	buf := make([]byte, plen+1)
	C.glGetShaderInfoLog(C.GLuint(s), C.GLsizei(len(buf)), &plen, (*C.GLchar)(unsafe.Pointer(&buf[0])))
	return string(buf[:len(buf)-1])
}

func (f *Functions) GetString(pname Enum) string {
	str := C.glGetString(C.GLenum(pname))
	return C.GoString((*C.char)(unsafe.Pointer(str)))
}

func (f *Functions) GetUniformLocation(p Program, name string) Uniform {
	cname := C.CString(name)
	defer C.free(unsafe.Pointer(cname))
	return Uniform(C.glGetUniformLocation(C.GLuint(p), cname))
}

func (f *Functions) InvalidateFramebuffer(target, attachment Enum) {
	C.gio_glInvalidateFramebuffer(C.GLenum(target), C.GLenum(attachment))
}

func (f *Functions) LinkProgram(p Program) {
	C.glLinkProgram(C.GLuint(p))
}

func (f *Functions) PixelStorei(pname Enum, param int32) {
	C.glPixelStorei(C.GLenum(pname), C.GLint(param))
}

func (f *Functions) Scissor(x, y, width, height int32) {
	C.glScissor(C.GLint(x), C.GLint(y), C.GLsizei(width), C.GLsizei(height))
}

func (f *Functions) RenderbufferStorage(target, internalformat Enum, width, height int) {
	C.glRenderbufferStorage(C.GLenum(target), C.GLenum(internalformat), C.GLsizei(width), C.GLsizei(height))
}

func (f *Functions) ShaderSource(s Shader, src string) {
	csrc := C.CString(src)
	defer C.free(unsafe.Pointer(csrc))
	strlen := C.GLint(len(src))
	C.glShaderSource(C.GLuint(s), 1, &csrc, &strlen)
}

func (f *Functions) TexImage2D(target Enum, level int, internalFormat int, width int, height int, format Enum, ty Enum, data []byte) {
	var p unsafe.Pointer
	if len(data) > 0 {
		p = unsafe.Pointer(&data[0])
	}
	C.glTexImage2D(C.GLenum(target), C.GLint(level), C.GLint(internalFormat), C.GLsizei(width), C.GLsizei(height), 0, C.GLenum(format), C.GLenum(ty), p)
}

func (f *Functions) TexSubImage2D(target Enum, level int, x int, y int, width int, height int, format Enum, ty Enum, data []byte) {
	var p unsafe.Pointer
	if len(data) > 0 {
		p = unsafe.Pointer(&data[0])
	}
	C.glTexSubImage2D(C.GLenum(target), C.GLint(level), C.GLint(x), C.GLint(y), C.GLsizei(width), C.GLsizei(height), C.GLenum(format), C.GLenum(ty), p)
}

func (f *Functions) TexParameteri(target, pname Enum, param int) {
	C.glTexParameteri(C.GLenum(target), C.GLenum(pname), C.GLint(param))
}

func (f *Functions) Uniform1f(dst Uniform, v float32) {
	C.glUniform1f(C.GLint(dst), C.GLfloat(v))
}

func (f *Functions) Uniform1i(dst Uniform, v int) {
	C.glUniform1i(C.GLint(dst), C.GLint(v))
}

func (f *Functions) Uniform2f(dst Uniform, v0 float32, v1 float32) {
	C.glUniform2f(C.GLint(dst), C.GLfloat(v0), C.GLfloat(v1))
}

func (f *Functions) Uniform3f(dst Uniform, v0 float32, v1 float32, v2 float32) {
	C.glUniform3f(C.GLint(dst), C.GLfloat(v0), C.GLfloat(v1), C.GLfloat(v2))
}

func (f *Functions) Uniform4f(dst Uniform, v0 float32, v1 float32, v2 float32, v3 float32) {
	C.glUniform4f(C.GLint(dst), C.GLfloat(v0), C.GLfloat(v1), C.GLfloat(v2), C.GLfloat(v3))
}

func (f *Functions) UseProgram(p Program) {
	C.glUseProgram(C.GLuint(p))
}

func (f *Functions) VertexAttribPointer(dst Attrib, size int, ty Enum, normalized bool, stride int, offset int) {
	var n C.GLboolean = C.GL_FALSE
	if normalized {
		n = C.GL_TRUE
	}
	C.gio_glVertexAttribPointer(C.GLuint(dst), C.GLint(size), C.GLenum(ty), n, C.GLsizei(stride), C.uintptr_t(offset))
}

func (f *Functions) Viewport(x int, y int, width int, height int) {
	C.glViewport(C.GLint(x), C.GLint(y), C.GLsizei(width), C.GLsizei(height))
}

A  => ui/app/internal/gl/gl.go +166 -0
@@ 1,166 @@
// SPDX-License-Identifier: Unlicense OR MIT

package gl

type (
	Attrib       uint
	Buffer       uint
	Enum         uint
	Framebuffer  uint
	Program      uint
	Renderbuffer uint
	Shader       uint
	Texture      uint
	Uniform      int
	Query        uint
)

type Context interface {
	Functions() *Functions
	Present() error
	MakeCurrent() error
	Release()
}

const (
	ARRAY_BUFFER                          = 0x8892
	BLEND                                 = 0xbe2
	CLAMP_TO_EDGE                         = 0x812f
	COLOR_ATTACHMENT0                     = 0x8ce0
	COLOR_BUFFER_BIT                      = 0x4000
	COMPILE_STATUS                        = 0x8b81
	DEPTH_BUFFER_BIT                      = 0x100
	DEPTH_ATTACHMENT                      = 0x8d00
	DEPTH_COMPONENT16                     = 0x81a5
	DEPTH_TEST                            = 0xb71
	DST_COLOR                             = 0x306
	ELEMENT_ARRAY_BUFFER                  = 0x8893
	EXTENSIONS                            = 0x1f03
	FLOAT                                 = 0x1406
	FRAGMENT_SHADER                       = 0x8b30
	FRAMEBUFFER                           = 0x8d40
	FRAMEBUFFER_ATTACHMENT_COLOR_ENCODING = 0x8210
	FRAMEBUFFER_BINDING                   = 0x8ca6
	FRAMEBUFFER_COMPLETE                  = 0x8cd5
	HALF_FLOAT                            = 0x140b
	GREATER                               = 0x204
	LINEAR                                = 0x2601
	LINK_STATUS                           = 0x8b82
	LUMINANCE                             = 0x1909
	MAX_TEXTURE_SIZE                      = 0xd33
	NEAREST                               = 0x2600
	ONE                                   = 0x1
	ONE_MINUS_SRC_ALPHA                   = 0x303
	QUERY_RESULT                          = 0x8866
	QUERY_RESULT_AVAILABLE                = 0x8867
	R16F                                  = 0x822d
	R8                                    = 0x8229
	READ_FRAMEBUFFER                      = 0x8ca8
	RED                                   = 0x1903
	RENDERBUFFER                          = 0x8d41
	RENDERBUFFER_BINDING                  = 0x8ca7
	RENDERBUFFER_HEIGHT                   = 0x8d43
	RENDERBUFFER_WIDTH                    = 0x8d42
	RGB                                   = 0x1907
	RGBA                                  = 0x1908
	RGBA8                                 = 0x8058
	SHORT                                 = 0x1402
	SRGB                                  = 0x8c40
	SRGB_ALPHA_EXT                        = 0x8c42
	SRGB8                                 = 0x8c41
	SRGB8_ALPHA8                          = 0x8c43
	STATIC_DRAW                           = 0x88e4
	TEXTURE_2D                            = 0xde1
	TEXTURE_MAG_FILTER                    = 0x2800
	TEXTURE_MIN_FILTER                    = 0x2801
	TEXTURE_WRAP_S                        = 0x2802
	TEXTURE_WRAP_T                        = 0x2803
	TEXTURE0                              = 0x84c0
	TEXTURE1                              = 0x84c1
	TRIANGLE_STRIP                        = 0x5
	TRIANGLES                             = 0x4
	UNPACK_ALIGNMENT                      = 0xcf5
	UNSIGNED_BYTE                         = 0x1401
	UNSIGNED_SHORT                        = 0x1403
	VERSION                               = 0x1f02
	VERTEX_SHADER                         = 0x8b31
	ZERO                                  = 0x0

	// EXT_disjoint_timer_query
	TIME_ELAPSED_EXT = 0x88BF
	GPU_DISJOINT_EXT = 0x8FBB
)

// Enforce Functions interface.
var _ interface {
	ActiveTexture(texture Enum)
	AttachShader(p Program, s Shader)
	BeginQuery(target Enum, query Query)
	BindAttribLocation(p Program, a Attrib, name string)
	BindBuffer(target Enum, b Buffer)
	BindFramebuffer(target Enum, fb Framebuffer)
	BindRenderbuffer(target Enum, rb Renderbuffer)
	BindTexture(target Enum, t Texture)
	BlendEquation(mode Enum)
	BlendFunc(sfactor, dfactor Enum)
	BufferData(target Enum, src []byte, usage Enum)
	CheckFramebufferStatus(target Enum) Enum
	Clear(mask Enum)
	ClearColor(red, green, blue, alpha float32)
	ClearDepthf(d float32)
	CompileShader(s Shader)
	CreateBuffer() Buffer
	CreateFramebuffer() Framebuffer
	CreateProgram() Program
	CreateQuery() Query
	CreateRenderbuffer() Renderbuffer
	CreateShader(ty Enum) Shader
	CreateTexture() Texture
	DeleteBuffer(v Buffer)
	DeleteFramebuffer(v Framebuffer)
	DeleteProgram(p Program)
	DeleteQuery(query Query)
	DeleteRenderbuffer(v Renderbuffer)
	DeleteShader(s Shader)
	DeleteTexture(v Texture)
	DepthFunc(f Enum)
	DepthMask(mask bool)
	DisableVertexAttribArray(a Attrib)
	Disable(cap Enum)
	DrawArrays(mode Enum, first, count int)
	DrawElements(mode Enum, count int, ty Enum, offset int)
	Enable(cap Enum)
	EnableVertexAttribArray(a Attrib)
	EndQuery(target Enum)
	Finish()
	FramebufferRenderbuffer(target, attachment, renderbuffertarget Enum, renderbuffer Renderbuffer)
	FramebufferTexture2D(target, attachment, texTarget Enum, t Texture, level int)
	GetError() Enum
	GetRenderbufferParameteri(target, pname Enum) int
	GetFramebufferAttachmentParameteri(target, attachment, pname Enum) int
	GetInteger(pname Enum) int
	GetProgrami(p Program, pname Enum) int
	GetProgramInfoLog(p Program) string
	GetQueryObjectuiv(query Query, pname Enum) uint
	GetShaderi(s Shader, pname Enum) int
	GetShaderInfoLog(s Shader) string
	GetString(pname Enum) string
	GetUniformLocation(p Program, name string) Uniform
	InvalidateFramebuffer(target, attachment Enum)
	LinkProgram(p Program)
	PixelStorei(pname Enum, param int32)
	RenderbufferStorage(target, internalformat Enum, width, height int)
	Scissor(x, y, width, height int32)
	ShaderSource(s Shader, src string)
	TexImage2D(target Enum, level int, internalFormat int, width, height int, format, ty Enum, data []byte)
	TexSubImage2D(target Enum, level int, x, y, width, height int, format, ty Enum, data []byte)
	TexParameteri(target, pname Enum, param int)
	Uniform1f(dst Uniform, v float32)
	Uniform1i(dst Uniform, v int)
	Uniform2f(dst Uniform, v0, v1 float32)
	Uniform3f(dst Uniform, v0, v1, v2 float32)
	Uniform4f(dst Uniform, v0, v1, v2, v3 float32)
	UseProgram(p Program)
	VertexAttribPointer(dst Attrib, size int, ty Enum, normalized bool, stride, offset int)
	Viewport(x, y, width, height int)
} = (*Functions)(nil)

A  => ui/app/internal/gl/gl_windows.go +397 -0
@@ 1,397 @@
// SPDX-License-Identifier: Unlicense OR MIT

package gl

import (
	"math"
	"reflect"
	"syscall"
	"unsafe"

	"golang.org/x/sys/windows"
)

var (
	LibGLESv2                             = windows.NewLazyDLL("libGLESv2.dll")
	_glActiveTexture                      = LibGLESv2.NewProc("glActiveTexture")
	_glAttachShader                       = LibGLESv2.NewProc("glAttachShader")
	_glBeginQuery                         = LibGLESv2.NewProc("glBeginQuery")
	_glBindAttribLocation                 = LibGLESv2.NewProc("glBindAttribLocation")
	_glBindBuffer                         = LibGLESv2.NewProc("glBindBuffer")
	_glBindFramebuffer                    = LibGLESv2.NewProc("glBindFramebuffer")
	_glBindRenderbuffer                   = LibGLESv2.NewProc("glBindRenderbuffer")
	_glBindTexture                        = LibGLESv2.NewProc("glBindTexture")
	_glBlendEquation                      = LibGLESv2.NewProc("glBlendEquation")
	_glBlendFunc                          = LibGLESv2.NewProc("glBlendFunc")
	_glBufferData                         = LibGLESv2.NewProc("glBufferData")
	_glCheckFramebufferStatus             = LibGLESv2.NewProc("glCheckFramebufferStatus")
	_glClear                              = LibGLESv2.NewProc("glClear")
	_glClearColor                         = LibGLESv2.NewProc("glClearColor")
	_glClearDepthf                        = LibGLESv2.NewProc("glClearDepthf")
	_glDeleteQueries                      = LibGLESv2.NewProc("glDeleteQueries")
	_glCompileShader                      = LibGLESv2.NewProc("glCompileShader")
	_glGenBuffers                         = LibGLESv2.NewProc("glGenBuffers")
	_glGenFramebuffers                    = LibGLESv2.NewProc("glGenFramebuffers")
	_glCreateProgram                      = LibGLESv2.NewProc("glCreateProgram")
	_glGenRenderbuffers                   = LibGLESv2.NewProc("glGenRenderbuffers")
	_glCreateShader                       = LibGLESv2.NewProc("glCreateShader")
	_glGenTextures                        = LibGLESv2.NewProc("glGenTextures")
	_glDeleteBuffers                      = LibGLESv2.NewProc("glDeleteBuffers")
	_glDeleteFramebuffers                 = LibGLESv2.NewProc("glDeleteFramebuffers")
	_glDeleteProgram                      = LibGLESv2.NewProc("glDeleteProgram")
	_glDeleteShader                       = LibGLESv2.NewProc("glDeleteShader")
	_glDeleteRenderbuffers                = LibGLESv2.NewProc("glDeleteRenderbuffers")
	_glDeleteTextures                     = LibGLESv2.NewProc("glDeleteTextures")
	_glDepthFunc                          = LibGLESv2.NewProc("glDepthFunc")
	_glDepthMask                          = LibGLESv2.NewProc("glDepthMask")
	_glDisableVertexAttribArray           = LibGLESv2.NewProc("glDisableVertexAttribArray")
	_glDisable                            = LibGLESv2.NewProc("glDisable")
	_glDrawArrays                         = LibGLESv2.NewProc("glDrawArrays")
	_glDrawElements                       = LibGLESv2.NewProc("glDrawElements")
	_glEnable                             = LibGLESv2.NewProc("glEnable")
	_glEnableVertexAttribArray            = LibGLESv2.NewProc("glEnableVertexAttribArray")
	_glEndQuery                           = LibGLESv2.NewProc("glEndQuery")
	_glFinish                             = LibGLESv2.NewProc("glFinish")
	_glFramebufferRenderbuffer            = LibGLESv2.NewProc("glFramebufferRenderbuffer")
	_glFramebufferTexture2D               = LibGLESv2.NewProc("glFramebufferTexture2D")
	_glGenQueries                         = LibGLESv2.NewProc("glGenQueries")
	_glGetError                           = LibGLESv2.NewProc("glGetError")
	_glGetRenderbufferParameteri          = LibGLESv2.NewProc("glGetRenderbufferParameteri")
	_glGetFramebufferAttachmentParameteri = LibGLESv2.NewProc("glGetFramebufferAttachmentParameteri")
	_glGetIntegerv                        = LibGLESv2.NewProc("glGetIntegerv")
	_glGetProgramiv                       = LibGLESv2.NewProc("glGetProgramiv")
	_glGetProgramInfoLog                  = LibGLESv2.NewProc("glGetProgramInfoLog")
	_glGetQueryObjectuiv                  = LibGLESv2.NewProc("glGetQueryObjectuiv")
	_glGetShaderiv                        = LibGLESv2.NewProc("glGetShaderiv")
	_glGetShaderInfoLog                   = LibGLESv2.NewProc("glGetShaderInfoLog")
	_glGetString                          = LibGLESv2.NewProc("glGetString")
	_glGetUniformLocation                 = LibGLESv2.NewProc("glGetUniformLocation")
	_glInvalidateFramebuffer              = LibGLESv2.NewProc("glInvalidateFramebuffer")
	_glLinkProgram                        = LibGLESv2.NewProc("glLinkProgram")
	_glPixelStorei                        = LibGLESv2.NewProc("glPixelStorei")
	_glRenderbufferStorage                = LibGLESv2.NewProc("glRenderbufferStorage")
	_glScissor                            = LibGLESv2.NewProc("glScissor")
	_glShaderSource                       = LibGLESv2.NewProc("glShaderSource")
	_glTexImage2D                         = LibGLESv2.NewProc("glTexImage2D")
	_glTexSubImage2D                      = LibGLESv2.NewProc("glTexSubImage2D")
	_glTexParameteri                      = LibGLESv2.NewProc("glTexParameteri")
	_glUniform1f                          = LibGLESv2.NewProc("glUniform1f")
	_glUniform1i                          = LibGLESv2.NewProc("glUniform1i")
	_glUniform2f                          = LibGLESv2.NewProc("glUniform2f")
	_glUniform3f                          = LibGLESv2.NewProc("glUniform3f")
	_glUniform4f                          = LibGLESv2.NewProc("glUniform4f")
	_glUseProgram                         = LibGLESv2.NewProc("glUseProgram")
	_glVertexAttribPointer                = LibGLESv2.NewProc("glVertexAttribPointer")
	_glViewport                           = LibGLESv2.NewProc("glViewport")
)

type Functions struct{}

func (c *Functions) ActiveTexture(t Enum) {
	syscall.Syscall(_glActiveTexture.Addr(), 1, uintptr(t), 0, 0)
}
func (c *Functions) AttachShader(p Program, s Shader) {
	syscall.Syscall(_glAttachShader.Addr(), 2, uintptr(p), uintptr(s), 0)
}
func (f *Functions) BeginQuery(target Enum, query Query) {
	syscall.Syscall(_glBeginQuery.Addr(), 2, uintptr(target), uintptr(query), 0)
}
func (c *Functions) BindAttribLocation(p Program, a Attrib, name string) {
	cname := cString(name)
	syscall.Syscall(_glBindAttribLocation.Addr(), 3, uintptr(p), uintptr(a), uintptr(unsafe.Pointer(&cname[0])))
}
func (c *Functions) BindBuffer(target Enum, b Buffer) {
	syscall.Syscall(_glBindBuffer.Addr(), 2, uintptr(target), uintptr(b), 0)
}
func (c *Functions) BindFramebuffer(target Enum, fb Framebuffer) {
	syscall.Syscall(_glBindFramebuffer.Addr(), 2, uintptr(target), uintptr(fb), 0)
}
func (c *Functions) BindRenderbuffer(target Enum, rb Renderbuffer) {
	syscall.Syscall(_glBindRenderbuffer.Addr(), 2, uintptr(target), uintptr(rb), 0)
}
func (c *Functions) BindTexture(target Enum, t Texture) {
	syscall.Syscall(_glBindTexture.Addr(), 2, uintptr(target), uintptr(t), 0)
}
func (c *Functions) BlendEquation(mode Enum) {
	syscall.Syscall(_glBlendEquation.Addr(), 1, uintptr(mode), 0, 0)
}
func (c *Functions) BlendFunc(sfactor, dfactor Enum) {
	syscall.Syscall(_glBlendFunc.Addr(), 2, uintptr(sfactor), uintptr(dfactor), 0)
}
func (c *Functions) BufferData(target Enum, src []byte, usage Enum) {
	if n := len(src); n == 0 {
		syscall.Syscall6(_glBufferData.Addr(), 4, uintptr(target), 0, 0, uintptr(usage), 0, 0)
	} else {
		syscall.Syscall6(_glBufferData.Addr(), 4, uintptr(target), uintptr(n), uintptr(unsafe.Pointer(&src[0])), uintptr(usage), 0, 0)
	}
}
func (c *Functions) CheckFramebufferStatus(target Enum) Enum {
	s, _, _ := syscall.Syscall(_glCheckFramebufferStatus.Addr(), 1, uintptr(target), 0, 0)
	return Enum(s)
}
func (c *Functions) Clear(mask Enum) {
	syscall.Syscall(_glClear.Addr(), 1, uintptr(mask), 0, 0)
}
func (c *Functions) ClearColor(red, green, blue, alpha float32) {
	syscall.Syscall6(_glClearColor.Addr(), 4, uintptr(math.Float32bits(red)), uintptr(math.Float32bits(green)), uintptr(math.Float32bits(blue)), uintptr(math.Float32bits(alpha)), 0, 0)
}
func (c *Functions) ClearDepthf(d float32) {
	syscall.Syscall(_glClearDepthf.Addr(), 1, uintptr(math.Float32bits(d)), 0, 0)
}
func (c *Functions) CompileShader(s Shader) {
	syscall.Syscall(_glCompileShader.Addr(), 1, uintptr(s), 0, 0)
}
func (c *Functions) CreateBuffer() Buffer {
	var buf uintptr
	syscall.Syscall(_glGenBuffers.Addr(), 2, 1, uintptr(unsafe.Pointer(&buf)), 0)
	return Buffer(buf)
}
func (c *Functions) CreateFramebuffer() Framebuffer {
	var fb uintptr
	syscall.Syscall(_glGenFramebuffers.Addr(), 2, 1, uintptr(unsafe.Pointer(&fb)), 0)
	return Framebuffer(fb)
}
func (c *Functions) CreateProgram() Program {
	p, _, _ := syscall.Syscall(_glCreateProgram.Addr(), 0, 0, 0, 0)
	return Program(p)
}
func (f *Functions) CreateQuery() Query {
	var q uintptr
	syscall.Syscall(_glGenQueries.Addr(), 2, 1, uintptr(unsafe.Pointer(&q)), 0)
	return Query(q)
}
func (c *Functions) CreateRenderbuffer() Renderbuffer {
	var rb uintptr
	syscall.Syscall(_glGenRenderbuffers.Addr(), 2, 1, uintptr(unsafe.Pointer(&rb)), 0)
	return Renderbuffer(rb)
}
func (c *Functions) CreateShader(ty Enum) Shader {
	s, _, _ := syscall.Syscall(_glCreateShader.Addr(), 1, uintptr(ty), 0, 0)
	return Shader(s)
}
func (c *Functions) CreateTexture() Texture {
	var t uintptr
	syscall.Syscall(_glGenTextures.Addr(), 2, 1, uintptr(unsafe.Pointer(&t)), 0)
	return Texture(t)
}
func (c *Functions) DeleteBuffer(v Buffer) {
	syscall.Syscall(_glDeleteBuffers.Addr(), 2, 1, uintptr(unsafe.Pointer(&v)), 0)
}
func (c *Functions) DeleteFramebuffer(v Framebuffer) {
	syscall.Syscall(_glDeleteFramebuffers.Addr(), 2, 1, uintptr(unsafe.Pointer(&v)), 0)
}
func (c *Functions) DeleteProgram(p Program) {
	syscall.Syscall(_glDeleteProgram.Addr(), 1, uintptr(p), 0, 0)
}
func (f *Functions) DeleteQuery(query Query) {
	syscall.Syscall(_glDeleteQueries.Addr(), 2, 1, uintptr(unsafe.Pointer(&query)), 0)
}
func (c *Functions) DeleteShader(s Shader) {
	syscall.Syscall(_glDeleteShader.Addr(), 1, uintptr(s), 0, 0)
}
func (c *Functions) DeleteRenderbuffer(v Renderbuffer) {
	syscall.Syscall(_glDeleteRenderbuffers.Addr(), 2, 1, uintptr(unsafe.Pointer(&v)), 0)
}
func (c *Functions) DeleteTexture(v Texture) {
	syscall.Syscall(_glDeleteTextures.Addr(), 2, 1, uintptr(unsafe.Pointer(&v)), 0)
}
func (c *Functions) DepthFunc(f Enum) {
	syscall.Syscall(_glDepthFunc.Addr(), 1, uintptr(f), 0, 0)
}
func (c *Functions) DepthMask(mask bool) {
	var m uintptr
	if mask {
		m = 1
	}
	syscall.Syscall(_glDepthMask.Addr(), 1, m, 0, 0)
}
func (c *Functions) DisableVertexAttribArray(a Attrib) {
	syscall.Syscall(_glDisableVertexAttribArray.Addr(), 1, uintptr(a), 0, 0)
}
func (c *Functions) Disable(cap Enum) {
	syscall.Syscall(_glDisable.Addr(), 1, uintptr(cap), 0, 0)
}
func (c *Functions) DrawArrays(mode Enum, first, count int) {
	syscall.Syscall(_glDrawArrays.Addr(), 3, uintptr(mode), uintptr(first), uintptr(count))
}
func (c *Functions) DrawElements(mode Enum, count int, ty Enum, offset int) {
	syscall.Syscall6(_glDrawElements.Addr(), 4, uintptr(mode), uintptr(count), uintptr(ty), uintptr(offset), 0, 0)
}
func (c *Functions) Enable(cap Enum) {
	syscall.Syscall(_glEnable.Addr(), 1, uintptr(cap), 0, 0)
}
func (c *Functions) EnableVertexAttribArray(a Attrib) {
	syscall.Syscall(_glEnableVertexAttribArray.Addr(), 1, uintptr(a), 0, 0)
}
func (f *Functions) EndQuery(target Enum) {
	syscall.Syscall(_glEndQuery.Addr(), 1, uintptr(target), 0, 0)
}
func (c *Functions) Finish() {
	syscall.Syscall(_glFinish.Addr(), 0, 0, 0, 0)
}
func (c *Functions) FramebufferRenderbuffer(target, attachment, renderbuffertarget Enum, renderbuffer Renderbuffer) {
	syscall.Syscall6(_glFramebufferRenderbuffer.Addr(), 4, uintptr(target), uintptr(attachment), uintptr(renderbuffertarget), uintptr(renderbuffer), 0, 0)
}
func (c *Functions) FramebufferTexture2D(target, attachment, texTarget Enum, t Texture, level int) {
	syscall.Syscall6(_glFramebufferTexture2D.Addr(), 5, uintptr(target), uintptr(attachment), uintptr(texTarget), uintptr(t), uintptr(level), 0)
}
func (c *Functions) GetError() Enum {
	e, _, _ := syscall.Syscall(_glGetError.Addr(), 0, 0, 0, 0)
	return Enum(e)
}
func (c *Functions) GetRenderbufferParameteri(target, pname Enum) int {
	p, _, _ := syscall.Syscall(_glGetRenderbufferParameteri.Addr(), 2, uintptr(target), uintptr(pname), 0)
	return int(p)
}
func (c *Functions) GetFramebufferAttachmentParameteri(target, attachment, pname Enum) int {