Skip to content

Commit

Permalink
add screen API
Browse files Browse the repository at this point in the history
  • Loading branch information
alexflint committed Nov 14, 2016
1 parent bb59ad8 commit e7c0ab9
Show file tree
Hide file tree
Showing 9 changed files with 190 additions and 43 deletions.
23 changes: 5 additions & 18 deletions app.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,14 +107,6 @@ type App struct {
ready chan struct{}
}

// Rect represents a rectangular region on the screen
type Rect struct {
Width int // Width in pixels
Height int // Height in pixels
Left int // Left is offset from left in pixel
Top int // Left is offset from top in pixels
}

// WindowOptions contains options for creating windows
type WindowOptions struct {
Title string // String to display in title bar
Expand All @@ -135,7 +127,7 @@ var FramedWindow = WindowOptions{
Width: 800,
Height: 600,
Left: 100,
Top: 100,
Bottom: 100,
},
TitleBar: true,
Frame: true,
Expand Down Expand Up @@ -169,7 +161,7 @@ var FramelessWindow = WindowOptions{
Width: 800,
Height: 600,
Left: 100,
Top: 100,
Bottom: 100,
},
Resizable: true,
}
Expand All @@ -194,7 +186,7 @@ func (app *App) OpenWindow(url string, opt WindowOptions) (*Window, error) {
C.int(opt.Shape.Width),
C.int(opt.Shape.Height),
C.int(opt.Shape.Left),
C.int(opt.Shape.Top),
C.int(opt.Shape.Bottom),
C.bool(opt.TitleBar),
C.bool(opt.Frame),
C.bool(opt.Resizable),
Expand All @@ -210,17 +202,12 @@ func (app *App) OpenWindow(url string, opt WindowOptions) (*Window, error) {

// Shape gets the current shape of the window.
func (w *Window) Shape() Rect {
return Rect{
Width: int(C.GalliumWindowGetWidth(w.c)),
Height: int(C.GalliumWindowGetHeight(w.c)),
Left: int(C.GalliumWindowGetLeft(w.c)),
Top: int(C.GalliumWindowGetTop(w.c)),
}
return rectFromC(C.GalliumWindowGetShape(w.c))
}

// Shape gets the current shape of the window.
func (w *Window) SetShape(r Rect) {
C.GalliumWindowSetShape(w.c, C.int(r.Width), C.int(r.Height), C.int(r.Left), C.int(r.Top))
C.GalliumWindowSetShape(w.c, C.int(r.Width), C.int(r.Height), C.int(r.Left), C.int(r.Bottom))
}

// URL gets the URL that the window is currently at.
Expand Down
4 changes: 2 additions & 2 deletions dist/Gallium.framework/Versions/A/Gallium
Git LFS file not shown
27 changes: 7 additions & 20 deletions dist/include/gallium/browser.h
Original file line number Diff line number Diff line change
@@ -1,19 +1,15 @@
#ifndef GALLIUM_API_GALLIUM_H_
#define GALLIUM_API_GALLIUM_H_
#ifndef GALLIUM_BROWSER_H_
#define GALLIUM_BROWSER_H_

#include <stdbool.h>

#include "gallium/export.h"
#include "gallium/rect.h"

#ifdef __cplusplus
extern "C" {
#endif

#define GALLIUM_EXPORT __attribute__ ((visibility ("default")))

// gallium_error represents an error
typedef struct GALLIUM_EXPORT gallium_error {
const char* msg;
} gallium_error_t;

// gallium_window represents a window
typedef struct GALLIUM_EXPORT gallium_window gallium_window_t;

Expand All @@ -40,16 +36,7 @@ GALLIUM_EXPORT gallium_window_t* GalliumOpenWindow(const char* url,


// GalliumWindowGetWidth gets the width of a window
GALLIUM_EXPORT int GalliumWindowGetWidth(gallium_window_t* window);

// GalliumWindowGetWidth gets the width of a window
GALLIUM_EXPORT int GalliumWindowGetHeight(gallium_window_t* window);

// GalliumWindowGetWidth gets the width of a window
GALLIUM_EXPORT int GalliumWindowGetLeft(gallium_window_t* window);

// GalliumWindowGetWidth gets the width of a window
GALLIUM_EXPORT int GalliumWindowGetTop(gallium_window_t* window);
GALLIUM_EXPORT gallium_rect_t GalliumWindowGetShape(gallium_window_t* window);

// GalliumWindowGetWidth gets the width of a window
GALLIUM_EXPORT void GalliumWindowSetShape(gallium_window_t* window,
Expand Down Expand Up @@ -128,4 +115,4 @@ GALLIUM_EXPORT void* GalliumWindowNativeContent(gallium_window_t* window);
}
#endif

#endif
#endif // ifndef GALLIUM_BROWSER_H_
7 changes: 4 additions & 3 deletions dist/include/gallium/cocoa.h
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
#ifndef GALLIUM_API_COCOA_H_
#define GALLIUM_API_COCOA_H_
#ifndef GALLIUM_COCOA_H_
#define GALLIUM_COCOA_H_

#include <stdbool.h>
#include <stdint.h>

#include "gallium/export.h"
#include "gallium/browser.h"

#ifdef __cplusplus
Expand Down Expand Up @@ -81,4 +82,4 @@ GALLIUM_EXPORT char* MainBundle_ObjectForKey(const char* key);
}
#endif

#endif
#endif // ifndef GALLIUM_COCOA_H_
23 changes: 23 additions & 0 deletions dist/include/gallium/export.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#ifndef GALLIUM_EXPORT_H_
#define GALLIUM_EXPORT_H_

#include <stdbool.h>
#include <stdint.h>

#ifdef __cplusplus
extern "C" {
#endif

#define GALLIUM_EXPORT __attribute__ ((visibility ("default")))

// gallium_error represents an error
typedef struct GALLIUM_EXPORT gallium_error {
const char* msg;
} gallium_error_t;

#ifdef __cplusplus
}
#endif

#endif // ifndef GALLIUM_EXPORT_H_

22 changes: 22 additions & 0 deletions dist/include/gallium/rect.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#ifndef GALLIUM_RECT_H_
#define GALLIUM_RECT_H_

#include <stdbool.h>
#include <stdint.h>

#ifdef __cplusplus
extern "C" {
#endif

typedef struct GALLIUM_EXPORT {
int width;
int height;
int left;
int bottom;
} gallium_rect_t;

#ifdef __cplusplus
}
#endif

#endif // ifndef GALLIUM_RECT_H_
41 changes: 41 additions & 0 deletions dist/include/gallium/screen.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#ifndef GALLIUM_SCREEN_H_
#define GALLIUM_SCREEN_H_

#include <stdbool.h>
#include <stdint.h>

#include "gallium/export.h"
#include "gallium/rect.h"

#ifdef __cplusplus
extern "C" {
#endif

typedef struct GALLIUM_EXPORT gallium_screen gallium_screen_t;

// GalliumScreenCount gets the number of screens
GALLIUM_EXPORT int GalliumScreenCount();

// GalliumScreen gets the i-th screen
GALLIUM_EXPORT gallium_screen_t* GalliumScreen(int index);

// GalliumFocussedScreen gets the screen containing the currently focussed window.
GALLIUM_EXPORT gallium_screen_t* GalliumFocussedScreen();

// GalliumScreenShape gets the shape of a screen
GALLIUM_EXPORT gallium_rect_t GalliumScreenShape(gallium_screen_t*);

// GalliumScreenShape gets the usable area of a screen (excludes dock and menubar)
GALLIUM_EXPORT gallium_rect_t GalliumScreenUsable(gallium_screen_t*);

// GalliumScreenBitsPerPixel gets the bits per pixels for a screen
GALLIUM_EXPORT int GalliumScreenBitsPerPixel(gallium_screen_t*);

// GalliumScreenID gets the unique ID for a screen
GALLIUM_EXPORT int GalliumScreenID(gallium_screen_t*);

#ifdef __cplusplus
}
#endif

#endif // ifndef GALLIUM_SCREEN_H_
28 changes: 28 additions & 0 deletions rect.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package gallium

/*
#cgo CFLAGS: -mmacosx-version-min=10.8
#cgo CFLAGS: -DGALLIUM_DIR=${SRCDIR}
#cgo CFLAGS: -Idist/include
#include <stdlib.h>
#include "gallium/rect.h"
*/
import "C"

// Rect represents a rectangular region on the screen
type Rect struct {
Width int // Width in pixels
Height int // Height in pixels
Left int // Left is offset from left in pixel
Bottom int // Left is offset from top in pixels
}

func rectFromC(c C.gallium_rect_t) Rect {
return Rect{
Width: int(c.width),
Height: int(c.height),
Left: int(c.left),
Bottom: int(c.bottom),
}
}
58 changes: 58 additions & 0 deletions screen.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package gallium

/*
#cgo CFLAGS: -mmacosx-version-min=10.8
#cgo CFLAGS: -DGALLIUM_DIR=${SRCDIR}
#cgo CFLAGS: -Idist/include
#include <stdlib.h>
#include "gallium/screen.h"
*/
import "C"
import "fmt"

// A screen represents a physical rectangular display. "Device coordinates"
// means a position on a screen measured from (0, 0) at the bottom left of
// the device. "Global coordinates" means the coordinate system in which
// each of the screens are positioned relative to each other. Global and
// device coordinates almost always have the same scale factor. It is
// possible for screens to overlap in global coordinates (such as when
// mirroring a display.)
type Screen struct {
Shape Rect // the size and position of this screen in global coords
Usable Rect // excludes the menubar and dock
BitsPerPixel int // color depth of this screen (total of all color components)
ID int // unique identifier for this screen
}

func screenFromC(c *C.gallium_screen_t) Screen {
return Screen{
Shape: rectFromC(C.GalliumScreenShape(c)),
Usable: rectFromC(C.GalliumScreenUsable(c)),
BitsPerPixel: int(C.GalliumScreenBitsPerPixel(c)),
ID: int(C.GalliumScreenID(c)),
}
}

// Screens gets a list of available screens
func Screens() []Screen {
var screens []Screen
n := int(C.GalliumScreenCount())
for i := 0; i < n; i++ {
c := C.GalliumScreen(C.int(i))
if c == nil {
panic(fmt.Sprintf("GalliumScreen returned nil for index %d", i))
}
screens = append(screens, screenFromC(c))
}
return screens
}

// FocussedScreen gets the screen containing the currently focussed window
func FocussedScreen() Screen {
c := C.GalliumFocussedScreen()
if c == nil {
panic("GalliumFocussedScreen returned nil")
}
return screenFromC(c)
}

0 comments on commit e7c0ab9

Please sign in to comment.