Skip to content

Commit

Permalink
To support custom themes
Browse files Browse the repository at this point in the history
  • Loading branch information
Sofiya-kumar committed May 6, 2024
1 parent 13925ea commit 5661c4d
Show file tree
Hide file tree
Showing 8 changed files with 100 additions and 12 deletions.
6 changes: 6 additions & 0 deletions DemoApp/DemoApp/DemoApp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,12 @@
</ItemGroup>

<ItemGroup>
<MauiXaml Update="Resources\Styles\DarkTheme.xaml">
<Generator>MSBuild:Compile</Generator>
</MauiXaml>
<MauiXaml Update="Resources\Styles\LightTheme.xaml">
<Generator>MSBuild:Compile</Generator>
</MauiXaml>
<MauiXaml Update="Views\SliderSamplePage.xaml">
<Generator>MSBuild:Compile</Generator>
</MauiXaml>
Expand Down
2 changes: 1 addition & 1 deletion DemoApp/DemoApp/MauiProgram.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public static MauiApp CreateMauiApp()
builder
.UseMauiApp<App>()
.UseMauiCommunityToolkit()
.UseTrimbleModus()
.UseTrimbleModus(new Resources.Styles.LightTheme(), new Resources.Styles.DarkTheme())
.ConfigureFonts(fonts =>
{
fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
Expand Down
7 changes: 7 additions & 0 deletions DemoApp/DemoApp/Resources/Styles/DarkTheme.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="utf-8" ?>
<ResourceDictionary xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="DemoApp.Resources.Styles.DarkTheme">

<Color x:Key="PrimaryFillButtonBackgroundColor">green</Color>
</ResourceDictionary>
9 changes: 9 additions & 0 deletions DemoApp/DemoApp/Resources/Styles/DarkTheme.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace DemoApp.Resources.Styles;

public partial class DarkTheme : ResourceDictionary
{
public DarkTheme()
{
InitializeComponent();
}
}
6 changes: 6 additions & 0 deletions DemoApp/DemoApp/Resources/Styles/LightTheme.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8" ?>
<ResourceDictionary xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="DemoApp.Resources.Styles.LightTheme">
<Color x:Key="PrimaryFillButtonBackgroundColor">brown</Color>
</ResourceDictionary>
9 changes: 9 additions & 0 deletions DemoApp/DemoApp/Resources/Styles/LightTheme.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace DemoApp.Resources.Styles;

public partial class LightTheme : ResourceDictionary
{
public LightTheme()
{
InitializeComponent();
}
}
55 changes: 52 additions & 3 deletions Trimble.Modus.Components/Helpers/ThemeManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
18 changes: 10 additions & 8 deletions Trimble.Modus.Components/Hosting/AppBuilderExtensions.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -20,7 +19,10 @@ public static class AppBuilderExtensions
/// <param name="builder"><see cref="MauiAppBuilder"/> generated by <see cref="MauiApp"/> </param>
/// <param name="options"><see cref="Options"/></param>
/// <returns><see cref="MauiAppBuilder"/> initialized for <see cref="CommunityToolkit.Maui"/></returns>
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 =>
Expand All @@ -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());
});
Expand All @@ -39,7 +41,7 @@ public static MauiAppBuilder UseTrimbleModus(this MauiAppBuilder builder)
{
ios.FinishedLaunching((app, resources) =>
{
ThemeManager.Initialize();
ThemeManager.Initialize(lightTheme, darkTheme,useDarkThemeAsLightTheme);
return true;
});
});
Expand All @@ -48,21 +50,21 @@ public static MauiAppBuilder UseTrimbleModus(this MauiAppBuilder builder)
{
mac.FinishedLaunching((app, resources) =>
{
ThemeManager.Initialize();
ThemeManager.Initialize(lightTheme, darkTheme, useDarkThemeAsLightTheme);
return true;
});
});
#elif WINDOWS
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");
Expand All @@ -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
Expand Down

0 comments on commit 5661c4d

Please sign in to comment.