88using System . Threading . Tasks ;
99using Windows . ApplicationModel . DataTransfer ;
1010using Windows . Storage . Streams ;
11+ using Windows . UI . Core ;
1112using Windows . UI . Popups ;
1213using Windows . UI . Xaml ;
1314using Windows . UI . Xaml . Controls ;
@@ -32,10 +33,15 @@ public sealed partial class MainPage : Page
3233
3334 public string SelectedClipAsCsharp { get ; set ; }
3435
36+ CoreWindow window ;
37+
3538 public MainPage ( )
3639 {
3740 InitializeComponent ( ) ;
3841
42+ window = CoreWindow . GetForCurrentThread ( ) ;
43+ window . Activated += Window_Activated ;
44+
3945 Keys = new ObservableCollection < FileMakerClip > ( ) ;
4046 Layouts = new ObservableCollection < FileMakerClip > ( ) ;
4147
@@ -51,57 +57,75 @@ public MainPage()
5157 Clipboard . ContentChanged += Clipboard_ContentChanged ;
5258 }
5359
54- private async void Clipboard_ContentChanged ( object sender , object e )
60+ private async Task ProcessClipboard ( )
5561 {
56- var clip = Clipboard . GetContent ( ) ;
57-
58- var formats = clip . AvailableFormats . Where ( f => f . StartsWith ( "Mac-" , StringComparison . CurrentCultureIgnoreCase ) ) . Distinct ( ) ;
62+ try
63+ {
64+ var clip = Clipboard . GetContent ( ) ;
5965
60- Debug . WriteLine ( $ "Formats: { formats . Count ( ) } " ) ;
66+ var formats = clip . AvailableFormats . Where ( f => f . StartsWith ( "Mac-" , StringComparison . CurrentCultureIgnoreCase ) ) . Distinct ( ) ;
6167
62- foreach ( var format in formats )
63- {
64- object clipData = null ;
68+ Debug . WriteLine ( $ "Formats: { formats . Count ( ) } ") ;
6569
66- try
70+ foreach ( var format in formats )
6771 {
68- if ( format . Equals ( "bitmap" , StringComparison . CurrentCultureIgnoreCase ) )
72+ object clipData = null ;
73+
74+ try
6975 {
70- clipData = await clip . GetBitmapAsync ( ) ;
76+ if ( format . Equals ( "bitmap" , StringComparison . CurrentCultureIgnoreCase ) )
77+ {
78+ clipData = await clip . GetBitmapAsync ( ) ;
79+ }
80+ clipData = await clip . GetDataAsync ( format ) ;
7181 }
72- clipData = await clip . GetDataAsync ( format ) ;
73- }
7482#pragma warning disable CA1031 // Do not catch general exception types
75- catch ( Exception ex )
83+ catch ( Exception ex )
7684#pragma warning restore CA1031 // Do not catch general exception types
77- {
78- Debug . WriteLine ( ex . Message ) ;
79- }
85+ {
86+ Debug . WriteLine ( ex . Message ) ;
87+ }
8088
81- if ( ! ( clipData is IRandomAccessStream dataObj ) )
82- {
83- // this is some type of clipboard data this program can't handle
84- continue ;
85- }
89+ if ( ! ( clipData is IRandomAccessStream dataObj ) )
90+ {
91+ // this is some type of clipboard data this program can't handle
92+ continue ;
93+ }
8694
87- var stream = dataObj . GetInputStreamAt ( 0 ) ;
88- IBuffer buff = new Windows . Storage . Streams . Buffer ( ( uint ) dataObj . Size ) ;
89- await stream . ReadAsync ( buff , ( uint ) dataObj . Size , InputStreamOptions . None ) ;
90- var buffArray = buff . ToArray ( ) ;
95+ var stream = dataObj . GetInputStreamAt ( 0 ) ;
96+ IBuffer buff = new Windows . Storage . Streams . Buffer ( ( uint ) dataObj . Size ) ;
97+ await stream . ReadAsync ( buff , ( uint ) dataObj . Size , InputStreamOptions . None ) ;
98+ var buffArray = buff . ToArray ( ) ;
9199
92- var fmclip = new FileMakerClip ( "new-clip" , format , buffArray ) ;
100+ var fmclip = new FileMakerClip ( "new-clip" , format , buffArray ) ;
93101
94- // don't bother adding a duplicate. For some reason entries were getting entered twice per clip
95- // this is not the most efficient method to detect it, but it works well enough for now
96- if ( Keys . Any ( k => k . XmlData == fmclip . XmlData ) )
97- {
98- continue ;
99- }
102+ // don't bother adding a duplicate. For some reason entries were getting entered twice per clip
103+ // this is not the most efficient method to detect it, but it works well enough for now
104+ if ( Keys . Any ( k => k . XmlData == fmclip . XmlData ) )
105+ {
106+ continue ;
107+ }
100108
101- Keys . Add ( fmclip ) ;
109+ Keys . Add ( fmclip ) ;
110+ }
111+ }
112+ catch ( Exception ex )
113+ {
114+ var md = new MessageDialog ( ex . Message + "\r \n " + ex . StackTrace ) ;
115+ await md . ShowAsync ( ) ;
102116 }
103117 }
104118
119+ private async void Window_Activated ( CoreWindow sender , WindowActivatedEventArgs args )
120+ {
121+ await ProcessClipboard ( ) ;
122+ }
123+
124+ private void Clipboard_ContentChanged ( object sender , object e )
125+ {
126+ window . Activate ( ) ;
127+ }
128+
105129 private void Button_Click_1 ( object sender , RoutedEventArgs e )
106130 {
107131 var dp = new DataPackage ( ) ;
@@ -146,6 +170,8 @@ private async void asModelAppBarButton_Click(object sender, RoutedEventArgs e)
146170 if ( pickerResult == ContentDialogResult . Primary )
147171 {
148172 // regenerate using the layout picker
173+ SelectedLayout = picker . DialogResult ;
174+
149175 var classString = data . CreateClass ( SelectedLayout ) ;
150176 var dp = new DataPackage ( ) ;
151177 dp . SetText ( classString ) ;
0 commit comments