Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
120 changes: 60 additions & 60 deletions .github/workflows/dotnet-desktop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@
# For a complete CI/CD sample to get started with GitHub Action workflows for Desktop Applications,
# refer to https://github.com/microsoft/github-actions-for-desktop-apps

# For more information on GitHub Actions, refer to https://github.com/features/actions
# For a complete CI/CD sample to get started with GitHub Action workflows for Desktop Applications,
# refer to https://github.com/microsoft/github-actions-for-desktop-apps

name: .NET Core Desktop

on:
Expand All @@ -45,71 +49,67 @@ on:
branches: [ "master" ]

jobs:

build:

strategy:
matrix:
configuration: [Debug, Release]

runs-on: windows-latest # For a list of available runner types, refer to
# https://help.github.com/en/actions/reference/workflow-syntax-for-github-actions#jobsjob_idruns-on
runs-on: windows-latest

env:
Solution_Name: your-solution-name # Replace with your solution name, i.e. MyWpfApp.sln.
Test_Project_Path: your-test-project-path # Replace with the path to your test project, i.e. MyWpfApp.Tests\MyWpfApp.Tests.csproj.
Wap_Project_Directory: your-wap-project-directory-name # Replace with the Wap project directory relative to the solution, i.e. MyWpfApp.Package.
Wap_Project_Path: your-wap-project-path # Replace with the path to your Wap project, i.e. MyWpf.App.Package\MyWpfApp.Package.wapproj.
Solution_Name: CpuLimiter.sln
Test_Project_Path: Avalonia.CpuLimitertTest\Avalonia.CpuLimitertTest.csproj
Wap_Project_Directory: Avalonia.CpuLimiter
Wap_Project_Path: Avalonia.CpuLimiter\Avalonia.CpuLimiter.csproj

steps:
- name: Checkout
uses: actions/checkout@v4
with:
fetch-depth: 0

# Install the .NET Core workload
- name: Install .NET Core
uses: actions/setup-dotnet@v4
with:
dotnet-version: 8.0.x

# Add MSBuild to the PATH: https://github.com/microsoft/setup-msbuild
- name: Setup MSBuild.exe
uses: microsoft/setup-msbuild@v2

# Execute all unit tests in the solution
- name: Execute unit tests
run: dotnet test

# Restore the application to populate the obj folder with RuntimeIdentifiers
- name: Restore the application
run: msbuild $env:Solution_Name /t:Restore /p:Configuration=$env:Configuration
env:
Configuration: ${{ matrix.configuration }}

# Decode the base 64 encoded pfx and save the Signing_Certificate
- name: Decode the pfx
run: |
$pfx_cert_byte = [System.Convert]::FromBase64String("${{ secrets.Base64_Encoded_Pfx }}")
$certificatePath = Join-Path -Path $env:Wap_Project_Directory -ChildPath GitHubActionsWorkflow.pfx
[IO.File]::WriteAllBytes("$certificatePath", $pfx_cert_byte)

# Create the app package by building and packaging the Windows Application Packaging project
- name: Create the app package
run: msbuild $env:Wap_Project_Path /p:Configuration=$env:Configuration /p:UapAppxPackageBuildMode=$env:Appx_Package_Build_Mode /p:AppxBundle=$env:Appx_Bundle /p:PackageCertificateKeyFile=GitHubActionsWorkflow.pfx /p:PackageCertificatePassword=${{ secrets.Pfx_Key }}
env:
Appx_Bundle: Always
Appx_Bundle_Platforms: x86|x64
Appx_Package_Build_Mode: StoreUpload
Configuration: ${{ matrix.configuration }}

# Remove the pfx
- name: Remove the pfx
run: Remove-Item -path $env:Wap_Project_Directory\GitHubActionsWorkflow.pfx

