diff --git a/DemoApp/DemoApp/DemoApp.csproj b/DemoApp/DemoApp/DemoApp.csproj index 84c0a07f..c36428c4 100644 --- a/DemoApp/DemoApp/DemoApp.csproj +++ b/DemoApp/DemoApp/DemoApp.csproj @@ -162,6 +162,12 @@ + + MSBuild:Compile + + + MSBuild:Compile + MSBuild:Compile diff --git a/DemoApp/DemoApp/MauiProgram.cs b/DemoApp/DemoApp/MauiProgram.cs index be53816e..2c0aebf4 100644 --- a/DemoApp/DemoApp/MauiProgram.cs +++ b/DemoApp/DemoApp/MauiProgram.cs @@ -14,7 +14,7 @@ public static MauiApp CreateMauiApp() builder .UseMauiApp() .UseMauiCommunityToolkit() - .UseTrimbleModus() + .UseTrimbleModus(new Resources.Styles.LightTheme(), new Resources.Styles.DarkTheme()) .ConfigureFonts(fonts => { fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular"); diff --git a/DemoApp/DemoApp/Resources/Styles/DarkTheme.xaml b/DemoApp/DemoApp/Resources/Styles/DarkTheme.xaml new file mode 100644 index 00000000..db8c72a6 --- /dev/null +++ b/DemoApp/DemoApp/Resources/Styles/DarkTheme.xaml @@ -0,0 +1,7 @@ + + + + green + diff --git a/DemoApp/DemoApp/Resources/Styles/DarkTheme.xaml.cs b/DemoApp/DemoApp/Resources/Styles/DarkTheme.xaml.cs new file mode 100644 index 00000000..6beb7088 --- /dev/null +++ b/DemoApp/DemoApp/Resources/Styles/DarkTheme.xaml.cs @@ -0,0 +1,9 @@ +namespace DemoApp.Resources.Styles; + +public partial class DarkTheme : ResourceDictionary +{ + public DarkTheme() + { + InitializeComponent(); + } +} \ No newline at end of file diff --git a/DemoApp/DemoApp/Resources/Styles/LightTheme.xaml b/DemoApp/DemoApp/Resources/Styles/LightTheme.xaml new file mode 100644 index 00000000..681d97c7 --- /dev/null +++ b/DemoApp/DemoApp/Resources/Styles/LightTheme.xaml @@ -0,0 +1,6 @@ + + + brown + diff --git a/DemoApp/DemoApp/Resources/Styles/LightTheme.xaml.cs b/DemoApp/DemoApp/Resources/Styles/LightTheme.xaml.cs new file mode 100644 index 00000000..dd55fb6b --- /dev/null +++ b/DemoApp/DemoApp/Resources/Styles/LightTheme.xaml.cs @@ -0,0 +1,9 @@ +namespace DemoApp.Resources.Styles; + +public partial class LightTheme : ResourceDictionary +{ + public LightTheme() + { + InitializeComponent(); + } +} \ No newline at end of file diff --git a/Trimble.Modus.Components/Helpers/ThemeManager.cs b/Trimble.Modus.Components/Helpers/ThemeManager.cs index ec89923c..bb150232 100644 --- a/Trimble.Modus.Components/Helpers/ThemeManager.cs +++ b/Trimble.Modus.Components/Helpers/ThemeManager.cs @@ -7,10 +7,59 @@ public class ThemeManager private static ResourceDictionary DarkThemeResourceDictionary { get; set; } internal static AppTheme CurrentTheme { get; private set; } - public static void Initialize() + public static void Initialize(ResourceDictionary lightTheme = null, ResourceDictionary darkTheme = null, bool useDarkThemeAsLightTheme = false) { - LightThemeResourceDictionary = new Styles.LightTheme(); - DarkThemeResourceDictionary = new Styles.DarkTheme(); + var defaultLightTheme = new Styles.LightTheme(); + var defaultDarkTheme = new Styles.DarkTheme(); + if (lightTheme != null && lightTheme.Count > 0) + { + LightThemeResourceDictionary = lightTheme; + var notExistsInDictionary = defaultLightTheme.Where(x => !lightTheme.ContainsKey(x.Key)).ToDictionary(x => x.Key, x => x.Value); + foreach (var item in notExistsInDictionary) + { + var keyExists = LightThemeResourceDictionary.ContainsKey(item.Key); + if (keyExists) + { + LightThemeResourceDictionary[item.Key] = item.Value; + } + else + { + LightThemeResourceDictionary.Add(item.Key, item.Value); + } + } + } + else + { + LightThemeResourceDictionary = defaultLightTheme; + } + if (useDarkThemeAsLightTheme) + { + DarkThemeResourceDictionary = LightThemeResourceDictionary; + } + else + { + if (darkTheme != null && darkTheme.Count > 0) + { + DarkThemeResourceDictionary = darkTheme; + var notExistsInDictionary = defaultDarkTheme.Where(x => !darkTheme.ContainsKey(x.Key)).ToDictionary(x => x.Key, x => x.Value); + foreach (var item in notExistsInDictionary) + { + var keyExists = DarkThemeResourceDictionary.ContainsKey(item.Key); + if (keyExists) + { + DarkThemeResourceDictionary[item.Key] = item.Value; + } + else + { + DarkThemeResourceDictionary.Add(item.Key, item.Value); + } + } + } + else + { + DarkThemeResourceDictionary = defaultDarkTheme; + } + } UpdateTheme(Application.Current.RequestedTheme); diff --git a/Trimble.Modus.Components/Hosting/AppBuilderExtensions.cs b/Trimble.Modus.Components/Hosting/AppBuilderExtensions.cs index 8551faba..3f06c6cc 100644 --- a/Trimble.Modus.Components/Hosting/AppBuilderExtensions.cs +++ b/Trimble.Modus.Components/Hosting/AppBuilderExtensions.cs @@ -1,6 +1,5 @@ using Microsoft.Maui.LifecycleEvents; using SkiaSharp.Views.Maui.Handlers; -using Trimble.Modus.Components; using Trimble.Modus.Components.Helpers; #if ANDROID using Trimble.Modus.Components.Popup.Pages; @@ -20,7 +19,10 @@ public static class AppBuilderExtensions /// generated by /// /// initialized for - public static MauiAppBuilder UseTrimbleModus(this MauiAppBuilder builder) + public static MauiAppBuilder UseTrimbleModus(this MauiAppBuilder builder, + ResourceDictionary lightTheme = null, + ResourceDictionary darkTheme = null, + bool useDarkThemeAsLightTheme = false) { builder .ConfigureLifecycleEvents(lifecycle => @@ -30,7 +32,7 @@ public static MauiAppBuilder UseTrimbleModus(this MauiAppBuilder builder) { d.OnApplicationCreate(del => { - ThemeManager.Initialize(); + ThemeManager.Initialize(lightTheme, darkTheme,useDarkThemeAsLightTheme); }); d.OnBackPressed(activity => Droid.Implementation.AndroidPopups.SendBackPressed()); }); @@ -39,7 +41,7 @@ public static MauiAppBuilder UseTrimbleModus(this MauiAppBuilder builder) { ios.FinishedLaunching((app, resources) => { - ThemeManager.Initialize(); + ThemeManager.Initialize(lightTheme, darkTheme,useDarkThemeAsLightTheme); return true; }); }); @@ -48,7 +50,7 @@ public static MauiAppBuilder UseTrimbleModus(this MauiAppBuilder builder) { mac.FinishedLaunching((app, resources) => { - ThemeManager.Initialize(); + ThemeManager.Initialize(lightTheme, darkTheme, useDarkThemeAsLightTheme); return true; }); }); @@ -56,13 +58,13 @@ public static MauiAppBuilder UseTrimbleModus(this MauiAppBuilder builder) lifecycle.AddWindows(windows => { windows.OnLaunched((window, args) => { - ThemeManager.Initialize(); + ThemeManager.Initialize(lightTheme, darkTheme); }); }); #endif }) - .ConfigureMauiHandlers(handlers => SetHandlers(handlers)) + .ConfigureMauiHandlers(SetHandlers) .ConfigureFonts(fonts => { fonts.AddFont("OpenSans-Bold.ttf", "OpenSansBold"); @@ -88,7 +90,7 @@ public static MauiAppBuilder UseTrimbleModus(this MauiAppBuilder builder, Action { #if ANDROID lifecycle.AddAndroid(d => - { + { d.OnBackPressed(activity => Droid.Implementation.AndroidPopups.SendBackPressed(backPressHandler)); }); #endif