Ionite
1 year ago
committed by
GitHub
29 changed files with 1702 additions and 510 deletions
@ -0,0 +1,408 @@
|
||||
// Modified from https://github.com/AvaloniaUI/AvaloniaAutoGrid |
||||
/*The MIT License (MIT) |
||||
|
||||
Copyright (c) 2013 Charles Brown (carbonrobot) |
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy of |
||||
this software and associated documentation files (the "Software"), to deal in |
||||
the Software without restriction, including without limitation the rights to |
||||
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of |
||||
the Software, and to permit persons to whom the Software is furnished to do so, |
||||
subject to the following conditions: |
||||
|
||||
The above copyright notice and this permission notice shall be included in all |
||||
copies or substantial portions of the Software. |
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS |
||||
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR |
||||
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER |
||||
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
||||
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.*/ |
||||
|
||||
using System; |
||||
using System.ComponentModel; |
||||
using System.Diagnostics.CodeAnalysis; |
||||
using System.Linq; |
||||
using Avalonia; |
||||
using Avalonia.Controls; |
||||
using Avalonia.Data; |
||||
using Avalonia.Layout; |
||||
|
||||
namespace StabilityMatrix.Avalonia.Controls; |
||||
|
||||
/// <summary> |
||||
/// Defines a flexible grid area that consists of columns and rows. |
||||
/// Depending on the orientation, either the rows or the columns are auto-generated, |
||||
/// and the children's position is set according to their index. |
||||
/// </summary> |
||||
[SuppressMessage("ReSharper", "MemberCanBePrivate.Global")] |
||||
public class AutoGrid : Grid |
||||
{ |
||||
/// <summary> |
||||
/// Gets or sets the child horizontal alignment. |
||||
/// </summary> |
||||
/// <value>The child horizontal alignment.</value> |
||||
[Category("Layout"), Description("Presets the horizontal alignment of all child controls")] |
||||
public HorizontalAlignment? ChildHorizontalAlignment |
||||
{ |
||||
get => (HorizontalAlignment?)GetValue(ChildHorizontalAlignmentProperty); |
||||
set => SetValue(ChildHorizontalAlignmentProperty, value); |
||||
} |
||||
|
||||
/// <summary> |
||||
/// Gets or sets the child margin. |
||||
/// </summary> |
||||
/// <value>The child margin.</value> |
||||
[Category("Layout"), Description("Presets the margin of all child controls")] |
||||
public Thickness? ChildMargin |
||||
{ |
||||
get => (Thickness?)GetValue(ChildMarginProperty); |
||||
set => SetValue(ChildMarginProperty, value); |
||||
} |
||||
|
||||
/// <summary> |
||||
/// Gets or sets the child vertical alignment. |
||||
/// </summary> |
||||
/// <value>The child vertical alignment.</value> |
||||
[Category("Layout"), Description("Presets the vertical alignment of all child controls")] |
||||
public VerticalAlignment? ChildVerticalAlignment |
||||
{ |
||||
get => (VerticalAlignment?)GetValue(ChildVerticalAlignmentProperty); |
||||
set => SetValue(ChildVerticalAlignmentProperty, value); |
||||
} |
||||
|
||||
/// <summary> |
||||
/// Gets or sets the column count |
||||
/// </summary> |
||||
[Category("Layout"), Description("Defines a set number of columns")] |
||||
public int ColumnCount |
||||
{ |
||||
get => (int)GetValue(ColumnCountProperty)!; |
||||
set => SetValue(ColumnCountProperty, value); |
||||
} |
||||
|
||||
/// <summary> |
||||
/// Gets or sets the fixed column width |
||||
/// </summary> |
||||
[Category("Layout"), Description("Presets the width of all columns set using the ColumnCount property")] |
||||
|
||||
public GridLength ColumnWidth |
||||
{ |
||||
get => (GridLength)GetValue(ColumnWidthProperty)!; |
||||
set => SetValue(ColumnWidthProperty, value); |
||||
} |
||||
|
||||
/// <summary> |
||||
/// Gets or sets a value indicating whether the children are automatically indexed. |
||||
/// <remarks> |
||||
/// The default is <c>true</c>. |
||||
/// Note that if children are already indexed, setting this property to <c>false</c> will not remove their indices. |
||||
/// </remarks> |
||||
/// </summary> |
||||
[Category("Layout"), Description("Set to false to disable the auto layout functionality")] |
||||
public bool IsAutoIndexing |
||||
{ |
||||
get => (bool)GetValue(IsAutoIndexingProperty)!; |
||||
set => SetValue(IsAutoIndexingProperty, value); |
||||
} |
||||
|
||||
/// <summary> |
||||
/// Gets or sets the orientation. |
||||
/// <remarks>The default is Vertical.</remarks> |
||||
/// </summary> |
||||
/// <value>The orientation.</value> |
||||
[Category("Layout"), Description("Defines the directionality of the autolayout. Use vertical for a column first layout, horizontal for a row first layout.")] |
||||
public Orientation Orientation |
||||
{ |
||||
get => (Orientation)GetValue(OrientationProperty)!; |
||||
set => SetValue(OrientationProperty, value); |
||||
} |
||||
|
||||
/// <summary> |
||||
/// Gets or sets the number of rows |
||||
/// </summary> |
||||
[Category("Layout"), Description("Defines a set number of rows")] |
||||
public int RowCount |
||||
{ |
||||
get => (int)GetValue(RowCountProperty)!; |
||||
set => SetValue(RowCountProperty, value); |
||||
} |
||||
|
||||
/// <summary> |
||||
/// Gets or sets the fixed row height |
||||
/// </summary> |
||||
[Category("Layout"), Description("Presets the height of all rows set using the RowCount property")] |
||||
public GridLength RowHeight |
||||
{ |
||||
get => (GridLength)GetValue(RowHeightProperty)!; |
||||
set => SetValue(RowHeightProperty, value); |
||||
} |
||||
|
||||
/// <summary> |
||||
/// Handles the column count changed event |
||||
/// </summary> |
||||
public static void ColumnCountChanged(AvaloniaPropertyChangedEventArgs e) |
||||
{ |
||||
if ((int)e.NewValue! < 0) |
||||
return; |
||||
|
||||
var grid = (AutoGrid)e.Sender; |
||||
|
||||
|
||||
// look for an existing column definition for the height |
||||
var width = grid.ColumnWidth; |
||||
if (!grid.IsSet(ColumnWidthProperty) && grid.ColumnDefinitions.Count > 0) |
||||
width = grid.ColumnDefinitions[0].Width; |
||||
|
||||
// clear and rebuild |
||||
grid.ColumnDefinitions.Clear(); |
||||
for (var i = 0; i < (int)e.NewValue; i++) |
||||
grid.ColumnDefinitions.Add( |
||||
new ColumnDefinition() { Width = width }); |
||||
} |
||||
|
||||
/// <summary> |
||||
/// Handle the fixed column width changed event |
||||
/// </summary> |
||||
public static void FixedColumnWidthChanged(AvaloniaPropertyChangedEventArgs e) |
||||
{ |
||||
var grid = (AutoGrid)e.Sender; |
||||
|
||||
// add a default column if missing |
||||
if (grid.ColumnDefinitions.Count == 0) |
||||
grid.ColumnDefinitions.Add(new ColumnDefinition()); |
||||
|
||||
// set all existing columns to this width |
||||
foreach (var t in grid.ColumnDefinitions) |
||||
t.Width = (GridLength)e.NewValue!; |
||||
} |
||||
|
||||
/// <summary> |
||||
/// Handle the fixed row height changed event |
||||
/// </summary> |
||||
public static void FixedRowHeightChanged(AvaloniaPropertyChangedEventArgs e) |
||||
{ |
||||
var grid = (AutoGrid)e.Sender; |
||||
|
||||
// add a default row if missing |
||||
if (grid.RowDefinitions.Count == 0) |
||||
grid.RowDefinitions.Add(new RowDefinition()); |
||||
|
||||
// set all existing rows to this height |
||||
foreach (var t in grid.RowDefinitions) |
||||
t.Height = (GridLength)e.NewValue!; |
||||
} |
||||
|
||||
/// <summary> |
||||
/// Handles the row count changed event |
||||
/// </summary> |
||||
public static void RowCountChanged(AvaloniaPropertyChangedEventArgs e) |
||||
{ |
||||
if ((int)e.NewValue! < 0) |
||||
return; |
||||
|
||||
var grid = (AutoGrid)e.Sender; |
||||
|
||||
// look for an existing row to get the height |
||||
var height = grid.RowHeight; |
||||
if (!grid.IsSet(RowHeightProperty) && grid.RowDefinitions.Count > 0) |
||||
height = grid.RowDefinitions[0].Height; |
||||
|
||||
// clear and rebuild |
||||
grid.RowDefinitions.Clear(); |
||||
for (var i = 0; i < (int)e.NewValue; i++) |
||||
grid.RowDefinitions.Add( |
||||
new RowDefinition() { Height = height }); |
||||
} |
||||
|
||||
/// <summary> |
||||
/// Called when [child horizontal alignment changed]. |
||||
/// </summary> |
||||
private static void OnChildHorizontalAlignmentChanged(AvaloniaPropertyChangedEventArgs e) |
||||
{ |
||||
var grid = (AutoGrid)e.Sender; |
||||
foreach (var child in grid.Children) |
||||
{ |
||||
child.SetValue(HorizontalAlignmentProperty, |
||||
grid.ChildHorizontalAlignment ?? AvaloniaProperty.UnsetValue); |
||||
} |
||||
} |
||||
|
||||
/// <summary> |
||||
/// Called when [child layout changed]. |
||||
/// </summary> |
||||
private static void OnChildMarginChanged(AvaloniaPropertyChangedEventArgs e) |
||||
{ |
||||
var grid = (AutoGrid)e.Sender; |
||||
foreach (var child in grid.Children) |
||||
{ |
||||
child.SetValue(MarginProperty, grid.ChildMargin ?? AvaloniaProperty.UnsetValue); |
||||
} |
||||
} |
||||
|
||||
/// <summary> |
||||
/// Called when [child vertical alignment changed]. |
||||
/// </summary> |
||||
private static void OnChildVerticalAlignmentChanged(AvaloniaPropertyChangedEventArgs e) |
||||
{ |
||||
var grid = (AutoGrid)e.Sender; |
||||
foreach (var child in grid.Children) |
||||
{ |
||||
child.SetValue(VerticalAlignmentProperty, grid.ChildVerticalAlignment ?? AvaloniaProperty.UnsetValue); |
||||
} |
||||
} |
||||
|
||||
/// <summary> |
||||
/// Apply child margins and layout effects such as alignment |
||||
/// </summary> |
||||
private void ApplyChildLayout(Control child) |
||||
{ |
||||
if (ChildMargin != null) |
||||
{ |
||||
child.SetValue(MarginProperty, ChildMargin.Value, BindingPriority.Template); |
||||
} |
||||
if (ChildHorizontalAlignment != null) |
||||
{ |
||||
child.SetValue(HorizontalAlignmentProperty, ChildHorizontalAlignment.Value, BindingPriority.Template); |
||||
} |
||||
if (ChildVerticalAlignment != null) |
||||
{ |
||||
child.SetValue(VerticalAlignmentProperty, ChildVerticalAlignment.Value, BindingPriority.Template); |
||||
} |
||||
} |
||||
|
||||
/// <summary> |
||||
/// Clamp a value to its maximum. |
||||
/// </summary> |
||||
private int Clamp(int value, int max) |
||||
{ |
||||
return (value > max) ? max : value; |
||||
} |
||||
|
||||
/// <summary> |
||||
/// Perform the grid layout of row and column indexes |
||||
/// </summary> |
||||
private void PerformLayout() |
||||
{ |
||||
var fillRowFirst = Orientation == Orientation.Horizontal; |
||||
var rowCount = RowDefinitions.Count; |
||||
var colCount = ColumnDefinitions.Count; |
||||
|
||||
if (rowCount == 0 || colCount == 0) |
||||
return; |
||||
|
||||
var position = 0; |
||||
var skip = new bool[rowCount, colCount]; |
||||
foreach (var child in Children.OfType<Control>()) |
||||
{ |
||||
var childIsCollapsed = !child.IsVisible; |
||||
if (IsAutoIndexing && !childIsCollapsed) |
||||
{ |
||||
if (fillRowFirst) |
||||
{ |
||||
var row = Clamp(position / colCount, rowCount - 1); |
||||
var col = Clamp(position % colCount, colCount - 1); |
||||
if (skip[row, col]) |
||||
{ |
||||
position++; |
||||
row = (position / colCount); |
||||
col = (position % colCount); |
||||
} |
||||
|
||||
SetRow(child, row); |
||||
SetColumn(child, col); |
||||
position += GetColumnSpan(child); |
||||
|
||||
var offset = GetRowSpan(child) - 1; |
||||
while (offset > 0) |
||||
{ |
||||
skip[row + offset--, col] = true; |
||||
} |
||||
} |
||||
else |
||||
{ |
||||
var row = Clamp(position % rowCount, rowCount - 1); |
||||
var col = Clamp(position / rowCount, colCount - 1); |
||||
if (skip[row, col]) |
||||
{ |
||||
position++; |
||||
row = position % rowCount; |
||||
col = position / rowCount; |
||||
} |
||||
|
||||
SetRow(child, row); |
||||
SetColumn(child, col); |
||||
position += GetRowSpan(child); |
||||
|
||||
var offset = GetColumnSpan(child) - 1; |
||||
while (offset > 0) |
||||
{ |
||||
skip[row, col + offset--] = true; |
||||
} |
||||
} |
||||
} |
||||
|
||||
ApplyChildLayout(child); |
||||
} |
||||
} |
||||
|
||||
public static readonly AvaloniaProperty<HorizontalAlignment?> ChildHorizontalAlignmentProperty = |
||||
AvaloniaProperty.Register<AutoGrid, HorizontalAlignment?>("ChildHorizontalAlignment"); |
||||
|
||||
public static readonly AvaloniaProperty<Thickness?> ChildMarginProperty = |
||||
AvaloniaProperty.Register<AutoGrid, Thickness?>("ChildMargin"); |
||||
|
||||
public static readonly AvaloniaProperty<VerticalAlignment?> ChildVerticalAlignmentProperty = |
||||
AvaloniaProperty.Register<AutoGrid, VerticalAlignment?>("ChildVerticalAlignment"); |
||||
|
||||
public static readonly AvaloniaProperty<int> ColumnCountProperty = |
||||
AvaloniaProperty.RegisterAttached<Control, int>("ColumnCount", typeof(AutoGrid), 1); |
||||
|
||||
public static readonly AvaloniaProperty<GridLength> ColumnWidthProperty = |
||||
AvaloniaProperty.RegisterAttached<Control, GridLength>("ColumnWidth", typeof(AutoGrid), GridLength.Auto); |
||||
|
||||
public static readonly AvaloniaProperty<bool> IsAutoIndexingProperty = |
||||
AvaloniaProperty.Register<AutoGrid, bool>("IsAutoIndexing", true); |
||||
|
||||
public static readonly AvaloniaProperty<Orientation> OrientationProperty = |
||||
AvaloniaProperty.Register<AutoGrid, Orientation>("Orientation", Orientation.Vertical); |
||||
|
||||
public static readonly AvaloniaProperty<int> RowCountProperty = |
||||
AvaloniaProperty.RegisterAttached<Control, int>("RowCount", typeof(AutoGrid), 1); |
||||
|
||||
public static readonly AvaloniaProperty<GridLength> RowHeightProperty = |
||||
AvaloniaProperty.RegisterAttached<Control, GridLength>("RowHeight", typeof(AutoGrid), GridLength.Auto); |
||||
|
||||
static AutoGrid() |
||||
{ |
||||
AffectsMeasure<AutoGrid>(ChildHorizontalAlignmentProperty, ChildMarginProperty, |
||||
ChildVerticalAlignmentProperty, ColumnCountProperty, ColumnWidthProperty, IsAutoIndexingProperty, OrientationProperty, |
||||
RowHeightProperty); |
||||
|
||||
ChildHorizontalAlignmentProperty.Changed.Subscribe(OnChildHorizontalAlignmentChanged); |
||||
ChildMarginProperty.Changed.Subscribe(OnChildMarginChanged); |
||||
ChildVerticalAlignmentProperty.Changed.Subscribe(OnChildVerticalAlignmentChanged); |
||||
ColumnCountProperty.Changed.Subscribe(ColumnCountChanged); |
||||
RowCountProperty.Changed.Subscribe(RowCountChanged); |
||||
ColumnWidthProperty.Changed.Subscribe(FixedColumnWidthChanged); |
||||
RowHeightProperty.Changed.Subscribe(FixedRowHeightChanged); |
||||
} |
||||
|
||||
#region Overrides |
||||
|
||||
/// <summary> |
||||
/// Measures the children of a <see cref="T:System.Windows.Controls.Grid"/> in anticipation of arranging them during the <see cref="M:ArrangeOverride"/> pass. |
||||
/// </summary> |
||||
/// <param name="constraint">Indicates an upper limit size that should not be exceeded.</param> |
||||
/// <returns> |
||||
/// <see cref="Size"/> that represents the required size to arrange child content. |
||||
/// </returns> |
||||
protected override Size MeasureOverride(Size constraint) |
||||
{ |
||||
PerformLayout(); |
||||
return base.MeasureOverride(constraint); |
||||
} |
||||
|
||||
#endregion Overrides |
||||
} |
@ -0,0 +1,52 @@
|
||||
using System.Collections.Generic; |
||||
using System.Collections.Immutable; |
||||
using System.Diagnostics.CodeAnalysis; |
||||
using System.Globalization; |
||||
|
||||
namespace StabilityMatrix.Avalonia.Languages; |
||||
|
||||
[SuppressMessage("ReSharper", "MemberCanBePrivate.Global")] |
||||
public static class Cultures |
||||
{ |
||||
public static CultureInfo Default { get; } = new("en-US"); |
||||
|
||||
public static CultureInfo Current => Resources.Culture; |
||||
|
||||
public static readonly Dictionary<string, CultureInfo> SupportedCulturesByCode = |
||||
new Dictionary<string, CultureInfo> |
||||
{ |
||||
["en-US"] = Default, |
||||
["ja-JP"] = new("ja-JP") |
||||
}; |
||||
|
||||
public static IReadOnlyList<CultureInfo> SupportedCultures |
||||
=> SupportedCulturesByCode.Values.ToImmutableList(); |
||||
|
||||
public static CultureInfo GetSupportedCultureOrDefault(string? cultureCode) |
||||
{ |
||||
if (cultureCode is null |
||||
|| !SupportedCulturesByCode.TryGetValue(cultureCode, out var culture)) |
||||
{ |
||||
return Default; |
||||
} |
||||
|
||||
return culture; |
||||
} |
||||
|
||||
public static bool TrySetSupportedCulture(string? cultureCode) |
||||
{ |
||||
if (cultureCode is null |
||||
|| !SupportedCulturesByCode.TryGetValue(cultureCode, out var culture)) |
||||
{ |
||||
return false; |
||||
} |
||||
|
||||
Resources.Culture = culture; |
||||
return true; |
||||
} |
||||
|
||||
public static bool TrySetSupportedCulture(CultureInfo? cultureInfo) |
||||
{ |
||||
return cultureInfo is not null && TrySetSupportedCulture(cultureInfo.Name); |
||||
} |
||||
} |
@ -0,0 +1,143 @@
|
||||
//------------------------------------------------------------------------------ |
||||
// <auto-generated> |
||||
// This code was generated by a tool. |
||||
// |
||||
// Changes to this file may cause incorrect behavior and will be lost if |
||||
// the code is regenerated. |
||||
// </auto-generated> |
||||
//------------------------------------------------------------------------------ |
||||
|
||||
namespace StabilityMatrix.Avalonia.Languages { |
||||
using System; |
||||
|
||||
|
||||
/// <summary> |
||||
/// A strongly-typed resource class, for looking up localized strings, etc. |
||||
/// </summary> |
||||
// This class was auto-generated by the StronglyTypedResourceBuilder |
||||
// class via a tool like ResGen or Visual Studio. |
||||
// To add or remove a member, edit your .ResX file then rerun ResGen |
||||
// with the /str option, or rebuild your VS project. |
||||
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "4.0.0.0")] |
||||
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()] |
||||
[global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] |
||||
public class Resources { |
||||
|
||||
private static global::System.Resources.ResourceManager resourceMan; |
||||
|
||||
private static global::System.Globalization.CultureInfo resourceCulture; |
||||
|
||||
[global::System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")] |
||||
internal Resources() { |
||||
} |
||||
|
||||
/// <summary> |
||||
/// Returns the cached ResourceManager instance used by this class. |
||||
/// </summary> |
||||
[global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] |
||||
public static global::System.Resources.ResourceManager ResourceManager { |
||||
get { |
||||
if (object.ReferenceEquals(resourceMan, null)) { |
||||
global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("StabilityMatrix.Avalonia.Languages.Resources", typeof(Resources).Assembly); |
||||
resourceMan = temp; |
||||
} |
||||
return resourceMan; |
||||
} |
||||
} |
||||
|
||||
/// <summary> |
||||
/// Overrides the current thread's CurrentUICulture property for all |
||||
/// resource lookups using this strongly typed resource class. |
||||
/// </summary> |
||||
[global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] |
||||
public static global::System.Globalization.CultureInfo Culture { |
||||
get { |
||||
return resourceCulture; |
||||
} |
||||
set { |
||||
resourceCulture = value; |
||||
} |
||||
} |
||||
|
||||
/// <summary> |
||||
/// Looks up a localized string similar to Cancel. |
||||
/// </summary> |
||||
public static string Action_Cancel { |
||||
get { |
||||
return ResourceManager.GetString("Action_Cancel", resourceCulture); |
||||
} |
||||
} |
||||
|
||||
/// <summary> |
||||
/// Looks up a localized string similar to Launch. |
||||
/// </summary> |
||||
public static string Action_Launch { |
||||
get { |
||||
return ResourceManager.GetString("Action_Launch", resourceCulture); |
||||
} |
||||
} |
||||
|
||||
/// <summary> |
||||
/// Looks up a localized string similar to Quit. |
||||
/// </summary> |
||||
public static string Action_Quit { |
||||
get { |
||||
return ResourceManager.GetString("Action_Quit", resourceCulture); |
||||
} |
||||
} |
||||
|
||||
/// <summary> |
||||
/// Looks up a localized string similar to Relaunch. |
||||
/// </summary> |
||||
public static string Action_Relaunch { |
||||
get { |
||||
return ResourceManager.GetString("Action_Relaunch", resourceCulture); |
||||
} |
||||
} |
||||
|
||||
/// <summary> |
||||
/// Looks up a localized string similar to Relaunch Later. |
||||
/// </summary> |
||||
public static string Action_RelaunchLater { |
||||
get { |
||||
return ResourceManager.GetString("Action_RelaunchLater", resourceCulture); |
||||
} |
||||
} |
||||
|
||||
/// <summary> |
||||
/// Looks up a localized string similar to Save. |
||||
/// </summary> |
||||
public static string Action_Save { |
||||
get { |
||||
return ResourceManager.GetString("Action_Save", resourceCulture); |
||||
} |
||||
} |
||||
|
||||
/// <summary> |
||||
/// Looks up a localized string similar to Language. |
||||
/// </summary> |
||||
public static string Label_Language { |
||||
get { |
||||
return ResourceManager.GetString("Label_Language", resourceCulture); |
||||
} |
||||
} |
||||
|
||||
/// <summary> |
||||
/// Looks up a localized string similar to Relaunch Required. |
||||
/// </summary> |
||||
public static string Label_RelaunchRequired { |
||||
get { |
||||
return ResourceManager.GetString("Label_RelaunchRequired", resourceCulture); |
||||
} |
||||
} |
||||
|
||||
/// <summary> |
||||
/// Looks up a localized string similar to Relaunch is required for new language option to take effect. |
||||
/// </summary> |
||||
public static string Text_RelaunchRequiredToApplyLanguage { |
||||
get { |
||||
return ResourceManager.GetString("Text_RelaunchRequiredToApplyLanguage", resourceCulture); |
||||
} |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,23 @@
|
||||
<root> |
||||
<resheader name="resmimetype"> |
||||
<value>text/microsoft-resx</value> |
||||
</resheader> |
||||
<resheader name="version"> |
||||
<value>1.3</value> |
||||
</resheader> |
||||
<resheader name="reader"> |
||||
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value> |
||||
</resheader> |
||||
<resheader name="writer"> |
||||
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value> |
||||
</resheader> |
||||
<data name="Action_Save" xml:space="preserve"> |
||||
<value>保存</value> |
||||
</data> |
||||
<data name="Action_Cancel" xml:space="preserve"> |
||||
<value>戻る</value> |
||||
</data> |
||||
<data name="Label_Language" xml:space="preserve"> |
||||
<value>言語</value> |
||||
</data> |
||||
</root> |
@ -0,0 +1,48 @@
|
||||
<?xml version="1.0" encoding="utf-8"?> |
||||
|
||||
<root> |
||||
<xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata"> |
||||
<xsd:element name="root" msdata:IsDataSet="true"> |
||||
|
||||
</xsd:element> |
||||
</xsd:schema> |
||||
<resheader name="resmimetype"> |
||||
<value>text/microsoft-resx</value> |
||||
</resheader> |
||||
<resheader name="version"> |
||||
<value>1.3</value> |
||||
</resheader> |
||||
<resheader name="reader"> |
||||
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value> |
||||
</resheader> |
||||
<resheader name="writer"> |
||||
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value> |
||||
</resheader> |
||||
<data name="Action_Launch" xml:space="preserve"> |
||||
<value>Launch</value> |
||||
</data> |
||||
<data name="Action_Quit" xml:space="preserve"> |
||||
<value>Quit</value> |
||||
</data> |
||||
<data name="Action_Save" xml:space="preserve"> |
||||
<value>Save</value> |
||||
</data> |
||||
<data name="Action_Cancel" xml:space="preserve"> |
||||
<value>Cancel</value> |
||||
</data> |
||||
<data name="Label_Language" xml:space="preserve"> |
||||
<value>Language</value> |
||||
</data> |
||||
<data name="Text_RelaunchRequiredToApplyLanguage" xml:space="preserve"> |
||||
<value>Relaunch is required for new language option to take effect</value> |
||||
</data> |
||||
<data name="Action_Relaunch" xml:space="preserve"> |
||||
<value>Relaunch</value> |
||||
</data> |
||||
<data name="Action_RelaunchLater" xml:space="preserve"> |
||||
<value>Relaunch Later</value> |
||||
</data> |
||||
<data name="Label_RelaunchRequired" xml:space="preserve"> |
||||
<value>Relaunch Required</value> |
||||
</data> |
||||
</root> |
@ -0,0 +1,221 @@
|
||||
using System; |
||||
using System.Collections.Generic; |
||||
using System.Collections.Immutable; |
||||
using System.Collections.ObjectModel; |
||||
using System.IO; |
||||
using System.Linq; |
||||
using System.Net.Http; |
||||
using System.Threading.Tasks; |
||||
using AsyncAwaitBestPractices; |
||||
using Avalonia.Controls; |
||||
using Avalonia.Controls.Notifications; |
||||
using AvaloniaEdit.Utils; |
||||
using CommunityToolkit.Mvvm.ComponentModel; |
||||
using FluentAvalonia.UI.Controls; |
||||
using Microsoft.Extensions.Logging; |
||||
using Refit; |
||||
using StabilityMatrix.Avalonia.Controls; |
||||
using StabilityMatrix.Avalonia.Services; |
||||
using StabilityMatrix.Avalonia.ViewModels.Base; |
||||
using StabilityMatrix.Avalonia.ViewModels.CheckpointManager; |
||||
using StabilityMatrix.Avalonia.ViewModels.Dialogs; |
||||
using StabilityMatrix.Avalonia.Views; |
||||
using StabilityMatrix.Avalonia.Views.Dialogs; |
||||
using StabilityMatrix.Core.Api; |
||||
using StabilityMatrix.Core.Attributes; |
||||
using StabilityMatrix.Core.Database; |
||||
using StabilityMatrix.Core.Extensions; |
||||
using StabilityMatrix.Core.Helper; |
||||
using StabilityMatrix.Core.Models; |
||||
using StabilityMatrix.Core.Models.Api; |
||||
using StabilityMatrix.Core.Services; |
||||
using Symbol = FluentIcons.Common.Symbol; |
||||
using SymbolIconSource = FluentIcons.FluentAvalonia.SymbolIconSource; |
||||
|
||||
namespace StabilityMatrix.Avalonia.ViewModels; |
||||
|
||||
[View(typeof(NewCheckpointsPage))] |
||||
public partial class NewCheckpointsPageViewModel : PageViewModelBase |
||||
{ |
||||
private readonly ILogger<NewCheckpointsPageViewModel> logger; |
||||
private readonly ISettingsManager settingsManager; |
||||
private readonly ILiteDbContext liteDbContext; |
||||
private readonly ICivitApi civitApi; |
||||
private readonly ServiceManager<ViewModelBase> dialogFactory; |
||||
private readonly INotificationService notificationService; |
||||
public override string Title => "Checkpoint Manager"; |
||||
public override IconSource IconSource => new SymbolIconSource |
||||
{Symbol = Symbol.Cellular5g, IsFilled = true}; |
||||
|
||||
public NewCheckpointsPageViewModel(ILogger<NewCheckpointsPageViewModel> logger, |
||||
ISettingsManager settingsManager, ILiteDbContext liteDbContext, ICivitApi civitApi, |
||||
ServiceManager<ViewModelBase> dialogFactory, INotificationService notificationService) |
||||
{ |
||||
this.logger = logger; |
||||
this.settingsManager = settingsManager; |
||||
this.liteDbContext = liteDbContext; |
||||
this.civitApi = civitApi; |
||||
this.dialogFactory = dialogFactory; |
||||
this.notificationService = notificationService; |
||||
} |
||||
|
||||
[ObservableProperty] |
||||
[NotifyPropertyChangedFor(nameof(ConnectedCheckpoints))] |
||||
[NotifyPropertyChangedFor(nameof(NonConnectedCheckpoints))] |
||||
private ObservableCollection<CheckpointFile> allCheckpoints = new(); |
||||
|
||||
[ObservableProperty] |
||||
private ObservableCollection<CivitModel> civitModels = new(); |
||||
|
||||
public ObservableCollection<CheckpointFile> ConnectedCheckpoints => new( |
||||
AllCheckpoints.Where(x => x.IsConnectedModel) |
||||
.OrderBy(x => x.ConnectedModel!.ModelName) |
||||
.ThenBy(x => x.ModelType) |
||||
.GroupBy(x => x.ConnectedModel!.ModelId) |
||||
.Select(x => x.First())); |
||||
|
||||
public ObservableCollection<CheckpointFile> NonConnectedCheckpoints => new( |
||||
AllCheckpoints.Where(x => !x.IsConnectedModel).OrderBy(x => x.ModelType)); |
||||
|
||||
public override async Task OnLoadedAsync() |
||||
{ |
||||
if (Design.IsDesignMode) return; |
||||
|
||||
var files = CheckpointFile.GetAllCheckpointFiles(settingsManager.ModelsDirectory); |
||||
AllCheckpoints = new ObservableCollection<CheckpointFile>(files); |
||||
|
||||
var connectedModelIds = ConnectedCheckpoints.Select(x => x.ConnectedModel.ModelId); |
||||
var modelRequest = new CivitModelsRequest |
||||
{ |
||||
CommaSeparatedModelIds = string.Join(',', connectedModelIds) |
||||
}; |
||||
|
||||
// See if query is cached |
||||
var cachedQuery = await liteDbContext.CivitModelQueryCache |
||||
.IncludeAll() |
||||
.FindByIdAsync(ObjectHash.GetMd5Guid(modelRequest)); |
||||
|
||||
// If cached, update model cards |
||||
if (cachedQuery is not null) |
||||
{ |
||||
CivitModels = new ObservableCollection<CivitModel>(cachedQuery.Items); |
||||
|
||||
// Start remote query (background mode) |
||||
// Skip when last query was less than 2 min ago |
||||
var timeSinceCache = DateTimeOffset.UtcNow - cachedQuery.InsertedAt; |
||||
if (timeSinceCache?.TotalMinutes >= 2) |
||||
{ |
||||
CivitQuery(modelRequest).SafeFireAndForget(); |
||||
} |
||||
} |
||||
else |
||||
{ |
||||
await CivitQuery(modelRequest); |
||||
} |
||||
} |
||||
|
||||
public async Task ShowVersionDialog(int modelId) |
||||
{ |
||||
var model = CivitModels.FirstOrDefault(m => m.Id == modelId); |
||||
if (model == null) |
||||
{ |
||||
notificationService.Show(new Notification("Model has no versions available", |
||||
"This model has no versions available for download", NotificationType.Warning)); |
||||
return; |
||||
} |
||||
var versions = model.ModelVersions; |
||||
if (versions is null || versions.Count == 0) |
||||
{ |
||||
notificationService.Show(new Notification("Model has no versions available", |
||||
"This model has no versions available for download", NotificationType.Warning)); |
||||
return; |
||||
} |
||||
|
||||
var dialog = new BetterContentDialog |
||||
{ |
||||
Title = model.Name, |
||||
IsPrimaryButtonEnabled = false, |
||||
IsSecondaryButtonEnabled = false, |
||||
IsFooterVisible = false, |
||||
MaxDialogWidth = 750, |
||||
}; |
||||
|
||||
var viewModel = dialogFactory.Get<SelectModelVersionViewModel>(); |
||||
viewModel.Dialog = dialog; |
||||
viewModel.Versions = versions.Select(version => |
||||
new ModelVersionViewModel( |
||||
settingsManager.Settings.InstalledModelHashes ?? new HashSet<string>(), version)) |
||||
.ToImmutableArray(); |
||||
viewModel.SelectedVersionViewModel = viewModel.Versions[0]; |
||||
|
||||
dialog.Content = new SelectModelVersionDialog |
||||
{ |
||||
DataContext = viewModel |
||||
}; |
||||
|
||||
var result = await dialog.ShowAsync(); |
||||
|
||||
if (result != ContentDialogResult.Primary) |
||||
{ |
||||
return; |
||||
} |
||||
|
||||
var selectedVersion = viewModel?.SelectedVersionViewModel?.ModelVersion; |
||||
var selectedFile = viewModel?.SelectedFile?.CivitFile; |
||||
} |
||||
|
||||
private async Task CivitQuery(CivitModelsRequest request) |
||||
{ |
||||
try |
||||
{ |
||||
var modelResponse = await civitApi.GetModels(request); |
||||
var models = modelResponse.Items; |
||||
// Filter out unknown model types and archived/taken-down models |
||||
models = models.Where(m => m.Type.ConvertTo<SharedFolderType>() > 0) |
||||
.Where(m => m.Mode == null).ToList(); |
||||
|
||||
// Database update calls will invoke `OnModelsUpdated` |
||||
// Add to database |
||||
await liteDbContext.UpsertCivitModelAsync(models); |
||||
// Add as cache entry |
||||
var cacheNew = await liteDbContext.UpsertCivitModelQueryCacheEntryAsync( |
||||
new CivitModelQueryCacheEntry |
||||
{ |
||||
Id = ObjectHash.GetMd5Guid(request), |
||||
InsertedAt = DateTimeOffset.UtcNow, |
||||
Request = request, |
||||
Items = models, |
||||
Metadata = modelResponse.Metadata |
||||
}); |
||||
|
||||
if (cacheNew) |
||||
{ |
||||
CivitModels = new ObservableCollection<CivitModel>(models); |
||||
} |
||||
} |
||||
catch (OperationCanceledException) |
||||
{ |
||||
notificationService.Show(new Notification("Request to CivitAI timed out", |
||||
"Could not check for checkpoint updates. Please try again later.")); |
||||
logger.LogWarning($"CivitAI query timed out ({request})"); |
||||
} |
||||
catch (HttpRequestException e) |
||||
{ |
||||
notificationService.Show(new Notification("CivitAI can't be reached right now", |
||||
"Could not check for checkpoint updates. Please try again later.")); |
||||
logger.LogWarning(e, $"CivitAI query HttpRequestException ({request})"); |
||||
} |
||||
catch (ApiException e) |
||||
{ |
||||
notificationService.Show(new Notification("CivitAI can't be reached right now", |
||||
"Could not check for checkpoint updates. Please try again later.")); |
||||
logger.LogWarning(e, $"CivitAI query ApiException ({request})"); |
||||
} |
||||
catch (Exception e) |
||||
{ |
||||
notificationService.Show(new Notification("CivitAI can't be reached right now", |
||||
$"Unknown exception during CivitAI query: {e.GetType().Name}")); |
||||
logger.LogError(e, $"CivitAI query unknown exception ({request})"); |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,105 @@
|
||||
<controls:UserControlBase xmlns="https://github.com/avaloniaui" |
||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" |
||||
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" |
||||
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" |
||||
xmlns:viewModels="clr-namespace:StabilityMatrix.Avalonia.ViewModels" |
||||
xmlns:mocks="clr-namespace:StabilityMatrix.Avalonia.DesignData" |
||||
xmlns:checkpointManager="clr-namespace:StabilityMatrix.Avalonia.ViewModels.CheckpointManager" |
||||
xmlns:controls="clr-namespace:StabilityMatrix.Avalonia.Controls" |
||||
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450" |
||||
x:Class="StabilityMatrix.Avalonia.Views.NewCheckpointsPage" |
||||
d:DataContext="{x:Static mocks:DesignData.NewCheckpointsPageViewModel}" |
||||
x:CompileBindings="True" |
||||
x:Name="ControlBase" |
||||
x:DataType="viewModels:NewCheckpointsPageViewModel"> |
||||
<ScrollViewer> |
||||
<Grid RowDefinitions="Auto, Auto" Margin="8"> |
||||
<TextBlock Text="Installed Checkpoints" |
||||
FontSize="24" |
||||
Margin="8"/> |
||||
|
||||
<ItemsRepeater Grid.Row="1" ItemsSource="{Binding ConnectedCheckpoints}"> |
||||
<ItemsRepeater.Layout> |
||||
<UniformGridLayout /> |
||||
</ItemsRepeater.Layout> |
||||
<ItemsRepeater.ItemTemplate> |
||||
<DataTemplate DataType="{x:Type checkpointManager:CheckpointFile}"> |
||||
<controls:Card |
||||
Margin="8" |
||||
MaxHeight="450" |
||||
Width="300" |
||||
CornerRadius="8"> |
||||
<Grid RowDefinitions="Auto, Auto, Auto, Auto"> |
||||
<TextBlock Grid.Row="0" |
||||
Margin="0,0,0,8" |
||||
Text="{Binding ConnectedModel.ModelName}" /> |
||||
|
||||
<controls:BetterAdvancedImage |
||||
Grid.Row="1" |
||||
Height="250" |
||||
Stretch="UniformToFill" |
||||
CornerRadius="8" |
||||
Source="{Binding PreviewImagePath}" /> |
||||
|
||||
<Grid Grid.Row="1" |
||||
Margin="8,8,0,0" |
||||
ColumnDefinitions="Auto, Auto"> |
||||
<controls:Card |
||||
Grid.Column="0" |
||||
Classes="info" |
||||
Height="24" |
||||
HorizontalAlignment="Left" |
||||
Padding="4" |
||||
Margin="0,0,4,0" |
||||
VerticalAlignment="Top"> |
||||
|
||||
<TextBlock |
||||
FontSize="11" |
||||
FontWeight="Medium" |
||||
HorizontalAlignment="Center" |
||||
Text="{Binding ModelType}" |
||||
VerticalAlignment="Center" /> |
||||
</controls:Card> |
||||
<controls:Card |
||||
Grid.Column="1" |
||||
Classes="info" |
||||
Height="24" |
||||
Margin="4,0" |
||||
HorizontalAlignment="Left" |
||||
Padding="4" |
||||
VerticalAlignment="Top"> |
||||
|
||||
<TextBlock |
||||
FontSize="11" |
||||
FontWeight="Medium" |
||||
HorizontalAlignment="Center" |
||||
Text="{Binding ConnectedModel.BaseModel}" |
||||
VerticalAlignment="Center" /> |
||||
</controls:Card> |
||||
</Grid> |
||||
|
||||
<Grid Grid.Row="2" |
||||
Margin="0,16,0,0" |
||||
ColumnDefinitions="*, *"> |
||||
<Button Grid.Column="0" |
||||
Content="Update" |
||||
Classes="accent" |
||||
Margin="0,0,4,0" |
||||
HorizontalAlignment="Stretch" /> |
||||
<Button Grid.Column="1" |
||||
Margin="4,0,0,0" |
||||
Content="All Versions" |
||||
Classes="accent" |
||||
Command="{Binding $parent[ItemsRepeater].((viewModels:NewCheckpointsPageViewModel)DataContext).ShowVersionDialog}" |
||||
CommandParameter="{Binding ConnectedModel.ModelId}" |
||||
HorizontalAlignment="Stretch" /> |
||||
</Grid> |
||||
|
||||
</Grid> |
||||
</controls:Card> |
||||
</DataTemplate> |
||||
</ItemsRepeater.ItemTemplate> |
||||
</ItemsRepeater> |
||||
</Grid> |
||||
</ScrollViewer> |
||||
</controls:UserControlBase> |
@ -0,0 +1,11 @@
|
||||
using StabilityMatrix.Avalonia.Controls; |
||||
|
||||
namespace StabilityMatrix.Avalonia.Views; |
||||
|
||||
public partial class NewCheckpointsPage : UserControlBase |
||||
{ |
||||
public NewCheckpointsPage() |
||||
{ |
||||
InitializeComponent(); |
||||
} |
||||
} |
Loading…
Reference in new issue