Skip to content

Commit

Permalink
Added isDark Mode
Browse files Browse the repository at this point in the history
  • Loading branch information
a7ul committed Nov 27, 2019
1 parent d6c3cfe commit 53e19ef
Show file tree
Hide file tree
Showing 13 changed files with 131 additions and 27 deletions.
3 changes: 3 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@ if(APPLE)
set(OS_SPECIFIC_SRC
"${PROJECT_SOURCE_DIR}/src/cpp/Dock/dock.mm"
"${PROJECT_SOURCE_DIR}/src/cpp/AboutPanel/about_panel.mm"
"${PROJECT_SOURCE_DIR}/src/cpp/Info/info.mm"
)
else()
set(OS_SPECIFIC_SRC
"${PROJECT_SOURCE_DIR}/src/cpp/Dock/dock.cpp"
"${PROJECT_SOURCE_DIR}/src/cpp/AboutPanel/about_panel.cpp"
"${PROJECT_SOURCE_DIR}/src/cpp/Info/info.cpp"
)
endif()

Expand All @@ -24,6 +26,7 @@ add_library(${PLUGIN_ADDON_NAME} SHARED
"${PROJECT_SOURCE_DIR}/src/cpp/main.cpp"
"${PROJECT_SOURCE_DIR}/src/cpp/Dock/dock_wrap.cpp"
"${PROJECT_SOURCE_DIR}/src/cpp/AboutPanel/about_panel_wrap.cpp"
"${PROJECT_SOURCE_DIR}/src/cpp/Info/info_wrap.cpp"
"${OS_SPECIFIC_SRC}"
)

Expand Down
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ Dock.show();
console.log("Hiding the Dock icon on macOS");
Dock.hide();
```

```js
import { AboutPanel } from "@nodegui/os-utils";

Expand All @@ -29,17 +30,28 @@ console.log("Open the about panel on macOS with modified defaults");
AboutPanel.open({ name: "Custom name", version: "2.3.5" });
```

```js
import { Info } from "@nodegui/os-utils";

