8000 feat(dark-mode): apply ns-dark|ns-light class to root view at app launch · NativeScript/NativeScript@030f8ce · GitHub
[go: up one dir, main page]

Skip to content

Commit 030f8ce

Browse files
committed
feat(dark-mode): apply ns-dark|ns-light class to root view at app launch
1 parent 502a63b commit 030f8ce

File tree

1 file changed

+35
-13
lines changed

1 file changed

+35
-13
lines changed

tns-core-modules/application/application.ios.ts

Lines changed: 35 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,12 @@ export * from "./application-common";
1717

1818
// TODO: Remove this and get it from global to decouple builder for angular
1919
import { createViewFromEntry } from "../ui/builder";
20-
import { CLASS_PREFIX, getRootViewCssClasses, pushToRootViewCssClasses } from "../css/system-classes";
20+
import {
21+
CLASS_PREFIX,
22+
getRootViewCssClasses,
23+
pushToRootViewCssClasses,
24+
resetRootViewCssClasses
25+
} from "../css/system-classes";
2126
import { ios as iosView, View } from "../ui/core/view";
2227
import { Frame, NavigationEntry } from "../ui/frame";
2328
import { device } from "../platform/platform";
@@ -311,14 +316,13 @@ function createRootView(v?: View) {
311316
}
312317
}
313318

319+
resetRootViewCssClasses();
320+
314321
const deviceType = device.deviceType.toLowerCase();
315322
pushToRootViewCssClasses(`${CLASS_PREFIX}${IOS_PLATFORM}`);
316323
pushToRootViewCssClasses(`${CLASS_PREFIX}${deviceType}`);
317324
pushToRootViewCssClasses(`${CLASS_PREFIX}${iosApp.orientation}`);
318325

319-
const rootViewCssClasses = getRootViewCssClasses();
320-
rootViewCssClasses.forEach(c => rootView.cssClasses.add(c));
321-
322326
return rootView;
323327
}
324328

@@ -388,18 +392,36 @@ export function getNativeApplication(): UIApplication {
388392
return iosApp.nativeApp;
389393
}
390394

391-
function getViewController(view: View): UIViewController {
392-
let viewController: UIViewController = view.viewController || view.ios;
393-
if (viewController instanceof UIViewController) {
394-
return viewController;
395-
} else {
395+
function getUserInterfaceStyleValue(userInterfaceStyle: number): "dark" | "light" | "unspecified" {
396+
switch (userInterfaceStyle) {
397+
case UIUserInterfaceStyle.Unspecified:
398+
case UIUserInterfaceStyle.Light:
399+
return "light";
400+
case UIUserInterfaceStyle.Dark:
401+
return "dark";
402+
}
403+
}
404+
405+
// TODO: Rename to getRootViewController
406+
function getViewController(rootView: View): UIViewController {
407+
let viewController: UIViewController = rootView.viewController || rootView.ios;
408+
409+
if (!(viewController instanceof UIViewController)) {
396410
// We set UILayoutViewController dynamically to the root view if it doesn't have a view controller
397411
// At the moment the root view doesn't have its native view created. We set it in the setViewControllerView func
398-
viewController = iosView.UILayoutViewController.initWithOwner(new WeakRef(view)) as UIViewController;
399-
view.viewController = viewController;
400-
401-
return viewController;
412+
viewController = iosView.UILayoutViewController.initWithOwner(new WeakRef(rootView)) as UIViewController;
413+
rootView.viewController = viewController;
402414
}
415+
416+
const userInterfaceStyle = viewController.traitCollection.userInterfaceStyle;
417+
const userInterfaceStyleValue = getUserInterfaceStyleValue(userInterfaceStyle);
418+
419+
pushToRootViewCssClasses(`${CLASS_PREFIX}${userInterfaceStyleValue}`);
420+
421+
const rootViewCssClasses = getRootViewCssClasses();
422+
rootViewCssClasses.forEach(c => rootView.cssClasses.add(c));
423+
424+
return viewController;
403425
}
404426

405427
function setViewControllerView(view: View): void {

0 commit comments

Comments
 (0)
0