-
Notifications
You must be signed in to change notification settings - Fork 99
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add preliminary Windows implementation
- Loading branch information
Showing
12 changed files
with
310 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
incubator/@react-native-webapis/battery-status/windows/PropertySheet.props
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<Project ToolsVersion="Current" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> | ||
<ImportGroup Label="PropertySheets" /> | ||
<PropertyGroup Label="UserMacros" /> | ||
<!-- | ||
To customize common C++/WinRT project properties: | ||
* right-click the project node | ||
* expand the Common Properties item | ||
* select the C++/WinRT property page | ||
For more advanced scenarios, and complete documentation, please see: | ||
https://github.com/Microsoft/xlang/tree/master/src/package/cppwinrt/nuget | ||
--> | ||
<PropertyGroup /> | ||
<ItemDefinitionGroup /> | ||
</Project> |
59 changes: 59 additions & 0 deletions
59
incubator/@react-native-webapis/battery-status/windows/RNWBatteryStatus.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
#pragma once | ||
|
||
#include <winrt/Windows.Devices.Power.h> | ||
|
||
#include "JSValue.h" | ||
#include "NativeModules.h" | ||
|
||
namespace winrt::ReactNativeBatteryStatus | ||
{ | ||
using winrt::Windows::Devices::Power::Battery; | ||
using winrt::Windows::System::Power::BatteryStatus; | ||
using winrt::Microsoft::ReactNative::JSValue; | ||
using winrt::Microsoft::ReactNative::JSValueObject; | ||
using winrt::Microsoft::ReactNative::ReactContext; | ||
using winrt::Microsoft::ReactNative::ReactPromise; | ||
|
||
REACT_MODULE(RNWBatteryStatus) | ||
struct RNWBatteryStatus | ||
{ | ||
REACT_INIT(Initialize) | ||
void Initialize(ReactContext const &reactContext) noexcept | ||
{ | ||
m_reactContext = reactContext; | ||
} | ||
|
||
REACT_METHOD(GetStatus, L"getStatus") | ||
void GetStatus(ReactPromise<JSValue> promise) noexcept | ||
{ | ||
JSValueObject status; | ||
|
||
// https://learn.microsoft.com/en-us/windows/uwp/devices-sensors/get-battery-info | ||
auto report = Battery::AggregateBattery().GetReport(); | ||
auto const batteryStatus = report.Status(); | ||
if (batteryStatus == BatteryStatus::NotPresent) { | ||
// These are values reported by Chrome/Edge on a desktop machine | ||
status["charging"] = true; | ||
status["chargingTime"] = 0; | ||
status["dischargingTime"] = -1; | ||
status["level"] = 1; | ||
} else { | ||
status["charging"] = batteryStatus == BatteryStatus::Charging; | ||
|
||
// TODO: Actually implement charging/discharging times | ||
status["chargingTime"] = batteryStatus == BatteryStatus::Charging ? -1 : 0; | ||
status["dischargingTime"] = batteryStatus == BatteryStatus::Discharging ? -1 : -1; | ||
|
||
auto const remaining = report.RemainingCapacityInMilliwattHours().GetInt32(); | ||
auto const fullCharge = report.FullChargeCapacityInMilliwattHours().GetInt32(); | ||
status["level"] = static_cast<float>(remaining) / static_cast<float>(fullCharge); | ||
} | ||
|
||
promise.Resolve(JSValue{std::move(status)}); | ||
} | ||
|
||
private: | ||
ReactContext m_reactContext{nullptr}; | ||
}; | ||
|
||
} // namespace winrt::ReactNativeBatteryStatus |
3 changes: 3 additions & 0 deletions
3
incubator/@react-native-webapis/battery-status/windows/ReactNativeBatteryStatus.def
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
EXPORTS | ||
DllCanUnloadNow = WINRT_CanUnloadNow PRIVATE | ||
DllGetActivationFactory = WINRT_GetActivationFactory PRIVATE |
142 changes: 142 additions & 0 deletions
142
incubator/@react-native-webapis/battery-status/windows/ReactNativeBatteryStatus.vcxproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<Project ToolsVersion="Current" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> | ||
<Import Project="$(SolutionDir)\ExperimentalFeatures.props" Condition="Exists('$(SolutionDir)\ExperimentalFeatures.props')" /> | ||
<PropertyGroup Label="Globals"> | ||
<CppWinRTOptimized>true</CppWinRTOptimized> | ||
<CppWinRTRootNamespaceAutoMerge>true</CppWinRTRootNamespaceAutoMerge> | ||
<MinimalCoreWin>true</MinimalCoreWin> | ||
<ProjectGuid>{e9b31ce3-6931-442e-bb6b-0ccf2943b6bd}</ProjectGuid> | ||
<ProjectName>ReactNativeBatteryStatus</ProjectName> | ||
<RootNamespace>ReactNativeBatteryStatus</RootNamespace> | ||
<DefaultLanguage>en-US</DefaultLanguage> | ||
<MinimumVisualStudioVersion>17.0</MinimumVisualStudioVersion> | ||
<AppContainerApplication>true</AppContainerApplication> | ||
<ApplicationType>Windows Store</ApplicationType> | ||
<ApplicationTypeRevision>10.0</ApplicationTypeRevision> | ||
</PropertyGroup> | ||
<PropertyGroup Label="ReactNativeWindowsProps"> | ||
<ReactNativeWindowsDir Condition="'$(ReactNativeWindowsDir)' == ''">$([MSBuild]::GetDirectoryNameOfFileAbove($(SolutionDir), 'node_modules\react-native-windows\package.json'))\node_modules\react-native-windows\</ReactNativeWindowsDir> | ||
</PropertyGroup> | ||
<Import Project="$(ReactNativeWindowsDir)\PropertySheets\External\Microsoft.ReactNative.WindowsSdk.Default.props" Condition="Exists('$(ReactNativeWindowsDir)\PropertySheets\External\Microsoft.ReactNative.WindowsSdk.Default.props')" /> | ||
<PropertyGroup Label="Fallback Windows SDK Versions"> | ||
<WindowsTargetPlatformVersion Condition=" '$(WindowsTargetPlatformVersion)' == '' ">10.0.19041.0</WindowsTargetPlatformVersion> | ||
<WindowsTargetPlatformMinVersion Condition=" '$(WindowsTargetPlatformMinVersion)' == '' ">10.0.17763.0</WindowsTargetPlatformMinVersion> | ||
</PropertyGroup> | ||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" /> | ||
<ItemGroup Label="ProjectConfigurations"> | ||
<ProjectConfiguration Include="Debug|ARM64"> | ||
<Configuration>Debug</Configuration> | ||
<Platform>ARM64</Platform> | ||
</ProjectConfiguration> | ||
<ProjectConfiguration Include="Debug|Win32"> | ||
<Configuration>Debug</Configuration> | ||
<Platform>Win32</Platform> | ||
</ProjectConfiguration> | ||
<ProjectConfiguration Include="Debug|x64"> | ||
<Configuration>Debug</Configuration> | ||
<Platform>x64</Platform> | ||
</ProjectConfiguration> | ||
<ProjectConfiguration Include="Release|ARM64"> | ||
<Configuration>Release</Configuration> | ||
<Platform>ARM64</Platform> | ||
</ProjectConfiguration> | ||
<ProjectConfiguration Include="Release|Win32"> | ||
<Configuration>Release</Configuration> | ||
<Platform>Win32</Platform> | ||
</ProjectConfiguration> | ||
<ProjectConfiguration Include="Release|x64"> | ||
<Configuration>Release</Configuration> | ||
<Platform>x64</Platform> | ||
</ProjectConfiguration> | ||
</ItemGroup> | ||
<PropertyGroup Label="Configuration"> | ||
<ConfigurationType>DynamicLibrary</ConfigurationType> | ||
<CharacterSet>Unicode</CharacterSet> | ||
<GenerateManifest>false</GenerateManifest> | ||
</PropertyGroup> | ||
<PropertyGroup Condition="'$(Configuration)'=='Debug'" Label="Configuration"> | ||
<UseDebugLibraries>true</UseDebugLibraries> | ||
<LinkIncremental>true</LinkIncremental> | ||
</PropertyGroup> | ||
<PropertyGroup Condition="'$(Configuration)'=='Release'" Label="Configuration"> | ||
<UseDebugLibraries>false</UseDebugLibraries> | ||
<WholeProgramOptimization>true</WholeProgramOptimization> | ||
<LinkIncremental>false</LinkIncremental> | ||
</PropertyGroup> | ||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" /> | ||
<ImportGroup Label="ExtensionSettings"></ImportGroup> | ||
<ImportGroup Label="PropertySheets"> | ||
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" /> | ||
</ImportGroup> | ||
<ImportGroup Label="PropertySheets"> | ||
<Import Project="PropertySheet.props" /> | ||
</ImportGroup> | ||
<ImportGroup Label="ReactNativeWindowsPropertySheets"> | ||
<Import Project="$(ReactNativeWindowsDir)\PropertySheets\external\Microsoft.ReactNative.Uwp.CppLib.props" Condition="Exists('$(ReactNativeWindowsDir)\PropertySheets\External\Microsoft.ReactNative.Uwp.CppLib.props')" /> | ||
</ImportGroup> | ||
<PropertyGroup Label="UserMacros" /> | ||
<ItemDefinitionGroup> | ||
<ClCompile> | ||
<PrecompiledHeader>Use</PrecompiledHeader> | ||
<PrecompiledHeaderFile>pch.h</PrecompiledHeaderFile> | ||
<PrecompiledHeaderOutputFile>$(IntDir)pch.pch</PrecompiledHeaderOutputFile> | ||
<WarningLevel>Level4</WarningLevel> | ||
<AdditionalOptions>%(AdditionalOptions) /bigobj</AdditionalOptions> | ||
<DisableSpecificWarnings>4453;28204</DisableSpecificWarnings> | ||
<PreprocessorDefinitions>_WINRT_DLL;%(PreprocessorDefinitions)</PreprocessorDefinitions> | ||
<AdditionalUsingDirectories>$(WindowsSDK_WindowsMetadata);$(AdditionalUsingDirectories)</AdditionalUsingDirectories> | ||
</ClCompile> | ||
<Midl> | ||
<!-- This allows applications targetting older Windows SDKs (e.g. RNW 0.65 apps) to consume the library generated WinMD --> | ||
<AdditionalOptions>%(AdditionalOptions) /noattributename</AdditionalOptions> | ||
</Midl> | ||
<Link> | ||
<SubSystem>Console</SubSystem> | ||
<GenerateWindowsMetadata>true</GenerateWindowsMetadata> | ||
<ModuleDefinitionFile>ReactNativeBatteryStatus.def</ModuleDefinitionFile> | ||
</Link> | ||
</ItemDefinitionGroup> | ||
<ItemDefinitionGroup Condition="'$(Configuration)'=='Debug'"> | ||
<ClCompile> | ||
<PreprocessorDefinitions>_DEBUG;%(PreprocessorDefinitions)</PreprocessorDefinitions> | ||
</ClCompile> | ||
</ItemDefinitionGroup> | ||
<ItemDefinitionGroup Condition="'$(Configuration)'=='Release'"> | ||
<ClCompile> | ||
<PreprocessorDefinitions>NDEBUG;%(PreprocessorDefinitions)</PreprocessorDefinitions> | ||
</ClCompile> | ||
</ItemDefinitionGroup> | ||
<ItemGroup> | ||
<ClInclude Include="ReactPackageProvider.h"> | ||
<DependentUpon>ReactPackageProvider.idl</DependentUpon> | ||
</ClInclude> | ||
<ClInclude Include="RNWBatteryStatus.h" /> | ||
<ClInclude Include="pch.h" /> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<ClCompile Include="pch.cpp"> | ||
<PrecompiledHeader>Create</PrecompiledHeader> | ||
</ClCompile> | ||
<ClCompile Include="ReactPackageProvider.cpp"> | ||
<DependentUpon>ReactPackageProvider.idl</DependentUpon> | ||
</ClCompile> | ||
<ClCompile Include="$(GeneratedFilesDir)module.g.cpp" /> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<Midl Include="ReactPackageProvider.idl" /> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<None Include="PropertySheet.props" /> | ||
</ItemGroup> | ||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" /> | ||
<ImportGroup Label="ReactNativeWindowsTargets"> | ||
<Import Project="$(ReactNativeWindowsDir)\PropertySheets\External\Microsoft.ReactNative.Uwp.CppLib.targets" Condition="Exists('$(ReactNativeWindowsDir)\PropertySheets\External\Microsoft.ReactNative.Uwp.CppLib.targets')" /> | ||
</ImportGroup> | ||
<Target Name="EnsureReactNativeWindowsTargets" BeforeTargets="PrepareForBuild"> | ||
<PropertyGroup> | ||
<ErrorText>This project references targets in your node_modules\react-native-windows folder that are missing. The missing file is {0}.</ErrorText> | ||
</PropertyGroup> | ||
<Error Condition="!Exists('$(ReactNativeWindowsDir)\PropertySheets\External\Microsoft.ReactNative.Uwp.CppLib.props')" Text="$([System.String]::Format('$(ErrorText)', '$(ReactNativeWindowsDir)\PropertySheets\External\Microsoft.ReactNative.Uwp.CppLib.props'))" /> | ||
<Error Condition="!Exists('$(ReactNativeWindowsDir)\PropertySheets\External\Microsoft.ReactNative.Uwp.CppLib.targets')" Text="$([System.String]::Format('$(ErrorText)', '$(ReactNativeWindowsDir)\PropertySheets\External\Microsoft.ReactNative.Uwp.CppLib.targets'))" /> | ||
</Target> | ||
</Project> |
19 changes: 19 additions & 0 deletions
19
...tor/@react-native-webapis/battery-status/windows/ReactNativeBatteryStatus.vcxproj.filters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<Project ToolsVersion="Current" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> | ||
<ItemGroup> | ||
<Midl Include="ReactPackageProvider.idl" /> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<ClCompile Include="pch.cpp" /> | ||
<ClCompile Include="$(GeneratedFilesDir)module.g.cpp" /> | ||
<ClCompile Include="ReactPackageProvider.cpp" /> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<ClInclude Include="pch.h" /> | ||
<ClInclude Include="ReactPackageProvider.h" /> | ||
<ClInclude Include="RNWBatteryStatus.h" /> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<None Include="PropertySheet.props" /> | ||
</ItemGroup> | ||
</Project> |
18 changes: 18 additions & 0 deletions
18
incubator/@react-native-webapis/battery-status/windows/ReactPackageProvider.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
#include "pch.h" | ||
#include "ReactPackageProvider.h" | ||
#if __has_include("ReactPackageProvider.g.cpp") | ||
#include "ReactPackageProvider.g.cpp" | ||
#endif | ||
|
||
#include "RNWBatteryStatus.h" | ||
|
||
namespace winrt::ReactNativeBatteryStatus::implementation | ||
{ | ||
using winrt::Microsoft::ReactNative::AddAttributedModules; | ||
using winrt::Microsoft::ReactNative::IReactPackageBuilder; | ||
|
||
void ReactPackageProvider::CreatePackage(IReactPackageBuilder const &packageBuilder) noexcept | ||
{ | ||
AddAttributedModules(packageBuilder, false); | ||
} | ||
} // namespace winrt::ReactNativeBatteryStatus::implementation |
21 changes: 21 additions & 0 deletions
21
incubator/@react-native-webapis/battery-status/windows/ReactPackageProvider.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
#pragma once | ||
#include "ReactPackageProvider.g.h" | ||
|
||
namespace winrt::ReactNativeBatteryStatus::implementation | ||
{ | ||
using winrt::Microsoft::ReactNative::IReactPackageBuilder; | ||
|
||
struct ReactPackageProvider : ReactPackageProviderT<ReactPackageProvider> | ||
{ | ||
ReactPackageProvider() = default; | ||
|
||
void CreatePackage(IReactPackageBuilder const &packageBuilder) noexcept; | ||
}; | ||
} // namespace winrt::ReactNativeBatteryStatus::implementation | ||
|
||
namespace winrt::ReactNativeBatteryStatus::factory_implementation | ||
{ | ||
using winrt::Microsoft::ReactNative::IReactPackageBuilder; | ||
|
||
struct ReactPackageProvider : ReactPackageProviderT<ReactPackageProvider, implementation::ReactPackageProvider> {}; | ||
} // namespace winrt::ReactNativeBatteryStatus::factory_implementation |
9 changes: 9 additions & 0 deletions
9
incubator/@react-native-webapis/battery-status/windows/ReactPackageProvider.idl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
namespace ReactNativeBatteryStatus | ||
{ | ||
[webhosthidden] | ||
[default_interface] | ||
runtimeclass ReactPackageProvider : Microsoft.ReactNative.IReactPackageProvider | ||
{ | ||
ReactPackageProvider(); | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
#include "pch.h" |
14 changes: 14 additions & 0 deletions
14
incubator/@react-native-webapis/battery-status/windows/pch.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
#pragma once | ||
|
||
#define NOMINMAX | ||
|
||
#include <hstring.h> | ||
#include <restrictederrorinfo.h> | ||
#include <unknwn.h> | ||
#include <windows.h> | ||
#include <CppWinRTIncludes.h> | ||
#if __has_include(<VersionMacros.h>) | ||
#include <VersionMacros.h> | ||
#endif | ||
|
||
#include <winrt/Microsoft.ReactNative.h> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters