Skip to content

Commit

Permalink
some updates on Uno UI
Browse files Browse the repository at this point in the history
  • Loading branch information
NoobNotFound committed Oct 6, 2024
1 parent f151c66 commit ac23225
Show file tree
Hide file tree
Showing 16 changed files with 742 additions and 9 deletions.
1 change: 1 addition & 0 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
<PackageVersion Include="CommunityToolkit.Mvvm" Version="8.2.2" />
<PackageVersion Include="CommunityToolkit.WinUI.UI.Animations" Version="7.1.2" />
<PackageVersion Include="CommunityToolkit.WinUI.UI.Controls" Version="7.1.2" />
<PackageVersion Include="Microsoft.Windows.CsWin32" Version="0.3.106" />
<PackageVersion Include="Microsoft.WindowsAppSDK" Version="1.5.240227000" />
<PackageVersion Include="Microsoft.Windows.SDK.BuildTools" Version="10.0.26031-preview" />
<PackageVersion Include="Octokit" Version="13.0.1" />
Expand Down
11 changes: 10 additions & 1 deletion Emerald/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public App()
this.InitializeComponent();
}

protected Window? MainWindow { get; private set; }
public Window? MainWindow { get; private set; }
protected IHost? Host { get; private set; }

protected override void OnLaunched(LaunchActivatedEventArgs args)
Expand All @@ -31,6 +31,7 @@ protected override void OnLaunched(LaunchActivatedEventArgs args)
);
MainWindow = builder.Window;


#if DEBUG
MainWindow.EnableHotReload();
#endif
Expand All @@ -48,6 +49,9 @@ protected override void OnLaunched(LaunchActivatedEventArgs args)
// Place the frame in the current Window
MainWindow.Content = rootFrame;
}
#if WINDOWS
var mica = Emerald.Uno.Helpers.WindowManager.IntializeWindow(MainWindow);
#endif

if (rootFrame.Content == null)
{
Expand All @@ -59,4 +63,9 @@ protected override void OnLaunched(LaunchActivatedEventArgs args)
// Ensure the current window is active
MainWindow.Activate();
}

/// <summary>
/// Gets the current <see cref="App"/> instance in use
/// </summary>
public new static App Current => (App)Application.Current;
}
Binary file added Emerald/Assets/icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
13 changes: 13 additions & 0 deletions Emerald/Emerald.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,20 @@
</UnoFeatures>
</PropertyGroup>
<ItemGroup>
<None Remove="Assets\icon.png" />
<None Remove="UserControls\Titlebar.xaml" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Windows.CsWin32">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="System.Text.Json" />
</ItemGroup>
<ItemGroup>
<Page Update="UserControls\Titlebar.xaml">
<Generator>MSBuild:Compile</Generator>
</Page>
</ItemGroup>

</Project>
155 changes: 155 additions & 0 deletions Emerald/Helpers/Extensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Microsoft.Windows.ApplicationModel.Resources;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using Windows.System.Diagnostics;

namespace Emerald.Uno.Helpers;