# Upload the MSIX package: https://github.com/marketplace/actions/upload-a-build-artifact
- name: Upload build artifacts
uses: actions/upload-artifact@v3
with:
name: MSIX Package
path: ${{ env.Wap_Project_Directory }}\AppPackages
- name: Checkout
uses: actions/checkout@v4
with:
fetch-depth: 0
# Install the .NET Core workload
- name: Install .NET Core
uses: actions/setup-dotnet@v4
with:
dotnet-version: 8.0.x
# Add MSBuild to the PATH: https://github.com/microsoft/setup-msbuild
- name: Setup MSBuild.exe
uses: microsoft/setup-msbuild@v2
# Execute all unit tests in the solution
- name: Execute unit tests
run: dotnet test ${{ env.Test_Project_Path }}
# Restore the application to populate the obj folder with RuntimeIdentifiers
- name: Restore the application
run: msbuild ${{ env.Solution_Name }} /t:Restore /p:Configuration=${{ matrix.configuration }}
env:
Configuration: ${{ matrix.configuration }}
# Decode the base 64 encoded pfx and save the Signing_Certificate
- name: Decode the pfx
run: |
$pfx_cert_byte = [System.Convert]::FromBase64String("${{ secrets.Base64_Encoded_Pfx }}")
$certificatePath = Join-Path -Path ${{ env.Wap_Project_Directory }} -ChildPath GitHubActionsWorkflow.pfx
[IO.File]::WriteAllBytes("$certificatePath", $pfx_cert_byte)
# Create the app package by building and packaging the Windows Application Packaging project
- name: Create the app package
run: msbuild ${{ env.Wap_Project_Path }} /p:Configuration=${{ matrix.configuration }} /p:UapAppxPackageBuildMode=$env:Appx_Package_Build_Mode /p:AppxBundle=$env:Appx_Bundle /p:PackageCertificateKeyFile=GitHubActionsWorkflow.pfx /p:PackageCertificatePassword=${{ secrets.Pfx_Key }}
env:
Appx_Bundle: Always
Appx_Bundle_Platforms: x86|x64
Appx_Package_Build_Mode: StoreUpload
Configuration: ${{ matrix.configuration }}
# Remove the pfx
- name: Remove the pfx
run: Remove-Item -path ${{ env.Wap_Project_Directory }}\GitHubActionsWorkflow.pfx
# Upload the MSIX package: https://github.com/marketplace/actions/upload-a-build-artifact
- name: Upload build artifacts
uses: actions/upload-artifact@v3
with:
name: MSIX Package
path: ${{ env.Wap_Project_Directory }}\AppPackages
16 changes: 9 additions & 7 deletions Avalonia.CpuLimiter/App.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="Avalonia.CpuLimiter.App"
xmlns:local="using:Avalonia.CpuLimiter"
xmlns:lang="using:Avalonia.CpuLimiter.Lang"
RequestedThemeVariant="Default">
<!-- "Default" ThemeVariant follows system theme variant. "Dark" or "Light" are other available options. -->

Expand All @@ -16,21 +17,22 @@

<Application.Resources>
<FontFamily x:Key="HarmonyOS Sans"> avares://Avalonia.CpuLimiter/Assets/Fonts#HarmonyOS Sans SC</FontFamily>
<FontFamily x:Key="Fira Code"> avares://Avalonia.CpuLimiter/Assets/Fonts2#Fira Code</FontFamily>
</Application.Resources>

<TrayIcon.Icons>
<TrayIcons>
<TrayIcon Icon="/Assets/avalonia-logo.ico"
ToolTipText="Game Launcher">
ToolTipText="Game Launcher"
Clicked="OnTrayIconClicked">
<TrayIcon.Menu>
<NativeMenu>
<NativeMenuItem Header="Open File"/>
<NativeMenuItem Header="Restore Window"/>
<NativeMenuItem Header="Launcher Program"/>
<NativeMenuItem Header="Settings"/>
<NativeMenuItem Click="OnRestoreButtonClicked" Header="{x:Static lang:Resources.TrayRestoreButton}">
</NativeMenuItem>
<NativeMenuItem Header="{x:Static lang:Resources.TrayOpenFileButton}" Click="OnOpenFileButtonClicked" />
<NativeMenuItem Header="{x:Static lang:Resources.TrayLaunchButton}" Click="OnLaunchProgramButtonClicked"/>
<NativeMenuItem Header="{x:Static lang:Resources.TrayOpenSettingButton}" Click="OnOpenSettingButtonClicked"/>
<NativeMenuItemSeparator />
<NativeMenuItem Header="Exit Program" Click="OnExitButtonClicked"/>
<NativeMenuItem Header="{x:Static lang:Resources.TrayExitButton}" Click="OnExitButtonClicked"/>
</NativeMenu>
</TrayIcon.Menu>
</TrayIcon>
Expand Down
119 changes: 72 additions & 47 deletions Avalonia.CpuLimiter/App.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
using Avalonia.CpuLimiter.Models;
using Avalonia.Media;


