Skip to content

Commit

Permalink
refactor: don't used naked pointers for handle
Browse files Browse the repository at this point in the history
  • Loading branch information
lessp committed Aug 10, 2020
1 parent 8406cce commit b7a2043
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 14 deletions.
2 changes: 2 additions & 0 deletions examples/Examples.re
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,8 @@ let init = app => {
)
|> ignore;

Native.Tray.make() |> Native.Tray.setTitle(~text="Hello Revery!");

let _renderFunction =
UI.start(window, <ExampleHost window initialExample />);
();
Expand Down
4 changes: 4 additions & 0 deletions src/Native/ReveryCocoa.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,16 @@ void *revery_getIconHandle_cocoa();
/* Tray */
void *revery_makeTrayHandleText_cocoa(const char *title_v);
void *revery_makeTrayHandleImage_cocoa(const char *title_v);
void revery_setTrayTitle_cocoa(const char *title_v);

/* Icon progress bar functions */
void revery_setIconProgress_cocoa(void* dt, double progress);
void revery_setIconProgressIndeterminate_cocoa(void *dt);
void revery_hideIconProgress_cocoa(void* ip);

/* Image functions */
void *revery_makeImageFromAbsolutePath(const char *image_path_v);

/* Open functions */
int revery_openURL_cocoa(const char *url_string);
int revery_openFile_cocoa(const char *path_string);
Expand Down
2 changes: 2 additions & 0 deletions src/Native/Tray.re
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
type t;

external make: (~imagePath: string=?, unit) => t = "revery_makeTrayHandle";

external setTitle: (t, ~text: string) => unit = "revery_setTrayTitle";
12 changes: 12 additions & 0 deletions src/Native/Tray.rei
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,15 @@ type t;
* let tray = Tray.make();
*/
let make: (~imagePath: string=?, unit) => t;

/**
* setTitle
*
* Takes a [title] of string and sets the tray's text to it.
* *
* Examples:
* tray |> Tray.setTitle(~text="Hello Revery!");
* Tray.make() |> Tray.setTitle(~text="Hello Revery!");
* Tray.setTitle(tray, ~text="Hello World!");
*/
let setTitle: (t, ~text: string) => unit;
1 change: 1 addition & 0 deletions src/Native/dune
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
notification notification_cocoa
environment
icon icon_cocoa icon_win32
image_cocoa
shell shell_cocoa shell_gtk shell_win32
locale locale_cocoa locale_win32
tray tray_cocoa
Expand Down
16 changes: 16 additions & 0 deletions src/Native/image_cocoa.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#include "config.h"
#ifdef USE_COCOA
#include <stdio.h>

#import <Cocoa/Cocoa.h>

void *revery_makeImageFromAbsolutePath(const char *imagePath) {
NSString *nsImagePath =
[NSString stringWithCString:imagePath encoding:NSUTF8StringEncoding];

NSImage *nsImage = [[NSImage alloc]initWithContentsOfFile:nsImagePath];

return nsImage;
}

#endif
56 changes: 42 additions & 14 deletions src/Native/tray.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,32 +14,60 @@
#include "ReveryWin32.h"
#elif USE_COCOA
#include "ReveryCocoa.h"
#import <Cocoa/Cocoa.h>
#elif USE_GTK
#include "ReveryGtk.h"
#endif

CAMLprim value revery_makeTrayHandle(value imagePath_v) {
CAMLparam1(imagePath_v);
CAMLlocal1(result);

void *ret;
#ifdef USE_COCOA
const char *imagePath = String_val(imagePath_v);

if (imagePath_v == Val_none) {
ret = NULL;
} else {
const char *imagePath = String_val(imagePath_v);
imagePath = String_val(Some_val(imagePath_v));
ret = revery_makeTrayHandleImage_cocoa(imagePath);
const char *imagePath;

NSStatusItem *statusItem = [NSStatusBar.systemStatusBar statusItemWithLength:NSVariableStatusItemLength];

if (imagePath_v != Val_none) {
const char *imagePath = String_val(Some_val(imagePath_v));

NSStatusItem *statusItem = [NSStatusBar.systemStatusBar statusItemWithLength:NSVariableStatusItemLength];

NSImage *nsImage = revery_makeImageFromAbsolutePath(imagePath);

statusItem.button.image = nsImage;
}

result = caml_alloc(1, Abstract_tag);
Store_field(result, 0, (long)statusItem);

UNUSED(imagePath);
CAMLreturn(result);
#elif USE_WIN32
result = caml_alloc(sizeof(NULL), Abstract_tag);
#else
result = caml_alloc(sizeof(NULL), Abstract_tag);
#endif
CAMLreturn(result);
}

void revery_setTrayTitle(value trayHandle_v, value title_v) {
CAMLparam2(trayHandle_v, title_v);
#ifdef USE_COCOA
NSStatusItem* statusItem = (NSStatusItem*)(void*)Field(trayHandle_v, 0);

const char *title = String_val(title_v);

NSString *nsTitle =
[NSString stringWithCString:title encoding:NSUTF8StringEncoding];

statusItem.button.image = NULL;
statusItem.button.title = nsTitle;

CAMLreturn0;
#elif USE_WIN32
/* fprintf(stderr, "WARNING: %s is not implemented on this platform.", __func__); */
ret = NULL;
CAMLreturn0;
#else
/* fprintf(stderr, "WARNING: %s is not implemented on this platform.", __func__); */
ret = NULL;
CAMLreturn0;
#endif
CAMLreturn((value)ret);
}

0 comments on commit b7a2043

Please sign in to comment.