public static class Extensions
{
private static readonly ConcurrentDictionary<string, string> cachedResources = new();

//public static void ShowAt(this TeachingTip tip, FrameworkElement element, TeachingTipPlacementMode placement = TeachingTipPlacementMode.Auto, bool closeWhenClick = true, bool addToMainGrid = true)
//{
// if (addToMainGrid)
// (App.Current.MainWindow.Content as Grid).Children.Add(tip);

// tip.Target = element;
// tip.PreferredPlacement = placement;
// tip.IsOpen = true;
// if (closeWhenClick)
// {
// tip.ActionButtonClick += (_, _) => tip.IsOpen = false;
// tip.CloseButtonClick += (_, _) => tip.IsOpen = false;
// }
//}

public static int GetMemoryGB()
{
SystemMemoryUsageReport systemMemoryUsageReport = SystemDiagnosticInfo.GetForCurrentSystem().MemoryUsage.GetReport();

long memkb = Convert.ToInt64(systemMemoryUsageReport.TotalPhysicalSizeInBytes);
return Convert.ToInt32(memkb / Math.Pow(1024, 3));
}

public static string KiloFormat(this int num)
{
if (num >= 100000000)
return (num / 1000000).ToString("#,0M");

if (num >= 10000000)
return (num / 1000000).ToString("0.#") + "M";

if (num >= 100000)
return (num / 1000).ToString("#,0K");

if (num >= 1000)
return (num / 100).ToString("0.#") + "K";

return num.ToString("#,0");
}

//public static ContentDialog ToContentDialog(this UIElement content, string title, string closebtnText = null, ContentDialogButton defaultButton = ContentDialogButton.Close)
//{
// ContentDialog dialog = new()
// {
// XamlRoot = App.Current.MainWindow.Content.XamlRoot,
// Style = Application.Current.Resources["DefaultContentDialogStyle"] as Style,
// Title = title,
// CloseButtonText = closebtnText,
// DefaultButton = defaultButton,
// Content = content,
// RequestedTheme = (ElementTheme)Settings.SettingsSystem.Settings.App.Appearance.Theme
// };
// return dialog;
//}

public static int Remove<T>(this ObservableCollection<T> coll, Func<T, bool> condition)
{
var itemsToRemove = coll.Where(condition).ToList();

foreach (var itemToRemove in itemsToRemove)
{
coll.Remove(itemToRemove);
}
return itemsToRemove.Count;
}

public static int Remove<T>(this List<T> coll, Func<T, bool> condition)
{
var itemsToRemove = coll.Where(condition).ToList();

foreach (var itemToRemove in itemsToRemove)
{
coll.Remove(itemToRemove);
}

return itemsToRemove.Count;
}
public static void AddRange<T>(this ObservableCollection<T> cll, IEnumerable<T> items)
{
foreach (var item in items)
cll.Add(item);
}

public static string ToBinaryString(this string str)
{
var binary = "";
foreach (char ch in str)
{
binary += Convert.ToString((int)ch, 2);
}
return binary;
}

public static string ToMD5(this string s)
{
StringBuilder sb = new();
byte[] hashValue = MD5.HashData(Encoding.UTF8.GetBytes(s));

foreach (byte b in hashValue)
{
sb.Append($"{b:X2}");
}

return sb.ToString();
}

public static string Localize(this string resourceKey)
{
try
{
string s = Windows.ApplicationModel.Resources.ResourceLoader
.GetForViewIndependentUse()
.GetString(resourceKey);

return string.IsNullOrEmpty(s) ? resourceKey : s;
}
catch
{
return resourceKey;
}
}

//public static string Localize(this Core.Localized resourceKey) =>
// resourceKey.ToString().Localize();

//public static Models.Account ToAccount(this CmlLib.Core.Auth.MSession session, bool plusCount = true)
//{
// bool isOffline = session.AccessToken == "access_token";
// return new Models.Account(session.Username, isOffline ? null : session.AccessToken, isOffline ? null : session.UUID, plusCount ? MainWindow.HomePage.AccountsPage.AllCount++ : 0, false, session.ClientToken);
//}

//public static CmlLib.Core.Auth.MSession ToMSession(this Models.Account account)
//{
// bool isOffline = account.UUID == null;
// return new CmlLib.Core.Auth.MSession(account.UserName, isOffline ? "access_token" : account.AccessToken, isOffline ? Guid.NewGuid().ToString().Replace("-", "") : account.UUID) { ClientToken = account.ClientToken };
//}
}
21 changes: 21 additions & 0 deletions Emerald/Helpers/MarkupExtensions/ConditionString.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using Microsoft.UI.Xaml.Markup;

namespace Emerald.Uno.Helpers;

[MarkupExtensionReturnType(ReturnType = typeof(string))]
public class ConditionString : MarkupExtension
{
public string TrueString { get; set; }

public string FalseString { get; set; }

public bool Condition { get; set; }

public string Result
=> Condition ? TrueString : FalseString;

public override string ToString()
=> Result;

protected override object ProvideValue() => Result;
}
14 changes: 14 additions & 0 deletions Emerald/Helpers/MarkupExtensions/FontIcon.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using Microsoft.UI.Xaml.Markup;

namespace Emerald.Uno.Helpers;

[MarkupExtensionReturnType(ReturnType = typeof(Microsoft.UI.Xaml.Controls.FontIcon))]
public sealed class FontIcon : MarkupExtension
{
public string Glyph { get; set; }

public int FontSize { get; set; } = 16;

protected override object ProvideValue()
=> new Microsoft.UI.Xaml.Controls.FontIcon() { Glyph = Glyph, FontSize = FontSize };
}
12 changes: 12 additions & 0 deletions Emerald/Helpers/MarkupExtensions/LocalizeString.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using Microsoft.UI.Xaml.Markup;
using CommunityToolkit.Mvvm;
namespace Emerald.Uno.Helpers;

[MarkupExtensionReturnType(ReturnType = typeof(string))]
public sealed class Localize : MarkupExtension
{
public string Name { get; set; }

protected override object ProvideValue()
=> Name.Localize();
}
Loading

0 comments on commit ac23225

Please sign in to comment.