1+ using System ;
2+ using System . Diagnostics ;
3+ using System . Runtime . InteropServices ;
4+ using Aura . UI . ExtensionProperties ;
5+ using AuraUtilities ;
6+ using Avalonia ;
7+ using Avalonia . Controls ;
8+ using Avalonia . Data ;
9+ using Avalonia . Interactivity ;
10+
11+ namespace CutCode . CrossPlatfrom . Helpers
12+ {
13+ public static class UrlUtils
14+ {
15+ public static void OpenUrl ( string url )
16+ {
17+ if ( url == null )
18+ return ;
19+ ProcessStartInfo startInfo = new ProcessStartInfo ( ) ;
20+ startInfo . RedirectStandardOutput = true ;
21+ startInfo . RedirectStandardError = true ;
22+ startInfo . UseShellExecute = false ;
23+ startInfo . WindowStyle = ProcessWindowStyle . Hidden ;
24+ startInfo . CreateNoWindow = true ;
25+ if ( RuntimeInformation . IsOSPlatform ( OSPlatform . Windows ) )
26+ {
27+ startInfo . FileName = "powershell" ;
28+ startInfo . Arguments = "start \" " + url + "\" " ;
29+ Process . Start ( startInfo ) ;
30+ }
31+ else if ( RuntimeInformation . IsOSPlatform ( OSPlatform . Linux ) )
32+ {
33+ startInfo . FileName = "xdg-open" ;
34+ startInfo . Arguments = " " + url ;
35+ Process . Start ( startInfo ) ;
36+ }
37+ else
38+ {
39+ if ( ! RuntimeInformation . IsOSPlatform ( OSPlatform . OSX ) )
40+ return ;
41+ startInfo . FileName = "open" ;
42+ startInfo . Arguments = url ?? "" ;
43+ Process . Start ( startInfo ) ;
44+ }
45+ }
46+ }
47+
48+ public class ButtonExts : AvaloniaObject
49+ {
50+ public static readonly AttachedProperty < string > UrlProperty = AvaloniaProperty . RegisterAttached < Aura . UI . ExtensionProperties . ButtonExts , Button , string > ( "Url" ) ;
51+
52+ public static string GetUrl ( Button button ) => button . GetValue < string > ( ( StyledPropertyBase < string > ) Aura . UI . ExtensionProperties . ButtonExts . UrlProperty ) ;
53+
54+ public static void SetUrl ( Button button , string url ) => button . SetValue < string > ( ( StyledPropertyBase < string > ) Aura . UI . ExtensionProperties . ButtonExts . UrlProperty , url , BindingPriority . LocalValue ) ;
55+
56+ static ButtonExts ( ) => Aura . UI . ExtensionProperties . ButtonExts . UrlProperty . Changed . Subscribe < AvaloniaPropertyChangedEventArgs < string > > ( ( Action < AvaloniaPropertyChangedEventArgs < string > > ) ( e1 =>
57+ {
58+ Button btn = e1 . Sender as Button ;
59+ if ( btn == null )
60+ return ;
61+ btn . Click += ( EventHandler < RoutedEventArgs > ) ( ( s , e2 ) =>
62+ {
63+ if ( string . IsNullOrEmpty ( Aura . UI . ExtensionProperties . ButtonExts . GetUrl ( btn ) ) )
64+ return ;
65+ UrlUtils . OpenUrl ( Aura . UI . ExtensionProperties . ButtonExts . GetUrl ( btn ) ) ;
66+ } ) ;
67+ } ) ) ;
68+ }
69+ }
0 commit comments