Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(environment): macOS version #1030

Merged
merged 4 commits into from
Dec 25, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions examples/Examples.re
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 373,8 @@ let init = app => {
Window.onMoved(window, ((x, y)) =>
Console.log(Printf.sprintf("Moved: %d x %d", x, y))
);

Console.log(Printf.sprintf("Operating system: %s", Environment.osString));

let _renderFunction =
UI.start(window, <ExampleHost window initialExample />);
Expand Down
40 changes: 20 additions & 20 deletions src/Core/Environment.re
Original file line number Diff line number Diff line change
Expand Up @@ -16,29 16,29 @@ let sleep = (t: Time.t) =>
external yield: unit => unit = "caml_thread_yield";

type os =
| Android
| IOS
| Linux
| Mac
| Windows
| Browser
| Unknown;
Revery_Native.Environment.os =
| Unknown
| Android
| IOS
| Linux
| Windows
| Browser
| Mac(int, int, int);

let os = {
webGL
? Browser
: (
switch (Revery_Native.Environment.get_os()) {
| `Android => Android
| `IOS => IOS
| `Linux => Linux
| `Mac => Mac
| `Windows => Windows
| _ => Unknown
}
);
webGL ? Browser : Revery_Native.Environment.getOS();
};

let osString = switch (os) {
| Mac(major, minor, bugfix) => Printf.sprintf("macOS %d.%d.%d", major, minor, bugfix)
| Windows => "Windows"
| Linux => "Linux"
| Android => "Android"
| Browser => "Browser"
| IOS => "iOS"
| Unknown => "Unknown"
};

module Internal = {
let addTrailingSlash = dir => {
let len = String.length(dir);
Expand All @@ -65,7 65,7 @@ let getExecutingDirectory = () =>
* the symlink destination - this causes problems when trying to load assets
* relative to the binary location when symlinked.
*/
| Mac =>
| Mac(_) =>
switch (
String.rindex_opt(Sys.executable_name, '/'),
String.rindex_opt(Sys.executable_name, '\\'),
Expand Down
5 changes: 3 additions & 2 deletions src/Core/Environment.rei
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 47,16 @@ let getAssetPath: string => string;
let getTempDirectory: unit => string;

type os =
| Unknown
| Android
| IOS
| Linux
| Mac
| Windows
| Browser
| Unknown;
| Mac(int, int, int);

let os: os;
let osString: string;

/**
[getUserLocale] returns the current user locale. Note that on some platforms
Expand Down
4 changes: 2 additions & 2 deletions src/Core/Window.re
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 68,7 @@ module WindowMetrics: {
// Mac and iOS is easy... there isn't any scaling factor. The window is automatically
// proportioned for us. The scaling is handled by the ratio of size / framebufferSize.
| IOS
| Mac => 1.0
| Mac(_) => 1.0
// On Windows, we need to try a Win32 API to get the scale factor
| Windows =>
let scale = Sdl2.Window.getWin32ScaleFactor(sdlWindow);
Expand Down Expand Up @@ -238,7 238,7 @@ type t = {
module Internal = {
let setTitlebarStyle = (w: Sdl2.Window.t, style: WindowStyles.titlebar) => {
switch (Environment.os) {
| Mac =>
| Mac(_) =>
switch (style) {
| Transparent => Sdl2.Window.setMacTitlebarTransparent(w)
| Hidden => Sdl2.Window.setMacTitlebarHidden(w)
Expand Down
6 changes: 3 additions & 3 deletions src/Font/FontFamily.re
Original file line number Diff line number Diff line change
Expand Up @@ -82,20 82,20 @@ let fromFile = (fileName, ~italic as _, _) => {
let default =
switch (Revery_Core.Environment.os) {
| Linux => system("Liberation Sans")
| Mac => system("System Font")
| Mac(_) => system("System Font")
| _ => system("Arial")
};

let defaultMono =
switch (Revery_Core.Environment.os) {
| Mac => system("Menlo")
| Mac(_) => system("Menlo")
| Windows => system("Consolas")
| _ => fromFile("Inconsolata.otf")
};

let defaultSerif =
switch (Revery_Core.Environment.os) {
| Mac => system("Palatino")
| Mac(_) => system("Palatino")
| Linux => system("Liberation Serif")
| _ => system("Times New Roman")
};
Expand Down
23 changes: 13 additions & 10 deletions src/Native/Environment.re
Original file line number Diff line number Diff line change
@@ -1,10 1,13 @@
external get_os: unit => string = "revery_getOperatingSystem";
let get_os = () =>
switch (get_os()) {
| "android" => `Android
| "ios" => `IOS
| "linux" => `Linux
| "mac" => `Mac
| "windows" => `Windows
| _ => `Unknown
};
/* If you change these you *must* keep environment.c up to date. */
type os =
/* Int values */
| Unknown // 0
| Android // 1
| IOS // 2
| Linux // 3
| Windows // 4
| Browser // 5
/* Block values */
| Mac(int, int, int); // 0

external getOS: unit => os = "revery_getOperatingSystem";
1 change: 1 addition & 0 deletions src/Native/ReveryCocoa.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 39,4 @@ void revery_menuRemoveItem_cocoa(void *nsMenu, void *nsMenuItem);
void revery_menuInsertItemAt_cocoa(void *nsMenu, void *nsMenuItem, int idx);
void revery_menuInsertSubmenuAt_cocoa(void *parent, void *child, int idx);
void revery_menuClear_cocoa(void *nsMenu);

4 changes: 4 additions & 0 deletions src/Native/ReveryMac.h
Original file line number Diff line number Diff line change
@@ -0,0 1,4 @@
#pragma once

/* Environment functions */
void getOperatingSystemVersion_mac(int *major, int *minor, int *bugfix);
6 changes: 6 additions & 0 deletions src/Native/config/discover.re
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 68,7 @@ let gen_config_header = (conf, features) => {
| Windows => "windows"
};
};
let is_os = os => get_os(conf) == os ? Value.Int(1) : Value.Switch(false);

gen_header_file(
conf,
Expand All @@ -77,6 78,11 @@ let gen_config_header = (conf, features) => {
("USE_GTK", includes(GTK)),
("USE_UIKIT", includes(UIKIT)),
("USE_WIN32", includes(WIN32)),
("IS_IOS", is_os(IOS)),
("IS_MACOS", is_os(Mac)),
("IS_ANDROID", is_os(Android)),
("IS_LINUX", is_os(Linux)),
("IS_WINDOWS", is_os(Windows)),
],
);
};
Expand Down
2 changes: 1 addition & 1 deletion src/Native/dune
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 9,7 @@
Revery_Native
dialog dialog_cocoa dialog_win32 dialog_gtk
notification notification_cocoa
environment
environment environment_mac
icon icon_cocoa icon_win32
shell shell_cocoa shell_gtk shell_win32
locale locale_cocoa locale_win32
Expand Down
25 changes: 24 additions & 1 deletion src/Native/environment.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 11,31 @@
#include <caml/mlvalues.h>

#include "config.h"
#ifdef IS_MACOS
#import "ReveryMac.h"
#endif

CAMLprim value revery_getOperatingSystem() {
CAMLparam0();
CAMLreturn(caml_copy_string(PLATFORM_NAME));
CAMLlocal1(vOS);
#ifdef IS_ANDROID
vOS = Val_int(1);
#elif IS_IOS
vOS = Val_int(2);
#elif IS_LINUX
vOS = Val_int(3);
#elif IS_WINDOWS
vOS = Val_int(4);
#elif IS_MACOS
int major, minor, bugfix;
getOperatingSystemVersion_mac(&major, &minor, &bugfix);
vOS = caml_alloc(3, 0);
Store_field(vOS, 0, Val_int(major));
Store_field(vOS, 1, Val_int(minor));
Store_field(vOS, 2, Val_int(bugfix));
#else
vOS = Val_int(0);
#endif

CAMLreturn(vOS);
}
12 changes: 12 additions & 0 deletions src/Native/environment_mac.c
Original file line number Diff line number Diff line change
@@ -0,0 1,12 @@
#include "config.h"
#ifdef IS_MACOS
#import <Foundation/Foundation.h>

void getOperatingSystemVersion_mac(int *major, int *minor, int *bugfix) {
NSOperatingSystemVersion nsOSVersion = [[NSProcessInfo processInfo] operatingSystemVersion];
*major = nsOSVersion.majorVersion;
*minor = nsOSVersion.minorVersion;
*bugfix = nsOSVersion.patchVersion;
}

#endif
6 changes: 5 additions & 1 deletion src/UI_Components/Input.re
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 320,11 @@ let%component make =
onKeyDown(event);

let code = event.keycode;
let mac = Environment.os === Mac;
let mac =
switch (Environment.os) {
| Mac(_) => true
| _ => false
};
let super = Sdl2.Keymod.isGuiDown(event.keymod);
let ctrl = Sdl2.Keymod.isControlDown(event.keymod);

Expand Down
6 changes: 5 additions & 1 deletion src/UI_Components/ScrollView.re
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 13,11 @@ let empty = React.empty;

let scrollTrackColor = Color.rgba(0.0, 0.0, 0.0, 0.4);
let scrollThumbColor = Color.rgba(0.5, 0.5, 0.5, 0.4);
let isMac = Environment.os === Mac;
let isMac =
switch (Environment.os) {
| Mac(_) => true
| _ => false
};

type action =
| ScrollUpdated(int);
Expand Down