Skip to content

Commit

Permalink
shiny: add window title support
Browse files Browse the repository at this point in the history
Titles are UTF-8 compatible on all platforms; drivers
default to an empty string.

Change-Id: I4cf1a16ceb4e9641b2e1fc78dcff0501aac7dedb
Reviewed-on: https://go-review.googlesource.com/37200
Reviewed-by: Nigel Tao <[email protected]>
  • Loading branch information
fabian-z authored and nigeltao committed Feb 23, 2017
1 parent 1451a5b commit 35a3e98
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 12 deletions.
14 changes: 12 additions & 2 deletions shiny/driver/gldriver/cocoa.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,13 @@ package gldriver
#import <Cocoa/Cocoa.h>
#include <pthread.h>
#include <stdint.h>
#include <stdlib.h>
void startDriver();
void stopDriver();
void makeCurrentContext(uintptr_t ctx);
void flushContext(uintptr_t ctx);
uintptr_t doNewWindow(int width, int height);
uintptr_t doNewWindow(int width, int height, char* title);
void doShowWindow(uintptr_t id);
void doCloseWindow(uintptr_t id);
uint64_t threadID();
Expand All @@ -33,6 +34,7 @@ import (
"fmt"
"log"
"runtime"
"unsafe"

"golang.org/x/exp/shiny/driver/internal/lifecycler"
"golang.org/x/exp/shiny/screen"
Expand Down Expand Up @@ -62,7 +64,15 @@ func init() {

func newWindow(opts *screen.NewWindowOptions) (uintptr, error) {
width, height := optsSize(opts)
return uintptr(C.doNewWindow(C.int(width), C.int(height))), nil

var title string
if opts != nil {
title = opts.Title
}
titlePtr := C.CString(title)
defer C.free(unsafe.Pointer(titlePtr))

return uintptr(C.doNewWindow(C.int(width), C.int(height), titlePtr)), nil
}

func initWindow(w *windowImpl) {
Expand Down
4 changes: 2 additions & 2 deletions shiny/driver/gldriver/cocoa.m
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ - (void)applicationWillHide:(NSNotification *)aNotification {
}
@end

uintptr_t doNewWindow(int width, int height) {
uintptr_t doNewWindow(int width, int height, char* title) {
NSScreen *screen = [NSScreen mainScreen];
double w = (double)width / [screen backingScaleFactor];
double h = (double)height / [screen backingScaleFactor];
Expand All @@ -254,7 +254,7 @@ uintptr_t doNewWindow(int width, int height) {
[NSApp setMainMenu:menuBar];

id menu = [NSMenu new];
NSString* name = [[NSProcessInfo processInfo] processName];
NSString* name = [[NSString alloc] initWithUTF8String:title];

id hideMenuItem = [[NSMenuItem alloc] initWithTitle:@"Hide"
action:@selector(hide:) keyEquivalent:@"h"];
Expand Down
14 changes: 11 additions & 3 deletions shiny/driver/gldriver/x11.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,14 @@
#include <EGL/egl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

Atom utf8_string;
Atom wm_delete_window;
Atom wm_name;
Atom wm_protocols;
Atom wm_take_focus;

EGLConfig e_config;
EGLContext e_ctx;
EGLDisplay e_dpy;
Expand Down Expand Up @@ -127,9 +131,11 @@ startDriver() {
exit(1);
}

utf8_string = XInternAtom(x_dpy, "UTF8_STRING", False);
wm_delete_window = XInternAtom(x_dpy, "WM_DELETE_WINDOW", False);
wm_name = XInternAtom(x_dpy, "WM_NAME", False);
wm_protocols = XInternAtom(x_dpy, "WM_PROTOCOLS", False);
wm_take_focus= XInternAtom(x_dpy, "WM_TAKE_FOCUS", False);
wm_take_focus = XInternAtom(x_dpy, "WM_TAKE_FOCUS", False);

const int key_lo = 8;
const int key_hi = 255;
Expand Down Expand Up @@ -225,7 +231,7 @@ doCloseWindow(uintptr_t id) {
}

uintptr_t
doNewWindow(int width, int height) {
doNewWindow(int width, int height, char* title) {
XSetWindowAttributes attr;
attr.colormap = x_colormap;
attr.event_mask =
Expand Down Expand Up @@ -253,7 +259,9 @@ doNewWindow(int width, int height) {
atoms[1] = wm_take_focus;
XSetWMProtocols(x_dpy, win, atoms, 2);

XSetStandardProperties(x_dpy, win, "App", "App", None, (char **)NULL, 0, &sizehints);
XSetStandardProperties(x_dpy, win, "", "App", None, (char **)NULL, 0, &sizehints);
XChangeProperty(x_dpy, win, wm_name, utf8_string, 8, PropModeReplace, title, strlen(title));

return win;
}

Expand Down
14 changes: 12 additions & 2 deletions shiny/driver/gldriver/x11.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,15 @@ package gldriver
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
char *eglGetErrorStr();
void startDriver();
void processEvents();
void makeCurrent(uintptr_t ctx);
void swapBuffers(uintptr_t ctx);
void doCloseWindow(uintptr_t id);
uintptr_t doNewWindow(int width, int height);
uintptr_t doNewWindow(int width, int height, char* title);
uintptr_t doShowWindow(uintptr_t id);
uintptr_t surfaceCreate();
*/
Expand All @@ -27,6 +28,7 @@ import (
"errors"
"runtime"
"time"
"unsafe"

"golang.org/x/exp/shiny/driver/internal/x11key"
"golang.org/x/exp/shiny/screen"
Expand All @@ -50,10 +52,18 @@ func init() {

func newWindow(opts *screen.NewWindowOptions) (uintptr, error) {
width, height := optsSize(opts)

var title string
if opts != nil {
title = opts.Title
}
titlePtr := C.CString(title)
defer C.free(unsafe.Pointer(titlePtr))

retc := make(chan uintptr)
uic <- uiClosure{
f: func() uintptr {
return uintptr(C.doNewWindow(C.int(width), C.int(height)))
return uintptr(C.doNewWindow(C.int(width), C.int(height), titlePtr))
},
retc: retc,
}
Expand Down
8 changes: 6 additions & 2 deletions shiny/driver/internal/win32/win32.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,11 @@ func newWindow(opts *screen.NewWindowOptions) (syscall.Handle, error) {
if err != nil {
return 0, err
}
title, err := syscall.UTF16PtrFromString("Shiny Window")
var title string
if opts != nil {
title = opts.Title
}
windowTitle, err := syscall.UTF16PtrFromString(title)
if err != nil {
return 0, err
}
Expand All @@ -80,7 +84,7 @@ func newWindow(opts *screen.NewWindowOptions) (syscall.Handle, error) {
}
}
hwnd, err := _CreateWindowEx(0,
wcname, title,
wcname, windowTitle,
_WS_OVERLAPPEDWINDOW,
_CW_USEDEFAULT, _CW_USEDEFAULT,
int32(w), int32(h),
Expand Down
12 changes: 12 additions & 0 deletions shiny/driver/x11driver/screen.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ type screenImpl struct {
xsi *xproto.ScreenInfo
keysyms x11key.KeysymTable

atomUTF8String xproto.Atom
atomWMDeleteWindow xproto.Atom
atomWMProtocols xproto.Atom
atomWMTakeFocus xproto.Atom
Expand Down Expand Up @@ -421,6 +422,13 @@ func (s *screenImpl) NewWindow(opts *screen.NewWindowOptions) (screen.Window, er
},
)
s.setProperty(xw, s.atomWMProtocols, s.atomWMDeleteWindow, s.atomWMTakeFocus)

var title []byte
if opts != nil {
title = []byte(opts.Title)
}
xproto.ChangeProperty(s.xc, xproto.PropModeReplace, xw, xproto.AtomWmName, s.atomUTF8String, 8, uint32(len(title)), title)

xproto.CreateGC(s.xc, xg, xproto.Drawable(xw), 0, nil)
render.CreatePicture(s.xc, xp, xproto.Drawable(xw), pictformat, 0, nil)
xproto.MapWindow(s.xc, xw)
Expand All @@ -429,6 +437,10 @@ func (s *screenImpl) NewWindow(opts *screen.NewWindowOptions) (screen.Window, er
}

func (s *screenImpl) initAtoms() (err error) {
s.atomUTF8String, err = s.internAtom("UTF8_STRING")
if err != nil {
return err
}
s.atomWMDeleteWindow, err = s.internAtom("WM_DELETE_WINDOW")
if err != nil {
return err
Expand Down
5 changes: 4 additions & 1 deletion shiny/screen/screen.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,10 @@ type NewWindowOptions struct {
// zero value dimension.
Width, Height int

// TODO: fullscreen, title, icon, cursorHidden?
// Title specifies the window title.
Title string

// TODO: fullscreen, icon, cursorHidden?
}

// Uploader is something you can upload a Buffer to.
Expand Down

0 comments on commit 35a3e98

Please sign in to comment.