Skip to content

Commit

Permalink
adding support for initial centered Windows
Browse files Browse the repository at this point in the history
  • Loading branch information
phaus committed Nov 5, 2020
1 parent a3833e1 commit 6b4e3a0
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 4 deletions.
2 changes: 0 additions & 2 deletions examples/helloworld/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,3 @@ module helloworld
go 1.12

replace github.com/mojbro/gocoa => ../../

require github.com/mojbro/gocoa v0.0.0-00010101000000-000000000000 // indirect
5 changes: 3 additions & 2 deletions examples/helloworld/helloworld.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ func main() {
gocoa.OnApplicationDidFinishLaunching(func() {
fmt.Println("App running!")
})
wnd = gocoa.NewWindow(windowTitle, 150, 150, 300, 200)

wnd = gocoa.NewCenteredWindow(windowTitle, 300, 200)
fmt.Printf("created window: %v\n", wnd)
fmt.Printf("screen size %v\n", wnd.GetScreen())
wnd.OnDidMove(func(uwnd *gocoa.Window) {
fmt.Printf("old: %v\nnew: %v\n", wnd, uwnd)
})
Expand Down
30 changes: 30 additions & 0 deletions window.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ type Window struct {
winPtr unsafe.Pointer
}

// Screen the screen of the window.
type Screen struct {
X int
Y int
}

var windows []*Window

// NewWindow constructs and returns a new window.
Expand All @@ -49,6 +55,30 @@ func NewWindow(title string, x int, y int, w int, h int) *Window {
return wnd
}

// NewCenteredWindow constructs and returns a new window.
func NewCenteredWindow(title string, w int, h int) *Window {
windowID := len(windows)
cTitle := C.CString(title)
defer C.free(unsafe.Pointer(cTitle))
wnd := &Window{
title: title,
w: w,
h: h,
callbacks: make(map[WindowEvent]EventHandler),
winPtr: C.Centered_Window_New(C.int(windowID), C.int(w), C.int(h), cTitle)}
wnd.x = int(C.Screen_Center_X(wnd.winPtr))
wnd.y = int(C.Screen_Center_Y(wnd.winPtr))
windows = append(windows, wnd)
return wnd
}

// GetScreen - returns the screen dimensions
func (wnd *Window) GetScreen() *Screen {
return &Screen{
X: int(C.Screen_X(wnd.winPtr)),
Y: int(C.Screen_Y(wnd.winPtr))}
}

// MakeKeyAndOrderFront moves the window to the front of the screen list, within its
// level and it shows the window.
func (wnd *Window) MakeKeyAndOrderFront() {
Expand Down
5 changes: 5 additions & 0 deletions window.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@
#include "progressIndicator.h"

void* Window_New(int goWindowID, int x, int y, int width, int height, const char* title);
void* Centered_Window_New(int goWindowID, int width, int height, const char* title);
int Screen_Center_X(void *wndPtr);
int Screen_Center_Y(void *wndPtr);
int Screen_X(void *wndPtr);
int Screen_Y(void *wndPtr);
void Window_MakeKeyAndOrderFront(void *wndPtr);
void Window_AddButton(void *wndPtr, ButtonPtr btnPtr);
void Window_AddTextView(void *wndPtr, TextViewPtr tvPtr);
Expand Down
46 changes: 46 additions & 0 deletions window.m
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,52 @@
return window;
}

void* Centered_Window_New(int goWindowID, int width, int height, const char* title)
{
NSRect windowRect = NSMakeRect(0, 0, width, height);
id window = [[NSWindow alloc] initWithContentRect:windowRect
styleMask:(NSWindowStyleMaskTitled | NSWindowStyleMaskClosable | NSWindowStyleMaskResizable | NSWindowStyleMaskMiniaturizable)
backing:NSBackingStoreBuffered
defer:NO];
[window setTitle:[NSString stringWithUTF8String:title]];
[window autorelease];
CGFloat xPos = NSWidth([[window screen] frame])/2 - NSWidth([window frame])/2;
CGFloat yPos = NSHeight([[window screen] frame])/2 - NSHeight([window frame])/2;
gocoa_windowDelegate = [[WindowDelegate alloc] init];
[gocoa_windowDelegate setGoWindowID:goWindowID];
[window setDelegate:gocoa_windowDelegate];
[window setFrame:NSMakeRect(xPos, yPos, NSWidth([window frame]), NSHeight([window frame])) display:YES];
return window;
}

int Screen_Center_X(void *wndPtr)
{
NSWindow* window = (NSWindow*)wndPtr;
CGFloat xPos = NSWidth([[window screen] frame])/2 - NSWidth([window frame])/2;
return (int)(xPos);
}

int Screen_Center_Y(void *wndPtr)
{
NSWindow* window = (NSWindow*)wndPtr;
CGFloat yPos = NSHeight([[window screen] frame])/2 - NSHeight([window frame])/2;
return (int)(yPos);
}

int Screen_X(void *wndPtr)
{
NSWindow* window = (NSWindow*)wndPtr;
CGFloat xPos = NSWidth([[window screen] frame]);
return (int)(xPos);
}

int Screen_Y(void *wndPtr)
{
NSWindow* window = (NSWindow*)wndPtr;
CGFloat yPos = NSHeight([[window screen] frame]);
return (int)(yPos);
}

void Window_MakeKeyAndOrderFront(void *wndPtr)
{
NSWindow* window = (NSWindow*)wndPtr;
Expand Down

0 comments on commit 6b4e3a0

Please sign in to comment.