|
1 | 1 | namespace LostTech.Stack |
2 | 2 | { |
3 | | - using System; |
4 | | - using System.Collections.Generic; |
5 | 3 | using System.Collections.ObjectModel; |
6 | 4 | using System.Collections.Specialized; |
7 | 5 | using System.Drawing; |
8 | 6 | using System.Diagnostics; |
9 | 7 | using System.IO; |
10 | | - using System.Linq; |
11 | 8 | using System.Reflection; |
12 | 9 | using System.Runtime.CompilerServices; |
13 | 10 | using System.Runtime.InteropServices; |
14 | 11 | using System.Threading; |
15 | | - using System.Threading.Tasks; |
16 | 12 | using System.Windows; |
17 | 13 | using System.Windows.Forms; |
18 | 14 | using System.Windows.Interop; |
@@ -88,12 +84,26 @@ public partial class App : BoilerplateApp, ILayoutsViewModel, IServiceProvider |
88 | 84 | int sessionLocked; |
89 | 85 | bool SessionLocked => this.sessionLocked != 0; |
90 | 86 | readonly Win32WindowFactory win32WindowFactory = new Win32WindowFactory(); |
| 87 | + readonly Thread uiThread = Thread.CurrentThread; |
| 88 | + readonly Microsoft.Extensions.Hosting.IHost host; |
| 89 | + readonly ILogger<App> log; |
91 | 90 |
|
92 | 91 | public event EventHandler<EventArgs<ScreenLayout>> LayoutLoaded; |
93 | 92 |
|
94 | 93 | internal static readonly bool IsUwp = new DesktopBridge.Helpers().IsRunningAsUwp(); |
95 | 94 |
|
96 | 95 | public App() { |
| 96 | + if (Environment.GetEnvironmentVariable("STACK_CONSOLE") == "1") { |
| 97 | + try { |
| 98 | + ConsoleWindow.Setup(); |
| 99 | + } catch (Exception e) { |
| 100 | + Debug.WriteLine(e.ToString()); |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + var builder = Microsoft.Extensions.Hosting.Host.CreateApplicationBuilder([]); |
| 105 | + this.host = builder.Build(); |
| 106 | + this.log = this.host.Services.GetRequiredService<ILogger<App>>(); |
97 | 107 | this.screenProvider = new Win32ScreenProvider(a => this.Dispatcher.BeginInvoke(a)); |
98 | 108 | } |
99 | 109 |
|
@@ -594,9 +604,10 @@ async Task StartLayout(StackSettings settings) |
594 | 604 | old.PropertyChanged -= ScreenPropertyChanged; |
595 | 605 | @new.PropertyChanged += ScreenPropertyChanged; |
596 | 606 | ScreenLayout layout = this.screenLayouts.FirstOrDefault(l => l.Screen?.ID == old.ID); |
597 | | - if (layout != null) |
| 607 | + if (layout != null) { |
| 608 | + this.log.LogWarning("Old screen not found: {Old} -> {New}", old, @new); |
598 | 609 | layout.ViewModel.Screen = @new; |
599 | | - else { |
| 610 | + } else { |
600 | 611 | RemoveLayoutForScreen(old); |
601 | 612 | await AddLayoutForScreen(@new); |
602 | 613 | } |
@@ -655,6 +666,10 @@ async void LayoutBoundsChanged(object sender, EventArgs e) { |
655 | 666 | } |
656 | 667 |
|
657 | 668 | async void ScreenPropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e) { |
| 669 | + await this.Dispatcher; |
| 670 | + |
| 671 | + if (this.uiThread.ManagedThreadId != Thread.CurrentThread.ManagedThreadId) |
| 672 | + throw new InvalidProgramException("not on UI thread"); |
658 | 673 | var screen = (Win32Screen)sender; |
659 | 674 | switch (e.PropertyName) { |
660 | 675 | case nameof(Win32Screen.WorkingArea): |
|
0 commit comments