Skip to content

Commit

Permalink
feat: add windows support
Browse files Browse the repository at this point in the history
  • Loading branch information
jpudysz committed Feb 27, 2024
1 parent 2915776 commit 67b89b9
Showing 1 changed file with 33 additions and 27 deletions.
60 changes: 33 additions & 27 deletions windows/ReactNativeUnistyles/ReactNativeUnistyles.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ using namespace winrt::Windows::UI::Core;
using namespace facebook;

struct UIInitialInfo {
int screenWidth;
int screenHeight;
Dimensions screen;
std::string colorScheme;
std::string contentSizeCategory;
};
Expand All @@ -39,7 +38,9 @@ struct Unistyles {
auto bounds = Window::Current().Bounds();

if (this->unistylesRuntime != nullptr) {
((UnistylesRuntime*)this->unistylesRuntime)->handleScreenSizeChange((int)bounds.Width, (int)bounds.Height, this->getInsets(), this->getStatusBarDimensions());
Dimensions screenDimensions = Dimensions{(int)bounds.Width, (int)bounds.Height};

((UnistylesRuntime*)this->unistylesRuntime)->handleScreenSizeChange(screenDimensions, this->getInsets(), this->getStatusBarDimensions(), this->getNavigationBarDimensions());
}
}));

Expand Down Expand Up @@ -80,8 +81,7 @@ struct Unistyles {
UIInitialInfo uiMetadata;
auto bounds = Window::Current().Bounds();

uiMetadata.screenWidth = bounds.Width;
uiMetadata.screenHeight = bounds.Height;
uiMetadata.screen = Dimensions{(int)bounds.Width, (int)bounds.Height};
uiMetadata.colorScheme = this->getColorScheme();
uiMetadata.contentSizeCategory = UnistylesUnspecifiedScheme;

Expand All @@ -91,12 +91,12 @@ struct Unistyles {
UIInitialInfo uiInfo = uiInfoFuture.get();

auto unistylesRuntime = std::make_shared<UnistylesRuntime>(
uiInfo.screenWidth,
uiInfo.screenHeight,
uiInfo.screen,
uiInfo.colorScheme,
uiInfo.contentSizeCategory,
this->getInsets(),
this->getStatusBarDimensions()
this->getStatusBarDimensions(),
this->getNavigationBarDimensions()
);

unistylesRuntime->onThemeChange([this](std::string theme) {
Expand All @@ -110,16 +110,30 @@ struct Unistyles {
this->OnThemeChange(payload);
});

unistylesRuntime.get()->onLayoutChange([this](std::string breakpoint, std::string orientation, int width, int height) {
unistylesRuntime.get()->onLayoutChange([this](std::string breakpoint, std::string orientation, Dimensions& screen, Dimensions& statusBar, Insets& insets, Dimensions& navigationBar) {
auto payload = winrt::Microsoft::ReactNative::JSValueObject{
{"type", "layout"},
{"payload", winrt::Microsoft::ReactNative::JSValueObject{
{"breakpoint", breakpoint},
{"orientation", orientation},
{"screen", winrt::Microsoft::ReactNative::JSValueObject{
{"width", width},
{"height", height}
}}
{"width", screen.width},
{"height", screen.height}
}},
{"statusBar", winrt::Microsoft::ReactNative::JSValueObject{
{"width", statusBar.width},
{"height", statusBar.height}
}},
{"navigationBar", winrt::Microsoft::ReactNative::JSValueObject{
{"width", navigationBar.width},
{"height", navigationBar.height}
}},
{"insets", winrt::Microsoft::ReactNative::JSValueObject{
{"top", insets.top},
{"bottom", insets.bottom},
{"left", insets.left},
{"right", insets.right}
}},
}}
};

Expand Down Expand Up @@ -195,24 +209,16 @@ struct Unistyles {
return UnistylesUnspecifiedScheme;
}

std::map<std::string, int>getInsets() {
std::map<std::string, int> insets;

insets.insert({ "top", 0 });
insets.insert({ "bottom", 0 });
insets.insert({ "left", 0 });
insets.insert({ "right", 0 });

return insets;
Insets getInsets() {
return Insets{ 0, 0, 0, 0 };
}

std::map<std::string, int>getStatusBarDimensions() {
std::map<std::string, int> statusBar;

statusBar.insert({ "height", 0 });
statusBar.insert({ "width", 0 });
Dimensions getStatusBarDimensions() {
return {0, 0};
}

return statusBar;
Dimensions getNavigationBarDimensions() {
return { 0, 0 };
}
};

Expand Down

0 comments on commit 67b89b9

Please sign in to comment.