-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTokenDialog.cs
More file actions
59 lines (49 loc) · 1.95 KB
/
Copy pathTokenDialog.cs
File metadata and controls
59 lines (49 loc) · 1.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
using System.Diagnostics;
using System.Text.RegularExpressions;
namespace WhisperTranscriber;
public static class TokenDialog
{
private static readonly Regex UrlRegex = new(@"https?://[^\s]+", RegexOptions.Compiled);
public static string? Prompt(IWin32Window owner)
{
using var form = new Form
{
Text = L.T("hf_token_title"),
Size = new Size(620, 340),
FormBorderStyle = FormBorderStyle.FixedDialog,
StartPosition = FormStartPosition.CenterParent,
MinimizeBox = false,
MaximizeBox = false
};
var lbl = new LinkLabel
{
Text = L.T("hf_token_text"),
Location = new Point(12, 12),
Size = new Size(590, 210),
LinkBehavior = LinkBehavior.HoverUnderline
};
foreach (Match m in UrlRegex.Matches(lbl.Text))
lbl.Links.Add(m.Index, m.Length, m.Value);
lbl.LinkClicked += (_, e) =>
{
if (e.Link?.LinkData is string url)
{
try { Process.Start(new ProcessStartInfo { FileName = url, UseShellExecute = true }); }
catch (Exception ex) { Logger.Warn($"Open URL failed: {ex.Message}"); }
}
};
var txt = new TextBox
{
Location = new Point(12, 230),
Size = new Size(590, 23)
};
var ok = new Button { Text = "OK", Location = new Point(430, 265), Size = new Size(80, 28), DialogResult = DialogResult.OK };
var cancel = new Button { Text = "Cancel", Location = new Point(520, 265), Size = new Size(80, 28), DialogResult = DialogResult.Cancel };
form.Controls.AddRange(new Control[] { lbl, txt, ok, cancel });
form.AcceptButton = ok;
form.CancelButton = cancel;
if (form.ShowDialog(owner) != DialogResult.OK) return null;
var token = txt.Text.Trim();
return string.IsNullOrEmpty(token) ? null : token;
}
}