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.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ 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 Down
24 changes: 24 additions & 0 deletions app/title.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package app

import "fmt"

type titleControl interface {
SetTitle(string) error
}

// SetTitle changes the window's title: the text of its system title bar, the
// task switcher and the taskbar or Dock window list. A window with
// WindowCustomTitlebar keeps no visible system title bar, but the title is still
// what the system lists and what assistive technology reads. Calls are safe from
// any goroutine; the change is applied on the window's thread. Unsupported hosts
// return an error wrapping ErrUnsupported.
func (window *Window) SetTitle(title string) error {
if window == nil || window.closed.Load() {
return fmt.Errorf("app: window is closed")
}
control, ok := window.control.(titleControl)
if !ok {
return fmt.Errorf("%w: window titles", ErrUnsupported)
}
return control.SetTitle(title)
}
41 changes: 41 additions & 0 deletions app/title_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
//go:build darwin || windows || linux

package app

import (
"errors"
"testing"
)

type titleTestControl struct {
fakeWindow
titles []string
}

func (control *titleTestControl) SetTitle(title string) error {
control.titles = append(control.titles, title)
return nil
}

func TestSetTitleDelegatesToTheHost(t *testing.T) {
control := &titleTestControl{}
window := &Window{control: control}
if err := window.SetTitle("project – main.go"); err != nil {
t.Fatal(err)
}
if len(control.titles) != 1 || control.titles[0] != "project – main.go" {
t.Fatalf("titles = %q", control.titles)
}
window.closed.Store(true)
if err := window.SetTitle("closed"); err == nil || len(control.titles) != 1 {
t.Fatal("closed window accepted a title")
}
var missing *Window
if err := missing.SetTitle("nil"); err == nil {
t.Fatal("nil window accepted a title")
}
unsupported := &Window{control: &fakeWindow{}}
if err := unsupported.SetTitle("x"); !errors.Is(err, ErrUnsupported) {
t.Fatalf("unsupported host: %v", err)
}
}
8 changes: 8 additions & 0 deletions docs/conformance.md
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,14 @@ moves). `VIVA_SCALE` overrides it for Viva alone, snapped to quarters and kept b
Linux: it draws a window at 125% or 150% on a display at 100%, which is how fractional scaling
was measured. The renderer puts every label texture and image on a whole device pixel.

## Window titles

`Window.SetTitle` changes the native title after creation from any goroutine. macOS
sets the NSWindow title on the main thread, Windows posts the change to the window
thread and applies it with `SetWindowTextW` (also updating the accessibility tree's
window name), and Linux sets the GTK toplevel's title on the GTK thread. The
headless host records it in the `windows` listing.

## Fullscreen windows

`Window.SetFullscreen` requests native fullscreen; `Config.OnFullscreen` publishes
Expand Down
12 changes: 12 additions & 0 deletions headless/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,18 @@ func (window *headlessWindow) SetFullscreen(fullscreen bool) error {
return nil
}

// SetTitle records the title the windows command lists.
func (window *headlessWindow) SetTitle(title string) error {
window.application.mu.Lock()
defer window.application.mu.Unlock()
entry := window.application.windows[window.id]
if entry == nil {
return fmt.Errorf("headless: window is closed")
}
entry.title = title
return nil
}

