Skip to content
This repository was archived by the owner on Aug 28, 2021. It is now read-only.

Commit 757af25

Browse files
authored
Merge pull request #271 from speckleworks/Dimitrie/dev/popup-flow
Dimitrie/dev/popup flow
2 parents d99c8c3 + 1751283 commit 757af25

File tree

8 files changed

+558
-443
lines changed

8 files changed

+558
-443
lines changed

SpeckleGrasshopper/BaseComponents/GhReceiverCoreClient.cs

Lines changed: 47 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ public override bool Write( GH_IWriter writer )
7676
formatter.Serialize( ms, Client );
7777
writer.SetByteArray( "speckleclient", ms.ToArray() );
7878
}
79-
writer.SetBoolean("deserialize", this.Deserialize);
79+
writer.SetBoolean( "deserialize", this.Deserialize );
8080
}
8181
catch { }
8282
return base.Write( writer );
@@ -101,7 +101,7 @@ public override bool Read( GH_IReader reader )
101101
InitReceiverEventsAndGlobals();
102102
}
103103

104-
this.Deserialize = reader.GetBoolean("deserialize");
104+
this.Deserialize = reader.GetBoolean( "deserialize" );
105105
}
106106
catch
107107
{
@@ -117,23 +117,34 @@ public override void AddedToDocument( GH_Document document )
117117

118118
if ( Client == null )
119119
{
120-
var myForm = new SpecklePopup.MainWindow( false, true );
121-
122-
var some = new System.Windows.Interop.WindowInteropHelper( myForm );
123-
some.Owner = Rhino.RhinoApp.MainWindowHandle();
124-
125-
myForm.ShowDialog();
126-
127-
if ( myForm.restApi != null && myForm.apitoken != null )
120+
Account account = null;
121+
try
128122
{
129-
RestApi = myForm.restApi;
130-
AuthToken = myForm.apitoken;
123+
account = LocalContext.GetDefaultAccount();
131124
}
132-
else
125+
catch ( Exception err )
133126
{
134-
AddRuntimeMessage( GH_RuntimeMessageLevel.Error, "Account selection failed." );
135-
return;
136127
}
128+
129+
if ( account == null )
130+
{
131+
var signInWindow = new SpecklePopup.SignInWindow( true );
132+
var helper = new System.Windows.Interop.WindowInteropHelper( signInWindow );
133+
helper.Owner = Rhino.RhinoApp.MainWindowHandle();
134+
135+
signInWindow.ShowDialog();
136+
137+
if ( signInWindow.AccountListBox.SelectedIndex != -1 )
138+
account = signInWindow.accounts[ signInWindow.AccountListBox.SelectedIndex ];
139+
else
140+
{
141+
AddRuntimeMessage( GH_RuntimeMessageLevel.Error, "Account selection failed." );
142+
return;
143+
}
144+
}
145+
146+
RestApi = account.RestApi;
147+
AuthToken = account.Token;
137148
}
138149

139150
StreamIdChanger = new System.Timers.Timer( 1000 ); StreamIdChanger.Enabled = false;
@@ -185,15 +196,15 @@ public override void AppendAdditionalMenuItems( ToolStripDropDown menu )
185196
GH_DocumentObject.Menu_AppendSeparator( menu );
186197

187198
base.AppendAdditionalMenuItems( menu );
188-
var toggleItem = new ToolStripMenuItem("Deserialize objects.") { Name = "Deserialize objects.", Checked = this.Deserialize, CheckOnClick = true };
189-
toggleItem.CheckStateChanged += (sender, e) =>
199+
var toggleItem = new ToolStripMenuItem( "Deserialize objects." ) { Name = "Deserialize objects.", Checked = this.Deserialize, CheckOnClick = true };
200+
toggleItem.CheckStateChanged += ( sender, e ) =>
190201
{
191-
this.Deserialize = ((ToolStripMenuItem)sender).Checked;
192-
Rhino.RhinoApp.MainApplicationWindow.Invoke(expireComponentAction);
202+
this.Deserialize = ( ( ToolStripMenuItem ) sender ).Checked;
203+
Rhino.RhinoApp.InvokeOnUiThread( expireComponentAction );
193204
};
194-
menu.Items.Add(toggleItem);
205+
menu.Items.Add( toggleItem );
195206

196-
GH_DocumentObject.Menu_AppendSeparator(menu);
207+
GH_DocumentObject.Menu_AppendSeparator( menu );
197208

198209
GH_DocumentObject.Menu_AppendItem( menu, "Force refresh.", ( sender, e ) =>
199210
{
@@ -206,7 +217,7 @@ public override void AppendAdditionalMenuItems( ToolStripDropDown menu )
206217
GH_DocumentObject.Menu_AppendItem( menu, "View stream.", ( sender, e ) =>
207218
{
208219
if ( StreamId == null ) return;
209-
System.Diagnostics.Process.Start(RestApi.Replace("/api/v1", "/#/view").Replace("/api", "/#/view") + @"/" + StreamId);
220+
System.Diagnostics.Process.Start( RestApi.Replace( "/api/v1", "/#/view" ).Replace( "/api", "/#/view" ) + @"/" + StreamId );
210221
} );
211222

212223
GH_DocumentObject.Menu_AppendItem( menu, "(API) View stream data.", ( sender, e ) =>
@@ -326,14 +337,14 @@ public virtual void UpdateGlobal( )
326337
this.Message = SNJ.JsonConvert.SerializeObject( String.Format( "{0}/{1}", i, payload.Length ) );
327338
}
328339

329-
foreach( var obj in newObjects )
340+
foreach ( var obj in newObjects )
330341
{
331342
var matches = Client.Stream.Objects.FindAll( o => o._id == obj._id );
332343

333344
//TODO: Do this efficiently, this is rather brute force
334-
for( int i = Client.Stream.Objects.Count - 1; i >= 0; i-- )
345+
for ( int i = Client.Stream.Objects.Count - 1; i >= 0; i-- )
335346
{
336-
if(Client.Stream.Objects[i]._id == obj._id)
347+
if ( Client.Stream.Objects[ i ]._id == obj._id )
337348
{
338349
Client.Stream.Objects[ i ] = obj;
339350
}
@@ -356,14 +367,14 @@ public virtual void UpdateGlobal( )
356367

357368
SpeckleObjects.Clear();
358369

359-
Task.Run(() =>
360-
{
361-
ConvertedObjects = SpeckleCore.Converter.Deserialise(Client.Stream.Objects);
362-
IsUpdating = false;
363-
Rhino.RhinoApp.MainApplicationWindow.Invoke(expireComponentAction);
370+
Task.Run( ( ) =>
371+
{
372+
ConvertedObjects = SpeckleCore.Converter.Deserialise( Client.Stream.Objects );
373+
IsUpdating = false;
374+
Rhino.RhinoApp.InvokeOnUiThread( expireComponentAction );
364375

365-
this.Message = "Got data\n@" + DateTime.Now.ToString("hh:mm:ss");
366-
});
376+
this.Message = "Got data\n@" + DateTime.Now.ToString( "hh:mm:ss" );
377+
} );
367378
}
368379

369380
public virtual void UpdateMeta( )
@@ -471,7 +482,7 @@ public void UpdateOutputStructure( )
471482
foreach ( var layer in toAdd )
472483
{
473484
Param_GenericObject newParam = getGhParameter( layer );
474-
Params.RegisterOutputParam( newParam, layer.OrderIndex != null ? ( int ) layer.OrderIndex : k );
485+
Params.RegisterOutputParam( newParam, layer.OrderIndex != null ? ( int ) layer.OrderIndex : k );
475486
k++;
476487
}
477488

@@ -487,10 +498,10 @@ public void SetObjects( IGH_DataAccess DA )
487498
{
488499
if ( Layers == null ) return;
489500
if ( ConvertedObjects.Count == 0 && this.Deserialize ) return;
490-
if ( Client.Stream.Objects.Count == 0 && !this.Deserialize) return;
501+
if ( Client.Stream.Objects.Count == 0 && !this.Deserialize ) return;
491502

492503
List<object> chosenObjects;
493-
if (this.Deserialize)
504+
if ( this.Deserialize )
494505
chosenObjects = ConvertedObjects;
495506
else
496507
chosenObjects = Client.Stream.Objects.Cast<object>().ToList();
@@ -528,7 +539,7 @@ public void SetObjects( IGH_DataAccess DA )
528539
subsetCount += elCount;
529540
}
530541
}
531-
DA.SetDataTree( layer.OrderIndex!=null ? ( int ) layer.OrderIndex : k, tree );
542+
DA.SetDataTree( layer.OrderIndex != null ? ( int ) layer.OrderIndex : k, tree );
532543
k++;
533544
}
534545
}

0 commit comments

Comments
 (0)