2
2
using Newtonsoft . Json ;
3
3
using Newtonsoft . Json . Linq ;
4
4
using Newtonsoft . Json . Serialization ;
5
- using System ;
6
- using System . Collections . Generic ;
7
5
using System . Threading . Tasks ;
6
+ using ElectronNET . API . Extensions ;
8
7
9
8
namespace ElectronNET . API
10
9
{
@@ -40,17 +39,14 @@ internal static Shell Instance
40
39
/// <summary>
41
40
/// Show the given file in a file manager. If possible, select the file.
42
41
/// </summary>
43
- /// <param name="fullPath"></param>
44
- /// <returns>Whether the item was successfully shown.</returns>
45
- public Task < bool > ShowItemInFolderAsync ( string fullPath )
42
+ /// <param name="fullPath">The full path to the directory / file.</param>
43
+ public Task ShowItemInFolderAsync ( string fullPath )
46
44
{
47
- var taskCompletionSource = new TaskCompletionSource < bool > ( ) ;
45
+ var taskCompletionSource = new TaskCompletionSource < object > ( ) ;
48
46
49
- BridgeConnector . Socket . On ( "shell-showItemInFolderCompleted" , ( success ) =>
47
+ BridgeConnector . Socket . On ( "shell-showItemInFolderCompleted" , ( ) =>
50
48
{
51
49
BridgeConnector . Socket . Off ( "shell-showItemInFolderCompleted" ) ;
52
-
53
- taskCompletionSource . SetResult ( ( bool ) success ) ;
54
50
} ) ;
55
51
56
52
BridgeConnector . Socket . Emit ( "shell-showItemInFolder" , fullPath ) ;
@@ -61,17 +57,17 @@ public Task<bool> ShowItemInFolderAsync(string fullPath)
61
57
/// <summary>
62
58
/// Open the given file in the desktop's default manner.
63
59
/// </summary>
64
- /// <param name="path">The path to the file.</param>
60
+ /// <param name="path">The path to the directory / file.</param>
65
61
/// <returns>The error message corresponding to the failure if a failure occurred, otherwise <see cref="string.Empty"/>.</returns>
66
62
public Task < string > OpenPathAsync ( string path )
67
63
{
68
64
var taskCompletionSource = new TaskCompletionSource < string > ( ) ;
69
65
70
- BridgeConnector . Socket . On ( "shell-openPathCompleted" , ( success ) =>
66
+ BridgeConnector . Socket . On ( "shell-openPathCompleted" , ( errorMessage ) =>
71
67
{
72
68
BridgeConnector . Socket . Off ( "shell-openPathCompleted" ) ;
73
69
74
- taskCompletionSource . SetResult ( ( string ) success ) ;
70
+ taskCompletionSource . SetResult ( ( string ) errorMessage ) ;
75
71
} ) ;
76
72
77
73
BridgeConnector . Socket . Emit ( "shell-openPath" , path ) ;
@@ -83,106 +79,61 @@ public Task<string> OpenPathAsync(string path)
83
79
/// Open the given external protocol URL in the desktop’s default manner.
84
80
/// (For example, mailto: URLs in the user’s default mail agent).
85
81
/// </summary>
86
- /// <param name="url"></param>
87
- /// <returns>Whether an application was available to open the URL.
88
- /// If callback is specified, always returns true.</returns>
89
- public Task < bool > OpenExternalAsync ( string url )
82
+ /// <param name="url">Max 2081 characters on windows.</param>
83
+ /// <returns>The error message corresponding to the failure if a failure occurred, otherwise <see cref="string.Empty"/>.</returns>
84
+ public Task < string > OpenExternalAsync ( string url )
90
85
{
91
- var taskCompletionSource = new TaskCompletionSource < bool > ( ) ;
92
-
93
- BridgeConnector . Socket . On ( "shell-openExternalCompleted" , ( success ) =>
94
- {
95
- BridgeConnector . Socket . Off ( "shell-openExternalCompleted" ) ;
96
-
97
- taskCompletionSource . SetResult ( ( bool ) success ) ;
98
- } ) ;
99
-
100
- BridgeConnector . Socket . Emit ( "shell-openExternal" , url ) ;
101
-
102
- return taskCompletionSource . Task ;
86
+ return OpenExternalAsync ( url , null ) ;
103
87
}
104
88
105
89
/// <summary>
106
90
/// Open the given external protocol URL in the desktop’s default manner.
107
91
/// (For example, mailto: URLs in the user’s default mail agent).
108
92
/// </summary>
109
- /// <param name="url"></param>
110
- /// <param name="options">macOS only</param>
111
- /// <returns>Whether an application was available to open the URL.
112
- /// If callback is specified, always returns true.</returns>
113
- public Task < bool > OpenExternalAsync ( string url , OpenExternalOptions options )
93
+ /// <param name="url">Max 2081 characters on windows.</param>
94
+ /// <param name="options">Controls the behavior of OpenExternal.</param>
95
+ /// <returns>The error message corresponding to the failure if a failure occurred, otherwise <see cref="string.Empty"/>.</returns>
96
+ public Task < string > OpenExternalAsync ( string url , OpenExternalOptions options )
114
97
{
115
- var taskCompletionSource = new TaskCompletionSource < bool > ( ) ;
98
+ var taskCompletionSource = new TaskCompletionSource < string > ( ) ;
116
99
117
- BridgeConnector . Socket . On ( "shell-openExternalCompleted" , ( success ) =>
100
+ BridgeConnector . Socket . On ( "shell-openExternalCompleted" , ( error ) =>
118
101
{
119
102
BridgeConnector . Socket . Off ( "shell-openExternalCompleted" ) ;
120
103
121
- taskCompletionSource . SetResult ( ( bool ) success ) ;
104
+ taskCompletionSource . SetResult ( ( string ) error ) ;
122
105
} ) ;
123
106
124
- BridgeConnector . Socket . Emit ( "shell-openExternal" , url , JObject . FromObject ( options , _jsonSerializer ) ) ;
125
-
126
- return taskCompletionSource . Task ;
127
- }
128
-
129
- /// <summary>
130
- /// Open the given external protocol URL in the desktop’s default manner.
131
- /// (For example, mailto: URLs in the user’s default mail agent).
132
- /// </summary>
133
- /// <param name="url"></param>
134
- /// <param name="options">macOS only</param>
135
- /// <param name="errorAction">Action to get the error message.</param>
136
- /// <returns>Whether an application was available to open the URL.
137
- /// If callback is specified, always returns true.</returns>
138
- public Task < bool > OpenExternalAsync ( string url , OpenExternalOptions options , Action < Error > errorAction )
139
- {
140
- var taskCompletionSource = new TaskCompletionSource < bool > ( ) ;
141
-
142
- BridgeConnector . Socket . On ( "shell-openExternalCompleted" , ( success ) =>
107
+ if ( options == null )
143
108
{
144
- BridgeConnector . Socket . Off ( "shell-openExternalCompleted" ) ;
145
-
146
- taskCompletionSource . SetResult ( ( bool ) success ) ;
147
- } ) ;
148
-
149
- BridgeConnector . Socket . Off ( "shell-openExternalCallback" ) ;
150
- BridgeConnector . Socket . On ( "shell-openExternalCallback" , ( args ) => {
151
- var urlKey = ( ( JArray ) args ) . First . ToString ( ) ;
152
- var error = ( ( JArray ) args ) . Last . ToObject < Error > ( ) ;
153
-
154
- if ( _openExternalCallbacks . ContainsKey ( urlKey ) )
155
- {
156
- _openExternalCallbacks [ urlKey ] ( error ) ;
157
- }
158
- } ) ;
159
-
160
- _openExternalCallbacks . Add ( url , errorAction ) ;
161
-
162
- BridgeConnector . Socket . Emit ( "shell-openExternal" , url , JObject . FromObject ( options , _jsonSerializer ) , true ) ;
109
+ BridgeConnector . Socket . Emit ( "shell-openExternal" , url ) ;
110
+ }
111
+ else
112
+ {
113
+ BridgeConnector . Socket . Emit ( "shell-openExternal" , url , JObject . FromObject ( options , _jsonSerializer ) ) ;
114
+ }
163
115
164
116
return taskCompletionSource . Task ;
165
117
}
166
118
167
- private Dictionary < string , Action < Error > > _openExternalCallbacks = new Dictionary < string , Action < Error > > ( ) ;
168
-
169
119
/// <summary>
170
- /// Move the given file to trash and returns a boolean status for the operation.
120
+ /// Move the given file to trash and returns a <see cref="bool"/> status for the operation.
171
121
/// </summary>
172
- /// <param name="fullPath"></param>
122
+ /// <param name="fullPath">The full path to the directory / file.</param>
123
+ /// <param name="deleteOnFail">Whether or not to unilaterally remove the item if the Trash is disabled or unsupported on the volume.</param>
173
124
/// <returns> Whether the item was successfully moved to the trash.</returns>
174
- public Task < bool > MoveItemToTrashAsync ( string fullPath )
125
+ public Task < bool > MoveItemToTrashAsync ( string fullPath , bool deleteOnFail )
175
126
{
176
127
var taskCompletionSource = new TaskCompletionSource < bool > ( ) ;
177
128
178
129
BridgeConnector . Socket . On ( "shell-moveItemToTrashCompleted" , ( success ) =>
179
130
{
180
131
BridgeConnector . Socket . Off ( "shell-moveItemToTrashCompleted" ) ;
181
132
182
- taskCompletionSource . SetResult ( ( bool ) success ) ;
133
+ taskCompletionSource . SetResult ( ( bool ) success ) ;
183
134
} ) ;
184
135
185
- BridgeConnector . Socket . Emit ( "shell-moveItemToTrash" , fullPath ) ;
136
+ BridgeConnector . Socket . Emit ( "shell-moveItemToTrash" , fullPath , deleteOnFail ) ;
186
137
187
138
return taskCompletionSource . Task ;
188
139
}
@@ -198,9 +149,9 @@ public void Beep()
198
149
/// <summary>
199
150
/// Creates or updates a shortcut link at shortcutPath.
200
151
/// </summary>
201
- /// <param name="shortcutPath"></param>
202
- /// <param name="operation"></param>
203
- /// <param name="options"></param>
152
+ /// <param name="shortcutPath">The path to the shortcut. </param>
153
+ /// <param name="operation">Default is <see cref="ShortcutLinkOperation.Create"/> </param>
154
+ /// <param name="options">Structure of a shortcut. </param>
204
155
/// <returns>Whether the shortcut was created successfully.</returns>
205
156
public Task < bool > WriteShortcutLinkAsync ( string shortcutPath , ShortcutLinkOperation operation , ShortcutDetails options )
206
157
{
@@ -210,21 +161,20 @@ public Task<bool> WriteShortcutLinkAsync(string shortcutPath, ShortcutLinkOperat
210
161
{
211
162
BridgeConnector . Socket . Off ( "shell-writeShortcutLinkCompleted" ) ;
212
163
213
- taskCompletionSource . SetResult ( ( bool ) success ) ;
164
+ taskCompletionSource . SetResult ( ( bool ) success ) ;
214
165
} ) ;
215
166
216
- BridgeConnector . Socket . Emit ( "shell-writeShortcutLink" , shortcutPath , operation . ToString ( ) , JObject . FromObject ( options , _jsonSerializer ) ) ;
167
+ BridgeConnector . Socket . Emit ( "shell-writeShortcutLink" , shortcutPath , operation . GetDescription ( ) , JObject . FromObject ( options , _jsonSerializer ) ) ;
217
168
218
169
return taskCompletionSource . Task ;
219
170
}
220
171
221
172
/// <summary>
222
173
/// Resolves the shortcut link at shortcutPath.
223
- ///
224
174
/// An exception will be thrown when any error happens.
225
175
/// </summary>
226
- /// <param name="shortcutPath"></param>
227
- /// <returns></returns>
176
+ /// <param name="shortcutPath">The path tot the shortcut. </param>
177
+ /// <returns><see cref="ShortcutDetails"/> of the shortcut.< /returns>
228
178
public Task < ShortcutDetails > ReadShortcutLinkAsync ( string shortcutPath )
229
179
{
230
180
var taskCompletionSource = new TaskCompletionSource < ShortcutDetails > ( ) ;
@@ -233,19 +183,22 @@ public Task<ShortcutDetails> ReadShortcutLinkAsync(string shortcutPath)
233
183
{
234
184
BridgeConnector . Socket . Off ( "shell-readShortcutLinkCompleted" ) ;
235
185
236
- taskCompletionSource . SetResult ( ( ShortcutDetails ) shortcutDetails ) ;
186
+ var shortcutObject = shortcutDetails as JObject ;
187
+ var details = shortcutObject ? . ToObject < ShortcutDetails > ( ) ;
188
+
189
+ taskCompletionSource . SetResult ( details ) ;
237
190
} ) ;
238
191
239
192
BridgeConnector . Socket . Emit ( "shell-readShortcutLink" , shortcutPath ) ;
240
193
241
194
return taskCompletionSource . Task ;
242
195
}
243
196
244
- private JsonSerializer _jsonSerializer = new JsonSerializer ( )
197
+ private readonly JsonSerializer _jsonSerializer = new JsonSerializer ( )
245
198
{
246
199
ContractResolver = new CamelCasePropertyNamesContractResolver ( ) ,
247
200
NullValueHandling = NullValueHandling . Ignore ,
248
201
DefaultValueHandling = DefaultValueHandling . Ignore
249
202
} ;
250
203
}
251
- }
204
+ }
0 commit comments