react-native-unistyles 2.8.0-rc.4 → 2.8.0-rc.6

Sign up to get free protection for your applications and to get access to all the features.
@@ -7,6 +7,7 @@
7
7
  #include <optional>
8
8
  #include <variant>
9
9
  #include <math.h>
10
+ #include <algorithm>
10
11
 
11
12
  using namespace facebook;
12
13
 
@@ -1,6 +1,6 @@
1
1
  #import <React/RCTBridgeModule.h>
2
2
  #import <React/RCTEventEmitter.h>
3
- #import <ReactCommon/RCTRuntimeExecutor.h>
3
+
4
4
  #import <string>
5
5
 
6
6
  #if TARGET_OS_OSX
@@ -1,3 +1,5 @@
1
+ #if TARGET_OS_TV || TARGET_OS_VISION || TARGET_OS_IOS
2
+
1
3
  #include "Platform_Shared.h"
2
4
 
3
5
  std::string getContentSizeCategory() {
@@ -154,3 +156,5 @@ UIColor* colorFromHexString(NSString* hexString, float alpha) {
154
156
  blue:(rgbValue & 0x0000FF) / 255.0
155
157
  alpha:alpha];
156
158
  }
159
+
160
+ #endif
@@ -45,8 +45,12 @@
45
45
  });
46
46
 
47
47
  dispatch_async(dispatch_get_main_queue(), ^{
48
- unistylesRuntime->screen = [self getScreenDimensions];
48
+ Screen screen = [self getScreenDimensions];
49
+
50
+ unistylesRuntime->screen = Dimensions({screen.width, screen.height});
49
51
  unistylesRuntime->colorScheme = [self getColorScheme];
52
+ unistylesRuntime->fontScale = screen.fontScale;
53
+ unistylesRuntime->pixelRatio = screen.pixelRatio;
50
54
  });
51
55
  }
52
56
 
@@ -69,7 +73,7 @@
69
73
  }
70
74
 
