diff --git a/README.md b/README.md index dce853abb..181e5ece1 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/app/title.go b/app/title.go new file mode 100644 index 000000000..3ad9c3a3e --- /dev/null +++ b/app/title.go @@ -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) +} diff --git a/app/title_test.go b/app/title_test.go new file mode 100644 index 000000000..a2828d29d --- /dev/null +++ b/app/title_test.go @@ -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) + } +} diff --git a/docs/conformance.md b/docs/conformance.md index 928444774..7586a44af 100644 --- a/docs/conformance.md +++ b/docs/conformance.md @@ -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 diff --git a/headless/application.go b/headless/application.go index e109d4bf4..e6db9731e 100644 --- a/headless/application.go +++ b/headless/application.go @@ -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] diff --git a/internal/platform/darwin/native.h b/internal/platform/darwin/native.h index 2560cb6da..a08ff0bd9 100644 --- a/internal/platform/darwin/native.h +++ b/internal/platform/darwin/native.h @@ -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); diff --git a/internal/platform/darwin/native.m b/internal/platform/darwin/native.m index 0de95ae4a..5eb7c1678 100644 --- a/internal/platform/darwin/native.m +++ b/internal/platform/darwin/native.m @@ -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; @@ -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) { @@ -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) = ^{ diff --git a/internal/platform/darwin/platform.go b/internal/platform/darwin/platform.go index 1ffa55d7e..226d30ff9 100644 --- a/internal/platform/darwin/platform.go +++ b/internal/platform/darwin/platform.go @@ -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 } diff --git a/internal/platform/linux/gtk_window_linux.go b/internal/platform/linux/gtk_window_linux.go index 62fdd2e26..c185e08f1 100644 --- a/internal/platform/linux/gtk_window_linux.go +++ b/internal/platform/linux/gtk_window_linux.go @@ -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") diff --git a/internal/platform/windows/title_windows.go b/internal/platform/windows/title_windows.go new file mode 100644 index 000000000..e8959a93f --- /dev/null +++ b/internal/platform/windows/title_windows.go @@ -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))) +} diff --git a/internal/platform/windows/window_windows.go b/internal/platform/windows/window_windows.go index 17d495bb5..d2268b024 100644 --- a/internal/platform/windows/window_windows.go +++ b/internal/platform/windows/window_windows.go @@ -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 @@ -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 {