99
1010namespace SourceGit . ViewModels
1111{
12- public class FileHistoriesRevisionFile ( string path , object content )
12+ public class FileHistoriesRevisionFile ( string path , object content = null , bool canOpenWithDefaultEditor = false )
1313 {
1414 public string Path { get ; set ; } = path ;
1515 public object Content { get ; set ; } = content ;
16+ public bool CanOpenWithDefaultEditor { get ; set ; } = canOpenWithDefaultEditor ;
1617 }
1718
1819 public class FileHistoriesSingleRevision : ObservableObject
@@ -49,6 +50,23 @@ public Task<bool> ResetToSelectedRevision()
4950 return Task . Run ( ( ) => new Commands . Checkout ( _repo . FullPath ) . FileWithRevision ( _file , $ "{ _revision . SHA } ") ) ;
5051 }
5152
53+ public Task OpenWithDefaultEditor ( )
54+ {
55+ if ( _viewContent is not FileHistoriesRevisionFile { CanOpenWithDefaultEditor : true } )
56+ return null ;
57+
58+ return Task . Run ( ( ) =>
59+ {
60+ var fullPath = Native . OS . GetAbsPath ( _repo . FullPath , _file ) ;
61+ var fileName = Path . GetFileNameWithoutExtension ( fullPath ) ?? "" ;
62+ var fileExt = Path . GetExtension ( fullPath ) ?? "" ;
63+ var tmpFile = Path . Combine ( Path . GetTempPath ( ) , $ "{ fileName } ~{ _revision . SHA . Substring ( 0 , 10 ) } { fileExt } ") ;
64+
65+ Commands . SaveRevisionFile . Run ( _repo . FullPath , _revision . SHA , _file , tmpFile ) ;
66+ Native . OS . OpenWithDefaultEditor ( tmpFile ) ;
67+ } ) ;
68+ }
69+
5270 private void RefreshViewContent ( )
5371 {
5472 if ( _isDiffMode )
@@ -62,7 +80,7 @@ private void SetViewContentAsRevisionFile()
6280 var objs = new Commands . QueryRevisionObjects ( _repo . FullPath , _revision . SHA , _file ) . Result ( ) ;
6381 if ( objs . Count == 0 )
6482 {
65- ViewContent = new FileHistoriesRevisionFile ( _file , null ) ;
83+ ViewContent = new FileHistoriesRevisionFile ( _file ) ;
6684 return ;
6785 }
6886
@@ -80,13 +98,13 @@ private void SetViewContentAsRevisionFile()
8098 {
8199 var source = ImageSource . FromRevision ( _repo . FullPath , _revision . SHA , _file , imgDecoder ) ;
82100 var image = new Models . RevisionImageFile ( _file , source . Bitmap , source . Size ) ;
83- Dispatcher . UIThread . Invoke ( ( ) => ViewContent = new FileHistoriesRevisionFile ( _file , image ) ) ;
101+ Dispatcher . UIThread . Invoke ( ( ) => ViewContent = new FileHistoriesRevisionFile ( _file , image , true ) ) ;
84102 }
85103 else
86104 {
87105 var size = new Commands . QueryFileSize ( _repo . FullPath , _file , _revision . SHA ) . Result ( ) ;
88106 var binaryFile = new Models . RevisionBinaryFile ( ) { Size = size } ;
89- Dispatcher . UIThread . Invoke ( ( ) => ViewContent = new FileHistoriesRevisionFile ( _file , binaryFile ) ) ;
107+ Dispatcher . UIThread . Invoke ( ( ) => ViewContent = new FileHistoriesRevisionFile ( _file , binaryFile , true ) ) ;
90108 }
91109
92110 return ;
@@ -101,18 +119,18 @@ private void SetViewContentAsRevisionFile()
101119 if ( imgDecoder != Models . ImageDecoder . None )
102120 {
103121 var combined = new RevisionLFSImage ( _repo . FullPath , _file , lfs , imgDecoder ) ;
104- Dispatcher . UIThread . Invoke ( ( ) => ViewContent = new FileHistoriesRevisionFile ( _file , combined ) ) ;
122+ Dispatcher . UIThread . Invoke ( ( ) => ViewContent = new FileHistoriesRevisionFile ( _file , combined , true ) ) ;
105123 }
106124 else
107125 {
108126 var rlfs = new Models . RevisionLFSObject ( ) { Object = lfs } ;
109- Dispatcher . UIThread . Invoke ( ( ) => ViewContent = new FileHistoriesRevisionFile ( _file , rlfs ) ) ;
127+ Dispatcher . UIThread . Invoke ( ( ) => ViewContent = new FileHistoriesRevisionFile ( _file , rlfs , true ) ) ;
110128 }
111129 }
112130 else
113131 {
114132 var txt = new Models . RevisionTextFile ( ) { FileName = obj . Path , Content = content } ;
115- Dispatcher . UIThread . Invoke ( ( ) => ViewContent = new FileHistoriesRevisionFile ( _file , txt ) ) ;
133+ Dispatcher . UIThread . Invoke ( ( ) => ViewContent = new FileHistoriesRevisionFile ( _file , txt , true ) ) ;
116134 }
117135 } ) ;
118136 break ;
@@ -132,7 +150,7 @@ private void SetViewContentAsRevisionFile()
132150 } ) ;
133151 break ;
134152 default :
135- ViewContent = new FileHistoriesRevisionFile ( _file , null ) ;
153+ ViewContent = new FileHistoriesRevisionFile ( _file ) ;
136154 break ;
137155 }
138156 }
0 commit comments