Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,8 @@ go run ./cmd/viva help package # 每个命令和参数

`viva.toml` 严格解码:未知的表或键、重复项、类型错误的值都是错误,并会指出行号。macOS 程序按 `min_system` 编译,而不是按构建机器的 macOS 版本;`package` 会拒绝声称支持的系统比其程序所需更旧的包。签名与公证通过 `--sign-identity` 或 `APPLE_SIGNING_IDENTITY` 加 notarytool 凭据显式开启;`--no-sign` 把它们全部关闭。`--arch amd64` 从任一宿主构建另一种 Darwin 架构:构建保留 cgo 和 Objective-C/Metal 桥,让 clang 产出另一份 Mach-O 切片。每个目标、安装器格式和参数见[构建与打包指南](https://vivaui.dev/zh/docs/build-and-package/)。

Linux 和 Windows 构建默认关闭 cgo。需要链接自己的 C 或 Rust 库的应用可以使用 `viva build --cgo` 或 `viva package --cgo`,并提供目标 C 编译器与 `CGO_CFLAGS`/`CGO_LDFLAGS`。这些程序必须针对它支持的系统库和最低操作系统版本验证;这个显式开关不提供交叉编译器,也不让原生依赖变得可移植。浏览器构建拒绝 `--cgo`;macOS 仍像以前一样为其系统桥保持 cgo 开启。

发布链接是测出来的,不是假设的。`go-toml/v2` 只被 `viva` 命令使用,`purego` 只被 Linux 后端使用,所以两者都不在 macOS 或 Windows 程序里。开发期热重载协议、单实例套接字、PNG 图标解码和 Go 测试运行时都留在发布程序之外(热重载、单实例和 PNG 是可选包;测试运行时则完全不存在),泄漏计数器保留,因为它们每个窗口或驱动只花两个原子量。

静态画布可以通过公开 API 呈现:
Expand Down
34 changes: 34 additions & 0 deletions app/composition_handoff_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,40 @@ func TestPainterTentativeHandoffRetainsInstalledOwner(t *testing.T) {
}
}

// A cached provider can hand back a different Editor than the one the
// platform was told about, before the next sync. A native text event arrives
// on the window thread in that window: it belongs to the installed owner, not
// to whatever the last published TextInput now names.
func TestPainterAppliesTextEventsToTheInstalledOwner(t *testing.T) {
installed := text.NewEditor("abc")
other := text.NewEditor("other")
cached := &TextInput{Editor: installed, Bounds: gfx.Rect{Width: 120, Height: 24}, FontSize: 13}
var canvas gfx.Canvas
config := Config{Title: "installed owner", Width: 200, Height: 80}
config.Paint = func(canvas *gfx.Canvas, size gfx.Size) {
canvas.HitWithOptions(gfx.Item(11), gfx.Rect{Width: 120, Height: 24}, "First", gfx.HitOptions{Role: gfx.RoleTextField, Autofocus: true})
}
config.TextInput = func(TextInputContext) *TextInput { return cached }
owner := newTestPainter(t, &canvas, config)
owner.syncTextInput()
if owner.activeEditor != installed || owner.activeText == nil || owner.activeText.Editor != installed {
t.Fatal("the painter did not install the cached editor")
}
// The provider swaps its cached Editor between paints; the platform still
// talks to the installed one.
cached.Editor = other
owner.activeText.Editor = other
if _, err := owner.applyTextEvent(platform.TextEvent{Kind: platform.TextInsert, Text: "x"}); err != nil {
t.Fatal(err)
}
if got := installed.String(); got != "abcx" {
t.Fatalf("installed owner = %q, want the event applied to it", got)
}
if got := other.String(); got != "other" {
t.Fatalf("the swapped-in editor = %q, want it untouched", got)
}
}

