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
11 changes: 6 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ Viva reaches Cocoa and Metal through cgo; on Linux, X11 with the system XCB and
Vulkan loaders; on Windows, Win32 with Direct3D 11 and DirectWrite. Viva loads
the system libraries at runtime and bundles none of them.

The current release is `v0.6.4`; name it explicitly (`@v0.6.4`) to pin it. Guides,
The current release is `v0.6.5`; name it explicitly (`@v0.6.5`) to pin it. Guides,
the API reference, and a component catalog with live previews live in
[`website/`](website/); run it with `npm run dev` there, or read them on
[vivaui.dev](https://vivaui.dev/en/), which is also available in
Expand Down Expand Up @@ -100,8 +100,6 @@ WebView and no bundled rendering runtime.
by the application.
- Native fullscreen through `Window.SetFullscreen`, with `Config.OnFullscreen`
reporting system transitions and restoring normal window placement on exit.
- `Window.SetTitle` changes a window's title after it opens: the system title
bar, the task switcher and the taskbar or Dock window list.
- Explicit multi-window applications through `app.RunApplication`: every
window owns independent retained paint, focus, input, text, invalidation, and
close state, and the native loop exits after the final window closes.
Expand All @@ -121,8 +119,11 @@ WebView and no bundled rendering runtime.
releasing it ends the request.
- `Application.SetMenuBar` puts the application's own File, Edit and View menus
between the system's application menu and Window menu on macOS, with key
equivalents, enablement, check marks, submenus and loop-thread callbacks; the
other desktop targets build their menus in the window with `components.Menubar`.
equivalents, enablement, check marks and submenus. An entry's action returns
the same `gfx.EventResult` a key handler does, and the runtime carries its
focus, clipboard and repaint out on the active window, so a command chosen
from the menu behaves as its shortcut does; the other desktop targets build
their menus in the window with `components.Menubar`.
- `Application.Post` hands work a background goroutine finished to the window
thread: accepted functions run there in order, with no goroutine, timer or
blocking wait added, and no "call the loop and wait" form that could deadlock.
Expand Down
2 changes: 1 addition & 1 deletion README.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ viva dev . # hot reload: save a file, the window updates and keeps its st

环境要求:Go 1.26 或更新版本(缺失时 viva 会自己下载)。macOS 上需要 Xcode 命令行工具,因为 Viva 通过 cgo 访问 Cocoa 和 Metal;Linux 上需要带系统 XCB 和 Vulkan 加载库的 X11;Windows 上需要 Win32、Direct3D 11 和 DirectWrite。Viva 在运行时加载系统库,自身不捆绑任何一个。

当前版本是 `v0.6.4`;要固定版本就写明(`@v0.6.4`)。指南、API 参考和带实时预览的组件目录在 [`website/`](website/) 里,本地用 `npm run dev` 运行;也已经发布在 [vivaui.dev](https://vivaui.dev/zh/)。
当前版本是 `v0.6.5`;要固定版本就写明(`@v0.6.5`)。指南、API 参考和带实时预览的组件目录在 [`website/`](website/) 里,本地用 `npm run dev` 运行;也已经发布在 [vivaui.dev](https://vivaui.dev/zh/)。

下面的一切描述的都是这个仓库:它的测量、它自己的程序,以及如何开发框架本身,而不是如何用框架。

Expand Down
70 changes: 67 additions & 3 deletions app/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"sync/atomic"

"github.com/vivaui/viva/gfx"
"github.com/vivaui/viva/keymap"
)

// ErrNoWindows is returned when an application startup callback returns
Expand Down Expand Up @@ -87,6 +88,16 @@ type Window struct {
fullscreen atomic.Bool
topmostMu sync.Mutex
topmost atomic.Bool
// applyResult carries out a command's event result on this window. The
// native host sets it; a host without windows leaves it nil, and the result
// of a menu action has nowhere to go.
applyResult func(gfx.EventResult) bool
requestRedraw func()
// runShortcut offers a menu entry's chord to the application's shortcut
// handler with the control that holds the keyboard, as pressing the chord
// does. The native host sets it; nil means no handler and the entry's
// OnSelect runs instead.
runShortcut func(keymap.Chord) (gfx.EventResult, bool)
}

// Close requests that the native window close. Closing the final window ends
Expand Down Expand Up @@ -196,9 +207,13 @@ func (window *Window) Closed() bool {
// RunApplication call. OpenWindow must be called from the startup callback or
// a window callback, both of which run on the native event-loop thread.
type Application struct {
host Host
mu sync.Mutex
windows map[*Window]struct{}
host Host
mu sync.Mutex
windows map[*Window]struct{}
// active is the window that last became the one the user works in, which is
// where the application menu bar's commands take effect, as a native menu's
// key equivalents do.
active *Window
openedAny bool
}

Expand All @@ -223,8 +238,16 @@ func (application *Application) OpenWindow(config Config, canvas *gfx.Canvas) (*
})
closed := func() {
window.closed.Store(true)
application.forgetActive(window)
forget()
}
onActivate := config.OnActivate
config.OnActivate = func() {
application.setActive(window)
if onActivate != nil {
onActivate()
}
}
// The runtime tracks the minimize state for every window, so Window.Minimized
// answers even when the application did not ask for OnMinimize. There is no
// minimize event during OpenWindow, so the zero value is the initial state.
Expand Down Expand Up @@ -256,12 +279,53 @@ func (application *Application) OpenWindow(config Config, canvas *gfx.Canvas) (*
return nil, err
}
window.control = control
if applier, ok := control.(interface {
applyResult(gfx.EventResult) bool
requestRedraw()
runShortcut(keymap.Chord) (gfx.EventResult, bool)
}); ok {
window.applyResult, window.requestRedraw, window.runShortcut = applier.applyResult, applier.requestRedraw, applier.runShortcut
}
application.mu.Lock()
application.openedAny = true
application.mu.Unlock()
return window, nil
}

// setActive records the window the user is working in.
func (application *Application) setActive(window *Window) {
application.mu.Lock()
application.active = window
application.mu.Unlock()
}

// forgetActive clears the active window as it closes, so a menu action after it
// does not run against a window that no longer paints.
func (application *Application) forgetActive(window *Window) {
application.mu.Lock()
if application.active == window {
application.active = nil
}
application.mu.Unlock()
}

// activeWindow is where the application menu's commands take effect: the
// window that last became active, or the only window while no activation has
// been reported. Nil when there is no such window.
func (application *Application) activeWindow() *Window {
application.mu.Lock()
defer application.mu.Unlock()
if application.active != nil && !application.active.closed.Load() {
return application.active
}
if len(application.windows) == 1 {
for window := range application.windows {
return window
}
}
return nil
}

// AddStatusItem puts an icon in the system's status area. It is called from the
// startup callback or a window callback, like OpenWindow.
func (application *Application) AddStatusItem(config StatusItemConfig) (*StatusItem, error) {
Expand Down
40 changes: 38 additions & 2 deletions app/application_native.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

"github.com/vivaui/viva/gfx"
"github.com/vivaui/viva/internal/platform"
"github.com/vivaui/viva/keymap"
)

// RunApplication starts the native application loop. start runs once on the
Expand Down Expand Up @@ -46,7 +47,7 @@ func RunApplication(start func(*Application) error) error {
type nativeHost struct{ native platform.Application }

func (host nativeHost) OpenWindow(config Config, canvas *gfx.Canvas, closed func()) (WindowControl, error) {
windowConfig, cleanup, err := prepareNativeWindow(config, canvas)
windowConfig, painter, cleanup, err := prepareNativeWindow(config, canvas)
if err != nil {
return nil, err
}
Expand All @@ -63,7 +64,42 @@ func (host nativeHost) OpenWindow(config Config, canvas *gfx.Canvas, closed func
cleanup()
return nil, err
}
return window, nil
// A command the runtime did not receive through an event handler, such as a
// menu action, hands its result here: the painter carries it out and the
// window's waker asks for the frame that shows it.
return resultWindow{WindowControl: window, painter: painter, waker: windowConfig.Waker}, nil
}

// resultWindow is the control of a window whose painter can carry out an event
// result from outside an event handler.
type resultWindow struct {
WindowControl
painter *painter
waker *platform.Waker
}

// What OpenWindow looks for in a host's window control.
var _ interface {
applyResult(gfx.EventResult) bool
requestRedraw()
runShortcut(keymap.Chord) (gfx.EventResult, bool)
} = resultWindow{}

func (window resultWindow) applyResult(result gfx.EventResult) bool {
return window.painter.applyEventResult(result)
}

func (window resultWindow) requestRedraw() {
if window.waker != nil {
window.waker.Wake()
}
}

// runShortcut offers a menu entry's chord to the shortcut handler with the
// control that holds the keyboard, as the platform offers a key it did not
// claim itself.
func (window resultWindow) runShortcut(chord keymap.Chord) (gfx.EventResult, bool) {
return window.painter.menuShortcut(chord)
}

// CanPreview says whether the adapter's windows can be a DragPreview.
Expand Down
43 changes: 43 additions & 0 deletions app/application_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"testing"

"github.com/vivaui/viva/gfx"
"github.com/vivaui/viva/keymap"
)

// fakeHost is an event loop with no windows: it records what the application
Expand All @@ -17,6 +18,9 @@ type fakeHost struct {
items []*fakeStatusItem
quits int
reject string
// menuBar is the menu bar the application installed, already wrapped so its
// actions carry out what they did in the active window.
menuBar []Menu
// badge is the label the application last put on the Dock tile.
badge string
// progress is the fraction the application last asked the icon to show.
Expand All @@ -28,6 +32,13 @@ type fakeWindow struct {
closed func()
hides, shows int
done bool
// focused is the control that holds the keyboard, which a menu shortcut
// aims at before the entry's own action.
focused gfx.ID
// applied is what an application menu action asked the window to carry
// out, in order; redraws counts the frames it asked for.
applied []gfx.EventResult
redraws int
}

func (window *fakeWindow) Close() {
Expand All @@ -39,6 +50,33 @@ func (window *fakeWindow) Close() {
func (window *fakeWindow) Hide() { window.hides++ }
func (window *fakeWindow) Show() { window.shows++ }

func (window *fakeWindow) applyResult(result gfx.EventResult) bool {
window.applied = append(window.applied, result)
return !result.Focus.IsZero()
}

func (window *fakeWindow) requestRedraw() { window.redraws++ }

// runShortcut plays the platform offering a menu entry's chord to the
// application, as pressing the same key would deliver it.
func (window *fakeWindow) runShortcut(chord keymap.Chord) (gfx.EventResult, bool) {
if !chord.Valid() {
return gfx.EventResult{}, false
}
handler := window.config.OnKey
event := gfx.KeyEvent{Key: chord.Key, Modifiers: chord.Modifiers, Target: window.focused}
if chord.Char != 0 {
handler = window.config.OnShortcut
event.Key = gfx.KeyUnknown
event.Text = string(chord.Char)
}
if handler == nil {
return gfx.EventResult{}, false
}
response := handler(event)
return response, response.Handled
}

// Frame is where the window's Config.Origin put it.
func (window *fakeWindow) Frame() (gfx.Rect, bool) {
origin := window.config.originPoint()
Expand Down Expand Up @@ -75,6 +113,11 @@ func (host *fakeHost) SetDockBadge(text string) { host.badge = text }

func (host *fakeHost) SetProgress(fraction float32) { host.progress = fraction }

func (host *fakeHost) SetMenuBar(menus []Menu) error {
host.menuBar = menus
return nil
}

func (host *fakeHost) Post(fn func()) {
if fn != nil {
fn()
Expand Down
2 changes: 1 addition & 1 deletion app/initial_bitmap_headless_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func TestPreparedWindowUploadsInitialBitmapToMetal(test *testing.T) {
var canvas gfx.Canvas
config.Paint(&canvas, gfx.Size{Width: 80, Height: 40})
caption = "ready"
native, cleanup, err := prepareNativeWindow(config, &canvas)
native, _, cleanup, err := prepareNativeWindow(config, &canvas)
if err != nil {
test.Fatal(err)
}
Expand Down
Loading
Loading