Compare commits

2 Commits
Author SHA1 Message Date
Blendman974 26e570f0e1 feat: choose translation output language independently
Tests / test (pull_request) Canceled after 0s
Tests / test-kotlin (pull_request) Canceled after 0s
Tests / stats-server (pull_request) Canceled after 0s
Tests / evenhub-extensions (pull_request) Canceled after 0s
Secret scan / gitleaks (pull_request) Canceled after 0s
2026-11-08 00:56:36 +01:00
Blendman974 69bb92da7b feat: configure an independent OpenAI translation model
Tests / test (pull_request) Canceled after 0s
Tests / test-kotlin (pull_request) Canceled after 0s
Tests / stats-server (pull_request) Canceled after 0s
Tests / evenhub-extensions (pull_request) Canceled after 0s
Secret scan / gitleaks (pull_request) Canceled after 0s
2026-11-08 00:48:54 +01:00
447 changed files with 4153 additions and 37715 deletions

No files matched your search

-2
View File
@@ -58,8 +58,6 @@ native/kotlin/.kotlin/
native/kotlin/build/
native/kotlin/shared/build/
native/kotlin/local.properties
# Go SSH transport build outputs (scripts/go-ssh-build.cjs)
native/ssh/build/
native/kotlin/plugin/platforms/android/*.aar
native/kotlin/plugin/platforms/ios/
!native/kotlin/plugin/platforms/
-16
View File
@@ -13,22 +13,6 @@ and others who contributed to documenting the G2's bluetooth protocol and firmwa
G2 microphone LC3 decoding uses Google's liblc3: https://github.com/google/liblc3
Even G1 support builds on ScouterKit (MIT License), by the ScouterKit authors:
https://github.com/mafik/scouter_kit. The firmware Faceclaw installs on a G1 is a
modified build of its g1v0, using the Zephyr RTOS and Nordic's nRF Connect SDK; the
parts of it that are Even Realities' own are downloaded from Even, not shipped here.
RayNeo iO support builds on the Glassly SDK and custom firmware by Tetramo, Inc.
and Mentra Labs, Inc. (MIT License): https://github.com/tetramo-labs/glassly and
https://github.com/tetramo-labs/glassly-rayneo-cfw. The iO wire codecs in
native/kotlin/shared/.../rayneo/RayNeoIoProtocol.kt follow their protocol work.
Bluetooth keyboard support on the G2 uses the keyboard bridge Tetramo, Inc. wrote
for their Glassly custom firmware (https://github.com/tetramo-labs/g2flash).
Faceclaw's client for it, in app/g2/keyboard/ and app/apps/terminal/terminal-keys.ts,
follows the reference client in that repository and the keyboard handling in the
Glassly app (MIT License): https://github.com/tetramo-labs/glassly.
iPhone notification icons are Pebble's notification and timeline icons, from
https://github.com/pebble-dev/iconography: the original Pebble Technology icons
and Rebble community additions. They are licensed under the Apache License 2.0;
+4 -13
View File
@@ -19,9 +19,6 @@ dependencies {
// app's captions (translation models download on demand, per language).
implementation 'com.google.mlkit:language-id:17.0.6'
implementation 'com.google.mlkit:translate:17.0.3'
// Pure-Java Opus decoder for the RayNeo iO's microphone (16 kHz mono Opus over
// BLE); the same one Glassly uses. See AndroidSpeechEngines.OpusDecoderAdapter.
implementation 'io.github.jaredmdobson:concentus:1.0.1'
}
def faceclawSherpaOnnxVersion = "1.13.0"
@@ -264,21 +261,15 @@ if (faceclawVersionFile.exists()) {
faceclawVersionName = faceclawVersionMatch.group(1)
}
}
def faceclawBuildRevision = System.getenv("FACECLAW_ANDROID_BUILD_REVISION") ?: "0"
if (!(faceclawBuildRevision ==~ /[0-4]/)) {
throw new GradleException("FACECLAW_ANDROID_BUILD_REVISION must be an integer from 0 to 4")
}
def faceclawVersionCode = {
// Prerelease labels belong in versionName, not the numeric versionCode.
def parts = (faceclawVersionName.split('-')[0].tokenize('.') + ["0", "0"]).take(3)*.toInteger()
// Last digit: build revision * 2, plus form factor (phone 0, Wear OS 1).
// Play requires distinct version codes even on separate form-factor tracks.
return (parts[0] * 10000 + parts[1] * 100 + parts[2]) * 10 + faceclawBuildRevision.toInteger() * 2
return parts[0] * 10000 + parts[1] * 100 + parts[2]
}()
android {
compileSdkVersion 36
buildToolsVersion "36.0.0"
compileSdkVersion 35
buildToolsVersion "35"
// ndkVersion ""
sourceSets.main {
@@ -288,7 +279,7 @@ android {
defaultConfig {
minSdkVersion 24
targetSdkVersion 36
targetSdkVersion 35
ndk {
// The NativeScript platform build.gradle has already added all four
// ABIs by the time app.gradle is applied, and abiFilters is additive,
@@ -46,15 +46,6 @@ class AndroidSessionLink(private val bleManager: FaceclawBleManager) : SessionLi
override fun isBonded(address: String): Boolean = bleManager.isBonded(address)
override fun requestBond(address: String) = bleManager.requestBond(address)
override fun removeBond(address: String): Boolean = bleManager.removeBond(address)
override fun hasCharacteristic(address: String, characteristicUuid: String): Boolean =
bleManager.hasCharacteristic(address, characteristicUuid)
override fun refreshServices(address: String, timeoutMs: Int): Boolean = bleManager.refreshServices(address, timeoutMs)
override fun prepareBenchmarkLink(address: String, mode: Int) = bleManager.prepareBenchmarkLink(address, mode)
override fun recordDisplayFrameSent() = FaceclawBleManager.recordDisplayFrameSent()
@@ -94,44 +94,6 @@ internal object AndroidSpeechEngines {
override fun close() = decoder.close()
}
/**
* The RayNeo iO's microphone: one 16 kHz mono Opus packet per BLE message (its remote
* audio stream, GlassesSessionIo), decoded with Concentus. No G2 DSP metadata; packets
* carry no counter, so "missing" stays 0 and a lost one is just a gap.
*/
class OpusDecoderAdapter : AudioPacketDecoder {
private val decoder = io.github.jaredmdobson.OpusDecoder(RayNeoIoProtocol.VOICE_SAMPLE_RATE, 1)
private var packets = 0L
private var errors = 0L
override fun decodePacket(packet: ByteArray?, pcmOut: ShortArray?): Int {
if (packet == null || packet.isEmpty() || pcmOut == null) return 0
return try {
val count = decoder.decode(packet, 0, packet.size, pcmOut, 0, pcmOut.size, false)
packets++
count
} catch (e: io.github.jaredmdobson.OpusException) {
errors++
if (errors == 1L || errors % 100 == 0L) {
android.util.Log.w("FaceclawVoice", "Opus decode error ($errors so far): ${e.message}")
}
0
}
}
/** 120 ms, Opus's longest frame. */
override val maxSamplesPerPacket: Int get() = RayNeoIoProtocol.VOICE_SAMPLE_RATE * 120 / 1000
override val hasFrameMeta: Boolean get() = false
override val lastAngleDegrees: Int get() = 0
override val lastSsr: Int get() = 0
override val realPackets: Long get() = packets
override val duplicatePackets: Long get() = 0
override val missingPackets: Long get() = 0
override val decodeErrors: Long get() = errors
override fun close() = decoder.resetState()
}
/**
* Open the phone's own microphone at the pipeline's native format (16 kHz
* mono PCM16), or null when it cannot start — the permission is missing
@@ -48,17 +48,6 @@ class AndroidStockLink(context: Context) : StockLink, FaceclawBleListener {
else -> BondState.UNKNOWN
}
override fun negotiatedMtu(address: String): Int = bleManager.getNegotiatedMtu(address)
override fun requestBond(address: String) = bleManager.requestBond(address)
override fun removeBond(address: String): Boolean = bleManager.removeBond(address)
override fun hasCharacteristic(address: String, characteristicUuid: String): Boolean =
bleManager.hasCharacteristic(address, characteristicUuid)
override fun refreshServices(address: String, timeoutMs: Int): Boolean = bleManager.refreshServices(address, timeoutMs)
override fun disconnect(address: String) = bleManager.disconnect(address)
override fun close() = bleManager.close()
@@ -189,28 +189,6 @@ class FaceclawBleCommunicator(context: Context, rightAddress: String?, leftAddre
fun setRequiredFirmwareRevision(revision: Int) = core.setRequiredFirmwareRevision(revision)
/**
* Drive a RayNeo iO (constructed with its address as both arms and no ring). The phone's
* protocol identity is derived from ANDROID_ID so it survives reinstalls: the glasses bind
* to the first identity that authenticates and refuse others until put in pairing mode.
*/
fun useRayNeoIo() {
val androidId = try {
android.provider.Settings.Secure.getString(appContext.contentResolver, android.provider.Settings.Secure.ANDROID_ID)
} catch (_: Exception) {
null
}
val seed = "faceclaw-rayneo-phone-id:" + (androidId ?: android.os.Build.FINGERPRINT)
val phoneId = java.security.MessageDigest.getInstance("SHA-256").digest(seed.toByteArray(Charsets.UTF_8)).copyOf(6)
val model = android.os.Build.MODEL ?: "Android"
core.useRayNeoIo(phoneId, model, android.os.Build.MANUFACTURER ?: "Android", model)
}
fun isRayNeoIo(): Boolean = core.isRayNeoIo()
/** Drive an Even G1 on g1v0 (two arms, as a G2 pair's addresses). */
fun useG1() = core.useG1()
fun start() {
if (core.start()) {
activeInstance = this
@@ -257,9 +235,6 @@ class FaceclawBleCommunicator(context: Context, rightAddress: String?, leftAddre
fun setFaceclawWakeLeaseEnabled(enabled: Boolean): Boolean = core.setFaceclawWakeLeaseEnabled(enabled)
/** See [GlassesSessionCore.setVoiceWakeEnabled]. */
fun setVoiceWakeEnabled(enabled: Boolean) = core.setVoiceWakeEnabled(enabled)
fun awaitEvenHubSessionReady(timeoutMs: Int): Boolean = core.awaitEvenHubSessionReady(timeoutMs)
fun setImuReportEnabled(enable: Boolean, reportFrq: Int) = core.setImuReportEnabled(enable, reportFrq)
@@ -290,13 +265,6 @@ class FaceclawBleCommunicator(context: Context, rightAddress: String?, leftAddre
/** See [GlassesSessionCore.getRingStatus]. */
fun getRingStatus(): String = core.getRingStatus()
/** See [GlassesSessionCore.setHeadUpTrigger]. */
fun setHeadUpTrigger(degrees: Int, originalSwitch: Int, originalAngle: Int) =
core.setHeadUpTrigger(degrees, originalSwitch, originalAngle)
/** See [GlassesSessionCore.getHeadUpStatus]. */
fun getHeadUpStatus(): String = core.getHeadUpStatus()
/** See [GlassesSessionCore.setGlassesRingLink]. */
fun setGlassesRingLink(action: String, fallbackAddress: String?, fallbackName: String?): Boolean =
core.setGlassesRingLink(action, fallbackAddress, fallbackName)
@@ -329,13 +297,6 @@ class FaceclawBleCommunicator(context: Context, rightAddress: String?, leftAddre
fun sendFaceclawMicControl(record: ByteArray?, label: String, rightTemple: Boolean, leftTemple: Boolean): Boolean =
core.sendFaceclawMicControl(record, label, rightTemple, leftTemple)
fun addKeyboardEventListener(listener: FaceclawKeyboardEventListener?) = core.addKeyboardEventListener(listener)
fun removeKeyboardEventListener(listener: FaceclawKeyboardEventListener?) = core.removeKeyboardEventListener(listener)
fun sendFaceclawKeyboardControl(record: ByteArray?, label: String): Boolean =
core.sendFaceclawKeyboardControl(record, label)
fun startG2AudioForwarding(listener: FaceclawAudioPacketListener?): Boolean = core.startG2AudioForwarding(listener)
fun stopG2AudioForwarding() = core.stopG2AudioForwarding()
@@ -27,11 +27,6 @@ import java.util.concurrent.atomic.AtomicLong
class FaceclawBleManager(context: Context) {
companion object {
private const val TAG = "FaceclawBle"
/** Not public API, but long stable, and the only way short of sending the user to Settings. */
fun removeBond(device: BluetoothDevice): Boolean =
device.bondState == BluetoothDevice.BOND_NONE || device.javaClass.getMethod("removeBond").invoke(device) == true
private val WRITE_RETRY_DELAYS_MS = intArrayOf(1, 1, 1, 2, 4, 8, 12, 20, 35, 100, 200)
// Gap after each LE 2M PHY request before checking whether it landed and asking
@@ -165,36 +160,6 @@ class FaceclawBleManager(context: Context) {
}
}
/**
* Remove this phone's half of a pairing the other side no longer has. False when the platform refused.
*/
fun removeBond(address: String?): Boolean {
if (address == null || address.trim().isEmpty()) {
return false
}
return try {
removeBond(bluetoothAdapter.getRemoteDevice(address))
} catch (t: Throwable) {
Log.w(TAG, "removeBond failed: $address", t)
false
}
}
/** Start bonding, without waiting: the OS may ask the user, and [isBonded] tells when it is done. */
fun requestBond(address: String?) {
if (address == null || address.trim().isEmpty()) {
return
}
try {
val device = bluetoothAdapter.getRemoteDevice(address)
if (device.bondState == BluetoothDevice.BOND_NONE) {
Log.i(TAG, "requestBond " + address + ": " + device.createBond())
}
} catch (t: Throwable) {
Log.w(TAG, "requestBond failed: " + address, t)
}
}
/**
* Android's bond state for this address (BluetoothDevice.BOND_NONE /
* BOND_BONDING / BOND_BONDED), or -1 when it cannot be determined.
@@ -399,31 +364,6 @@ class FaceclawBleManager(context: Context) {
return status == BluetoothGatt.GATT_SUCCESS
}
/** After discovery: whether the service table has this characteristic. */
fun hasCharacteristic(address: String, characteristicUuid: String): Boolean {
synchronized(bluetoothApiLock) {
val gatt = links[address]?.gatt ?: return false
val target = UUID.fromString(characteristicUuid)
return gatt.services.any { it.getCharacteristic(target) != null }
}
}
/**
* Forget the service table Android remembers for a bonded device and read it again. A G1 arm keeps its address across an
* install that changes its services, and a cached table would make new firmware look like the old one.
*/
fun refreshServices(address: String, timeoutMs: Int): Boolean {
synchronized(bluetoothApiLock) {
val gatt = links[address]?.gatt ?: return false
try {
gatt.javaClass.getMethod("refresh").invoke(gatt)
} catch (t: Throwable) {
Log.w(TAG, "refresh failed: $address", t)
}
}
return discoverServices(address, timeoutMs)
}
fun enableNotifications(address: String, characteristicUuid: String, enable: Boolean, timeoutMs: Int): Boolean {
synchronized(bluetoothApiLock) {
val gatt = requireGatt(address)
@@ -129,21 +129,13 @@ class FaceclawDeviceDiscovery(context: Context) {
&& manufacturerData[0] == 0x45.toByte() && manufacturerData[1] == 0x52.toByte()) {
return true
}
// RayNeo iO: manufacturer company 0x5716 ([07][MAC]...) outside pairing mode;
// in pairing mode only the "RayNeo iO-xxxx" name identifies it.
if (manufacturerData != null && manufacturerData.size >= 2
&& manufacturerData[0] == 0x16.toByte() && manufacturerData[1] == 0x57.toByte()) {
return true
}
if (name == null) {
return false
}
val upper = name.uppercase()
// A bare "R1" substring also matches unrelated hardware ("Oppo Enco
// R1"), and TS only classifies rings from the stock name prefix.
// Even G1: its stock name, or g1v0's G1CFW_L_XXXXXX (before 0.5.66: G1v0_L, G1v0S_R).
return upper.contains("G2") || upper.startsWith("EVEN R1") || upper.startsWith("RAYNEO") ||
upper.startsWith("EVEN G1") || upper.startsWith("G1CFW_") || upper.startsWith("G1V0")
return upper.contains("G2") || upper.startsWith("EVEN R1")
}
private fun buildJson(
@@ -1,106 +0,0 @@
package com.faceclaw.app
import android.content.Context
import android.os.Handler
import android.os.Looper
/**
* Android host for the shared [G1ArmFlashFlow]: owns the GATT link and the worker thread for one G1 arm's install, its
* restore to Even's firmware, or a question about what it runs, and delivers listener callbacks on the main thread. For app/native/g1-flasher.ts.
*/
class FaceclawG1Flasher(
context: Context,
address: String?,
side: String?,
firmwarePath: String?,
netCorePath: String?,
mode: String?,
connectWindowMs: Int,
) {
private val link = AndroidStockLink(context.applicationContext)
private val mainHandler = Handler(Looper.getMainLooper())
private val lock = Any()
@Volatile
private var listener: FaceclawFirmwareFlasherListener? = null
@Volatile
private var worker: Thread? = null
private val flow =
G1ArmFlashFlow(
link,
address ?: "",
side ?: "",
firmwarePath ?: "",
netCorePath ?: "",
G1ArmFlashFlow.mode(mode ?: ""),
connectWindowMs,
object : FaceclawFirmwareFlasherListener {
override fun onLog(line: String?) {
android.util.Log.i(G1ArmFlashFlow.TAG, line ?: "")
post { it.onLog(line) }
}
override fun onProgress(
lens: String?,
componentIndex: Int,
componentCount: Int,
blockIndex: Int,
blockCount: Int,
bytesSent: Long,
bytesTotal: Long,
) = post { it.onProgress(lens, componentIndex, componentCount, blockIndex, blockCount, bytesSent, bytesTotal) }
override fun onState(state: String?, detail: String?) = post { it.onState(state, detail ?: "") }
override fun onComplete(success: Boolean, detail: String?) = post { it.onComplete(success, detail ?: "") }
},
)
fun setListener(listener: FaceclawFirmwareFlasherListener?) {
this.listener = listener
}
fun start() {
synchronized(lock) {
if (worker != null) {
return
}
val w = Thread({ flow.run() }, "faceclaw-g1-flash")
worker = w
w.start()
}
}
fun cancel() {
flow.cancel()
}
fun close() {
cancel()
try {
link.close()
} catch (ignored: Exception) {
}
}
companion object {
/** The phone's pairing with an address these glasses no longer use. */
@JvmStatic
fun forgetPairing(context: Context, address: String?) {
if (address == null) return
val manager = context.applicationContext.getSystemService(Context.BLUETOOTH_SERVICE) as android.bluetooth.BluetoothManager
FaceclawBleManager.removeBond(manager.adapter.getRemoteDevice(address))
}
}
private fun post(action: (FaceclawFirmwareFlasherListener) -> Unit) {
mainHandler.post {
val current = listener
if (current != null) {
action(current)
}
}
}
}
@@ -1,212 +0,0 @@
package com.faceclaw.app
import android.content.ClipData
import android.content.ClipboardManager
import android.content.Context
import android.os.Build
import android.os.Handler
import android.os.Looper
import android.security.keystore.KeyGenParameterSpec
import android.security.keystore.KeyProperties
import android.security.keystore.StrongBoxUnavailableException
import android.util.Base64
import android.util.Log
import com.faceclaw.ssh.faceclawssh.Channel
import com.faceclaw.ssh.faceclawssh.Faceclawssh
import com.faceclaw.ssh.faceclawssh.Listener
import com.faceclaw.ssh.faceclawssh.Signer
import java.security.KeyPairGenerator
import java.security.KeyStore
import java.security.PrivateKey
import java.security.Signature
import java.security.spec.ECGenParameterSpec
import javax.crypto.Cipher
import javax.crypto.KeyGenerator
import javax.crypto.SecretKey
import javax.crypto.spec.GCMParameterSpec
/**
* One SSH channel for the TypeScript side (app/native/ssh.ts), backed by the
* gomobile-bound Go transport in native/ssh. `spec` is the Go openSpec JSON.
* Events arrive from Go goroutines and are posted to the Looper of the
* thread that constructed this object (a JS isolate's own thread), like
* FaceclawWebSocket.
*/
class FaceclawSshChannel(spec: String, useKey: Boolean, listener: FaceclawSshListener) {
private val handler = Handler(Looper.myLooper() ?: Looper.getMainLooper())
private val channel: Channel
init {
val signer = if (useKey) FaceclawSshKeys.signer() else null
channel = Faceclawssh.open(spec, signer, object : Listener {
override fun onEvent(kind: String?, payload: String?) {
handler.post {
try {
listener.onEvent(kind, payload)
} catch (t: Throwable) {
Log.w(TAG, "listener onEvent($kind) failed", t)
}
}
}
})
}
fun send(text: String): Boolean = channel.send(text)
fun close() = channel.close()
private companion object {
const val TAG = "FaceclawSsh"
}
}
/**
* The device's SSH client key and the Terminal app's saved SSH passwords.
*
* The client key is an ECDSA P-256 key generated inside Android Keystore
* (StrongBox when the device has one) and never exportable: only its public
* half leaves the keystore, as an authorized_keys line. Passwords are
* encrypted with an AES-GCM key that also lives in Keystore, and stored in a
* private SharedPreferences file kept apart from the settings store (which is
* exported by the settings port).
*/
object FaceclawSshKeys {
private const val TAG = "FaceclawSsh"
private const val CLIENT_KEY_ALIAS = "faceclaw-ssh-client"
private const val SECRET_KEY_ALIAS = "faceclaw-ssh-secrets"
private const val SECRETS_PREFS = "faceclaw_ssh_secrets"
private const val GCM_TAG_BITS = 128
private fun keyStore(): KeyStore = KeyStore.getInstance("AndroidKeyStore").apply { load(null) }
@JvmStatic
@Synchronized
fun hasClientKey(): Boolean = keyStore().containsAlias(CLIENT_KEY_ALIAS)
/** Generate the client key if it doesn't exist yet. */
@JvmStatic
@Synchronized
fun ensureClientKey() {
if (keyStore().containsAlias(CLIENT_KEY_ALIAS)) return
fun spec(strongBox: Boolean): KeyGenParameterSpec {
val builder = KeyGenParameterSpec.Builder(CLIENT_KEY_ALIAS, KeyProperties.PURPOSE_SIGN)
.setAlgorithmParameterSpec(ECGenParameterSpec("secp256r1"))
.setDigests(KeyProperties.DIGEST_SHA256)
if (strongBox && Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) builder.setIsStrongBoxBacked(true)
return builder.build()
}
val generator = KeyPairGenerator.getInstance(KeyProperties.KEY_ALGORITHM_EC, "AndroidKeyStore")
try {
generator.initialize(spec(strongBox = true))
generator.generateKeyPair()
} catch (e: Exception) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P && e is StrongBoxUnavailableException) {
Log.i(TAG, "StrongBox unavailable; generating the SSH key in the TEE")
} else {
Log.w(TAG, "StrongBox key generation failed; retrying in the TEE", e)
}
generator.initialize(spec(strongBox = false))
generator.generateKeyPair()
}
Log.i(TAG, "generated SSH client key")
}
/** X.509 SubjectPublicKeyInfo DER of the client key, or null if none. */
@JvmStatic
fun publicKey(): ByteArray? = keyStore().getCertificate(CLIENT_KEY_ALIAS)?.publicKey?.encoded
/** The client key's authorized_keys line (generating the key if needed). */
@JvmStatic
fun authorizedKey(comment: String): String {
ensureClientKey()
return Faceclawssh.authorizedKey(publicKey(), comment)
}
/** The client key's SHA256 fingerprint (generating the key if needed). */
@JvmStatic
fun fingerprint(): String {
ensureClientKey()
return Faceclawssh.fingerprint(publicKey())
}
/** A Go Signer over the client key, generating the key if needed. */
@JvmStatic
fun signer(): Signer? = try {
ensureClientKey()
val publicKey = publicKey() ?: throw IllegalStateException("client key has no certificate")
object : Signer {
override fun publicKey(): ByteArray = publicKey
override fun sign(data: ByteArray?): ByteArray {
val privateKey = keyStore().getKey(CLIENT_KEY_ALIAS, null) as PrivateKey
return Signature.getInstance("SHA256withECDSA").run {
initSign(privateKey)
update(data ?: ByteArray(0))
sign()
}
}
}
} catch (t: Throwable) {
Log.w(TAG, "SSH client key unavailable", t)
null
}
private fun secretKey(): SecretKey {
val store = keyStore()
(store.getKey(SECRET_KEY_ALIAS, null) as? SecretKey)?.let { return it }
val generator = KeyGenerator.getInstance(KeyProperties.KEY_ALGORITHM_AES, "AndroidKeyStore")
generator.init(
KeyGenParameterSpec.Builder(SECRET_KEY_ALIAS, KeyProperties.PURPOSE_ENCRYPT or KeyProperties.PURPOSE_DECRYPT)
.setBlockModes(KeyProperties.BLOCK_MODE_GCM)
.setEncryptionPaddings(KeyProperties.ENCRYPTION_PADDING_NONE)
.setKeySize(256)
.build()
)
return generator.generateKey()
}
private fun prefs(context: Context) =
context.applicationContext.getSharedPreferences(SECRETS_PREFS, Context.MODE_PRIVATE)
@JvmStatic
@Synchronized
fun setSecret(context: Context, id: String, value: String) {
val cipher = Cipher.getInstance("AES/GCM/NoPadding")
cipher.init(Cipher.ENCRYPT_MODE, secretKey())
val sealed = cipher.iv + cipher.doFinal(value.toByteArray(Charsets.UTF_8))
prefs(context).edit().putString(id, Base64.encodeToString(sealed, Base64.NO_WRAP)).apply()
}
/** The saved secret, or "" if none (or it can no longer be decrypted). */
@JvmStatic
@Synchronized
fun getSecret(context: Context, id: String): String {
val encoded = prefs(context).getString(id, null) ?: return ""
return try {
val sealed = Base64.decode(encoded, Base64.NO_WRAP)
val cipher = Cipher.getInstance("AES/GCM/NoPadding")
val ivLength = 12
cipher.init(Cipher.DECRYPT_MODE, secretKey(), GCMParameterSpec(GCM_TAG_BITS, sealed, 0, ivLength))
String(cipher.doFinal(sealed, ivLength, sealed.size - ivLength), Charsets.UTF_8)
} catch (t: Throwable) {
Log.w(TAG, "saved SSH secret unreadable; dropping it", t)
prefs(context).edit().remove(id).apply()
""
}
}
@JvmStatic
@Synchronized
fun removeSecret(context: Context, id: String) {
prefs(context).edit().remove(id).apply()
}
/** Put text on the phone's clipboard (from any thread). */
@JvmStatic
fun copyToClipboard(context: Context, label: String, text: String) {
val appContext = context.applicationContext
Handler(Looper.getMainLooper()).post {
val clipboard = appContext.getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
clipboard.setPrimaryClip(ClipData.newPlainText(label, text))
}
}
}
@@ -114,24 +114,7 @@ class FaceclawVoiceController(context: Context) {
}
}
override fun micArrayLink(): MicArrayLink? {
val currentCommunicator = communicator ?: return null
return object : MicArrayLink {
override fun startForwarding(listener: FaceclawAudioPacketListener): Boolean =
currentCommunicator.startG2AudioForwarding(listener)
override fun stopForwarding() = currentCommunicator.stopG2AudioForwarding()
override fun isForwarding(): Boolean = currentCommunicator.isAudioCaptureActive()
override fun sendControl(record: ByteArray, label: String, rightTemple: Boolean, leftTemple: Boolean): Boolean =
currentCommunicator.sendFaceclawMicControl(record, label, rightTemple, leftTemple)
}
}
override fun createPacketDecoder(): AudioPacketDecoder =
if (communicator?.isRayNeoIo() == true) AndroidSpeechEngines.OpusDecoderAdapter()
else AndroidSpeechEngines.Lc3DecoderAdapter()
override fun createPacketDecoder(): AudioPacketDecoder = AndroidSpeechEngines.Lc3DecoderAdapter()
override fun openPhoneMic(): PcmAudioSource? = AndroidSpeechEngines.openPhoneMic(PHONE_MIC_CHUNK_SAMPLES)
@@ -151,15 +134,12 @@ class FaceclawVoiceController(context: Context) {
override fun release() {}
}
}
override fun recordingSink(): RecordingSink = RecordingSink { pcmBytes, sampleRate -> saveRecording(pcmBytes, sampleRate) }
}
private val session = VoiceCaptureSession(host)
// The recording of this controller's live captures (see startRecording). It outlives any one
// capture, and is only handed to the session while recording is enabled.
private var recording: MicRecordingWriter? = null
private var recordingEnabled = true
fun setListener(listener: FaceclawVoiceControllerListener?) {
session.setListener(listener)
}
@@ -173,6 +153,11 @@ class FaceclawVoiceController(context: Context) {
session.setUsePhoneMic(usePhoneMic)
}
/** When true, the decoded mic PCM for each session is saved as a WAV. */
fun setSaveRecordings(saveRecordings: Boolean) {
session.setSaveRecordings(saveRecordings)
}
/**
* Which on-device model {@link #start}("onboard") should load: "whisper"
* selects the second on-device model (sherpa-onnx offline Whisper); any
@@ -221,88 +206,6 @@ class FaceclawVoiceController(context: Context) {
session.setBeamFilter(enabled, centerDeg, halfWidthDeg)
}
/**
* Capture glasses sessions from the temples' own microphones (the custom firmware's
* mic_control stream) instead of the stock mixed stream. [configureRecord] is the CONFIGURE
* record to arm both temples with; null goes back to the mixed stream. Must be set before
* {@link #start}.
*/
fun setMicArray(configureRecord: ByteArray?) {
session.setMicArray(configureRecord)
}
/**
* Have the next {@link #start} play the recording at [path] (a .mka file) from [startMs] in
* place of any microphone; null goes back to the microphones. Progress arrives through the
* listener's onReplay.
*/
fun setReplay(path: String?, startMs: Long) {
session.setReplay(path, startMs)
}
/** Continue the running replay from [positionMs] into its recording. */
fun seekReplay(positionMs: Long) {
session.seekReplay(positionMs)
}
fun setReplayPaused(paused: Boolean) {
session.setReplayPaused(paused)
}
/**
* Record live captures to a new Matroska audio file at [path], from now until
* {@link #stopRecording}, across however many captures run in between: every source's audio
* as it arrives, before any processing. [configJson] describes the capture and is stored in
* the file. Returns false when the file cannot be created.
*/
@Synchronized
fun startRecording(path: String, configJson: String?): Boolean {
stopRecording()
return try {
val parent = File(path).parentFile
if (parent != null && !parent.exists()) parent.mkdirs()
recording = MicRecordingWriter(openFileOutput(path), configJson, System.currentTimeMillis())
session.setRecorder(if (recordingEnabled) recording else null)
true
} catch (t: Throwable) {
Log.w(TAG, "could not create recording $path", t)
recording = null
false
}
}
/**
* Whether captures are written to the recording right now: off while the capture running
* isn't one that is to be recorded.
*/
@Synchronized
fun setRecordingEnabled(enabled: Boolean) {
recordingEnabled = enabled
session.setRecorder(if (enabled) recording else null)
}
/** Whether a recording is open and still being written (false once writing to it failed). */
@Synchronized
fun isRecording(): Boolean = recording?.failed == false
/**
* Note a change in what is being captured (a JSON object) at the recording's current
* position; the audio after it is placed afresh on the recording's timeline.
*/
@Synchronized
fun noteRecordingEvent(json: String) {
recording?.event(json)
}
/** Complete the recording's file. Returns its length in ms, or -1 when none was open. */
@Synchronized
fun stopRecording(): Long {
val current = recording ?: return -1
recording = null
session.setRecorder(null)
return current.finish()
}
fun start(requestedMode: String?) {
session.start(requestedMode, 0)
}
@@ -328,6 +231,29 @@ class FaceclawVoiceController(context: Context) {
stop()
}
/** Save the session's decoded mic PCM as a 16 kHz mono 16-bit WAV. */
private fun saveRecording(pcmBytes: ByteArray, sampleRate: Int) {
try {
val dir = File(appContext.getExternalFilesDir(null), "voice-recordings")
if (!dir.exists() && !dir.mkdirs()) {
Log.w(TAG, "could not create voice-recordings dir")
return
}
val stamp = java.text.SimpleDateFormat("yyyyMMdd-HHmmss-SSS", java.util.Locale.US)
.format(java.util.Date())
val file = File(dir, "voice-$stamp.wav")
java.io.FileOutputStream(file).use { fos ->
fos.write(BinaryEncoding.wavHeader(pcmBytes.size, sampleRate, 1))
fos.write(pcmBytes)
}
Log.i(TAG, "saved voice recording " + file.absolutePath
+ " samples=" + (pcmBytes.size / 2)
+ " sec=" + String.format(java.util.Locale.US, "%.2f", pcmBytes.size / 2.0 / sampleRate))
} catch (t: Throwable) {
Log.w(TAG, "failed to save voice recording", t)
}
}
/**
* The on-device model directory for the given kind, populated by the
* download flow in asr-model.ts (releases before 0.5.0 copied the
@@ -167,7 +167,6 @@ class FontFileRenderer private constructor() {
* [width u16][height u16]
* [width*height coverage bytes, gamma-mapped]
* An ink-free glyph (space) has width = height = 0 but a valid advance.
* A color glyph (emoji) carries its ColorGlyphArt shade as its coverage.
* Empty array when the font cannot be loaded or the render fails.
*/
@JvmStatic
@@ -197,16 +196,11 @@ class FontFileRenderer private constructor() {
}
val penX = pad - bounds.left
val baselineY = pad - bounds.top
// Drawn in color: an emoji the system fallback supplies from a color font
// would keep only its silhouette in an alpha-only bitmap. ColorGlyphArt shades
// color glyphs and passes white ones through as their alpha.
val bitmap = Bitmap.createBitmap(bw, bh, Bitmap.Config.ARGB_8888)
val bitmap = Bitmap.createBitmap(bw, bh, Bitmap.Config.ALPHA_8)
val canvas = Canvas(bitmap)
canvas.drawText(text, penX.toFloat(), baselineY.toFloat(), paint)
val argb = IntArray(bw * bh)
bitmap.getPixels(argb, 0, bw, 0, 0, bw, bh)
val pixels = alpha8Pixels(bitmap, bw, bh)
bitmap.recycle()
val pixels = ColorGlyphArt.coverage(argb)
// Trim to the tight ink box; bearing/inkTop become pen- and
// line-relative inside the shared packer.
@@ -108,64 +108,6 @@ class ImageFileLoader private constructor() {
}
}
/** Longest side decodeRgbaFromBytes decodes at; larger images are subsampled. */
private const val MAX_RGBA_SIDE = 4096
/**
* Decode encoded image bytes (JPEG, PNG, WebP, GIF's first frame, BMP)
* to [widthLo, widthHi, heightLo, heightHi, R, G, B, A, ...] at native
* size, not premultiplied, so a transparent pixel keeps its colour;
* empty when undecodable. The EvenHub host converts these to grey the
* way the Even app does (app/apps/evenhub/image-pipeline.ts).
*/
@JvmStatic
fun decodeRgbaFromBytes(buffer: java.nio.ByteBuffer?): ByteArray {
if (buffer == null) return ByteArray(0)
try {
val source = buffer.duplicate()
source.rewind()
val data = ByteArray(source.remaining())
source.get(data)
if (data.isEmpty()) return ByteArray(0)
val bounds = BitmapFactory.Options().apply { inJustDecodeBounds = true }
BitmapFactory.decodeByteArray(data, 0, data.size, bounds)
if (bounds.outWidth <= 0 || bounds.outHeight <= 0) return ByteArray(0)
var sample = 1
while (maxOf(bounds.outWidth, bounds.outHeight) / sample > MAX_RGBA_SIDE) sample *= 2
val options = BitmapFactory.Options().apply {
inPreferredConfig = Bitmap.Config.ARGB_8888
inPremultiplied = false
inSampleSize = sample
}
val bitmap = BitmapFactory.decodeByteArray(data, 0, data.size, options) ?: return ByteArray(0)
val width = bitmap.width
val height = bitmap.height
val argb = IntArray(width * height)
bitmap.getPixels(argb, 0, width, 0, 0, width, height)
bitmap.recycle()
val packet = ByteArray(4 + argb.size * 4)
packet[0] = (width and 0xff).toByte()
packet[1] = ((width shr 8) and 0xff).toByte()
packet[2] = (height and 0xff).toByte()
packet[3] = ((height shr 8) and 0xff).toByte()
for (i in argb.indices) {
val pixel = argb[i]
val o = 4 + i * 4
packet[o] = (pixel shr 16).toByte()
packet[o + 1] = (pixel shr 8).toByte()
packet[o + 2] = pixel.toByte()
packet[o + 3] = (pixel ushr 24).toByte()
}
return packet
} catch (e: Exception) {
Log.w(TAG, "RGBA image decode failed", e)
return ByteArray(0)
} catch (e: OutOfMemoryError) {
Log.w(TAG, "RGBA image decode failed", e)
return ByteArray(0)
}
}
/**
* Scale a bitmap to fit within maxWidth x maxHeight (preserving aspect,
* never upscaling) and convert it to the grayscale packet format.
@@ -62,7 +62,7 @@ class PreviewBitmapUtil private constructor() {
/**
* A white-on-transparent bitmap of an 8bpp glasses icon (gray becomes
* alpha; black, which the glasses show as unlit, is transparent), for
* showing glasses artwork in the phone UI, which tints it to suit.
* showing glasses artwork on the phone's dark action bar.
*/
@JvmStatic
fun whiteIconFromGray(gray: ByteBuffer?, width: Int, height: Int): Bitmap {
@@ -1,13 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- Gear icon for the main page's Settings button (beside the mic and keyboard
buttons). Material Design "settings" glyph (Apache 2.0); base fill is
overridden by tint-color in remote-controls.css. -->
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24">
<path
android:fillColor="#FF333333"
android:pathData="M19.14,12.94c0.04,-0.3 0.06,-0.61 0.06,-0.94c0,-0.32 -0.02,-0.64 -0.07,-0.94l2.03,-1.58c0.18,-0.14 0.23,-0.41 0.12,-0.61l-1.92,-3.32c-0.12,-0.22 -0.37,-0.29 -0.59,-0.22l-2.39,0.96c-0.5,-0.38 -1.03,-0.7 -1.62,-0.94L14.4,2.81c-0.04,-0.24 -0.24,-0.41 -0.48,-0.41h-3.84c-0.24,0 -0.43,0.17 -0.47,0.41L9.25,5.35C8.66,5.59 8.12,5.92 7.63,6.29L5.24,5.33c-0.22,-0.08 -0.47,0 -0.59,0.22L2.74,8.87C2.62,9.08 2.66,9.34 2.86,9.48l2.03,1.58C4.84,11.36 4.8,11.69 4.8,12s0.02,0.64 0.07,0.94l-2.03,1.58c-0.18,0.14 -0.23,0.41 -0.12,0.61l1.92,3.32c0.12,0.22 0.37,0.29 0.59,0.22l2.39,-0.96c0.5,0.38 1.03,0.7 1.62,0.94l0.36,2.54c0.05,0.24 0.24,0.41 0.48,0.41h3.84c0.24,0 0.44,-0.17 0.47,-0.41l0.36,-2.54c0.59,-0.24 1.13,-0.56 1.62,-0.94l2.39,0.96c0.22,0.08 0.47,0 0.59,-0.22l1.92,-3.32c0.12,-0.22 0.07,-0.47 -0.12,-0.61L19.14,12.94zM12,15.6c-1.98,0 -3.6,-1.62 -3.6,-3.6s1.62,-3.6 3.6,-3.6s3.6,1.62 3.6,3.6S13.98,15.6 12,15.6z" />
</vector>
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- Keyboard icon for the main page's keyboard-dialog button (beside the mic
button). Material Design "keyboard" glyph (Apache 2.0); base fill is
overridden by tint-color in remote-controls.css. -->
<!-- Keyboard icon for the main page's tab-bar keyboard-dialog button (beside
the mic button). Material Design "keyboard" glyph (Apache 2.0); base fill
is overridden by tint-color in app.css. -->
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- Microphone icon for the main page's voice-input (wakeword) button.
<!-- Microphone icon for the main page's tab-bar wakeword button.
Converted from ~/Downloads/noun-microphone-1368757.svg (Noun Project
icon #1368757); base fill is overridden by tint-color in remote-controls.css. -->
icon #1368757); base fill is overridden by tint-color in app.css. -->
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
@@ -1,13 +0,0 @@
{
"images": [
{
"idiom": "universal",
"filename": "ic_control_settings@3x.png",
"scale": "3x"
}
],
"info": {
"author": "xcode",
"version": 1
}
}
Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.7 KiB

@@ -2,7 +2,7 @@
"images": [
{
"idiom": "universal",
"filename": "ic_control_keyboard@3x.png",
"filename": "ic_tab_keyboard@3x.png",
"scale": "3x"
}
],
File renamed without changes.

Before

Width:  |  Height:  |  Size: 590 B

After

Width:  |  Height:  |  Size: 590 B

@@ -2,7 +2,7 @@
"images": [
{
"idiom": "universal",
"filename": "ic_control_mic@3x.png",
"filename": "ic_tab_mic@3x.png",
"scale": "3x"
}
],
File renamed without changes.

Before

Width:  |  Height:  |  Size: 1.2 KiB

After

Width:  |  Height:  |  Size: 1.2 KiB

+1 -1
View File
@@ -13,7 +13,7 @@
<key>CFBundleDisplayName</key>
<string>Faceclaw</string>
<key>CFBundleShortVersionString</key>
<string>0.9.0</string>
<string>0.8.6</string>
<key>CFBundleVersion</key>
<string>1</string>
<key>LSRequiresIPhoneOS</key>
+9 -30
View File
@@ -1,7 +1,6 @@
#import "FaceclawFontRenderer.h"
#import <CoreText/CoreText.h>
#import <CoreGraphics/CoreGraphics.h>
#import <FaceclawKit/FaceclawKit.h>
#import <math.h>
// Immutable font descriptors may be shared; layout objects never cross threads.
@@ -43,9 +42,8 @@ static BOOL HasColorRun(CTLineRef line) {
}
// iOS falls back to Apple Color Emoji for default-text symbols the face lacks
// (▶ U+25B6 in Roboto), where Android's minikin picks a text font; the
// emoji is a tile (▶ becomes a rounded square: a silhouette in the alpha-only
// line rasters, a shaded tile in glyph cells), which reads worse than the
// text symbol. Request text presentation (U+FE0E) for each lone codepoint
// alpha-only raster keeps just the emoji's silhouette (▶ becomes a solid
// rounded square). Request text presentation (U+FE0E) for each lone codepoint
// that fell back to a color font, wherever a text font can supply it. Returns
// `text` itself when nothing changes.
static NSString *PreferTextPresentation(CTFontRef font, NSString *text, CTLineRef line, BOOL glyphLayout) {
@@ -80,34 +78,18 @@ static BOOL ValidText(NSString *text) { return text != nil && text.length <= 100
static BOOL ValidRaster(NSInteger width, NSInteger height) {
return width > 0 && height > 0 && width <= 65535 && height <= 65535 && width * height <= 4000000;
}
static void ConfigureTextContext(CGContextRef ctx) {
CGContextSetShouldAntialias(ctx, true);
CGContextSetShouldSmoothFonts(ctx, false); // Grayscale coverage, never LCD subpixel color.
CGContextSetShouldSubpixelPositionFonts(ctx, true);
CGContextSetShouldSubpixelQuantizeFonts(ctx, false);
CGContextSetTextMatrix(ctx, CGAffineTransformIdentity);
}
static CGContextRef CreateContext(NSMutableData *pixels, NSInteger width, NSInteger height) CF_RETURNS_RETAINED {
CGContextRef ctx = CGBitmapContextCreate(pixels.mutableBytes, width, height, 8, width, NULL, (CGBitmapInfo)kCGImageAlphaOnly);
if (ctx) {
ConfigureTextContext(ctx);
CGContextSetShouldAntialias(ctx, true);
CGContextSetShouldSmoothFonts(ctx, false); // Grayscale coverage, never LCD subpixel color.
CGContextSetShouldSubpixelPositionFonts(ctx, true);
CGContextSetShouldSubpixelQuantizeFonts(ctx, false);
CGContextSetTextMatrix(ctx, CGAffineTransformIdentity);
CGContextSetGrayFillColor(ctx, 1, 1);
}
return ctx;
}
// Premultiplied RGBA bytes, for a line with a color (emoji) run: an alpha-only raster keeps
// just the emoji's silhouette, so its colors go to ColorGlyphArt to be shaded instead.
static CGContextRef CreateColorContext(NSMutableData *pixels, NSInteger width, NSInteger height) CF_RETURNS_RETAINED {
CGColorSpaceRef space = CGColorSpaceCreateWithName(kCGColorSpaceSRGB);
CGContextRef ctx = CGBitmapContextCreate(pixels.mutableBytes, width, height, 8, width * 4, space,
(CGBitmapInfo)kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
CGColorSpaceRelease(space);
if (ctx) {
ConfigureTextContext(ctx);
CGContextSetRGBFillColor(ctx, 1, 1, 1, 1);
}
return ctx;
}
static void U16(uint8_t *out, NSInteger value) { out[0] = value & 255; out[1] = (value >> 8) & 255; }
static void GammaCopy(uint8_t *dst, const uint8_t *src, NSInteger count, double gamma) {
if (!isfinite(gamma) || gamma <= 0) gamma = 1;
@@ -168,13 +150,10 @@ static NSData *ImagePacket(NSData *pixels, NSInteger width, NSInteger height, do
NSInteger bottom = floor(CGRectGetMinY(bounds)) - 2;
NSInteger width = ceil(CGRectGetMaxX(bounds)) + 2 - left, height = top - bottom;
if (!ValidRaster(width, height)) { CFRelease(line); CFRelease(font); return [NSData data]; }
BOOL color = HasColorRun(line);
NSMutableData *raster = [NSMutableData dataWithLength:width * height * (color ? 4 : 1)];
CGContextRef ctx = color ? CreateColorContext(raster, width, height) : CreateContext(raster, width, height);
NSMutableData *pixels = [NSMutableData dataWithLength:width * height];
CGContextRef ctx = CreateContext(pixels, width, height);
if (!ctx) { CFRelease(line); CFRelease(font); return [NSData data]; }
CGContextSetTextPosition(ctx, -left, -bottom); CTLineDraw(line, ctx); CGContextRelease(ctx);
NSData *pixels = color ? [[FaceclawKitIosColorGlyph shared] shadeRgba:raster] : raster;
if ((NSInteger)pixels.length != width * height) { CFRelease(line); CFRelease(font); return [NSData data]; }
const uint8_t *src = pixels.bytes;
NSInteger x0 = width, y0 = height, x1 = -1, y1 = -1;
for (NSInteger y = 0; y < height; y++) for (NSInteger x = 0; x < width; x++) if (src[y * width + x]) {
-6
View File
@@ -14,10 +14,4 @@
+ (NSData *)bgraPacket:(CGImageRef)image maxSize:(NSInteger)maxSize;
/** bgraPacket of encoded image bytes (JPEG, PNG, ...); nil when undecodable. */
+ (NSData *)bgraPacketFromImageData:(NSData *)data maxSize:(NSInteger)maxSize;
/**
* [widthLo, widthHi, heightLo, heightHi, RGBA...] of encoded image bytes at native size
* (at most 4096 px a side), first frame only; nil when undecodable. CoreGraphics
* premultiplies, so transparent pixels come out black. For the EvenHub image pipeline.
*/
+ (NSData *)rgbaPacketFromImageData:(NSData *)data;
@end
-22
View File
@@ -158,26 +158,4 @@
CGImageRelease(image);
return packet;
}
+ (NSData *)rgbaPacketFromImageData:(NSData *)data {
if (!data.length) return nil;
CGImageSourceRef source = CGImageSourceCreateWithData((__bridge CFDataRef)data, NULL);
if (!source) return nil;
CGImageRef image = CGImageSourceCreateImageAtIndex(source, 0, NULL);
CFRelease(source);
if (!image) return nil;
size_t width = CGImageGetWidth(image), height = CGImageGetHeight(image);
if (width == 0 || height == 0 || width > 4096 || height > 4096) { CGImageRelease(image); return nil; }
uint8_t dimensions[] = { width & 255, (width >> 8) & 255, height & 255, (height >> 8) & 255 };
NSMutableData *packet = [NSMutableData dataWithBytes:dimensions length:4];
[packet increaseLengthBy:width * height * 4];
CGColorSpaceRef space = CGColorSpaceCreateDeviceRGB();
CGContextRef context = CGBitmapContextCreate((uint8_t *)packet.mutableBytes + 4, width, height, 8, width * 4, space,
kCGBitmapByteOrder32Big | (CGBitmapInfo)kCGImageAlphaPremultipliedLast);
CGColorSpaceRelease(space);
if (!context) { CGImageRelease(image); return nil; }
CGContextDrawImage(context, CGRectMake(0, 0, width, height), image);
CGContextRelease(context);
CGImageRelease(image);
return packet;
}
@end
-31
View File
@@ -1,31 +0,0 @@
#import <Foundation/Foundation.h>
/**
* One SSH channel for the TypeScript side (app/native/ssh.ios.ts), backed by
* the gomobile-bound Go transport in native/ssh. `spec` is the Go openSpec
* JSON. Events arrive on Go threads and queue here; the owning JS isolate
* drains them with takeEvents (a JSON array of {kind, payload}).
*/
@interface FaceclawSshChannel : NSObject
- (instancetype)initWithSpec:(NSString *)spec useKey:(BOOL)useKey;
- (NSString *)takeEvents;
- (BOOL)send:(NSString *)text;
- (void)close;
@end
/**
* The device's SSH client key (a non-exportable Secure Enclave P-256 key; a
* plain keychain key on the simulator, which has no Secure Enclave) and the
* Terminal app's saved SSH passwords (keychain generic passwords).
*/
@interface FaceclawSshKeys : NSObject
/** "ecdsa-sha2-nistp256 AAAA... comment"; generates the key if needed. nil on failure. */
+ (NSString *)authorizedKeyWithComment:(NSString *)comment;
/** "SHA256:..." fingerprint; generates the key if needed. nil on failure. */
+ (NSString *)fingerprint;
+ (void)setSecret:(NSString *)value forId:(NSString *)secretId;
/** The saved secret, or "" if none. */
+ (NSString *)secretForId:(NSString *)secretId;
+ (void)removeSecretForId:(NSString *)secretId;
+ (void)copyToClipboard:(NSString *)text;
@end
-234
View File
@@ -1,234 +0,0 @@
#import "FaceclawSsh.h"
#import <Faceclawssh/Faceclawssh.h>
#import <Security/Security.h>
#import <UIKit/UIKit.h>
static NSString *const kClientKeyTag = @"com.faceclaw.ssh.client";
static NSString *const kSecretService = @"com.faceclaw.ssh.secret";
#pragma mark - Client key
static NSData *ClientKeyTagData(void) {
return [kClientKeyTag dataUsingEncoding:NSUTF8StringEncoding];
}
static SecKeyRef CopyClientKey(void) {
NSDictionary *query = @{
(id)kSecClass: (id)kSecClassKey,
(id)kSecAttrApplicationTag: ClientKeyTagData(),
(id)kSecAttrKeyType: (id)kSecAttrKeyTypeECSECPrimeRandom,
(id)kSecReturnRef: @YES,
};
CFTypeRef key = NULL;
if (SecItemCopyMatching((__bridge CFDictionaryRef)query, &key) != errSecSuccess) return NULL;
return (SecKeyRef)key;
}
static SecKeyRef CreateClientKey(BOOL secureEnclave) {
SecAccessControlRef access = SecAccessControlCreateWithFlags(
kCFAllocatorDefault, kSecAttrAccessibleAfterFirstUnlockThisDeviceOnly,
secureEnclave ? kSecAccessControlPrivateKeyUsage : 0, NULL);
if (!access) return NULL;
NSMutableDictionary *attributes = [@{
(id)kSecAttrKeyType: (id)kSecAttrKeyTypeECSECPrimeRandom,
(id)kSecAttrKeySizeInBits: @256,
(id)kSecPrivateKeyAttrs: @{
(id)kSecAttrIsPermanent: @YES,
(id)kSecAttrApplicationTag: ClientKeyTagData(),
(id)kSecAttrAccessControl: (__bridge id)access,
},
} mutableCopy];
if (secureEnclave) attributes[(id)kSecAttrTokenID] = (id)kSecAttrTokenIDSecureEnclave;
CFErrorRef error = NULL;
SecKeyRef key = SecKeyCreateRandomKey((__bridge CFDictionaryRef)attributes, &error);
CFRelease(access);
if (!key && error) {
NSLog(@"FaceclawSsh: key generation failed (secure enclave %d): %@", secureEnclave, (__bridge NSError *)error);
CFRelease(error);
}
return key;
}
/** The client key, generating it on first use. Caller releases. */
static SecKeyRef CopyOrCreateClientKey(void) {
@synchronized([FaceclawSshKeys class]) {
SecKeyRef key = CopyClientKey();
if (key) return key;
key = CreateClientKey(YES);
#if TARGET_OS_SIMULATOR
if (!key) key = CreateClientKey(NO);
#endif
return key;
}
}
/** Uncompressed X9.62 public point (0x04 || X || Y). */
static NSData *PublicKeyData(SecKeyRef key) {
SecKeyRef publicKey = SecKeyCopyPublicKey(key);
if (!publicKey) return nil;
NSData *data = (__bridge_transfer NSData *)SecKeyCopyExternalRepresentation(publicKey, NULL);
CFRelease(publicKey);
return data;
}
@interface FaceclawSshSigner : NSObject <FaceclawsshSigner>
@end
@implementation FaceclawSshSigner {
SecKeyRef _key;
NSData *_publicKey;
}
- (instancetype)initWithKey:(SecKeyRef)key {
if ((self = [super init])) {
_key = (SecKeyRef)CFRetain(key);
_publicKey = PublicKeyData(key);
}
return self;
}
- (void)dealloc {
if (_key) CFRelease(_key);
}
- (NSData *)publicKey {
return _publicKey;
}
- (NSData *)sign:(NSData *)data error:(NSError **)error {
CFErrorRef cfError = NULL;
NSData *signature = (__bridge_transfer NSData *)SecKeyCreateSignature(
_key, kSecKeyAlgorithmECDSASignatureMessageX962SHA256, (__bridge CFDataRef)(data ?: [NSData data]), &cfError);
if (!signature) {
if (error) *error = (__bridge_transfer NSError *)cfError;
else if (cfError) CFRelease(cfError);
}
return signature;
}
@end
#pragma mark - Channel
@interface FaceclawSshEventQueue : NSObject <FaceclawsshListener>
@property(nonatomic, strong) NSMutableArray *events;
@property(nonatomic) BOOL ended;
@end
@implementation FaceclawSshEventQueue
- (instancetype)init {
if ((self = [super init])) _events = [NSMutableArray array];
return self;
}
- (void)onEvent:(NSString *)kind payload:(NSString *)payload {
@synchronized(self) {
if (_ended) return;
[_events addObject:@{@"kind": kind ?: @"", @"payload": payload ?: @""}];
}
}
@end
@implementation FaceclawSshChannel {
FaceclawsshChannel *_channel;
FaceclawSshEventQueue *_queue;
}
- (instancetype)initWithSpec:(NSString *)spec useKey:(BOOL)useKey {
if ((self = [super init])) {
_queue = [FaceclawSshEventQueue new];
FaceclawSshSigner *signer = nil;
if (useKey) {
SecKeyRef key = CopyOrCreateClientKey();
if (key) {
signer = [[FaceclawSshSigner alloc] initWithKey:key];
CFRelease(key);
}
}
_channel = FaceclawsshOpen(spec, signer, _queue);
}
return self;
}
- (NSString *)takeEvents {
NSArray *events;
@synchronized(_queue) {
if (_queue.events.count == 0) return @"[]";
events = [_queue.events copy];
[_queue.events removeAllObjects];
}
NSData *data = [NSJSONSerialization dataWithJSONObject:events options:0 error:nil];
return [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] ?: @"[]";
}
- (BOOL)send:(NSString *)text {
return [_channel send:text];
}
- (void)close {
@synchronized(_queue) {
_queue.ended = YES;
[_queue.events removeAllObjects];
}
[_channel close];
}
@end
#pragma mark - Keys and secrets
@implementation FaceclawSshKeys
+ (NSString *)authorizedKeyWithComment:(NSString *)comment {
SecKeyRef key = CopyOrCreateClientKey();
if (!key) return nil;
NSData *publicKey = PublicKeyData(key);
CFRelease(key);
NSError *error = nil;
NSString *line = FaceclawsshAuthorizedKey(publicKey, comment ?: @"", &error);
if (error) NSLog(@"FaceclawSsh: authorizedKey failed: %@", error);
return line;
}
+ (NSString *)fingerprint {
SecKeyRef key = CopyOrCreateClientKey();
if (!key) return nil;
NSData *publicKey = PublicKeyData(key);
CFRelease(key);
NSError *error = nil;
NSString *fingerprint = FaceclawsshFingerprint(publicKey, &error);
if (error) NSLog(@"FaceclawSsh: fingerprint failed: %@", error);
return fingerprint;
}
+ (NSDictionary *)secretQuery:(NSString *)secretId {
return @{
(id)kSecClass: (id)kSecClassGenericPassword,
(id)kSecAttrService: kSecretService,
(id)kSecAttrAccount: secretId ?: @"",
};
}
+ (void)setSecret:(NSString *)value forId:(NSString *)secretId {
NSDictionary *query = [self secretQuery:secretId];
SecItemDelete((__bridge CFDictionaryRef)query);
NSMutableDictionary *item = [query mutableCopy];
item[(id)kSecValueData] = [(value ?: @"") dataUsingEncoding:NSUTF8StringEncoding];
item[(id)kSecAttrAccessible] = (id)kSecAttrAccessibleAfterFirstUnlockThisDeviceOnly;
OSStatus status = SecItemAdd((__bridge CFDictionaryRef)item, NULL);
if (status != errSecSuccess) NSLog(@"FaceclawSsh: saving secret failed: %d", (int)status);
}
+ (NSString *)secretForId:(NSString *)secretId {
NSMutableDictionary *query = [[self secretQuery:secretId] mutableCopy];
query[(id)kSecReturnData] = @YES;
query[(id)kSecMatchLimit] = (id)kSecMatchLimitOne;
CFTypeRef result = NULL;
if (SecItemCopyMatching((__bridge CFDictionaryRef)query, &result) != errSecSuccess) return @"";
NSData *data = (__bridge_transfer NSData *)result;
return [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] ?: @"";
}
+ (void)removeSecretForId:(NSString *)secretId {
SecItemDelete((__bridge CFDictionaryRef)[self secretQuery:secretId]);
}
+ (void)copyToClipboard:(NSString *)text {
dispatch_async(dispatch_get_main_queue(), ^{
UIPasteboard.generalPasteboard.string = text ?: @"";
});
}
@end
-1
View File
@@ -2,7 +2,6 @@ module FaceclawGraphics {
header "FaceclawGraphics.h"
header "FaceclawFontRenderer.h"
header "FaceclawSocket.h"
header "FaceclawSsh.h"
header "FaceclawLc3Decoder.h"
header "FaceclawSpeech.h"
header "FaceclawLocation.h"
-21
View File
@@ -1,26 +1,5 @@
# Changelog
## 0.9.0 - 2026-11-08
### Major Features
* Add support for the Even Realities G1
* Add support for the Apple Watch
### Minor Features
* Show charging case battery for the G2
* Terminals now default to opening in the same window rather than opening a new window
* Better rendering of color emojis
* Inrease the max timeout for glanceboard show-on-tap display
* Fix some minor issues in the oboarding flow
* Add option to auto-close notification popups after a delay
### Bug Fixes
* Fix an issue that would cause microphone permission check to fail if you swiped away the Android Activity
* Fix an issue that broke the Nightscout widget on the status bar on iOS
* Fix an issue were EvenHub app texet containers weren't indented correctly
## 0.8.6 - 2026-11-01
* Connect directly to the R1 ring rather than indirectly via the glasses
+3 -6
View File
@@ -43,10 +43,7 @@ local checkout above this workspace at ../g2-kit-unofficial.
This Android app uses a modded firmware for the G2 smart glasses. Source code
to the corresponding firmware modifications lives in
https://github.com/jimrandomh/g2flash; there is a compiled version of the
firmware patch-set in app/g2/firmware/cfw-patches.ts, regenerated by
scripts/update_deltas.sh. Even G1 glasses run g1v0, a firmware from ScouterKit
(currently checked out at ~/src/scouter_kit, in g1_firmware/); Faceclaw embeds
it in app/g1/firmware/, regenerated by scripts/update_g1_firmware.sh VERSION.
Strongly prefer making changes to the Android app over making changes to the
firmware, as firmware development is considerably more fraught.
firmware patch-set in app/g2/firmware/cfw-patches.ts. Strongly prefer making
changes to the Android app over making changes to changes to the firmware, as
firmware development is considerably more fraught.
+1 -25
View File
@@ -5,8 +5,6 @@ collects and shares information when you use an iOS TestFlight build, even
when Faceclaw statistics are set to None. Information you voluntarily send
us, such as feedback, may also identify you. This policy describes these
separate channels and the third-party services you can choose to use.
On Android, Google ML Kit also collects SDK diagnostics separately from
Faceclaw statistics, as described below.
## Usage Statistics
@@ -18,8 +16,7 @@ your choice at any time in Settings > Privacy > Faceclaw statistics. Until
you choose, it is None. This setting controls only Faceclaw's own statistics.
With None, Faceclaw sends and stores none of its own usage statistics. This
does not disable Apple's TestFlight reports, Google ML Kit diagnostics, or
services you choose to use.
does not disable Apple's TestFlight reports or services you choose to use.
With Minimal, Faceclaw keeps counts on your phone and sends them about once a
day to stats.faceclaw.org, a server run by Faceclaw's developers:
@@ -51,27 +48,6 @@ phone, and stops all reports; turning them back on starts a new identifier.
The statistics server doesn't record the IP addresses reports come from.
## Android and Google ML Kit
Android translation and language identification use Google ML Kit. The
developer QR-code scanner uses Google's code scanner. Text and scanner
images are processed on-device, but the SDKs send Google diagnostic and
usage metrics over HTTPS: device/app details, installation identifiers,
timings, errors, feature events, input/output sizes, and detected or
configured languages. Translation also uses Firebase installation and
remote-configuration services. This reporting is separate from the
Faceclaw statistics setting; selecting None does not turn it off. Faceclaw
does not provide a switch to disable these SDK diagnostics.
These metrics do not include the text being translated or scanned images.
Cloud transcription and assistant providers are separate, as described below.
For Google's handling of SDK metrics, see:
https://developers.google.com/ml-kit/android-data-disclosure
https://developers.google.com/ml-kit/terms
https://policies.google.com/privacy
## iOS and TestFlight
For TestFlight builds, Apple automatically collects crash reports and beta
+3 -5
View File
@@ -39,7 +39,7 @@ The instructions below build the Android app.
If you want to customize faceclaw, the best way to do it is to download the
source code and using a coding agent such as Codex or Claude Code.
To compile, you will need an Android SDK environment with SDK version 36
To compile, you will need an Android SDK environment with SDK version 35
installed and licenses accepted, and the ANDROID_HOME pointing to the install.
You will need Nativescript installed, and a JDK environment (v21), with the
JAVA_HOME environment variable pointed at it. You will also need a reasonably
@@ -80,10 +80,8 @@ import it into the new version (see [config transfer](scripts/config-transfer.md
* **Integration with Android notifications**: shows the same notification
icons your phone does, and popups when notifications arrive.
* **Terminal mirroring.** Mirror terminal apps such as Claude Code or Codex
CLI with [g2mirror](https://github.com/jimrandomh/g2mirror), or attach to
tmux sessions over SSH (no server-side setup beyond tmux 3.2+), view them
on the glasses, and send them inputs with the voice assistant. g2mirror
can also be tunnelled over SSH instead of a VPN.
CLI with [g2mirror](https://github.com/jimrandomh/g2mirror), view them on
the glasses, and send them inputs with the voice assistant.
* **Media player controls** including playlist and media library navigation,
compatible with most Android media players.
* **Turn-by-turn directions** (requires a Mapbox API token).
+9 -8
View File
@@ -1,9 +1,10 @@
/**
* One-line pointer for iOS. The full TestFlight disclosure lives only in the
* PRIVACY file (shown in-app as Settings > Privacy > Privacy policy).
*/
export const IOS_PRIVACY_SUMMARY =
"With TestFlight, crash/usage reports continue even with None. See Privacy policy for details.";
/** Conditional wording: also shown on self-built iOS apps, where TestFlight does not apply. */
export const IOS_PRIVACY_NOTICE = [
"If installed with TestFlight, Apple automatically collects crash logs, installs, sessions, and device/app details and shares them with Faceclaw's developers. This continues even when Faceclaw statistics are None; the app cannot turn it off.",
"Joining only through a public link hides your name and email from us, not from Apple. Email invitations can identify you. Optional feedback can disclose your email, comments, screenshots and diagnostics. Review what you send.",
"iPhone Settings > Privacy & Security > Analytics & Improvements controls ordinary iOS analytics. It does not stop TestFlight crash reports. Faceclaw cannot change Apple's analytics settings.",
"To leave the beta, open Faceclaw in TestFlight and choose Stop Testing, then stop using and uninstall the beta. This does not erase earlier reports. Details: Settings > Privacy > Privacy policy.",
].join("\n\n");
export const ANDROID_PRIVACY_SUMMARY =
"Google ML Kit diagnostics for translation and QR scanning are separate from this setting. See Privacy policy for details.";
export const IOS_PRIVACY_SUMMARY =
"With TestFlight, crash/usage reports continue even with None. Open iOS / TestFlight privacy for details.";
+19 -3
View File
@@ -1,7 +1,6 @@
@import '@nativescript/theme/css/core.css';
@import '@nativescript/theme/css/default.css';
@import './phone-ui/remote-controls.css';
@import './phone-ui/settings-page.css';
@import './phone-ui/pairing.css';
Page {
@@ -18,8 +17,9 @@ ActionItem {
color: #ffffff;
}
/* The main page's custom titleView: title left; connection status and the
* warning triangle right-aligned beside the overflow menu. */
/* The main page's custom titleView: title left; the EvenHub app icon,
* connection status and the warning triangle right-aligned beside the
* overflow menu. */
.action-bar-title {
color: #ffffff;
font-size: 18;
@@ -34,6 +34,16 @@ ActionItem {
vertical-align: center;
}
/* The foreground EvenHub app's icon (opens its phone UI); the padding widens
* the tap target. */
.action-bar-app-icon {
width: 32;
height: 32;
padding: 5.5;
margin-right: 8;
vertical-align: center;
}
/* The emoji brings its own yellow; the padding widens the tap target. */
.action-bar-warning {
font-size: 18;
@@ -71,6 +81,12 @@ Button.-primary {
font-size: 18;
}
/* The logo art is black-on-transparent; tint it light in dark mode so it
* doesn't vanish into the dark page background. */
.ns-dark .onboarding-logo {
tint-color: #e6e6e6;
}
/* License / privacy-policy links in the onboarding disclaimer footer. The
* padding widens the tap target beyond the small text. */
.footer-link {
+5 -2
View File
@@ -1,12 +1,10 @@
@import '@nativescript/theme/css/core.css';
@import './phone-ui/remote-controls.css';
@import './phone-ui/settings-page.css';
@import './phone-ui/pairing.css';
.body-text { font-size: 18; }
.h1 { font-size: 28; font-weight: bold; }
.footer-link { color: #285c45; padding: 4 8; text-decoration: underline; }
.ns-dark .footer-link { color: #7bc47f; }
Page {
background-color: #f5f5f2;
@@ -37,6 +35,8 @@ Page.main-page {
.main-page .action-bar-title { font-size: 18; font-weight: bold; color: #ffffff; }
.main-page .action-bar-status { font-size: 14; color: #ffffff; margin-right: 12; }
.main-page .action-bar-warning { font-size: 18; padding: 4 6; }
/* Matches Android's 32x32 padded icon; the Image inside is 21x21. */
.main-page .action-bar-app-icon { width: 32; height: 32; margin-right: 8; }
.main-page .overflow-button {
background-color: transparent;
color: #ffffff;
@@ -46,6 +46,9 @@ Page.main-page {
margin: 0;
padding: 0;
}
/* Match Android's button layout boxes, including the inset around each tile. */
.main-page Button.tab-button { height: 48; margin-top: 6; margin-bottom: 6; }
.main-page .mode-button { background-color: #e2e2e2; color: #333333; border-radius: 6; }
.main-page .ble-bandwidth-indicator { color: #777777; font-size: 12; padding: 2 8; }
.main-page .system-name-modal {
+6 -16
View File
@@ -13,10 +13,6 @@ import { createInProcessWindow, type InProcessAppOptions, type InProcessWindow }
import { shell } from "../../ui/shell/shell";
import { VoiceDraft } from "./voice-draft";
/** The transcript's scrollbar, hugging the window's right padding, and the gap the text keeps from it. */
const SCROLLBAR_WIDTH = 2;
const SCROLLBAR_GAP = 11;
class AiChatLayer implements Layer {
readonly acceptsDirectional = true;
private firstLine: number | null = null; // null follows the streaming tail
@@ -42,30 +38,24 @@ class AiChatLayer implements Layer {
}
paint(ctx: LayerContext): GrayImage {
const size = ctx.stack.getBaseSize();
const { padding } = size;
const image = new GrayImage(size.width, size.height);
const font = getDefaultSmallFont();
const step = font.lineHeight + 3;
// The title, and the footer under the transcript, stay put inside the
// padding suggested for the window's edges; the transcript scrolls
// between them. Text runs from the left padding to the gutter kept for
// the scrollbar.
const left = padding.left;
const scrollbarX = size.width - padding.right - SCROLLBAR_WIDTH;
const width = Math.max(0, scrollbarX - SCROLLBAR_GAP - left);
const left = 12;
const width = size.width - 28;
const record = this.conversations.current();
if (this.selectedId !== record.id) { this.selectedId = record.id; this.firstLine = null; }
const transcript = record.session?.transcript ?? record.history?.transcript ?? [];
const title = this.conversations.title();
image.drawText(font, left, padding.top, truncateText(font, title, width), 180);
image.drawText(font, left, 6, truncateText(font, title, width), 180);
const label = assistantBackendSetting.get() === "external" ? "External agent" :
`${assistantModelLabel(record.model)} · ${record.reasoning === "default" ? "default reasoning" : record.reasoning}`;
const footer = wrapText(font, `${label} ${gestureHints([
[GESTURE_CLICK, "menu"], [GESTURE_DOUBLE_CLICK, "back"], [GESTURE_LONG_PRESS, "hold to talk"],
])}`, width);
const footerY = size.height - padding.bottom - font.lineHeight - step * (footer.length - 1);
const footerY = size.height - step * footer.length;
const footerTop = footerY - step * 3 - 12;
const historyTop = padding.top + step + 10;
const historyTop = step + 16;
const visible = Math.max(1, Math.floor((footerTop - historyTop - 8) / step));
const lines: { text: string; color: number }[] = [];
for (const entry of transcript) {
@@ -92,7 +82,7 @@ class AiChatLayer implements Layer {
if (this.maxFirstLine > 0) {
const track = visible * step;
const thumb = Math.max(8, Math.floor(track * visible / lines.length));
image.fillRect(scrollbarX, historyTop + Math.floor((track - thumb) * first / this.maxFirstLine), SCROLLBAR_WIDTH, thumb, 110);
image.fillRect(size.width - 5, historyTop + Math.floor((track - thumb) * first / this.maxFirstLine), 2, thumb, 110);
}
image.fillRect(left, footerTop, width, 1, 65);
const state = this.draft.active ? this.draft.status : record.session?.status || this.draft.status;
+1 -13
View File
@@ -30,13 +30,6 @@ export type AppDefinition = {
boot?: (ctx: AppContext) => void;
/** False for apps that must not appear in the launcher grid (the launcher itself). */
showInLauncher?: boolean;
/**
* False while the paired glasses can't run the app (absent means they can).
* It then leaves the launcher grid and what mirrors it: the watch remote and
* the apps.launch tool. A window restored from before a re-pair still opens,
* so the app needs to say what's wrong rather than assume it never runs.
*/
isSupported?: () => boolean;
/** Present on the app that handles text shared via the Android share intent. */
openSharedText?: (ctx: AppContext, title: string, text: string) => void;
/**
@@ -55,10 +48,8 @@ export type GlanceboardProvider = {
isEnabled: () => boolean;
/** Whether a single tap shows the board (for tapTimeoutMs). */
showOnTap: () => boolean;
/** How long a tap keeps the board up (and a head-tilt, when its head-down is unlikely to come). */
/** How long a tap or head-tilt keeps the board up. */
tapTimeoutMs: () => number;
/** How long a head-tilt showing may last without a head-down (null = no limit). */
tiltTimeoutMs: () => number | null;
/** Whether a long-press holds the board up until released. */
showOnLongPress: () => boolean;
/** Whether the head-tilt wake goes to the board rather than the regular UI. */
@@ -189,8 +180,6 @@ export async function launchWorkerAppWindow(
matchExistingBy?: "appId" | "windowId";
/** Deliver watch swipes raw instead of the fallback (see WorkerWindowSpec). */
acceptsDirectional?: boolean;
/** Deliver hardware-keyboard keys as keys (see WorkerWindowSpec). */
acceptsKeys?: boolean;
},
): Promise<void> {
const existing = shell
@@ -211,7 +200,6 @@ export async function launchWorkerAppWindow(
icon: options.icon,
focus: true,
acceptsDirectional: options.acceptsDirectional,
acceptsKeys: options.acceptsKeys,
});
ctx.appendLog(`launched ${options.windowId}`);
}
+2 -13
View File
@@ -25,7 +25,6 @@ import { loadSoundEnabled, saveSoundEnabled } from "../../ui/sound-setting";
import { type MenuItem } from "../../ui/menu";
import { WindowMenu } from "../../ui/window-menu";
import type { WorkerAppMessage, WorkerAppReply } from "../../ui/shell/worker-window";
import type { Viewport } from "../../ui/viewport";
import {
directionalFallback,
GESTURE_CLICK,
@@ -156,13 +155,6 @@ type BlocksWindow = {
title: string;
viewportWidth: number;
viewportHeight: number;
/**
* The viewport as the shell last sent it: besides the size above, what each
* edge touches and the padding suggested there. The window menu's layers
* lay out by it; the game's own layout is fixed art that keeps its place
* (see LAYOUT_HEIGHT).
*/
viewport: Viewport;
foreground: boolean;
/** Whether this window is the shell's input target (pushed with each message). */
focused: boolean;
@@ -215,7 +207,6 @@ global.onmessage = (event: { data: WorkerAppMessage }) => {
title: message.title,
viewportWidth: message.viewport.width,
viewportHeight: message.viewport.height,
viewport: message.viewport,
foreground: false,
focused: false,
menu: null,
@@ -304,15 +295,13 @@ global.onmessage = (event: { data: WorkerAppMessage }) => {
if (!window) break;
window.viewportWidth = message.viewport.width;
window.viewportHeight = message.viewport.height;
window.viewport = message.viewport;
window.menu?.resize(message.viewport);
window.menu?.resize({ width: window.viewportWidth, height: window.viewportHeight });
window.lastSubmittedFingerprint = "";
if (window.foreground) renderAndSubmit(window, 0);
break;
}
case "navigation-sensors":
case "text-input":
case "key":
case "tool-call":
break;
}
@@ -368,7 +357,7 @@ function windowMenu(window: BlocksWindow): WindowMenu {
title: () => window.title,
items: () => (window.phase === "playing" ? [] : windowMenuItems(window)),
claimsLongPress: () => window.phase === "playing",
size: window.viewport,
size: { width: window.viewportWidth, height: window.viewportHeight },
paintBase: () => paintContent(window),
isFocused: () => window.focused,
});
+5 -7
View File
@@ -35,27 +35,25 @@ export function drawGraph(
const clampX = (x: number): number => Math.max(rect.x, Math.min(rect.x + rect.width - 1, x));
const clampY = (y: number): number => Math.max(rect.y, Math.min(rect.y + rect.height - 1, y));
// Tick grid first so the curve and axes draw over it. A tick or axis at
// the viewport's far limit lands on the frame's last column or row, not
// past it: the caller may have run the rect to the window's edge.
// Tick grid first so the curve and axes draw over it.
for (const tick of graph.xAxis.ticks) {
if (tick < viewport.xMin || tick > viewport.xMax) continue;
const x = Math.round(clampX(px(tick)));
const x = Math.round(px(tick));
image.drawLine(x, rect.y, x, rect.y + rect.height - 1, GRID_VALUE);
}
for (const tick of graph.yAxis.ticks) {
if (tick < viewport.yMin || tick > viewport.yMax) continue;
const y = Math.round(clampY(py(tick)));
const y = Math.round(py(tick));
image.drawLine(rect.x, y, rect.x + rect.width - 1, y, GRID_VALUE);
}
// Axes.
if (viewport.yMin <= 0 && viewport.yMax >= 0) {
const y = Math.round(clampY(py(0)));
const y = Math.round(py(0));
image.drawLine(rect.x, y, rect.x + rect.width - 1, y, AXIS_VALUE);
}
if (viewport.xMin <= 0 && viewport.xMax >= 0) {
const x = Math.round(clampX(px(0)));
const x = Math.round(px(0));
image.drawLine(x, rect.y, x, rect.y + rect.height - 1, AXIS_VALUE);
}
+51 -76
View File
@@ -62,24 +62,6 @@ export const calculatorDegreesSetting = new ConfigSettingBoolean({
const CONTINUOUS_CLIENT = "calculator";
const DICTATION_HOLDER = "calculator-dictation";
// ---------------------------------------------------------------------------
// Layout
/**
* The column the view is laid out in: the window's width inside the padding
* suggested for its side edges (viewport.ts). Tabs, text, the plot and the
* footer all start at its left; centred text is centred in it.
*/
type Column = { x: number; width: number };
/** Below the tabs' text top: their underline, and where the body starts. */
const TAB_UNDERLINE_OFFSET = 20;
const BODY_TOP_OFFSET = 31;
/** Indent, on both sides, of wrapped body text (a failure's reason, a step's detail, an answer). */
const BODY_INDENT = 8;
/** The same for the prompt of an empty view, which keeps well clear of the sides. */
const PROMPT_INDENT = 24;
// ---------------------------------------------------------------------------
// Layer
@@ -398,17 +380,14 @@ export class CalculatorLayer implements Layer {
// Paint
paint(ctx: LayerContext): GrayImage {
const { width, height, padding } = ctx.stack.getBaseSize();
const { width, height } = ctx.stack.getBaseSize();
const image = new GrayImage(width, height, 0);
const small = getDefaultSmallFont();
const column: Column = { x: padding.left, width: Math.max(0, width - padding.left - padding.right) };
// The tabs sit at the top edge's padding and the footer at the bottom's;
// the body is what lies between.
this.paintTabs(image, small, column, padding.top);
this.paintTabs(image, small, width);
const footerY = height - padding.bottom - small.lineHeight;
const bodyTop = padding.top + BODY_TOP_OFFSET;
const footerY = height - small.lineHeight - 6;
const bodyTop = 46;
let bodyBottom = footerY - 4;
// A standing offer or a running deep pass takes the rows just above the
@@ -416,36 +395,35 @@ export class CalculatorLayer implements Layer {
const offer = this.coordinator.pendingLongRun;
const progress = this.coordinator.longRunProgress;
if (offer || progress) {
bodyBottom = this.paintLongRunPanel(image, small, column, bodyBottom, offer, progress);
bodyBottom = this.paintLongRunPanel(image, small, width, bodyBottom, offer, progress);
}
if (this.working) {
this.drawCentered(image, small, column, Math.round((bodyTop + bodyBottom) / 2) - 8, "Working…", 180);
this.drawCentered(image, small, Math.round((bodyTop + bodyBottom) / 2) - 8, "Working…", 180);
} else if (this.lastFailure !== null) {
this.paintFailure(image, small, column, bodyTop, bodyBottom);
this.paintFailure(image, small, width, bodyTop, bodyBottom);
} else if (this.mode === "explain" && this.coordinator.stepPosition !== null) {
this.paintExplain(image, column, bodyTop, bodyBottom);
this.paintExplain(image, width, bodyTop, bodyBottom);
} else if (this.mode === "graph" && this.coordinator.lastGraph) {
this.paintGraph(image, small, column, bodyTop, bodyBottom);
this.paintGraph(image, small, width, bodyTop, bodyBottom);
} else if (this.lastAnswer) {
this.paintAnswer(image, column, bodyTop, bodyBottom);
this.paintAnswer(image, width, bodyTop, bodyBottom);
} else {
this.paintEmpty(image, small, column, bodyTop, bodyBottom);
this.paintEmpty(image, small, width, bodyTop, bodyBottom);
}
image.drawText(small, column.x, footerY, this.footerHints(), 110);
image.drawText(small, 24, footerY, this.footerHints(), 110);
return image;
}
/** The three modes as tabs from `y` down, plus the deg/listening status chips. */
private paintTabs(image: GrayImage, font: UiFont, column: Column, y: number): void {
let x = column.x;
const underlineY = y + TAB_UNDERLINE_OFFSET;
/** The three modes as tabs, plus the deg/listening status chips. */
private paintTabs(image: GrayImage, font: UiFont, width: number): void {
let x = 24;
for (const mode of CALCULATOR_MODES) {
const label = modeLabel(mode);
const active = mode === this.mode;
image.drawText(font, x, y, label, active ? 245 : 105);
image.drawLine(x, underlineY, x + font.measureText(label), underlineY, active ? 200 : 30);
image.drawText(font, x, 15, label, active ? 245 : 105);
image.drawLine(x, 35, x + font.measureText(label), 35, active ? 200 : 30);
x += font.measureText(label) + 18;
}
@@ -455,28 +433,28 @@ export class CalculatorLayer implements Layer {
else if (this.continuousActive && this.foreground) chips.push("mic on");
if (chips.length > 0) {
const text = chips.join(" · ");
image.drawText(font, column.x + column.width - font.measureText(text), y, text, this.dictating ? 220 : 110);
image.drawText(font, width - 24 - font.measureText(text), 15, text, this.dictating ? 220 : 110);
}
}
private paintFailure(
image: GrayImage,
font: UiFont,
column: Column,
width: number,
bodyTop: number,
bodyBottom: number,
): void {
this.paintHeardLine(image, font, column, bodyTop);
this.paintHeardLine(image, font, width, bodyTop);
const top = this.lastInput ? bodyTop + lineStep(font) + 6 : bodyTop + 8;
const lines = wrapText(font, this.lastFailure ?? "", column.width - 2 * BODY_INDENT);
const lines = wrapText(font, this.lastFailure ?? "", width - 64);
const step = lineStep(font);
const maxLines = Math.max(1, Math.floor((bodyBottom - top) / step));
for (let index = 0; index < Math.min(lines.length, maxLines); index++) {
image.drawText(font, column.x + BODY_INDENT, top + index * step, lines[index]!, 200);
image.drawText(font, 32, top + index * step, lines[index]!, 200);
}
}
private paintExplain(image: GrayImage, column: Column, bodyTop: number, bodyBottom: number): void {
private paintExplain(image: GrayImage, width: number, bodyTop: number, bodyBottom: number): void {
const small = getDefaultSmallFont();
const medium = getDefaultMediumFont();
const position = this.coordinator.stepPosition;
@@ -485,65 +463,63 @@ export class CalculatorLayer implements Layer {
if (!position || !step) return;
let y = bodyTop + 2;
image.drawText(small, column.x, y, `Step ${position.index + 1} of ${position.total}`, 130);
image.drawText(small, 24, y, `Step ${position.index + 1} of ${position.total}`, 130);
y += lineStep(small) + 2;
image.drawText(medium, column.x, y, truncateText(medium, step.title, column.width), 245);
image.drawText(medium, 24, y, truncateText(medium, step.title, width - 48), 245);
y += lineStep(medium) + 2;
image.drawText(medium, column.x, y, truncateText(medium, plain(step.expression), column.width), 220);
image.drawText(medium, 24, y, truncateText(medium, plain(step.expression), width - 48), 220);
y += lineStep(medium) + 6;
const detailLines = wrapText(small, step.detail, column.width - 2 * BODY_INDENT);
const detailLines = wrapText(small, step.detail, width - 64);
const stepPitch = lineStep(small);
const maxLines = Math.max(0, Math.floor((bodyBottom - y) / stepPitch));
for (let index = 0; index < Math.min(detailLines.length, maxLines); index++) {
image.drawText(small, column.x + BODY_INDENT, y + index * stepPitch, detailLines[index]!, 150);
image.drawText(small, 32, y + index * stepPitch, detailLines[index]!, 150);
}
}
private paintGraph(
image: GrayImage,
font: UiFont,
column: Column,
width: number,
bodyTop: number,
bodyBottom: number,
): void {
const graph = this.coordinator.lastGraph;
if (!graph) return;
const headline = this.lastAnswer.split("\n")[0] ?? "";
image.drawText(font, column.x, bodyTop, truncateText(font, headline, column.width), 200);
// The plot's frame spans the column, so it meets the side padding itself.
image.drawText(font, 24, bodyTop, truncateText(font, headline, width - 48), 200);
const plotTop = bodyTop + lineStep(font) + 4;
drawGraph(image, graph, {
x: column.x,
x: 18,
y: plotTop,
width: column.width,
width: width - 36,
height: Math.max(2, bodyBottom - plotTop),
});
}
private paintAnswer(image: GrayImage, column: Column, bodyTop: number, bodyBottom: number): void {
private paintAnswer(image: GrayImage, width: number, bodyTop: number, bodyBottom: number): void {
const small = getDefaultSmallFont();
this.paintHeardLine(image, small, column, bodyTop);
this.paintHeardLine(image, small, width, bodyTop);
const top = this.lastInput ? bodyTop + lineStep(small) + 4 : bodyTop;
// A short single-line answer earns the large font; anything longer drops
// to the medium font and wraps.
const lines = this.lastAnswer.split("\n");
const large = getDefaultLargeFont();
const maxWidth = column.width - 2 * BODY_INDENT;
if (lines.length === 1 && large.measureText(lines[0]!) <= maxWidth) {
if (lines.length === 1 && large.measureText(lines[0]!) <= width - 64) {
const y = Math.round((top + bodyBottom - large.lineHeight) / 2);
this.drawCentered(image, large, column, y, lines[0]!, 245);
this.drawCentered(image, large, y, lines[0]!, 245);
return;
}
const medium = getDefaultMediumFont();
const wrapped = lines.flatMap((line) => wrapText(medium, line, maxWidth));
const wrapped = lines.flatMap((line) => wrapText(medium, line, width - 64));
const step = lineStep(medium);
const maxLines = Math.max(1, Math.floor((bodyBottom - top) / step));
const shown = wrapped.slice(0, maxLines);
let y = Math.max(top, Math.round((top + bodyBottom - shown.length * step) / 2));
for (const line of shown) {
this.drawCentered(image, medium, column, y, line, 235);
this.drawCentered(image, medium, y, line, 235);
y += step;
}
}
@@ -551,7 +527,7 @@ export class CalculatorLayer implements Layer {
private paintEmpty(
image: GrayImage,
font: UiFont,
column: Column,
width: number,
bodyTop: number,
bodyBottom: number,
): void {
@@ -560,32 +536,31 @@ export class CalculatorLayer implements Layer {
: this.listening === "continuous"
? `Say a problem, then "${modeLabel(this.mode).toLowerCase()} it"`
: "Tap to say a problem — or use Voice input to type one";
const lines = wrapText(font, prompt, column.width - 2 * PROMPT_INDENT);
const lines = wrapText(font, prompt, width - 96);
const step = lineStep(font);
let y = Math.round((bodyTop + bodyBottom - lines.length * step) / 2);
for (const line of lines) {
this.drawCentered(image, font, column, y, line, 150);
this.drawCentered(image, font, y, line, 150);
y += step;
}
}
private paintHeardLine(image: GrayImage, font: UiFont, column: Column, y: number): void {
private paintHeardLine(image: GrayImage, font: UiFont, width: number, y: number): void {
if (!this.lastInput) return;
const label = this.textInput ? "Entered" : "Heard";
image.drawText(font, column.x, y, label, 110);
const x = column.x + font.measureText(label) + 10;
image.drawText(font, x, y, truncateText(font, this.lastInput, column.x + column.width - x), 160);
image.drawText(font, 24, y, label, 110);
const x = 24 + font.measureText(label) + 10;
image.drawText(font, x, y, truncateText(font, this.lastInput, width - x - 24), 160);
}
/**
* The standing offer, or the live progress of an accepted run, in the two
* rows above the footer, under a rule across the column. Returns the new
* body bottom.
* rows above the footer. Returns the new body bottom.
*/
private paintLongRunPanel(
image: GrayImage,
font: UiFont,
column: Column,
width: number,
bodyBottom: number,
offer: LongRunOffer | null,
progress: LongRunProgress | null,
@@ -597,9 +572,9 @@ export class CalculatorLayer implements Layer {
? offerGlassesPrompt(offer).split("\n")
: [];
const panelTop = bodyBottom - lines.length * step - 6;
if (column.width > 0) image.drawLine(column.x, panelTop, column.x + column.width - 1, panelTop, 40);
image.drawLine(16, panelTop, width - 16, panelTop, 40);
for (let index = 0; index < lines.length; index++) {
image.drawText(font, column.x, panelTop + 4 + index * step, truncateText(font, lines[index]!, column.width), 190);
image.drawText(font, 24, panelTop + 4 + index * step, truncateText(font, lines[index]!, width - 48), 190);
}
return panelTop - 2;
}
@@ -629,8 +604,8 @@ export class CalculatorLayer implements Layer {
]);
}
private drawCentered(image: GrayImage, font: UiFont, column: Column, y: number, text: string, value: number): void {
const x = column.x + Math.max(0, Math.round((column.width - font.measureText(text)) / 2));
private drawCentered(image: GrayImage, font: UiFont, y: number, text: string, value: number): void {
const x = Math.max(0, Math.round((image.width - font.measureText(text)) / 2));
image.drawText(font, x, y, text, value);
}
}
+33 -77
View File
@@ -8,26 +8,14 @@ import { GESTURE_CLICK, type InputEvent } from "../../ui/gestures";
import { hasCalendarPermission } from "../../native/calendar-permissions";
import { type Layer, type LayerContext } from "../../ui/layers";
import { lineStep } from "../../ui/metrics";
import type { Viewport } from "../../ui/viewport";
import { listingDayMs, spanLabel, spansDays } from "./event-days";
/** How far a row's text sits inside its selection box; the title lines up with it. */
const ROW_TEXT_INSET = 10;
/**
* The title's drop below the window's top padding (the position shared with
* the other list apps: terminal, notifications), and the distance from its
* top to the first row.
*/
const TITLE_OFFSET_Y = 2;
const TITLE_BAND = 28;
// Title position, shared with the other list apps (terminal, notifications).
const TITLE_X = 18;
const TITLE_Y = 10;
const LIST_TOP = 38;
const ROW_X = 16;
const ROW_GAP = 4;
const MAX_EVENTS = 50;
/**
* The message shown in place of the list (no permission, nothing to show):
* its indent from the title, and its drop below the window's top padding.
*/
const MESSAGE_INDENT = 6;
const MESSAGE_OFFSET_Y = 64;
/** Height of the day header band above a row that starts a new day. */
function dayHeaderHeight(font: UiFont): number {
@@ -58,25 +46,20 @@ export class CalendarLayer implements Layer {
paint(ctx: LayerContext): GrayImage {
const font = getDefaultSmallFont();
const viewport = ctx.stack.getBaseSize();
const { width, height, padding } = viewport;
const { width, height } = ctx.stack.getBaseSize();
const image = new GrayImage(width, height, 0);
// Rows' selection boxes sit at the padding suggested for the window's
// side edges; the title lines up with their text.
const titleX = padding.left + ROW_TEXT_INSET;
const titleY = padding.top + TITLE_OFFSET_Y;
if (!hasCalendarPermission()) {
image.drawText(font, titleX, titleY, "Calendar", 220);
this.paintPermissionPrompt(image, font, viewport);
image.drawText(font, TITLE_X, TITLE_Y, "Calendar", 220);
this.paintPermissionPrompt(image, font, width, height);
return image;
}
const events = readUpcomingEvents(MAX_EVENTS);
if (!events.length) {
image.drawText(font, titleX, titleY, "Calendar", 220);
image.drawText(font, TITLE_X, TITLE_Y, "Calendar", 220);
const state = getCalendarReadState();
image.drawText(font, titleX + MESSAGE_INDENT, padding.top + MESSAGE_OFFSET_Y, state === "loading" ? "Loading calendar..."
image.drawText(font, 24, 72, state === "loading" ? "Loading calendar..."
: state === "error" ? "Calendar unavailable. Try again shortly." : "No upcoming events.", 190);
return image;
}
@@ -84,26 +67,22 @@ export class CalendarLayer implements Layer {
const rows = buildEventRows(font, events);
this.selectedIndex = clamp(this.selectedIndex, 0, rows.length - 1);
// The agenda scrolls, so it runs to the window's top and bottom edges
// and rows scrolled past an end are cut off right at the edge. The
// padding suggested for those edges is inside the scrolled content
// instead: above the title and below the last row.
const listTop = titleY + TITLE_BAND;
const scrollY = scrollForSelected(rows, this.selectedIndex, height - listTop, padding.bottom);
const listBottom = height;
const scrollY = scrollForSelected(rows, this.selectedIndex, listBottom - LIST_TOP);
// The title scrolls away with the list; row text is deferred glyphs
// (composited above raster fills), so a fixed title would show through
// rows scrolled over it.
if (titleY - scrollY + font.lineHeight > 0) {
image.drawText(font, titleX, titleY - scrollY, "Calendar", 220);
if (TITLE_Y - scrollY + font.lineHeight > 0) {
image.drawText(font, TITLE_X, TITLE_Y - scrollY, "Calendar", 220);
}
const rowWidth = width - padding.left - padding.right;
let cursorY = listTop - scrollY;
for (let index = 0; index < rows.length && cursorY < height; index++) {
let cursorY = LIST_TOP - scrollY;
for (let index = 0; index < rows.length; index++) {
const row = rows[index]!;
if (cursorY + row.height > 0) {
drawEventRow(image, font, row, padding.left, cursorY, rowWidth, index === this.selectedIndex);
if (cursorY + row.height >= LIST_TOP && cursorY <= listBottom) {
drawEventRow(image, font, row, ROW_X, cursorY, width, index === this.selectedIndex);
}
cursorY += row.height + ROW_GAP;
if (cursorY > listBottom + 80) break;
}
return image;
}
@@ -123,41 +102,27 @@ export class CalendarLayer implements Layer {
}
}
private paintPermissionPrompt(image: GrayImage, font: UiFont, viewport: Viewport): void {
const { width, height, padding } = viewport;
private paintPermissionPrompt(image: GrayImage, font: UiFont, width: number, height: number): void {
const message = "Grant calendar permission on your phone to see your events on the glasses.";
// Indented under the title, and as far short of the right padding.
const indent = ROW_TEXT_INSET + MESSAGE_INDENT;
const x = padding.left + indent;
const lines = wrapText(font, message, width - padding.left - padding.right - 2 * indent);
// The hint rests on the bottom padding; the message keeps to the lines that fit above it.
const hintY = height - padding.bottom - font.lineHeight;
const top = padding.top + MESSAGE_OFFSET_Y;
const maxLines = Math.max(1, Math.floor((hintY - top) / lineStep(font)));
for (let index = 0; index < Math.min(lines.length, maxLines); index++) {
image.drawText(font, x, top + index * lineStep(font), lines[index]!, 190);
const lines = wrapText(font, message, width - 48);
for (let index = 0; index < lines.length; index++) {
image.drawText(font, 24, 72 + index * lineStep(font), lines[index]!, 190);
}
image.drawText(font, x, hintY, `${GESTURE_CLICK} request`, 110);
image.drawText(font, 24, height - 36, `${GESTURE_CLICK} request`, 110);
}
}
function buildEventRows(font: UiFont, events: CalendarEvent[]): EventRow[] {
const rows: EventRow[] = [];
let previousDayKey = "";
const nowMs = Date.now();
for (const event of events) {
// An event still going since an earlier day is listed under today, not its first day.
const listedMs = listingDayMs(event, nowMs);
const dayKey = dayKeyOf(listedMs);
const dayHeader = dayKey === previousDayKey ? null : dayHeaderLabel(listedMs);
const dayKey = dayKeyOf(event.startMs);
const dayHeader = dayKey === previousDayKey ? null : dayHeaderLabel(event.startMs);
previousDayKey = dayKey;
const lines: string[] = [];
const timeLabel = event.allDay ? "All day" : formatEventTime(event.startMs);
lines.push(`${timeLabel} ${event.title || "(untitled)"}`);
if (spansDays(event)) {
lines.push(spanLabel(event));
}
if (event.location) {
lines.push(event.location);
}
@@ -172,7 +137,6 @@ function buildEventRows(font: UiFont, events: CalendarEvent[]): EventRow[] {
return rows;
}
/** Draw a row whose selection box spans `width` from `x`; its day header and text sit ROW_TEXT_INSET inside that. */
function drawEventRow(
image: GrayImage,
font: UiFont,
@@ -182,38 +146,30 @@ function drawEventRow(
width: number,
selected: boolean,
): void {
const textX = x + ROW_TEXT_INSET;
let cursorY = y;
if (row.dayHeader) {
image.drawText(font, textX, cursorY + 2, row.dayHeader, 150);
image.drawText(font, x, cursorY + 2, row.dayHeader, 150);
cursorY += dayHeaderHeight(font);
}
const bodyHeight = row.lines.length * lineStep(font) + 4;
if (selected) {
image.fillRoundedRect(x, cursorY, width, bodyHeight, 15, 6);
image.drawRoundedRect(x, cursorY, width, bodyHeight, 90, 6);
image.fillRoundedRect(x - 6, cursorY, width - 2 * (x - 6), bodyHeight, 15, 6);
image.drawRoundedRect(x - 6, cursorY, width - 2 * (x - 6), bodyHeight, 90, 6);
}
const maxTextWidth = width - 2 * ROW_TEXT_INSET;
const maxTextWidth = width - 2 * x;
for (let index = 0; index < row.lines.length; index++) {
const value = index === 0 ? (selected ? 235 : 205) : 160;
image.drawText(font, textX, cursorY + 3 + index * lineStep(font), truncateText(font, row.lines[index]!, maxTextWidth), value);
image.drawText(font, x, cursorY + 3 + index * lineStep(font), truncateText(font, row.lines[index]!, maxTextWidth), value);
}
}
/**
* The scroll offset that centers the selected row in `viewportHeight`, the
* room below the title's resting place, within the ends of what scrolls
* there: the rows, then `padBottom` after the last of them. So the first
* row rests with the title and the padding above it in view, and the last
* with the padding below it.
*/
function scrollForSelected(rows: EventRow[], selectedIndex: number, viewportHeight: number, padBottom: number): number {
function scrollForSelected(rows: EventRow[], selectedIndex: number, viewportHeight: number): number {
let selectedTop = 0;
for (let index = 0; index < selectedIndex; index++) {
selectedTop += rows[index]!.height + ROW_GAP;
}
const selectedBottom = selectedTop + rows[selectedIndex]!.height;
const contentHeight = rows.reduce((sum, row) => sum + row.height + ROW_GAP, -ROW_GAP) + padBottom;
const contentHeight = rows.reduce((sum, row) => sum + row.height + ROW_GAP, 0);
const maxScroll = Math.max(0, contentHeight - viewportHeight);
const centered = selectedTop - Math.max(0, (viewportHeight - (selectedBottom - selectedTop)) / 2);
return clamp(centered | 0, 0, maxScroll);
-43
View File
@@ -1,43 +0,0 @@
/**
* Which day an event is listed under, and how a multi-day span reads. Pure (no
* native or UI imports) so it can be unit-tested.
*/
const WEEKDAYS = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"];
const MONTHS = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
export type DayEvent = { startMs: number; endMs: number; allDay: boolean };
function midnight(timestampMs: number): number {
const date = new Date(timestampMs);
return new Date(date.getFullYear(), date.getMonth(), date.getDate()).getTime();
}
/** The last moment the event covers: all-day ends are exclusive (the midnight after its last day). */
function lastMomentMs(event: DayEvent): number {
return event.allDay ? Math.max(event.startMs, event.endMs - 1) : Math.max(event.startMs, event.endMs);
}
/**
* The day to list an event under: its start, or today for one that began on an
* earlier day and is still going. Without this, the third day of a three-day
* event shows under its first day and never under today.
*/
export function listingDayMs(event: DayEvent, nowMs: number): number {
return Math.max(midnight(event.startMs), Math.min(midnight(nowMs), midnight(lastMomentMs(event))));
}
/** Whether the event covers more than one calendar day. */
export function spansDays(event: DayEvent): boolean {
return midnight(lastMomentMs(event)) > midnight(event.startMs);
}
/** "Mon 16 – Wed 18 Nov", or with both months when they differ ("Mon 30 Nov – Tue 1 Dec"). */
export function spanLabel(event: DayEvent): string {
const start = new Date(event.startMs);
const last = new Date(lastMomentMs(event));
const day = (date: Date) => `${WEEKDAYS[date.getDay()]} ${date.getDate()}`;
return start.getMonth() === last.getMonth() && start.getFullYear() === last.getFullYear()
? `${day(start)} – ${day(last)} ${MONTHS[last.getMonth()]}`
: `${day(start)} ${MONTHS[start.getMonth()]} – ${day(last)} ${MONTHS[last.getMonth()]}`;
}
+10 -9
View File
@@ -25,6 +25,10 @@ const INSTRUCTIONS_BY_FRAME: Record<NorthReference, string> = {
magnetic: "magnetic compass heading (as read by another compass), then adjust the calibration offset below",
};
/** Margins around the instruction paragraph. */
const TEXT_PADDING_X = 16;
const TEXT_PADDING_TOP = 10;
export class CompassCalibrationLayer implements Layer {
constructor(
/** Current uncorrected magnetometer heading, or null before the first reading. */
@@ -32,7 +36,7 @@ export class CompassCalibrationLayer implements Layer {
) {}
paint(ctx: LayerContext): GrayImage {
const { width, height, padding } = ctx.stack.getBaseSize();
const { width, height } = ctx.stack.getBaseSize();
const image = new GrayImage(width, height, 0);
const small = getDefaultSmallFont();
const medium = getDefaultMediumFont();
@@ -43,17 +47,14 @@ export class CompassCalibrationLayer implements Layer {
// Before the first reading there is no frame to name yet; the true-mode
// wording is the default and the readout will correct it once data arrives.
const frame = resolved?.frame ?? "true";
// The instructions fill the width inside the padding suggested for the
// window's edges, from the top's down.
const lines = wrapText(small, INSTRUCTIONS_PREAMBLE + INSTRUCTIONS_BY_FRAME[frame], Math.max(0, width - padding.left - padding.right));
const lines = wrapText(small, INSTRUCTIONS_PREAMBLE + INSTRUCTIONS_BY_FRAME[frame], width - TEXT_PADDING_X * 2);
for (let i = 0; i < lines.length; i++) {
image.drawText(small, padding.left, padding.top + i * smallStep, lines[i]!, 190);
image.drawText(small, TEXT_PADDING_X, TEXT_PADDING_TOP + i * smallStep, lines[i]!, 190);
}
const textBottom = padding.top + lines.length * smallStep;
const textBottom = TEXT_PADDING_TOP + lines.length * smallStep;
// Readouts along the bottom, above its padding: dim label over a bright
// value, in equal columns.
const valueTop = height - padding.bottom - medium.lineHeight;
// Readouts along the bottom: dim label over a bright value, in thirds.
const valueTop = height - medium.lineHeight;
const labelTop = valueTop - small.lineHeight;
const columns: Array<[string, string]> = [
["Raw", raw === null ? "--°" : `${Math.round(raw)}°`],
+24 -25
View File
@@ -10,7 +10,6 @@ import {
type CompassDiagnostics,
} from "../../native/compass";
import { wrapText } from "../../graphics/textwrap";
import { glassesHaveCompass } from "../../ui/dashboard-settings";
import { type InputEvent } from "../../ui/gestures";
import { type Layer, type LayerContext } from "../../ui/layers";
import { lineStep } from "../../ui/metrics";
@@ -41,9 +40,18 @@ export const COMPASS_SURFACE_ID = "window:compass";
const RECONCILE_INTERVAL_MS = 400;
/** Vertical breathing room between the readout, the status line and the rose. */
const STACK_GAP = 8;
/** Padding above the readout and below the rose's near edge. */
const TOP_PAD = 2;
const BOTTOM_PAD = 4;
/** Clearance between the rose's widest point and the viewport edge. */
const EDGE_PAD = 8;
/** Inset of the top-right magnetometer calibration readout from the window edges. */
const ACCURACY_TOP_PAD = TOP_PAD + 4;
const ACCURACY_RIGHT_PAD = EDGE_PAD + 4;
/** Cap on the rose's radius, so a tall window doesn't get a comical one. */
const MAX_ROSE_RADIUS = 98;
class CompassLayer implements Layer {
private rawHeading: number | null = null;
private diagnostics: CompassDiagnostics | null = null;
@@ -143,7 +151,6 @@ class CompassLayer implements Layer {
}
private statusText(): string {
if (!glassesHaveCompass()) return "These glasses have no compass sensor";
if (this.firmwareStatus !== null) return this.firmwareStatus;
if (!this.enabled) return "Compass paused";
if (this.rawHeading === null) return "Waiting for compass data…";
@@ -152,41 +159,35 @@ class CompassLayer implements Layer {
}
paint(ctx: LayerContext): GrayImage {
const { width, height, padding } = ctx.stack.getBaseSize();
const { width, height } = ctx.stack.getBaseSize();
const small = getDefaultSmallFont();
const large = getDefaultLargeFont();
const heading = this.rawHeading === null ? null : resolveHeading(this.rawHeading).displayDegrees;
const headingText = heading === null ? "--°" : `${Math.round(heading)}° ${cardinalDirection(heading)}`;
// Text and the rose keep inside the padding suggested for the window's edges.
const textWidth = Math.max(0, width - padding.left - padding.right);
const statusLines = wrapText(small, this.statusText(), textWidth);
const statusLines = wrapText(small, this.statusText(), width - STACK_GAP * 2);
const smallStep = lineStep(small);
const headingFont = large.measureText(headingText) <= textWidth ? large : small;
const headingFont = large.measureText(headingText) <= width - EDGE_PAD * 2 ? large : small;
const readoutWidth = headingFont.measureText(headingText);
const readoutHeight = headingFont.lineHeight;
// One column centred on the display's true centre, so the rose sits where
// the wearer is looking rather than 32px beside it (with a side strip).
const cx = screenCenterInViewportX();
// A line of that column: centred there, short of leaving the padded width.
const lineX = (lineWidth: number) =>
Math.round(Math.max(padding.left, Math.min(width - padding.right - lineWidth, cx - lineWidth / 2)));
// The rose is sized and placed first — it hangs off the bottom edge — and
// the readout then floats in whatever room is left above it. Its far
// edges land on the last row and column inside the padding.
// the readout then floats in whatever room is left above it.
const textHeight = readoutHeight + 4 + statusLines.length * smallStep;
const { cy, radius, ringTop } = layoutCompassRose({
width,
height,
cx,
topClearance: padding.top + textHeight + STACK_GAP,
bottomPad: padding.bottom + 1,
edgePad: { left: padding.left, right: padding.right + 1 },
topClearance: TOP_PAD + textHeight + STACK_GAP,
bottomPad: BOTTOM_PAD,
edgePad: EDGE_PAD,
maxRadius: MAX_ROSE_RADIUS,
});
let y = Math.max(padding.top, Math.round((ringTop - TICK_HEIGHT - textHeight) / 2));
let y = Math.max(TOP_PAD, Math.round((ringTop - TICK_HEIGHT - textHeight) / 2));
const fade = heading === null ? 0.45 : 1;
const clipY = y + textHeight + 6;
const backgroundKey = [width, height, cx, cy, radius, clipY, fade].join(",");
@@ -198,17 +199,15 @@ class CompassLayer implements Layer {
}
const image = this.background.image.clone();
drawCompassRose(image, cx, cy, radius, heading);
image.drawText(headingFont, lineX(readoutWidth), y, headingText, heading === null ? 150 : 255);
const readoutX = Math.round(Math.max(EDGE_PAD, Math.min(width - EDGE_PAD - readoutWidth, cx - readoutWidth / 2)));
image.drawText(headingFont, readoutX, y, headingText, heading === null ? 150 : 255);
y += readoutHeight + 4;
for (const line of statusLines) {
image.drawText(small, lineX(small.measureText(line)), y, line, 125);
image.drawText(small, Math.round(cx - small.measureText(line) / 2), y, line, 125);
y += smallStep;
}
if (glassesHaveCompass()) {
// The magnetometer calibration readout sits in the window's top-right corner.
const accuracyText = magneticAccuracyText(this.diagnostics);
image.drawText(small, width - padding.right - small.measureText(accuracyText), padding.top, accuracyText, 175);
}
const accuracyText = magneticAccuracyText(this.diagnostics);
image.drawText(small, width - ACCURACY_RIGHT_PAD - small.measureText(accuracyText), ACCURACY_TOP_PAD, accuracyText, 175);
return image;
}
@@ -217,7 +216,7 @@ class CompassLayer implements Layer {
// "Uncalibrated" prompt advertises, and re-calibrating is a normal thing to
// want once the glasses have been taken off and put back on.
handleInput(event: InputEvent, ctx: LayerContext): void {
if (event.type === "click" && glassesHaveCompass()) this.openCalibration(ctx);
if (event.type === "click") this.openCalibration(ctx);
}
}
@@ -232,7 +231,7 @@ export function createCompassAppWindow(options: InProcessAppOptions): InProcessW
icon: "compass",
closeable: true,
actions: options.actions,
menuItems: () => !glassesHaveCompass() ? [] : [
menuItems: () => [
{
label: "Calibrate",
onSelect: (ctx) => {
+2 -4
View File
@@ -78,19 +78,17 @@ export function layoutCompassRose(options: {
/** Lowest y already used above the rose (readout text, its gap). */
topClearance: number;
bottomPad: number;
/** Clearance between the rose's widest point and the box's sides: one figure for both, or each side's. */
edgePad: number | { left: number; right: number };
edgePad: number;
maxRadius: number;
minRadius?: number;
}): { cx: number; cy: number; radius: number; ringTop: number } {
const { width, height, cx } = options;
const sidePad = typeof options.edgePad === "number" ? { left: options.edgePad, right: options.edgePad } : options.edgePad;
const radius = Math.max(
options.minRadius ?? 24,
Math.min(
options.maxRadius,
Math.floor((height - options.bottomPad - options.topClearance - TICK_HEIGHT) / (RING_HEIGHT_PER_RADIUS + DISC_DEPTH)),
Math.floor(Math.min(cx - sidePad.left, width - cx - sidePad.right) / RING_HALF_WIDTH_PER_RADIUS),
Math.floor((Math.min(cx, width - cx) - options.edgePad) / RING_HALF_WIDTH_PER_RADIUS),
),
);
// The wall hangs below the rim, so the near rim sits a wall's depth up
-2
View File
@@ -1,4 +1,3 @@
import { glassesHaveCompass } from "../../ui/dashboard-settings";
import { type AppDefinition } from "../app-definition";
import { COMPASS_SURFACE_ID, COMPASS_WINDOW_ID, createCompassAppWindow } from "./compass-app";
@@ -6,7 +5,6 @@ const compassApp: AppDefinition = {
appId: "compass",
title: "Compass",
icon: "compass",
isSupported: glassesHaveCompass,
launch: (ctx) => ctx.launchInProcessApp(COMPASS_WINDOW_ID, COMPASS_SURFACE_ID, createCompassAppWindow),
};
+15 -28
View File
@@ -1,7 +1,7 @@
import { getDefaultMediumFont, getDefaultSmallFont } from "../../graphics/ui-fonts";
import { GrayImage } from "../../graphics/image";
import { clamp } from "../../util/numeric-util";
import { addImuListener, imuPitchDegrees, imuSourceLabel, setImuReportEnabled, type ImuReading } from "../../native/imu";
import { addImuListener, imuSourceLabel, setImuReportEnabled, type ImuReading } from "../../native/imu";
import { type InputEvent } from "../../ui/gestures";
import { Layer, type LayerContext } from "../../ui/layers";
import { shell } from "../../ui/shell/shell";
@@ -13,18 +13,11 @@ import { shell } from "../../ui/shell/shell";
const IMU_REPORT_PACE = 200;
// How often to reconcile the IMU on/off state with window visibility.
const RECONCILE_INTERVAL_MS = 400;
/** The readout under the title sits this far in from the title's left edge. */
const BODY_INDENT = 2;
/** An axis row, from the readout's left edge: its value, then its bar, which stops this short of the right edge's padding. */
const VALUE_OFFSET_X = 22;
const BAR_OFFSET_X = 96;
const BAR_RIGHT_INSET = 4;
/**
* Accelerometer demo: turns the IMU stream on while this page is visible (the
* Developer window is foregrounded and the screen is on), off otherwise, and
* shows the latest x/y/z reading (Faceclaw's frame, imu-types.ts) with a
* simple per-axis bar, and the head's pitch.
* shows the latest x/y/z reading with a simple per-axis bar.
*
* The IMU stream is experimental — the firmware may never populate it — so the
* page also reports how many samples have arrived.
@@ -84,18 +77,12 @@ export class AccelerometerDemoLayer implements Layer {
paint(ctx: LayerContext): GrayImage {
const small = getDefaultSmallFont();
const medium = getDefaultMediumFont();
const { width, height, padding } = ctx.stack.getBaseSize();
const { width, height } = ctx.stack.getBaseSize();
const image = new GrayImage(width, height, 0);
// The title line sits at the padding suggested for the top, left and
// right edges; everything under it is placed from there.
const top = padding.top;
const right = width - padding.right;
const bodyX = padding.left + BODY_INDENT;
image.drawText(small, padding.left, top, "Accelerometer", 220);
const pitch = this.reading ? `pitch ${Math.round(imuPitchDegrees(this.reading))}° ` : "";
const status = pitch + (this.enabled ? "IMU on" : "IMU off");
image.drawText(small, right - small.measureText(status), top, status, 150);
image.drawText(small, 20, 8, "Accelerometer", 220);
const status = this.enabled ? "IMU on" : "IMU off";
image.drawText(small, width - 20 - small.measureText(status), 8, status, 150);
if (this.reading) {
const axes: Array<[string, number]> = [
@@ -103,19 +90,19 @@ export class AccelerometerDemoLayer implements Layer {
["Y", this.reading.y],
["Z", this.reading.z],
];
const barX = bodyX + BAR_OFFSET_X;
const barW = Math.max(0, right - BAR_RIGHT_INSET - barX);
const barX = 118;
const barW = width - barX - 24;
const scale = Math.max(this.peak, 1e-6);
let y = top + 32;
let y = 40;
for (const [label, value] of axes) {
image.drawText(medium, bodyX, y, label, 210);
image.drawText(medium, bodyX + VALUE_OFFSET_X, y, value.toFixed(3), 235);
image.drawText(medium, 22, y, label, 210);
image.drawText(medium, 44, y, value.toFixed(3), 235);
drawCenteredBar(image, barX, y + 2, barW, 12, clamp(value / scale, -1, 1));
y += 30;
}
image.drawText(
small,
bodyX,
22,
y + 6,
`source: ${imuSourceLabel(this.reading.source)} samples: ${this.sampleCount}`,
130,
@@ -124,9 +111,9 @@ export class AccelerometerDemoLayer implements Layer {
const message = this.enabled
? "Waiting for IMU data…"
: "Paused (page not visible).";
image.drawText(small, bodyX, top + 36, message, 190);
image.drawText(small, bodyX, top + 58, "The firmware may not report the", 120);
image.drawText(small, bodyX, top + 74, "accelerometer at all — experimental.", 120);
image.drawText(small, 22, 44, message, 190);
image.drawText(small, 22, 66, "The firmware may not report the", 120);
image.drawText(small, 22, 82, "accelerometer at all — experimental.", 120);
}
return image;
+16 -27
View File
@@ -27,12 +27,9 @@ const POLL_INTERVAL_MS = 500;
// timeout drain + slack).
const POLL_DEADLINE_MS = DURATION_MS + 15_000;
/** The title band: the rows and the run's text start this far below the top edge's padding. */
const HEADER_HEIGHT = 22;
/** Horizontal inset of a row's text from its highlight box; the title and the text under the rows line up with it. */
const ROW_TEXT_INSET = 6;
/** A row's value column, from its label's left edge. */
const VALUE_OFFSET_X = 130;
const HEADER_HEIGHT = 30;
const LIST_X = 20;
const VALUE_X = 150;
type Phase = "config" | "arming" | "running" | "done";
type ConfigRow = "size" | "window" | "link" | "start";
@@ -129,43 +126,36 @@ export class BandwidthBenchmarkLayer implements Layer {
paint(ctx: LayerContext): GrayImage {
const font = getDefaultSmallFont();
const { width, height, padding } = ctx.stack.getBaseSize();
const { width, height } = ctx.stack.getBaseSize();
const image = new GrayImage(width, height, 0);
// The rows' highlight boxes span the window inside the padding suggested
// for its side edges, and the title sits at the top edge's; the page's
// text shares the rows' text column.
const textX = padding.left + ROW_TEXT_INSET;
const bodyTop = padding.top + HEADER_HEIGHT;
image.drawText(font, textX, padding.top, "BLE bandwidth", 220);
image.drawText(font, 20, 8, "BLE bandwidth", 220);
if (this.phase === "arming" || this.phase === "running") {
image.drawText(font, textX, bodyTop + 8, "Running: streaming no-op packets for 15 s...", 200);
image.drawText(font, LIST_X, HEADER_HEIGHT + 8, "Running: streaming no-op packets for 15 s...", 200);
image.drawText(
font,
textX,
bodyTop + 30,
LIST_X,
HEADER_HEIGHT + 30,
`${MESSAGE_SIZES[this.sizeIndex]} B, window ${WINDOW_SIZES[this.windowIndex]}`
+ (LINK_REQUESTS_SUPPORTED ? `, ${LINK_MODES[this.linkModeIndex]}` : ""),
150,
);
image.drawText(font, textX, bodyTop + 52, "Display updates pause until the run finishes.", 110);
image.drawText(font, LIST_X, HEADER_HEIGHT + 52, "Display updates pause until the run finishes.", 110);
return image;
}
// The rows all fit their box, so it is a fixed block under the title
// rather than a scroller that would run to the window's bottom edge.
const rowH = tightRowHeight(font) + 4;
const rowCount = this.menu.items.length;
this.menu.paint(
image,
{ x: padding.left, y: bodyTop - 1, width: Math.max(0, width - padding.left - padding.right), height: rowCount * rowH },
{ x: LIST_X - 6, y: HEADER_HEIGHT - 1, width: width - 2 * LIST_X + 12, height: rowCount * rowH },
ctx.stack.isFocused(),
);
let y = bodyTop + rowCount * rowH + 10;
let y = HEADER_HEIGHT + rowCount * rowH + 10;
if (this.error) {
image.drawText(font, textX, y, this.error, 180);
image.drawText(font, LIST_X, y, this.error, 180);
} else if (this.result) {
const r = this.result;
const seconds = r.elapsedMs / 1000;
@@ -181,9 +171,7 @@ export class BandwidthBenchmarkLayer implements Layer {
lines.push(`Link request: ${LINK_MODES[r.linkMode] ?? "Current"}${phy}`);
}
for (const line of lines) {
// A window too short for every line (the RayNeo iO's) shows the first ones.
if (y + font.lineHeight > height) break;
image.drawText(font, textX, y, line, 190);
image.drawText(font, LIST_X, y, line, 190);
y += 20;
}
}
@@ -199,9 +187,10 @@ export class BandwidthBenchmarkLayer implements Layer {
: item === "link" ? ["Link request", LINK_MODES[this.linkModeIndex]!]
: [this.phase === "done" ? "Run again" : "Start", ""];
const textY = centeredTextY(font, y, height);
image.drawText(font, x + ROW_TEXT_INSET, textY, label, selected ? 255 : 200);
// Text sits 6px in from the row box (LIST_X), values at VALUE_X.
image.drawText(font, x + 6, textY, label, selected ? 255 : 200);
if (value) {
image.drawText(font, x + ROW_TEXT_INSET + VALUE_OFFSET_X, textY, value, selected ? 235 : 160);
image.drawText(font, x + 6 + VALUE_X - LIST_X, textY, value, selected ? 235 : 160);
}
}
+14 -24
View File
@@ -13,12 +13,9 @@ import {
type SoundEffect,
} from "../../ui/sound-effects";
/** The title band: the list starts this far below the top edge's padding. */
const HEADER_HEIGHT = 22;
/** Horizontal inset of a row's text from its highlight box; the title lines up with it. */
const ROW_TEXT_INSET = 6;
/** The status text ends this far short of the rows' right edge. */
const STATUS_INSET = 10;
const HEADER_HEIGHT = 30;
const LIST_X = 20;
const FOOTER_HEIGHT = 0;
function sleep(ms: number): Promise<void> {
return new Promise((resolve) => setTimeout(resolve, ms));
@@ -42,26 +39,19 @@ export class BuzzerDemoLayer implements Layer {
paint(ctx: LayerContext): GrayImage {
const font = getDefaultSmallFont();
const { width, height, padding } = ctx.stack.getBaseSize();
const { width, height } = ctx.stack.getBaseSize();
const image = new GrayImage(width, height, 0);
image.drawText(font, padding.left + ROW_TEXT_INSET, padding.top, "Buzzer demo", 220);
image.drawText(font, 20, 8, "Buzzer demo", 220);
const status = this.playing ? `Playing: ${this.playing}` : `${GESTURE_CLICK} play`;
image.drawText(font, width - padding.right - STATUS_INSET - font.measureText(status), padding.top, status, 140);
image.drawText(font, width - 24 - font.measureText(status), 8, status, 140);
// The title line stays put, so the list starts right under it with no
// padding of its own there. It scrolls, so it runs to the window's
// bottom edge with the padding suggested for that edge inside it, after
// the last row.
const listTop = padding.top + HEADER_HEIGHT - 1;
if (height > listTop) {
this.menu.paint(
image,
{ x: padding.left, y: listTop, width: Math.max(0, width - padding.left - padding.right), height: height - listTop,
padTop: 0, padBottom: padding.bottom },
ctx.stack.isFocused(),
);
}
const listHeight = height - HEADER_HEIGHT - FOOTER_HEIGHT;
this.menu.paint(
image,
{ x: LIST_X - 6, y: HEADER_HEIGHT - 1, width: width - 2 * LIST_X + 12, height: listHeight },
ctx.stack.isFocused(),
);
return image;
}
@@ -114,6 +104,6 @@ export class BuzzerDemoLayer implements Layer {
function drawEffectRow({ image, item: effect, x, y, height, selected }: MenuDrawArgs<SoundEffect>): void {
const font = getDefaultSmallFont();
const textY = centeredTextY(font, y, height);
image.drawText(font, x + ROW_TEXT_INSET, textY, effect.name, selected ? 255 : 200);
image.drawText(font, x + ROW_TEXT_INSET + 110, textY, effect.desc, selected ? 180 : 120);
image.drawText(font, x + 6, textY, effect.name, selected ? 255 : 200);
image.drawText(font, x + 6 + 110, textY, effect.desc, selected ? 180 : 120);
}
+12 -11
View File
@@ -6,13 +6,13 @@ import { BuzzerDemoLayer } from "./buzzer-demo";
import { AccelerometerDemoLayer } from "./accelerometer-demo";
import { BandwidthBenchmarkLayer } from "./bandwidth-benchmark";
import { LightSensorDemoLayer } from "./light-sensor-demo";
import { MouseTestLayer } from "./mouse-test";
import { MicrophonesMonitorLayer } from "./microphones-monitor";
import { ResourceUsageLayer } from "./resource-usage";
import { RingStatusLayer } from "./ring-status";
import { LoadAppFromQrLayer, LoadAppFromUrlLayer } from "./load-app";
import { unicodeTestMenu } from "./unicode-test";
import { type AppContext } from "../app-definition";
import { appViewportSize } from "../../ui/shell/geometry";
import {
createInProcessWindow,
YieldAtRootLayer,
@@ -23,9 +23,17 @@ import {
export const DEVELOPER_WINDOW_ID = "developer";
export const DEVELOPER_SURFACE_ID = "window:developer";
// These menus are pages, not popups: "Debug tests" replaces the root menu
// visually instead of letting the taller root show through beneath it.
const MENU_LAYOUT = { page: true, width: 248 } as const;
const MENU_LAYOUT = {
x: 8,
y: 8,
width: 272,
showBorder: false,
minHeight: 0,
maxHeight: appViewportSize("min").height - 16,
// These menus are pages, not popups: "Debug tests" replaces the root menu
// visually instead of letting the taller root show through beneath it.
opaque: true,
};
type OpenPage = (ctx: Parameters<MenuItem["onSelect"]>[0]) => void;
@@ -71,13 +79,6 @@ function debugTestsMenu(openInputEvents: OpenPage, openMicrophones: OpenPage): M
label: "Microphones",
onSelect: openMicrophones,
},
{
// The touchpad or mouse of a keyboard paired through the glasses.
label: "Mouse cursor",
onSelect: (ctx) => {
ctx.stack.push(new MouseTestLayer(DEVELOPER_WINDOW_ID, ctx.actions.requestRender));
},
},
{
label: "BLE bandwidth",
onSelect: (ctx) => {
+15 -36
View File
@@ -6,14 +6,6 @@ import { addInputListener, InputEventLog, inputTimestamp } from "../../ui/input-
import type { Layer, LayerContext } from "../../ui/layers";
import { drawListScrollbar, type MenuItem } from "../../ui/menu";
/** Room the log's rows keep free on the right for the scrollbar, and the bar's own width. */
const SCROLLBAR_GUTTER = 6;
const SCROLLBAR_WIDTH = 3;
/** A row's second (raw report) line is indented this far, and stops as far short of the row's end. */
const DETAIL_INDENT = 12;
/** Gap between the log and the hint line under it. */
const FOOTER_GAP = 6;
/** Gestures are samples here; back/clear/pause live in the tap-then-hold menu. */
export class InputEventsLayer implements Layer {
readonly acceptsDirectional = true;
@@ -56,41 +48,30 @@ export class InputEventsLayer implements Layer {
}
paint(ctx: LayerContext): GrayImage {
const { width, height, padding } = ctx.stack.getBaseSize();
const { width, height } = ctx.stack.getBaseSize();
const image = new GrayImage(width, height, 0);
const font = getDefaultSmallFont();
// The title and column-heading lines stay put at the top and the hint
// line at the bottom, each inside the padding suggested for its edge.
// The log scrolls between them, so it needs no padding of its own.
const left = padding.left;
const top = padding.top;
const right = width - padding.right;
const footerY = height - padding.bottom - font.lineHeight;
const listTop = top + 42;
const listBottom = footerY - FOOTER_GAP;
// The rows stop short of the scrollbar, which keeps inside the padding.
const rowsRight = right - SCROLLBAR_GUTTER;
image.drawText(font, left, top, "Input events", 230);
image.drawText(font, 12, 8, "Input events", 230);
const status = `${this.log.paused ? "Paused" : "Live"} ${this.log.count} events`;
image.drawText(font, right - font.measureText(status), top, status, 180);
image.drawText(font, left, top + 20, this.phoneTime ? "Phone receive time | gap ms | source | event" : "Time (R=ring tick) | gap | source | event", 140);
image.drawText(font, width - 12 - font.measureText(status), 8, status, 180);
image.drawText(font, 12, 28, this.phoneTime ? "Phone receive time | gap ms | source | event" : "Time (R=ring tick) | gap | source | event", 140);
const lineHeight = Math.max(18, font.lineHeight + 2);
const rowHeight = lineHeight * 2 + 4;
this.visibleRows = Math.max(1, Math.floor((listBottom - listTop) / rowHeight));
this.visibleRows = Math.max(1, Math.floor((height - 76) / rowHeight));
this.scrollRow = Math.min(this.scrollRow, Math.max(0, this.log.entries.length - this.visibleRows));
const timeX = left;
const timeX = 12;
const gapX = timeX + font.measureText("00:00:00.000") + 12;
const sourceX = gapX + font.measureText("+99999") + 12;
const eventX = sourceX + font.measureText("right-arm") + 12;
if (!this.log.entries.length) {
image.drawText(font, left, listTop + 12, "Touch, tap, double-tap, hold, or swipe the ring.", 210);
image.drawText(font, left, listTop + 32, "Press arrives before an interpreted gesture.", 150);
image.drawText(font, 12, 62, "Touch, tap, double-tap, hold, or swipe the ring.", 210);
image.drawText(font, 12, 82, "Press arrives before an interpreted gesture.", 150);
}
for (const [index, entry] of this.log.entries.slice(this.scrollRow, this.scrollRow + this.visibleRows).entries()) {
const { event } = entry;
const ringTime = !this.phoneTime && event.ringInput;
const gapMs = ringTime ? entry.ringGapTicks : entry.gapMs;
const y = listTop + index * rowHeight;
const y = 50 + index * rowHeight;
const gap = gapMs === null ? "-" : `${gapMs >= 0 ? "+" : ""}${gapMs}`;
const source = "source" in event ? event.source ?? "-" : "-";
const label = (entry.filtered ? "*" : "") + (event.type === "unknown"
@@ -99,20 +80,18 @@ export class InputEventsLayer implements Layer {
image.drawText(font, timeX, y, ringTime ? `R${ringTime.tick}` : inputTimestamp(event.timestampMs), 170);
image.drawText(font, gapX, y, truncateText(font, gap, sourceX - gapX - 4), 160);
image.drawText(font, sourceX, y, source, 180);
image.drawText(font, eventX, y, truncateText(font, label, rowsRight - eventX), event.type === "ring-press" ? 255 : 200);
image.drawText(font, eventX, y, truncateText(font, label, width - eventX - 12), event.type === "ring-press" ? 255 : 200);
if (event.ringInput) {
const { type, aux, speed } = event.ringInput;
// These are the original report bytes, not physical distance/speed units.
const details = `Raw ring: type ${type} AUX ${aux} speed ${speed}`;
image.drawText(font, timeX + DETAIL_INDENT, y + lineHeight,
truncateText(font, details, rowsRight - timeX - 2 * DETAIL_INDENT), 170);
image.drawText(font, timeX + 12, y + lineHeight,
truncateText(font, details, width - timeX - 36), 170);
}
}
if (this.log.paused && listBottom > listTop) {
drawListScrollbar(image, right - SCROLLBAR_WIDTH, listTop, listBottom - listTop,
this.scrollRow, this.visibleRows, this.log.entries.length);
}
image.drawText(font, left, footerY,
if (this.log.paused) drawListScrollbar(image, width - 5, 50, height - 78,
this.scrollRow, this.visibleRows, this.log.entries.length);
image.drawText(font, 12, height - 20,
this.log.paused ? "Scroll: history Tap-hold: controls" : "Unfiltered (*=dedup) Tap-hold: controls", 140);
return image;
}
+16 -29
View File
@@ -22,10 +22,6 @@ const RECONCILE_INTERVAL_MS = 400;
// Length of the on-screen trace.
const HISTORY_MS = 30_000;
const MAX_SAMPLES = Math.ceil(HISTORY_MS / 100) + 2;
/** The readout under the title sits this far in from the title's left edge. */
const BODY_INDENT = 2;
/** Gap between the trace's box and the footer line under it. */
const CHART_FOOTER_GAP = 18;
type Sample = { atMs: number; value: number; peak: number };
@@ -106,47 +102,39 @@ export class LightSensorDemoLayer implements Layer {
paint(ctx: LayerContext): GrayImage {
const small = getDefaultSmallFont();
const medium = getDefaultMediumFont();
const { width, height, padding } = ctx.stack.getBaseSize();
const { width, height } = ctx.stack.getBaseSize();
const image = new GrayImage(width, height, 0);
// The title line, the trace's box and the footer line sit at the padding
// suggested for the edges they reach; the rest is placed from them.
const left = padding.left;
const top = padding.top;
const right = width - padding.right;
const bodyX = left + BODY_INDENT;
const footerY = height - padding.bottom - small.lineHeight;
image.drawText(small, left, top, "Light sensor", 220);
image.drawText(small, 20, 8, "Light sensor", 220);
const status = this.enabled ? "polling" : "paused";
image.drawText(small, right - small.measureText(status), top, status, 150);
image.drawText(small, width - 20 - small.measureText(status), 8, status, 150);
const report = this.report;
if (!report) {
const message = this.enabled ? "Waiting for a report…" : "Paused (page not visible).";
image.drawText(small, bodyX, top + 36, message, 190);
image.drawText(small, bodyX, top + 58, "Needs Faceclaw's custom firmware.", 120);
image.drawText(small, left, footerY, `${GESTURE_DOUBLE_CLICK} back`, 110);
image.drawText(small, 22, 44, message, 190);
image.drawText(small, 22, 66, "Needs Faceclaw's custom firmware.", 120);
image.drawText(small, 20, height - 16, `${GESTURE_DOUBLE_CLICK} back`, 110);
return image;
}
// Headline reading. The stock driver's unit is roughly a tenth of a lux.
const lux = (report.alsValue / 10).toFixed(1);
image.drawText(medium, bodyX, top + 22, `${lux} lux`, 245);
image.drawText(medium, 22, 30, `${lux} lux`, 245);
const peakText = `peak ${(report.peak / 10).toFixed(1)}`;
image.drawText(small, bodyX + medium.measureText(`${lux} lux`) + 14, top + 28, peakText, 170);
image.drawText(small, 22 + medium.measureText(`${lux} lux`) + 14, 36, peakText, 170);
const flags: string[] = [];
if (!report.opened) flags.push("sensor closed");
if (report.passive) flags.push("passive");
if (report.autoBrightness) flags.push("auto on");
if (report.passive && !report.readOk) flags.push("read failed");
image.drawText(small, bodyX, top + 50, flags.join(" ") || "stock polling", 150);
image.drawText(small, 22, 58, flags.join(" ") || "stock polling", 150);
const statsY = top + 68;
image.drawText(
small,
bodyX,
statsY,
22,
76,
`brightness ${report.brightness} stock target ${report.stockTarget} gear ${report.gear} scale ${(report.scaleQ10 / 1024).toFixed(2)}`,
170,
);
@@ -154,17 +142,16 @@ export class LightSensorDemoLayer implements Layer {
// 30 s trace of the reading (bright) and the driver's 5-sample peak (dim).
// The axis label sits between the stats line and the chart, so the chart
// starts a full text line lower than the stats.
const chartTop = statsY + small.lineHeight * 2 + 4;
const bounds = { x: left, y: chartTop, width: right - left, height: footerY - CHART_FOOTER_GAP - chartTop };
const chartTop = 76 + small.lineHeight * 2 + 4;
const bounds = { x: 20, y: chartTop, width: width - 40, height: height - chartTop - 34 };
const maxValue = Math.max(...this.samples.map((sample) => Math.max(sample.value, sample.peak)), 10);
const scale = roundUpScale(maxValue * 1.08);
image.drawText(small, bounds.x, bounds.y - small.lineHeight - 2, `0–${(scale / 10).toFixed(0)} lux, last 30 s`, 120);
// A window too short for the trace (a large font on the RayNeo iO) goes without.
if (bounds.width > 1 && bounds.height > 1) drawTrace(image, this.samples, Date.now(), bounds, scale);
drawTrace(image, this.samples, Date.now(), bounds, scale);
image.drawText(small, left, footerY, `reports: ${this.reportCount}`, 110);
image.drawText(small, 20, height - 16, `reports: ${this.reportCount}`, 110);
const back = `${GESTURE_DOUBLE_CLICK} back`;
image.drawText(small, right - small.measureText(back), footerY, back, 110);
image.drawText(small, width - 20 - small.measureText(back), height - 16, back, 110);
return image;
}
+17 -21
View File
@@ -9,6 +9,8 @@ import { lineStep } from "../../ui/metrics";
import { developerAppUrlSetting } from "../../ui/dashboard-settings";
import { cancelQrScan, isQrScannerAvailable, scanQrCode } from "../../native/qr-scan";
const MARGIN = 20;
/**
* "Load app from URL": the glasses side of the phone text editor. Entering the
* page opens the editor on developerAppUrlSetting, the typed URL echoes here
@@ -42,30 +44,27 @@ export class LoadAppFromUrlLayer implements Layer {
paint(ctx: LayerContext): GrayImage {
const font = getDefaultSmallFont();
// The page's text fills the window inside the padding suggested for its
// edges: the title at the top, the hint line at the bottom.
const { width, height, padding } = ctx.stack.getBaseSize();
const { width, height } = ctx.stack.getBaseSize();
const image = new GrayImage(width, height, 0);
const step = lineStep(font);
const left = padding.left;
const textWidth = Math.max(0, width - padding.left - padding.right);
const textWidth = width - 2 * MARGIN;
image.drawText(font, left, padding.top, "Load app from URL", 220);
let y = padding.top + step + 6;
image.drawText(font, left, y, "Type the app's URL in the phone app", 150);
image.drawText(font, MARGIN, 8, "Load app from URL", 220);
let y = 8 + step + 6;
image.drawText(font, MARGIN, y, "Type the app's URL in the phone app", 150);
y += step;
image.drawText(font, left, y, "(or use voice input from the menu).", 150);
image.drawText(font, MARGIN, y, "(or use voice input from the menu).", 150);
y += step + 8;
const url = developerAppUrlSetting.get();
for (const line of wrapText(font, url || "(empty)", textWidth).slice(0, 3)) {
image.drawText(font, left, y, line, url ? 230 : 110);
image.drawText(font, MARGIN, y, line, url ? 230 : 110);
y += step;
}
if (this.error) {
y += 4;
for (const line of wrapText(font, this.error, textWidth).slice(0, 2)) {
image.drawText(font, left, y, line, 150);
image.drawText(font, MARGIN, y, line, 150);
y += step;
}
}
@@ -73,7 +72,7 @@ export class LoadAppFromUrlLayer implements Layer {
const footer = this.launching
? "Loading..."
: `${GESTURE_CLICK} load ${GESTURE_DOUBLE_CLICK} cancel`;
image.drawText(font, left, height - padding.bottom - font.lineHeight, footer, 110);
image.drawText(font, MARGIN, height - font.lineHeight - 12, footer, 110);
return image;
}
@@ -146,22 +145,19 @@ export class LoadAppFromQrLayer implements Layer {
paint(ctx: LayerContext): GrayImage {
const font = getDefaultSmallFont();
// Laid out like the URL page: text inside the padding suggested for the
// window's edges, the hint line at the bottom.
const { width, height, padding } = ctx.stack.getBaseSize();
const { width, height } = ctx.stack.getBaseSize();
const image = new GrayImage(width, height, 0);
const step = lineStep(font);
const left = padding.left;
image.drawText(font, left, padding.top, "Load app from QR code", 220);
let y = padding.top + step + 6;
for (const line of wrapText(font, this.status, Math.max(0, width - padding.left - padding.right)).slice(0, 5)) {
image.drawText(font, left, y, line, 170);
image.drawText(font, MARGIN, 8, "Load app from QR code", 220);
let y = 8 + step + 6;
for (const line of wrapText(font, this.status, width - 2 * MARGIN).slice(0, 5)) {
image.drawText(font, MARGIN, y, line, 170);
y += step;
}
const footer = this.busy ? `${GESTURE_DOUBLE_CLICK} back` : `${GESTURE_CLICK} scan again ${GESTURE_DOUBLE_CLICK} back`;
image.drawText(font, left, height - padding.bottom - font.lineHeight, footer, 110);
image.drawText(font, MARGIN, height - font.lineHeight - 12, footer, 110);
return image;
}
+16 -24
View File
@@ -37,8 +37,6 @@ const STOCK_COUNTER_OFFSET = 204;
// Counter gaps at or beyond this are late/duplicate copies, not losses.
const STOCK_LATE_GAP = 128;
const LEVEL_FLOOR_DB = -90;
/** Gap between the two temples' columns. */
const COLUMN_GAP = 16;
// wallMs (JS receipt time) windows the rates; arrivalMs (the session core's
// elapsed-realtime stamp, taken on the BLE thread) measures gaps without the
@@ -231,45 +229,39 @@ export class MicrophonesMonitorLayer implements Layer {
paint(ctx: LayerContext): GrayImage {
const font = getDefaultSmallFont();
const line = font.lineHeight;
const { width, height, padding } = ctx.stack.getBaseSize();
const { width, height } = ctx.stack.getBaseSize();
const image = new GrayImage(width, height, 0);
const nowMs = Date.now();
// The title line, the two columns and the footer line span the window
// inside the padding suggested for its edges.
const left = padding.left;
const titleY = padding.top;
const right = width - padding.right;
const footerY = height - padding.bottom - line;
image.drawText(font, left, titleY, "Microphones (passive)", 230);
image.drawText(font, 16, 5, "Microphones (passive)", 230);
const capture = isFaceclawAudioCaptureActive() ? "Faceclaw capture: on" : "Faceclaw capture: off";
image.drawText(font, right - font.measureText(capture), titleY, capture, 170);
image.drawText(font, width - 16 - font.measureText(capture), 5, capture, 170);
if (!this.communicator) {
image.drawText(font, left, titleY + line * 2, "Glasses not connected.", 190);
image.drawText(font, left, footerY, `${GESTURE_DOUBLE_CLICK} back`, 110);
image.drawText(font, 16, 5 + line * 2, "Glasses not connected.", 190);
image.drawText(font, 16, height - 16, `${GESTURE_DOUBLE_CLICK} back`, 110);
return image;
}
const columnWidth = Math.max(0, Math.floor((right - left - COLUMN_GAP) / 2));
const rightColumnX = left + columnWidth + COLUMN_GAP;
const gutter = 16;
const columnWidth = Math.floor((width - gutter * 3) / 2);
const stripHeight = 22;
const top = titleY + line + 6;
const bottom = footerY - stripHeight - 8;
this.paintArm(image, "L", "Left", left, top, columnWidth, bottom, nowMs);
this.paintArm(image, "R", "Right", rightColumnX, top, columnWidth, bottom, nowMs);
const top = 5 + line + 6;
const bottom = height - 16 - stripHeight - 8;
this.paintArm(image, "L", "Left", gutter, top, columnWidth, bottom, nowMs);
this.paintArm(image, "R", "Right", gutter * 2 + columnWidth, top, columnWidth, bottom, nowMs);
const stripY = footerY - stripHeight - 4;
const stripY = height - 16 - stripHeight - 4;
const stripMax = Math.max(1, ...this.stats.L.buckets, ...this.stats.R.buckets);
drawStrip(image, this.stats.L.buckets, left, stripY, columnWidth, stripHeight, stripMax);
drawStrip(image, this.stats.R.buckets, rightColumnX, stripY, columnWidth, stripHeight, stripMax);
drawStrip(image, this.stats.L.buckets, gutter, stripY, columnWidth, stripHeight, stripMax);
drawStrip(image, this.stats.R.buckets, gutter * 2 + columnWidth, stripY, columnWidth, stripHeight, stripMax);
const footer = this.unknownArmPackets > 0
? `last ${HISTORY_MS / 1000} s unattributed packets ${this.unknownArmPackets}`
: `packets per ${BUCKET_MS} ms, last ${HISTORY_MS / 1000} s`;
image.drawText(font, left, footerY, footer, 110);
image.drawText(font, gutter, height - 16, footer, 110);
const back = `${GESTURE_DOUBLE_CLICK} back tap reset`;
image.drawText(font, right - font.measureText(back), footerY, back, 110);
image.drawText(font, width - gutter - font.measureText(back), height - 16, back, 110);
return image;
}
-206
View File
@@ -1,206 +0,0 @@
import { getDefaultSmallFont } from "../../graphics/ui-fonts";
import { GrayImage } from "../../graphics/image";
import type { HidPointerSample } from "../../g2/keyboard/hid";
import { keyboardService } from "../../g2/keyboard/service";
import { type InputEvent } from "../../ui/gestures";
import { Layer, type LayerContext } from "../../ui/layers";
import { shell } from "../../ui/shell/shell";
import { clamp } from "../../util/numeric-util";
// How often to reconcile holding the pointer with window visibility.
const RECONCILE_INTERVAL_MS = 400;
/** A touchpad reports a hundred times a second; the page repaints at most this often. */
const RENDER_INTERVAL_MS = 33;
/** Cursor pixels per count the touchpad reports. */
const POINTER_GAIN = 1;
/** Left-click marks kept on the page. */
const MAX_MARKS = 12;
/** The arrow, tip at its top left: '#' is the outline, '.' the fill. */
const CURSOR = [
"#",
"##",
"#.#",
"#..#",
"#...#",
"#....#",
"#.....#",
"#......#",
"#.......#",
"#........#",
"#.....#####",
"#..#..#",
"#.# #..#",
"## #..#",
"# #..#",
" #..#",
" ##",
];
/**
* Mouse cursor test: moves an arrow with the touchpad or mouse of the
* keyboard paired through the glasses (app/g2/keyboard/), and shows its
* buttons and wheel. The main UI has no cursor; this is where one is tried
* out.
*
* While the page is visible it takes the pointer, so the buttons and wheel
* show here instead of acting as select, back and scroll. A left click
* leaves a mark where the arrow is and a right click clears them. Leave
* with Esc, or a double tap on the ring or a temple.
*/
export class MouseTestLayer implements Layer {
private x = -1;
private y = -1;
private size = { width: 0, height: 0 };
private buttons = 0;
private wheel = 0;
private marks: Array<{ x: number; y: number }> = [];
private samples = 0;
private rate = 0;
private rateWindowStart = Date.now();
private removed = false;
private release: (() => void) | null = null;
private reconcileTimer: ReturnType<typeof setInterval> | null = null;
private renderTimer: ReturnType<typeof setTimeout> | null = null;
constructor(
private readonly windowId: string,
private readonly requestRender: () => void,
) {
this.reconcileTimer = setInterval(() => this.reconcile(), RECONCILE_INTERVAL_MS);
this.reconcile();
}
/** Hold the pointer only while the page can be seen; elsewhere its buttons go back to being keys. */
private reconcile(): void {
if (this.removed) return;
const visible = shell.isWindowVisible(this.windowId);
if (visible && !this.release) {
this.release = keyboardService.takePointer((sample) => this.onSample(sample));
} else if (!visible && this.release) {
this.release();
this.release = null;
this.buttons = 0;
}
// The rate reads zero once the pointer has been still for a second.
const now = Date.now();
if (now - this.rateWindowStart >= 1000) {
const rate = this.samples;
this.samples = 0;
this.rateWindowStart = now;
if (rate !== this.rate) {
this.rate = rate;
this.scheduleRender();
}
}
}
private onSample(sample: HidPointerSample): void {
if (this.removed) return;
this.samples++;
const { width, height } = this.size;
if (width > 0 && height > 0) {
if (this.x < 0) {
this.x = width / 2;
this.y = height / 2;
}
this.x = clamp(this.x + sample.dx * POINTER_GAIN, 0, width - 1);
this.y = clamp(this.y + sample.dy * POINTER_GAIN, 0, height - 1);
}
const pressed = sample.buttons & ~this.buttons;
this.buttons = sample.buttons;
this.wheel += sample.wheel;
if (pressed & 1 && this.x >= 0) {
this.marks.push({ x: Math.round(this.x), y: Math.round(this.y) });
if (this.marks.length > MAX_MARKS) this.marks.shift();
}
if (pressed & 2) this.marks = [];
this.scheduleRender();
}
private scheduleRender(): void {
if (this.renderTimer !== null || this.removed) return;
this.renderTimer = setTimeout(() => {
this.renderTimer = null;
if (!this.removed) this.requestRender();
}, RENDER_INTERVAL_MS);
}
onRemoved(): void {
this.removed = true;
if (this.reconcileTimer !== null) clearInterval(this.reconcileTimer);
this.reconcileTimer = null;
if (this.renderTimer !== null) clearTimeout(this.renderTimer);
this.renderTimer = null;
this.release?.();
this.release = null;
}
paint(ctx: LayerContext): GrayImage {
const font = getDefaultSmallFont();
const { width, height, padding } = ctx.stack.getBaseSize();
this.size = { width, height };
const image = new GrayImage(width, height, 0);
const left = padding.left;
const right = width - padding.right;
const top = padding.top;
const line = font.lineHeight + 4;
image.drawText(font, left, top, "Mouse cursor", 220);
const snapshot = keyboardService.snapshot();
const problem = snapshot.state !== "connected"
? "No keyboard is connected."
: !snapshot.pointer ? "This keyboard has no touchpad or mouse buttons." : null;
if (problem) {
image.drawText(font, left, top + 2 * line, problem, 190);
image.drawText(font, left, top + 3 * line, "Pair one under Settings > Keyboard.", 120);
return image;
}
const position = this.x < 0 ? "move the pointer" : `${Math.round(this.x)}, ${Math.round(this.y)}`;
image.drawText(font, right - font.measureText(position), top, position, 150);
// Buttons: a box each, filled while held.
let x = left;
const boxY = top + line;
for (const [index, label] of ["L", "M", "R"].entries()) {
// HID numbers them left, right, middle; they sit left, middle, right.
const held = !!(this.buttons & [1, 4, 2][index]!);
const boxW = font.measureText(label) + 10;
if (held) image.fillRect(x, boxY, boxW, font.lineHeight + 2, 200);
else image.drawRect(x, boxY, boxW, font.lineHeight + 2, 90);
image.drawText(font, x + 5, boxY + 1, label, held ? 1 : 170);
x += boxW + 4;
}
image.drawText(font, x + 8, boxY + 1, `wheel ${this.wheel}`, 150);
const rate = `${this.rate}/s`;
image.drawText(font, right - font.measureText(rate), boxY + 1, rate, 110);
const hint = "Left click marks, right click clears. Esc or double tap: back";
image.drawText(font, left, height - padding.bottom - font.lineHeight, hint, 90);
for (const mark of this.marks) {
image.drawLine(mark.x - 4, mark.y, mark.x + 4, mark.y, 140);
image.drawLine(mark.x, mark.y - 4, mark.x, mark.y + 4, 140);
}
if (this.x >= 0) drawCursor(image, Math.round(this.x), Math.round(this.y));
return image;
}
async handleInput(event: InputEvent, ctx: LayerContext): Promise<void> {
if (event.type === "double-click") {
ctx.stack.pop();
}
}
}
function drawCursor(image: GrayImage, x: number, y: number): void {
for (const [row, pixels] of CURSOR.entries()) {
for (let col = 0; col < pixels.length; col++) {
const pixel = pixels[col];
if (pixel === " ") continue;
const px = x + col;
const py = y + row;
if (px < 0 || py < 0 || px >= image.width || py >= image.height) continue;
image.setPixel(px, py, pixel === "#" ? 255 : 70);
}
}
}
+21 -46
View File
@@ -16,14 +16,6 @@ const HISTORY_MS = 60_000;
const MAX_SAMPLES = Math.ceil(HISTORY_MS / SAMPLE_INTERVAL_MS) + 2;
/** Line brightness per memory series, headline first. */
const MEMORY_SHADES = [240, 155, 80];
/** The charts' heights where the window has room for them. */
const MEMORY_CHART_HEIGHT = 70;
const CPU_CHART_HEIGHT = 60;
/** Between the charts: a gap, then the CPU chart's label line. */
const CHART_GAP = 10;
const CHART_LABEL_HEIGHT = 16;
/** Least gap between the CPU chart and the footer line. */
const CHART_FOOTER_GAP = 4;
type ChartSample = ResourceSample & { cpuPercent: number };
@@ -64,26 +56,20 @@ export class ResourceUsageLayer implements Layer {
paint(ctx: LayerContext): GrayImage {
const font = getDefaultSmallFont();
const { width, height, padding } = ctx.stack.getBaseSize();
const { width, height } = ctx.stack.getBaseSize();
const image = new GrayImage(width, height, 0);
const latest = this.samples[this.samples.length - 1];
// The title line, the charts and the footer line span the window inside
// the padding suggested for its edges; the rest is placed from the top.
const left = padding.left;
const top = padding.top;
const right = width - padding.right;
const footerY = height - padding.bottom - font.lineHeight;
image.drawText(font, left, top, "Resource usage — last minute", 230);
image.drawText(font, 16, 5, "Resource usage — last minute", 230);
if (!latest) {
image.drawText(font, left, top + 25, this.sampleError || "Waiting for first sample…", 170);
image.drawText(font, left, footerY, `${GESTURE_DOUBLE_CLICK} back`, 110);
image.drawText(font, 16, 30, this.sampleError || "Waiting for first sample…", 170);
image.drawText(font, 16, height - 16, `${GESTURE_DOUBLE_CLICK} back`, 110);
return image;
}
const cpuText = `CPU ${formatPercent(latest.cpuPercent)} ${latest.threads} threads`;
image.drawText(font, right - font.measureText(cpuText), top, cpuText, 200);
image.drawText(font, left, top + 16, this.sampleError || latest.status, this.sampleError ? 170 : 140);
image.drawText(font, width - 16 - font.measureText(cpuText), 5, cpuText, 200);
image.drawText(font, 16, 21, this.sampleError || latest.status, this.sampleError ? 170 : 140);
const memorySeries = MEMORY_SERIES.map((label, index) => ({
label,
@@ -92,44 +78,34 @@ export class ResourceUsageLayer implements Layer {
}));
const memoryPeak = Math.max(...this.samples.flatMap((sample) => sample.memoryMb), 64);
const memoryScale = roundUp(memoryPeak * 1.08, 64);
const legendY = top + 43;
const legendLeft = drawLegend(
image,
font,
right,
legendY,
width,
48,
memorySeries.map((series) => [`${series.label} ${formatMb(series.value(latest))}`, series.shade] as const),
);
const scaleLabel = `Memory 0–${formatMb(memoryScale)}`;
const shortScaleLabel = `0–${formatMb(memoryScale)}`;
image.drawText(
font,
left,
legendY,
left + font.measureText(scaleLabel) + 8 <= legendLeft ? scaleLabel : shortScaleLabel,
16,
48,
16 + font.measureText(scaleLabel) + 8 <= legendLeft ? scaleLabel : shortScaleLabel,
140,
);
// The charts shrink together where the window is too short for both
// above the footer line (the RayNeo iO's).
const chartsTop = legendY + CHART_LABEL_HEIGHT;
const room = footerY - CHART_FOOTER_GAP - chartsTop - CHART_GAP - CHART_LABEL_HEIGHT;
const fit = clamp(room / (MEMORY_CHART_HEIGHT + CPU_CHART_HEIGHT), 0, 1);
const memoryHeight = Math.floor(MEMORY_CHART_HEIGHT * fit);
const chartWidth = Math.max(0, right - left);
drawChart(image, this.samples, latest.atMs, { x: left, y: chartsTop, width: chartWidth, height: memoryHeight }, memoryScale, memorySeries);
drawChart(image, this.samples, latest.atMs, { x: 16, y: 64, width: width - 32, height: 70 }, memoryScale, memorySeries);
const cpuPeak = Math.max(...this.samples.map((sample) => sample.cpuPercent), 100);
const cpuScale = roundUp(cpuPeak * 1.08, 25);
const cpuLabelY = chartsTop + memoryHeight + CHART_GAP;
image.drawText(font, left, cpuLabelY, `CPU 0–${Math.round(cpuScale)}% (one core)`, 140);
drawChart(image, this.samples, latest.atMs,
{ x: left, y: cpuLabelY + CHART_LABEL_HEIGHT, width: chartWidth, height: Math.floor(CPU_CHART_HEIGHT * fit) }, cpuScale, [
{ value: (sample) => sample.cpuPercent, shade: 220 },
]);
image.drawText(font, 16, 144, `CPU 0–${Math.round(cpuScale)}% (one core)`, 140);
drawChart(image, this.samples, latest.atMs, { x: 16, y: 160, width: width - 32, height: 60 }, cpuScale, [
{ value: (sample) => sample.cpuPercent, shade: 220 },
]);
image.drawText(font, left, footerY, `-60s`, 80);
image.drawText(font, 16, height - 16, `-60s`, 80);
const nowLabel = `now`;
image.drawText(font, right - font.measureText(nowLabel), footerY, nowLabel, 110);
image.drawText(font, width - 16 - font.measureText(nowLabel), height - 16, nowLabel, 110);
return image;
}
@@ -172,7 +148,6 @@ function drawChart(
scale: number,
series: readonly Series[],
): void {
if (bounds.width < 2 || bounds.height < 2) return;
image.drawRect(bounds.x, bounds.y, bounds.width, bounds.height, 45);
const midY = bounds.y + Math.round((bounds.height - 1) / 2);
image.drawLine(bounds.x + 1, midY, bounds.x + bounds.width - 2, midY, 24);
@@ -192,16 +167,16 @@ function drawChart(
}
}
/** Legend of line swatches and labels, right-aligned to end at `right`; returns its left edge. */
/** Right-aligned legend of line swatches and labels; returns its left edge. */
function drawLegend(
image: GrayImage,
font: ReturnType<typeof getDefaultSmallFont>,
right: number,
width: number,
y: number,
entries: ReadonlyArray<readonly [string, number]>,
): number {
const totalWidth = entries.reduce((sum, [label]) => sum + font.measureText(label) + 14, 0);
const left = right - totalWidth;
const left = width - 16 - totalWidth;
let x = left;
for (const [label, shade] of entries) {
image.drawLine(x, y + 6, x + 8, y + 6, shade);
+12 -19
View File
@@ -4,7 +4,7 @@ import { type InputEvent } from "../../ui/gestures";
import { Layer, type LayerContext } from "../../ui/layers";
import { shell } from "../../ui/shell/shell";
import { MenuLayer, type MenuItem } from "../../ui/menu";
import { effectiveRingConnectionMode, ringConnectionModeSetting } from "../../ui/dashboard-settings";
import { ringConnectionModeSetting } from "../../ui/dashboard-settings";
import { loadDeviceAddresses } from "../../g2/device-addresses";
import {
getRingSessionStatus,
@@ -18,10 +18,8 @@ import {
} from "../../native/ring-status";
const POLL_INTERVAL_MS = 1_000;
/** The title sits this far in from the left edge of the rows and buttons under it. */
const TITLE_INDENT = 2;
/** A row's value column, from its label's left edge. */
const VALUE_OFFSET_X = 114;
const LEFT_X = 16;
const VALUE_X = 130;
const BUTTON_GAP = 8;
type Row = { label: string; value: string; dim?: boolean };
@@ -104,31 +102,26 @@ export class RingStatusLayer implements Layer {
paint(ctx: LayerContext): GrayImage {
const font = getDefaultSmallFont();
const { width, height, padding } = ctx.stack.getBaseSize();
const { width, height } = ctx.stack.getBaseSize();
const image = new GrayImage(width, height, 0);
const lineStep = font.lineHeight + 2;
const buttonHeight = font.lineHeight + 8;
// The rows and the button strip span the window inside the padding
// suggested for its side edges; the title sits at the top edge's and the
// buttons' outlines at the bottom edge's.
const left = padding.left;
const buttonTop = height - padding.bottom - buttonHeight;
const buttonTop = height - buttonHeight - 6;
image.drawText(font, left + TITLE_INDENT, padding.top, "Ring status", 230);
let y = padding.top + lineStep + 4;
image.drawText(font, 18, 10, "Ring status", 230);
let y = 10 + lineStep + 4;
const rows = this.rows();
if (this.notice) rows.push({ label: "", value: this.notice });
for (const row of rows) {
if (y + font.lineHeight > buttonTop - 4) break;
image.drawText(font, left, y, row.label, row.dim ? 100 : 150);
image.drawText(font, left + VALUE_OFFSET_X, y, row.value, row.dim ? 120 : 220);
image.drawText(font, LEFT_X, y, row.label, row.dim ? 100 : 150);
image.drawText(font, VALUE_X, y, row.value, row.dim ? 120 : 220);
y += lineStep;
}
const buttonWidth = Math.max(0,
Math.floor((width - padding.left - padding.right - BUTTON_GAP * (BUTTONS.length - 1)) / BUTTONS.length));
const buttonWidth = Math.floor((width - 2 * LEFT_X - BUTTON_GAP * (BUTTONS.length - 1)) / BUTTONS.length);
BUTTONS.forEach((button, index) => {
const x = left + index * (buttonWidth + BUTTON_GAP);
const x = LEFT_X + index * (buttonWidth + BUTTON_GAP);
const selected = index === this.selected;
if (selected) image.fillRoundedRect(x, buttonTop, buttonWidth, buttonHeight, 40, 4);
image.drawRoundedRect(x, buttonTop, buttonWidth, buttonHeight, selected ? 200 : 70, 4);
@@ -153,7 +146,7 @@ export class RingStatusLayer implements Layer {
}
if (!s.directEnabled) {
const pending = effectiveRingConnectionMode() !== "glasses";
const pending = ringConnectionModeSetting.get() !== "glasses";
rows.push({ label: "Direct link", value: pending ? "on after glasses reconnect" : "off (only via glasses)", dim: true });
} else {
const state = s.directNotificationsReady ? "subscribed"
+14 -15
View File
@@ -3,7 +3,7 @@ import { fontSelectionLabel, getDefaultSmallFont, getUiFontSelection } from "../
import { GrayImage, type UiFont } from "../../graphics/image";
import { type InputEvent } from "../../ui/gestures";
import { Layer, type LayerContext } from "../../ui/layers";
import { MenuLayer, type MenuPageLayout } from "../../ui/menu";
import { MenuLayer, type MenuLayout } from "../../ui/menu";
/**
* Unicode test: sample texts chosen to exercise text-rendering features that
@@ -175,6 +175,9 @@ export const UNICODE_SAMPLES: readonly UnicodeSample[] = [
},
];
const MARGIN_X = 18;
const TITLE_Y = 8;
const FOOTER_MARGIN = 20;
const BODY_GAP = 6;
/**
@@ -193,36 +196,32 @@ export class UnicodeSampleLayer implements Layer {
paint(ctx: LayerContext): GrayImage {
const font = getDefaultSmallFont();
// The title and note stay put at the top and the footer line at the
// bottom, inside the padding suggested for the window's edges; the text
// pages between them, so it needs no padding of its own.
const { width, height, padding } = ctx.stack.getBaseSize();
const { width, height } = ctx.stack.getBaseSize();
const image = new GrayImage(width, height, 0);
const left = padding.left;
const textWidth = Math.max(0, width - padding.left - padding.right);
const textWidth = width - 2 * MARGIN_X;
image.drawText(font, left, padding.top, truncateText(font, this.sample.label, textWidth), 220);
const noteY = padding.top + font.lineHeight;
image.drawText(font, MARGIN_X, TITLE_Y, truncateText(font, this.sample.label, textWidth), 220);
const noteY = TITLE_Y + font.lineHeight;
const noteLines = wrapText(font, this.sample.exercises, textWidth);
for (let i = 0; i < noteLines.length; i++) {
image.drawText(font, left, noteY + i * font.lineHeight, noteLines[i]!, 120);
image.drawText(font, MARGIN_X, noteY + i * font.lineHeight, noteLines[i]!, 120);
}
const bodyY = noteY + noteLines.length * font.lineHeight + BODY_GAP;
const footerY = height - padding.bottom - font.lineHeight;
const footerY = height - FOOTER_MARGIN;
this.bodyLineCount = Math.max(1, Math.floor((footerY - bodyY) / font.lineHeight));
const lines = this.getLines(font, textWidth);
this.clampScroll(lines.length);
const visible = lines.slice(this.firstLine, this.firstLine + this.bodyLineCount);
for (let i = 0; i < visible.length; i++) {
image.drawText(font, left, bodyY + i * font.lineHeight, visible[i]!, 235);
image.drawText(font, MARGIN_X, bodyY + i * font.lineHeight, visible[i]!, 235);
}
const page = Math.floor(this.firstLine / this.pageStep()) + 1;
image.drawText(font, left, footerY, `Page ${page}/${this.pageCount(lines.length)}`, 110);
image.drawText(font, MARGIN_X, footerY, `Page ${page}/${this.pageCount(lines.length)}`, 110);
const fontLabel = fontSelectionLabel(getUiFontSelection());
image.drawText(font, width - padding.right - font.measureText(fontLabel), footerY, fontLabel, 110);
image.drawText(font, width - MARGIN_X - font.measureText(fontLabel), footerY, fontLabel, 110);
return image;
}
@@ -272,7 +271,7 @@ export class UnicodeSampleLayer implements Layer {
}
/** The language list; selecting a row opens its sample. */
export function unicodeTestMenu(layout: MenuPageLayout): MenuLayer {
export function unicodeTestMenu(layout: MenuLayout): MenuLayer {
return new MenuLayer(
"Unicode test",
UNICODE_SAMPLES.map((sample) => ({
+48 -129
View File
@@ -7,23 +7,16 @@
* match what apps expect. Extended layouts may pick other fonts per container
* (fonts.ts), which wrap with Faceclaw's own text wrapping.
*
* Containers stack in stock's order (paintOrder): without zOrderIndex, lists,
* then texts, then images, so an image container covers text beneath it. Text
* containers are transparent windows that clip their text and scroll it.
*
* Known deviations from stock, acceptable for now:
* - List rendering is a plain vertical list. A non-zero itemWidth sets the
* selection outline's width and truncates long items to fit, as on stock;
* whether stock also lays narrow items out side by side is unknown.
* - Lists scroll with our menus' animation and bounce (see list-menu.ts).
* - Text scrolls in jumps; stock eases each 28px step over 200ms and
* rubber-bands at the ends.
*/
import { GrayImage } from "../../graphics/image";
import { containerFace } from "./fonts";
import {
borderGrey,
paintOrder,
type EvenHubContainer,
type EvenHubImageContainer,
type EvenHubListContainer,
type EvenHubPage,
@@ -37,122 +30,35 @@ export const EVENHUB_SCREEN_HEIGHT = 288;
const TEXT_WHITE = 255;
/**
* SDK 0.0.14 textColor levels 0..4 as the greys stock draws them (measured
* in the firmware emulator): level 0 is dim, not off. A missing or
* out-of-range level draws white, the only colour before 0.0.14.
* SDK 0.0.14 textColor levels 0..4 as grey values. Level 4 is the default and
* the only one apps got before 0.0.14, so it must stay exactly TEXT_WHITE.
* Level 0 is the firmware's "fully dim" step, which over the black page
* background means the text is effectively not shown.
*/
const TEXT_BRIGHTNESS_GREYS = [51, 102, 153, 204, 255];
const TEXT_BRIGHTNESS_GREYS = [0, 64, 128, 191, 255];
function textGrey(level: number | undefined): number {
return level === undefined ? TEXT_WHITE : (TEXT_BRIGHTNESS_GREYS[level] ?? TEXT_WHITE);
}
/**
* The scrollbar stock draws on any text container whose text overflows
* (LV_SCROLLBAR_MODE_AUTO), whether or not it captures input: 4px wide, 10px
* in from the right edge, in a track 6px in from top and bottom, with its
* corner pixels cut. Measured in the firmware emulator.
*/
const SCROLLBAR_GREY = 187;
const SCROLLBAR_WIDTH = 4;
const SCROLLBAR_RIGHT_INSET = 10;
const SCROLLBAR_TRACK_INSET = 6;
type Rect = { x: number; y: number; width: number; height: number };
/** A text container's wrapped lines and the geometry its scrolling works in. */
export type TextLayout = {
lines: string[];
lineHeight: number;
/** Left edge and unscrolled top of the text. */
contentX: number;
contentY: number;
/** Height of the window the text scrolls in (inside border and padding). */
viewportHeight: number;
/** Largest scrollY: how far the text overflows that window. */
maxScroll: number;
};
/**
* Stock lays text out at x + border + padding and wraps it at width - 2 *
* padding: the label is sized ignoring the border, so a line can run into
* the right border, where it is clipped.
*/
export function layoutText(container: EvenHubTextContainer): TextLayout {
/** Chars the font lacks are skipped silently on stock (no tofu); EvenHubFont
* skips unknown glyphs, matching that. */
function paintTextContainer(image: GrayImage, container: EvenHubTextContainer): void {
// Only the text dims: textColor is a text-brightness level, and stock draws
// the container's border at full brightness regardless.
paintBorder(image, container);
const inset = container.borderWidth + container.paddingLength;
const face = containerFace(container.font);
const lines = face.wrap(container.content, Math.max(1, container.width - 2 * container.paddingLength));
const viewportHeight = Math.max(0, container.height - 2 * inset);
return {
lines,
lineHeight: face.lineHeight,
contentX: container.x + inset,
contentY: container.y + inset,
viewportHeight,
maxScroll: Math.max(0, lines.length * face.lineHeight - viewportHeight),
};
}
/**
* Text is clipped at the border's inner edge (at the container's edge when it
* has no border). Lines wholly inside stay deferred firmware-text draws; a
* line the edge cuts is rasterized and blitted clipped. Chars the font lacks
* are skipped silently, as on stock (no tofu).
*/
function paintTextContainer(image: GrayImage, container: EvenHubTextContainer): void {
// Only the text dims: textColor is a text-brightness level; the border has its own color.
paintBorder(image, container);
const layout = layoutText(container);
const face = containerFace(container.font);
const grey = textGrey(container.textColor);
const border = container.borderWidth;
const clip: Rect = {
x: container.x + border,
y: container.y + border,
width: Math.max(0, container.width - 2 * border),
height: Math.max(0, container.height - 2 * border),
};
for (let i = 0; i < layout.lines.length; i++) {
const y = layout.contentY + i * layout.lineHeight - container.scrollY;
const bottom = y + layout.lineHeight;
if (bottom <= clip.y || y >= clip.y + clip.height) continue;
const line = layout.lines[i]!;
if (y >= clip.y && bottom <= clip.y + clip.height && layout.contentX + face.measureLine(line) <= clip.x + clip.width) {
face.drawText(image, layout.contentX, y, line, grey);
continue;
}
const strip = new GrayImage(container.width, layout.lineHeight, 0);
face.drawText(strip, layout.contentX - container.x, 0, line, grey);
const visibleTop = Math.max(y, clip.y);
image.bitBlt(strip.withDrawsBaked(), clip.x, visibleTop, {
sx: clip.x - container.x,
sy: visibleTop - y,
width: clip.width,
height: Math.min(bottom, clip.y + clip.height) - visibleTop,
transparentZero: true,
});
}
if (layout.maxScroll > 0) paintScrollbar(image, container, layout);
}
function paintScrollbar(image: GrayImage, container: EvenHubTextContainer, layout: TextLayout): void {
const height = container.height;
const track = height - 2 * SCROLLBAR_TRACK_INSET;
const length = Math.floor((track * height) / (height + layout.maxScroll));
if (track <= 0 || length <= 0) return;
const top = container.y + SCROLLBAR_TRACK_INSET + Math.floor(((track - length) * container.scrollY) / layout.maxScroll);
const left = container.x + container.width - SCROLLBAR_RIGHT_INSET;
for (let row = 0; row < length; row++) {
const end = row === 0 || row === length - 1;
image.fillRect(end ? left + 1 : left, top + row, end ? SCROLLBAR_WIDTH - 2 : SCROLLBAR_WIDTH, 1, SCROLLBAR_GREY);
const lines = face.wrap(container.content, Math.max(1, container.width - 2 * inset));
for (let i = 0; i < lines.length; i++) {
face.drawText(image, container.x + inset, container.y + inset + i * face.lineHeight, lines[i]!, textGrey(container.textColor));
}
}
function paintBorder(
image: GrayImage,
container: { x: number; y: number; width: number; height: number; borderWidth: number; borderColor: number; borderRadius: number },
container: { x: number; y: number; width: number; height: number; borderWidth: number; borderRadius: number },
): void {
const grey = borderGrey(container.borderColor);
for (let i = 0; i < container.borderWidth; i++) {
if (container.borderRadius > 0) {
image.drawRoundedRect(
@@ -160,28 +66,28 @@ function paintBorder(
container.y + i,
container.width - 2 * i,
container.height - 2 * i,
grey,
TEXT_WHITE,
Math.max(0, container.borderRadius - i),
);
} else {
image.drawRect(container.x + i, container.y + i, container.width - 2 * i, container.height - 2 * i, grey);
image.drawRect(container.x + i, container.y + i, container.width - 2 * i, container.height - 2 * i, TEXT_WHITE);
}
}
}
/**
* An opaque bitmap (black pixels cover what's beneath). Updates are converted
* to exactly the container's size (image-pipeline.ts); pixels an extended
* layout carried over from a differently sized container are clipped.
*/
function paintImageContainer(image: GrayImage, container: EvenHubImageContainer): void {
if (!container.pixels || container.pixelsWidth <= 0 || container.pixelsHeight <= 0) return;
const source = new GrayImage(container.pixelsWidth, container.pixelsHeight, 0);
source.pixels.set(container.pixels);
image.bitBlt(source, container.x, container.y, {
width: Math.min(container.pixelsWidth, container.width),
height: Math.min(container.pixelsHeight, container.height),
});
// Stock quirk: raw data smaller than the container tiles/repeats to fill it.
for (let ty = 0; ty < container.height; ty += container.pixelsHeight) {
for (let tx = 0; tx < container.width; tx += container.pixelsWidth) {
image.bitBlt(source, container.x + tx, container.y + ty, {
width: Math.min(container.pixelsWidth, container.width - tx),
height: Math.min(container.pixelsHeight, container.height - ty),
});
}
}
}
function paintListContainer(image: GrayImage, container: EvenHubListContainer, focused: boolean): void {
@@ -189,23 +95,36 @@ function paintListContainer(image: GrayImage, container: EvenHubListContainer, f
paintListItems(image, container, containerFace(container.font), focused);
}
/** Bottom-to-top paint order for a page's containers. */
function paintOrder(containers: EvenHubContainer[]): EvenHubContainer[] {
const anyZ = containers.some((c) => c.zOrderIndex !== undefined);
if (anyZ) {
// Explicit z-order: larger zOrderIndex renders closer to the front, i.e.
// painted later. (Stock requires all-or-nothing + unique values.)
return [...containers].sort((a, b) => (a.zOrderIndex ?? 0) - (b.zOrderIndex ?? 0));
}
// Default (no z-order): image containers sit below lists and text. Cross-type
// order isn't defined by the wire format (separate object arrays), so this is
// our choice, matching stock. Order within each group is preserved.
const images = containers.filter((c) => c.kind === "image");
const rest = containers.filter((c) => c.kind !== "image");
return [...images, ...rest];
}
/** Render a page into a fresh app-viewport image (positioned by the shell). */
export function compositePage(page: EvenHubPage | null, size: { width: number; height: number }, focused: boolean): GrayImage {
const image = new GrayImage(size.width, size.height, 0);
if (!page) return image;
for (const container of paintOrder(page.containers)) {
switch (container.kind) {
case "image": {
if (!container.pixels) break; // Nothing is drawn before the first update.
case "image":
// Text painted so far is deferred draws (firmware-text runs), which
// would otherwise render above this container's raster. The image is
// opaque and covers what's under it, so bake the draws it overlaps
// first; the others, and text painted later, stay deferred.
const box: Rect = { x: container.x, y: container.y, width: container.width, height: container.height };
image.bakeDeferredDrawsWithin(box);
// would otherwise render above this container's raster. Stock puts a
// later image container OVER earlier text, so bake the pending draws
// first; text painted after this container stays deferred (and above).
image.bakeDeferredDrawsInPlace();
paintImageContainer(image, container);
break;
}
case "text":
paintTextContainer(image, container);
break;
+13 -130
View File
@@ -6,7 +6,6 @@
* reads go through loose key matching (the SDK's pickLoose behavior).
*/
import { mergeFontSpecs, parseFontSpec, type EvenHubFontSpec } from "./font-spec";
import { inStockImageRange } from "./image-pipeline";
export type EvenHubTextContainer = {
kind: "text";
@@ -17,8 +16,6 @@ export type EvenHubTextContainer = {
width: number;
height: number;
borderWidth: number;
/** Firmware border color index 0..16; see borderGrey. */
borderColor: number;
borderRadius: number;
paddingLength: number;
isEventCapture: boolean;
@@ -30,11 +27,6 @@ export type EvenHubTextContainer = {
textColor: number | undefined;
/** Extended-layout font (merged with the layout's default); undefined is the stock font. */
font: EvenHubFontSpec | undefined;
/**
* How far the text is scrolled up, in pixels. Host-local like a list's
* selection: stock scrolls an overflowing capture container itself.
*/
scrollY: number;
};
export type EvenHubImageContainer = {
@@ -62,8 +54,6 @@ export type EvenHubListContainer = {
width: number;
height: number;
borderWidth: number;
/** Firmware border color index 0..16; see borderGrey. */
borderColor: number;
borderRadius: number;
paddingLength: number;
isEventCapture: boolean;
@@ -90,7 +80,7 @@ export type EvenHubMenuItem = {
};
export type EvenHubPage = {
/** Lists, then texts, then images, each in declaration order (stock's container list order); z-sorted at paint time. */
/** In declaration order (lists, images, texts); z-sorted at paint time. */
containers: EvenHubContainer[];
/**
* The page's contextual-menu entries (SDK 0.0.14 `menuObject`). Empty when
@@ -102,26 +92,6 @@ export type EvenHubPage = {
/** Stock draws at most a 5px border; wider requests are clamped. */
const MAX_BORDER_WIDTH = 5;
/**
* Stock firmware limits for list and text containers (common_text_create /
* common_list_create clamp to these), from the evenRealities-openCFW
* reconstruction. Extended layouts are exempt: they have their own canvas.
*/
const STOCK_SCREEN_WIDTH = 576;
const STOCK_SCREEN_HEIGHT = 288;
const STOCK_MAX_BORDER_COLOR = 16;
const STOCK_MAX_BORDER_RADIUS = 10;
const STOCK_MAX_PADDING = 32;
const STOCK_MAX_LIST_ITEMS = 20;
/**
* The border color index's grey: indices 0..15 step by 0x11 (0 is black, so a
* border without a color is invisible) and 16 is white.
*/
export function borderGrey(index: number): number {
return index >= 0 && index < 16 ? index * 0x11 : 0xff;
}
/** Firmware brightness levels for text; 4 is the device default. */
const MIN_TEXT_BRIGHTNESS = 0;
const MAX_TEXT_BRIGHTNESS = 4;
@@ -184,11 +154,6 @@ function readBorderWidth(json: Record<string, unknown>): number {
return Math.max(0, Math.min(MAX_BORDER_WIDTH, readNumber(json, "borderWidth", 0)));
}
/** A protobuf field, so an omitted borderColor is 0 (black). */
function readBorderColor(json: Record<string, unknown>): number {
return Math.max(0, readNumber(json, "borderColor", 0));
}
/** A container's font: its own `font` over the layout-wide default. */
function readFont(json: Record<string, unknown>, layoutFont: EvenHubFontSpec | undefined): EvenHubFontSpec | undefined {
return mergeFontSpecs(layoutFont, parseFontSpec(pickLoose(json, "font")));
@@ -204,7 +169,6 @@ function parseTextContainer(json: Record<string, unknown>, layoutFont: EvenHubFo
width: readNumber(json, "width", 0),
height: readNumber(json, "height", 0),
borderWidth: readBorderWidth(json),
borderColor: readBorderColor(json),
borderRadius: readNumber(json, "borderRadius", 0),
paddingLength: readNumber(json, "paddingLength", 0),
isEventCapture: readFlag(json, "isEventCapture"),
@@ -213,7 +177,6 @@ function parseTextContainer(json: Record<string, unknown>, layoutFont: EvenHubFo
content: readString(json, "content", ""),
textColor: readTextBrightness(json),
font: readFont(json, layoutFont),
scrollY: 0,
};
}
@@ -299,7 +262,6 @@ function parseListContainer(json: Record<string, unknown>, layoutFont: EvenHubFo
width: readNumber(json, "width", 0),
height: readNumber(json, "height", 0),
borderWidth: readBorderWidth(json),
borderColor: readBorderColor(json),
borderRadius: readNumber(json, "borderRadius", 0),
paddingLength: readNumber(json, "paddingLength", 0),
isEventCapture: readFlag(json, "isEventCapture"),
@@ -315,8 +277,9 @@ function parseListContainer(json: Record<string, unknown>, layoutFont: EvenHubFo
/**
* Parse a CreateStartUpPageContainer / RebuildPageContainer payload into a
* page. Containers are kept in stock's container-list order: lists, then
* texts, then images, each in declaration order (evenhub_build_container_list).
* page. Containers keep declaration order within and across the three arrays
* (lists, then images, then texts — texts last so info panels and capture
* containers sit above image tiles when no zOrderIndex is given).
*
* The whole page is replaced on rebuild, which is also what gives an omitted
* menuObject its documented meaning: the previous contextual menu goes away.
@@ -329,100 +292,20 @@ export function parsePage(data: Record<string, unknown>): EvenHubPage {
if (Array.isArray(lists)) {
for (const item of lists) containers.push(parseListContainer(asRecord(item), layoutFont));
}
const texts = pickLoose(data, "textObject");
if (Array.isArray(texts)) {
for (const item of texts) containers.push(parseTextContainer(asRecord(item), layoutFont));
}
const images = pickLoose(data, "imageObject");
if (Array.isArray(images)) {
for (const item of images) containers.push(parseImageContainer(asRecord(item)));
}
const texts = pickLoose(data, "textObject");
if (Array.isArray(texts)) {
for (const item of texts) containers.push(parseTextContainer(asRecord(item), layoutFont));
}
return { containers, menuItems: parseMenuItems(data) };
}
/**
* Clamp a stock page's containers the way the firmware does when it creates
* them: positions and sizes to the 576x288 screen (images keep their size,
* which a separate check rejects instead), border color, radius and padding to
* their maxima, and lists to 20 items. Not for extended layouts.
*/
export function clampToStockLimits(page: EvenHubPage): void {
for (const c of page.containers) {
c.x = Math.min(c.x, STOCK_SCREEN_WIDTH);
c.y = Math.min(c.y, STOCK_SCREEN_HEIGHT);
if (c.kind === "image") continue;
c.width = Math.min(c.width, STOCK_SCREEN_WIDTH);
c.height = Math.min(c.height, STOCK_SCREEN_HEIGHT);
c.borderColor = Math.min(c.borderColor, STOCK_MAX_BORDER_COLOR);
c.borderRadius = Math.min(c.borderRadius, STOCK_MAX_BORDER_RADIUS);
c.paddingLength = Math.min(c.paddingLength, STOCK_MAX_PADDING);
if (c.kind === "list" && c.itemNames.length > STOCK_MAX_LIST_ITEMS) {
c.itemNames = c.itemNames.slice(0, STOCK_MAX_LIST_ITEMS);
}
}
}
/** The firmware's fixed protobuf arrays: more containers than this fail to decode. */
const STOCK_MAX_LISTS = 4;
const STOCK_MAX_TEXTS = 8;
const STOCK_MAX_IMAGES = 4;
/** EhStartUpPageCreateResult values for a page the firmware can't build. */
export const PAGE_CREATE_INVALID = 1;
export const PAGE_CREATE_OUT_OF_MEMORY = 3;
/**
* Why the stock firmware would fail to build this page, with the create result
* the app gets, or null when it builds. Too many containers overflow the
* firmware's protobuf arrays, so decoding fails (it replies with the code the
* phone app maps to outOfMemory). An image container outside 20..288 x 20..144
* fails to create (invalid container).
*/
export function stockPageFailure(page: EvenHubPage): { code: number; reason: string } | null {
const count = (kind: EvenHubContainer["kind"]) => page.containers.filter((c) => c.kind === kind).length;
const [lists, texts, images] = [count("list"), count("text"), count("image")];
if (lists > STOCK_MAX_LISTS || texts > STOCK_MAX_TEXTS || images > STOCK_MAX_IMAGES) {
return {
code: PAGE_CREATE_OUT_OF_MEMORY,
reason: `${lists} list, ${texts} text and ${images} image containers (at most ${STOCK_MAX_LISTS}, ${STOCK_MAX_TEXTS} and ${STOCK_MAX_IMAGES})`,
};
}
for (const c of page.containers) {
if (c.kind === "image" && !inStockImageRange(c.width, c.height)) {
return { code: PAGE_CREATE_INVALID, reason: `image container ${JSON.stringify(c.name)} is ${c.width}x${c.height}, outside 20..288 x 20..144` };
}
}
return null;
}
/**
* Bottom-to-top paint order, as stock stacks its containers: the container
* list order (lists, texts, images), stably sorted by zOrderIndex (larger is
* closer to the front) only when every container has one and there are at
* least two. So without z-order, images are drawn over text.
*/
export function paintOrder(containers: readonly EvenHubContainer[]): EvenHubContainer[] {
const allHaveZ = containers.every((c) => c.zOrderIndex !== undefined);
if (!allHaveZ || containers.length < 2) return [...containers];
return [...containers].sort((a, b) => (a.zOrderIndex ?? 0) - (b.zOrderIndex ?? 0));
}
/**
* The container that owns gesture events. Stock binds the first list or text
* container with isEventCapture in paint order and ignores any later ones.
*/
export function eventCaptureContainer(page: EvenHubPage): EvenHubTextContainer | EvenHubListContainer | undefined {
for (const c of paintOrder(page.containers)) {
if (c.kind !== "image" && c.isEventCapture) return c;
}
return undefined;
}
/**
* The container stock resolves an id to: the first one with that id in paint
* order, whatever its kind, so an update aimed at a later container that
* shares the id never reaches it.
*/
export function containerById(page: EvenHubPage | null, id: number): EvenHubContainer | undefined {
return page ? paintOrder(page.containers).find((c) => c.id === id) : undefined;
/** The container that owns gesture events (exactly one per valid page). */
export function eventCaptureContainer(page: EvenHubPage): EvenHubContainer | undefined {
return page.containers.find(
(container) => container.kind !== "image" && container.isEventCapture,
);
}
@@ -1,65 +0,0 @@
import { wrapText } from "../../graphics/textwrap";
import { type LayerContext } from "../../ui/layers";
import { lineStep } from "../../ui/metrics";
import { ScrollingDialogLayer, type DialogBody, type DialogRow } from "./scrolling-dialog";
/** Wider than the permission dialog: the RayNeo iO's window is short, so fewer, longer lines fit better. */
const DIALOG_WIDTH = 420;
/**
* Shown over an EvenHub app the first time it runs with a RayNeo iO paired
* (see glasses-compat.ts): the app is made for Even Realities glasses and
* won't display correctly on the iO's screen. The app starts underneath.
* Continue (or double-click) dismisses the warning; Close app closes the
* app's window. Either answer counts as having seen it; a window closed with
* the warning still up shows it again next time.
*/
export class EvenHubDisplayWarningLayer extends ScrollingDialogLayer {
constructor(
private readonly appName: string,
private readonly onAnswered: () => void,
private readonly closeApp: () => void,
) {
super(DIALOG_WIDTH);
}
protected bodyRows({ image, font, left, textWidth }: DialogBody): DialogRow[] {
const step = lineStep(font);
const rows: DialogRow[] = [];
for (const line of wrapText(font, "Compatibility warning", textWidth)) {
rows.push({ height: step + 2, draw: (top) => image.drawText(font, left, top, line, 235) });
}
rows[rows.length - 1]!.height += 6;
const message =
`${this.appName} is an EvenHub app, made for Even Realities glasses. It will not display correctly ` +
"on the RayNeo iO, because the iO's screen has a different resolution.";
// One row per wrapped line, so a large font scrolls line by line.
const lines = wrapText(font, message, textWidth);
for (let index = 0; index < lines.length; index++) {
rows.push({
height: step + (index === lines.length - 1 ? 8 : 0),
draw: (top) => image.drawText(font, left, top, lines[index]!, 190),
});
}
return rows;
}
protected actions(): string[] {
return ["Continue", "Close app"];
}
protected activate(action: string, ctx: LayerContext): void {
this.close(ctx, () => {
this.onAnswered();
if (action === "Close app") this.closeApp();
});
}
protected backedOut(): void {
this.onAnswered();
}
protected abandoned(): void {
// Not answered: the next launch shows it again.
}
}
-13
View File
@@ -16,8 +16,6 @@ import { shell } from "../../ui/shell/shell";
import { makeImageWindowIcon, windowIcon } from "../../ui/shell/chrome-layer";
import { EvenHubSession } from "./session";
import { renderInstalledEvenHubIcon } from "./installed-apps";
import { markDisplayWarningShown, needsDisplayWarning } from "./glasses-compat";
import { EvenHubDisplayWarningLayer } from "./display-warning-dialog";
class EvenHubAppLayer implements Layer {
/**
@@ -151,16 +149,5 @@ export function createEvenHubWindow(
session.setScreenOn(on);
};
// The app's first run with a RayNeo iO paired: warn, over the app itself,
// that it won't display correctly there.
const packageId = session.manifest.packageId;
if (needsDisplayWarning(packageId)) {
created.stack.push(new EvenHubDisplayWarningLayer(
session.manifest.name,
() => markDisplayWarningShown(packageId),
() => shell.closeWindow(windowId),
));
}
return created;
}
-38
View File
@@ -1,38 +0,0 @@
/**
* EvenHub with glasses that aren't Even Realities': with a RayNeo iO paired,
* the store is off (it is Even's, and its apps are made for their glasses),
* while apps sideloaded through Files or the Developer app still run, after a
* warning the first time each one launches that it won't display correctly
* (EvenHub apps lay out for the G2's 576x288 display; the iO's is 540x220).
*/
import { glassesTypeSetting } from "../../ui/dashboard-settings";
import { getStringSetting, setStringSetting } from "../../native/settings-store";
/** The package IDs whose RayNeo iO display warning has been answered, as a JSON array. */
const DISPLAY_WARNED_KEY = "evenhub.rayneoDisplayWarned";
/** Whether the EvenHub store can be used: with Even Realities glasses paired, not a RayNeo iO. */
export function isEvenHubStoreAvailable(): boolean {
return glassesTypeSetting.get() !== "rayneo-io";
}
/** Whether launching this package should warn that it won't display correctly on these glasses. */
export function needsDisplayWarning(packageId: string): boolean {
return glassesTypeSetting.get() === "rayneo-io" && !displayWarnedPackages().includes(packageId);
}
/** The user answered the package's display warning: don't show it again. */
export function markDisplayWarningShown(packageId: string): void {
const warned = displayWarnedPackages();
if (warned.includes(packageId)) return;
setStringSetting(DISPLAY_WARNED_KEY, JSON.stringify([...warned, packageId]));
}
function displayWarnedPackages(): string[] {
try {
const parsed: unknown = JSON.parse(getStringSetting(DISPLAY_WARNED_KEY, "[]"));
return Array.isArray(parsed) ? parsed.filter((id): id is string => typeof id === "string") : [];
} catch {
return [];
}
}
-247
View File
@@ -1,247 +0,0 @@
/**
* updateImageRawData the way stock turns an app's imageData into the 4-bit
* bitmap the glasses show. The Even phone app does the conversion
* (EvenHubAppService.updateImageRawData → _prepare / _prepareLegacyRaw, read
* from its blutter decompilation); the glasses firmware then shows the result
* only if it is exactly the container's size (common_image_container_set_data).
*
* - imageData must be a JSON array of integers; a base64 string or an object
* (a typed array that skipped the SDK's toJson) fails as sendFailed.
* - PNG, JPEG, WebP, GIF and BMP are decoded, scaled to cover the container
* (aspect-fill, centre crop, nearest neighbour) and converted to grey with
* alpha ignored.
* - Anything else is raw pixels, accepted only with compressMode 2 (which the
* SDK sends since 0.0.12) and exactly the container's size: one byte per
* pixel (values up to 15 are already 4-bit levels) or 4-bit packed rows.
*
* Native-free so it runs under Node; JPEG/WebP/GIF decoding is injected.
*/
const UPNG = require("upng-js");
/** EhImageUpdateResult, as the SDK reads the host's integer. */
export const IMAGE_SUCCESS = 0;
export const IMAGE_EXCEPTION = 1;
export const IMAGE_SIZE_INVALID = 2;
export const IMAGE_TO_GRAY4_FAILED = 3;
export const IMAGE_SEND_FAILED = 4;
/** Stock image containers are 20..288 wide and 20..144 high. */
export const STOCK_IMAGE_MIN_SIZE = 20;
export const STOCK_IMAGE_MAX_WIDTH = 288;
export const STOCK_IMAGE_MAX_HEIGHT = 144;
/** The compressMode the SDK sends, and the phone app requires for raw pixels. */
const RAW_COMPRESS_MODE = 2;
export function inStockImageRange(width: number, height: number): boolean {
return width >= STOCK_IMAGE_MIN_SIZE && width <= STOCK_IMAGE_MAX_WIDTH &&
height >= STOCK_IMAGE_MIN_SIZE && height <= STOCK_IMAGE_MAX_HEIGHT;
}
export type ImageFormat = "png" | "jpeg" | "webp" | "gif" | "bmp" | "raw";
/** Format by magic bytes, as the phone app's EvenHubImageFormatDetector. */
export function detectImageFormat(b: Uint8Array): ImageFormat {
const at = (i: number, ...values: number[]) => values.every((v, j) => b[i + j] === v);
if (at(0, 0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a)) return "png";
if (at(0, 0xff, 0xd8, 0xff)) return "jpeg";
if (at(0, 0x52, 0x49, 0x46, 0x46) && at(8, 0x57, 0x45, 0x42, 0x50)) return "webp";
if (at(0, 0x47, 0x49, 0x46, 0x38, 0x37, 0x61) || at(0, 0x47, 0x49, 0x46, 0x38, 0x39, 0x61)) return "gif";
if (at(0, 0x42, 0x4d)) return "bmp";
return "raw";
}
/** Decoded pixels, RGBA, not premultiplied. */
export type RgbaImage = { width: number; height: number; rgba: Uint8Array };
export type NativeImageDecoder = (bytes: Uint8Array) => RgbaImage | null;
/** 8-bit grey from RGB (alpha ignored), then the nearest of 16 levels. */
export function grayLevel(r: number, g: number, b: number): number {
const g8 = Math.floor((299 * r + 587 * g + 114 * b + 500) / 1000);
return Math.min(15, Math.max(0, Math.floor((g8 * 15 + 127) / 255)));
}
export function rgbaToLevels(image: RgbaImage): Uint8Array {
const levels = new Uint8Array(image.width * image.height);
for (let i = 0; i < levels.length; i++) {
const o = i * 4;
levels[i] = grayLevel(image.rgba[o]!, image.rgba[o + 1]!, image.rgba[o + 2]!);
}
return levels;
}
/**
* Scale to exactly width x height covering it (aspect-fill), cropping the
* overflow equally on both sides, sampling the nearest source pixel centre.
*/
export function resizeRgbaCover(image: RgbaImage, width: number, height: number): RgbaImage {
const scale = Math.max(width / image.width, height / image.height);
const offX = (image.width - width / scale) / 2;
const offY = (image.height - height / scale) / 2;
const rgba = new Uint8Array(width * height * 4);
for (let y = 0; y < height; y++) {
const sy = Math.min(image.height - 1, Math.max(0, Math.floor(offY + (y + 0.5) / scale)));
for (let x = 0; x < width; x++) {
const sx = Math.min(image.width - 1, Math.max(0, Math.floor(offX + (x + 0.5) / scale)));
const from = (sy * image.width + sx) * 4;
rgba.set(image.rgba.subarray(from, from + 4), (y * width + x) * 4);
}
}
return { width, height, rgba };
}
function decodePng(bytes: Uint8Array): RgbaImage | null {
try {
const decoded = UPNG.decode(bytes.buffer.slice(bytes.byteOffset, bytes.byteOffset + bytes.byteLength));
const frames = UPNG.toRGBA8(decoded) as ArrayBuffer[];
if (!frames.length) return null;
return { width: decoded.width, height: decoded.height, rgba: new Uint8Array(frames[0]!) };
} catch {
return null;
}
}
/**
* Uncompressed Windows BMP ('BM', BI_RGB): 1/4/8-bit paletted and 24/32-bit,
* top-down or bottom-up. Null for other variants (the native decoder may still
* read them). EvenChess sends 1-bit BMPs for full-board refreshes.
*/
export function decodeBmp(bytes: Uint8Array): RgbaImage | null {
if (bytes.length < 54 || bytes[0] !== 0x42 || bytes[1] !== 0x4d) return null;
const u16 = (o: number) => bytes[o]! | (bytes[o + 1]! << 8);
const u32 = (o: number) => (bytes[o]! | (bytes[o + 1]! << 8) | (bytes[o + 2]! << 16) | (bytes[o + 3]! << 24)) >>> 0;
const i32 = (o: number) => bytes[o]! | (bytes[o + 1]! << 8) | (bytes[o + 2]! << 16) | (bytes[o + 3]! << 24);
const pixelOffset = u32(10);
const dibSize = u32(14);
const width = i32(18);
const heightRaw = i32(22);
const bitCount = u16(28);
if (u32(30) !== 0) return null; // only BI_RGB
if (width <= 0 || width > 4096) return null;
const topDown = heightRaw < 0;
const height = Math.abs(heightRaw);
if (height === 0 || height > 4096) return null;
if (![1, 4, 8, 24, 32].includes(bitCount)) return null;
// Palette entries are 4 bytes, BGRx.
let palette: Uint8Array | null = null;
if (bitCount <= 8) {
let count = u32(46);
if (count === 0 || count > 1 << bitCount) count = 1 << bitCount;
palette = new Uint8Array(count * 3);
for (let i = 0; i < count; i++) {
const o = 14 + dibSize + i * 4;
if (o + 2 >= bytes.length) break;
palette.set([bytes[o + 2]!, bytes[o + 1]!, bytes[o]!], i * 3);
}
}
const stride = ((width * bitCount + 31) >> 5) << 2;
const rgba = new Uint8Array(width * height * 4);
for (let y = 0; y < height; y++) {
const row = pixelOffset + (topDown ? y : height - 1 - y) * stride;
if (row >= bytes.length) break;
for (let x = 0; x < width; x++) {
let r: number, g: number, b: number;
if (palette) {
let index: number;
if (bitCount === 1) index = ((bytes[row + (x >> 3)] ?? 0) >> (7 - (x & 7))) & 1;
else if (bitCount === 4) index = (x & 1) === 0 ? (bytes[row + (x >> 1)] ?? 0) >> 4 : (bytes[row + (x >> 1)] ?? 0) & 0x0f;
else index = bytes[row + x] ?? 0;
if (index * 3 >= palette.length) index = 0;
[r, g, b] = [palette[index * 3]!, palette[index * 3 + 1]!, palette[index * 3 + 2]!];
} else {
const o = row + x * (bitCount / 8);
[r, g, b] = [bytes[o + 2] ?? 0, bytes[o + 1] ?? 0, bytes[o] ?? 0];
}
rgba.set([r, g, b, 255], (y * width + x) * 4);
}
}
return { width, height, rgba };
}
/** imageData as bytes: a JSON array of integers only (Uint8List.fromList keeps the low 8 bits). */
function imageDataBytes(raw: unknown): Uint8Array | null {
if (raw === undefined || raw === null) return new Uint8Array(0);
if (!Array.isArray(raw)) return null;
const out = new Uint8Array(raw.length);
for (let i = 0; i < raw.length; i++) {
const value: unknown = raw[i];
if (typeof value !== "number" || !Number.isInteger(value)) return null;
out[i] = value & 0xff;
}
return out;
}
export type PreparedImage = {
result: number;
/** 4-bit levels (0..15), row-major, when result is success. */
levels: Uint8Array | null;
width: number;
height: number;
format: ImageFormat | null;
/** Why it failed, for the debug log. */
reason: string;
};
export type ImageRequest = {
imageData: unknown;
compressMode: number;
/** width/height sent with the update; when both are positive they replace the container's. */
requestWidth: number;
requestHeight: number;
/** The image container's size, looked up by id or name; null when none matched. */
geometry: { width: number; height: number } | null;
decodeNative: NativeImageDecoder;
/** Stock pages limit image sizes; Faceclaw's extended layouts don't. */
stockLimits: boolean;
};
/** Convert an update's imageData to 4-bit levels the way the phone app does. */
export function prepareImageUpdate(request: ImageRequest): PreparedImage {
const fail = (result: number, reason: string, format: ImageFormat | null = null): PreparedImage =>
({ result, levels: null, width: 0, height: 0, format, reason });
const bytes = imageDataBytes(request.imageData);
if (!bytes) return fail(IMAGE_SEND_FAILED, "imageData is not an array of integers");
const inRange = (w: number, h: number) => (request.stockLimits ? inStockImageRange(w, h) : w > 0 && h > 0);
const [width, height] = request.requestWidth > 0 && request.requestHeight > 0
? [request.requestWidth, request.requestHeight]
: [request.geometry?.width ?? 0, request.geometry?.height ?? 0];
const format = detectImageFormat(bytes);
if (format === "raw") {
if (request.stockLimits && request.compressMode !== RAW_COMPRESS_MODE) {
return fail(IMAGE_EXCEPTION, `raw pixels need compressMode ${RAW_COMPRESS_MODE}`, format);
}
if (!inRange(width, height)) return fail(IMAGE_SIZE_INVALID, `raw size ${width}x${height} out of range`, format);
const levels = new Uint8Array(width * height);
const packedStride = Math.ceil(width / 2);
if (bytes.length === width * height) {
for (let i = 0; i < levels.length; i++) {
const v = bytes[i]!;
levels[i] = v <= 15 ? v : Math.floor((v * 15 + 127) / 255);
}
} else if (bytes.length === packedStride * height) {
for (let y = 0; y < height; y++) {
for (let x = 0; x < width; x++) {
const byte = bytes[y * packedStride + (x >> 1)]!;
levels[y * width + x] = (x & 1) === 0 ? byte >> 4 : byte & 0x0f;
}
}
} else {
return fail(IMAGE_EXCEPTION, `raw length ${bytes.length} fits neither ${width}x${height} 8-bit nor 4-bit`, format);
}
return { result: IMAGE_SUCCESS, levels, width, height, format, reason: "" };
}
let decoded = format === "png" ? decodePng(bytes) : format === "bmp" ? decodeBmp(bytes) : null;
decoded ??= request.decodeNative(bytes);
if (!decoded || decoded.width <= 0 || decoded.height <= 0) return fail(IMAGE_EXCEPTION, `${format} decode failed`, format);
if (inRange(width, height) && (decoded.width !== width || decoded.height !== height)) {
decoded = resizeRgbaCover(decoded, width, height);
}
if (!inRange(decoded.width, decoded.height)) {
return fail(IMAGE_SIZE_INVALID, `${decoded.width}x${decoded.height} out of range`, format);
}
return { result: IMAGE_SUCCESS, levels: rgbaToLevels(decoded), width: decoded.width, height: decoded.height, format, reason: "" };
}
+1 -14
View File
@@ -5,9 +5,6 @@
* a phone WebView (its runtime + phone UI) and a 576x288 glasses window
* composited by Faceclaw instead of the stock firmware.
*
* With a RayNeo iO paired the store is unavailable and sideloaded apps warn
* on first run that they won't display correctly; see glasses-compat.ts.
*
* Design notes: notes/evenhub_compatibility.txt.
*/
import { type AppContext, type AppDefinition } from "../app-definition";
@@ -18,7 +15,6 @@ import { createInProcessWindow, type InProcessWindow } from "../../ui/shell/in-p
import { renderEvenRealitiesLogo } from "../../graphics/even-realities-logo";
import { makeImageWindowIcon, windowIcon } from "../../ui/shell/chrome-layer";
import { type InstalledEvenHubApp } from "./installed-apps";
import { isEvenHubStoreAvailable } from "./glasses-compat";
const EVENHUB_STORE_WINDOW_ID = "evenhub:store";
const EVENHUB_STORE_SURFACE_ID = "window:evenhub:store";
@@ -108,17 +104,8 @@ const evenhubApp: AppDefinition = {
title: "EvenHub",
icon: "package",
renderIcon: renderEvenRealitiesLogo,
// Off the launcher with a RayNeo iO paired. A store window that opens
// anyway (restored from before a re-pair, or the launcher's route for an
// installed app whose package is missing) says why instead of loading.
isSupported: isEvenHubStoreAvailable,
launch: async (ctx) => {
const { window } = await ensureStoreWindow(ctx);
if (!isEvenHubStoreAvailable()) {
// Store pages opened while Even glasses were paired would cover the notice.
window.stack.clearToBase();
window.requestRender();
}
await ensureStoreWindow(ctx);
},
};
-181
View File
@@ -1,181 +0,0 @@
/**
* Debug-log descriptions of EvenHub page layouts and container updates.
* session.ts sends them to logcat at debug level (tag FaceclawEvenHubLayout).
*
* "note:" lines point out page quirks that change what stock (and so we) do
* with it: shared ids, missing or extra capture containers, a wrong
* containerTotalNum.
*/
import {
paintOrder,
readOptionalNumber,
type EvenHubContainer,
type EvenHubListContainer,
type EvenHubPage,
type EvenHubTextContainer,
} from "./containers";
import type { PreparedImage } from "./image-pipeline";
const CONTENT_PREVIEW_CHARS = 40;
const RAW_STRING_CHARS = 120;
const RAW_ARRAY_ITEMS = 32;
function label(c: EvenHubContainer): string {
return `${c.kind}#${c.id} ${JSON.stringify(c.name)}`;
}
function preview(text: string): string {
return JSON.stringify(text.length > CONTENT_PREVIEW_CHARS ? `${text.slice(0, CONTENT_PREVIEW_CHARS)}…` : text);
}
function decoration(c: EvenHubTextContainer | EvenHubListContainer): string {
let out = "";
if (c.borderWidth > 0) out += ` border=${c.borderWidth}c${c.borderColor}${c.borderRadius > 0 ? `r${c.borderRadius}` : ""}`;
if (c.paddingLength > 0) out += ` pad=${c.paddingLength}`;
if (c.isEventCapture) out += " capture";
if (c.font) out += " font";
return out;
}
function describeContainer(c: EvenHubContainer): string {
const z = c.zOrderIndex === undefined ? "" : ` z=${c.zOrderIndex}`;
const head = `${label(c)} @${c.x},${c.y} ${c.width}x${c.height}${z}`;
switch (c.kind) {
case "image":
return `${head} ${c.pixels ? `pixels=${c.pixelsWidth}x${c.pixelsHeight}` : "no pixels yet"}`;
case "text": {
const color = c.textColor === undefined ? "" : ` color=${c.textColor}`;
return `${head}${decoration(c)}${color} len=${c.content.length} ${preview(c.content)}`;
}
case "list":
return `${head}${decoration(c)} items=${c.itemNames.length} itemWidth=${c.itemWidth}${c.selectBorder ? " selectBorder" : ""} selected=${c.selectedIndex}`;
}
}
/** Quirks of a page that change how stock treats it. */
export function pageNotes(page: EvenHubPage, declaredTotal?: number): string[] {
const notes: string[] = [];
const ordered = paintOrder(page.containers);
const byId = new Map<number, EvenHubContainer[]>();
for (const c of ordered) byId.set(c.id, [...(byId.get(c.id) ?? []), c]);
byId.forEach((shared, id) => {
if (shared.length > 1) {
notes.push(`id ${id} is shared by ${shared.map(label).join(", ")}; updates and events resolve to ${label(shared[0]!)} only`);
}
});
const captures = ordered.filter((c) => c.kind !== "image" && c.isEventCapture);
if (captures.length === 0) notes.push("no event-capture container, so the app gets no input");
else if (captures.length > 1) notes.push(`${captures.length} event-capture containers; only the first, ${label(captures[0]!)}, gets input`);
if (declaredTotal !== undefined && declaredTotal !== page.containers.length) {
notes.push(`containerTotalNum is ${declaredTotal} but ${page.containers.length} containers were sent (nothing checks it)`);
}
return notes;
}
/** The payload, as compact JSON with long strings and arrays cut short. */
function rawJson(data: Record<string, unknown>): string {
try {
return JSON.stringify(data, (_key, value: unknown) => {
if (typeof value === "string" && value.length > RAW_STRING_CHARS) {
return `${value.slice(0, RAW_STRING_CHARS)}…(${value.length} chars)`;
}
if (Array.isArray(value) && value.length > RAW_ARRAY_ITEMS) {
return [...value.slice(0, RAW_ARRAY_ITEMS), `…(${value.length} items)`];
}
return value;
});
} catch (error) {
return `(unserializable: ${String(error)})`;
}
}
/** A create/rebuild/extension layout: one line per container in paint order, then notes, then the raw payload. */
export function describePage(method: string, page: EvenHubPage, data: Record<string, unknown>): string[] {
const containers = page.containers;
const count = (kind: EvenHubContainer["kind"]) => containers.filter((c) => c.kind === kind).length;
const declaredTotal = readOptionalNumber(data, "containerTotalNum");
const total = declaredTotal === undefined ? "" : `, containerTotalNum=${declaredTotal}`;
const menu = page.menuItems.length > 0 ? `, ${page.menuItems.length} menu items` : "";
const lines = [
`${method}: ${containers.length} containers (${count("list")} list, ${count("text")} text, ${count("image")} image)` +
`${total}${menu}; painted bottom to top:`,
];
paintOrder(containers).forEach((c, i) => lines.push(` ${i}: ${describeContainer(c)}`));
for (const note of pageNotes(page, declaredTotal)) lines.push(` note: ${note}`);
lines.push(` raw: ${rawJson(data)}`);
return lines;
}
function payloadKind(raw: unknown): string {
if (raw === undefined || raw === null) return "missing";
if (Array.isArray(raw)) return `number[${raw.length}]`;
if (typeof raw === "string") return `string(${raw.length} chars)`;
if (typeof raw === "object") return `object(${Object.keys(raw).length} keys)`;
return typeof raw;
}
/** Lit-pixel count and bounding box of 4-bit levels, so a blank "hide this" image is recognizable. */
function litSummary(levels: Uint8Array, width: number): string {
let lit = 0;
let max = 0;
let x0 = width;
let y0 = Infinity;
let x1 = -1;
let y1 = -1;
for (let i = 0; i < levels.length; i++) {
const level = levels[i]!;
if (level === 0) continue;
lit++;
if (level > max) max = level;
const x = i % width;
const y = Math.floor(i / width);
if (x < x0) x0 = x;
if (x > x1) x1 = x;
if (y < y0) y0 = y;
if (y > y1) y1 = y;
}
return lit === 0 ? "blank" : `lit=${lit}px max=${max}/15 bbox=${x0},${y0}-${x1},${y1}`;
}
export type ImageUpdateDebug = {
sentId: number;
sentName: string;
raw: unknown;
/** The container the id resolves to. */
target: EvenHubContainer | undefined;
prepared: PreparedImage;
/** Whether the container now shows the update. */
shown: boolean;
/** EhImageUpdateResult returned to the app. */
result: number;
};
export function describeImageUpdate(u: ImageUpdateDebug): string[] {
const parts = [`updateImageRawData id=${u.sentId} ${JSON.stringify(u.sentName)}`];
parts.push(`-> ${u.target ? label(u.target) : "no container"}`);
parts.push(`payload=${payloadKind(u.raw)}`);
if (u.prepared.format) parts.push(`format=${u.prepared.format}`);
if (u.prepared.levels) parts.push(`${u.prepared.width}x${u.prepared.height} ${litSummary(u.prepared.levels, u.prepared.width)}`);
parts.push(`result=${u.result}`, u.shown ? "shown" : "not shown");
const lines = [parts.join(" ")];
if (u.prepared.reason) lines.push(` ${u.prepared.reason}`);
if (u.prepared.result === 0 && u.target?.kind !== "image") {
lines.push(` the id ${u.target ? `resolves to ${label(u.target)}` : "matches no container"}, so the glasses refuse the transfer`);
} else if (u.prepared.result === 0 && !u.shown && u.target) {
lines.push(` ${u.prepared.width}x${u.prepared.height} isn't the container's ${u.target.width}x${u.target.height}, so the glasses drop it`);
}
return lines;
}
export function describeTextUpgrade(
sentId: number,
sentName: string,
container: EvenHubTextContainer | null,
offset: number | undefined,
sentLength: number,
): string {
if (!container) return `textContainerUpgrade id=${sentId} ${JSON.stringify(sentName)}: no container with that id`;
const at = offset === undefined ? "" : ` offset=${offset}`;
const color = container.textColor === undefined ? "" : ` color=${container.textColor}`;
return `textContainerUpgrade ${label(container)}${at} sent=${sentLength} chars -> len=${container.content.length}${color} ${preview(container.content)}`;
}
+7 -7
View File
@@ -6,8 +6,8 @@
*
* Glasses-first: launching does NOT take over the phone screen. The phone
* stays on the Faceclaw dashboard; an app's phone UI is shown on demand
* (window menu -> "Show phone UI", or the app-icon button in the bar under the
* phone's mirror while the app is foreground on the glasses), overlaying the
* (window menu -> "Show phone UI", or the app-icon button in the phone's
* action bar while the app is foreground on the glasses), overlaying the
* dashboard, and the Back button or the overlay's close button returns to it.
* Apps are kept alive until explicitly closed (no memory eviction yet).
*/
@@ -40,14 +40,14 @@ type RunningApp = {
packageId: string;
session: EvenHubSession;
webView: EvenHubWebView;
/** The app's icon for the phone's button bar, rendered on first use. */
/** The app's icon for the phone action bar, rendered on first use. */
phoneIcon?: ImageSource | null;
};
/** The button under the phone's mirror that opens the foreground app's phone UI. */
/** The phone action bar's button for opening the foreground app's phone UI. */
export type PhoneUiButton = { windowId: string; title: string; icon: ImageSource | null };
/** Pixel size the button's icon is rendered at (~21dp at xxhdpi; shown at 20dp). */
/** Pixel size the action-bar icon is rendered at (~21dp at xxhdpi). */
const PHONE_ICON_SIZE = 63;
const running = new Map<string, RunningApp>();
@@ -244,7 +244,7 @@ export function hidePhone(): void {
/**
* The foreground glasses window, when it is an EvenHub app whose phone UI
* isn't showing: the bar under the phone's mirror then offers a button to show it.
* isn't showing: the phone action bar then offers a button to show it.
*/
export function phoneUiButton(): PhoneUiButton | null {
const window = shell.foregroundWindow();
@@ -252,7 +252,7 @@ export function phoneUiButton(): PhoneUiButton | null {
if (!window || !app || phoneShownWindowId === app.windowId) return null;
if (app.phoneIcon === undefined) {
// The same artwork as the glasses sidebar (installed icon, else the
// generic package icon), drawn white; the button's stylesheet tints it.
// generic package icon), drawn white.
const image = new GrayImage(PHONE_ICON_SIZE, PHONE_ICON_SIZE, 0);
window.drawIcon(image, 0, 0, PHONE_ICON_SIZE);
app.phoneIcon = grayImageToWhiteIconSource(image);
-141
View File
@@ -1,141 +0,0 @@
/**
* The checks the Even phone app applies to createStartUpPageContainer,
* rebuildPageContainer and textContainerUpgrade payloads before it sends
* anything to the glasses (read from its blutter decompilation:
* EvenHubProtoJsonMapper.validateTextBrightness, mapProtoJsonKeys and
* protobuf.dart's tag-keyed JSON parsing):
*
* - every key must be one the message defines (a numeric key passes as a raw
* protobuf tag, which we then ignore);
* - numeric fields take an integer or a numeric string — not a boolean, a
* fraction or null; string fields take a string; repeated fields an array;
* - any key named textColor, anywhere, must be an integer from 0 to 4.
*
* A failure means the call fails without reaching the glasses: create
* returns invalid (1), rebuild and textContainerUpgrade return false.
* Faceclaw's extended layouts are not checked; they carry their own keys.
*/
type FieldType = "uint" | "string" | "any" | { list: FieldType } | { message: Schema };
type Schema = Readonly<Record<string, FieldType>>;
const BOX: Schema = {
xPosition: "uint",
yPosition: "uint",
width: "uint",
height: "uint",
containerID: "uint",
containerName: "string",
zOrderIndex: "uint",
};
const DECORATED: Schema = {
...BOX,
borderWidth: "uint",
borderColor: "uint",
borderRadius: "uint",
paddingLength: "uint",
isEventCapture: "uint",
};
const TEXT: Schema = { ...DECORATED, content: "string", textColor: "uint" };
const ITEM_CONTAINER: Schema = {
itemCount: "uint",
itemWidth: "uint",
isItemSelectBorderEn: "uint",
itemName: { list: "string" },
};
const LIST: Schema = { ...DECORATED, itemContainer: { message: ITEM_CONTAINER } };
// The app knows position, subItems and checkedIndex (second-level menus, not
// yet in the SDK); their value types are unconfirmed, so they aren't checked.
const MENU_ITEM: Schema = { itemName: "string", itemID: "uint", position: "any", subItems: "any", checkedIndex: "any" };
const MENU: Schema = { menuItems: { list: { message: MENU_ITEM } } };
const REBUILD: Schema = {
containerTotalNum: "uint",
listObject: { list: { message: LIST } },
textObject: { list: { message: TEXT } },
imageObject: { list: { message: BOX } },
menuObject: { message: MENU },
};
const CREATE: Schema = { ...REBUILD, widgetId: "uint" };
const TEXT_UPGRADE: Schema = {
containerID: "uint",
containerName: "string",
contentOffset: "uint",
contentLength: "uint",
content: "string",
textColor: "uint",
};
const SCHEMAS = { create: CREATE, rebuild: REBUILD, textUpgrade: TEXT_UPGRADE } as const;
/** What Dart's int.parse accepts. */
const INT_STRING = /^\s*[+-]?(?:\d+|0[xX][0-9a-fA-F]+)\s*$/;
function isRecord(value: unknown): value is Record<string, unknown> {
return !!value && typeof value === "object" && !Array.isArray(value);
}
function checkValue(type: FieldType, value: unknown, path: string): string | null {
if (type === "any") return null;
if (value === null || value === undefined) return `${path} is null`;
if (type === "uint") {
if (typeof value === "number" ? Number.isInteger(value) : typeof value === "string" && INT_STRING.test(value)) return null;
return `${path} must be an integer, not ${JSON.stringify(value)}`;
}
if (type === "string") return typeof value === "string" ? null : `${path} must be a string, not ${JSON.stringify(value)}`;
if ("list" in type) {
if (!Array.isArray(value)) return `${path} must be an array`;
for (let i = 0; i < value.length; i++) {
const error = checkValue(type.list, value[i], `${path}[${i}]`);
if (error) return error;
}
return null;
}
return isRecord(value) ? checkMessage(type.message, value, path) : `${path} must be an object`;
}
function checkMessage(schema: Schema, message: Record<string, unknown>, path: string): string | null {
for (const [key, value] of Object.entries(message)) {
const field = schema[key];
if (!field) {
if (INT_STRING.test(key)) continue;
return `unknown key ${path ? `${path}.` : ""}${key}`;
}
const error = checkValue(field, value, path ? `${path}.${key}` : key);
if (error) return error;
}
return null;
}
/** validateTextBrightness: every textColor key, at any depth, holds an integer 0..4. */
function checkTextColors(value: unknown, path: string): string | null {
if (Array.isArray(value)) {
for (let i = 0; i < value.length; i++) {
const error = checkTextColors(value[i], `${path}[${i}]`);
if (error) return error;
}
} else if (isRecord(value)) {
for (const [key, child] of Object.entries(value)) {
const at = path ? `${path}.${key}` : key;
if (key === "textColor" && !(typeof child === "number" && Number.isInteger(child) && child >= 0 && child <= 4)) {
return `${at} must be an integer from 0 to 4, not ${JSON.stringify(child)}`;
}
const error = checkTextColors(child, at);
if (error) return error;
}
}
return null;
}
/** Why the Even app would refuse this payload without sending it, or null if it would send it. */
export function stockPayloadError(kind: keyof typeof SCHEMAS, data: Record<string, unknown>): string | null {
return checkTextColors(data, "") ?? checkMessage(SCHEMAS[kind], data, "");
}
+148 -27
View File
@@ -1,11 +1,17 @@
import { getDefaultSmallFont } from "../../graphics/ui-fonts";
import { GrayImage } from "../../graphics/image";
import { truncateText, wrapText } from "../../graphics/textwrap";
import { type LayerContext } from "../../ui/layers";
import { type InputEvent } from "../../ui/gestures";
import { Layer, type LayerContext, type PaintBelow } from "../../ui/layers";
import { drawListScrollbar, drawSelectionHighlight } from "../../ui/menu";
import { permissionDetail, permissionLabel, type EvenHubPermission } from "./permissions";
import { openPrivacyPolicyOnPhone } from "./privacy-policy";
import { lineStep } from "../../ui/metrics";
import { ScrollingDialogLayer, type DialogBody, type DialogRow } from "./scrolling-dialog";
import { lineStep, listRowHeight } from "../../ui/metrics";
/** Minimum gap between the dialog and the edges of the window it's centered in. */
const DIALOG_MARGIN = 8;
const DIALOG_WIDTH = 320;
const PADDING = 10;
const PERM_GAP = 4;
/**
@@ -14,26 +20,56 @@ const PERM_GAP = 4;
* Cancel (or double-click) backs out. A privacy-policy-only app still reaches
* this dialog so the user can review the policy before allowing first run.
*
* A long list scrolls a page at a time, and Allow stays inert until the
* bottom has been reached (see ScrollingDialogLayer), so the user can't
* Allow without having had the whole list on screen.
* When the list overflows the viewport the dialog scrolls: scroll events move
* through the content row by row, and only once the bottom is fully visible do
* further scrolls move the action selection. The actions are inert until then
* (a click just scrolls), so the user can't Allow without having had the whole
* list on screen.
*/
export class EvenHubPermissionDialogLayer extends ScrollingDialogLayer {
export class EvenHubPermissionDialogLayer implements Layer {
/** Floats in front of the window content, like the context menus. */
readonly depth = 2;
/** Starts on Allow; the optional privacy-policy action is inserted after it. */
private selectedIndex = 0;
/** First content row drawn; rows above it are scrolled off. */
private firstRow = 0;
/** Scroll position at which the last row is fully visible; set by paint. */
private maxFirstRow = 0;
private resolved = false;
constructor(
private readonly appName: string,
private readonly permissions: EvenHubPermission[],
private readonly privacyPolicyUrl: string,
private readonly onConfirm: () => void,
private readonly onCancel: () => void,
) {
super(DIALOG_WIDTH);
) {}
/** Whether the dialog is scrolled to the bottom, making the actions live. */
private atBottom(): boolean {
return this.firstRow >= this.maxFirstRow;
}
protected bodyRows({ image, font, left, textWidth }: DialogBody): DialogRow[] {
paint(ctx: LayerContext, paintBelow: PaintBelow): GrayImage {
const font = getDefaultSmallFont();
const { width: viewportWidth, height: viewportHeight } = ctx.stack.getBaseSize();
const image = paintBelow();
const dialogWidth = Math.min(DIALOG_WIDTH, viewportWidth - 2 * DIALOG_MARGIN);
const dialogX = ((viewportWidth - dialogWidth) / 2) | 0;
const textWidth = dialogWidth - 2 * PADDING - 4;
const left = dialogX + PADDING + 2;
const headerStep = lineStep(font) + 2;
const permLabelStep = lineStep(font) + 1;
const permDetailStep = lineStep(font) - 1;
const rows: DialogRow[] = [];
const actionRowH = listRowHeight(font);
const actions = this.actions();
const focused = ctx.stack.isFocused();
// The dialog body as scrollable rows; each row's height includes the gap
// below it, so scrolling and visibility checks work on whole rows.
type Row = { height: number; draw: (top: number) => void };
const rows: Row[] = [];
const heading = this.permissions.length ? `${this.appName} needs:` : `${this.appName} is ready to run`;
rows.push({
height: headerStep + 6,
@@ -62,28 +98,113 @@ export class EvenHubPermissionDialogLayer extends ScrollingDialogLayer {
});
}
}
return rows;
for (let index = 0; index < actions.length; index++) {
rows.push({
height: actionRowH,
draw: (top) => {
const selected = this.atBottom() && index === this.selectedIndex;
if (selected) {
drawSelectionHighlight(image, dialogX + 12, top, dialogWidth - 24, actionRowH - 1, focused, 8);
}
image.drawText(font, dialogX + 22, top + 3, actions[index]!, selected ? 255 : 200);
},
});
}
const contentHeight = rows.reduce((sum, row) => sum + row.height, 0);
const height = Math.min(contentHeight + 2 * PADDING, viewportHeight - 2 * DIALOG_MARGIN);
const dialogY = ((viewportHeight - height) / 2) | 0;
const visibleHeight = height - 2 * PADDING;
// The bottommost scroll position: the largest suffix of rows that fits.
let maxFirstRow = rows.length;
for (let suffixHeight = 0; maxFirstRow > 0; maxFirstRow--) {
const rowHeight = rows[maxFirstRow - 1]!.height;
if (suffixHeight + rowHeight > visibleHeight) break;
suffixHeight += rowHeight;
}
this.maxFirstRow = maxFirstRow;
this.firstRow = Math.max(0, Math.min(this.firstRow, maxFirstRow));
// Fill 1 (transparent color key is 0), outline for the dialog edge.
image.fillRoundedRect(dialogX, dialogY, dialogWidth, height, 1);
image.drawRoundedRect(dialogX, dialogY, dialogWidth, height, 72);
let y = dialogY + PADDING;
for (let index = this.firstRow; index < rows.length; index++) {
const row = rows[index]!;
if (y + row.height > dialogY + PADDING + visibleHeight) break;
row.draw(y);
y += row.height;
}
if (maxFirstRow > 0) {
drawListScrollbar(
image,
dialogX + dialogWidth - 7,
dialogY + PADDING,
visibleHeight,
this.firstRow,
rows.length - maxFirstRow,
rows.length,
);
}
return image;
}
protected actions(): string[] {
return this.privacyPolicyUrl ? ["Allow", "Privacy policy", "Cancel"] : ["Allow", "Cancel"];
}
protected activate(action: string, ctx: LayerContext): void {
if (action === "Privacy policy") {
openPrivacyPolicyOnPhone(this.privacyPolicyUrl, this.appName);
} else if (action === "Allow") {
this.close(ctx, () => this.onConfirm());
} else {
this.close(ctx, () => this.onCancel());
handleInput(event: InputEvent, ctx: LayerContext): void {
const actions = this.actions();
switch (event.type) {
case "scroll-up":
if (this.atBottom() && this.selectedIndex > 0) {
this.selectedIndex -= 1;
} else if (this.firstRow > 0) {
this.firstRow -= 1;
}
return;
case "scroll-down":
if (!this.atBottom()) {
this.firstRow += 1;
} else {
this.selectedIndex = Math.min(actions.length - 1, this.selectedIndex + 1);
}
return;
case "click":
if (!this.atBottom()) {
this.firstRow += 1;
return;
}
if (actions[this.selectedIndex] === "Privacy policy") {
openPrivacyPolicyOnPhone(this.privacyPolicyUrl, this.appName);
} else {
this.resolve(ctx, actions[this.selectedIndex] === "Allow");
}
return;
case "double-click":
this.resolve(ctx, false);
return;
default:
return;
}
}
protected backedOut(): void {
this.onCancel();
onRemoved(): void {
// Dismissed some other way (window closed, back button): treat as cancel.
if (!this.resolved) {
this.resolved = true;
this.onCancel();
}
}
protected abandoned(): void {
this.onCancel();
private resolve(ctx: LayerContext, confirmed: boolean): void {
if (this.resolved) return;
this.resolved = true;
ctx.stack.pop();
if (confirmed) this.onConfirm();
else this.onCancel();
}
private actions(): string[] {
return this.privacyPolicyUrl ? ["Allow", "Privacy policy", "Cancel"] : ["Allow", "Cancel"];
}
}
-201
View File
@@ -1,201 +0,0 @@
import { getDefaultSmallFont } from "../../graphics/ui-fonts";
import { type GrayImage, type UiFont } from "../../graphics/image";
import { type InputEvent } from "../../ui/gestures";
import { type Layer, type LayerContext, type PaintBelow } from "../../ui/layers";
import { drawListScrollbar, drawSelectionHighlight } from "../../ui/menu";
import { listRowHeight } from "../../ui/metrics";
/** Minimum gap between the dialog and the edges of the window it's centered in. */
const DIALOG_MARGIN = 8;
const PADDING = 10;
/** One row of a dialog's content. Its height includes the gap below it, so scrolling works on whole rows. */
export type DialogRow = { height: number; draw: (top: number) => void };
/** What a dialog's body rows draw with: the canvas, the font, and the text column. */
export type DialogBody = { image: GrayImage; font: UiFont; left: number; textWidth: number };
/**
* A dialog box centered over the window it opens in: body rows, then a
* column of actions, starting on the first. EvenHub's confirmations and
* warnings (permissions, the RayNeo iO display warning) are built on it.
*
* When the content overflows the viewport the dialog scrolls a page at a
* time, keeping one line of overlap (the last line of one page is the first
* of the next), and only once the bottom is fully visible do further scrolls
* move the action selection. The actions are inert until then (a click just
* pages down), so nothing can be chosen without the whole dialog having been
* on screen. Double-click backs out.
*/
export abstract class ScrollingDialogLayer implements Layer {
/** Floats in front of the window content, like the context menus. */
readonly depth = 2;
private selectedIndex = 0;
/** First content row drawn; rows above it are scrolled off. */
private firstRow = 0;
/** Scroll position at which the last row is fully visible; set by paint. */
private maxFirstRow = 0;
/** Heights of the content rows and of the viewport they scroll in, as last painted (for paging). */
private rowHeights: number[] = [];
private visibleHeight = 0;
private resolved = false;
constructor(private readonly maxWidth: number) {}
/** The rows above the actions. */
protected abstract bodyRows(body: DialogBody): DialogRow[];
/** The action labels, top to bottom. */
protected abstract actions(): string[];
/** A click on an action once the actions are live; close() dismisses the dialog with it. */
protected abstract activate(action: string, ctx: LayerContext): void;
/** Double-click: the user backed out of the dialog. */
protected abstract backedOut(): void;
/** The dialog left the stack unanswered (its window closed). */
protected abstract abandoned(): void;
/** Whether the dialog is scrolled to the bottom, making the actions live. */
private atBottom(): boolean {
return this.firstRow >= this.maxFirstRow;
}
paint(ctx: LayerContext, paintBelow: PaintBelow): GrayImage {
const font = getDefaultSmallFont();
const { width: viewportWidth, height: viewportHeight } = ctx.stack.getBaseSize();
const image = paintBelow();
const dialogWidth = Math.min(this.maxWidth, viewportWidth - 2 * DIALOG_MARGIN);
const dialogX = ((viewportWidth - dialogWidth) / 2) | 0;
const textWidth = dialogWidth - 2 * PADDING - 4;
const left = dialogX + PADDING + 2;
const actionRowH = listRowHeight(font);
const actions = this.actions();
const focused = ctx.stack.isFocused();
const rows = this.bodyRows({ image, font, left, textWidth });
for (let index = 0; index < actions.length; index++) {
rows.push({
height: actionRowH,
draw: (top) => {
const selected = this.atBottom() && index === this.selectedIndex;
if (selected) {
drawSelectionHighlight(image, dialogX + 12, top, dialogWidth - 24, actionRowH - 1, focused, 8);
}
image.drawText(font, dialogX + 22, top + 3, actions[index]!, selected ? 255 : 200);
},
});
}
const contentHeight = rows.reduce((sum, row) => sum + row.height, 0);
const height = Math.min(contentHeight + 2 * PADDING, viewportHeight - 2 * DIALOG_MARGIN);
const dialogY = ((viewportHeight - height) / 2) | 0;
const visibleHeight = height - 2 * PADDING;
// The bottommost scroll position: the largest suffix of rows that fits.
let maxFirstRow = rows.length;
for (let suffixHeight = 0; maxFirstRow > 0; maxFirstRow--) {
const rowHeight = rows[maxFirstRow - 1]!.height;
if (suffixHeight + rowHeight > visibleHeight) break;
suffixHeight += rowHeight;
}
this.maxFirstRow = maxFirstRow;
this.firstRow = Math.max(0, Math.min(this.firstRow, maxFirstRow));
this.rowHeights = rows.map((row) => row.height);
this.visibleHeight = visibleHeight;
// Fill 1 (transparent color key is 0), outline for the dialog edge.
image.fillRoundedRect(dialogX, dialogY, dialogWidth, height, 1);
image.drawRoundedRect(dialogX, dialogY, dialogWidth, height, 72);
let y = dialogY + PADDING;
for (let index = this.firstRow; index < rows.length; index++) {
const row = rows[index]!;
if (y + row.height > dialogY + PADDING + visibleHeight) break;
row.draw(y);
y += row.height;
}
if (maxFirstRow > 0) {
drawListScrollbar(
image,
dialogX + dialogWidth - 7,
dialogY + PADDING,
visibleHeight,
this.firstRow,
rows.length - maxFirstRow,
rows.length,
);
}
return image;
}
handleInput(event: InputEvent, ctx: LayerContext): void {
const actions = this.actions();
switch (event.type) {
case "scroll-up":
if (this.atBottom() && this.selectedIndex > 0) {
this.selectedIndex -= 1;
} else if (this.firstRow > 0) {
this.pageUp();
}
return;
case "scroll-down":
if (!this.atBottom()) {
this.pageDown();
} else {
this.selectedIndex = Math.min(actions.length - 1, this.selectedIndex + 1);
}
return;
case "click":
if (!this.atBottom()) {
this.pageDown();
return;
}
this.activate(actions[this.selectedIndex]!, ctx);
return;
case "double-click":
this.close(ctx, () => this.backedOut());
return;
default:
return;
}
}
/** Scroll so the last fully visible row becomes the first (one row of overlap). */
private pageDown(): void {
let y = 0;
let lastVisible = this.firstRow;
for (let index = this.firstRow; index < this.rowHeights.length; index++) {
y += this.rowHeights[index]!;
if (y > this.visibleHeight) break;
lastVisible = index;
}
this.firstRow = Math.min(this.maxFirstRow, Math.max(this.firstRow + 1, lastVisible));
}
/** Scroll so the current first row becomes the last fully visible one (one row of overlap). */
private pageUp(): void {
let first = this.firstRow;
let y = this.rowHeights[first] ?? 0;
while (first > 0 && y + this.rowHeights[first - 1]! <= this.visibleHeight) {
first -= 1;
y += this.rowHeights[first]!;
}
this.firstRow = Math.max(0, Math.min(this.firstRow - 1, first));
}
onRemoved(): void {
// Dismissed some other way (window closed, back button).
if (!this.resolved) {
this.resolved = true;
this.abandoned();
}
}
/** Take the dialog down, then run the answer's effect (once, however it was answered). */
protected close(ctx: LayerContext, then: () => void): void {
if (this.resolved) return;
this.resolved = true;
ctx.stack.pop();
then();
}
}
+219 -191
View File
@@ -38,11 +38,7 @@ import {
import { EvenHubApiKeyDialogLayer } from "./api-key-dialog";
import {
asRecord,
clampToStockLimits,
containerById,
declaredMenuItems,
PAGE_CREATE_INVALID,
stockPageFailure,
eventCaptureContainer,
parsePage,
readNumber,
@@ -54,13 +50,8 @@ import {
type EvenHubContainer,
type EvenHubMenuItem,
type EvenHubPage,
type EvenHubTextContainer,
} from "./containers";
import { compositePage, layoutText } from "./compositor";
import { describeImageUpdate, describePage, describeTextUpgrade } from "./layout-debug";
import { IMAGE_SEND_FAILED, IMAGE_SUCCESS, prepareImageUpdate } from "./image-pipeline";
import { stockPayloadError } from "./payload-validation";
import { decodeImageRgba } from "../../native/image-rgba";
import { compositePage } from "./compositor";
import { stepListSelection } from "./list-menu";
import { type EvenHubManifest } from "./ehpk";
import { permissionsIncludeMicrophone, permissionsInclude } from "./permissions";
@@ -83,15 +74,7 @@ import { parseWindowIcon, WindowIconRenderer } from "./window-icon";
import { availableFontFamilies, containerFace, measureContainerText } from "./fonts";
import { parseFontSpec } from "./font-spec";
/**
* Page layouts and container updates go to logcat at debug level (see
* layout-debug.ts): `adb logcat -s FaceclawEvenHubLayout:D`. Android only.
*/
const LAYOUT_LOG_TAG = "FaceclawEvenHubLayout";
const LAYOUT_LOG_ENABLED = !!global.isAndroid;
/** Logcat truncates entries around 4 KB; split longer lines below that. */
const LOGCAT_CHUNK_CHARS = 3000;
const UPNG = require("upng-js");
/** OsEventTypeList values (PB ordinals). */
const CLICK_EVENT = 0;
@@ -100,20 +83,12 @@ const SCROLL_BOTTOM_EVENT = 2;
const DOUBLE_CLICK_EVENT = 3;
const FOREGROUND_ENTER_EVENT = 4;
const FOREGROUND_EXIT_EVENT = 5;
const ABNORMAL_EXIT_EVENT = 6;
const SYSTEM_EXIT_EVENT = 7;
const IMU_DATA_REPORT_EVENT = 8;
/** SDK 0.0.14: the press gesture, delivered like any other list/text/sys event. */
const LONG_PRESS_EVENT = 9;
const LONG_PRESS_RELEASE_EVENT = 10;
/** After a failed rebuild, how long the app gets to hear ABNORMAL_EXIT before it is closed. */
const ABNORMAL_EXIT_CLOSE_DELAY_MS = 200;
/** Stock text scrolling: one step per swipe, and "at an end" within this tolerance. */
const TEXT_SCROLL_STEP_PX = 28;
const TEXT_SCROLL_EDGE_PX = 3;
/**
* Injected into every served HTML document at document start (before any app
* JS): (1) the promise-returning flutter_inappwebview.callHandler the EvenHub
@@ -441,8 +416,6 @@ export class EvenHubSession implements EvenHubMicClient, EvenHubImuClient, EvenH
private page: EvenHubPage | null = null;
private pageCreated = false;
/** The page came from Faceclaw's extended layout API, which isn't held to stock limits. */
private extendedLayout = false;
private webViewHandle: EvenHubWebViewHandle | null = null;
private windowHooks: EvenHubWindowHooks | null = null;
private closed = false;
@@ -623,8 +596,6 @@ export class EvenHubSession implements EvenHubMicClient, EvenHubImuClient, EvenH
const eventType = event.type === "scroll-up" ? SCROLL_TOP_EVENT : SCROLL_BOTTOM_EVENT;
if (capture?.kind === "list") {
this.scrollList(capture, eventType, inputSource);
} else if (capture?.kind === "text") {
this.scrollText(capture, eventType, inputSource);
} else {
this.emitSysEvent(eventType, 0, inputSource);
}
@@ -649,25 +620,6 @@ export class EvenHubSession implements EvenHubMicClient, EvenHubImuClient, EvenH
this.pushMessage("evenHubEvent", { type: "menuItemClickEvent", jsonData: { itemID } });
}
/**
* Stock scrolls a capture text container that overflows by itself, a 28px
* step per swipe, and tells the app only at the ends (a textEvent naming the
* container). Text that fits reports every swipe as reaching an end.
*/
private scrollText(text: EvenHubTextContainer, eventType: number, inputSource?: ExtensionInputSource): void {
const { maxScroll } = layoutText(text);
const forward = eventType === SCROLL_BOTTOM_EVENT;
const atEnd = forward ? maxScroll - text.scrollY < TEXT_SCROLL_EDGE_PX : text.scrollY < TEXT_SCROLL_EDGE_PX;
if (maxScroll === 0 || atEnd) {
this.emitTextEvent(text, eventType, inputSource);
return;
}
text.scrollY = forward
? Math.min(maxScroll, text.scrollY + TEXT_SCROLL_STEP_PX)
: Math.max(0, text.scrollY - TEXT_SCROLL_STEP_PX);
this.windowHooks?.requestRender();
}
private scrollList(list: EvenHubListContainer, eventType: number, inputSource?: ExtensionInputSource): void {
// Swipe up selects the previous item, swipe down the next (stock direction).
const delta = eventType === SCROLL_TOP_EVENT ? -1 : 1;
@@ -856,8 +808,7 @@ export class EvenHubSession implements EvenHubMicClient, EvenHubImuClient, EvenH
deliverImu(reading: ImuReading): void {
this.pushEvenHubEvent("sysEvent", {
eventType: IMU_DATA_REPORT_EVENT,
// The Even app's field name (protobuf IMU_Data); the SDK reads it as imuData.
iMUData: { x: reading.x, y: reading.y, z: reading.z },
imuData: { x: reading.x, y: reading.y, z: reading.z },
});
}
@@ -907,8 +858,7 @@ export class EvenHubSession implements EvenHubMicClient, EvenHubImuClient, EvenH
const first = args[0];
const message = asRecord(typeof first === "string" ? JSON.parse(first) : first);
const method = readString(message, "method", "");
// As the Even app: `data`, else `payload`.
const data = asRecord(message.data ?? message.payload);
const data = asRecord(message.data);
result = await this.dispatch(method, data);
} catch (error) {
this.log(`evenhub bridge call failed: ${error}`);
@@ -966,29 +916,13 @@ export class EvenHubSession implements EvenHubMicClient, EvenHubImuClient, EvenH
private async createStartUpPage(data: Record<string, unknown>): Promise<number> {
if (this.pageCreated) {
// One-shot per session on stock: a second call blocks ~2s, then fails.
this.layoutLog(() => ["createStartUpPageContainer: repeated call, rejected as stock does"]);
await delay(DUPLICATE_CREATE_DELAY_MS);
return 1;
}
// The Even app refuses some payloads without sending them; then the
// firmware may fail to build the page.
const payloadError = stockPayloadError("create", data);
if (payloadError) {
this.layoutLog(() => [`createStartUpPageContainer refused, as the Even app would: ${payloadError}`]);
return PAGE_CREATE_INVALID;
}
const page = parsePage(data);
const failure = stockPageFailure(page);
if (failure) {
this.layoutLog(() => [`createStartUpPageContainer failed, as the firmware would: ${failure.reason}`]);
return failure.code;
}
clampToStockLimits(page);
this.warnOnDroppedMenuItems(page, data);
this.layoutLog(() => describePage("createStartUpPageContainer", page, data));
this.warnOnInvalidPage(page, data);
this.page = page;
this.pageCreated = true;
this.extendedLayout = false;
// The launch focus arrived before the page existed; deliver the ENTER now.
if (this.foreground && !this.launchEnterSent) {
this.launchEnterSent = true;
@@ -999,39 +933,16 @@ export class EvenHubSession implements EvenHubMicClient, EvenHubImuClient, EvenH
}
private rebuildPage(data: Record<string, unknown>): boolean {
// Only createStartUpPageContainer starts an EvenHub page on the glasses.
if (!this.pageCreated) {
this.layoutLog(() => ["rebuildPageContainer before createStartUpPageContainer: no page to rebuild"]);
return false;
}
const payloadError = stockPayloadError("rebuild", data);
if (payloadError) {
this.layoutLog(() => [`rebuildPageContainer refused, as the Even app would: ${payloadError}`]);
return false;
}
// Also serves as create for apps that fall back to rebuild-on-error.
const page = parsePage(data);
const failure = stockPageFailure(page);
if (failure) {
// The firmware has already torn the old page down; it reports an
// abnormal exit and the Even app closes the app.
this.layoutLog(() => [`rebuildPageContainer failed, as the firmware would: ${failure.reason}; closing the app`]);
this.page = { containers: [], menuItems: [] };
this.windowHooks?.requestRender();
this.emitSysEvent(ABNORMAL_EXIT_EVENT, 0);
this.systemExitSent = true;
setTimeout(() => this.close(), ABNORMAL_EXIT_CLOSE_DELAY_MS);
return false;
}
clampToStockLimits(page);
this.warnOnDroppedMenuItems(page, data);
this.layoutLog(() => describePage("rebuildPageContainer", page, data));
this.warnOnInvalidPage(page, data);
this.page = page;
this.extendedLayout = false;
this.pageCreated = true;
this.windowHooks?.requestRender();
return true;
}
private warnOnDroppedMenuItems(page: EvenHubPage, data: Record<string, unknown>): void {
private warnOnInvalidPage(page: EvenHubPage, data: Record<string, unknown>): void {
const declaredMenuCount = declaredMenuItems(data).length;
if (declaredMenuCount > page.menuItems.length) {
this.log(
@@ -1039,106 +950,64 @@ export class EvenHubSession implements EvenHubMicClient, EvenHubImuClient, EvenH
"items (max 10, itemID must be non-zero and unique, itemName at most 32 UTF-8 bytes)",
);
}
// Stock rejects these outright; we accept-and-log until rejection is
// proven necessary for compatibility.
const captures = page.containers.filter((c) => c.kind !== "image" && c.isEventCapture).length;
if (captures !== 1) this.log(`evenhub: page has ${captures} event-capture containers (stock requires exactly 1)`);
const texts = page.containers.filter((c) => c.kind === "text").length;
const images = page.containers.filter((c) => c.kind === "image").length;
if (texts > 8 || images > 4) this.log(`evenhub: page exceeds stock limits (${texts} text, ${images} image)`);
}
private textUpgrade(data: Record<string, unknown>): boolean {
const id = readNumber(data, "containerID", 0);
const id = readNumber(data, "containerID", -1);
// containerID is the identity; containerName is optional in the wild (e.g.
// level-even-g2 omits it). Match by id, enforcing the name only when the app
// actually supplies a non-empty one. (updateImage already matches by id.)
const name = readString(data, "containerName", "");
const container = this.page?.containers.find(
(c) => c.kind === "text" && c.id === id && (name === "" || c.name === name),
);
if (!container || container.kind !== "text") return false;
const content = readString(data, "content", "");
const offset = readOptionalNumber(data, "contentOffset");
const payloadError = stockPayloadError("textUpgrade", data);
if (payloadError) {
this.layoutLog(() => [`textContainerUpgrade refused, as the Even app would: ${payloadError}`]);
return false;
}
// The firmware finds the container by id alone (its first container with
// that id, of any kind); the name isn't checked anywhere.
const target = containerById(this.page, id);
if (!target) {
this.layoutLog(() => [describeTextUpgrade(id, name, null, offset, content.length)]);
return false;
}
// Aimed at a list or image, the firmware writes into it as if it were
// text and reports success; nothing we could show.
if (target.kind !== "text") return true;
// The content replaces the whole text: stock ignores contentOffset and
// contentLength (firmware 2.2.9.22 in the emulator showed "XY" alone for
// every offset/length tried). The firmware re-sets the label, scrolled
// back to the top, only for non-empty text, so emptying a container
// leaves its old text showing (apps send " " to clear one).
if (content !== "") {
target.content = content;
target.scrollY = 0;
// (contentLength is intentionally ignored — see below.)
// Stock semantics are "write `content` at contentOffset and truncate the
// rest" (a null-terminating strcpy(buf+offset, content)), NOT a splice that
// keeps the old tail after offset+contentLength. Keeping the tail left stray
// trailing text when an app (e.g. g2oom) shrank a string with an
// under-sized contentLength; dropping it matches stock and still supports
// suffix rewrites (offset>0) and full replaces (offset 0 / omitted).
if (offset !== undefined && offset > 0) {
container.content = container.content.slice(0, offset) + content;
} else {
container.content = content;
}
// SDK 0.0.14: textColor on an incremental update sets the container's
// brightness; omitting it keeps whatever level the container has.
const brightness = readTextBrightness(data);
if (brightness !== undefined) target.textColor = brightness;
this.layoutLog(() => [describeTextUpgrade(id, name, target, offset, content.length)]);
if (brightness !== undefined) container.textColor = brightness;
this.windowHooks?.requestRender();
return true;
}
/** EhImageUpdateResult: 0 success, 1 imageException, 2 sizeInvalid, 3 toGray4Failed, 4 sendFailed. */
/** BleEhSendImageResult: 0 success, 1 imageException, 2 sizeInvalid, 3 sendFailed. */
private updateImage(data: Record<string, unknown>): number {
const id = readNumber(data, "containerID", 0);
const name = readString(data, "containerName", "");
const raw = data.imageData;
// Before the page exists (and after shutdown) the Even app fails it outright.
if (!this.pageCreated || !this.page) {
this.layoutLog(() => [`updateImageRawData id=${id}: no page yet, sendFailed`]);
return IMAGE_SEND_FAILED;
}
// The Even app sizes the bitmap from the image container it finds by id,
// then by trimmed name.
const images = this.page.containers.filter((c): c is EvenHubImageContainer => c.kind === "image");
const geometry = (id > 0 ? images.find((c) => c.id === id) : undefined) ??
(name.trim() ? images.find((c) => c.name.trim() === name.trim()) : undefined);
const prepared = prepareImageUpdate({
imageData: raw,
compressMode: readNumber(data, "compressMode", 0),
requestWidth: readNumber(data, "width", 0),
requestHeight: readNumber(data, "height", 0),
geometry: geometry ?? null,
decodeNative: decodeImageRgba,
stockLimits: !this.extendedLayout,
});
// The firmware takes the transfer only for an id whose first container is
// an image, and then shows the bitmap only if it's exactly that size.
const target = containerById(this.page, id);
let result = prepared.result;
let shown = false;
if (result === IMAGE_SUCCESS && target?.kind !== "image") {
result = IMAGE_SEND_FAILED;
} else if (result === IMAGE_SUCCESS && target?.kind === "image" && prepared.levels &&
prepared.width === target.width && prepared.height === target.height) {
target.pixels = prepared.levels.map((level) => level * 17);
target.pixelsWidth = prepared.width;
target.pixelsHeight = prepared.height;
shown = true;
this.windowHooks?.requestRender();
}
this.layoutLog(() => describeImageUpdate({ sentId: id, sentName: name, raw, target, prepared, shown, result }));
return result;
}
/** Debug-level logcat for layouts and updates; `describe` runs only when logging is on. */
private layoutLog(describe: () => string[]): void {
if (!LAYOUT_LOG_ENABLED) return;
try {
const prefix = `[${this.manifest.packageId}] `;
for (const line of describe()) {
for (let i = 0; i < line.length; i += LOGCAT_CHUNK_CHARS) {
android.util.Log.d(LAYOUT_LOG_TAG, prefix + line.slice(i, i + LOGCAT_CHUNK_CHARS));
}
}
} catch (error) {
console.warn(`evenhub: layout log failed: ${error}`);
}
const id = readNumber(data, "containerID", -1);
const container = this.page?.containers.find((c) => c.kind === "image" && c.id === id);
if (!container || container.kind !== "image") return 3;
const bytes = imagePayloadBytes(data.imageData ?? data.rawData ?? asRecord(data).mapRawData);
if (!bytes || bytes.length === 0) return 1;
const decoded = decodeImageBytes(bytes, container);
if (!decoded) return 1;
container.pixels = decoded.pixels;
container.pixelsWidth = decoded.width;
container.pixelsHeight = decoded.height;
this.windowHooks?.requestRender();
return 0;
}
private shutDown(exitMode: number): boolean {
this.layoutLog(() => [`shutDownPageContainer exitMode=${exitMode}`]);
if (exitMode === 1) {
// The app asked to quit (stock would pop an on-glasses confirm). We
// decline and hand focus to the app switcher instead, leaving the app
@@ -1206,15 +1075,6 @@ export class EvenHubSession implements EvenHubMicClient, EvenHubImuClient, EvenH
this.pushEvenHubEvent("sysEvent", { eventType, eventSource, [INPUT_SOURCE_FIELD]: inputSource });
}
private emitTextEvent(text: EvenHubTextContainer, eventType: number, inputSource?: ExtensionInputSource): void {
this.pushEvenHubEvent("textEvent", {
containerID: text.id,
containerName: text.name,
eventType,
[INPUT_SOURCE_FIELD]: inputSource,
});
}
private emitListEvent(list: EvenHubListContainer, eventType: number, inputSource?: ExtensionInputSource): void {
this.pushEvenHubEvent("listEvent", {
containerID: list.id,
@@ -1392,10 +1252,8 @@ export class EvenHubSession implements EvenHubMicClient, EvenHubImuClient, EvenH
for (const container of page.containers) {
if (container.preserve) preserveContent(container, previous);
}
this.layoutLog(() => describePage("extension layout", page, data));
this.page = page;
this.pageCreated = true;
this.extendedLayout = true;
// The extended layout uses the taller app area.
this.windowHooks?.setTallCanvas(true);
if (this.foreground && !this.launchEnterSent) {
@@ -1701,6 +1559,82 @@ function normalizeBuzzerSteps(raw: unknown[]): Step[] {
return out;
}
/**
* Decode an uncompressed Windows BMP ('BM', BI_RGB) to 8bpp grayscale. Handles
* 1/4/8-bit paletted and 24/32-bit BGR(A), top-down or bottom-up. Returns null
* for compressed or unsupported variants. Apps like EvenChess send 1-bit BMPs
* (black/white palette) for full-board refreshes.
*/
function decodeBmp(bytes: Uint8Array): { pixels: Uint8Array; width: number; height: number } | null {
if (bytes.length < 54 || bytes[0] !== 0x42 || bytes[1] !== 0x4d) return null;
const u16 = (o: number) => bytes[o]! | (bytes[o + 1]! << 8);
const u32 = (o: number) => (bytes[o]! | (bytes[o + 1]! << 8) | (bytes[o + 2]! << 16) | (bytes[o + 3]! << 24)) >>> 0;
const i32 = (o: number) => bytes[o]! | (bytes[o + 1]! << 8) | (bytes[o + 2]! << 16) | (bytes[o + 3]! << 24);
const pixelOffset = u32(10);
const dibSize = u32(14);
const width = i32(18);
const heightRaw = i32(22);
const bitCount = u16(28);
const compression = u32(30);
if (compression !== 0) return null; // only BI_RGB (uncompressed)
if (width <= 0 || width > 4096) return null;
const topDown = heightRaw < 0;
const height = Math.abs(heightRaw);
if (height === 0 || height > 4096) return null;
// Palette (grayscale per index) for <=8bpp; each entry is 4 bytes BGRA.
let palette: Uint8Array | null = null;
if (bitCount <= 8) {
let numColors = u32(46);
if (numColors === 0 || numColors > (1 << bitCount)) numColors = 1 << bitCount;
const palOffset = 14 + dibSize;
palette = new Uint8Array(numColors);
for (let i = 0; i < numColors; i++) {
const o = palOffset + i * 4;
if (o + 2 >= bytes.length) break;
palette[i] = luminance(bytes[o + 2]!, bytes[o + 1]!, bytes[o]!);
}
}
const stride = (((width * bitCount + 31) >> 5) << 2); // rows are 4-byte aligned
const pixels = new Uint8Array(width * height);
for (let y = 0; y < height; y++) {
const srcY = topDown ? y : height - 1 - y;
const rowStart = pixelOffset + srcY * stride;
if (rowStart >= bytes.length) break;
const dstRow = y * width;
for (let x = 0; x < width; x++) {
let gray: number;
if (bitCount === 1) {
const bit = ((bytes[rowStart + (x >> 3)] ?? 0) >> (7 - (x & 7))) & 1;
gray = palette?.[bit] ?? (bit ? 255 : 0);
} else if (bitCount === 4) {
const byte = bytes[rowStart + (x >> 1)] ?? 0;
const nib = (x & 1) === 0 ? byte >> 4 : byte & 0x0f;
gray = palette?.[nib] ?? nib * 17;
} else if (bitCount === 8) {
const idx = bytes[rowStart + x] ?? 0;
gray = palette?.[idx] ?? idx;
} else if (bitCount === 24) {
const o = rowStart + x * 3;
gray = luminance(bytes[o + 2] ?? 0, bytes[o + 1] ?? 0, bytes[o] ?? 0);
} else if (bitCount === 32) {
const o = rowStart + x * 4;
gray = luminance(bytes[o + 2] ?? 0, bytes[o + 1] ?? 0, bytes[o] ?? 0);
} else {
return null;
}
pixels[dstRow + x] = gray;
}
}
return { pixels, width, height };
}
function luminance(r: number, g: number, b: number): number {
return Math.round(0.2126 * r + 0.7152 * g + 0.0722 * b);
}
/**
* Input-device names in the extension API: the touchDown event's source, and
* the `faceclawInputSource` field added to stock gesture events.
@@ -1752,3 +1686,97 @@ function elideZeroFields(obj: Record<string, unknown>): Record<string, unknown>
return out;
}
/** Accept the payload forms seen on the wire: number[], JSON-ified typed
* array ({"0": 1, ...}), or base64 string. */
function imagePayloadBytes(raw: unknown): Uint8Array | null {
if (Array.isArray(raw)) {
const out = new Uint8Array(raw.length);
for (let i = 0; i < raw.length; i++) out[i] = Number(raw[i]) & 0xff;
return out;
}
if (typeof raw === "string") return base64Decode(raw);
if (raw && typeof raw === "object") {
const record = raw as Record<string, unknown>;
const keys = Object.keys(record);
const out = new Uint8Array(keys.length);
for (let i = 0; i < keys.length; i++) {
const value = record[String(i)];
if (typeof value !== "number") return null;
out[i] = value & 0xff;
}
return out;
}
return null;
}
const BASE64_ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
function base64Decode(text: string): Uint8Array | null {
const clean = text.replace(/[\s=]+$/g, "").replace(/\s+/g, "");
const out = new Uint8Array(Math.floor((clean.length * 3) / 4));
let bits = 0;
let bitCount = 0;
let outIndex = 0;
for (const char of clean) {
const value = BASE64_ALPHABET.indexOf(char);
if (value < 0) return null;
bits = (bits << 6) | value;
bitCount += 6;
if (bitCount >= 8) {
bitCount -= 8;
out[outIndex++] = (bits >> bitCount) & 0xff;
}
}
return out.subarray(0, outIndex);
}
/**
* Decode an image payload: PNG or BMP (both are sent by real apps — EvenChess
* force-refreshes as 1-bit BMP but streams live moves as PNG, so a host that
* only did PNG dropped the initial board), or raw grayscale sized to the
* container (8bpp, or 4bpp packed two pixels per byte, high nibble first).
*/
function decodeImageBytes(
bytes: Uint8Array,
container: EvenHubImageContainer,
): { pixels: Uint8Array; width: number; height: number } | null {
if (bytes.length > 2 && bytes[0] === 0x42 && bytes[1] === 0x4d) {
const bmp = decodeBmp(bytes);
if (bmp) return bmp;
}
if (bytes.length > 4 && bytes[0] === 0x89 && bytes[1] === 0x50 && bytes[2] === 0x4e && bytes[3] === 0x47) {
try {
const buffer = bytes.buffer.slice(bytes.byteOffset, bytes.byteOffset + bytes.byteLength);
const decoded = UPNG.decode(buffer);
const frames = UPNG.toRGBA8(decoded) as ArrayBuffer[];
if (!frames.length) return null;
const rgba = new Uint8Array(frames[0]!);
const pixels = new Uint8Array(decoded.width * decoded.height);
for (let i = 0; i < pixels.length; i++) {
const offset = i * 4;
const alpha = (rgba[offset + 3] ?? 0) / 255;
pixels[i] = Math.round(
(0.2126 * (rgba[offset] ?? 0) + 0.7152 * (rgba[offset + 1] ?? 0) + 0.0722 * (rgba[offset + 2] ?? 0)) * alpha,
);
}
return { pixels, width: decoded.width, height: decoded.height };
} catch (error) {
console.warn(`evenhub: PNG decode failed: ${error}`);
return null;
}
}
const area = container.width * container.height;
if (bytes.length === area) {
return { pixels: Uint8Array.from(bytes), width: container.width, height: container.height };
}
if (bytes.length === Math.ceil(area / 2)) {
const pixels = new Uint8Array(area);
for (let i = 0; i < area; i++) {
const byte = bytes[i >> 1]!;
const nibble = i % 2 === 0 ? byte >> 4 : byte & 0x0f;
pixels[i] = nibble * 17;
}
return { pixels, width: container.width, height: container.height };
}
return null;
}
+17 -22
View File
@@ -16,11 +16,7 @@ import { cleanError, installStoreApp } from "./store-install";
import { describeUpdate } from "./updates";
import { lineStep, listRowHeight } from "../../ui/metrics";
/** How far an action row's highlight box overhangs the column of text it belongs to, on each side. */
const ACTION_BOX_OVERHANG = 5;
/** The side-by-side layout: the action menu's label column, and the gap between the details and its box. */
const SIDE_MENU_WIDTH = 150;
const SIDE_MENU_GAP = 9;
const X = 18;
type DetailAction = "about" | "whats-new" | "primary";
const ACTIONS: readonly DetailAction[] = ["about", "whats-new", "primary"];
@@ -64,15 +60,10 @@ export class EvenHubStoreDetailLayer implements Layer {
paint(ctx: LayerContext): GrayImage {
if (!this.detailPromise) void this.ensureDetail(ctx);
const font = getDefaultSmallFont();
const { width, height, padding } = ctx.stack.getBaseSize();
const { width, height } = ctx.stack.getBaseSize();
const image = new GrayImage(width, height, 0);
const step = lineStep(font);
const actionRowH = listRowHeight(font) + 4;
// The action rows' highlight boxes are the outermost thing drawn toward
// the edges they reach, so they sit at the padding suggested for those
// edges; the details keep to the text column inside the boxes' overhang.
const textX = padding.left + ACTION_BOX_OVERHANG;
const fullTextWidth = Math.max(0, width - padding.left - padding.right - 2 * ACTION_BOX_OVERHANG);
const installed = getInstalledEvenHubApp(this.app.packageId);
this.primaryLabel = this.working ? "Installing..." : this.primaryActionLabel(installed);
@@ -118,22 +109,26 @@ export class EvenHubStoreDetailLayer implements Layer {
// Stacked layout (details full-width, actions at the bottom) when it
// fits; otherwise the actions become a narrow top-right menu and the
// details flow down a left column beside it.
const stackedActionTop = height - padding.bottom - ACTIONS.length * actionRowH;
const stacked = padding.top + linesHeight(buildLines(fullTextWidth)) + 6 <= stackedActionTop;
const sideBoxWidth = SIDE_MENU_WIDTH + 2 * ACTION_BOX_OVERHANG;
const sideBoxX = width - padding.right - sideBoxWidth;
const textWidth = stacked ? fullTextWidth : Math.max(0, sideBoxX - SIDE_MENU_GAP - textX);
const stackedActionTop = height - 6 - ACTIONS.length * actionRowH;
const stacked = 8 + linesHeight(buildLines(width - X * 2)) + 6 <= stackedActionTop;
const menuW = 150;
const menuX = width - menuW - 12;
const textWidth = stacked ? width - X * 2 : menuX - X - 14;
let y = padding.top;
let y = 8;
for (const line of buildLines(textWidth)) {
image.drawText(font, textX, y, line.text, line.value);
image.drawText(font, X, y, line.text, line.value);
y += step + line.gapAfter;
}
const actionBox = stacked
? { x: padding.left, y: stackedActionTop, width: fullTextWidth + 2 * ACTION_BOX_OVERHANG }
: { x: sideBoxX, y: padding.top, width: sideBoxWidth };
this.actions.paint(image, { ...actionBox, height: ACTIONS.length * actionRowH - 1 }, ctx.stack.isFocused());
const actionX = stacked ? X : menuX;
const actionW = stacked ? textWidth : menuW;
const actionTop = stacked ? stackedActionTop : 8;
this.actions.paint(
image,
{ x: actionX - 5, y: actionTop, width: actionW + 10, height: ACTIONS.length * actionRowH - 1 },
ctx.stack.isFocused(),
);
return image;
}
+47 -169
View File
@@ -1,7 +1,7 @@
import { getDefaultSmallFont } from "../../graphics/ui-fonts";
import { GrayImage, type UiFont } from "../../graphics/image";
import { truncateText, wrapText } from "../../graphics/textwrap";
import { directionalFallback, GESTURE_CLICK, isDirectionalInput, type InputEvent } from "../../ui/gestures";
import { GESTURE_CLICK, type InputEvent } from "../../ui/gestures";
import { type Layer, type LayerActions, type LayerContext, type LayerStack } from "../../ui/layers";
import { drawSelectionHighlight, type MenuItem } from "../../ui/menu";
import { Menu } from "../../ui/menu-core";
@@ -14,37 +14,14 @@ import { EvenHubStoreDetailLayer, type EvenHubStoreDetailOptions } from "./store
import { cleanError, installStoreApp } from "./store-install";
import { checkForEvenHubUpdates, describeUpdate, type EvenHubAppUpdate } from "./updates";
import { lineStep } from "../../ui/metrics";
import type { Viewport } from "../../ui/viewport";
import { isEvenHubStoreAvailable } from "./glasses-compat";
/** Horizontal inset of a row's text from its highlight box; the header's text lines up with it. */
const ROW_TEXT_INSET = 6;
const LIST_X = 18;
/** Horizontal gap between tab labels. */
const TAB_GAP = 18;
/** How far a focused tab's highlight box reaches past its label, sideways and above/below. */
const TAB_HIGHLIGHT_PAD_X = 6;
const TAB_HIGHLIGHT_PAD_Y = 3;
/** Room kept right of the rows for the scrollbar, and the bar's own width. */
const SCROLLBAR_GUTTER = 6;
const SCROLLBAR_WIDTH = 3;
/**
* Where the header sits in a viewport. Its text shares a column with the
* rows' text, whose highlight boxes start at the left edge's padding. The
* outermost thing it draws toward the top is a focused tab's highlight box,
* so that box sits at the top edge's padding and the title line follows
* from it. `bottom` is where the header ends: title + tabs line, then the
* status/subtitle line, then a small gap.
*/
function headerLayout(viewport: Viewport, font: UiFont): { textX: number; textWidth: number; titleY: number; bottom: number } {
const { width, padding } = viewport;
const titleY = padding.top + TAB_HIGHLIGHT_PAD_Y;
return {
textX: padding.left + ROW_TEXT_INSET,
textWidth: Math.max(0, width - padding.left - padding.right - 2 * ROW_TEXT_INSET),
titleY,
bottom: titleY + lineStep(font) + font.lineHeight + 6,
};
/** Header: title + tabs line, then the status/subtitle line, then a small gap. */
function headerHeight(font: UiFont): number {
return 8 + lineStep(font) + font.lineHeight + 6;
}
/** Row pitch of the two-line app list (title + detail), including the 2px gap between highlights. */
@@ -78,32 +55,7 @@ type StoreRow =
/** The Search tab's first row: shows the query and opens the phone editor. */
| { kind: "search-query" }
/** A registry entry whose package is gone and which the store no longer lists. */
| { kind: "orphan"; update: EvenHubAppUpdate }
/** The Updates tab's first row while any update is installable; `busy` while Update All runs. */
| { kind: "update-all"; count: number; busy: boolean };
type AppRow = Extract<StoreRow, { kind: "app" }>;
/** An Updates row that installing the store's version would satisfy. */
function isUpdatableRow(row: StoreRow): row is AppRow {
return row.kind === "app" && row.update !== undefined;
}
/** An Updates row for one installed app (not the Update All row). */
function isInstalledAppRow(row: StoreRow): boolean {
return row.kind === "app" || row.kind === "orphan";
}
function rowPackageId(row: StoreRow): string | undefined {
switch (row.kind) {
case "app":
return row.app.packageId;
case "orphan":
return row.update.installed.packageId;
default:
return undefined;
}
}
| { kind: "orphan"; update: EvenHubAppUpdate };
type TabState = {
rows: StoreRow[];
@@ -141,8 +93,8 @@ function emptyTab(): TabState {
draw: ({ image, item, x, y, width }) => {
const font = getDefaultSmallFont();
const { title, detail, titleValue } = rowText(item);
const textX = x + ROW_TEXT_INSET;
const textW = width - 2 * ROW_TEXT_INSET;
const textX = x + 6;
const textW = width - 12;
image.drawText(font, textX, y + 2, truncateText(font, title, textW), titleValue);
image.drawText(font, textX, y + 2 + lineStep(font), truncateText(font, detail, textW), 115);
},
@@ -166,18 +118,10 @@ export type EvenHubStoreLayerOptions = {
* The EvenHub storefront: Top (leaderboard), New (by first publication),
* Search, and Updates (installed apps that are stale or whose package is
* missing). The tab row is its own focus level above the list: double-click
* in the list focuses the tabs, scrolling (or a watch swipe in any direction)
* there switches tabs, and a click returns to the list.
*
* With glasses that can't use the store (a RayNeo iO; see glasses-compat.ts)
* it shows only a notice saying so, and loads nothing. That is checked on
* every paint and input, so a window open across a re-pair follows the
* glasses either way.
* in the list focuses the tabs, scrolling there switches tabs, and a click
* returns to the list.
*/
export class EvenHubStoreLayer implements Layer {
/** Swipes left/right switch tabs on the tab strip; elsewhere they get the usual fallback. */
readonly acceptsDirectional = true;
private readonly tabs: Record<StoreTab, TabState> = {
top: emptyTab(),
new: emptyTab(),
@@ -196,15 +140,12 @@ export class EvenHubStoreLayer implements Layer {
private updatingAll = false;
/** An app page requested while the login form was up; opened once signed in. */
private pendingPackage: InstalledEvenHubApp | null = null;
/** An app page requested while the store was unavailable, named on its notice. */
private unavailablePackageName: string | null = null;
constructor(private readonly options: EvenHubStoreLayerOptions) {
if (this.showingLogin) resetEvenHubLoginForm();
}
paint(ctx: LayerContext): GrayImage {
if (!isEvenHubStoreAvailable()) return this.paintUnavailable(ctx);
if (!this.started) {
this.started = true;
// Do NOT open the phone credential editor here. This runs on the first
@@ -220,93 +161,66 @@ export class EvenHubStoreLayer implements Layer {
}
const font = getDefaultSmallFont();
const viewport = ctx.stack.getBaseSize();
const { width, height, padding } = viewport;
const { width, height } = ctx.stack.getBaseSize();
const image = new GrayImage(width, height, 0);
const { textX, textWidth, titleY, bottom: headerBottom } = headerLayout(viewport, font);
image.drawText(font, textX, titleY, "EvenHub", 220);
const headerH = headerHeight(font);
image.drawText(font, LIST_X, 8, "EvenHub", 220);
if (this.showingLogin) {
image.drawText(font, textX, titleY + lineStep(font), truncateText(font, this.loginStatus, textWidth), 125);
image.drawText(font, LIST_X, 8 + lineStep(font), truncateText(font, this.loginStatus, width - LIST_X * 2), 125);
const message = this.loginBusy
? "Signing in..."
: "Enter your Even account email and password in the phone app.";
for (const [index, line] of wrapText(font, message, textWidth).entries()) {
image.drawText(font, textX, headerBottom + 16 + index * lineStep(font), line, 190);
for (const [index, line] of wrapText(font, message, width - LIST_X * 2).entries()) {
image.drawText(font, LIST_X, headerH + 16 + index * lineStep(font), line, 190);
}
image.drawText(font, textX, height - padding.bottom - font.lineHeight, `${GESTURE_CLICK} log in`, 105);
image.drawText(font, LIST_X, height - font.lineHeight - 4, `${GESTURE_CLICK} log in`, 105);
return image;
}
this.paintTabs(image, font, ctx, textX, titleY);
this.paintTabs(image, font, ctx);
const tab = this.tabs[this.activeTab];
const subtitle = tab.status || this.subtitle(this.activeTab, tab);
image.drawText(font, textX, titleY + lineStep(font), truncateText(font, subtitle, textWidth), 125);
image.drawText(font, LIST_X, 8 + lineStep(font), truncateText(font, subtitle, width - LIST_X * 2), 125);
if (tab.rows.length === 0) {
const message = tab.loading ? "Loading..." : this.emptyMessage(this.activeTab, tab);
for (const [index, line] of wrapText(font, message, textWidth).slice(0, 3).entries()) {
image.drawText(font, textX, headerBottom + 22 + index * lineStep(font), line, 190);
for (const [index, line] of wrapText(font, message, width - LIST_X * 2).slice(0, 3).entries()) {
image.drawText(font, LIST_X, headerH + 22 + index * lineStep(font), line, 190);
}
return image;
}
if (height <= headerBottom) return image;
// The header stays put, so the list starts right under it with no
// padding of its own there. It scrolls, so it runs to the window's
// bottom edge, the padding suggested for that edge inside it: the last
// row keeps clear of a border there, and a row cut off for scrolling
// meets the edge rather than a blank strip. The scrollbar keeps inside
// the padding, in a gutter the rows leave free.
const rowH = storeRowHeight(font);
const visibleRows = Math.max(1, Math.floor((height - headerH - 4) / rowH));
const listFocused = ctx.stack.isFocused() && this.focus === "list";
const menu = tabMenu(tab);
const rowsWidth = Math.max(0, width - padding.left - padding.right - SCROLLBAR_GUTTER);
menu.paint(
image,
{ x: padding.left, y: headerBottom, width: rowsWidth, height: height - headerBottom, padTop: 0, padBottom: padding.bottom },
{ x: LIST_X - 6, y: headerH, width: width - LIST_X * 2 + 12, height: visibleRows * rowH - 2 },
listFocused,
);
menu.drawScrollbar(image, width - padding.right - SCROLLBAR_WIDTH, headerBottom,
Math.max(0, height - padding.bottom - headerBottom));
menu.drawScrollbar(image, width - 5, headerH, visibleRows * rowH - 3);
return image;
}
/** The notice that replaces the store when the paired glasses can't use it. */
private paintUnavailable(ctx: LayerContext): GrayImage {
const font = getDefaultSmallFont();
const viewport = ctx.stack.getBaseSize();
const image = new GrayImage(viewport.width, viewport.height, 0);
const { textX, textWidth, titleY, bottom: headerBottom } = headerLayout(viewport, font);
image.drawText(font, textX, titleY, "EvenHub", 220);
if (this.unavailablePackageName) {
const status = `${this.unavailablePackageName}'s package is missing.`;
image.drawText(font, textX, titleY + lineStep(font), truncateText(font, status, textWidth), 125);
}
const message = "Using the Even Realities app store requires Even Realities glasses.";
for (const [index, line] of wrapText(font, message, textWidth).entries()) {
image.drawText(font, textX, headerBottom + 16 + index * lineStep(font), line, 190);
}
return image;
}
/** Tab labels to the right of the title (at titleX, titleY); the active one is bright and underlined. */
private paintTabs(image: GrayImage, font: UiFont, ctx: LayerContext, titleX: number, titleY: number): void {
/** Tab labels to the right of the title; the active one is bright and underlined. */
private paintTabs(image: GrayImage, font: UiFont, ctx: LayerContext): void {
const tabsFocused = ctx.stack.isFocused() && this.focus === "tabs";
let x = titleX + font.measureText("EvenHub") + 28;
let x = LIST_X + font.measureText("EvenHub") + 28;
for (const tab of TABS) {
const label = this.tabLabel(tab.id);
const w = font.measureText(label);
const active = tab.id === this.activeTab;
if (active) {
if (tabsFocused) {
drawSelectionHighlight(image, x - TAB_HIGHLIGHT_PAD_X, titleY - TAB_HIGHLIGHT_PAD_Y,
w + 2 * TAB_HIGHLIGHT_PAD_X, font.lineHeight + 2 * TAB_HIGHLIGHT_PAD_Y, true, 4);
drawSelectionHighlight(image, x - 6, 5, w + 12, font.lineHeight + 6, true, 4);
} else {
image.fillRect(x, titleY + font.lineHeight + 1, w, 1, 150);
image.fillRect(x, 8 + font.lineHeight + 1, w, 1, 150);
}
}
image.drawText(font, x, titleY, label, active ? 235 : 120);
image.drawText(font, x, 8, label, active ? 235 : 120);
x += w + TAB_GAP;
}
}
@@ -315,7 +229,7 @@ export class EvenHubStoreLayer implements Layer {
const label = TABS.find((entry) => entry.id === tab)!.label;
if (tab !== "updates") return label;
const updates = this.tabs.updates;
const count = updates.rows.filter(isInstalledAppRow).length;
const count = updates.rows.filter((row) => row.kind !== "search-query").length;
return updates.loaded && count > 0 ? `${label} (${count})` : label;
}
@@ -331,11 +245,9 @@ export class EvenHubStoreLayer implements Layer {
if (!state.query) return "Search public apps";
return results === 1 ? `1 result for "${state.query}"` : `${results} results for "${state.query}"`;
}
case "updates": {
case "updates":
if (!state.loaded) return "Installed apps with newer versions";
const apps = state.rows.filter(isInstalledAppRow).length;
return apps === 0 ? "All installed apps are up to date" : apps === 1 ? "1 app needs attention" : `${apps} apps need attention`;
}
return count === 0 ? "All installed apps are up to date" : count === 1 ? "1 app needs attention" : `${count} apps need attention`;
}
}
@@ -351,14 +263,7 @@ export class EvenHubStoreLayer implements Layer {
}
}
async handleInput(rawEvent: InputEvent, ctx: LayerContext): Promise<void> {
if (!isEvenHubStoreAvailable()) {
if (directionalFallback(rawEvent).type === "double-click") shell.yieldFocusToSidebar();
return;
}
const tabSwipe = !this.showingLogin && this.focus === "tabs"
&& (rawEvent.type === "swipe-left" || rawEvent.type === "swipe-right");
const event = isDirectionalInput(rawEvent) && !tabSwipe ? directionalFallback(rawEvent) : rawEvent;
async handleInput(event: InputEvent, ctx: LayerContext): Promise<void> {
if (this.showingLogin) {
if (event.type === "click" && !this.loginBusy) {
this.openCredentialEditor(ctx);
@@ -396,11 +301,9 @@ export class EvenHubStoreLayer implements Layer {
const index = TABS.findIndex((tab) => tab.id === this.activeTab);
switch (event.type) {
case "scroll-up":
case "swipe-left":
this.switchTab(ctx, TABS[Math.max(0, index - 1)]!.id);
return;
case "scroll-down":
case "swipe-right":
this.switchTab(ctx, TABS[Math.min(TABS.length - 1, index + 1)]!.id);
return;
case "click":
@@ -430,9 +333,6 @@ export class EvenHubStoreLayer implements Layer {
case "search-query":
this.openSearchEditor(ctx);
return;
case "update-all":
void this.updateAll(ctx);
return;
case "orphan": {
// Nothing can be reinstalled; the only useful action is to drop the
// dead registry entry so the launcher stops offering it.
@@ -458,12 +358,6 @@ export class EvenHubStoreLayer implements Layer {
* placeholder built from the registry entry stands in.
*/
showInstalledPackage(stack: LayerStack, installed: InstalledEvenHubApp): void {
if (!isEvenHubStoreAvailable()) {
this.unavailablePackageName = installed.name;
stack.clearToBase();
return;
}
this.unavailablePackageName = null;
if (this.showingLogin) {
this.pendingPackage = installed;
return;
@@ -475,7 +369,7 @@ export class EvenHubStoreLayer implements Layer {
/** Dictated text becomes a search (the shell's "Type Into App" / voice input). */
receiveTextInput(text: string, ctx: LayerContext): void {
if (this.showingLogin || !isEvenHubStoreAvailable()) return;
if (this.showingLogin) return;
const query = evenHubSearchQuerySetting.set(text);
if (!query) return;
this.closePhoneEditor(ctx.actions, "search");
@@ -485,7 +379,7 @@ export class EvenHubStoreLayer implements Layer {
}
buildMenuItems(): MenuItem[] {
if (this.showingLogin || !isEvenHubStoreAvailable()) return [];
if (this.showingLogin) return [];
const items: MenuItem[] = [];
if (this.activeTab === "search") {
items.push({
@@ -638,7 +532,8 @@ export class EvenHubStoreLayer implements Layer {
const rows: StoreRow[] = updates.map((update) =>
update.latest ? { kind: "app", app: update.latest, update } : { kind: "orphan", update },
);
this.setUpdateRows(state, rows, 0);
state.rows = rows;
state.menu.setItems(rows, 0);
state.menu.scrollTop = 0;
state.status = "";
state.exhausted = true;
@@ -653,20 +548,10 @@ export class EvenHubStoreLayer implements Layer {
}
}
private updatableRows(): AppRow[] {
return this.tabs.updates.rows.filter(isUpdatableRow);
}
/**
* Replace the Updates rows, putting an Update All row on top while any of
* them can be installed (and dropping it once none can).
*/
private setUpdateRows(state: TabState, entries: readonly StoreRow[], selectedIndex?: number): void {
const rows = entries.filter(isInstalledAppRow);
const count = rows.filter(isUpdatableRow).length;
if (count > 0) rows.unshift({ kind: "update-all", count, busy: this.updatingAll });
state.rows = rows;
state.menu.setItems(rows, selectedIndex);
private updatableRows(): Extract<StoreRow, { kind: "app" }>[] {
return this.tabs.updates.rows.filter(
(row): row is Extract<StoreRow, { kind: "app" }> => row.kind === "app" && row.update !== undefined,
);
}
/** Install every pending update in turn; a declined permission dialog skips that app. */
@@ -674,7 +559,6 @@ export class EvenHubStoreLayer implements Layer {
if (this.updatingAll) return;
this.updatingAll = true;
const state = this.tabs.updates;
this.setUpdateRows(state, state.rows);
const pending = this.updatableRows();
let installed = 0;
const failures: string[] = [];
@@ -709,7 +593,6 @@ export class EvenHubStoreLayer implements Layer {
: installed === 1 ? "Updated 1 app." : `Updated ${installed} apps.`;
} finally {
this.updatingAll = false;
this.setUpdateRows(this.tabs.updates, this.tabs.updates.rows);
if (!this.closed) ctx.actions.requestRender();
}
}
@@ -726,7 +609,10 @@ export class EvenHubStoreLayer implements Layer {
private removeUpdateRow(packageId: string): void {
const state = this.tabs.updates;
this.setUpdateRows(state, state.rows.filter((row) => rowPackageId(row) !== packageId));
state.rows = state.rows.filter((row) =>
row.kind === "search-query" ? true : row.kind === "app" ? row.app.packageId !== packageId : row.update.installed.packageId !== packageId,
);
state.menu.setItems(state.rows);
}
// ---- login and phone editors --------------------------------------------
@@ -865,14 +751,6 @@ function rowText(row: StoreRow): { title: string; detail: string; titleValue: nu
titleValue: 200,
};
}
case "update-all":
return {
title: row.busy ? "Updating..." : "Update All",
detail: row.busy
? "Installing updates; progress is shown above."
: row.count === 1 ? "Install the 1 available update" : `Install all ${row.count} available updates`,
titleValue: row.busy ? 150 : 230,
};
case "orphan":
return {
title: row.update.installed.name,
+24 -34
View File
@@ -19,21 +19,13 @@ import {
import { directionalFallback, type InputEvent } from "../../ui/gestures";
import { Layer, type LayerContext } from "../../ui/layers";
const LIST_X = 20;
/** List view: the selection box extends this far left and right of the row text. */
const LIST_HIGHLIGHT_PAD = 6;
/** Header text: how far in from the rows' sides, which lines it up with list-row text. */
const HEADER_INSET_X = LIST_HIGHLIGHT_PAD;
/** Between the header's title and the path after it. */
const HEADER_PATH_GAP = 14;
/** Under the header's line, before the rows. */
const HEADER_GAP = 2;
/**
* Bottom of the header: one line holding both the title and the current
* path, below the padding suggested for the window's top edge.
*/
function headerBottom(font: UiFont, paddingTop: number): number {
return paddingTop + font.lineHeight + HEADER_GAP;
/** Header: one line holding both the title and the current path. */
function headerHeight(font: UiFont): number {
return 10 + font.lineHeight;
}
export type FilesViewMode = "icons" | "list";
@@ -155,37 +147,35 @@ export class FileBrowserLayer implements Layer {
paint(ctx: LayerContext): GrayImage {
const font = getDefaultSmallFont();
const { width, height, padding } = ctx.stack.getBaseSize();
const { width, height } = ctx.stack.getBaseSize();
const image = new GrayImage(width, height, 0);
// The rows (grid bands, list selection boxes) span the window inside the
// padding suggested for its sides.
const rowsX = padding.left;
const rowsWidth = Math.max(0, width - padding.left - padding.right);
const titleX = rowsX + HEADER_INSET_X;
image.drawText(font, titleX, padding.top, "Files", 220);
image.drawText(font, 20, 8, "Files", 220);
const pathLabel = this.location === null ? "Places" : this.location;
const pathX = titleX + font.measureText("Files") + HEADER_PATH_GAP;
image.drawText(font, pathX, padding.top, truncateLeft(font, pathLabel, rowsX + rowsWidth - HEADER_INSET_X - pathX), 130);
const pathX = 20 + font.measureText("Files") + 14;
image.drawText(font, pathX, 8, truncateLeft(font, pathLabel, width - pathX - 20), 130);
// The header stays put and the rows scroll under it, as far as the
// window's bottom edge: the padding suggested for that edge goes inside
// the scrolled content, so the last row keeps clear of a border there
// and a row cut off for scrolling meets it rather than a blank strip.
const listView = filesViewModeSetting.get() === "list";
const headerEnd = headerBottom(font, padding.top);
// List row boxes start one pixel above the header's bottom edge.
const top = listView ? headerEnd - 1 : headerEnd;
const box = { x: rowsX, y: top, width: rowsWidth, height: Math.max(0, height - top), padTop: 0, padBottom: padding.bottom };
if (listView) {
this.listMenu.setItems(this.flatRows());
this.listMenu.paint(image, box, ctx.stack.isFocused());
if (filesViewModeSetting.get() === "list") {
this.paintList(image, font, ctx);
} else {
this.grid.paint(image, box, ctx.stack.isFocused());
const headerH = headerHeight(font);
this.grid.paint(image, { x: 0, y: headerH, width, height: height - headerH - 6 }, ctx.stack.isFocused());
}
return image;
}
private paintList(image: GrayImage, font: UiFont, ctx: LayerContext): void {
const { width, height } = ctx.stack.getBaseSize();
const headerH = headerHeight(font);
this.listMenu.setItems(this.flatRows());
// Row boxes start one pixel above the header's bottom edge.
this.listMenu.paint(
image,
{ x: LIST_X - LIST_HIGHLIGHT_PAD, y: headerH - 1, width: width - 2 * LIST_X + 2 * LIST_HIGHLIGHT_PAD, height: height - headerH - 4 },
ctx.stack.isFocused(),
);
}
private drawListRow({ image, item, x, y, width, height, selected }: MenuDrawArgs<BrowserItem>): void {
const font = getDefaultSmallFont();
const label = truncateText(font, item.label, width - 2 * LIST_HIGHLIGHT_PAD);
+1 -15
View File
@@ -2,7 +2,6 @@ import { readTextFile, type DirectoryEntry } from "../../native/file-access";
import { isFontFile } from "../../native/font-files";
import { installFontFile, isFontInstalled } from "../../graphics/installed-fonts";
import { isDecodableImageFile } from "../../native/image-files";
import { isMicRecordingFile } from "../../native/mic-recording";
import { isPlayableVideoFile } from "../../native/video-player";
import { readEvenHubPackageManifest } from "../evenhub/installed-apps";
import { EvenHubPermissionDialogLayer } from "../evenhub/permission-dialog";
@@ -39,8 +38,6 @@ export type FilesAppOptions = InProcessAppOptions & {
openFontWindow: (title: string, path: string) => void;
/** Open a video file's player (settings panel first) as its own shell window. */
openVideoWindow: (title: string, path: string) => void;
/** Replay a recording (.mka) through Transcribe, in a window of its own. */
openRecordingInTranscribe: (title: string, path: string) => void;
};
/**
@@ -53,7 +50,7 @@ export function createFilesAppWindow(options: FilesAppOptions): InProcessWindow
const browser = new FileBrowserLayer({
isSupportedFile: (name) =>
TEXT_FILE.test(name) || isFontFile(name) || EHPK_FILE.test(name) ||
(!global.isIOS && (isDecodableImageFile(name) || isPlayableVideoFile(name) || isMicRecordingFile(name))),
(!global.isIOS && (isDecodableImageFile(name) || isPlayableVideoFile(name))),
// The browser handles double-click itself (up a level), so it is not
// wrapped in YieldAtRootLayer; it yields explicitly from the top level.
onLeave: () => shell.yieldFocusToSidebar(),
@@ -238,17 +235,6 @@ function fileOpenActions(entry: DirectoryEntry, options: FilesAppOptions): FileI
},
];
}
if (isMicRecordingFile(entry.name)) {
return [
{
label: "Open in Transcribe",
onSelect: (ctx) => {
ctx.stack.pop();
options.openRecordingInTranscribe(entry.name, entry.path);
},
},
];
}
if (isPlayableVideoFile(entry.name)) {
return [
{
+16 -19
View File
@@ -5,8 +5,10 @@ import { canLoadFontFile, fontFileDisplayName, fontFileMetrics, renderFontFileTe
import { GESTURE_CLICK, GESTURE_DOUBLE_CLICK, GESTURE_SCROLL, gestureHints, type InputEvent } from "../../ui/gestures";
import { Layer, type LayerContext } from "../../ui/layers";
/** From the title's top to the first sample's. */
const TITLE_BAND = 18;
const MARGIN_X = 18;
const TITLE_Y = 8;
const BODY_TOP = 26;
const FOOTER_MARGIN = 20;
const SAMPLE_GAP = 4;
const SIZES = [8,9,10,11, 12, 13, 14, 16, 18, 20, 24, 28, 32, 40, 48] as const;
@@ -46,41 +48,36 @@ export class FontPreviewerLayer implements Layer {
paint(ctx: LayerContext): GrayImage {
const font = getDefaultSmallFont();
const { width, height, padding } = ctx.stack.getBaseSize();
const { width, height } = ctx.stack.getBaseSize();
const out = new GrayImage(width, height, 0);
const size = SIZES[this.sizeIndex]!;
const gamma = GAMMAS[this.gammaIndex]!;
// Everything keeps inside the padding suggested for the window's edges:
// the title line at the top, the samples across the width, the hint
// line at the bottom.
const left = padding.left;
const bodyWidth = Math.max(0, width - padding.left - padding.right);
const bodyTop = padding.top + TITLE_BAND;
const status = `${size}px γ ${gamma.toFixed(1)}`;
const statusWidth = font.measureText(status);
const title = truncateText(font, this.getDisplayName(), bodyWidth - statusWidth - 12);
out.drawText(font, left, padding.top, title, 220);
out.drawText(font, left + bodyWidth - statusWidth, padding.top, status, 150);
const title = truncateText(font, this.getDisplayName(), width - 2 * MARGIN_X - statusWidth - 12);
out.drawText(font, MARGIN_X, TITLE_Y, title, 220);
out.drawText(font, width - MARGIN_X - statusWidth, TITLE_Y, status, 150);
if (this.loadable === null) {
this.loadable = canLoadFontFile(this.path);
}
const footerY = height - padding.bottom - font.lineHeight;
const footerY = height - FOOTER_MARGIN;
if (!this.loadable) {
out.drawText(font, left, bodyTop + 16, "(could not load font)", 160);
out.drawText(font, left, footerY, gestureHints([[GESTURE_DOUBLE_CLICK, "back"]]), 110);
out.drawText(font, MARGIN_X, BODY_TOP + 16, "(could not load font)", 160);
out.drawText(font, MARGIN_X, footerY, gestureHints([[GESTURE_DOUBLE_CLICK, "back"]]), 110);
return out;
}
let y = bodyTop;
const bodyWidth = width - 2 * MARGIN_X;
let y = BODY_TOP;
for (const [id, text] of [
["pangram", PANGRAM],
["charset", CHARSET],
] as const) {
const line = this.getCached(id, () => renderFontFileText(this.path, text, size, gamma));
if (!line) continue;
out.bitBlt(line, left, y, { width: bodyWidth });
out.bitBlt(line, MARGIN_X, y, { width: bodyWidth });
y += line.height + SAMPLE_GAP;
}
@@ -100,13 +97,13 @@ export class FontPreviewerLayer implements Layer {
}),
);
if (paragraph) {
out.bitBlt(paragraph, left, y + SAMPLE_GAP);
out.bitBlt(paragraph, MARGIN_X, y + SAMPLE_GAP);
}
}
out.drawText(
font,
left,
MARGIN_X,
footerY,
gestureHints([
[GESTURE_SCROLL, "size"],
+13 -21
View File
@@ -5,9 +5,9 @@ import { loadImageFileAsGray, PHOTO_TONE } from "../../native/image-files";
import { GESTURE_DOUBLE_CLICK, type InputEvent } from "../../ui/gestures";
import { Layer, type LayerContext } from "../../ui/layers";
/** From the title's top to the image area's. */
const TITLE_BAND = 16;
/** How far the image area keeps from the window's sides and bottom. */
const MARGIN_X = 18;
const TITLE_Y = 8;
const BODY_TOP = 24;
const BODY_MARGIN = 2;
/**
@@ -19,8 +19,7 @@ const BODY_MARGIN = 2;
*/
export class ImageViewerLayer implements Layer {
private image: GrayImage | null = null;
/** The image area ("WxH") the file was last decoded to fit; null before the first load. */
private fittedTo: string | null = null;
private loadAttempted = false;
constructor(
private readonly path: string,
@@ -29,31 +28,24 @@ export class ImageViewerLayer implements Layer {
paint(ctx: LayerContext): GrayImage {
const font = getDefaultSmallFont();
const { width, height, padding } = ctx.stack.getBaseSize();
const { width, height } = ctx.stack.getBaseSize();
const out = new GrayImage(width, height, 0);
// The title line keeps inside the padding suggested for the window's
// edges; the image fills the rest of the window whatever its edges touch.
out.drawText(font, padding.left, padding.top, truncateText(font, this.title, width - padding.left - padding.right), 220);
out.drawText(font, MARGIN_X, TITLE_Y, truncateText(font, this.title, width - 2 * MARGIN_X), 220);
const bodyTop = padding.top + TITLE_BAND;
const bodyWidth = width - 2 * BODY_MARGIN;
const bodyHeight = height - bodyTop - BODY_MARGIN;
// The area moves with the window's size and its top padding, so the
// file is decoded again whenever it changes rather than once.
const fit = `${bodyWidth}x${bodyHeight}`;
if (this.fittedTo !== fit) {
this.fittedTo = fit;
this.image = loadImageFileAsGray(this.path, bodyWidth, bodyHeight, PHOTO_TONE);
const bodyHeight = height - BODY_TOP - BODY_MARGIN;
if (!this.loadAttempted) {
this.loadAttempted = true;
this.image = loadImageFileAsGray(this.path, width - 2 * BODY_MARGIN, bodyHeight, PHOTO_TONE);
}
if (!this.image) {
out.drawText(font, padding.left, bodyTop + 16, "(could not load image)", 160);
out.drawText(font, padding.left, height - padding.bottom - font.lineHeight, `${GESTURE_DOUBLE_CLICK} back`, 110);
out.drawText(font, MARGIN_X, BODY_TOP + 16, "(could not load image)", 160);
out.drawText(font, MARGIN_X, height - 16, `${GESTURE_DOUBLE_CLICK} back`, 110);
return out;
}
out.bitBlt(
this.image,
Math.round((width - this.image.width) / 2),
bodyTop + Math.round((bodyHeight - this.image.height) / 2),
BODY_TOP + Math.round((bodyHeight - this.image.height) / 2),
);
return out;
}
-4
View File
@@ -14,7 +14,6 @@ import {
FILES_WINDOW_ID,
} from "./files-app";
import { createVideoPlayerWindow } from "./video-window";
import { openRecordingInTranscribe } from "../transcribe";
// Document windows are closeable and non-singleton; the serial keeps their
// windowIds unique.
@@ -92,9 +91,6 @@ const filesApp: AppDefinition = {
},
openFontWindow: (title, path) => openFontDocumentWindow(ctx, title, path),
openVideoWindow: (title, path) => openVideoWindow(ctx, title, path),
openRecordingInTranscribe: (title, path) => {
openRecordingInTranscribe(ctx, path, title);
},
}),
),
// A document arriving via Android's Share intent opens as its own window.
+18 -23
View File
@@ -5,9 +5,11 @@ import { GrayImage, type UiFont } from "../../graphics/image";
import { type InputEvent } from "../../ui/gestures";
import { Layer, type LayerContext } from "../../ui/layers";
/** The title: how far right of the body's column, and from its top to the first body line's. */
const TITLE_INSET_X = 4;
const TITLE_BAND = 28;
const MARGIN_X = 18;
const TITLE_Y = 16;
const BODY_X = 18;
const BODY_Y = 44;
const FOOTER_MARGIN = 36;
/** Line step for a bitmap face (the 12px font plus leading, as before). */
const BDF_LINE_STEP = 14;
@@ -22,11 +24,8 @@ function lineStepOf(font: UiFont): number {
}
/**
* Paged text viewer for a document: a title line, the page's lines, and a
* page-number line, all inside the padding suggested for the window's edges
* (the title and page number stay put, so the lines page between them).
* Sized to its hosting stack (a Files document window, or pushed over the
* file browser for view-in-place).
* Paged text viewer for a document. Sized to its hosting stack (a Files
* document window, or pushed over the file browser for view-in-place).
*/
export class TextViewerLayer implements Layer {
private lines: string[] | null = null;
@@ -43,23 +42,20 @@ export class TextViewerLayer implements Layer {
paint(ctx: LayerContext): GrayImage {
const font = getViewerFont();
const lineStep = lineStepOf(font);
const { width, height, padding } = ctx.stack.getBaseSize();
const { width, height } = ctx.stack.getBaseSize();
const image = new GrayImage(width, height, 0);
const bodyX = padding.left;
const bodyWidth = Math.max(0, width - padding.left - padding.right);
const bodyY = padding.top + TITLE_BAND;
const footerY = height - padding.bottom - font.lineHeight;
this.bodyLineCount = Math.max(1, Math.floor((footerY - bodyY) / lineStep));
image.drawText(font, bodyX + TITLE_INSET_X, padding.top, truncateText(font, this.title, bodyWidth - 2 * TITLE_INSET_X), 220);
const footerY = height - FOOTER_MARGIN;
this.bodyLineCount = Math.max(1, Math.floor((footerY - BODY_Y) / lineStep));
image.drawText(font, MARGIN_X + 4, TITLE_Y, truncateText(font, this.title, width - 2 * MARGIN_X - 8), 220);
const lines = this.getLines(font, bodyWidth);
const lines = this.getLines(font, width);
const visibleLines = lines.slice(this.firstLine, this.firstLine + this.bodyLineCount);
for (let index = 0; index < visibleLines.length; index++) {
image.drawText(font, bodyX, bodyY + index * lineStep, visibleLines[index]!, 230);
image.drawText(font, BODY_X, BODY_Y + index * lineStep, visibleLines[index]!, 230);
}
const currentPage = Math.floor(this.firstLine / this.pageStep()) + 1;
image.drawText(font, bodyX, footerY, `Page ${currentPage}/${this.totalPageCount(lines.length)}`, 110);
image.drawText(font, BODY_X, footerY, `Page ${currentPage}/${this.totalPageCount(lines.length)}`, 110);
return image;
}
@@ -93,15 +89,14 @@ export class TextViewerLayer implements Layer {
this.firstLine = Math.max(0, Math.min(maxFirstLine, this.firstLine + delta));
}
/** The document wrapped to the body column's width; cached per width and font. */
private getLines(font: UiFont, bodyWidth: number): string[] {
if (this.lines === null || this.wrappedForWidth !== bodyWidth || this.wrappedWithFont !== font.fingerprintId) {
private getLines(font: UiFont, width: number): string[] {
if (this.lines === null || this.wrappedForWidth !== width || this.wrappedWithFont !== font.fingerprintId) {
const normalized = this.documentText.replace(/\t/g, " ").replace(/\r/g, "");
this.lines = wrapText(font, normalized, bodyWidth, {
this.lines = wrapText(font, normalized, width - BODY_X - 12, {
preserveLeadingWhitespace: true,
breakLongWords: true,
});
this.wrappedForWidth = bodyWidth;
this.wrappedForWidth = width;
this.wrappedWithFont = font.fingerprintId;
}
return this.lines;
+14 -24
View File
@@ -53,11 +53,9 @@ export const videoMonochromeSetting = new ConfigSettingBoolean({
/** How far one ring scroll seeks. */
const SEEK_STEP_MS = 10_000;
/** The settings panel's rows: at most this wide, their text this far inside their selection box. */
const MARGIN_X = 18;
const TITLE_Y = 10;
const PANEL_WIDTH = 340;
const PANEL_ROW_TEXT_INSET = 10;
/** The panel's title: how far below the window's top padding. */
const TITLE_OFFSET_Y = 2;
/**
* One opened video: its probe result, the native player, and the window
@@ -204,9 +202,7 @@ type PanelRow = { kind: "size" } | { kind: "audio" } | { kind: "monochrome" } |
/**
* The pre-play settings panel (the window's root): picture size, audio and
* monochrome, all remembered for the next video, then Play (or Resume plus
* Play from start once a position is kept). The rows' selection boxes start
* at the padding suggested for the window's left edge, and the title and
* the lines under it line up with the rows' text.
* Play from start once a position is kept).
*/
class VideoSettingsLayer implements Layer {
private readonly menu = new Menu<PanelRow>({
@@ -228,16 +224,11 @@ class VideoSettingsLayer implements Layer {
paint(ctx: LayerContext): GrayImage {
const font = getDefaultSmallFont();
const { width, height, padding } = ctx.stack.getBaseSize();
const { width, height } = ctx.stack.getBaseSize();
const image = new GrayImage(width, height, 0);
const rowsX = padding.left;
const available = Math.max(0, width - padding.left - padding.right);
const textX = rowsX + PANEL_ROW_TEXT_INSET;
const textWidth = available - 2 * PANEL_ROW_TEXT_INSET;
const titleY = padding.top + TITLE_OFFSET_Y;
image.drawText(font, textX, titleY, truncateText(font, this.session.title, textWidth), 220);
image.drawText(font, MARGIN_X, TITLE_Y, truncateText(font, this.session.title, width - 2 * MARGIN_X), 220);
const step = lineStep(font);
let y = titleY + step;
let y = TITLE_Y + step;
const info = this.session.info;
const errors: string[] = [];
@@ -245,12 +236,12 @@ class VideoSettingsLayer implements Layer {
if (this.session.error) errors.push(`Playback stopped: ${this.session.error}`);
if (info) {
const facts = [`${info.width}x${info.height}`, formatMediaTime(info.durationMs), info.hasAudio ? "sound" : "no sound"];
image.drawText(font, textX, y, facts.join(" · "), 130);
image.drawText(font, MARGIN_X, y, facts.join(" · "), 130);
y += step;
}
for (const error of errors) {
for (const line of wrapText(font, error, textWidth, { breakLongWords: true }).slice(0, 3)) {
image.drawText(font, textX, y, line, 200);
for (const line of wrapText(font, error, width - 2 * MARGIN_X, { breakLongWords: true }).slice(0, 3)) {
image.drawText(font, MARGIN_X, y, line, 200);
y += step;
}
}
@@ -262,10 +253,9 @@ class VideoSettingsLayer implements Layer {
// Land on Resume/Play by default: the settings usually stay as they were.
const playIndex = rows.findIndex((row) => row.kind === "play");
this.menu.setItems(rows, index >= 0 ? index : playIndex >= 0 ? playIndex : undefined);
// The rows scroll under the fixed lines above, as far as the window's
// bottom edge, with the padding suggested for that edge inside the list.
this.menu.paint(image, { x: rowsX, y, width: Math.min(PANEL_WIDTH, available), height: Math.max(0, height - y),
padTop: 0, padBottom: padding.bottom }, ctx.stack.isFocused());
const panelWidth = Math.min(PANEL_WIDTH, width - 2 * (MARGIN_X - 6));
this.menu.paint(image, { x: MARGIN_X - 6, y, width: panelWidth, height: Math.max(0, height - y - 4) },
ctx.stack.isFocused());
return image;
}
@@ -304,8 +294,8 @@ class VideoSettingsLayer implements Layer {
private drawRow({ image, item, x, y, width, selected }: MenuDrawArgs<PanelRow>): void {
const font = getDefaultSmallFont();
const textX = x + PANEL_ROW_TEXT_INSET;
const textWidth = width - 2 * PANEL_ROW_TEXT_INSET;
const textX = x + 10;
const textWidth = width - 20;
switch (item.kind) {
case "size": {
const size = videoSizeSetting.get();
+5 -26
View File
@@ -41,7 +41,6 @@ import { loadSoundEnabled, saveSoundEnabled } from "../../ui/sound-setting";
import { type MenuItem } from "../../ui/menu";
import { WindowMenu } from "../../ui/window-menu";
import type { WorkerAppMessage, WorkerAppReply } from "../../ui/shell/worker-window";
import type { Viewport } from "../../ui/viewport";
import {
directionalFallback,
GESTURE_CLICK,
@@ -96,12 +95,6 @@ const RENDER_TICK_MS = 30;
/** Physics steps per wing frame while flying. */
const WING_STEPS_PER_FRAME = 6;
// --- HUD (pixels below the top line's y) --------------------------------------
/** The title: a shorter font than the score's, centered on the same line. */
const HUD_TITLE_DROP = 4;
/** The best score, under the title. */
const HUD_BEST_DROP = 34;
const SFX_FLAP: Step[] = [{ freq: 880, duty: 40, ms: 20 }, { freq: 1175, duty: 40, ms: 25 }];
const SFX_SCORE: Step[] = [{ freq: 1568, duty: 40, ms: 40 }, { freq: 2093, duty: 40, ms: 60 }];
const SFX_HIT: Step[] = [{ freq: 220, duty: 55, ms: 80 }, { freq: 165, duty: 55, ms: 120 }];
@@ -142,13 +135,6 @@ type FlappyWindow = {
title: string;
viewportWidth: number;
viewportHeight: number;
/**
* The viewport as the shell last sent it: besides the size above, what each
* edge touches and the padding suggested there. The playfield fills the
* window whatever its edges touch; only the HUD's top line keeps the
* padding (see paintHud).
*/
viewport: Viewport;
foreground: boolean;
/** Whether this window is the shell's input target (pushed with each message). */
focused: boolean;
@@ -212,7 +198,6 @@ global.onmessage = (event: { data: WorkerAppMessage }) => {
title: message.title,
viewportWidth: message.viewport.width,
viewportHeight: message.viewport.height,
viewport: message.viewport,
foreground: false,
focused: false,
menu: null,
@@ -240,8 +225,7 @@ global.onmessage = (event: { data: WorkerAppMessage }) => {
if (!window) break;
window.viewportWidth = message.viewport.width;
window.viewportHeight = message.viewport.height;
window.viewport = message.viewport;
window.menu?.resize(message.viewport);
window.menu?.resize({ width: window.viewportWidth, height: window.viewportHeight });
if (window.foreground) renderAndSubmit(window, 0);
break;
}
@@ -305,7 +289,6 @@ global.onmessage = (event: { data: WorkerAppMessage }) => {
break;
case "navigation-sensors":
case "text-input":
case "key":
case "tool-call":
break;
}
@@ -389,7 +372,7 @@ function windowMenu(window: FlappyWindow): WindowMenu {
post,
title: () => window.title,
items: () => windowMenuItems(window),
size: window.viewport,
size: { width: window.viewportWidth, height: window.viewportHeight },
paintBase: () => paintContent(window),
isFocused: () => window.focused,
});
@@ -729,18 +712,14 @@ function birdWingFrame(window: FlappyWindow): number {
function paintHud(image: GrayImage, window: FlappyWindow): void {
const width = window.viewportWidth;
// The top line (the score, or the title before the first flap) is the
// only thing here placed from a window edge: it keeps the padding
// suggested for the top, and the best score hangs under it.
const top = window.viewport.padding.top;
if (window.phase !== "ready") {
drawCenteredIn(image, largeFont, 0, width, top, String(window.score), 245);
drawCenteredIn(image, largeFont, 0, width, 10, String(window.score), 245);
}
switch (window.phase) {
case "ready":
drawCenteredIn(image, mediumFont, 0, width, top + HUD_TITLE_DROP, "FLAPPY", 245);
drawCenteredIn(image, mediumFont, 0, width, 14, "FLAPPY", 245);
if (window.highScore > 0) {
drawCenteredIn(image, smallFont, 0, width, top + HUD_BEST_DROP, `best ${window.highScore}`, 150);
drawCenteredIn(image, smallFont, 0, width, 44, `best ${window.highScore}`, 150);
}
drawCenteredIn(image, smallFont, 0, width, groundTop(window) - 26, "Touch ring / swipe to flap", 150);
break;
+22 -88
View File
@@ -33,7 +33,6 @@ import { loadSoundEnabled, saveSoundEnabled } from "../../ui/sound-setting";
import { WindowMenu } from "../../ui/window-menu";
import type { MenuItem } from "../../ui/menu";
import type { WorkerAppMessage, WorkerAppReply } from "../../ui/shell/worker-window";
import type { Viewport } from "../../ui/viewport";
import { directionalFallback, GESTURE_CLICK, GESTURE_DOUBLE_CLICK, GESTURE_LONG_PRESS, type InputEvent } from "../../ui/gestures";
declare const global: any;
@@ -62,16 +61,8 @@ function isRed(card: number): boolean {
const CARD_W = 60;
const CARD_H = 36;
/** How far outside the card or cascade it marks the cursor's outline is drawn. */
const CURSOR_OUTSET = 3;
/** Gap between the top row (free cells and foundations) and the cascades. */
const ROW_GAP = 8;
/** The most of a buried cascade card left showing: enough for its label. */
const MAX_STRIP = 16;
/** Least column pitch that keeps the cursor's outline round one column off the next column's cards. */
const MIN_PITCH = CARD_W + CURSOR_OUTSET;
/** Height of the longest cascade of a fresh deal (7 cards) fanned at MAX_STRIP. */
const DEALT_CASCADE_H = 6 * MAX_STRIP + CARD_H;
const TOP_Y = 4;
const CASCADE_Y = 48;
/**
* Cursor locations, in scroll order: free cells 0-3, foundations 0-3 (the
@@ -115,12 +106,6 @@ type FreecellWindow = {
title: string;
viewportWidth: number;
viewportHeight: number;
/**
* The viewport as the shell last sent it: besides the size above, what each
* edge touches and the padding suggested there, which the board is laid
* out inside (see layoutFor).
*/
viewport: Viewport;
foreground: boolean;
/** Whether this window is the shell's input target (pushed with each message). */
focused: boolean;
@@ -170,7 +155,6 @@ global.onmessage = (event: { data: WorkerAppMessage }) => {
title: message.title,
viewportWidth: message.viewport.width,
viewportHeight: message.viewport.height,
viewport: message.viewport,
foreground: false,
focused: false,
menu: null,
@@ -226,8 +210,7 @@ global.onmessage = (event: { data: WorkerAppMessage }) => {
if (!window) break;
window.viewportWidth = message.viewport.width;
window.viewportHeight = message.viewport.height;
window.viewport = message.viewport;
window.menu?.resize(message.viewport);
window.menu?.resize({ width: window.viewportWidth, height: window.viewportHeight });
window.lastSubmittedFingerprint = "";
if (window.foreground) renderAndSubmit(window, 0);
break;
@@ -236,7 +219,6 @@ global.onmessage = (event: { data: WorkerAppMessage }) => {
case "navigation-sensors":
case "screen":
case "text-input":
case "key":
case "tool-call":
break;
}
@@ -304,7 +286,7 @@ function windowMenu(window: FreecellWindow): WindowMenu {
post,
title: () => window.title,
items: () => windowMenuItems(window),
size: window.viewport,
size: { width: window.viewportWidth, height: window.viewportHeight },
paintBase: () => paintContent(window),
isFocused: () => window.focused,
});
@@ -724,67 +706,21 @@ function checkWin(window: FreecellWindow): void {
// Painting
type Layout = {
/** Distance between the left edges of neighboring columns. */
pitch: number;
/** Left edge of the first column's cards. */
marginX: number;
/** Top of the free cells and foundations. */
topY: number;
/** Top of the cascades. */
cascadeY: number;
/** Vertical pixels per buried cascade card (its visible label strip). */
strip: (cascadeLength: number) => number;
};
/**
* The eight columns across a window of the given width, keeping the given
* padding at its left and right edges: spread as far apart as a whole-pixel
* pitch allows, with what is left over split between the two sides.
*/
function columnLayout(width: number, padLeft: number, padRight: number): Pick<Layout, "pitch" | "marginX"> {
const left = padLeft + CURSOR_OUTSET;
const slack = width - padRight - CURSOR_OUTSET - left - CARD_W;
const pitch = Math.floor(slack / 7);
return { pitch, marginX: left + Math.floor((slack - pitch * 7) / 2) };
}
/**
* The two rows down a window of the given height, keeping the given padding
* at its top and bottom edges, and the height that leaves a cascade to fan
* out in.
*/
function rowLayout(
height: number,
padTop: number,
padBottom: number,
): Pick<Layout, "topY" | "cascadeY"> & { cascadeH: number } {
const topY = padTop + CURSOR_OUTSET;
const cascadeY = topY + CARD_H + ROW_GAP;
return { topY, cascadeY, cascadeH: height - padBottom - CURSOR_OUTSET - cascadeY };
}
/**
* Lay the board out in the window. The cursor's outline is the outermost
* thing drawn toward every edge (CURSOR_OUTSET outside whatever it marks), so
* it is what sits at the padding suggested for the edge; the columns spread
* across the width inside it, and the cascades fan down the height. A
* window too small for that (the RayNeo iO's) is short of room for the cards
* as it is, so along the axis where they would crowd, they take the
* padding's room too.
*/
function layoutFor(window: FreecellWindow): Layout {
const { padding } = window.viewport;
let columns = columnLayout(window.viewportWidth, padding.left, padding.right);
if (columns.pitch < MIN_PITCH) columns = columnLayout(window.viewportWidth, 0, 0);
let rows = rowLayout(window.viewportHeight, padding.top, padding.bottom);
if (rows.cascadeH < DEALT_CASCADE_H) rows = rowLayout(window.viewportHeight, 0, 0);
const { topY, cascadeY, cascadeH } = rows;
const pitch = Math.floor((window.viewportWidth - 12) / 8);
const marginX = Math.floor((window.viewportWidth - (pitch * 8 - (pitch - CARD_W))) / 2);
const available = window.viewportHeight - CASCADE_Y - 2;
return {
...columns,
topY,
cascadeY,
pitch,
marginX,
strip: (cascadeLength: number) =>
cascadeLength <= 1 ? 0 : Math.min(MAX_STRIP, Math.floor((cascadeH - CARD_H) / (cascadeLength - 1))),
cascadeLength <= 1 ? 0 : Math.min(16, Math.floor((available - CARD_H) / (cascadeLength - 1))),
};
}
@@ -807,26 +743,25 @@ function paintContent(window: FreecellWindow): GrayImage {
const image = new GrayImage(window.viewportWidth, window.viewportHeight, 0);
const layout = layoutFor(window);
const colX = (i: number) => layout.marginX + i * layout.pitch;
const { topY } = layout;
// Top row: free cells then foundations, with a divider between the groups.
for (let i = 0; i < 4; i++) {
const card = window.cells[i]!;
if (card === null) {
image.drawRoundedRect(colX(i), topY, CARD_W, CARD_H, 70, 4);
image.drawRoundedRect(colX(i), TOP_Y, CARD_W, CARD_H, 70, 4);
} else {
paintCard(image, colX(i), topY, card, window.selected === LOC_CELL0 + i, true);
paintCard(image, colX(i), TOP_Y, card, window.selected === LOC_CELL0 + i, true);
}
}
image.drawLine(colX(4) - 4, topY + 4, colX(4) - 4, topY + CARD_H - 4, 70);
image.drawLine(colX(4) - 4, TOP_Y + 4, colX(4) - 4, TOP_Y + CARD_H - 4, 70);
for (let i = 0; i < 4; i++) {
const x = colX(4 + i);
const card = window.foundations[i]!;
if (card === null) {
image.drawRoundedRect(x, topY, CARD_W, CARD_H, 70, 4);
image.drawText(labelFont, x + Math.round((CARD_W - labelFont.measureText("A")) / 2), topY + 10, "A", 70);
image.drawRoundedRect(x, TOP_Y, CARD_W, CARD_H, 70, 4);
image.drawText(labelFont, x + Math.round((CARD_W - labelFont.measureText("A")) / 2), TOP_Y + 10, "A", 70);
} else {
paintCard(image, x, topY, card, window.selected === LOC_FOUNDATION0 + i, true);
paintCard(image, x, TOP_Y, card, window.selected === LOC_FOUNDATION0 + i, true);
}
}
@@ -842,7 +777,7 @@ function paintCascade(image: GrayImage, window: FreecellWindow, layout: Layout,
const x = layout.marginX + index * layout.pitch;
const cascade = window.cascades[index]!;
if (cascade.length === 0) {
image.drawRoundedRect(x, layout.cascadeY, CARD_W, CARD_H, 45, 4);
image.drawRoundedRect(x, CASCADE_Y, CARD_W, CARD_H, 45, 4);
return;
}
const strip = layout.strip(cascade.length);
@@ -853,7 +788,7 @@ function paintCascade(image: GrayImage, window: FreecellWindow, layout: Layout,
highlightFrom = cascade.length - run;
}
for (let i = 0; i < cascade.length; i++) {
paintCard(image, x, layout.cascadeY + i * strip, cascade[i]!, i >= highlightFrom, i === cascade.length - 1);
paintCard(image, x, CASCADE_Y + i * strip, cascade[i]!, i >= highlightFrom, i === cascade.length - 1);
}
}
@@ -896,18 +831,17 @@ function paintCursor(image: GrayImage, window: FreecellWindow, layout: Layout):
let height: number;
if (window.cursor < LOC_CASCADE0) {
x = layout.marginX + (window.cursor % 8) * layout.pitch;
y = layout.topY;
y = TOP_Y;
height = CARD_H;
} else {
const index = window.cursor - LOC_CASCADE0;
const cascade = window.cascades[index]!;
x = layout.marginX + index * layout.pitch;
y = layout.cascadeY;
y = CASCADE_Y;
height = cascade.length === 0 ? CARD_H : (cascade.length - 1) * layout.strip(cascade.length) + CARD_H;
}
for (const outset of [CURSOR_OUTSET, CURSOR_OUTSET - 1]) {
image.drawRect(x - outset, y - outset, CARD_W + 2 * outset, height + 2 * outset, 255);
}
image.drawRect(x - 3, y - 3, CARD_W + 6, height + 6, 255);
image.drawRect(x - 2, y - 2, CARD_W + 4, height + 4, 255);
}
function paintWinOverlay(image: GrayImage, window: FreecellWindow): void {
+48 -89
View File
@@ -5,14 +5,11 @@ import { enumSettingMenuItem, onAnySettingChanged, toggleSettingMenuItem } from
import { uiFontOverridePickerMenuItem } from "../../ui/font-picker";
import { type InputEvent } from "../../ui/gestures";
import { type Layer, type LayerContext } from "../../ui/layers";
import { drawSelectionHighlight, isMenuItemDisabled, menuHeadingItem, MenuLayer, openModalMenu, submenuItem, type MenuItem } from "../../ui/menu";
import { liveHeadPitchFooter } from "../../g2/head-angle";
import { headUpControlsAvailable } from "../../native/glasses-controls";
import { drawSelectionHighlight, isMenuItemDisabled, MenuLayer, openModalMenu, submenuItem, type MenuItem } from "../../ui/menu";
import { Menu } from "../../ui/menu-core";
import { LIST_ROW_TEXT_INSET, lineStep, listRowHeight } from "../../ui/metrics";
import { createInProcessWindow, type InProcessAppOptions, type InProcessWindow } from "../../ui/shell/in-process-window";
import { shell } from "../../ui/shell/shell";
import type { Viewport } from "../../ui/viewport";
import { GlanceBoard } from "./board";
import { glanceFont } from "./glance-font";
import {
@@ -22,9 +19,7 @@ import {
glanceLayout,
glanceLayoutSetting,
glanceShowLinesSetting,
glanceAngleSetting,
glanceShowOnHeadTiltSetting,
glanceTiltTimeoutSetting,
glanceShowOnLongPressSetting,
glanceSlotChoiceLabel,
glanceSlotSettings,
@@ -38,20 +33,17 @@ import { findGlanceWidget, glanceWidgetSpans } from "./widgets";
export const GLANCEBOARD_WINDOW_ID = "glanceboard";
export const GLANCEBOARD_SURFACE_ID = "window:glanceboard";
/** The entry page's title: its drop below the top padding. It lines up with the menu rows' text. */
const TITLE_OFFSET_Y = 2;
/** Indent of the intro paragraph under the title, and how far short of the middle it wraps. */
const PARAGRAPH_INDENT = 6;
const PARAGRAPH_RIGHT_GAP = 12;
/** The entry page splits at the middle: menu left, slot preview right, this far from the split. */
const TITLE_X = 18;
const TITLE_Y = 10;
const PARAGRAPH_X = 24;
/** The entry page splits at the middle: menu left, slot preview right. */
const PREVIEW_MARGIN = 8;
/** The preview's outline, drawn just outside the scaled board. */
const PREVIEW_OUTLINE = 1;
const PREVIEW_MIN_LINE_VALUE = 20;
const PREVIEW_LINE_VALUE = 100;
/** The entry menu's row boxes: gap below the paragraph, and how far short of the middle they end. */
/** The entry menu's row boxes: left inset, gap below the paragraph, and inset from the page's bottom. */
const HOME_MENU_X = 20;
const HOME_MENU_TOP_GAP = 12;
const HOME_MENU_RIGHT_GAP = 20;
const HOME_MENU_BOTTOM_MARGIN = 8;
/** Horizontal inset of row text (and custom row content) from its selection box. */
const HOME_ROW_TEXT_X = 10;
// A throwaway menu to satisfy MenuItem.onSelect's second parameter; the
@@ -64,7 +56,9 @@ const INTRO =
"long-press-and-hold. It will close automatically.";
/** Full-page list layout shared by the sub-screens. */
const PAGE_MENU_LAYOUT = { page: true } as const;
function pageMenuLayout(width: number) {
return { x: 8, y: 8, width: width - 16, opaque: true, showBorder: false } as const;
}
/**
* The Glanceboard app's entry page. Left half: title, a short description
@@ -73,10 +67,6 @@ const PAGE_MENU_LAYOUT = { page: true } as const;
* name of the widget in each. "Select Contents" moves focus into that
* preview, where scroll picks a slot and a tap opens the picker for it;
* a double-tap returns focus to the menu.
*
* The menu's row boxes and the preview's outline sit at the padding
* suggested for the window's edges (viewport.ts). The menu scrolls, so it
* runs to the bottom edge itself, with that edge's padding after its last row.
*/
class GlanceboardHomeLayer implements Layer {
private readonly menu: Menu<MenuItem>;
@@ -135,23 +125,17 @@ class GlanceboardHomeLayer implements Layer {
});
}
/**
* Where the scaled board sits: board aspect, centred in the right half's
* room inside the window's padding, which its outline may reach.
*/
private previewRect(viewport: Viewport): { x: number; y: number; width: number; height: number } {
const { width, height, padding } = viewport;
const left = ((width / 2) | 0) + PREVIEW_MARGIN;
const top = padding.top + PREVIEW_OUTLINE;
const availableWidth = Math.max(0, width - padding.right - PREVIEW_OUTLINE - left);
const availableHeight = Math.max(0, height - padding.bottom - PREVIEW_OUTLINE - top);
/** Where the scaled board sits: the right half, board aspect, vertically centred. */
private previewRect(width: number, height: number): { x: number; y: number; width: number; height: number } {
const half = (width / 2) | 0;
const availableWidth = width - half - 2 * PREVIEW_MARGIN;
const layout = glanceLayout();
const scale = Math.min(availableWidth / layout.width, availableHeight / layout.height);
const scale = Math.min(availableWidth / layout.width, (height - 2 * PREVIEW_MARGIN) / layout.height);
const previewWidth = Math.round(layout.width * scale);
const previewHeight = Math.round(layout.height * scale);
return {
x: left + (((availableWidth - previewWidth) / 2) | 0),
y: top + (((availableHeight - previewHeight) / 2) | 0),
x: half + PREVIEW_MARGIN + (((availableWidth - previewWidth) / 2) | 0),
y: ((height - previewHeight) / 2) | 0,
width: previewWidth,
height: previewHeight,
};
@@ -159,37 +143,32 @@ class GlanceboardHomeLayer implements Layer {
paint(ctx: LayerContext): GrayImage {
const font = getDefaultSmallFont();
const viewport = ctx.stack.getBaseSize();
const { width, height, padding } = viewport;
const { width, height } = ctx.stack.getBaseSize();
const image = new GrayImage(width, height, 0);
const half = (width / 2) | 0;
const menuX = padding.left;
const titleX = menuX + HOME_ROW_TEXT_X;
const titleY = padding.top + TITLE_OFFSET_Y;
image.drawText(font, titleX, titleY, "Glanceboard", 220);
image.drawText(font, TITLE_X, TITLE_Y, "Glanceboard", 220);
const step = lineStep(font);
const paragraphX = titleX + PARAGRAPH_INDENT;
let y = titleY + step + 6;
for (const line of wrapText(font, INTRO, half - PARAGRAPH_RIGHT_GAP - paragraphX)) {
image.drawText(font, paragraphX, y, line, 160);
let y = TITLE_Y + step + 6;
for (const line of wrapText(font, INTRO, half - PARAGRAPH_X - 12)) {
image.drawText(font, PARAGRAPH_X, y, line, 160);
y += step;
}
// The menu sits below the paragraph in the left half. While the preview
// has focus the menu keeps its outline-only selection.
const menuTop = y + HOME_MENU_TOP_GAP;
const menuWidth = Math.max(0, half - HOME_MENU_RIGHT_GAP - menuX);
const menuHeight = Math.max(0, height - menuTop);
const menuWidth = half - 2 * HOME_MENU_X;
const menuHeight = Math.max(0, height - menuTop - HOME_MENU_BOTTOM_MARGIN);
this.paintCtx = ctx;
try {
this.menu.paint(image, { x: menuX, y: menuTop, width: menuWidth, height: menuHeight, padBottom: padding.bottom },
this.menu.paint(image, { x: HOME_MENU_X, y: menuTop, width: menuWidth, height: menuHeight },
ctx.stack.isFocused() && this.focus === "menu");
} finally {
this.paintCtx = null;
}
this.menu.drawScrollbar(image, menuX + menuWidth + 5, menuTop, Math.max(0, menuHeight - padding.bottom - 4));
this.menu.drawScrollbar(image, HOME_MENU_X + menuWidth + 5, menuTop, menuHeight - 4);
this.selectedSlot = Math.min(this.selectedSlot, glanceLayout().slots.length - 1);
const preview = this.previewRect(viewport);
const preview = this.previewRect(width, height);
drawSlotPreview(image, preview, {
selectedSlot: this.selectedSlot,
selectionFocused: ctx.stack.isFocused() && this.focus === "preview",
@@ -259,7 +238,7 @@ function drawSlotPreview(
height: Math.round(r.height * sy),
});
image.drawRect(rect.x - PREVIEW_OUTLINE, rect.y - PREVIEW_OUTLINE, rect.width + 2 * PREVIEW_OUTLINE, rect.height + 2 * PREVIEW_OUTLINE, 72);
image.drawRect(rect.x - 1, rect.y - 1, rect.width + 2, rect.height + 2, 72);
const selected = scaled(layout.slots[options.selectedSlot]!.rect);
drawSelectionHighlight(image, selected.x + 1, selected.y + 1, selected.width - 2, selected.height - 2, options.selectionFocused, 4);
@@ -323,46 +302,26 @@ function openSlotPicker(ctx: LayerContext, slotIndex: number): void {
}
/**
* Settings, in four sections: Layout (the slot grid), Trigger (the sleep
* gestures that show the board, and for how long), Display (slot lines,
* widget font, stereo depth) and Cards (a row per widget on the board, for
* its own settings; left out while the board is empty).
* Settings: layout, sleep gestures, duration, slot lines, widget font, and
* stereo depth, then a row per widget on the board for its own settings.
*/
function openGlanceSettings(ctx: LayerContext): void {
const items = (): MenuItem[] => {
const cards = widgetSettingsMenuItems();
return [
menuHeadingItem("Layout"),
// The layout decides which slots (and so which widgets) are on the board.
enumSettingMenuItem(glanceLayoutSetting, { onChange: () => layer.setItems(items()) }),
menuHeadingItem("Trigger"),
enumSettingMenuItem(glanceTapDurationSetting),
toggleSettingMenuItem(glanceShowOnLongPressSetting),
toggleSettingMenuItem(glanceShowOnHeadTiltSetting),
enumSettingMenuItem(glanceTiltTimeoutSetting),
// Only Android can change the glasses' head-up setting (glasses-controls.ts).
// The controller's settings listener sends a new angle to the glasses; the
// picker shows the head's current angle to choose by.
...(headUpControlsAvailable()
? [enumSettingMenuItem(glanceAngleSetting, {
pickerFooter: liveHeadPitchFooter(() => shell.isWindowVisible(GLANCEBOARD_WINDOW_ID)),
})]
: []),
menuHeadingItem("Display"),
toggleSettingMenuItem(glanceShowLinesSetting),
uiFontOverridePickerMenuItem(glanceFont, {
title: "Glanceboard font",
description:
"Typeface for the Glanceboard's widgets. Same as UI font follows the Font setting in Settings > Customization.",
}),
enumSettingMenuItem(glanceDepthSetting),
...(cards.length ? [menuHeadingItem("Cards"), ...cards] : []),
];
};
const layer = new MenuLayer("Glanceboard settings", items(), PAGE_MENU_LAYOUT);
const items = (): MenuItem[] => [
// The layout decides which slots (and so which widgets) are on the board.
enumSettingMenuItem(glanceLayoutSetting, { onChange: () => layer.setItems(items()) }),
enumSettingMenuItem(glanceTapDurationSetting),
toggleSettingMenuItem(glanceShowOnLongPressSetting),
toggleSettingMenuItem(glanceShowOnHeadTiltSetting),
toggleSettingMenuItem(glanceShowLinesSetting),
uiFontOverridePickerMenuItem(glanceFont, {
title: "Glanceboard font",
description:
"Typeface for the Glanceboard's widgets. Same as UI font follows the Font setting in Settings > Customization.",
}),
enumSettingMenuItem(glanceDepthSetting),
...widgetSettingsMenuItems(),
];
const layer = new MenuLayer("Glanceboard settings", items(), pageMenuLayout(ctx.stack.getBaseSize().width));
ctx.stack.push(layer);
}
@@ -382,7 +341,7 @@ function widgetSettingsMenuItems(): MenuItem[] {
return { label: widget.label, description: `${widget.label} has no settings.`, disabled: true, onSelect: () => {} };
}
return submenuItem(widget.label, (ctx) => {
ctx.stack.push(new MenuLayer(`${widget.label} settings`, settingsMenuItems(), PAGE_MENU_LAYOUT));
ctx.stack.push(new MenuLayer(`${widget.label} settings`, settingsMenuItems(), pageMenuLayout(ctx.stack.getBaseSize().width)));
}, { description: `Settings for the ${widget.label} widget.` });
});
}
+27 -116
View File
@@ -1,4 +1,4 @@
import { ConfigSettingBoolean, ConfigSettingEnum, glassesHaveCompass } from "../../ui/dashboard-settings";
import { ConfigSettingBoolean, ConfigSettingEnum } from "../../ui/dashboard-settings";
import { QUADRANT_LAYOUT, SIX_SLOT_LAYOUT, slotsVerticallyAdjacent, type GlanceLayout } from "./layout";
import { type GlanceWidgetId } from "./widget";
import { glanceWidgetSpans } from "./widgets";
@@ -23,11 +23,6 @@ export function glanceSlotChoiceLabel(choice: GlanceSlotChoice): string {
return SLOT_CHOICE_LABELS[choice] ?? choice;
}
/** Whether the paired glasses can drive this widget; if not, it is left out of the slot picker. */
function slotChoiceAvailable(choice: GlanceSlotChoice): boolean {
return choice !== "compass" || glassesHaveCompass();
}
/**
* Slot order follows QUADRANT_LAYOUT: top left, top right, bottom left, bottom
* right. Calendar in both right slots merges into one double-height region.
@@ -49,9 +44,8 @@ export function glanceLayout(): GlanceLayout {
/**
* Whether sleep-time gestures show the board at all. Off restores the plain
* behaviour: a tap or hold while asleep does nothing, a head-tilt wakes the
* regular UI, double-tap wakes; and the head-up angle goes back to the glasses'
* own.
* behaviour: a tap, hold or head-tilt while asleep does nothing (a head-tilt
* wakes the regular UI), double-tap wakes.
*/
export const glanceboardEnabledSetting = new ConfigSettingBoolean({
id: "glanceboard-enabled",
@@ -62,44 +56,24 @@ export const glanceboardEnabledSetting = new ConfigSettingBoolean({
"While the display is asleep, a tap, a long-press or a head-tilt shows the Glanceboard instead of the regular UI. Double-tap still wakes the regular UI.",
});
const TAP_DURATION_SECONDS = {
"3s": 3,
"5s": 5,
"7s": 7,
"10s": 10,
"20s": 20,
"30s": 30,
"1min": 60,
} as const;
export type GlanceTapDuration = "off" | "3s" | "5s" | "7s" | "10s";
type GlanceTapDurationTimed = keyof typeof TAP_DURATION_SECONDS;
export type GlanceTapDuration = "off" | GlanceTapDurationTimed;
const TAP_DURATION_VALUES: readonly GlanceTapDuration[] = ["off", ...(Object.keys(TAP_DURATION_SECONDS) as GlanceTapDurationTimed[])];
const DEFAULT_TAP_DURATION: GlanceTapDurationTimed = "5s";
function formatTapDuration(value: GlanceTapDuration): string {
if (value === "off") return "Disabled";
const seconds = TAP_DURATION_SECONDS[value];
return seconds % 60 === 0 ? `${seconds / 60} min` : `${seconds} s`;
}
const DEFAULT_TAP_DURATION: GlanceTapDuration = "5s";
/**
* How long a tap keeps the board up; each further tap restarts it. "off"
* means a single tap does not show the board at all. A head-tilt showing
* lasts until the head comes back down, falling back to this duration (the
* default one when "off") where the head-down is unlikely to arrive.
* How long a tap (or head-tilt) keeps the board up; each further tap restarts
* it. "off" means a single tap does not show the board at all; a head-tilt
* then uses the default duration.
*/
export const glanceTapDurationSetting = new ConfigSettingEnum<GlanceTapDuration>({
id: "glanceboard-tap-duration",
label: "Show on tap",
storageKey: "glanceboard.tapDuration",
defaultValue: DEFAULT_TAP_DURATION,
values: TAP_DURATION_VALUES,
formatValue: formatTapDuration,
values: ["off", "3s", "5s", "7s", "10s"],
formatValue: (value) => (value === "off" ? "Disabled" : value.replace("s", " s")),
description:
"How long a single tap keeps the Glanceboard on screen. Disabled: a single tap does not show the Glanceboard.",
"How long a single tap (or a head-tilt) keeps the Glanceboard on screen. Disabled: a single tap does not show the Glanceboard.",
});
/** Whether a single tap while asleep shows the board. */
@@ -109,7 +83,8 @@ export function glanceShowOnTap(): boolean {
export function glanceTapTimeoutMs(): number {
const duration = glanceTapDurationSetting.get();
return TAP_DURATION_SECONDS[duration === "off" ? DEFAULT_TAP_DURATION : duration] * 1000;
const seconds = duration === "off" ? DEFAULT_TAP_DURATION : duration;
return Number.parseInt(seconds, 10) * 1000;
}
/** A long-press holds the board up until the press is released. */
@@ -121,71 +96,15 @@ export const glanceShowOnLongPressSetting = new ConfigSettingBoolean({
description: "A long-press or short-then-long-press while asleep shows the Glanceboard until the press is released.",
});
/** The head-tilt wake shows the board (until the head comes back down) instead of the regular UI. */
/** The head-tilt wake shows the board (for the tap duration) instead of the regular UI. */
export const glanceShowOnHeadTiltSetting = new ConfigSettingBoolean({
id: "glanceboard-show-on-head-tilt",
label: "Show on head tilt",
storageKey: "glanceboard.showOnHeadTilt",
defaultValue: true,
description: "Tilting your head up while asleep shows the Glanceboard instead of waking the regular UI. It stays up until you look back down.",
description: "Tilting your head up while asleep shows the Glanceboard instead of waking the regular UI.",
});
export type GlanceTiltTimeout = "60s" | "30s" | "15s" | "10s" | "never";
/**
* How long a head-tilt showing may last if the head-down never comes (the
* board normally hides when the head comes back down). "never" removes the
* limit; a double-tap still dismisses the board.
*/
export const glanceTiltTimeoutSetting = new ConfigSettingEnum<GlanceTiltTimeout>({
id: "glanceboard-head-tilt-timeout",
label: "Head tilt timeout",
storageKey: "glanceboard.headTiltTimeout",
defaultValue: "60s",
values: ["60s", "30s", "15s", "10s", "never"],
formatValue: (value) => (value === "never" ? "Never" : value.replace("s", " s")),
description:
"The longest a head-tilt keeps the Glanceboard up when looking back down is not detected. Never: it stays until you look down or double-tap. " +
"When looking down is unlikely to register (a low Head tilt angle, or the display was suspended), the tap duration applies if shorter.",
});
/** The Head tilt timeout in ms, or null for none. */
export function glanceTiltTimeoutMs(): number | null {
const value = glanceTiltTimeoutSetting.get();
return value === "never" ? null : Number.parseInt(value, 10) * 1000;
}
const GLANCE_ANGLE_VALUES = ["glasses", "5", "10", "15", "20", "25", "30", "35", "40", "45", "50", "55", "60"] as const;
export type GlanceAngle = (typeof GLANCE_ANGLE_VALUES)[number];
/**
* How far the head must tilt up for a head-tilt wake, as the glasses' own
* pitch reading (degrees). "glasses" keeps the glasses' own setting (from the
* Even app), writing back their original values if Faceclaw changed them. A
* number is written to the glasses' head-up setting on every connection while
* the Glanceboard is enabled (head-angle.ts). Android only.
*/
export const glanceAngleSetting = new ConfigSettingEnum<GlanceAngle>({
id: "glanceboard-head-tilt-angle",
label: "Head tilt angle",
storageKey: "glanceboard.headTiltAngle",
defaultValue: "glasses",
values: GLANCE_ANGLE_VALUES,
formatValue: (value) => (value === "glasses" ? "Glasses default" : `${value}°`),
description:
"How far you tilt your head up to wake the display; the list shows your head's angle now. Glasses default uses the glasses' own setting from the Even app. " +
"The board hides when your head drops 20° below this angle; under 25°, looking straight ahead may not count, so it hides after the tap duration instead.",
});
/**
* The trigger Faceclaw should maintain, in degrees: the Head tilt angle while
* the Glanceboard is enabled, else 0 (the glasses' own setting).
*/
export function glanceAngleTriggerDegrees(): number {
const value = glanceAngleSetting.get();
return value === "glasses" || !glanceboardEnabledSetting.get() ? 0 : Number(value);
}
const DEPTH_VALUES = ["-64", "-48", "-32", "-16", "0", "16", "32", "48", "64"] as const;
export type GlanceDepth = (typeof DEPTH_VALUES)[number];
@@ -227,26 +146,18 @@ export const glanceShowLinesSetting = new ConfigSettingBoolean({
export function glanceSlotSettings(layout: GlanceLayout = glanceLayout()): ConfigSettingEnum<GlanceSlotChoice>[] {
const grid = layout === QUADRANT_LAYOUT || layout === SIX_SLOT_LAYOUT;
const storageId = grid ? QUADRANT_LAYOUT.id : layout.id;
return layout.slots.map((slot, index) => {
const defaultValue: GlanceSlotChoice = (grid ? DEFAULT_QUADRANT_CHOICES[index] : undefined) ?? "none";
return new ConfigSettingEnum<GlanceSlotChoice>({
id: `glanceboard-${layout.id}-slot-${index}`,
label: slot.label,
storageKey: `glanceboard.${storageId}.slot.${index}`,
defaultValue,
values: SLOT_CHOICES.filter(slotChoiceAvailable),
// A stored choice the glasses can't drive reads as Empty, not the slot's
// default (which could put a widget on the board twice). The stored value
// stays, so pairing glasses that can drive it brings it back.
normalize: (value) => {
const choice = value as GlanceSlotChoice;
if (!SLOT_CHOICES.includes(choice)) return defaultValue;
return slotChoiceAvailable(choice) ? choice : "none";
},
formatValue: glanceSlotChoiceLabel,
description: `Which widget fills the ${slot.label.toLowerCase()} slot of the Glanceboard.`,
});
});
return layout.slots.map(
(slot, index) =>
new ConfigSettingEnum<GlanceSlotChoice>({
id: `glanceboard-${layout.id}-slot-${index}`,
label: slot.label,
storageKey: `glanceboard.${storageId}.slot.${index}`,
defaultValue: (grid ? DEFAULT_QUADRANT_CHOICES[index] : undefined) ?? "none",
values: SLOT_CHOICES,
formatValue: glanceSlotChoiceLabel,
description: `Which widget fills the ${slot.label.toLowerCase()} slot of the Glanceboard.`,
}),
);
}
/**
-2
View File
@@ -9,7 +9,6 @@ import {
glanceShowOnLongPressSetting,
glanceShowOnTap,
glanceTapTimeoutMs,
glanceTiltTimeoutMs,
} from "./glanceboard-settings";
const glanceboardApp: AppDefinition = {
@@ -25,7 +24,6 @@ const glanceboardApp: AppDefinition = {
isEnabled: () => glanceboardEnabledSetting.get(),
showOnTap: glanceShowOnTap,
tapTimeoutMs: glanceTapTimeoutMs,
tiltTimeoutMs: glanceTiltTimeoutMs,
showOnLongPress: () => glanceShowOnLongPressSetting.get(),
showOnHeadTilt: () => glanceShowOnHeadTiltSetting.get(),
depth: glanceDepth,
+5 -15
View File
@@ -50,26 +50,16 @@ export const musicShowAlbumArtSetting = new ConfigSettingBoolean({
"Show the album art beside the track details. When off, the title, artist, album and progress bar use the full width of the card.",
});
export const musicShowIdleNoteSetting = new ConfigSettingBoolean({
id: "glanceboard-music-show-idle-note",
label: "Show note when idle",
storageKey: "glanceboard.music.showIdleNote",
defaultValue: true,
description: "Show a dim music note on the card while nothing is playing. When off, the card is left blank.",
});
/** The music card's submenu in Glanceboard > Settings. */
export function musicSettingsMenuItems(): MenuItem[] {
return [toggleSettingMenuItem(musicShowAlbumArtSetting), toggleSettingMenuItem(musicShowIdleNoteSetting)];
return [toggleSettingMenuItem(musicShowAlbumArtSetting)];
}
/**
* Now playing: album art (unless turned off), title/artist/album and a
* progress bar for the active media session (Apple Music on iOS), the Music
* app's header at card size. With nothing playing the card holds a dim note,
* or nothing with that turned off. The media bridge runs for the whole
* session; the widget subscribes and ticks once a second only while
* something is playing.
* app's header at card size. The media bridge runs for the whole session; the
* widget subscribes and ticks once a second only while something is playing.
*/
export class MusicWidget implements GlanceWidget {
private media: MediaControllerState = mediaControllerBridge.snapshot();
@@ -118,8 +108,8 @@ export class MusicWidget implements GlanceWidget {
image.drawText(small, PAD, PAD + small.lineHeight + 6, mediaControllerBridge.accessLabel, 140);
return;
}
// Nothing playing: a dim note, centred, and nothing else; or a blank card.
const note = musicShowIdleNoteSetting.get() ? idleNoteImage() : null;
// Nothing playing: a dim note, centred, and nothing else.
const note = idleNoteImage();
if (note) image.drawImage(note, ((image.width - note.width) / 2) | 0, ((image.height - note.height) / 2) | 0);
return;
}
+2 -20
View File
@@ -11,7 +11,6 @@ import { ensureLocationPermission, hasLocationPermission } from "../../../native
import { onAndroidNotificationsChanged, readActiveNotificationIcons } from "../../../native/notification-icons";
import { NOTIFICATION_ICON_SIZE } from "../../../native/notification-types";
import { readPhoneBatteryState } from "../../../native/phone-battery";
import { voiceControlBridge } from "../../../native/voice-control";
import { weatherBridge } from "../../../native/weather";
import { formatClockDate, formatClockTime } from "../../../ui/clock-format";
import {
@@ -19,7 +18,6 @@ import {
batteryIndicatorVisible,
ConfigSettingBoolean,
glassesBatteryVisibilitySetting,
glassesModelName,
onAnySettingChanged,
phoneBatteryVisibilitySetting,
ringBatteryVisibilitySetting,
@@ -27,7 +25,6 @@ import {
watchBatteryVisibilitySetting,
type BatteryIndicatorVisibility,
} from "../../../ui/dashboard-settings";
import { RECORDING_ICON } from "../../../ui/listening-header";
import { type MenuItem } from "../../../ui/menu";
import { shell } from "../../../ui/shell/shell";
import { noteStaleDataUsed, renderPassAllowsStaleData } from "../../../util/render-freshness";
@@ -151,16 +148,13 @@ function requestWeatherLocationAccess(): void {
* off, and the layout mirrored either way, in the card's submenu of
* Glanceboard > Settings. Battery style and per-device visibility follow
* Settings > Customization > Battery indicators, so the card agrees with
* the bar. Countdowns tick once a second only while one is running. While
* Transcribe or Translate is saving the mic's audio (their Save Recording),
* the recording circle sits under the batteries and weather.
* the bar. Countdowns tick once a second only while one is running.
*/
export class SystemCardWidget implements GlanceWidget {
private requestRender: (() => void) | null = null;
private minuteTimer: ReturnType<typeof setTimeout> | null = null;
private secondTimer: ReturnType<typeof setInterval> | null = null;
private unsubscribeNotifications: (() => void) | null = null;
private unsubscribeRecording: (() => void) | null = null;
private unsubscribeSettings: (() => void) | null = null;
private unsubscribeTimers: (() => void) | null = null;
private unsubscribeWeather: (() => void) | null = null;
@@ -169,7 +163,6 @@ export class SystemCardWidget implements GlanceWidget {
this.requestRender = requestRender;
this.scheduleMinuteTick();
this.unsubscribeNotifications = onAndroidNotificationsChanged(() => this.requestRender?.());
this.unsubscribeRecording = voiceControlBridge.onRecordingChange(() => this.requestRender?.());
this.unsubscribeSettings = onAnySettingChanged(() => this.requestRender?.());
this.unsubscribeTimers = timerEngine.onChange(() => {
this.syncSecondTick();
@@ -188,8 +181,6 @@ export class SystemCardWidget implements GlanceWidget {
this.secondTimer = null;
this.unsubscribeNotifications?.();
this.unsubscribeNotifications = null;
this.unsubscribeRecording?.();
this.unsubscribeRecording = null;
this.unsubscribeSettings?.();
this.unsubscribeSettings = null;
this.unsubscribeTimers?.();
@@ -245,8 +236,6 @@ export class SystemCardWidget implements GlanceWidget {
if (batteryItems.length) sideBlocks.push(batteryBlock(small, batteryItems));
const weather = weatherShowOnSystemCardSetting.get() ? weatherBlock(medium, now.getTime()) : null;
if (weather) sideBlocks.push(weather);
// Last, so it comes and goes without moving the others.
if (voiceControlBridge.recording) sideBlocks.push(RECORDING_BLOCK);
const side: Rect[] = [];
let sideOffset = PAD;
for (const block of sideBlocks) {
@@ -339,7 +328,7 @@ function collectBatteryItems(): BatteryItem[] {
push(phoneBatteryVisibilitySetting.get(), "Phone", "phone", phone.battery, phone.charging);
const levels = shell.getBatteryLevels();
push(watchBatteryVisibilitySetting.get(), "Watch", "watch", levels.watch, levels.watchCharging);
push(glassesBatteryVisibilitySetting.get(), glassesModelName(), "glasses", levels.headset, levels.headsetCharging);
push(glassesBatteryVisibilitySetting.get(), "G2", "glasses", levels.headset, levels.headsetCharging);
if (levels.ring !== null && Number.isInteger(levels.ring) && levels.ring >= 0 && levels.ring <= 100) {
push(ringBatteryVisibilitySetting.get(), "R1", "ring", levels.ring, levels.ringCharging);
}
@@ -392,13 +381,6 @@ function batteryBlock(font: UiFont, items: readonly BatteryItem[]): SideBlock {
};
}
/** The recording circle: the mic's audio is being saved (see RECORDING_ICON). */
const RECORDING_BLOCK: SideBlock = {
width: RECORDING_ICON.width,
height: RECORDING_ICON.height,
draw: (image, left, top) => image.drawImage(RECORDING_ICON, left, top),
};
/** The current weather's condition icon and temperature, or null without a fresh reading. */
function weatherBlock(font: UiFont, nowMs: number): SideBlock | null {
const reading = weatherReading(weatherBridge.snapshot(), nowMs);
+2 -3
View File
@@ -123,12 +123,11 @@ class HealthLayer implements Layer {
}
paint(ctx: LayerContext): GrayImage {
// The pages lay out inside the padding suggested for the window's edges.
const { width, height, padding } = ctx.stack.getBaseSize();
const { width, height } = ctx.stack.getBaseSize();
const image = new GrayImage(width, height, 0);
drawGlancePage(
image,
{ width, height, padding },
{ width, height },
{ small: getDefaultSmallFont(), large: getDefaultLargeFont() },
GLANCE_PAGES[this.pageIndex] ?? GLANCE_PAGES[0]!,
{
+1 -1
View File
@@ -11,7 +11,7 @@ import {
export function launcherEntries(apps: readonly AppDefinition[]) {
return [
...apps
.filter((app) => app.showInLauncher !== false && app.isSupported?.() !== false)
.filter((app) => app.showInLauncher !== false)
.map((app) => ({
appId: app.appId,
label: app.title,
+14 -20
View File
@@ -4,7 +4,7 @@ import { GrayImage } from "../../graphics/image";
import { type Plane } from "../../graphics/plane";
import { renderIcon, type IconName } from "../../graphics/icons";
import { InputEvent } from "../../ui/gestures";
import { IconGrid, ICON_GRID_ICON_SIZE } from "../../ui/icon-grid";
import { IconGrid, ICON_GRID_BAND_INSET_X, ICON_GRID_HIGHLIGHT_INSET_Y, ICON_GRID_ICON_SIZE } from "../../ui/icon-grid";
import { Layer, LayerActions, LayerContext } from "../../ui/layers";
import { MenuLayer, type MenuItem } from "../../ui/menu";
import { WINDOW_MENU_LAYOUT } from "../../ui/window-menu";
@@ -43,9 +43,12 @@ export type LauncherOptions = {
export const LAUNCHER_WINDOW_ID = "launcher";
export const LAUNCHER_SURFACE_ID = "window:launcher";
/** A folder's name, above its grid: how far in from the grid's side, and the gap under it. */
const FOLDER_NAME_INSET_X = 4;
const FOLDER_NAME_GAP = 6;
const GRID_TOP = 6;
/**
* The root grid's top: its first row's highlight band then sits as far
* below the window's top edge as it is from the side edges.
*/
const ROOT_GRID_TOP = ICON_GRID_BAND_INSET_X - ICON_GRID_HIGHLIGHT_INSET_Y;
/** One cell of the grid: an app, or a folder holding some of the apps. */
type LauncherGridEntry =
@@ -257,25 +260,16 @@ class LauncherGridLayer implements Layer {
paint(ctx: LayerContext): GrayImage {
const font = getDefaultSmallFont();
const { width, height, padding } = ctx.stack.getBaseSize();
const { width, height } = ctx.stack.getBaseSize();
const image = new GrayImage(width, height, 0);
const gridX = padding.left;
const gridWidth = width - padding.left - padding.right;
// The grid scrolls, so it runs to the window's top and bottom edges, the
// padding suggested for them inside it: its end rows keep clear of a
// border there, and a row cut off for scrolling meets the border rather
// than a blank strip. A folder's name stays put above its grid instead,
// in a band that scales with the font.
let gridTop = 0;
let padTop = padding.top;
// The folder-name header band scales with the font.
const gridTop = this.currentFolder !== null ? GRID_TOP + font.lineHeight + 4 : ROOT_GRID_TOP;
if (this.currentFolder !== null) {
const nameX = gridX + FOLDER_NAME_INSET_X;
image.drawText(font, nameX, padding.top, truncateText(font, this.currentFolder, gridWidth - 2 * FOLDER_NAME_INSET_X), 160);
gridTop = padding.top + font.lineHeight + FOLDER_NAME_GAP;
padTop = 0;
image.drawText(font, 8, GRID_TOP - 2, truncateText(font, this.currentFolder, width - 16), 160);
}
this.grid.paint(image, { x: gridX, y: gridTop, width: gridWidth, height: height - gridTop, padTop, padBottom: padding.bottom },
ctx.stack.isFocused());
// The grid runs to the window's bottom edge, so a row cut off there for
// scrolling meets the window's border rather than a blank strip.
this.grid.paint(image, { x: 0, y: gridTop, width, height: height - gridTop }, ctx.stack.isFocused());
return image;
}
+10 -20
View File
@@ -5,19 +5,11 @@ import { GESTURE_DOUBLE_CLICK, GESTURE_SCROLL, type InputEvent } from "../../ui/
import { Layer, type LayerContext } from "../../ui/layers";
import { micSession, type CaptionLine, type MicSessionState } from "./mic-session";
/** From the title's top to the first caption row. */
const BODY_TOP_OFFSET = 20;
/** Indent of the caption rows under the title. */
const ROW_INDENT = 4;
/**
* On-glasses captions: each line is prefixed with the speaker's name (their
* id until tagged); foreign-language lines show the translation directly
* beneath the recognized original. Scroll reviews recent lines; the view
* snaps back to live on new speech.
*
* The title and the hint line stay put, at the padding suggested for the
* window's top and bottom edges; the caption rows scroll between the two.
*/
export class CaptionsLayer implements Layer {
private state: MicSessionState = micSession.getState();
@@ -41,21 +33,19 @@ export class CaptionsLayer implements Layer {
paint(ctx: LayerContext): GrayImage {
const font = getDefaultSmallFont();
const { width, height, padding } = ctx.stack.getBaseSize();
const { width, height } = ctx.stack.getBaseSize();
const image = new GrayImage(width, height, 0);
const state = this.state;
const left = padding.left;
const rowX = left + ROW_INDENT;
const bodyTop = padding.top + BODY_TOP_OFFSET;
image.drawText(font, left, padding.top, "Captions", 220);
image.drawText(font, 12, 4, "Captions", 220);
if (!state.captionsActive) {
image.drawText(font, left, bodyTop + 2, state.captionsNote, 130);
image.drawText(font, 12, 26, state.captionsNote, 130);
}
const rows = this.buildRows(font, Math.max(0, width - padding.right - rowX), state.captionLines);
const rows = this.buildRows(font, width - 36, state.captionLines);
const step = lineStep(font) + 1;
const footerY = height - padding.bottom - font.lineHeight;
const bodyTop = 24;
const footerY = height - font.lineHeight - 4;
const visibleRows = Math.max(1, Math.floor((footerY - bodyTop) / step));
const maxScrollback = Math.max(0, rows.length - visibleRows);
this.scrollback = Math.min(this.scrollback, maxScrollback);
@@ -64,16 +54,16 @@ export class CaptionsLayer implements Layer {
const row = rows[index]!;
const y = bodyTop + (index - firstRow) * step;
if (row.prefix) {
image.drawText(font, rowX, y, row.prefix, row.prefixValue);
image.drawText(font, rowX + font.measureText(row.prefix), y, row.text, row.value);
image.drawText(font, 16, y, row.prefix, row.prefixValue);
image.drawText(font, 16 + font.measureText(row.prefix), y, row.text, row.value);
} else {
image.drawText(font, rowX + row.indent, y, row.text, row.value);
image.drawText(font, 16 + row.indent, y, row.text, row.value);
}
}
image.drawText(
font,
left,
12,
footerY,
`${GESTURE_SCROLL} history ${GESTURE_DOUBLE_CLICK} back` +
(this.scrollback > 0 ? ` (${this.scrollback} back)` : ""),
+12 -21
View File
@@ -4,12 +4,6 @@ import { GESTURE_DOUBLE_CLICK, type InputEvent } from "../../ui/gestures";
import { Layer, type LayerContext } from "../../ui/layers";
import { micSession, type MicSessionState } from "./mic-session";
/** From the title's top to the status line, and to the first meter row. */
const STATUS_OFFSET_Y = 18;
const METERS_OFFSET_Y = 42;
/** Width of the mic-name column left of the meters. */
const LABEL_COLUMN_WIDTH = 118;
/**
* Live level meters, one per active microphone (a single mixed channel on
* stock firmware; up to four mics with the mic-control firmware), plus the
@@ -28,27 +22,24 @@ export class LevelsLayer implements Layer {
paint(ctx: LayerContext): GrayImage {
const font = getDefaultSmallFont();
const { width, height, padding } = ctx.stack.getBaseSize();
const { width, height } = ctx.stack.getBaseSize();
const image = new GrayImage(width, height, 0);
const state = this.state;
// Text starts at the padding suggested for the window's left edge, and
// the meters run to the right edge's.
const left = padding.left;
image.drawText(font, left, padding.top, "Microphone Levels", 220);
image.drawText(font, left, padding.top + STATUS_OFFSET_Y, state.statusText, 110);
image.drawText(font, 12, 6, "Microphone Levels", 220);
image.drawText(font, 12, 24, state.statusText, 110);
const barX = left + LABEL_COLUMN_WIDTH;
const barWidth = Math.max(0, width - padding.right - barX);
const barX = 130;
const barWidth = width - barX - 40;
const rowHeight = 26;
let y = padding.top + METERS_OFFSET_Y;
let y = 48;
const labels = state.levelLabels.length ? state.levelLabels : ["Glasses mic"];
labels.forEach((label, index) => {
const level = state.levels[index] ?? 0;
const enabled = state.levelsEnabled[index] ?? true;
image.drawText(font, left, y + 3, enabled ? label : `${label} (off)`, enabled ? 190 : 90);
image.drawText(font, 12, y + 3, enabled ? label : `${label} (off)`, enabled ? 190 : 90);
image.drawRect(barX, y, barWidth, 16, enabled ? 70 : 40);
const fill = Math.round(Math.max(0, barWidth - 4) * level);
const fill = Math.round((barWidth - 4) * level);
if (fill > 0) {
// A disabled mic still meters (dimly) so its signal can be checked
// before switching it back on.
@@ -64,15 +55,15 @@ export class LevelsLayer implements Layer {
y += 6;
if (state.doaDeviceDeg !== null) {
image.drawText(font, left, y, `Direction of arrival: ${Math.round(state.doaDeviceDeg)}°`, 160);
image.drawText(font, 12, y, `Direction of arrival: ${Math.round(state.doaDeviceDeg)}°`, 160);
y += 18;
}
image.drawText(font, left, y, `Signal-strength ratio: ${state.ssr}`, 130);
image.drawText(font, 12, y, `Signal-strength ratio: ${state.ssr}`, 130);
y += 18;
const ancText = state.ancOn && state.ancEngine ? `on — ${state.ancEngine}` : "off";
image.drawText(font, left, y, `Noise cancellation: ${ancText}`, 130);
image.drawText(font, 12, y, `Noise cancellation: ${ancText}`, 130);
image.drawText(font, left, height - padding.bottom - font.lineHeight, `${GESTURE_DOUBLE_CLICK} back`, 110);
image.drawText(font, 12, height - font.lineHeight - 4, `${GESTURE_DOUBLE_CLICK} back`, 110);
return image;
}
-21
View File
@@ -56,27 +56,6 @@ export function saveMicConfig(config: MicConfig): void {
setStringSetting(CONFIG_SETTING_KEY, JSON.stringify(config));
}
/**
* The configuration a capture of the temples' own microphones arms them
* with: `mics` of 2 is one microphone per temple (the front one), 4 is both
* of each. The front end and direction detection follow Microphone setup
* (the Microphones app), except that both of a temple's microphones only
* come through the codec front end. Always raw PCM16 at 16 kHz, which is
* what the firmware streams and transcription takes.
*/
export function micArrayCaptureConfig(mics: 2 | 4): MicConfig {
const base = loadMicConfig();
return {
...base,
source: mics === 4 ? "codec" : base.source,
channelMask: mics === 4 ? 0b11 : 0b01,
codec: "raw",
format: "pcm16",
sampleRateHz: 16_000,
armHardware: true,
};
}
class MicArrayController {
private readonly statusListeners = new Set<(statuses: MicArrayStatuses) => void>();
private statuses: MicArrayStatuses = { left: null, right: null };
+10 -1
View File
@@ -1,5 +1,6 @@
import { imageFromAsciiArt } from "../../graphics/image";
import { getDefaultSmallFont } from "../../graphics/ui-fonts";
import { appViewportSize } from "../../ui/shell/geometry";
import {
MenuLayer,
TextPageLayer,
@@ -61,7 +62,15 @@ const TRAY_ICON = imageFromAsciiArt(
{ "#": 220 },
);
const MENU_LAYOUT = { page: true, width: 268 } as const;
const MENU_LAYOUT = {
x: 8,
y: 8,
width: 292,
showBorder: false,
minHeight: 0,
maxHeight: appViewportSize("min").height - 16,
opaque: true,
};
/** Persist a config change; the running session applies it live. */
function updateMicConfig(mutate: (config: MicConfig) => MicConfig): void {
+32 -43
View File
@@ -5,16 +5,6 @@ import { Layer, type LayerContext } from "../../ui/layers";
import { micSession, type MicSessionState } from "./mic-session";
import { dotBrightness } from "./radar-dots";
/** Room above the plot's ring for its "front" label. */
const FRONT_LABEL_BAND = 14;
/** Gap between the ring and the hint line under it. */
const RING_FOOTER_GAP = 10;
/** The least the ring keeps clear of the window's sides, which leaves the readout panel its corner. */
const RING_SIDE_CLEARANCE = 90;
/** The readout panel: the least its column is wide, and its drop below the title's top. */
const PANEL_MIN_WIDTH = 104;
const PANEL_OFFSET_Y = 20;
/**
* The Sonic Radar: a top-down view around the wearer's head. The shaded
* wedge is the listening beam (scroll rotates it, click locks it onto the
@@ -24,10 +14,6 @@ const PANEL_OFFSET_Y = 20;
* scene is world-anchored (an N tick marks north and dots stay put as the
* head turns); dots glow full-bright while their speaker is talking and fade
* with the time since they last spoke.
*
* The plot is centred on the window, between the title row and the hint
* line; those and the readout panel keep to the padding suggested for the
* window's edges.
*/
export class RadarLayer implements Layer {
private state: MicSessionState = micSession.getState();
@@ -42,20 +28,17 @@ export class RadarLayer implements Layer {
paint(ctx: LayerContext): GrayImage {
const font = getDefaultSmallFont();
const { width, height, padding } = ctx.stack.getBaseSize();
const { width, height } = ctx.stack.getBaseSize();
const image = new GrayImage(width, height, 0);
const state = this.state;
const footerY = height - padding.bottom - font.lineHeight;
const ringTop = padding.top + FRONT_LABEL_BAND;
const ringBottom = footerY - RING_FOOTER_GAP;
const centerX = (width / 2) | 0;
const centerY = ((ringTop + ringBottom) / 2) | 0;
const radius = Math.max(0, Math.min(centerX - RING_SIDE_CLEARANCE, ((ringBottom - ringTop) / 2) | 0));
const centerY = ((height - 24) / 2 + 6) | 0;
const radius = Math.min(centerX - 90, ((height - 40) / 2) | 0);
image.drawText(font, padding.left, padding.top, "Sonic Radar", 220);
image.drawText(font, 12, 6, "Sonic Radar", 220);
const modeText = state.mode === "extended" ? "4-mic array" : "firmware DoA";
image.drawText(font, width - padding.right - font.measureText(modeText), padding.top, modeText, 110);
image.drawText(font, width - font.measureText(modeText) - 12, 6, modeText, 110);
// Beam wedge (device frame; the head is the reference for display).
const beamDeviceDeg = this.deviceBeamDeg(state);
@@ -87,7 +70,7 @@ export class RadarLayer implements Layer {
// Radar rings.
drawCircle(image, centerX, centerY, radius, 60);
drawCircle(image, centerX, centerY, (radius / 2) | 0, 30);
image.drawText(font, centerX - (font.measureText("front") / 2) | 0, centerY - radius - FRONT_LABEL_BAND, "front", 90);
image.drawText(font, centerX - (font.measureText("front") / 2) | 0, centerY - radius - 14, "front", 90);
// Compass north tick: anchors the world frame visually as the head turns.
if (state.headingDeg !== null) {
@@ -144,31 +127,37 @@ export class RadarLayer implements Layer {
}
const label = dot.name;
const labelValue = Math.max(90, value - 50);
const labelX = dotX + 8 + font.measureText(label) > width - padding.right ? dotX - font.measureText(label) - 8 : dotX + 8;
const labelX = dotX + 8 + font.measureText(label) > width ? dotX - font.measureText(label) - 8 : dotX + 8;
image.drawText(font, labelX, dotY - 6, label, labelValue);
}
// Side panel: beam + level readouts, in a column that ends at the right
// edge's padding and is wide enough for its longest line.
const panelLines: Array<[text: string, value: number]> = [
state.headingDeg !== null ? [`Heading ${Math.round(state.headingDeg)}°`, 150] : ["No compass", 90],
[`Beam ${Math.round(state.beamWorldDeg)}°`, 170],
[`Width ${Math.round(state.beamWidthDeg)}°`, 130],
[state.beamLocked ? "Locked on talker" : "Manual aim", 130],
];
if (state.doaDeviceDeg !== null) panelLines.push([`Heard ${Math.round(state.doaDeviceDeg)}°`, 200]);
panelLines.push([state.beamFilterOn ? "Filter: beam only" : "Filter: off", 130]);
const panelWidth = Math.max(PANEL_MIN_WIDTH, ...panelLines.map(([text]) => Math.ceil(font.measureText(text))));
const panelX = width - padding.right - panelWidth;
let panelY = padding.top + PANEL_OFFSET_Y;
for (const [text, value] of panelLines) {
image.drawText(font, panelX, panelY, text, value);
panelY += 16;
}
// Side panel: beam + level readouts.
const panelX = width - 116;
let panelY = 26;
image.drawText(
font,
padding.left,
panelX,
panelY,
state.headingDeg !== null ? `Heading ${Math.round(state.headingDeg)}°` : "No compass",
state.headingDeg !== null ? 150 : 90,
);
panelY += 16;
image.drawText(font, panelX, panelY, `Beam ${Math.round(state.beamWorldDeg)}°`, 170);
panelY += 16;
image.drawText(font, panelX, panelY, `Width ${Math.round(state.beamWidthDeg)}°`, 130);
panelY += 16;
image.drawText(font, panelX, panelY, state.beamLocked ? "Locked on talker" : "Manual aim", 130);
panelY += 16;
if (state.doaDeviceDeg !== null) {
image.drawText(font, panelX, panelY, `Heard ${Math.round(state.doaDeviceDeg)}°`, 200);
panelY += 16;
}
image.drawText(font, panelX, panelY, state.beamFilterOn ? "Filter: beam only" : "Filter: off", 130);
const footerY = height - font.lineHeight - 4;
image.drawText(
font,
12,
footerY,
`${GESTURE_SCROLL} aim ${GESTURE_CLICK} lock ${GESTURE_DOUBLE_CLICK} back`,
110,
+31 -65
View File
@@ -33,7 +33,6 @@ import { loadSoundEnabled, saveSoundEnabled } from "../../ui/sound-setting";
import { type MenuItem } from "../../ui/menu";
import { WindowMenu } from "../../ui/window-menu";
import type { WorkerAppMessage, WorkerAppReply } from "../../ui/shell/worker-window";
import type { Viewport } from "../../ui/viewport";
import {
directionalFallback,
GESTURE_CLICK,
@@ -54,10 +53,9 @@ const smallFont = getDefaultSmallFont();
const COLS = 12;
const ROWS = 9;
const CELL = 26;
/** How far outside its cells the board's frame is drawn. */
const BOARD_FRAME = 2;
/** Gap between the board's cells and the panel to their right. */
const PANEL_GAP = 28;
const BOARD_X = 14;
const BOARD_Y = 14;
const PANEL_X = BOARD_X + COLS * CELL + 28;
const DIFFICULTIES = [
{ name: "Easy", mines: 14 },
@@ -109,12 +107,6 @@ type MinesweeperWindow = {
title: string;
viewportWidth: number;
viewportHeight: number;
/**
* The viewport as the shell last sent it: besides the size above, what each
* edge touches and the padding suggested there, which places the board
* (see boardOrigin).
*/
viewport: Viewport;
foreground: boolean;
/** Whether this window is the shell's input target (pushed with each message). */
focused: boolean;
@@ -174,7 +166,6 @@ global.onmessage = (event: { data: WorkerAppMessage }) => {
title: message.title,
viewportWidth: message.viewport.width,
viewportHeight: message.viewport.height,
viewport: message.viewport,
foreground: false,
focused: false,
menu: null,
@@ -264,15 +255,13 @@ global.onmessage = (event: { data: WorkerAppMessage }) => {
if (!window) break;
window.viewportWidth = message.viewport.width;
window.viewportHeight = message.viewport.height;
window.viewport = message.viewport;
window.menu?.resize(message.viewport);
window.menu?.resize({ width: window.viewportWidth, height: window.viewportHeight });
window.lastSubmittedFingerprint = "";
if (window.foreground) renderAndSubmit(window, 0);
break;
}
case "navigation-sensors":
case "text-input":
case "key":
case "tool-call":
break;
}
@@ -363,7 +352,7 @@ function windowMenu(window: MinesweeperWindow): WindowMenu {
title: () => window.title,
items: () => windowMenuItems(window),
claimsLongPress: () => window.phase === "playing",
size: window.viewport,
size: { width: window.viewportWidth, height: window.viewportHeight },
paintBase: () => paintContent(window),
isFocused: () => window.focused,
});
@@ -677,67 +666,46 @@ function paint(window: MinesweeperWindow): Plane[] {
return windowMenu(window).paint();
}
/** Top-left corner of the board's cells, in viewport coordinates. */
type BoardOrigin = { x: number; y: number };
/**
* Where the board goes: its frame, the outermost thing drawn toward the
* window's top and left edges, sits at the padding suggested for them. The
* board is a fixed size and the panel hangs off its right side, so the whole
* layout moves with it and nothing is placed from the other two edges.
*/
function boardOrigin(window: MinesweeperWindow): BoardOrigin {
const { padding } = window.viewport;
return { x: padding.left + BOARD_FRAME, y: padding.top + BOARD_FRAME };
}
function paintContent(window: MinesweeperWindow): GrayImage {
const image = new GrayImage(window.viewportWidth, window.viewportHeight, 0);
const board = boardOrigin(window);
image.drawRect(
board.x - BOARD_FRAME,
board.y - BOARD_FRAME,
COLS * CELL + 2 * BOARD_FRAME,
ROWS * CELL + 2 * BOARD_FRAME,
120,
);
image.drawRect(BOARD_X - 2, BOARD_Y - 2, COLS * CELL + 4, ROWS * CELL + 4, 120);
if (window.phase === "paused") {
// Hide the board so pausing can't be used to study it off the clock.
drawCenteredIn(image, mediumFont, board.x, COLS * CELL, board.y + 80, "PAUSED", 230);
drawCenteredIn(image, smallFont, board.x, COLS * CELL, board.y + 120, `${GESTURE_CLICK} resume`, 150);
drawCenteredIn(image, smallFont, board.x, COLS * CELL, board.y + 140, `${GESTURE_DOUBLE_CLICK} leave`, 150);
drawCenteredIn(image, mediumFont, BOARD_X, COLS * CELL, BOARD_Y + 80, "PAUSED", 230);
drawCenteredIn(image, smallFont, BOARD_X, COLS * CELL, BOARD_Y + 120, `${GESTURE_CLICK} resume`, 150);
drawCenteredIn(image, smallFont, BOARD_X, COLS * CELL, BOARD_Y + 140, `${GESTURE_DOUBLE_CLICK} leave`, 150);
} else {
paintBoard(image, window, board);
paintBoard(image, window);
}
paintPanel(image, window, board);
paintPanel(image, window);
return image;
}
function paintBoard(image: GrayImage, window: MinesweeperWindow, board: BoardOrigin): void {
function paintBoard(image: GrayImage, window: MinesweeperWindow): void {
for (let y = 0; y < ROWS; y++) {
for (let x = 0; x < COLS; x++) {
paintCell(image, window, board, x, y);
paintCell(image, window, x, y);
}
}
// Cursor: a double bright outline so it stays visible over any cell. In
// row-select it spans the whole selected row; in column-select it marks the
// cell (games over show neither).
if (window.phase !== "playing") return;
const cx = board.x + window.cursorX * CELL;
const cy = board.y + window.cursorY * CELL;
const cx = BOARD_X + window.cursorX * CELL;
const cy = BOARD_Y + window.cursorY * CELL;
if (window.selectMode === "row") {
image.drawRect(board.x - 1, cy - 1, COLS * CELL + 1, CELL + 1, 255);
image.drawRect(board.x, cy, COLS * CELL - 1, CELL - 1, 255);
image.drawRect(BOARD_X - 1, cy - 1, COLS * CELL + 1, CELL + 1, 255);
image.drawRect(BOARD_X, cy, COLS * CELL - 1, CELL - 1, 255);
} else {
image.drawRect(cx - 1, cy - 1, CELL + 1, CELL + 1, 255);
image.drawRect(cx, cy, CELL - 1, CELL - 1, 255);
}
}
function paintCell(image: GrayImage, window: MinesweeperWindow, board: BoardOrigin, x: number, y: number): void {
function paintCell(image: GrayImage, window: MinesweeperWindow, x: number, y: number): void {
const index = y * COLS + x;
const cx = board.x + x * CELL;
const cy = board.y + y * CELL;
const cx = BOARD_X + x * CELL;
const cy = BOARD_Y + y * CELL;
const state = window.cellState[index]!;
const isMine = window.mines[index] === 1;
const gameOver = window.phase === "lost";
@@ -789,23 +757,21 @@ function drawMine(image: GrayImage, cx: number, cy: number, shade: number): void
image.drawLine(centerX - 7, centerY + 7, centerX + 7, centerY - 7, shade);
}
/** The stats and gesture hints beside the board, laid out down from its top. */
function paintPanel(image: GrayImage, window: MinesweeperWindow, board: BoardOrigin): void {
const x = board.x + COLS * CELL + PANEL_GAP;
drawStat(image, x, board.y, "Mines", String(mineCount(window) - window.flagCount));
drawStat(image, x, board.y + 62, "Time", formatElapsed(elapsedMs(window)));
image.drawText(smallFont, x, board.y + 126, DIFFICULTIES[window.difficultyIndex]!.name, 140);
function paintPanel(image: GrayImage, window: MinesweeperWindow): void {
drawStat(image, PANEL_X, BOARD_Y, "Mines", String(mineCount(window) - window.flagCount));
drawStat(image, PANEL_X, BOARD_Y + 62, "Time", formatElapsed(elapsedMs(window)));
image.drawText(smallFont, PANEL_X, BOARD_Y + 126, DIFFICULTIES[window.difficultyIndex]!.name, 140);
if (window.phase === "won" || window.phase === "lost") {
image.drawText(mediumFont, x, board.y + 152, window.phase === "won" ? "CLEARED!" : "BOOM!", 250);
image.drawText(smallFont, x, board.y + 178, `${GESTURE_CLICK} new game`, 150);
image.drawText(smallFont, x, board.y + 198, `${GESTURE_LONG_PRESS} menu`, 150);
image.drawText(mediumFont, PANEL_X, BOARD_Y + 152, window.phase === "won" ? "CLEARED!" : "BOOM!", 250);
image.drawText(smallFont, PANEL_X, BOARD_Y + 178, `${GESTURE_CLICK} new game`, 150);
image.drawText(smallFont, PANEL_X, BOARD_Y + 198, `${GESTURE_LONG_PRESS} menu`, 150);
} else if (window.selectMode === "row") {
image.drawText(smallFont, x, board.y + 186, `${GESTURE_SCROLL} row ${GESTURE_CLICK} pick`, 115);
image.drawText(smallFont, x, board.y + 208, `${GESTURE_DOUBLE_CLICK} pause`, 115);
image.drawText(smallFont, PANEL_X, 200, `${GESTURE_SCROLL} row ${GESTURE_CLICK} pick`, 115);
image.drawText(smallFont, PANEL_X, 222, `${GESTURE_DOUBLE_CLICK} pause`, 115);
} else {
image.drawText(smallFont, x, board.y + 186, `${GESTURE_SCROLL} cell ${GESTURE_CLICK} dig`, 115);
image.drawText(smallFont, x, board.y + 208, `${GESTURE_LONG_PRESS} flag ${GESTURE_DOUBLE_CLICK} rows`, 115);
image.drawText(smallFont, PANEL_X, 200, `${GESTURE_SCROLL} cell ${GESTURE_CLICK} dig`, 115);
image.drawText(smallFont, PANEL_X, 222, `${GESTURE_LONG_PRESS} flag ${GESTURE_DOUBLE_CLICK} rows`, 115);
}
}
+27 -51
View File
@@ -11,24 +11,13 @@ import {
} from "../../native/media-browser";
import { Layer, type LayerContext } from "../../ui/layers";
/** Header: title line + path line (26px at the 12px bitmap default), below the window's top padding. */
/** Header: title line + path line (34px at the 12px bitmap default). */
function headerHeight(font: UiFont): number {
return lineStep(font) + font.lineHeight;
return 8 + lineStep(font) + font.lineHeight;
}
/**
* The selection box extends this far left and right of the row text. The
* boxes start at the window's left padding, so the title, path and hint
* line, which line up with the row text, start this far in from it.
*/
const LIST_X = 20;
/** The selection box extends this far left and right of the row text. */
const LIST_HIGHLIGHT_PAD = 6;
/** Status messages (connecting, errors, empty) are indented this far from the title, and sit this far under the header. */
const MESSAGE_INDENT = 4;
const MESSAGE_GAP = 8;
/** Clear rows between the list and the hint line under it. */
const FOOTER_GAP = 7;
/** Width of the bar Menu.drawScrollbar draws, and the room kept for it right of the rows. */
const SCROLLBAR_WIDTH = 3;
const SCROLLBAR_GUTTER = 6;
export type MediaBrowseOptions = {
app: MediaBrowserApp;
@@ -70,68 +59,55 @@ export class MediaBrowseLayer implements Layer {
this.startConnect(ctx);
}
const font = getDefaultSmallFont();
const { width, height, padding } = ctx.stack.getBaseSize();
const { width, height } = ctx.stack.getBaseSize();
const image = new GrayImage(width, height, 0);
// The list's selection boxes span the window's side padding, less the
// scrollbar's gutter. The header and the hint line are fixed, inside the
// top and bottom padding, with the list scrolling between them.
const listX = padding.left;
const textX = listX + LIST_HIGHLIGHT_PAD;
const textWidth = width - padding.left - padding.right - 2 * LIST_HIGHLIGHT_PAD;
const headerBottom = padding.top + headerHeight(font);
const messageX = textX + MESSAGE_INDENT;
const messageY = headerBottom + MESSAGE_GAP;
const footerY = height - padding.bottom - font.lineHeight;
image.drawText(font, textX, padding.top, `Library: ${this.options.app.appName}`, 220);
image.drawText(font, 20, 8, `Library: ${this.options.app.appName}`, 220);
if (this.phase === "connecting" || this.phase === "idle") {
image.drawText(font, messageX, messageY, `Starting ${this.options.app.appName}...`, 150);
image.drawText(font, textX, footerY, `${GESTURE_DOUBLE_CLICK} back`, 110);
image.drawText(font, 24, headerHeight(font) + 8, `Starting ${this.options.app.appName}...`, 150);
image.drawText(font, 20, height - font.lineHeight - 4, `${GESTURE_DOUBLE_CLICK} back`, 110);
return image;
}
if (this.phase === "failed") {
image.drawText(font, messageX, messageY, "Could not connect:", 180);
image.drawText(font, messageX, messageY + lineStep(font), truncateRight(font, this.connectError, textWidth - 2 * MESSAGE_INDENT), 140);
image.drawText(font, textX, footerY, `${GESTURE_CLICK} retry ${GESTURE_DOUBLE_CLICK} back`, 110);
image.drawText(font, 24, headerHeight(font) + 8, "Could not connect:", 180);
image.drawText(font, 24, headerHeight(font) + 8 + lineStep(font), truncateRight(font, this.connectError, width - 48), 140);
image.drawText(font, 20, height - font.lineHeight - 4, `${GESTURE_CLICK} retry ${GESTURE_DOUBLE_CLICK} back`, 110);
return image;
}
const level = this.currentLevel();
const path = this.levels.map((entry) => entry.title).join(" / ");
image.drawText(font, textX, padding.top + lineStep(font), truncateLeft(font, path, textWidth), 130);
image.drawText(font, 20, 8 + lineStep(font), truncateLeft(font, path, width - 40), 130);
if (!level || level.items === null) {
image.drawText(font, messageX, messageY, level?.error ? level.error : "Loading...", level?.error ? 140 : 150);
image.drawText(font, textX, footerY, `${GESTURE_DOUBLE_CLICK} up / back`, 110);
image.drawText(font, 24, headerHeight(font) + 8, level?.error ? level.error : "Loading...", level?.error ? 140 : 150);
image.drawText(font, 20, height - font.lineHeight - 4, `${GESTURE_DOUBLE_CLICK} up / back`, 110);
return image;
}
const items = level.items;
if (!items.length) {
image.drawText(font, messageX, messageY, "(empty)", 120);
image.drawText(font, textX, footerY, `${GESTURE_DOUBLE_CLICK} up / back`, 110);
image.drawText(font, 24, headerHeight(font) + 8, "(empty)", 120);
image.drawText(font, 20, height - font.lineHeight - 4, `${GESTURE_DOUBLE_CLICK} up / back`, 110);
return image;
}
const listTop = headerHeight(font);
const listHeight = height - listTop - font.lineHeight - 10;
const visibleRows = Math.max(1, (listHeight / tightRowHeight(font)) | 0);
// Row boxes start one pixel above the header's bottom edge.
const listTop = headerBottom - 1;
const listBottom = footerY - FOOTER_GAP;
if (listBottom > listTop) {
level.menu.paint(
image,
{ x: listX, y: listTop, width: width - padding.left - padding.right - SCROLLBAR_GUTTER, height: listBottom - listTop },
ctx.stack.isFocused(),
);
// The bar hugs the window's right padding, a little short of the list's ends.
const trackHeight = listBottom - listTop - 4;
if (trackHeight > 0) level.menu.drawScrollbar(image, width - padding.right - SCROLLBAR_WIDTH, headerBottom, trackHeight);
}
level.menu.paint(
image,
{ x: LIST_X - LIST_HIGHLIGHT_PAD, y: listTop - 1, width: width - 2 * LIST_X + 2 * LIST_HIGHLIGHT_PAD, height: listHeight },
ctx.stack.isFocused(),
);
level.menu.drawScrollbar(image, width - 12, listTop, visibleRows * tightRowHeight(font) - 4);
image.drawText(
font,
textX,
footerY,
20,
height - 16,
`${GESTURE_SCROLL} select ${GESTURE_CLICK} open/play ${GESTURE_DOUBLE_CLICK} up / back`,
110,
);
Loaded 100 of 447 files, more files were not shown because too many files have changed in this diff. Show more