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

Add WinRT example page #415

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
* @format
*/

import 'react-native-winrt';
import {AppRegistry} from 'react-native';
import App from './src/App';
import {name as appName} from './app.json';
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
"react-native-tts": "ak1394/react-native-tts",
"react-native-webview": "^13.2.2",
"react-native-windows": "0.73.10",
"react-native-winrt": "^0.72.1",
"react-native-xaml": "^0.0.74"
},
"devDependencies": {
Expand Down
7 changes: 7 additions & 0 deletions src/RNGalleryList.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import {TrackPlayerExamplePage} from './examples/TrackPlayerExamplePage';
import {WindowsHelloExamplePage} from './examples/WindowsHelloExamplePage';
import {ExpanderExamplePage} from './examples/ExpanderExamplePage';
import {VirtualizedListExamplePage} from './examples/VirtualizedListExamplePage';
import {WinRTExamplePage} from './examples/WinRTExamplePage';

let RNGalleryCategories = [
'Basic Input',
Expand Down Expand Up @@ -266,6 +267,12 @@ export const RNGalleryList: Array<IRNGalleryExample> = [
icon: '\uE8A4',
type: 'Layout',
},
{
key: 'WinRT',
component: WinRTExamplePage,
icon: '\uEF15',
type: 'Layout',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
type: 'Layout',
type: 'System',

},
Comment on lines +270 to +275
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI when you have to merge this later, look for:
icon -> textIcon

{
key: 'Xaml',
component: XamlExamplePage,
Expand Down
55 changes: 55 additions & 0 deletions src/components/Notifications.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
const Notifications = Windows.UI.Notifications;
const ToastTemplateType = Notifications.ToastTemplateType;
const ToastNotificationManager = Notifications.ToastNotificationManager;
const ToastNotification = Notifications.ToastNotification;

export function showNotification(notification) {
var type = ToastTemplateType.toastText01;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
var type = ToastTemplateType.toastText01;
let type = ToastTemplateType.toastText01;

I'm surprised we don't have lint rules setup about var. As a default in our JS, we should always prefer let. It's scoped in a way that is much more sensible.


var obj = {};
if (typeof notification === 'string') {
obj.text = notification;
} else {
obj = notification;
}

if (obj.template != undefined) {
type = obj.template;
}

var xml = ToastNotificationManager.getTemplateContent(type);
for (var tagName in obj) {
var xmlElements = xml.getElementsByTagName(tagName);
var value = obj[tagName];
if (typeof value === 'string') {
fillXmlElements(xml, xmlElements, [value]);
} else if (Array.isArray(value)) {
fillXmlElements(xml, xmlElements, value);
} else if (typeof value === 'object') {
fillXmlElements(xml, xmlElements, [value]);
}
Comment on lines +24 to +30
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like case 1 and 3 are the same. Is this still correct?

Suggested change
if (typeof value === 'string') {
fillXmlElements(xml, xmlElements, [value]);
} else if (Array.isArray(value)) {
fillXmlElements(xml, xmlElements, value);
} else if (typeof value === 'object') {
fillXmlElements(xml, xmlElements, [value]);
}
if (Array.isArray(value)) {
fillXmlElements(xml, xmlElements, value);
} else {
fillXmlElements(xml, xmlElements, [value]);
}

Or do you get types other than string/object in there? (like numeric literals or something)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If your goal is "if it's an array, leave it; otherwise, place in an array" there might be other options. Like:

[...value]

Does that do the right thing? It definitely spreads a source array into a new array. But for a single item... I think still the same thing? Hmmm... except strings are iterable so that'll break apart the string. Anyway, might be some syntactic magic here.

}

var toast = new ToastNotification(xml);
ToastNotificationManager.createToastNotifier().show(toast);
}

function fillXmlElements(xml, xmlElements, arr) {
var i = 0;
for (var arrValue of arr) {
var node = xmlElements[i++];
Comment on lines +39 to +40
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's happening here? Are arr and xmlElements ensured to be the same length? Why are we enumerating over arr but indexing into xmlElements?

if (typeof arrValue === 'string') {
node.appendChild(xml.createTextNode(arrValue));
} else if (typeof arrValue === 'object') {
for (var attrName in arrValue) {
var attr = node.attributes.getNamedItem(attrName);
if (!attr) {
attr = xml.createAttribute(attrName);
node.attributes.setNamedItem(attr);
}

attr.nodeValue = arrValue[attrName];
}
}
}
}
121 changes: 121 additions & 0 deletions src/examples/WinRTExamplePage.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
'use strict';
import React, {useState} from 'react';
import {Example} from '../components/Example';
import {Page} from '../components/Page';
import {
Image,
SafeAreaView,
StyleSheet,
ScrollView,
View,
Text,
StatusBar,
Platform,
PlatformColor,
Pressable,
Linking,
} from 'react-native';

import {useTheme} from '@react-navigation/native';

import {showNotification} from '../components/Notifications';

export const WinRTExamplePage: React.FunctionComponent<{}> = () => {
const {colors} = useTheme();
const example1jsx = `
<View
style={{
flexDirection: 'row',
flexWrap: 'wrap',
alignItems: 'center',
}}>
<Pressable
style={{
width: 200,
height: 50,
borderRadius: 2,
backgroundColor:
Platform.OS === 'windows'
? PlatformColor('SystemColorButtonFaceColor')
: 'silver',
}}
onPress={() => {
showNotification({
template:
Windows.UI.Notifications.ToastTemplateType
.toastImageAndText01,
// The template schema can be found at https://docs.microsoft.com/previous-versions/windows/apps/hh761494(v=win.10)
text: 'hello world',
image: {
src: 'https://microsoft.github.io/react-native-windows/img/header_logo.svg',
alt: 'React logo',
},
});
}}>
<Text
style={{
textAlign: 'center',
paddingVertical: 15,
color: colors.text,
}}>
Press Me
</Text>
</Pressable>
</View>`;

return (
<Page
title="WinRT"
description="A React Native Windows module that allows use of native (non-XAML) WinRT APIs"
componentType="Community"
pageCodeUrl="https://github.com/microsoft/react-native-gallery/blob/main/src/examples/WinRTExamplePage.tsx"
documentation={[
{
label: 'WinRT Source Code',
url: 'https://github.com/asklar/react-native-winrt',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
url: 'https://github.com/asklar/react-native-winrt',
url: 'https://github.com/microsoft/react-native-winrt',

},
]}>
<Example title="A Windows Notification example" code={example1jsx}>
<View
style={{
flexDirection: 'row',
flexWrap: 'wrap',
alignItems: 'center',
}}>
<Pressable
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any reason this doesn't just use a Button instead of a custom Pressable? It looks... button-y. And the point of the sample isn't any exact visual style, so it should be simple first.

style={{
width: 200,
height: 50,
borderRadius: 2,
backgroundColor:
Platform.OS === 'windows'
? PlatformColor('SystemColorButtonFaceColor')
: 'silver',
}}
onPress={() => {
showNotification({
template:
Windows.UI.Notifications.ToastTemplateType
.toastImageAndText01,
// The template schema can be found at https://docs.microsoft.com/previous-versions/windows/apps/hh761494(v=win.10)
text: 'hello world',
image: {
src: 'https://microsoft.github.io/react-native-windows/img/header_logo.svg',
alt: 'React logo',
},
});
}}>
<Text
style={{
textAlign: 'center',
paddingVertical: 15,
color: colors.text,
}}>
Press Me
</Text>
</Pressable>
</View>
</Example>
</Page>
);
};
12 changes: 12 additions & 0 deletions windows/ExperimentalFeatures.props
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,18 @@
<UseExperimentalNuget>false</UseExperimentalNuget>

<ReactExperimentalFeaturesSet>true</ReactExperimentalFeaturesSet>

<RnWinRTParameters>
-include Windows.Foundation
-include Windows.UI.StartScreen
-include Windows.UI.ViewManagement
-include Windows.Storage
-include Windows.Security.Cryptography.CryptographicBuffer
-include Windows.Security.Cryptography.ICryptographicBufferStatics
-include Windows.Security.Cryptography.BinaryStringEncoding
-include Windows.UI.Notifications
-include Windows.Data.Xml.Dom
</RnWinRTParameters>

</PropertyGroup>

Expand Down
14 changes: 14 additions & 0 deletions windows/rngallery.sln
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "ReactNativeXaml", "..\node_
EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "ReactNativeWindowsHello", "..\node_modules\react-native-windows-hello\windows\ReactNativeWindowsHello\ReactNativeWindowsHello.vcxproj", "{82BF2B2C-EDA5-47CA-A9F5-56BA622A15EA}"
EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "WinRTTurboModule", "..\node_modules\react-native-winrt\windows\WinRTTurboModule\WinRTTurboModule.vcxproj", "{E13C9C82-0013-422C-945E-848A258AC4DB}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|ARM64 = Debug|ARM64
Expand Down Expand Up @@ -375,6 +377,18 @@ Global
{82BF2B2C-EDA5-47CA-A9F5-56BA622A15EA}.Release|x64.Build.0 = Release|x64
{82BF2B2C-EDA5-47CA-A9F5-56BA622A15EA}.Release|x86.ActiveCfg = Release|Win32
{82BF2B2C-EDA5-47CA-A9F5-56BA622A15EA}.Release|x86.Build.0 = Release|Win32
{E13C9C82-0013-422C-945E-848A258AC4DB}.Debug|ARM64.ActiveCfg = Debug|ARM64
{E13C9C82-0013-422C-945E-848A258AC4DB}.Debug|ARM64.Build.0 = Debug|ARM64
{E13C9C82-0013-422C-945E-848A258AC4DB}.Debug|x64.ActiveCfg = Debug|x64
{E13C9C82-0013-422C-945E-848A258AC4DB}.Debug|x64.Build.0 = Debug|x64
{E13C9C82-0013-422C-945E-848A258AC4DB}.Debug|x86.ActiveCfg = Debug|Win32
{E13C9C82-0013-422C-945E-848A258AC4DB}.Debug|x86.Build.0 = Debug|Win32
{E13C9C82-0013-422C-945E-848A258AC4DB}.Release|ARM64.ActiveCfg = Release|ARM64
{E13C9C82-0013-422C-945E-848A258AC4DB}.Release|ARM64.Build.0 = Release|ARM64
{E13C9C82-0013-422C-945E-848A258AC4DB}.Release|x64.ActiveCfg = Release|x64
{E13C9C82-0013-422C-945E-848A258AC4DB}.Release|x64.Build.0 = Release|x64
{E13C9C82-0013-422C-945E-848A258AC4DB}.Release|x86.ActiveCfg = Release|Win32
{E13C9C82-0013-422C-945E-848A258AC4DB}.Release|x86.Build.0 = Release|Win32
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
3 changes: 2 additions & 1 deletion windows/rngallery/App.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ App::App() noexcept
InstanceSettings().UseFastRefresh(false);
#else
JavaScriptBundleFile(L"index");
InstanceSettings().UseWebDebugger(true);
InstanceSettings().UseWebDebugger(false);
InstanceSettings().UseDirectDebugger(true);
InstanceSettings().UseFastRefresh(true);
#endif

Expand Down
5 changes: 5 additions & 0 deletions windows/rngallery/AutolinkedNativeModules.g.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@
// Includes from react-native-webview
#include <winrt/ReactNativeWebView.h>

// Includes from react-native-winrt
#include <winrt/WinRTTurboModule.h>

// Includes from react-native-xaml
#include <winrt/ReactNativeXaml.h>

Expand Down Expand Up @@ -94,6 +97,8 @@ void RegisterAutolinkedNativeModulePackages(winrt::Windows::Foundation::Collecti
packageProviders.Append(winrt::RNTTS::ReactPackageProvider());
// IReactPackageProviders from react-native-webview
packageProviders.Append(winrt::ReactNativeWebView::ReactPackageProvider());
// IReactPackageProviders from react-native-winrt
packageProviders.Append(winrt::WinRTTurboModule::ReactPackageProvider());
// IReactPackageProviders from react-native-xaml
packageProviders.Append(winrt::ReactNativeXaml::ReactPackageProvider());
// IReactPackageProviders from react-native-windows-hello
Expand Down
4 changes: 4 additions & 0 deletions windows/rngallery/AutolinkedNativeModules.g.targets
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@
<ProjectReference Include="$(ProjectDir)..\..\node_modules\react-native-webview\windows\ReactNativeWebView\ReactNativeWebView.vcxproj">
<Project>{00AA3765-C6A0-4713-B3F9-BFE47B9C83F5}</Project>
</ProjectReference>
<!-- Projects from react-native-winrt -->
<ProjectReference Include="$(ProjectDir)..\..\node_modules\react-native-winrt\windows\WinRTTurboModule\WinRTTurboModule.vcxproj">
<Project>{E13C9C82-0013-422C-945E-848A258AC4DB}</Project>
</ProjectReference>
<!-- Projects from react-native-xaml -->
<ProjectReference Include="$(ProjectDir)..\..\node_modules\react-native-xaml\windows\ReactNativeXaml\ReactNativeXaml.vcxproj">
<Project>{0ff7027a-222c-4ffb-8f17-91d18bbaf7a8}</Project>
Expand Down
13 changes: 9 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3320,10 +3320,10 @@
resolved "https://registry.yarnpkg.com/@opentelemetry/semantic-conventions/-/semantic-conventions-1.22.0.tgz#d7502533a7c96e25baab86bac965468e0703a8b4"
integrity sha512-CAOgFOKLybd02uj/GhCdEeeBjOS0yeoDeo/CA7ASBSmenpZHAKGB3iDm/rv3BQLcabb/OprDEsSQ1y0P8A7Siw==

"@react-native-clipboard/clipboard@^1.13.2":
version "1.13.2"
resolved "https://registry.yarnpkg.com/@react-native-clipboard/clipboard/-/clipboard-1.13.2.tgz#28adcfc43ed2addddf79a59198ec1b25087c115e"
integrity sha512-uVM55oEGc6a6ZmSATDeTcMm55A/C1km5X47g0xaoF0Zagv7N/8RGvLceA5L/izPwflIy78t7XQeJUcnGSib0nA==
"@react-native-clipboard/clipboard@^1.14.0":
version "1.14.0"
resolved "https://registry.yarnpkg.com/@react-native-clipboard/clipboard/-/clipboard-1.14.0.tgz#ae3b7b59f51b1da1fc6a362244c18ddec113d1f6"
integrity sha512-kDLfA6HzP4T+kfGTEGdbsc2r4q0xQALshRQp8Ph/5YD5qT4CZdgkQM3oloKHIdh+AVks+QjtVHK1cZ1xb0Or7w==

"@react-native-community/checkbox@^0.5.16":
version "0.5.16"
Expand Down Expand Up @@ -9956,6 +9956,11 @@ [email protected]:
ws "^6.2.2"
yargs "^17.6.2"

react-native-winrt@^0.72.1:
version "0.72.1"
resolved "https://registry.yarnpkg.com/react-native-winrt/-/react-native-winrt-0.72.1.tgz#d5b2bc0fe27080c03a85c33bd477b6da3e70f0f9"
integrity sha512-uhUAzKjOFpsfeoByBBA1FTgsCZuwLYN9y9C/Zg+StZRUYVBcqpOT46avwxHKkgWHN1ixoDc/jUFdE5lmV4DuoQ==

react-native-xaml@^0.0.74:
version "0.0.74"
resolved "https://registry.yarnpkg.com/react-native-xaml/-/react-native-xaml-0.0.74.tgz#fc747308320eb1fda6dd69f5317bfeae37686b57"
Expand Down
Loading