namespace Avalonia.CpuLimiter
{
public partial class App : Application
Expand All @@ -24,69 +23,40 @@
AvaloniaXamlLoader.Load(this);
}

public MyConfigModel ConfigModel { get; set; } = null!;
public MyConfigModel ConfigModel { get; set; }
public new static App? Current => Application.Current as App;

private MainWindowViewModel _mainWindowViewModel;

public ServiceProvider? Services { get; private set; }

private ServiceProvider ConfigureServices()
{
if (ApplicationLifetime is not IClassicDesktopStyleApplicationLifetime desktop)
throw new PlatformNotSupportedException();
var services = new ServiceCollection();
services.AddSingleton<IFilesService, FilesService>(_ => new FilesService(desktop));
services.AddSingleton<IClipBoardService, ClipBoardService>(_ => new ClipBoardService(desktop));
services.AddSingleton<IHistoryItemFileService, HistoryItemFileService>();

services.AddSingleton<MainWindowViewModel>();
services.AddSingleton<MainWindow>(sp =>
{
MainWindow mainWindow= new()
{
DataContext = sp.GetRequiredService<MainWindowViewModel>(),
RequestedThemeVariant = ConfigModel.ThemeVariantConfig,
};
mainWindow.MainBorder.Material.TintColor = ColorCollection[ConfigModel.ColorIndex].Color;
return mainWindow;
});

services.AddTransient<SettingWindowViewModel>();
services.AddTransient<SettingWindow>( _ =>
{
var settingWindow = new SettingWindow();

settingWindow.SettingBorder.Material.TintColor = ColorCollection[ConfigModel.ColorIndex].Color;
return settingWindow;
});

services.AddTransient<AboutWindow>(_ =>
{
var aboutWindow = new AboutWindow();
aboutWindow.RequestedThemeVariant = ConfigModel.ThemeVariantConfig;
aboutWindow.AboutBorder.Material.TintColor = ColorCollection[ConfigModel.ColorIndex].Color;
// to do theme related
return aboutWindow;
});
return services.BuildServiceProvider();
}