func TestPainterCachedCustomHandoffPreservesProviderOwnership(t *testing.T) {
first, second := text.NewEditor("first"), text.NewEditor("second")
for _, editor := range []*text.Editor{first, second} {
Expand Down
24 changes: 16 additions & 8 deletions app/painter.go
Original file line number Diff line number Diff line change
Expand Up @@ -1400,15 +1400,23 @@ func (painter *painter) disableTextInput() platform.TextInputUpdate {
func (painter *painter) applyTextEvent(event platform.TextEvent) (platform.TextState, error) {
painter.tap.Cancel()
active := painter.activeText
if active == nil || active.Editor == nil && active.OnEvent == nil {
// The installed owner is the editor the platform has been told about, not
// whatever the last published TextInput names: a cached provider can hand
// back a different Editor before the next sync. Keep activeText for the
// provider's own state and CopyText.
editor := painter.activeEditor
if editor == nil && active != nil {
editor = active.Editor
}
if active == nil || editor == nil && active.OnEvent == nil {
return platform.TextState{}, fmt.Errorf("app: native text event without an active handler")
}
epoch := painter.textEpoch
appEvent := appTextEvent(event)
explicit := event.CompositionPhase != text.CompositionNone
if explicit {
if active.Editor != nil {
explicit = active.Editor.TentativeCompositionEnabled()
if editor != nil {
explicit = editor.TentativeCompositionEnabled()
} else {
explicit = active.State.TentativeComposition
}
Expand All @@ -1429,12 +1437,12 @@ func (painter *painter) applyTextEvent(event platform.TextEvent) (platform.TextS
return painter.nativeText.State, nil
}
var next TextState
if active.Editor != nil {
accepted := appEvent.apply(active.Editor)
if painter.nativeText.Secure && accepted && !active.Editor.TentativeCompositionActive() {
active.Editor.ClearHistory()
if editor != nil {
accepted := appEvent.apply(editor)
if painter.nativeText.Secure && accepted && !editor.TentativeCompositionActive() {
editor.ClearHistory()
}
next = TextStateOf(active.Editor)
next = TextStateOf(editor)
} else {
var err error
if next, err = active.OnEvent(appEvent); err != nil {
Expand Down
28 changes: 17 additions & 11 deletions cmd/viva/package_tarball.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,24 +18,30 @@ import (
)

// linuxRuntimeDepends are the libraries a Viva program opens at run time with
// dlopen (Vulkan, XCB and Cairo). Nothing in the executable's headers names
// them, so a .deb has to say so itself.
var linuxRuntimeDepends = []string{"libvulkan1", "libxcb1", "libcairo2"}
// dlopen and cannot start without: Vulkan, XCB, Cairo, and the Pango/fontconfig
// stack text rendering needs. Nothing in the executable's headers names them, so
// a .deb has to say so itself. Libraries the backend loads with a fallback (the
// optional XCB extensions and PangoFT2) stay out: requiring them would refuse an
// install on a distribution that keeps a provider out of its enabled repos.
var linuxRuntimeDepends = []string{
"libvulkan1", "libxcb1", "libcairo2",
"libfontconfig1", "libpango-1.0-0", "libpangocairo-1.0-0", "libglib2.0-0",
}

// linuxArchRuntimeDepends are the same libraries under Arch's names.
var linuxArchRuntimeDepends = []string{"vulkan-icd-loader", "libxcb", "cairo"}
var linuxArchRuntimeDepends = []string{
"vulkan-icd-loader", "libxcb", "cairo", "fontconfig", "pango", "glib2",
}

// linuxRPMRuntimeDepends names the shared-library capabilities that RPM's library
// packages provide. Distribution package names differ; both supported targets are
// 64-bit. These dlopen libraries are invisible to normal ELF dependency scanning.
// GTK, IME and WebKit integrations remain optional and are not forced on every app.
// packages provide for the same set. Distribution package names differ; both
// supported targets are 64-bit. These dlopen libraries are invisible to normal ELF
// dependency scanning. GTK, IME, WebKit and the fallback-loaded XCB extensions
// remain optional and are not forced on every app.
var linuxRPMRuntimeDepends = []string{
"libvulkan.so.1()(64bit)", "libxcb.so.1()(64bit)", "libcairo.so.2()(64bit)",
"libfontconfig.so.1()(64bit)", "libpango-1.0.so.0()(64bit)",
"libpangocairo-1.0.so.0()(64bit)", "libpangoft2-1.0.so.0()(64bit)",
"libgobject-2.0.so.0()(64bit)", "libxcb-cursor.so.0()(64bit)",
"libxcb-randr.so.0()(64bit)", "libxcb-render.so.0()(64bit)",
"libxcb-shape.so.0()(64bit)", "libxcb-sync.so.1()(64bit)",
"libpangocairo-1.0.so.0()(64bit)", "libgobject-2.0.so.0()(64bit)",
}

// linuxApplication describes the application for the Debian and RPM packages:
Expand Down
86 changes: 78 additions & 8 deletions cmd/viva/package_tarball_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ import (
"bytes"
"compress/gzip"
"crypto/sha256"
"encoding/binary"
"encoding/hex"
"fmt"
"io"
"os"
"os/exec"
"path/filepath"
"slices"
"strings"
"testing"
)
Expand Down Expand Up @@ -163,7 +165,7 @@ func TestPackageLinuxMakesADeb(t *testing.T) {
text := string(control["./control"])
for _, want := range []string{
"Package: fixture-notes\n", "Version: 2.0.1\n", "Architecture: amd64\n", "Maintainer: Ann Lee <ann@example.com>\n",
"Depends: libvulkan1, libxcb1, libcairo2, libfoo1 (>= 1.2)\n", "Homepage: https://example.com/notes\n", "Description: Takes notes\n",
"Depends: libvulkan1, libxcb1, libcairo2, libfontconfig1, libpango-1.0-0, libpangocairo-1.0-0, libglib2.0-0, libfoo1 (>= 1.2)\n", "Homepage: https://example.com/notes\n", "Description: Takes notes\n",
} {
if !strings.Contains(text, want) {
t.Errorf("control lacks %q:\n%s", want, text)
Expand Down Expand Up @@ -288,24 +290,92 @@ func TestPackageLinuxRPMDeclaresRuntimeLibraries(t *testing.T) {
if err != nil {
t.Fatal(err)
}
requires := rpmRequiredCapabilities(t, data)
for _, library := range []string{
"libvulkan.so.1", "libxcb.so.1", "libcairo.so.2", "libfontconfig.so.1",
"libpango-1.0.so.0", "libpangocairo-1.0.so.0", "libpangoft2-1.0.so.0",
"libgobject-2.0.so.0", "libxcb-cursor.so.0", "libxcb-randr.so.0",
"libpango-1.0.so.0", "libpangocairo-1.0.so.0", "libgobject-2.0.so.0",
} {
capability := library + "()(64bit)"
if !slices.Contains(requires, capability) {
t.Errorf("RPM lacks runtime requirement %s, has %v", capability, requires)
}
}
for _, library := range []string{
"libpangoft2-1.0.so.0", "libxcb-cursor.so.0", "libxcb-randr.so.0",
"libxcb-render.so.0", "libxcb-shape.so.0", "libxcb-sync.so.1",
} {
capability := library + "()(64bit)"
if !bytes.Contains(data, []byte(capability+"\x00")) {
t.Errorf("RPM lacks runtime requirement %s", capability)
if slices.Contains(requires, capability) {
t.Errorf("RPM hard-requires %s, which the backend loads with a fallback", capability)
}
}
if bytes.Contains(data, []byte("libfoo1 (>= 1.2)")) {
if slices.Contains(requires, "libfoo1 (>= 1.2)") {
t.Fatal("RPM contains a Debian-only manifest dependency")
}
})
}
}

// rpmRequiredCapabilities reads RPMTAG_REQUIRENAME from an RPM's main header.
// Grepping the whole file for the capability strings would also match the
// payload or a description, so the test could pass after the requires moved or
// were encoded differently.
func rpmRequiredCapabilities(t *testing.T, data []byte) []string {
t.Helper()
const (
leadSize = 96
headerMagic = 0x8eade801
stringArrayTp = 8
requireName = 1049
)
if len(data) < leadSize {
t.Fatal("the rpm is shorter than its lead")
}
offset := leadSize
// The signature header, padded to the eight-byte boundary the main header
// starts on.
if len(data) < offset+16 {
t.Fatal("the rpm has no signature header")
}
index := int(binary.BigEndian.Uint32(data[offset+8:]))
store := int(binary.BigEndian.Uint32(data[offset+12:]))
offset = (offset + 16 + index*16 + store + 7) &^ 7
if len(data) < offset+16 || binary.BigEndian.Uint32(data[offset:]) != headerMagic {
t.Fatal("the rpm has no main header")
}
entries := int(binary.BigEndian.Uint32(data[offset+8:]))
storeSize := int(binary.BigEndian.Uint32(data[offset+12:]))
indexStart := offset + 16
storeStart := indexStart + entries*16
if storeStart+storeSize > len(data) {
t.Fatal("the rpm header runs past the file")
}
for entry := 0; entry < entries; entry++ {
at := indexStart + entry*16
tag := binary.BigEndian.Uint32(data[at:])
if tag != requireName {
continue
}
if kind := binary.BigEndian.Uint32(data[at+4:]); kind != stringArrayTp {
t.Fatalf("REQUIRENAME has type %d, want a string array", kind)
}
count := int(binary.BigEndian.Uint32(data[at+12:]))
position := storeStart + int(binary.BigEndian.Uint32(data[at+8:]))
names := make([]string, 0, count)
for range count {
end := bytes.IndexByte(data[position:storeStart+storeSize], 0)
if end < 0 {
t.Fatal("REQUIRENAME holds an unterminated string")
}
names = append(names, string(data[position:position+end]))
position += end + 1
}
return names
}
t.Fatal("the rpm declares no REQUIRENAME tag")
return nil
}

func TestPackageLinuxFailures(t *testing.T) {
for _, test := range []struct {
name, toml string
Expand Down Expand Up @@ -375,7 +445,7 @@ func TestPackageLinuxRefusesAFileAsOutput(t *testing.T) {

func TestDebDepends(t *testing.T) {
got := strings.Join(debDepends([]string{"libcairo2 (>= 1.16)", "foo"}), ", ")
if got != "libvulkan1, libxcb1, libcairo2 (>= 1.16), foo" {
if got != "libvulkan1, libxcb1, libfontconfig1, libpango-1.0-0, libpangocairo-1.0-0, libglib2.0-0, libcairo2 (>= 1.16), foo" {
t.Errorf("depends = %q", got)
}
}
Expand Down Expand Up @@ -441,7 +511,7 @@ func TestPackageLinuxMakesAPKGBUILD(t *testing.T) {
"pkgname='fixture-notes-bin'",
"pkgver='2.0.1'",
"arch=('x86_64')",
"depends=('vulkan-icd-loader' 'libxcb>=1.16' 'cairo' 'extra-lib')",
"depends=('vulkan-icd-loader' 'fontconfig' 'pango' 'glib2' 'libxcb>=1.16' 'cairo' 'extra-lib')",
"source=('fixture-notes-2.0.1-linux-amd64.tar.gz')",
"sha256sums=('" + hex.EncodeToString(digest[:]) + "')",
"_icon_size=256",
Expand Down
3 changes: 3 additions & 0 deletions internal/platform/linux/ime_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,9 @@ func (ime *linuxIME) sync() {
// this native session, keep the text the field already
// shows, and let sync open a fresh context next frame.
keep := ime.phase != nil && ime.phase.session
// Retire before settling: the settlement is delivered through
// the router, and a reentrant sync must not find the session it
// just retired still current.
ime.retire()
if keep {
ime.apply(platform.TextEvent{Kind: platform.TextUnmark, CompositionPhase: text.CompositionCommit})
Expand Down
6 changes: 5 additions & 1 deletion internal/platform/linux/ime_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,12 @@ func newIMEHarness(t testing.TB) *imeHarness {
_, _, err := harness.input.prepare(frame)
return err
}
// ibusTried keeps a present() that defers sync from dialing the host's
// daemon: these tests are about the state machine, and a machine with a
// reachable IBus address would otherwise get a fresh native context in the
// middle of an assertion.
harness.ime = &linuxIME{
app: &linuxApplication{}, text: harness.input, router: router, context: harness.context,
app: &linuxApplication{ibusTried: true}, text: harness.input, router: router, context: harness.context,
origin: func() (int32, int32) { return harness.origin[0], harness.origin[1] },
}
router.ime = harness.ime
Expand Down
24 changes: 24 additions & 0 deletions internal/platform/linux/ime_retirement_linux_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package linux

import (
"errors"
"os"
"testing"

Expand Down Expand Up @@ -31,7 +32,11 @@ func TestIBusTentativeUnsupportedResetSettlesEditorBeforeRetirement(t *testing.T
if !h.ime.composing() {
t.Fatal("native session did not start")
}
settled := len(h.events)
h.ime.setFocused(false)
if len(h.events) != settled+1 {
t.Fatalf("retirement settled %d events, want exactly one", len(h.events)-settled)
}
if h.ime.app.firstErr != nil || h.ime.context != nil || !h.context.closed {
t.Fatal("unsupported mode did not retire the context without disabling input")
}
Expand Down Expand Up @@ -59,6 +64,25 @@ func TestIBusTentativeUnsupportedResetSettlesEditorBeforeRetirement(t *testing.T
}
}

// A Reset that crosses an owner change must not settle the retired session's
// text into whatever now owns the field: sync re-checks the context, epoch and
// owner after a failed reset.
func TestRetirementKeepsSettlementWhenResetCrossesAnOwnerChange(t *testing.T) {
h := newTentativeIMEHarness(t)
h.preedit(t, nativeMode("ni", ibus.PreeditModeClear))
before := len(h.events)
h.context.err = errors.New("reset failed")
h.context.onCall = func(name string) {
if name == "Reset" {
h.ime.epoch++
}
}
h.ime.setFocused(false)
if len(h.events) != before || h.ime.context == nil {
t.Fatalf("a settlement crossed an owner change: events %d -> %d, context live=%t", before, len(h.events), h.ime.context != nil)
}
}

func TestNativeIBusUnsupportedModeRetirement(t *testing.T) {
if os.Getenv("VIVA_IBUS_NATIVE") != "1" {
t.Skip("native retirement requires a private input-method daemon")
Expand Down
Loading
Loading