diff --git a/CMakeLists.txt b/CMakeLists.txt index 590d647..ac7015d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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() @@ -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}" ) diff --git a/README.md b/README.md index abce2d2..d0ee502 100644 --- a/README.md +++ b/README.md @@ -20,6 +20,7 @@ Dock.show(); console.log("Hiding the Dock icon on macOS"); Dock.hide(); ``` + ```js import { AboutPanel } from "@nodegui/os-utils"; @@ -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 diff --git a/package-lock.json b/package-lock.json index e2cdbce..ce18254 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "@nodegui/os-utils", - "version": "1.0.2", + "version": "1.1.2", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index 0d3a267..4df7523 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/cpp/Info/info.cpp b/src/cpp/Info/info.cpp new file mode 100644 index 0000000..9d67a17 --- /dev/null +++ b/src/cpp/Info/info.cpp @@ -0,0 +1,5 @@ +#include "info.h" + +bool Info::isDarkMode() { + return false; +} diff --git a/src/cpp/Info/info.h b/src/cpp/Info/info.h new file mode 100644 index 0000000..bac45da --- /dev/null +++ b/src/cpp/Info/info.h @@ -0,0 +1,8 @@ +#pragma once + +namespace Info +{ + bool isDarkMode(); +}; + + diff --git a/src/cpp/Info/info.mm b/src/cpp/Info/info.mm new file mode 100644 index 0000000..e2cafd5 --- /dev/null +++ b/src/cpp/Info/info.mm @@ -0,0 +1,9 @@ +#import +#include "info.h" + +bool Info::isDarkMode() { + NSString *darkModeMode = @"Dark"; + NSString *osxMode = [[NSUserDefaults standardUserDefaults] stringForKey:@"AppleInterfaceStyle"]; + // check here if dark mode; + return [darkModeMode isEqualToString:osxMode]; +} diff --git a/src/cpp/Info/info_wrap.cpp b/src/cpp/Info/info_wrap.cpp new file mode 100644 index 0000000..9efd769 --- /dev/null +++ b/src/cpp/Info/info_wrap.cpp @@ -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(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); +} + + diff --git a/src/cpp/Info/info_wrap.h b/src/cpp/Info/info_wrap.h new file mode 100644 index 0000000..b103b17 --- /dev/null +++ b/src/cpp/Info/info_wrap.h @@ -0,0 +1,17 @@ +#pragma once +#include +#include +#include "info.h" + +class InfoWrap : public Napi::ObjectWrap{ + 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); +} \ No newline at end of file diff --git a/src/cpp/main.cpp b/src/cpp/main.cpp index 8fdc64b..fdce4e6 100644 --- a/src/cpp/main.cpp +++ b/src/cpp/main.cpp @@ -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; } diff --git a/src/demo.tsx b/src/demo.tsx index 2573e39..f6e10f0 100644 --- a/src/demo.tsx +++ b/src/demo.tsx @@ -1,23 +1,22 @@ -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", @@ -25,24 +24,36 @@ const Demo = () => { 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 ; + return ; }; -render(); +render(); diff --git a/src/index.ts b/src/index.ts index 46c1a50..22c38e1 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,2 +1,3 @@ export { Dock } from "./lib/dock"; export { AboutPanel } from "./lib/aboutPanel"; +export { Info } from "./lib/info"; diff --git a/src/lib/info.ts b/src/lib/info.ts new file mode 100644 index 0000000..ad0f645 --- /dev/null +++ b/src/lib/info.ts @@ -0,0 +1,7 @@ +import addon from "./utils/addon"; + +export class Info { + static isDarkMode(): boolean { + return addon.Info.isDarkMode(); + } +}