Skip to content

Commit 1110578

Browse files
committed
Fixes in PseudoSom
1 parent 1248a7f commit 1110578

3 files changed

Lines changed: 107 additions & 51 deletions

File tree

Examples/DeveloperExample/Program.cs

Lines changed: 96 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -2,76 +2,127 @@
22
using EmptyFlow.SciterAPI;
33
using EmptyFlow.SciterAPI.Client;
44
using EmptyFlow.SciterAPI.Client.Models;
5+
using EmptyFlow.SciterAPI.Client.PseudoSom;
56
using EmptyFlow.SciterAPI.Enums;
67
using System.Numerics;
78

89
// These example developer used for experiment with some new features
910
// please use another examples in folder Examples. Thanks!
1011

11-
var pathToSciter = "C:/IDEs/sciter/sciter-js-sdk-6.0.3.2";
12+
var pathToSciter = "C:/IDEs/sciter/sciter-js-sdk-6.0.3.9";
1213

1314
var host = new SciterAPIHost ( Path.Combine ( pathToSciter, "bin/windows/x64" ), true, true );
1415
var path = "file://" + Path.Combine ( pathToSciter, "samples/html/details-summary.htm" );
15-
host.Callbacks.AddAttachBehaviourFactory ( "testbehaviour", ( element ) => new TestGraphicsEventHandler ( element, host ) );
16-
host.CreateMainWindow ( 300, 300, enableDebug: true, enableFeature: true );
16+
//host.Callbacks.AddAttachBehaviourFactory ( "testbehaviour", ( element ) => new TestGraphicsEventHandler ( element, host ) );
17+
host.Callbacks.AddAttachBehaviourFactory ( "psom", ( element ) => new PseudoSomHandler ( element, host ) );
18+
host.EnableDebugMode ();
19+
host.EnableFeatures ( SciterRuntimeFeatures.ALLOW_SOCKET_IO );
20+
host.CreateWindow ( asMain: true, debugOutput: true );
1721
host.AddWindowEventHandler ( new MyWindowEventHandler ( host.MainWindow, host ) );
1822
host.LoadFile ( path );
1923
host.Process ();
2024

2125

2226
public class MyWindowEventHandler : WindowEventHandler {
2327

24-
public MyWindowEventHandler ( nint window, SciterAPIHost host ) : base ( window, host ) {
25-
}
26-
27-
public override void BehaviourEvent ( BehaviourEvents cmd, nint heTarget, nint he, nint reason, SciterValue data, string name ) {
28-
if ( cmd == BehaviourEvents.DOCUMENT_READY ) {
29-
Host.MoveWindow ( Host.MainWindow, new SciterWindowPosition ( 800, 300 ) );
30-
var files = Host.ShowWindowSelectFileDialog (
31-
Host.MainWindow,
32-
SciterSelectFileDialogMode.Open,
33-
new Dictionary<string, string> {
34-
["Pdf Files (*.pdf)"] = "*.pdf",
35-
["All Files (*.*)"] = "*.*",
36-
},
37-
"pdf",
38-
"Lalala",
39-
"" );
40-
if ( files?.Any () == true ) {
41-
42-
}
43-
}
44-
}
28+
public MyWindowEventHandler ( nint window, SciterAPIHost host ) : base ( window, host ) {
29+
}
30+
31+
public override void BehaviourEvent ( BehaviourEvents cmd, nint heTarget, nint he, nint reason, SciterValue data, string name ) {
32+
if ( cmd == BehaviourEvents.DOCUMENT_READY ) {
33+
var value = Host.GetWindowParent ( Host.MainWindow );
34+
if ( value == 0 ) {
35+
36+
}
37+
}
38+
}
39+
40+
}
41+
42+
public class TestModel : IPseudoSomModel {
43+
44+
private readonly SciterAPIHost m_host;
45+
46+
public TestModel ( SciterAPIHost host ) {
47+
m_host = host;
48+
}
49+
50+
public SciterValue CallMethod ( string name, IEnumerable<SciterValue> parameters ) {
51+
return m_host.CreateNullValue ();
52+
}
53+
54+
public HashSet<string> GetMethods () {
55+
return [];
56+
}
57+
58+
public string GetModelName () => "mymodel";
59+
60+
public HashSet<string> GetProperties () => ["counter"];
61+
62+
public int Counter { get; set; } = 5;
63+
64+
public SciterValue GetPropetyValue ( string name ) {
65+
return name switch {
66+
"counter" => m_host.CreateValue ( Counter ),
67+
_ => m_host.CreateNullValue ()
68+
};
69+
}
70+
71+
public bool SetPropetyValue ( SciterValue value, string name ) {
72+
if ( name == "counter" ) {
73+
Counter = m_host.GetValueInt32 ( ref value );
74+
return true;
75+
}
76+
77+
return false;
78+
}
79+
}
80+
81+
public class PseudoSomHandler : ElementEventHandler {
82+
83+
readonly TestModel _model;
84+
85+
public PseudoSomHandler ( nint element, SciterAPIHost host ) : base ( element, host ) {
86+
_model = new TestModel ( host );
87+
}
88+
89+
public override void AfterRegisterEvent () {
90+
PseudoSom.RegisterModel ( _model, Host, Host.MainWindow, m_subscribedElement );
91+
}
92+
93+
public override (SciterValue? value, bool handled) ScriptMethodCall ( string name, IEnumerable<SciterValue> arguments ) {
94+
return PseudoSom.Handle ( _model, Host, name, arguments );
95+
}
4596

4697
}
4798

