Skip to content

Commit 28686d1

Browse files
committed
+ Added PseudoSom scaffold
+ Added SciterAPIHost.ExecuteWindowFunction
1 parent e5ae48a commit 28686d1

4 files changed

Lines changed: 119 additions & 0 deletions

File tree

src/EmptyFlow.SciterAPI/Client/HostWindowsAPI.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,10 @@ public nint CreateWindow ( int width = 0, int height = 0, int x = 0, int y = 0,
7676
return windowPointer;
7777
}
7878

79+
/// <summary>
80+
/// Activate and show window on desktop.
81+
/// </summary>
82+
/// <param name="windowPointer"></param>
7983
public void ShowWindow ( nint windowPointer ) {
8084
//activate window
8185
m_basicApi.SciterWindowExec ( windowPointer, WindowCommand.SCITER_WINDOW_ACTIVATE, 1, nint.Zero );
@@ -84,9 +88,29 @@ public void ShowWindow ( nint windowPointer ) {
8488
m_basicApi.SciterWindowExec ( windowPointer, WindowCommand.SCITER_WINDOW_SET_STATE, 1, nint.Zero );
8589
}
8690

91+
/// <summary>
92+
/// Make JavaScript evaluation for passed script on window.
93+
/// </summary>
94+
/// <param name="window">Window for context.</param>
95+
/// <param name="script">Any JavaScript source code.</param>
96+
/// <param name="result">Result of evaluation.</param>
97+
/// <returns></returns>
8798
[MethodImpl ( MethodImplOptions.AggressiveInlining )]
8899
public bool ExecuteWindowEval ( nint window, string script, out SciterValue result ) => m_basicApi.SciterEval ( window, script, (uint) script.Length, out result );
89100

101+
/// <summary>
102+
/// Execute JavaScript function inside window.
103+
/// </summary>
104+
/// <param name="window">Window for context.</param>
105+
/// <param name="functionName">Name of function.</param>
106+
/// <param name="parameters">Parameters which will be passed to function.</param>
107+
/// <param name="result">Result of function.</param>
108+
/// <returns></returns>
109+
[MethodImpl ( MethodImplOptions.AggressiveInlining )]
110+
public bool ExecuteWindowFunction ( nint window, string functionName, IEnumerable<SciterValue> parameters, out SciterValue result ) {
111+
return m_basicApi.SciterCall ( window, functionName, (uint) parameters.Count (), [.. parameters], out result );
112+
}
113+
90114
/// <summary>
91115
/// Get window size and position.
92116
/// </summary>
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
namespace EmptyFlow.SciterAPI.Client.PseudoSom {
2+
public interface IPseudoSomModel {
3+
4+
SciterValue GetPropetyValue ( string name );
5+
6+
bool SetPropetyValue ( SciterValue value, string name );
7+
8+
SciterValue CallMethod ( string name, IEnumerable<SciterValue> parameters );
9+
10+
HashSet<string> GetProperties ();
11+
12+
HashSet<string> GetMethods ();
13+
14+
string GetModelName ();
15+
16+
}
17+
18+
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
using System.Text;
2+
3+
namespace EmptyFlow.SciterAPI.Client.PseudoSom {
4+
5+
public static class PseudoSom {
6+
7+
public static SciterValue Handle ( IPseudoSomModel model, SciterAPIHost host, string method, IEnumerable<SciterValue> parameters ) {
8+
var (target, name) = GetTarget ( method );
9+
10+
switch ( target ) {
11+
case "get":
12+
return model.GetPropetyValue ( name );
13+
case "set":
14+
if ( !parameters.Any () ) return host.CreateNullValue ();
15+
16+
var setResult = model.SetPropetyValue ( parameters.FirstOrDefault (), name );
17+
return host.CreateValue ( setResult );
18+
case "call":
19+
return model.CallMethod ( name, parameters );
20+
default: return host.CreateNullValue ();
21+
}
22+
}
23+
24+
public static void RegisterModel ( IPseudoSomModel model, SciterAPIHost host, nint window, nint element ) {
25+
var tempId = "temporary-element-" + Guid.NewGuid().ToString();
26+
host.SetElementAttribute ( element, tempId, "enabled" );
27+
var script = new StringBuilder ();
28+
script.AppendLine ( "const model = Object.create(Object.prototype, {" );
29+
30+
foreach ( var property in model.GetProperties () ) {
31+
script.Append (
32+
$$"""
33+
{{property}}: {
34+
configurable: false,
35+
get() {
36+
return element.xcall('get_' + name);
37+
},
38+
set(value) {
39+
element.xcall('set_' + name, value);
40+
}
41+
}
42+
"""
43+
);
44+
}
45+
foreach ( var method in model.GetMethods () ) {
46+
script.Append (
47+
$$"""
48+
{{method}}: {
49+
writable: false,
50+
configurable: false,
51+
value: function(...args) {
52+
element.xcall('call_' + name, args);
53+
}
54+
}
55+
"""
56+
);
57+
}
58+
59+
script.AppendLine ( "});" );
60+
script.AppendLine ( $"const element = document.querySelector('[attr=\"{tempId}\"]')" );
61+
script.AppendLine ( $"element.{model.GetModelName()} = model;" );
62+
63+
host.ExecuteWindowEval ( window, script.ToString (), out var result );
64+
}
65+
66+
private static (string target, string name) GetTarget ( string method ) {
67+
var parts = method.Split ( '_' );
68+
var firstPath = parts[0];
69+
var secondPath = parts[1];
70+
71+
return (firstPath, secondPath);
72+
}
73+
74+
}
75+
76+
}

src/EmptyFlow.SciterAPI/EmptyFlow.SciterAPI.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
SciterAPIHost.GetWindowFocus get focus element in window
2727
SciterAPIHost.GetWindowScreen get window current screen
2828
SciterAPIHost.GetWindowParent get window parent pointer
29+
SciterAPIHost.ExecuteWindowFunction execute function in window
2930
</PackageReleaseNotes>
3031
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
3132
</PropertyGroup>

0 commit comments

Comments
 (0)