Skip to content

Commit 17b9401

Browse files
committed
Folder Picker
1 parent 34c2b8e commit 17b9401

File tree

5 files changed

+109
-4
lines changed

5 files changed

+109
-4
lines changed

UploaderX/IFolderPicker.cs

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
using System;
2+
namespace UploaderX
3+
{
4+
public interface IFolderPicker
5+
{
6+
Task<string> PickFolder();
7+
}
8+
}
9+

UploaderX/MainPage.xaml

+3-1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
FontSize="Subtitle"
2222
HorizontalOptions="Start" />
2323

24+
<Label Margin="0,10,0,0">App Path:</Label>
25+
<Editor x:Name="txtAppPath" IsReadOnly="True"></Editor>
2426
<Label Margin="0,10,0,0">ApplicationConfig Path:</Label>
2527
<Editor x:Name="txtAppConfigPath" IsReadOnly="True"></Editor>
2628
<Label Margin="0,10,0,0">UploadersConfig Path:</Label>
@@ -32,7 +34,7 @@
3234

3335
<Label>Watch dir:</Label>
3436
<HorizontalStackLayout>
35-
<Button x:Name="btnBrowseWatchDir" Text="Browse" Margin="10"></Button>
37+
<Button x:Name="btnBrowseWatchDir" Text="Browse" Margin="10" Clicked="btnBrowseWatchDir_Clicked"></Button>
3638
<Label x:Name="txtWatchDir" Margin="10"></Label>
3739
</HorizontalStackLayout>
3840

UploaderX/MainPage.xaml.cs

+12-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
using System.Diagnostics;
22
using HelpersLib;
3+
using MediaLib;
34
using Microsoft.Extensions.Logging;
45
using ShareX.HelpersLib;
56
using ShareX.UploadersLib;
7+
using UploaderX.Platforms.MacCatalyst;
68

79
namespace UploaderX;
810

@@ -22,9 +24,9 @@ public partial class MainPage : ContentPage
2224
public MainPage()
2325
{
2426
InitializeComponent();
25-
27+
txtAppPath.Text = Process.GetCurrentProcess().MainModule.FileName;
2628
string AppSettingsDir = Path.Combine(AppDir, "Settings");
27-
29+
2830
txtAppConfigPath.Text = Path.Combine(AppSettingsDir, "ApplicationConfig.json");
2931
App.Settings = ApplicationConfig.Load(txtAppConfigPath.Text);
3032

@@ -60,6 +62,7 @@ private void MainPage_UrlReceived(string url)
6062
Clipboard.Default.SetTextAsync(url);
6163
lblUrl.Text = url;
6264
wvUrl.Source = new UrlWebViewSource() { Url = url };
65+
wvUrl.Reload();
6366
}
6467

6568
private void OnUrlReceived(string url)
@@ -104,7 +107,7 @@ await Helpers.WaitWhileAsync(() =>
104107
if (Path.GetExtension(destPath).ToLower().Equals(".mov"))
105108
{
106109
string ffmpegPath = Path.Combine(AppDir, "ffmpeg");
107-
ShareX.MediaLib.FFmpegCLIManager ffmpeg = new ShareX.MediaLib.FFmpegCLIManager(ffmpegPath);
110+
FFmpegCLIManager ffmpeg = new FFmpegCLIManager(ffmpegPath);
108111
string mp4Path = Path.ChangeExtension(destPath, "mp4");
109112
string args = $"-i \"{destPath}\" -c:v libx264 -preset medium -crf 23 -pix_fmt yuv420p -movflags +faststart -y \"{mp4Path}\"";
110113
if (ffmpeg.Run(args))
@@ -134,6 +137,12 @@ async void btnGo_ClickedAsync(System.Object sender, System.EventArgs e)
134137
Uri uri = new Uri(lblUrl.Text);
135138
await Browser.Default.OpenAsync(uri, BrowserLaunchMode.SystemPreferred);
136139
}
140+
141+
async void btnBrowseWatchDir_Clicked(System.Object sender, System.EventArgs e)
142+
{
143+
IFolderPicker folderPicker = new FolderPicker(); //TODO: Result returns file://
144+
txtWatchDir.Text = await folderPicker.PickFolder();
145+
}
137146
}
138147

139148

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
using System;
2+
using Foundation;
3+
using MobileCoreServices;
4+
using UIKit;
5+
using UploaderX;
6+
7+
namespace UploaderX.Platforms.MacCatalyst
8+
{
9+
public class FolderPicker : IFolderPicker
10+
{
11+
class PickerDelegate : UIDocumentPickerDelegate
12+
{
13+
public Action<NSUrl[]> PickHandler { get; set; }
14+
15+
public override void WasCancelled(UIDocumentPickerViewController controller)
16+
=> PickHandler?.Invoke(null);
17+
18+
public override void DidPickDocument(UIDocumentPickerViewController controller, NSUrl[] urls)
19+
=> PickHandler?.Invoke(urls);
20+
21+
public override void DidPickDocument(UIDocumentPickerViewController controller, NSUrl url)
22+
=> PickHandler?.Invoke(new NSUrl[] { url });
23+
}
24+
25+
static void GetFileResults(NSUrl[] urls, TaskCompletionSource<string> tcs)
26+
{
27+
try
28+
{
29+
tcs.TrySetResult(urls?[0]?.ToString() ?? "");
30+
}
31+
catch (Exception ex)
32+
{
33+
tcs.TrySetException(ex);
34+
}
35+
}
36+
37+
public async Task<string> PickFolder()
38+
{
39+
var allowedTypes = new string[]
40+
{
41+
"public.folder"
42+
};
43+
44+
var picker = new UIDocumentPickerViewController(allowedTypes, UIDocumentPickerMode.Open);
45+
var tcs = new TaskCompletionSource<string>();
46+
47+
picker.Delegate = new PickerDelegate
48+
{
49+
PickHandler = urls => GetFileResults(urls, tcs)
50+
};
51+
52+
if (picker.PresentationController != null)
53+
{
54+
picker.PresentationController.Delegate =
55+
new UIPresentationControllerDelegate(() => GetFileResults(null, tcs));
56+
}
57+
58+
var parentController = Platform.GetCurrentUIViewController();
59+
60+
parentController.PresentViewController(picker, true, null);
61+
62+
return await tcs.Task;
63+
}
64+
65+
internal class UIPresentationControllerDelegate : UIAdaptivePresentationControllerDelegate
66+
{
67+
Action dismissHandler;
68+
69+
internal UIPresentationControllerDelegate(Action dismissHandler)
70+
=> this.dismissHandler = dismissHandler;
71+
72+
public override void DidDismiss(UIPresentationController presentationController)
73+
{
74+
dismissHandler?.Invoke();
75+
dismissHandler = null;
76+
}
77+
78+
protected override void Dispose(bool disposing)
79+
{
80+
dismissHandler?.Invoke();
81+
base.Dispose(disposing);
82+
}
83+
}
84+
}
85+
}
24.8 KB
Loading

0 commit comments

Comments
 (0)