@@ -28,6 +28,8 @@ public class MainViewModel : ObservableObject {
2828 private AutomationBase ? _automation ;
2929 private RelayCommand ? _captureSelectedItemCommand ;
3030 private RelayCommand ? _closeInfoCommand ;
31+ private RelayCommand ? _copyDetailsToClipboardCommand ;
32+ private RelayCommand ? _currentElementSaveStateCommand ;
3133 private ObservableCollection < ElementPatternItem > ? _elementPatterns = [ ] ;
3234 private FocusTrackingMode ? _focusTrackingMode ;
3335 private HoverMode ? _hoverMode ;
@@ -39,7 +41,6 @@ public class MainViewModel : ObservableObject {
3941 private AutomationElement ? _rootElement ;
4042 private RelayCommand ? _startNewInstanceCommand ;
4143 private ITreeWalker ? _treeWalker ;
42- private RelayCommand ? _currentElementSaveStateCommand ;
4344
4445 public MainViewModel ( AutomationType automationType , string applicationVersion , InternalLogger logger ) {
4546 _logger = logger ;
@@ -182,14 +183,41 @@ public string? ApplicationVersion {
182183 public ICommand CloseInfoCommand => _closeInfoCommand ??= new RelayCommand ( _ => {
183184 IsInfoVisible = false ;
184185 } ) ;
185-
186- public event Action ? CopiedNotificationRequested ;
187186
188187 public ICommand CurrentElementSaveStateCommand => _currentElementSaveStateCommand ??= new RelayCommand ( _ => {
189188 if ( SelectedItem ? . AutomationElement == null ) {
190189 return ;
191190 }
192191
192+ try {
193+ XDocument document = new ( ) ;
194+ document . Add ( new XElement ( "Root" ) ) ;
195+ ExportElement ( document . Root ! , SelectedItem ) ;
196+
197+ Clipboard . SetText ( document . ToString ( ) ) ;
198+ CopiedNotificationCurrentElementSaveStateRequested ? . Invoke ( ) ;
199+ } catch ( Exception e ) {
200+ _logger ? . LogError ( e . ToString ( ) ) ;
201+ }
202+ } ) ;
203+
204+ public ICommand CollapseAllDetailsCommand => new RelayCommand ( _ => {
205+ foreach ( ElementPatternItem pattern in ElementPatterns ) {
206+ pattern . IsExpanded = false ;
207+ }
208+ } ) ;
209+
210+ public ICommand ExpandAllDetailsCommand => new RelayCommand ( _ => {
211+ foreach ( ElementPatternItem pattern in ElementPatterns ) {
212+ pattern . IsExpanded = true ;
213+ }
214+ } ) ;
215+
216+ public ICommand CopyDetailsToClipboardCommand => _copyDetailsToClipboardCommand ??= new RelayCommand ( _ => {
217+ if ( SelectedItem ? . AutomationElement == null ) {
218+ return ;
219+ }
220+
193221 try {
194222 XDocument document = new ( ) ;
195223 document . Add ( new XElement ( "Root" ) ) ;
@@ -217,17 +245,49 @@ public string? ApplicationVersion {
217245 }
218246 } ) ;
219247
220- public ICommand CollapseAllDetailsCommand => new RelayCommand ( _ => {
221- foreach ( ElementPatternItem pattern in ElementPatterns ) {
222- pattern . IsExpanded = false ;
248+ public event Action ? CopiedNotificationRequested ;
249+ public event Action ? CopiedNotificationCurrentElementSaveStateRequested ;
250+
251+ private void ExportElement ( XElement parent , ElementViewModel element ) {
252+ XElement xElement = CreateXElement ( element ) ;
253+ parent . Add ( xElement ) ;
254+
255+ try {
256+ foreach ( ElementViewModel children in element . Children ! ) {
257+ try {
258+ xElement . Add ( CreateXElement ( children ! ) ) ;
259+ } catch {
260+ // ignored
261+ }
262+ }
263+
264+ foreach ( ElementViewModel children in element . Children . Where ( x => x is { IsExpanded : true } ) . Where ( x => x != null ) ! ) {
265+ try {
266+ ExportElement ( xElement , children ! ) ;
267+ } catch {
268+ // ignored
269+ }
270+ }
271+ } catch {
272+ // ignored
223273 }
224- } ) ;
274+ }
225275
226- public ICommand ExpandAllDetailsCommand => new RelayCommand ( _ => {
227- foreach ( ElementPatternItem pattern in ElementPatterns ) {
228- pattern . IsExpanded = true ;
276+ private XElement CreateXElement ( ElementViewModel element ) {
277+
278+ List < XAttribute > attrs = [
279+ new ( "Name" , element . Name ) ,
280+ new ( "AutomationId" , element . AutomationId ) ,
281+ new ( "ControlType" , element . ControlType )
282+ ] ;
283+
284+ if ( EnableXPath ) {
285+ attrs . Add ( new XAttribute ( "XPath" , element . XPath ) ) ;
229286 }
230- } ) ;
287+
288+ XElement xElement = new ( "Element" , attrs ) ;
289+ return xElement ;
290+ }
231291
232292 private void ReadPatternsForSelectedItem ( AutomationElement ? selectedItemAutomationElement ) {
233293 if ( SelectedItem ? . AutomationElement == null || selectedItemAutomationElement == null ) {
0 commit comments