Skip to content

Commit f64690c

Browse files
committed
Added SciterAPIHost.ExecuteWindowEval and SciterAPIHost.ShowWindowSelectFileDialog
1 parent 914320c commit f64690c

File tree

2 files changed

+63
-0
lines changed

2 files changed

+63
-0
lines changed

src/EmptyFlow.SciterAPI/Client/HostWindowsAPI.cs

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using EmptyFlow.SciterAPI.Enums;
2+
using System.Runtime.CompilerServices;
23

34
namespace EmptyFlow.SciterAPI {
45

@@ -16,6 +17,9 @@ public void CloseWindow ( IntPtr window ) {
1617
m_basicApi.SciterWindowExec ( window, WindowCommand.SCITER_WINDOW_SET_STATE, (int) WindowState.SCITER_WINDOW_STATE_CLOSED, IntPtr.Zero );
1718
}
1819

20+
[MethodImpl ( MethodImplOptions.AggressiveInlining )]
21+
public void ExecuteWindowEval ( nint window, string script, out SciterValue result ) => m_basicApi.SciterEval ( m_mainWindow, script, (uint) script.Length, out result );
22+
1923
/// <summary>
2024
/// Get main window size and position.
2125
/// </summary>
@@ -324,8 +328,65 @@ public bool SetWindowResizable ( nint window, bool state ) {
324328
return false;
325329
}
326330

331+
public IEnumerable<string>? ShowWindowSelectFileDialog ( nint window, SciterSelectFileDialogMode mode, IDictionary<string, string> filters, string defaultExtension, string? caption = default, string? path = default ) {
332+
var keys = new Dictionary<string, string> ();
333+
var scriptMode = mode switch {
334+
SciterSelectFileDialogMode.Save => "save",
335+
SciterSelectFileDialogMode.Open => "open",
336+
SciterSelectFileDialogMode.OpenMultiple => "open-multiple",
337+
_ => "open"
338+
};
339+
keys.Add ( "mode", scriptMode );
340+
keys.Add ( "filter", string.Join ( '|', filters.Select ( a => string.Join ( '|', a.Key, a.Value ) ) ) );
341+
var scriptCaption = mode switch {
342+
SciterSelectFileDialogMode.Save => "Save As",
343+
SciterSelectFileDialogMode.Open => "Open File",
344+
SciterSelectFileDialogMode.OpenMultiple => "Open Files",
345+
_ => "Open File"
346+
};
347+
keys.Add ( "caption", scriptCaption );
348+
keys.Add ( "extension", defaultExtension );
349+
if ( path != default ) keys.Add ( "path", path );
350+
351+
var properties = "{" + string.Join ( ",", keys.Select ( a => a.Key + ": \"" + a.Value + "\"" ) ) + "}";
352+
353+
var script = $"Window.this.selectFile({properties})";
354+
if ( m_basicApi.SciterEval ( m_mainWindow, script, (uint) script.Length, out var result ) ) {
355+
if ( result.IsErrorString || result.IsObjectError ) {
356+
Console.WriteLine ( "ShowWindowSelectFileDialog: error occurs when calling! " );
357+
return null;
358+
}
359+
360+
if ( result.IsString ) return [GetValueString ( ref result )];
361+
if ( result.IsArray || result.IsArrayLike ) {
362+
List<string> files = [];
363+
var count = GetArrayOrMapCount ( ref result );
364+
for ( var i = 0; i < count; i++ ) {
365+
var element = GetArrayItem ( ref result, i );
366+
files.Add ( GetValueString ( ref element ) );
367+
}
368+
return files;
369+
}
370+
if ( result.IsNull ) return null;
371+
372+
return null;
373+
}
374+
375+
return null;
376+
}
377+
327378
}
328379

380+
public enum SciterSelectFileDialogMode {
381+
382+
Save = 0,
383+
384+
Open = 1,
385+
386+
OpenMultiple = 2
387+
388+
};
389+
329390
public enum SciterWindowFrameType {
330391

331392
Unknown = 0,

src/EmptyFlow.SciterAPI/EmptyFlow.SciterAPI.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@
3636
SciterAPIHost.SetWindowFrameType set value frameType
3737
SciterAPIHost.GetWindowResizable get value of isResizable
3838
SciterAPIHost.SetWindowResizable set value isResizable
39+
SciterAPIHost.ExecuteWindowEval can evaluate any custom script in context of window
40+
SciterAPIHost.ShowWindowSelectFileDialog show open or save file dialog
3941
</PackageReleaseNotes>
4042
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
4143
</PropertyGroup>

0 commit comments

Comments
 (0)