Skip to content

Commit 16ddf34

Browse files
committed
fix threading bug with logging in ui
1 parent e6e4414 commit 16ddf34

2 files changed

Lines changed: 21 additions & 12 deletions

File tree

Gui/ViewModels/Graphics/ImageViewModel.cs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,16 @@ public GraphicsElementFlags Flags
7777
[Reactive, Browsable(false)]
7878
public Bitmap DisplayedImage { get; private set; }
7979

80-
[Reactive, Browsable(false)]
81-
public Image<Rgba32> UnderlyingImage { get; set; }
80+
[Browsable(false)]
81+
public Image<Rgba32> UnderlyingImage
82+
{
83+
get => Model.Image!;
84+
set
85+
{
86+
Model.Image = value;
87+
this.RaisePropertyChanged(nameof(UnderlyingImage));
88+
}
89+
}
8290

8391
[Browsable(false)]
8492
public Avalonia.Rect SelectedBitmapPreviewBorder
@@ -98,7 +106,7 @@ public ImageViewModel(GraphicsElement graphicsElement)
98106

99107
_ = this.WhenAnyValue(o => o.UnderlyingImage)
100108
.Where(x => x != null)
101-
.Subscribe(_ => UnderlyingImageChanged());
109+
.Subscribe(_ => DisplayedImage = UnderlyingImage!.ToAvaloniaBitmap());
102110

103111
_ = this.WhenAnyValue(o => o.DisplayedImage)
104112
.Subscribe(_ => this.RaisePropertyChanged(nameof(SelectedBitmapPreviewBorder)));
@@ -133,9 +141,6 @@ public void RecolourImage(ColourSwatch primary, ColourSwatch secondary, PaletteM
133141
DisplayedImage = image!.ToAvaloniaBitmap();
134142
}
135143

136-
void UnderlyingImageChanged()
137-
=> DisplayedImage = UnderlyingImage!.ToAvaloniaBitmap();
138-
139144
public void CropImage()
140145
{
141146
var cropRegion = FindCropRegion(UnderlyingImage);
@@ -153,7 +158,7 @@ public void CropImage()
153158
YOffset += (short)cropRegion.Top;
154159
}
155160

156-
UnderlyingImageChanged();
161+
this.RaisePropertyChanged(nameof(UnderlyingImage));
157162

158163
static Rectangle FindCropRegion(Image<Rgba32> image)
159164
{

Gui/ViewModels/MainWindowViewModel.cs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -101,12 +101,16 @@ public MainWindowViewModel()
101101
var log = laea.Log;
102102
if (log.Level is LogLevel.Error)
103103
{
104-
// check if the logs window is already open
105-
if (App.GetOpenWindows().Any(x => x.DataContext is LogWindowViewModel))
104+
// Dispatch the UI-related work to the UI thread.
105+
Avalonia.Threading.Dispatcher.UIThread.Post(() =>
106106
{
107-
return;
108-
}
109-
ShowLogsCommand.Execute();
107+
// check if the logs window is already open
108+
if (App.GetOpenWindows().Any(x => x.DataContext is LogWindowViewModel))
109+
{
110+
return;
111+
}
112+
ShowLogsCommand.Execute();
113+
});
110114
}
111115
}
112116
};

0 commit comments

Comments
 (0)