console.log("isDarkMode: " + Info.isDarkMode());
```

## Supported APIs

### macOS

- Dock

- `show()` - static method - Shows the Dock icon on macOS. Does nothing on other platforms.
- `hide()` - static method - Hides the Dock icon on macOS. Does nothing on other platforms.

- AboutPanel

- `open(options?: object)` - static method - Opens the about panel on macOS. Does nothing on other platforms. `options` is optional and can contain the following strings: `name`, `version`, `applicationVersion`, and `copyright`. The default values are derived from `Info.plist`.

- Info
- `isDarkMode` - static method - Checks if the OS theme is dark mode in macOS. Does nothing on other platforms and returns false. Returns a boolean.

## License

MIT
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@nodegui/os-utils",
"version": "1.0.2",
"version": "1.1.2",
"description": "A helper module which contains OS specific native features.",
"main": "dist/index.js",
"typings": "dist/index.d.ts",
Expand Down
5 changes: 5 additions & 0 deletions src/cpp/Info/info.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#include "info.h"

bool Info::isDarkMode() {
return false;
}
8 changes: 8 additions & 0 deletions src/cpp/Info/info.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#pragma once

namespace Info
{
bool isDarkMode();
};


9 changes: 9 additions & 0 deletions src/cpp/Info/info.mm
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#import <AppKit/AppKit.h>
#include "info.h"

bool Info::isDarkMode() {
NSString *darkModeMode = @"Dark";
NSString *osxMode = [[NSUserDefaults standardUserDefaults] stringForKey:@"AppleInterfaceStyle"];
// check here if dark mode;
return [darkModeMode isEqualToString:osxMode];
}
29 changes: 29 additions & 0 deletions src/cpp/Info/info_wrap.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#include "info_wrap.h"

Napi::FunctionReference InfoWrap::constructor;

Napi::Object InfoWrap::init(Napi::Env env, Napi::Object exports) {
Napi::HandleScope scope(env);
char CLASSNAME[] = "Info";
Napi::Function func = DefineClass(env, CLASSNAME, {
StaticMethod("isDarkMode", &InfoWrapStatic::isDarkMode),
});
constructor = Napi::Persistent(func);
exports.Set(CLASSNAME, func);
return exports;
}


InfoWrap::InfoWrap(const Napi::CallbackInfo& info): Napi::ObjectWrap<InfoWrap>(info) {
Napi::Env env = info.Env();
Napi::HandleScope scope(env);
}

Napi::Value InfoWrapStatic::isDarkMode(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
Napi::HandleScope scope(env);
bool isDarkMode = Info::isDarkMode();
return Napi::Value::From(env, isDarkMode);
}


17 changes: 17 additions & 0 deletions src/cpp/Info/info_wrap.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#pragma once
#include <napi.h>
#include <stdlib.h>
#include "info.h"

class InfoWrap : public Napi::ObjectWrap<InfoWrap>{
public:
static Napi::Object init(Napi::Env env, Napi::Object exports);
InfoWrap(const Napi::CallbackInfo& info);
~InfoWrap();
static Napi::FunctionReference constructor;
};


namespace InfoWrapStatic {
Napi::Value isDarkMode(const Napi::CallbackInfo& info);
}
2 changes: 2 additions & 0 deletions src/cpp/main.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
#include "src/cpp/Dock/dock_wrap.h"
#include "src/cpp/AboutPanel/about_panel_wrap.h"
#include "src/cpp/Info/info_wrap.h"

Napi::Object Main(Napi::Env env, Napi::Object exports) {
DockWrap::init(env, exports);
AboutPanelWrap::init(env, exports);
InfoWrap::init(env, exports);
return exports;
}

Expand Down
61 changes: 36 additions & 25 deletions src/demo.tsx
Original file line number Diff line number Diff line change
@@ -1,48 +1,59 @@
import React from 'react';
import {render} from 'ink';
import SelectInput from 'ink-select-input';
import React from "react";
import { render } from "ink";
import SelectInput from "ink-select-input";

import { AboutPanel } from "./lib/aboutPanel";
import { Dock } from "./lib/dock";
import { Info, Dock, AboutPanel } from "./index";

const Demo = () => {
const handleSelect = (item: any) => {
switch(item.value) {
case 'hideDock':
switch (item.value) {
case "hideDock":
Dock.hide();
break;
case 'showDock':
case "showDock":
Dock.show();
break;
case 'openDefaultAbout':
case "openDefaultAbout":
AboutPanel.open();
break;
case 'openCustomAbout':
case "openCustomAbout":
AboutPanel.open({
name: "NodeGUI OS utils",
copyright: "Copyright © NodeGUI 2019",
version: "78.0",
applicationVersion: "78.0.3904.70"
});
break;
case "isDarkMode":
console.log(Info.isDarkMode());
break;
}
};

const items = [{
label: 'Hide dock icon',
value: 'hideDock'
}, {
label: 'Show dock icon',
value: 'showDock'
}, {
label: 'Open default about window',
value: 'openDefaultAbout'
}, {
label: 'Open custom about window',
value: 'openCustomAbout'
}];
const items = [
{
label: "Hide dock icon",
value: "hideDock"
},
{
label: "Show dock icon",
value: "showDock"
},
{
label: "Open default about window",
value: "openDefaultAbout"
},
{
label: "Open custom about window",
value: "openCustomAbout"
},
{
label: "Check if dark mode",
value: "isDarkMode"
}
];

return <SelectInput items={items} onSelect={handleSelect}/>;
return <SelectInput items={items} onSelect={handleSelect} />;
};

render(<Demo/>);
render(<Demo />);
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export { Dock } from "./lib/dock";
export { AboutPanel } from "./lib/aboutPanel";
export { Info } from "./lib/info";
7 changes: 7 additions & 0 deletions src/lib/info.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import addon from "./utils/addon";

export class Info {
static isDarkMode(): boolean {
return addon.Info.isDarkMode();
}
}

0 comments on commit 53e19ef

Please sign in to comment.