Skip to content

Commit

Permalink
Updated app startup logic to accelerate the first paint (#58)
Browse files Browse the repository at this point in the history
  • Loading branch information
lcarrere authored Dec 8, 2024
1 parent c7ee3b4 commit ec2cd0a
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 35 deletions.
71 changes: 37 additions & 34 deletions LM-Kit-Maestro/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@
using Microsoft.UI.Windowing;
#endif


using LMKit.Maestro.ViewModels;
using Microsoft.AspNetCore.Components.WebView.Maui;

[assembly: XamlCompilation(XamlCompilationOptions.Compile)]

namespace LMKit.Maestro
{
public partial class App : Application
Expand All @@ -15,7 +18,8 @@ public partial class App : Application
public App(AppShellViewModel appShellViewModel)
{
InitializeComponent();
LMKit.Global.Runtime.Initialize();

Task.Run(() => Global.Runtime.Initialize()); //Initialize LM-Kit in the background to avoid blocking UI initialization.

BlazorWebViewHandler.BlazorWebViewMapper.AppendToMapping("CustomBlazorWebView", (handler, view) =>
{
Expand All @@ -38,7 +42,6 @@ public App(AppShellViewModel appShellViewModel)
});

_appShellViewModel = appShellViewModel;
MainPage = new AppShell(appShellViewModel);
}

protected override async void OnStart()
Expand All @@ -51,17 +54,17 @@ protected override async void OnStart()

protected override Window CreateWindow(IActivationState? activationState)
{
Window window = base.CreateWindow(activationState);
Window window = new Window(new AppShell(_appShellViewModel));

#if WINDOWS
window.HandlerChanged += (sender, args) =>
{
var nativeWindow = window.Handler?.PlatformView as Microsoft.UI.Xaml.Window;
if (nativeWindow != null)
{
CustomizeTitleBar(nativeWindow);
}
};
window.HandlerChanged += (sender, args) =>
{
var nativeWindow = window.Handler?.PlatformView as Microsoft.UI.Xaml.Window;
if (nativeWindow != null)
{
CustomizeTitleBar(nativeWindow);
}
};
#endif

window.Destroying += OnAppWindowDestroying;
Expand All @@ -78,29 +81,29 @@ private void OnAppWindowDestroying(object? sender, EventArgs e)
}

#if WINDOWS
private void CustomizeTitleBar(Microsoft.UI.Xaml.Window nativeWindow)
{
var hwnd = WinRT.Interop.WindowNative.GetWindowHandle(nativeWindow);
var windowId = Win32Interop.GetWindowIdFromWindow(hwnd);
var appWindow = AppWindow.GetFromWindowId(windowId);

if (appWindow is not null && AppWindowTitleBar.IsCustomizationSupported())
{
var titleBar = appWindow.TitleBar;

// Set the minimize, maximize, and close button icon colors
titleBar.ButtonForegroundColor = Microsoft.UI.Colors.White;
titleBar.ButtonHoverForegroundColor = Microsoft.UI.Colors.White;
titleBar.ButtonPressedForegroundColor = Microsoft.UI.Colors.White;
titleBar.ButtonInactiveForegroundColor = Microsoft.UI.Colors.White;

// Set background colors for different states
titleBar.ButtonBackgroundColor = ColorHelper.FromArgb(255, 81, 43, 212);
titleBar.ButtonHoverBackgroundColor = ColorHelper.FromArgb(255, 100, 60, 230);
titleBar.ButtonPressedBackgroundColor = ColorHelper.FromArgb(255, 60, 30, 150);
titleBar.ButtonInactiveBackgroundColor = ColorHelper.FromArgb(255, 81, 43, 212);
}
}
private void CustomizeTitleBar(Microsoft.UI.Xaml.Window nativeWindow)
{
var hwnd = WinRT.Interop.WindowNative.GetWindowHandle(nativeWindow);
var windowId = Win32Interop.GetWindowIdFromWindow(hwnd);
var appWindow = AppWindow.GetFromWindowId(windowId);

if (appWindow is not null && AppWindowTitleBar.IsCustomizationSupported())
{
var titleBar = appWindow.TitleBar;

// Set the minimize, maximize, and close button icon colors
titleBar.ButtonForegroundColor = Microsoft.UI.Colors.White;
titleBar.ButtonHoverForegroundColor = Microsoft.UI.Colors.White;
titleBar.ButtonPressedForegroundColor = Microsoft.UI.Colors.White;
titleBar.ButtonInactiveForegroundColor = Microsoft.UI.Colors.White;

// Set background colors for different states
titleBar.ButtonBackgroundColor = ColorHelper.FromArgb(255, 81, 43, 212);
titleBar.ButtonHoverBackgroundColor = ColorHelper.FromArgb(255, 100, 60, 230);
titleBar.ButtonPressedBackgroundColor = ColorHelper.FromArgb(255, 60, 30, 150);
titleBar.ButtonInactiveBackgroundColor = ColorHelper.FromArgb(255, 81, 43, 212);
}
}
#endif
}
}
2 changes: 1 addition & 1 deletion LM-Kit-Maestro/ViewModels/Pages/AppShellViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public async Task Init()

if (_appSettingsService.LastLoadedModel != null)
{
TryLoadLastUsedModel();
_ = Task.Run(TryLoadLastUsedModel); //Loading model in the background to avoid blocking UI initialization.
}

// todo: we should ensure UI is loaded before starting loading a model with this call.
Expand Down

0 comments on commit ec2cd0a

Please sign in to comment.