71
75
  - (void)onWindowChange {
72
- Dimensions screen = [self getScreenDimensions];
76
+ Screen screen = [self getScreenDimensions];
73
77
 
74
78
  if (self.unistylesRuntime != nullptr) {
75
79
  ((UnistylesRuntime*)self.unistylesRuntime)->handleScreenSizeChange(
@@ -91,11 +95,21 @@
91
95
  return UnistylesLightScheme;
92
96
  }
93
97
 
94
- - (Dimensions)getScreenDimensions {
98
+ - (Screen)getScreenDimensions {
95
99
  NSWindow *window = RCTSharedApplication().mainWindow;
96
- Dimensions screenDimension = {(int)window.frame.size.width, (int)window.frame.size.height};
100
+ int width = (int)window.frame.size.width;
101
+ int height = (int)window.frame.size.height;
102
+ float pixelRatio = window.backingScaleFactor;
103
+ float fontScale = [self getFontScale];
97
104
 
98
- return screenDimension;
105
+ return Screen({width, height, pixelRatio, fontScale});
106
+ }
107
+
108
+ - (float)getFontScale {
109
+ NSFont *systemFont = [NSFont systemFontOfSize:[NSFont systemFontSize]];
110
+ CGFloat systemFontScale = systemFont.pointSize / 13.0;
111
+
112
+ return systemFontScale;
99
113
  }
100
114
 
101
115
  @end
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-native-unistyles",
3
- "version": "2.8.0-rc.4",
3
+ "version": "2.8.0-rc.6",
4
4
  "description": "Level up your React Native StyleSheet",
5
5
  "scripts": {
6
6
  "test": "jest",
@@ -2,10 +2,10 @@
2
2
  <Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3
3
 
4
4
  <!--
5
- Application projects contain a file with this name to specify some important settings
6
- that will apply globally for the app and *all* native modules the app consumes. These
7
- values are set by the app developer. However you can also set them here to test building
8
- the library solution without an app.
5
+ This file contains some important settings that will apply globally for
6
+ your app and *all* native modules your app consumes. These values were
7
+ set when you created the app project, and in some cases cannot be
8
+ simply changed here without recreating a new project.
9
9
  -->
10
10
 
11
11
  <PropertyGroup Label="Microsoft.ReactNative Experimental Features">
@@ -5,7 +5,6 @@
5
5
  </config>
6
6
  <packageSources>
7
7
  <clear />
8
- <add key="react-native" value="https://pkgs.dev.azure.com/ms/react-native/_packaging/react-native-public/nuget/v3/index.json" />
9
8
  <add key="Nuget.org" value="https://api.nuget.org/v3/index.json" />
10
9
  </packageSources>
11
10
  <disabledPackageSources>
@@ -8,21 +8,18 @@
8
8
  #include <winrt/Microsoft.ReactNative.h>
9
9
  #include <winrt/Windows.UI.Xaml.h>
10
10
  #include <winrt/Windows.UI.Core.h>
11
+ #include <winrt/Windows.Graphics.Display.h>
11
12
  #include <winrt/Windows.UI.ViewManagement.h>
12
13
  #include "UnistylesRuntime.h"
14
+ #include <TurboModuleProvider.h>
13
15
 
14
16
  using namespace winrt::Windows::UI::ViewManagement;
17
+ using namespace winrt::Windows::Graphics::Display;
15
18
  using namespace winrt::Microsoft::ReactNative;
16
19
  using namespace winrt::Windows::UI::Xaml;
17
20
  using namespace winrt::Windows::UI::Core;
18
21
  using namespace facebook;
19
22
 
20
- struct UIInitialInfo {
21
- Dimensions screen;
22
- std::string colorScheme;
23
- std::string contentSizeCategory;
24
- };
25
-
26
23
  namespace winrt::ReactNativeUnistyles
27
24
  {
28
25
 
@@ -30,157 +27,86 @@ REACT_MODULE(Unistyles, L"Unistyles")
30
27
  struct Unistyles {
31
28
 
32
29
  REACT_INIT(Initialize)
33
- void Initialize(ReactContext const &reactContext) noexcept {
30
+ void Initialize(ReactContext const& reactContext) noexcept {
34
31
  m_reactContext = reactContext;
35
32
 
36
33
  m_reactContext.UIDispatcher().Post([this]() mutable {
37
34
  this->windowSizeChange = Window::Current().CoreWindow().SizeChanged(TypedEventHandler<CoreWindow, WindowSizeChangedEventArgs>([=](CoreWindow const sender, WindowSizeChangedEventArgs const&) {
38
- auto bounds = Window::Current().Bounds();
39
-
40
35
  if (this->unistylesRuntime != nullptr) {
41
- Dimensions screenDimensions = Dimensions{(int)bounds.Width, (int)bounds.Height};
42
-
43
- ((UnistylesRuntime*)this->unistylesRuntime)->handleScreenSizeChange(screenDimensions, this->getInsets(), this->getStatusBarDimensions(), this->getNavigationBarDimensions());
36
+ ((UnistylesRuntime*)this->unistylesRuntime)->handleScreenSizeChange(getScreenDimensions(), std::nullopt, std::nullopt, std::nullopt);
44
37
  }
45
38
  }));
46
39
 
47
- UISettings settings;
48
-
49
- this->colorValuesChange = settings.ColorValuesChanged(TypedEventHandler<UISettings, IInspectable>([this](UISettings const& sender, IInspectable const&){
40
+ this->colorValuesChange = UISettings().ColorValuesChanged(TypedEventHandler<UISettings, IInspectable>([this](UISettings const& sender, IInspectable const&) {
50
41
  if (this->unistylesRuntime != nullptr) {
51
42
  ((UnistylesRuntime*)this->unistylesRuntime)->handleAppearanceChange(this->getColorScheme());
52
43
  }
53
44
  }));
54
45
  });
55
46
  }
56
-
47
+
57
48
  REACT_SYNC_METHOD(install)
58
- bool install() noexcept {
49
+ bool install() noexcept {
59
50
  if (m_reactContext == nullptr) {
60
51
  return false;
61
52
  }
62
53
 
63
54
  jsi::Runtime* jsiRuntime = TryGetOrCreateContextRuntime(m_reactContext);
64
-
55
+
65
56
  if (jsiRuntime == nullptr) {
66
57
  return false;
67
58
  }
68
59
 
69
60
  auto& runtime = *jsiRuntime;
61
+ auto callInvoker = MakeAbiCallInvoker(m_reactContext.Handle().JSDispatcher());
70
62
 
71
- registerUnistylesHostObject(runtime);
63
+ registerUnistylesHostObject(runtime, callInvoker);
72
64
 
73
65
  return true;
74
66
  }
75
67
 
76
- void registerUnistylesHostObject(jsi::Runtime& runtime) {
77
- std::promise<UIInitialInfo> uiInfoPromise;
78
- auto uiInfoFuture = uiInfoPromise.get_future();
79
-
80
- m_reactContext.UIDispatcher().Post([this, promise = std::move(uiInfoPromise)]() mutable {
81
- UIInitialInfo uiMetadata;
82
- auto bounds = Window::Current().Bounds();
68
+ void registerUnistylesHostObject(jsi::Runtime& runtime, std::shared_ptr<facebook::react::CallInvoker> callInvoker) {
69
+ auto unistylesRuntime = std::make_shared<UnistylesRuntime>(runtime, callInvoker);
83
70
 
84
- uiMetadata.screen = Dimensions{(int)bounds.Width, (int)bounds.Height};
85
- uiMetadata.colorScheme = this->getColorScheme();
86
- uiMetadata.contentSizeCategory = UnistylesUnspecifiedScheme;
87
-
88
- promise.set_value(std::move(uiMetadata));
89
- });
90
-
91
- UIInitialInfo uiInfo = uiInfoFuture.get();
71
+ this->unistylesRuntime = unistylesRuntime.get();
92
72
 
93
- auto unistylesRuntime = std::make_shared<UnistylesRuntime>(
94
- uiInfo.screen,
95
- uiInfo.colorScheme,
96
- uiInfo.contentSizeCategory,
97
- this->getInsets(),
98
- this->getStatusBarDimensions(),
99
- this->getNavigationBarDimensions()
100
- );
73
+ makeShared((UnistylesRuntime*)unistylesRuntime.get());
101
74
 
102
- unistylesRuntime->onThemeChange([this](std::string theme) {
103
- winrt::hstring themeName = winrt::to_hstring(theme);
75
+ auto hostObject = jsi::Object::createFromHostObject(runtime, unistylesRuntime);
104
76
 
105
- auto payload = winrt::Microsoft::ReactNative::JSValueObject{
106
- {"type", "theme"},
107
- {"payload", winrt::Microsoft::ReactNative::JSValueObject{{"themeName", winrt::to_string(themeName)}}}
108
- };
77
+ runtime.global().setProperty(runtime, "__UNISTYLES__", std::move(hostObject));
78
+ }
109
79
 
110
- this->OnThemeChange(payload);
80
+ void makeShared(UnistylesRuntime* unistylesRuntime) {
81
+ unistylesRuntime->setScreenDimensionsCallback([this]() {
82
+ return getScreenDimensions();
111
83
  });
112
84
 
113
- unistylesRuntime.get()->onLayoutChange([this](std::string breakpoint, std::string orientation, Dimensions& screen, Dimensions& statusBar, Insets& insets, Dimensions& navigationBar) {
114
- auto payload = winrt::Microsoft::ReactNative::JSValueObject{
115
- {"type", "layout"},
116
- {"payload", winrt::Microsoft::ReactNative::JSValueObject{
117
- {"breakpoint", breakpoint},
118
- {"orientation", orientation},
119
- {"screen", winrt::Microsoft::ReactNative::JSValueObject{
120
- {"width", screen.width},
121
- {"height", screen.height}
122
- }},
123
- {"statusBar", winrt::Microsoft::ReactNative::JSValueObject{
124
- {"width", statusBar.width},
125
- {"height", statusBar.height}
126
- }},
127
- {"navigationBar", winrt::Microsoft::ReactNative::JSValueObject{
128
- {"width", navigationBar.width},
129
- {"height", navigationBar.height}
130
- }},
131
- {"insets", winrt::Microsoft::ReactNative::JSValueObject{
132
- {"top", insets.top},
133
- {"bottom", insets.bottom},
134
- {"left", insets.left},
135
- {"right", insets.right}
136
- }},
137
- }}
138
- };
139
-
140
- this->OnLayoutChange(payload);
85
+ unistylesRuntime->setColorSchemeCallback([this]() {
86
+ return getColorScheme();
141
87
  });
142
88
 
143
- unistylesRuntime.get()->onPluginChange([this]() {
144
- auto payload = winrt::Microsoft::ReactNative::JSValueObject{
145
- {"type", "plugin"}
146
- };
89
+ m_reactContext.UIDispatcher().Post([this, unistylesRuntime]() {
90
+ auto screen = getScreenDimensions();
147
91
 
148
- this->OnPluginChange(payload);
92
+ unistylesRuntime->screen = Dimensions({ screen.width, screen.height });
93
+ unistylesRuntime->pixelRatio = screen.pixelRatio;
94
+ unistylesRuntime->fontScale = screen.fontScale;
95
+ unistylesRuntime->colorScheme = getColorScheme();
149
96
  });
150
-
151
- unistylesRuntime.get()->onContentSizeCategoryChange([this](std::string contentSizeCategory) {
152
- // not available on windows
153
- });
154
-
155
- this->unistylesRuntime = unistylesRuntime.get();
156
-
157
- auto hostObject = jsi::Object::createFromHostObject(runtime, unistylesRuntime);
158
-
159
- runtime.global().setProperty(runtime, "__UNISTYLES__", std::move(hostObject));
160
97
  }
161
98
 
162
- REACT_EVENT(OnThemeChange, L"__unistylesOnChange")
163
- std::function<void(winrt::Microsoft::ReactNative::JSValueObject const&)> OnThemeChange;
164
-
165
- REACT_EVENT(OnLayoutChange, L"__unistylesOnChange")
166
- std::function<void(winrt::Microsoft::ReactNative::JSValueObject const&)> OnLayoutChange;
167
-
168
- REACT_EVENT(OnPluginChange, L"__unistylesOnChange")
169
- std::function<void(winrt::Microsoft::ReactNative::JSValueObject const&)> OnPluginChange;
170
-
171
99
  ~Unistyles() {
172
100
  if (this->unistylesRuntime != nullptr) {
173
101
  this->unistylesRuntime = nullptr;
174
102
  }
175
103
 
176
104
  if (this->windowSizeChange) {
177
- Window::Current().CoreWindow().SizeChanged(this->windowSizeChange);
105
+ this->windowSizeChange = { NULL };
178
106
  }
179
107
 
180
108
  if (this->colorValuesChange) {
181
- UISettings settings;
182
-
183
- settings.ColorValuesChanged(this->colorValuesChange);
109
+ this->colorValuesChange = { NULL };
184
110
  }
185
111
  }
186
112
 
@@ -191,16 +117,17 @@ struct Unistyles {
191
117
  winrt::event_token colorValuesChange{ NULL };
192
118
 
193
119
  std::string getColorScheme() {
194
- auto uiSettings = UISettings();
195
- auto background = uiSettings.GetColorValue(UIColorType::Background);
120
+ UISettings uiSettings;
121
+
122
+ auto backgroundColor = uiSettings.GetColorValue(UIColorType::Background);
196
123
 
197
- bool isDark = background == winrt::Windows::UI::Colors::Black();
124
+ bool isDark = backgroundColor == Colors::Black();
198
125
 
199
126
  if (isDark) {
200
127
  return UnistylesDarkScheme;
201
128
  }
202
129
 
203
- bool isLight = background == winrt::Windows::UI::Colors::White();
130
+ bool isLight = backgroundColor == Colors::White();
204
131
 
205
132
  if (isLight) {
206
133
  return UnistylesLightScheme;
@@ -209,16 +136,13 @@ struct Unistyles {
209
136
  return UnistylesUnspecifiedScheme;
210
137
  }
211
138
 
212
- Insets getInsets() {
213
- return Insets{ 0, 0, 0, 0 };
214
- }
215
-
216
- Dimensions getStatusBarDimensions() {
217
- return {0, 0};
218
- }
139
+ Screen getScreenDimensions() {
140
+ auto bounds = Window::Current().Bounds();
141
+ auto displayInfo = DisplayInformation::GetForCurrentView();
142
+ auto pixelRatio = displayInfo.LogicalDpi() / 96.0f;
143
+ float fontScale = UISettings().TextScaleFactor();
219
144
 
220
- Dimensions getNavigationBarDimensions() {
221
- return { 0, 0 };
145
+ return Screen({ (int)bounds.Width, (int)bounds.Height, pixelRatio, fontScale });
222
146
  }
223
147
  };
224
148
 
@@ -79,7 +79,7 @@
79
79
  <PropertyGroup Label="UserMacros" />
80
80
  <ItemDefinitionGroup>
81
81
  <ClCompile>
82
- <PrecompiledHeader>Use</PrecompiledHeader>
82
+ <PrecompiledHeader>NotUsing</PrecompiledHeader>
83
83
  <PrecompiledHeaderFile>pch.h</PrecompiledHeaderFile>
84
84
  <PrecompiledHeaderOutputFile>$(IntDir)pch.pch</PrecompiledHeaderOutputFile>
85
85
  <WarningLevel>Level4</WarningLevel>
@@ -112,6 +112,9 @@
112
112
  </ClCompile>
113
113
  </ItemDefinitionGroup>
114
114
  <ItemGroup>
115
+ <ClInclude Include="..\..\cxx\Macros.h" />
116
+ <ClInclude Include="..\..\cxx\UnistylesModel.h" />
117
+ <ClInclude Include="..\..\cxx\UnistylesRuntime.h" />
115
118
  <ClInclude Include="ReactPackageProvider.h">
116
119
  <DependentUpon>ReactPackageProvider.idl</DependentUpon>
117
120
  </ClInclude>
@@ -119,6 +122,8 @@
119
122
  <ClInclude Include="pch.h" />
120
123
  </ItemGroup>
121
124
  <ItemGroup>
125
+ <ClCompile Include="..\..\cxx\UnistylesImpl.cpp" />
126
+ <ClCompile Include="..\..\cxx\UnistylesModel.cpp" />
122
127
  <ClCompile Include="..\..\cxx\UnistylesRuntime.cpp">
123
128
  <PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug|ARM64'">NotUsing</PrecompiledHeader>
124
129
  <PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">NotUsing</PrecompiledHeader>
@@ -154,4 +159,4 @@
154
159
  <Error Condition="!Exists('$(ReactNativeWindowsDir)\PropertySheets\External\Microsoft.ReactNative.Uwp.CppLib.props')" Text="$([System.String]::Format('$(ErrorText)', '$(ReactNativeWindowsDir)\PropertySheets\External\Microsoft.ReactNative.Uwp.CppLib.props'))" />
155
160
  <Error Condition="!Exists('$(ReactNativeWindowsDir)\PropertySheets\External\Microsoft.ReactNative.Uwp.CppLib.targets')" Text="$([System.String]::Format('$(ErrorText)', '$(ReactNativeWindowsDir)\PropertySheets\External\Microsoft.ReactNative.Uwp.CppLib.targets'))" />
156
161
  </Target>
157
- </Project>
162
+ </Project>
@@ -8,13 +8,18 @@
8
8
  <ClCompile Include="$(GeneratedFilesDir)module.g.cpp" />
9
9
  <ClCompile Include="ReactPackageProvider.cpp" />
10
10
  <ClCompile Include="..\..\cxx\UnistylesRuntime.cpp" />
11
+ <ClCompile Include="..\..\cxx\UnistylesImpl.cpp" />
12
+ <ClCompile Include="..\..\cxx\UnistylesModel.cpp" />
11
13
  </ItemGroup>
12
14
  <ItemGroup>
13
15
  <ClInclude Include="pch.h" />
14
16
  <ClInclude Include="ReactPackageProvider.h" />
15
17
  <ClInclude Include="ReactNativeUnistyles.h" />
18
+ <ClInclude Include="..\..\cxx\Macros.h" />
19
+ <ClInclude Include="..\..\cxx\UnistylesModel.h" />
20
+ <ClInclude Include="..\..\cxx\UnistylesRuntime.h" />
16
21
  </ItemGroup>
17
22
  <ItemGroup>
18
23
  <None Include="PropertySheet.props" />
19
24
  </ItemGroup>
20
- </Project>
25
+ </Project>