Skip to content

Commit

Permalink
Added PositronFilePicker
Browse files Browse the repository at this point in the history
  • Loading branch information
ackava committed Aug 29, 2024

Verified

This commit was signed with the committer’s verified signature.
snyk-bot Snyk bot
1 parent 4b9e265 commit 719f726
Showing 14 changed files with 1,831 additions and 15 deletions.
139 changes: 139 additions & 0 deletions Positron/Controls/ProgressPanel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Positron.Controls;

public class CloseButton: Button
{
public CloseButton()
{
this.Text = "x";
this.BackgroundColor = Color.Parse("Red");
}
}

public sealed class ProgressPanel : Grid, IDisposable
{

public readonly CancellationToken CancelToken;
private readonly CancellationTokenSource cancellationTokenSource;

private Grid? parentGrid;
private readonly ProgressBar progress;

public double Progress { get => progress.Progress; set => progress.Progress = value; }

public static ProgressPanel Create(string title)
{
return new ProgressPanel(title);
}

public ProgressPanel(string title)
{
cancellationTokenSource = new CancellationTokenSource();
CancelToken = cancellationTokenSource.Token;
var grid = (Application.Current.MainPage as ContentPage)?.Content as Grid;
grid?.Children?.Add(this);
this.HorizontalOptions = LayoutOptions.Fill;
this.VerticalOptions = LayoutOptions.Fill;
this.parentGrid = grid;

this.progress = new ProgressBar();
this.progress.HorizontalOptions = LayoutOptions.Fill;
this.progress.VerticalOptions = LayoutOptions.Center;
this.progress.MinimumWidthRequest = 100;

this.ColumnDefinitions.Add(new ColumnDefinition
{
Width = new GridLength(1, GridUnitType.Star)
});
this.ColumnDefinitions.Add(new ColumnDefinition
{
Width = 5
});
this.ColumnDefinitions.Add(new ColumnDefinition
{
Width = GridLength.Auto
});
this.ColumnDefinitions.Add(new ColumnDefinition
{
Width = GridLength.Auto
});
this.ColumnDefinitions.Add(new ColumnDefinition
{
Width = 5
});
this.ColumnDefinitions.Add(new ColumnDefinition
{
Width = new GridLength(1, GridUnitType.Star)
});

this.RowDefinitions.Add(new RowDefinition
{
Height = GridLength.Star
});
this.RowDefinitions.Add(new RowDefinition
{
Height = 5
});
this.RowDefinitions.Add(new RowDefinition
{
Height = GridLength.Auto
});
this.RowDefinitions.Add(new RowDefinition
{
Height = GridLength.Auto
});
this.RowDefinitions.Add(new RowDefinition
{
Height = 5
});
this.RowDefinitions.Add(new RowDefinition
{
Height = GridLength.Star
});

this.Children.Add(new Frame
{
BackgroundColor = Color.Parse("White"),
HasShadow = true,
}.SetGrid(column: 1, columnSpan: 4, row: 1, rowSpan: 4));

this.Children.Add(new Label
{
Text = title,
}.SetGrid(column: 2, row: 2));

this.Children.Add(progress.SetGrid(column: 2, row: 3, columnSpan: 2));
this.Children.Add(new CloseButton
{
Padding = 0,
Command = new Command(() => Cancel()),
}.SetGrid(row: 2, column: 3));
}

public void Cancel()
{
HybridRunner.RunAsync(async () =>

Check failure on line 120 in Positron/Controls/ProgressPanel.cs

GitHub Actions / build

The name 'HybridRunner' does not exist in the current context

Check failure on line 120 in Positron/Controls/ProgressPanel.cs

GitHub Actions / build

The name 'HybridRunner' does not exist in the current context

Check failure on line 120 in Positron/Controls/ProgressPanel.cs

GitHub Actions / build

The name 'HybridRunner' does not exist in the current context

Check failure on line 120 in Positron/Controls/ProgressPanel.cs

GitHub Actions / build

The name 'HybridRunner' does not exist in the current context

Check failure on line 120 in Positron/Controls/ProgressPanel.cs

GitHub Actions / build

The name 'HybridRunner' does not exist in the current context

Check failure on line 120 in Positron/Controls/ProgressPanel.cs

GitHub Actions / build

The name 'HybridRunner' does not exist in the current context
{
if (await Xamarin.Forms.Application.Current.MainPage.DisplayAlert("Cancel?", "Are you sure you want to cancel?", "Yes", "No"))
{
cancellationTokenSource.Cancel();
this.Dispose();
parentGrid = null;
}
});
}

public void Dispose()
{
if (parentGrid == null)
{
return;
}
MainThread.BeginInvokeOnMainThread(() => parentGrid?.Children?.Remove(this));
}
}
38 changes: 38 additions & 0 deletions Positron/Core/GridExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Positron;
public static class GridExtensions
{

public static T SetGrid<T>(
this T @this,
int? column = null,
int? row = null,
int? columnSpan = null,
int? rowSpan = null)
where T : Element
{
if (column != null)
{
Grid.SetColumn(@this, column.Value);
}
if (row != null)
{
Grid.SetRow(@this, row.Value);
}
if (columnSpan != null)
{
Grid.SetColumnSpan(@this, columnSpan.Value);
}
if (rowSpan != null)
{
Grid.SetRowSpan(@this, rowSpan.Value);
}
return @this;
}

}
58 changes: 58 additions & 0 deletions Positron/Core/StringExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Positron;
public static partial class StringExtensions
{

public static string Left(this string @this, int maxLength)
{
if (@this.Length <= maxLength)
{
return @this;
}
return @this.Substring(0, maxLength);
}
public static string SafeFileName(this string @this, string padLeft = "", int maxLength = 60)
{
var name = System.IO.Path.GetFileNameWithoutExtension(@this).Left(maxLength) + padLeft;
var ext = System.IO.Path.GetExtension(@this);
var sb = new StringBuilder(maxLength + ext.Length);
for (int i = 0; i < name.Length; i++)
{
char ch = name[i];
ch = char.IsLetterOrDigit(ch) || ch == '.' || ch == '-' ? ch : '-';
sb.Append(ch);
}
sb.Append(ext);
return sb.ToString();
}

//public static ReadOnlySpan<char> Left(this ReadOnlySpan<char> @this, int maxLength)
//{
// if (@this.Length <= maxLength)
// {
// return @this;
// }
// return @this.Slice(0, maxLength);
//}

//public static string SafeFileName(this ReadOnlySpan<char> @this, int maxLength = 80)
//{
// var name = System.IO.Path.GetFileNameWithoutExtension(@this).Left(maxLength);
// var ext = System.IO.Path.GetExtension(@this);
// Span<char> newName = stackalloc char[80 + ext.Length];
// for (int i = 0; i < name.Length; i++)
// {
// char ch = name[i];
// ch = char.IsLetterOrDigit(ch) || ch == '.' || ch == '-' ? ch : '-';
// newName[i] = ch;
// }
// ext.CopyTo(newName.Slice(name.Length));
// return new string(newName., name.Length + ext.Length);
//}

}
2 changes: 1 addition & 1 deletion Positron/Engine/StringExtensions.cs
Original file line number Diff line number Diff line change
@@ -4,7 +4,7 @@

namespace Positron
{
public static class StringExtensions
public static partial class StringExtensions
{

public static bool EqualsIgnoreCase(this string text, string compare) {
Loading

0 comments on commit 719f726

Please sign in to comment.