Skip to content

Commit 315a0cb

Browse files
bjorn/cnx-1105-add-dynamic-setselection-support-no-events-available (#522)
* refresh GetSelection and UI icon - refreshing GetSelection based on RVT22 approach - adding Speckle Beta icon to the UI * feat: changing default plugin size
1 parent d753ea4 commit 315a0cb

File tree

5 files changed

+94
-5
lines changed

5 files changed

+94
-5
lines changed

Connectors/CSi/Speckle.Connectors.CSiShared/Bindings/CsiSharedSelectionBinding.cs

Lines changed: 53 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,63 @@
33
using Speckle.Connectors.DUI.Bindings;
44
using Speckle.Connectors.DUI.Bridge;
55
using Speckle.Converters.CSiShared.Utils;
6+
using Timer = System.Timers.Timer;
67

78
namespace Speckle.Connectors.CSiShared.Bindings;
89

9-
public class CsiSharedSelectionBinding(IBrowserBridge parent, ICsiApplicationService csiApplicationService)
10-
: ISelectionBinding
10+
public class CsiSharedSelectionBinding : ISelectionBinding, IDisposable
1111
{
12+
private bool _disposed;
13+
private readonly Timer _selectionTimer;
14+
private readonly ICsiApplicationService _csiApplicationService;
15+
private HashSet<string> _lastSelection = new();
16+
17+
public IBrowserBridge Parent { get; }
1218
public string Name => "selectionBinding";
13-
public IBrowserBridge Parent { get; } = parent;
19+
20+
public CsiSharedSelectionBinding(
21+
IBrowserBridge parent,
22+
ICsiApplicationService csiApplicationService,
23+
ITopLevelExceptionHandler topLevelExceptionHandler
24+
)
25+
{
26+
Parent = parent;
27+
_csiApplicationService = csiApplicationService;
28+
29+
_selectionTimer = new Timer(1000);
30+
_selectionTimer.Elapsed += (_, _) => topLevelExceptionHandler.CatchUnhandled(CheckSelectionChanged);
31+
_selectionTimer.Start();
32+
}
33+
34+
private void CheckSelectionChanged()
35+
{
36+
var currentSelection = GetSelection();
37+
var currentIds = new HashSet<string>(currentSelection.SelectedObjectIds);
38+
39+
if (!_lastSelection.SetEquals(currentIds))
40+
{
41+
_lastSelection = currentIds;
42+
Parent.Send(SelectionBindingEvents.SET_SELECTION, currentSelection);
43+
}
44+
}
45+
46+
protected virtual void Dispose(bool disposing)
47+
{
48+
if (!_disposed)
49+
{
50+
if (disposing)
51+
{
52+
_selectionTimer?.Dispose();
53+
}
54+
_disposed = true;
55+
}
56+
}
57+
58+
public void Dispose()
59+
{
60+
Dispose(true);
61+
GC.SuppressFinalize(this);
62+
}
1463

1564
/// <summary>
1665
/// Gets the selection and creates an encoded ID (objectType and objectName).
@@ -24,7 +73,7 @@ public SelectionInfo GetSelection()
2473
int[] objectType = [];
2574
string[] objectName = [];
2675

27-
csiApplicationService.SapModel.SelectObj.GetSelected(ref numberItems, ref objectType, ref objectName);
76+
_csiApplicationService.SapModel.SelectObj.GetSelected(ref numberItems, ref objectType, ref objectName);
2877

2978
var encodedIds = new List<string>(numberItems);
3079
var typeCounts = new Dictionary<string, int>();

Connectors/CSi/Speckle.Connectors.CSiShared/Plugin/CsiPluginBase.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ namespace Speckle.Connectors.CSiShared;
33
[System.Diagnostics.CodeAnalysis.SuppressMessage("Style", "IDE1006:Naming Styles", Justification = "<Pending>")]
44
public abstract class CSiPluginBase : cPluginContract, IDisposable
55
{
6+
private const string s_modality = "Non-Modal";
67
private SpeckleFormBase? _panel;
78
private bool _disposed;
89

@@ -11,7 +12,15 @@ public void Main(ref cSapModel sapModel, ref cPluginCallback pluginCallback)
1112
_panel = CreateForm();
1213
_panel.Initialize(ref sapModel, ref pluginCallback);
1314
_panel.FormClosed += (_, _) => Dispose();
14-
_panel.ShowDialog();
15+
16+
if (string.Equals(s_modality, "Non-Modal", StringComparison.OrdinalIgnoreCase))
17+
{
18+
_panel.Show();
19+
}
20+
else
21+
{
22+
_panel.ShowDialog();
23+
}
1524
}
1625

1726
protected abstract SpeckleFormBase CreateForm();

Connectors/CSi/Speckle.Connectors.CSiShared/Plugin/SpeckleFormBase.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System.ComponentModel;
2+
using System.Reflection;
23
using System.Windows.Forms.Integration;
34
using Microsoft.Extensions.DependencyInjection;
45
using Speckle.Connectors.Common;
@@ -22,6 +23,7 @@ public abstract class SpeckleFormBase : Form, ICsiApplicationService
2223
protected SpeckleFormBase()
2324
{
2425
Text = "Speckle (Beta)";
26+
Size = new System.Drawing.Size(400, 600);
2527
}
2628

2729
public cSapModel SapModel { get; private set; }
@@ -39,16 +41,37 @@ protected virtual void ConfigureServices(IServiceCollection services)
3941

4042
public void Initialize(ref cSapModel sapModel, ref cPluginCallback pluginCallback)
4143
{
44+
// store app-specific model and callback references (callback if at all possible?)
4245
SapModel = sapModel;
4346
_pluginCallback = pluginCallback;
4447

48+
string assemblyName =
49+
Assembly.GetExecutingAssembly().GetName().Name
50+
?? throw new InvalidOperationException("Could not determine executing assembly name");
51+
string resourcePath = $"{assemblyName}.Resources.et_element_Speckle.bmp";
52+
53+
// load and set the speckle icon from embedded resources
54+
using (var stream = System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream(resourcePath))
55+
{
56+
if (stream == null)
57+
{
58+
throw new InvalidOperationException($"Could not find resource: {resourcePath}");
59+
}
60+
61+
using var bmp = new Bitmap(stream);
62+
Icon = Icon.FromHandle(bmp.GetHicon());
63+
}
64+
65+
// configure dependency injection services
4566
var services = new ServiceCollection();
4667
services.AddSingleton<ICsiApplicationService>(this);
4768
ConfigureServices(services);
4869

70+
// build service container and initialize ui framework
4971
_container = services.BuildServiceProvider();
5072
_container.UseDUI();
5173

74+
// setup webview control and form properties
5275
var webview = _container.GetRequiredService<DUI3ControlWebView>();
5376
Host = new() { Child = webview, Dock = DockStyle.Fill };
5477
Controls.Add(Host);
Binary file not shown.

Connectors/CSi/Speckle.Connectors.CSiShared/Speckle.Connectors.CSiShared.projitems

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@
88
<PropertyGroup Label="Configuration">
99
<Import_RootNamespace>Speckle.Connectors.CSiShared</Import_RootNamespace>
1010
</PropertyGroup>
11+
<ItemGroup>
12+
<EmbeddedResource Include="$(MSBuildThisFileDirectory)Resources\et_element_Speckle.bmp">
13+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
14+
</EmbeddedResource>
15+
</ItemGroup>
1116
<ItemGroup>
1217
<Compile Include="$(MSBuildThisFileDirectory)Bindings\CsiSharedBasicConnectorBinding.cs" />
1318
<Compile Include="$(MSBuildThisFileDirectory)Bindings\CsiSharedSelectionBinding.cs" />
@@ -30,4 +35,7 @@
3035
<Compile Include="$(MSBuildThisFileDirectory)ServiceRegistration.cs" />
3136
<Compile Include="$(MSBuildThisFileDirectory)Utils\ObjectIdentifiers.cs" />
3237
</ItemGroup>
38+
<ItemGroup>
39+
<Content Include="$(MSBuildThisFileDirectory)Resources\et_element_Speckle.bmp" />
40+
</ItemGroup>
3341
</Project>

0 commit comments

Comments
 (0)