4899
public class TestGraphicsEventHandler : ElementEventHandler {
49100

50-
public TestGraphicsEventHandler ( nint element, SciterAPIHost host ) : base ( element, host ) {
51-
Text = Host.GraphicsCreateTextForElement ( element, "test text!!!111", "test-class" );
52-
Text2 = Host.GraphicsCreateTextForElementWithStyle ( element, "test text!!!111", "font-size: 18px;color: green;" );
53-
Color1 = Host.GraphicsGetColor ( 0, 204, 0 );
54-
Color2 = Host.GraphicsGetColor ( 0, 0, 255 );
55-
}
101+
public TestGraphicsEventHandler ( nint element, SciterAPIHost host ) : base ( element, host ) {
102+
Text = Host.GraphicsCreateTextForElement ( element, "test text!!!111", "test-class" );
103+
Text2 = Host.GraphicsCreateTextForElementWithStyle ( element, "test text!!!111", "font-size: 18px;color: green;" );
104+
Color1 = Host.GraphicsGetColor ( 0, 204, 0 );
105+
Color2 = Host.GraphicsGetColor ( 0, 0, 255 );
106+
}
56107

57-
public uint Color1 { get; set; }
108+
public uint Color1 { get; set; }
58109

59-
public uint Color2 { get; set; }
110+
public uint Color2 { get; set; }
60111

61-
public GraphicsTextModel Text { get; set; }
112+
public GraphicsTextModel Text { get; set; }
62113

63-
public GraphicsTextModel Text2 { get; set; }
114+
public GraphicsTextModel Text2 { get; set; }
64115

65-
public override void DrawEvent ( DrawEvents command, nint gfx, SciterRectangle area, uint reserved ) {
66-
if ( command == DrawEvents.DRAW_CONTENT ) {
67-
Host.GraphicsSaveState ( gfx );
68-
Host.GraphicsFillColor ( gfx, Color1 );
69-
Host.GraphicsDrawRectangle ( gfx, area.Left, area.Top, area.Left + area.Width, area.Top + area.Height );
70-
Host.GraphicsDrawLine ( gfx, area.LeftTopCorner, area.RightBottomCorner, Color2, 10 );
71-
Host.GraphicsDrawText ( gfx, Text, new Vector2 ( area.Left, area.Top ), SciterTextPosition.TopLeft );
72-
Host.GraphicsDrawText ( gfx, Text2, new Vector2 ( area.Left, area.Top + 30 ), SciterTextPosition.TopLeft );
73-
Host.GraphicsRestoreState ( gfx );
74-
}
75-
}
116+
public override void DrawEvent ( DrawEvents command, nint gfx, SciterRectangle area, uint reserved ) {
117+
if ( command == DrawEvents.DRAW_CONTENT ) {
118+
Host.GraphicsSaveState ( gfx );
119+
Host.GraphicsFillColor ( gfx, Color1 );
120+
Host.GraphicsDrawRectangle ( gfx, area.Left, area.Top, area.Left + area.Width, area.Top + area.Height );
121+
Host.GraphicsDrawLine ( gfx, area.LeftTopCorner, area.RightBottomCorner, Color2, 10 );
122+
Host.GraphicsDrawText ( gfx, Text, new Vector2 ( area.Left, area.Top ), SciterTextPosition.TopLeft );
123+
Host.GraphicsDrawText ( gfx, Text2, new Vector2 ( area.Left, area.Top + 30 ), SciterTextPosition.TopLeft );
124+
Host.GraphicsRestoreState ( gfx );
125+
}
126+
}
76127

77128
}

src/EmptyFlow.SciterAPI/Client/PseudoSom/PseudoSom.cs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,18 +25,20 @@ public static bool RegisterModel ( IPseudoSomModel model, SciterAPIHost host, ni
2525
var tempId = "temporary-element-" + Guid.NewGuid ().ToString ();
2626
host.SetElementAttribute ( element, tempId, "enabled" );
2727
var script = new StringBuilder ();
28+
script.AppendLine ( $"const element = document.querySelector('[{tempId}]')" );
2829
script.AppendLine ( "const model = Object.create(Object.prototype, {" );
2930

3031
foreach ( var property in model.GetProperties () ) {
3132
script.Append (
3233
$$"""
3334
{{property}}: {
3435
configurable: false,
35-
get() {
36-
return element.xcall('get_' + name);
36+
enumerable: true,
37+
get: () => {
38+
return element.xcall('get_{{property}}');
3739
},
38-
set(value) {
39-
element.xcall('set_' + name, value);
40+
set: (value) => {
41+
element.xcall('set_{{property}}', value);
4042
}
4143
}
4244
"""
@@ -48,19 +50,21 @@ public static bool RegisterModel ( IPseudoSomModel model, SciterAPIHost host, ni
4850
{{method}}: {
4951
writable: false,
5052
configurable: false,
53+
enumerable: true,
5154
value: function(...args) {
52-
element.xcall('call_' + name, args);
55+
element.xcall('call_{{method}}', args);
5356
}
5457
}
5558
"""
5659
);
5760
}
5861

5962
script.AppendLine ( "});" );
60-
script.AppendLine ( $"const element = document.querySelector('[{tempId}]')" );
63+
6164
script.AppendLine ( $"element.{model.GetModelName ()} = model;" );
6265

6366
host.ExecuteWindowEval ( window, script.ToString (), out var result );
67+
6468
if ( result.IsErrorString || result.IsObjectError ) {
6569
return false;
6670
}

src/EmptyFlow.SciterAPI/EmptyFlow.SciterAPI.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
SciterAPIHost.GetWindowParent get window parent pointer
2929
SciterAPIHost.ExecuteWindowFunction execute function in window
3030
Fix show window for version 6.0.3.6 and upper, old version still will be have backawrd compatibility
31+
Added PseudoSom helper
3132
</PackageReleaseNotes>
3233
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
3334
</PropertyGroup>

0 commit comments

Comments
 (0)