-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7d7a235
commit 602e698
Showing
3 changed files
with
101 additions
and
106 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,170 +1,165 @@ | ||
using DOT.Core.Extensions; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading; | ||
using System; | ||
using System.Threading.Tasks; | ||
using System.Windows; | ||
using System.Windows.Controls; | ||
using System.Windows.Data; | ||
using System.Windows.Documents; | ||
using System.Windows.Input; | ||
using System.Windows.Media; | ||
using System.Windows.Media.Imaging; | ||
using System.Windows.Navigation; | ||
using System.Windows.Shapes; | ||
using DOT.Core.Extensions; | ||
using Tick42; | ||
using Tick42.AppManager; | ||
using Tick42.StartingContext; | ||
using Tick42.Windows; | ||
using static MultiWindowDemo.ChartWindow; | ||
|
||
namespace MultiWindowDemo | ||
{ | ||
/// <summary> | ||
/// Interaction logic for MainWindow.xaml | ||
/// Interaction logic for MainWindow.xaml | ||
/// </summary> | ||
public partial class MainWindow : Window | ||
{ | ||
public Glue42 glue42_; | ||
private const string ChartWindowAppName = "ChartChild"; | ||
private const string ColorWindowAppName = "ColorChild"; | ||
|
||
// The shape of the state that will be saved and restored with the window | ||
public class AppState | ||
{ | ||
public int SelectedIndex { get; set; } | ||
} | ||
|
||
public string TabGroupId { get; set; } | ||
|
||
private readonly string ColorWindowAppName = "ColorChild"; | ||
private readonly string ChartWindowAppName = "ChartChild"; | ||
private Glue42 glue42_; | ||
|
||
public MainWindow() | ||
{ | ||
InitializeComponent(); | ||
|
||
var initializeOptions = new InitializeOptions(); | ||
initializeOptions.SetSaveRestoreStateEndpoint(_ => | ||
// Returning the state that has to be saved when the applicaiton is saved in a layout | ||
// In this case the app saves the selected color from the dropdown | ||
new AppState | ||
{ | ||
SelectedIndex = ColorSelector.SelectedIndex | ||
}.AsCompletedTask(), null, Dispatcher); | ||
// Returning the state that has to be saved when the applicaiton is saved in a layout | ||
// In this case the app saves the selected color from the dropdown | ||
new AppState | ||
{ | ||
SelectedIndex = ColorSelector.SelectedIndex | ||
}.AsCompletedTask(), null, Dispatcher); | ||
|
||
Task<Glue42> g = Glue42.InitializeGlue(initializeOptions); | ||
|
||
// Registering the main window and then continuing with the registration of the additional applications | ||
RegisterMainWindow(g).ContinueWith(r => | ||
RegisterMainWindow(g).ContinueWith(async r => | ||
{ | ||
// glue will be initialized with the registration of the main window | ||
glue42_ = g.Result; | ||
// Getting the restored state if one is provided (the restored state will be populated when the app is restored from GlueDesktop) | ||
var appState = glue42_.GetRestoreState<AppState>(); | ||
ColorSelector.SelectedIndex = appState?.SelectedIndex ?? -1; | ||
|
||
RegisterColorApp(); | ||
RegisterChartApp(); | ||
await RegisterChartApp().ConfigureAwait(false); | ||
await RegisterColorApp().ConfigureAwait(false); | ||
}, TaskScheduler.FromCurrentSynchronizationContext()); | ||
} | ||
|
||
private void Color_Click(object sender, RoutedEventArgs e) | ||
{ | ||
var currColor = "#FFFFFF"; // white is the default color | ||
|
||
if (ColorSelector.SelectedItem != null) | ||
{ | ||
currColor = ((SolidColorBrush)((Rectangle)ColorSelector.SelectedItem).Fill).Color.ToString(); | ||
} | ||
|
||
var colorWindow = new ColorWindow(currColor); | ||
var synchronizationContext = SynchronizationContext.Current; | ||
public string TabGroupId { get; set; } | ||
|
||
// First the window is registered as a GlueWindow and then an instance of the application which corresponds to the window is registered | ||
RegisterColorWindow(colorWindow).ContinueWith(r => | ||
private async void Color_Click(object sender, RoutedEventArgs e) | ||
{ | ||
var cxt = AppManagerContext.CreateNew(); | ||
cxt.SetObject(new ColorWindow.State | ||
{ | ||
glue42_.AppManager.RegisterInstance(ColorWindowAppName, r.Result.Id, colorWindow, synchronizationContext); | ||
}); | ||
// Too see how to do it in one invocation please see Chart_Click | ||
RectangleColor = "#AAAAAA" | ||
}, glue42_.AGMObjectSerializer); | ||
await (await glue42_.AppManager.AwaitApplication(app => app.Name == ColorWindowAppName)).Start(cxt); | ||
} | ||
|
||
private void Chart_Click(object sender, RoutedEventArgs e) | ||
private void AssociateWindowToAppInstance() | ||
{ | ||
var chartWindow = new ChartWindow(); | ||
var synchronizationContext = SynchronizationContext.Current; | ||
var placement = new GlueWindowScreenPlacement().WithTabGroupId(TabGroupId); // Adding the tab group id so the chart window appears in the same tab group as the main window | ||
var placement = | ||
new GlueWindowScreenPlacement() | ||
.WithTabGroupId( | ||
TabGroupId); // Adding the tab group id so the chart window appears in the same tab group as the main window | ||
|
||
// With the RegisterAppWindow invocation both the window and an instance of the application are being registered | ||
glue42_.GlueWindows.RegisterAppWindow(chartWindow, chartWindow, ChartWindowAppName, | ||
builder => builder | ||
.WithPlacement(placement) | ||
.WithType(GlueWindowType.Tab) | ||
.WithTitle(ChartWindowAppName)); | ||
.WithPlacement(placement) | ||
.WithType(GlueWindowType.Tab) | ||
.WithTitle(ChartWindowAppName)); | ||
|
||
// alternatively if you have a GlueWindow you can use glue42_.AppManager.RegisterInstance to bind it as an application instance | ||
} | ||
|
||
private async void Chart_Click(object sender, RoutedEventArgs e) | ||
{ | ||
var cxt = AppManagerContext.CreateNew(); | ||
cxt.SetObject(new SymbolState | ||
{ | ||
ActiveSymbol = "FIRST.L" | ||
}, glue42_.AGMObjectSerializer); | ||
await (await glue42_.AppManager.AwaitApplication(app => app.Name == ChartWindowAppName)).Start( | ||
cxt); | ||
} | ||
|
||
private Task<IGlueWindow> RegisterMainWindow(Task<Glue42> initGlueTask) | ||
{ | ||
TabGroupId = Guid.NewGuid().ToString(); | ||
return initGlueTask.RegisterWindow(this, gwo => | ||
{ | ||
gwo.WithChannelSupport(true).WithTitle("MultiWindowWPF").WithType(GlueWindowType.Tab); | ||
|
||
// Making sure that the TabGroupId is correct, so the windows can be opened in the same tab group when started from the MainWindow | ||
if (gwo.Placement is GlueWindowScreenPlacement placement && placement.TabGroupId == null) | ||
{ | ||
placement.WithTabGroupId(TabGroupId); | ||
} | ||
else if (gwo.Placement == null) | ||
{ | ||
gwo.Placement = new GlueWindowScreenPlacement().WithTabGroupId(TabGroupId); | ||
} | ||
else if (gwo.Placement is GlueWindowScreenPlacement screenPlacement && screenPlacement.TabGroupId != null) | ||
{ | ||
TabGroupId = screenPlacement.TabGroupId; | ||
} | ||
}); | ||
{ | ||
gwo.WithChannelSupport(true).WithTitle("MultiWindowWPF").WithType(GlueWindowType.Tab); | ||
|
||
// Making sure that the TabGroupId is correct, so the windows can be opened in the same tab group when started from the MainWindow | ||
if (gwo.Placement is GlueWindowScreenPlacement placement && placement.TabGroupId == null) | ||
{ | ||
placement.WithTabGroupId(TabGroupId); | ||
} | ||
else if (gwo.Placement == null) | ||
{ | ||
gwo.Placement = new GlueWindowScreenPlacement().WithBounds(new GlueWindowBounds(0, 0, 600, 600)) | ||
.WithTabGroupId(TabGroupId); | ||
} | ||
else if (gwo.Placement is GlueWindowScreenPlacement screenPlacement && | ||
screenPlacement.TabGroupId != null) | ||
{ | ||
TabGroupId = screenPlacement.TabGroupId; | ||
} | ||
}); | ||
} | ||
|
||
private void RegisterColorApp() | ||
private async Task RegisterColorApp() | ||
{ | ||
// Registering the WPF window as a Glue application and providing the shape of its state | ||
glue42_.AppManager.RegisterWPFApp<ColorWindow, ColorWindow.State, MainWindow>(app => | ||
var baseApp = await glue42_.AppManager | ||
.AwaitApplication(app => app.Name == ChartWindowAppName).ConfigureAwait(false); | ||
|
||
var baseDefinition = await baseApp.GetFullConfig().ConfigureAwait(false); | ||
try | ||
{ | ||
app.WithName(ColorWindowAppName) | ||
.WithTitle(ColorWindowAppName) | ||
.WithContext(this) | ||
.WithType(GlueWindowType.Tab); | ||
}); | ||
await glue42_.AppManager | ||
.RegisterWPFAppAsync<ColorWindow, ColorWindow.State, MainWindow>( | ||
app => | ||
{ | ||
app.WithAppDefinitionModifier(appDef => | ||
{ | ||
baseDefinition.Title = ColorWindowAppName; | ||
baseDefinition.Name = ColorWindowAppName; | ||
baseDefinition.Details.Owner = glue42_.InitializeOptions.ApplicationName; | ||
baseDefinition.Details.WindowStyle = null; | ||
baseDefinition.Type = ApplicationType.ChildWindow; | ||
return baseDefinition; | ||
}); | ||
}).ConfigureAwait(false); | ||
} | ||
catch (Exception e) | ||
{ | ||
Console.WriteLine(e); | ||
} | ||
} | ||
|
||
private void RegisterChartApp() | ||
private Task RegisterChartApp() | ||
{ | ||
// Registering the WPF window as a Glue application and providing the shape of its state | ||
glue42_.AppManager.RegisterWPFApp<ChartWindow, ChartWindow.SymbolState, MainWindow>((app) => | ||
return glue42_.AppManager.RegisterWPFAppAsync<ChartWindow, SymbolState, MainWindow>(app => | ||
{ | ||
app.WithName(ChartWindowAppName) | ||
.WithTitle(ChartWindowAppName) | ||
.WithContext(this) | ||
.WithType(GlueWindowType.Tab); | ||
.WithTitle(ChartWindowAppName) | ||
.WithContext(this) | ||
.WithType(GlueWindowType.Tab); | ||
}); | ||
} | ||
|
||
private Task<IGlueWindow> RegisterColorWindow(Window colorWindow) | ||
// The shape of the state that will be saved and restored with the window | ||
public class AppState | ||
{ | ||
return glue42_.AsCompletedTask().RegisterWindow(colorWindow, gwo => | ||
{ | ||
gwo.WithTitle(ColorWindowAppName).WithType(GlueWindowType.Tab); | ||
|
||
// Making sure the TabGroupId is correct, so the windows can be opened in the same tab group when started from the MainWindow | ||
if (gwo.Placement is GlueWindowScreenPlacement placement && placement.TabGroupId == null) | ||
{ | ||
placement.WithTabGroupId(TabGroupId); | ||
} | ||
else if (gwo.Placement == null) | ||
{ | ||
gwo.Placement = new GlueWindowScreenPlacement().WithTabGroupId(TabGroupId); | ||
} | ||
}); | ||
public int SelectedIndex { get; set; } | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<packages> | ||
<package id="Glue42" version="2018.2174.0.0" targetFramework="net45" /> | ||
<package id="io.Connect.NET" version="1.27.0.0" targetFramework="net45" /> | ||
<package id="LiveCharts" version="0.9.7" targetFramework="net45" /> | ||
<package id="LiveCharts.Wpf" version="0.9.7" targetFramework="net45" /> | ||
</packages> |