func (window *headlessWindow) Fullscreen() bool {
window.application.mu.Lock()
entry := window.application.windows[window.id]
Expand Down
1 change: 1 addition & 0 deletions internal/platform/darwin/native.h
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,7 @@ int viva_application_window_frame(void *window, double *out);
/* Puts the window's content area at x, y in top-left screen coordinates. */
void viva_application_move_window(void *window, double x, double y);
int viva_application_set_fullscreen(void *window, int fullscreen);
int viva_application_set_title(void *window, const char *title);
int viva_application_fullscreen(void *window);
int viva_application_set_always_on_top(void *window, int enabled);
int viva_application_run(void);
Expand Down
31 changes: 31 additions & 0 deletions internal/platform/darwin/native.m
Original file line number Diff line number Diff line change
Expand Up @@ -5702,6 +5702,7 @@ - (void)confirmClose;
- (void)hideWindow;
- (void)showWindow;
- (void)setFullscreen:(BOOL)fullscreen;
- (void)setWindowTitle:(NSString *)title;
- (void)setAlwaysOnTop:(BOOL)enabled;
- (BOOL)isFullscreen;
- (void)reopenWindow;
Expand Down Expand Up @@ -6244,6 +6245,14 @@ - (BOOL)isFullscreen {
return (_window.styleMask & NSWindowStyleMaskFullScreen) != 0;
}

- (void)setWindowTitle:(NSString *)title {
[_title release];
_title = [title copy];
if (_window != nil) {
_window.title = _title;
}
}

- (void)setFullscreen:(BOOL)fullscreen {
_fullscreenTarget = fullscreen;
if (!_fullscreenTransition && [self isFullscreen] != fullscreen) {
Expand Down Expand Up @@ -6960,6 +6969,28 @@ int viva_application_set_fullscreen(void *window, int fullscreen) {
return found;
}

int viva_application_set_title(void *window, const char *title) {
__block int found = 0;
NSString *value = [[NSString alloc] initWithUTF8String:title != NULL ? title : ""];
if (value == nil) {
value = [@"" retain];
}
void (^work)(void) = ^{
VivaAppDelegate *delegate = viva_find_window(window);
if (delegate != nil) {
found = 1;
[delegate setWindowTitle:value];
}
};
if ([NSThread isMainThread]) {
work();
} else {
dispatch_sync(dispatch_get_main_queue(), work);
}
[value release];
return found;
}

int viva_application_fullscreen(void *window) {
__block int fullscreen = 0;
void (^work)(void) = ^{
Expand Down
13 changes: 13 additions & 0 deletions internal/platform/darwin/platform.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,19 @@ func (window *nativeWindow) SetFullscreen(fullscreen bool) error {
return nil
}

// SetTitle changes the NSWindow's title on the main thread.
func (window *nativeWindow) SetTitle(title string) error {
if window == nil || window.closed.Load() {
return fmt.Errorf("darwin: window is closed")
}
value := C.CString(title)
defer C.free(unsafe.Pointer(value))
if C.viva_application_set_title(window.token, value) == 0 {
return fmt.Errorf("darwin: window is unavailable")
}
return nil
}

func (window *nativeWindow) Fullscreen() bool {
return window != nil && !window.closed.Load() && C.viva_application_fullscreen(window.token) != 0
}
Expand Down
11 changes: 11 additions & 0 deletions internal/platform/linux/gtk_window_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,17 @@ func (w *gtkWindow) SetFullscreen(fullscreen bool) error {

func (w *gtkWindow) Fullscreen() bool { return w.fullscreen.Load() }

// SetTitle changes the toplevel's title on the GTK thread.
func (w *gtkWindow) SetTitle(title string) error {
w.app.enqueue(func() {
if !w.closed {
value := cString(title)
w.g.windowSetTitle(w.toplevel, &value[0])
}
})
return nil
}

func (w *gtkWindow) SetAlwaysOnTop(enabled bool) error {
if w == nil || w.app == nil {
return fmt.Errorf("linux: window is closed")
Expand Down
54 changes: 54 additions & 0 deletions internal/platform/windows/title_windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package windows

import (
"fmt"
"sync"
"syscall"
"unsafe"
)

// wmSetTitle asks the window thread to apply the title SetTitle stored.
// SetWindowTextW from another thread would send WM_SETTEXT and wait for the
// window thread, so the change is posted instead.
const wmSetTitle = 0x8052

var setWindowText = user.NewProc("SetWindowTextW")

// titleRequest holds the latest title asked for until the window thread applies it.
type titleRequest struct {
mu sync.Mutex
title string
pending bool
}

func (window *nativeWindow) SetTitle(title string) error {
if window == nil || window.closed.Load() {
return fmt.Errorf("windows: window is closed")
}
request := &window.state.pendingTitle
request.mu.Lock()
request.title, request.pending = title, true
request.mu.Unlock()
if ok, _, err := postMessage.Call(window.state.window, wmSetTitle, 0, 0); ok == 0 {
return fmt.Errorf("windows: request title: %w", err)
}
return nil
}

// applyTitle sets the stored title on the window and in the accessibility tree.
func (state *windowState) applyTitle() {
request := &state.pendingTitle
request.mu.Lock()
title, pending := request.title, request.pending
request.pending = false
request.mu.Unlock()
if !pending || state.window == 0 {
return
}
value, err := syscall.UTF16PtrFromString(title)
if err != nil {
return
}
state.config.Title = title
setWindowText.Call(state.window, uintptr(unsafe.Pointer(value)))
}
5 changes: 5 additions & 0 deletions internal/platform/windows/window_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ const (
)

type windowState struct {
// pendingTitle is the title SetTitle asked for, applied on the window thread.
pendingTitle titleRequest
fullscreen atomic.Bool
fullscreenChanging bool
fullscreenStyle uint32
Expand Down Expand Up @@ -433,6 +435,9 @@ func windowProc(window, kind, first, second uintptr) (result uintptr) {
case wmFullscreen:
state.setFullscreen(first != 0)
return 0
case wmSetTitle:
state.applyTitle()
return 0
case wmSysCommand:
if state.fullscreen.Load() {
switch first & 0xfff0 {
Expand Down