public override void OnFrameworkInitializationCompleted()
{
if(ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
{
//dependency injection and load config.json from file
Services = ConfigureServices();
// Services.Add
var services = new ServiceCollection();

services.AddSerilogConfiguration();

ConfigModel = ConfigFileService.LoadConfig();

this.Services = services.InitServices(ApplicationLifetime, ConfigModel);

if(!string.IsNullOrWhiteSpace(ConfigModel.StartupCultureConfig))
Lang.Resources.Culture = new CultureInfo(ConfigModel.StartupCultureConfig);

try
{
desktop.MainWindow = Services.GetService<MainWindow>();
_mainWindowViewModel = desktop.MainWindow.DataContext as MainWindowViewModel;

Check warning on line 53 in Avalonia.CpuLimiter/App.axaml.cs

View workflow job for this annotation

GitHub Actions / build (Release)

Dereference of a possibly null reference.

Check warning on line 53 in Avalonia.CpuLimiter/App.axaml.cs

View workflow job for this annotation

GitHub Actions / build (Release)

Possible null reference assignment.

Check warning on line 53 in Avalonia.CpuLimiter/App.axaml.cs

View workflow job for this annotation

GitHub Actions / build (Debug)

Dereference of a possibly null reference.

Check warning on line 53 in Avalonia.CpuLimiter/App.axaml.cs

View workflow job for this annotation

GitHub Actions / build (Debug)

Possible null reference assignment.

Check warning on line 53 in Avalonia.CpuLimiter/App.axaml.cs

View workflow job for this annotation

GitHub Actions / build (Release)

Dereference of a possibly null reference.

Check warning on line 53 in Avalonia.CpuLimiter/App.axaml.cs

View workflow job for this annotation

GitHub Actions / build (Release)

Possible null reference assignment.

// assign a lot delegate to mainwindow
_openFileFromMWVM += _mainWindowViewModel.ChooseExeFile;

Check warning on line 56 in Avalonia.CpuLimiter/App.axaml.cs

View workflow job for this annotation

GitHub Actions / build (Release)

Dereference of a possibly null reference.

Check warning on line 56 in Avalonia.CpuLimiter/App.axaml.cs

View workflow job for this annotation

GitHub Actions / build (Debug)

Dereference of a possibly null reference.

Check warning on line 56 in Avalonia.CpuLimiter/App.axaml.cs

View workflow job for this annotation

GitHub Actions / build (Release)

Dereference of a possibly null reference.
_launchProgramFromMWVM += _mainWindowViewModel.RunGame;
_OpenSettingFromMWVM += _mainWindowViewModel.OpenSettingWindow;

}
catch (Exception e)
{
Expand All @@ -96,13 +66,14 @@
desktop.ShutdownRequested += DesktopOnShutdownRequested;

ExitApplication += OnExitApplicationTriggered;

}
base.OnFrameworkInitializationCompleted();

if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime normal)
{
// init and load from config.json
LoadHistoryItemToMainVM();

Check warning on line 76 in Avalonia.CpuLimiter/App.axaml.cs

View workflow job for this annotation

GitHub Actions / build (Release)

Because this call is not awaited, execution of the current method continues before the call is completed. Consider applying the 'await' operator to the result of the call.

Check warning on line 76 in Avalonia.CpuLimiter/App.axaml.cs

View workflow job for this annotation

GitHub Actions / build (Debug)

Because this call is not awaited, execution of the current method continues before the call is completed. Consider applying the 'await' operator to the result of the call.

Check warning on line 76 in Avalonia.CpuLimiter/App.axaml.cs

View workflow job for this annotation

GitHub Actions / build (Release)

Because this call is not awaited, execution of the current method continues before the call is completed. Consider applying the 'await' operator to the result of the call.
}

}
Expand Down Expand Up @@ -149,13 +120,13 @@
_mainWindowViewModel.ResetIndexAndItems();
}
}
public async void OnExitButtonClicked(object? sender, EventArgs eventArgs)

private async void OnExitButtonClicked(object? sender, EventArgs eventArgs)
{
ExitApplication?.Invoke(this, EventArgs.Empty);
}

public void OnExitApplicationTriggered(object? sender, EventArgs eventArgs)
public async void OnExitApplicationTriggered(object? sender, EventArgs eventArgs)
{
if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
{
Expand All @@ -167,7 +138,59 @@
// the program calling this Event to exit;
// App.Current.ExitApplication.invoke
public event EventHandler? ExitApplication;

private Func<Task> _openFileFromMWVM;

private async void OnOpenFileButtonClicked(object? sender, EventArgs eventArgs)
{
if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
if (desktop.MainWindow != null)
desktop.MainWindow.WindowState = WindowState.Normal;
await _openFileFromMWVM();
}


private async void OnRestoreButtonClicked(object? sender, EventArgs eventArgs)
{
if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop && desktop.MainWindow != null)
desktop.MainWindow.Show();
}

private Func<Task> _launchProgramFromMWVM;

private async void OnLaunchProgramButtonClicked(object? sender, EventArgs eventArgs)
{
if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop && desktop.MainWindow != null)
_launchProgramFromMWVM();
}

private Func<Task> _OpenSettingFromMWVM;

private async void OnOpenSettingButtonClicked(object? sender, EventArgs eventArgs)
{
await _OpenSettingFromMWVM();
}

private DateTime? _lastTrayIconClicked;

private async void OnTrayIconClicked(object? sender, EventArgs eventArgs)
{
if (_lastTrayIconClicked == null)
{
_lastTrayIconClicked = DateTime.Now;
return;
}

if(DateTime.Now.Subtract((DateTime)_lastTrayIconClicked).TotalMilliseconds > 600)
_lastTrayIconClicked = DateTime.Now;
else
{
if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop && desktop.MainWindow != null)
desktop.MainWindow.Show();
_lastTrayIconClicked = null;
}
}

public Collection<CustomColor> ColorCollection { get; } = new Collection<CustomColor>
{
new CustomColor("#e95815"),
Expand All @@ -194,4 +217,6 @@
new CustomColor(Colors.LightSkyBlue.ToString())
};
}


}
Loading
Loading