-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f151c66
commit ac23225
Showing
16 changed files
with
742 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; | ||
//} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
Oops, something went wrong.