Skip to content

Commit 7f71967

Browse files
committed
fix(Windows): ChatWindow initialization issues
1 parent f9d95e3 commit 7f71967

File tree

4 files changed

+37
-41
lines changed

4 files changed

+37
-41
lines changed

src/Everywhere/Initialization/ChatWindowInitializer.cs

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,8 @@ public Task InitializeAsync()
4040

4141
HandleChatShortcutChanged(settings.ChatWindow.Shortcut);
4242

43-
Dispatcher.UIThread.Invoke(() =>
44-
{
45-
// Preload ChatWindow to avoid delay on first open
46-
var chatWindow = ServiceLocator.Resolve<ChatWindow>();
47-
chatWindow.ViewModel.IsOpened = true;
48-
chatWindow.ViewModel.IsOpened = false;
49-
chatWindow.ShowActivated = true;
50-
chatWindow.Topmost = true;
51-
});
43+
// Preload ChatWindow to avoid delay on first open
44+
Dispatcher.UIThread.Invoke(() => ServiceLocator.Resolve<ChatWindow>().Initialize());
5245

5346
return Task.CompletedTask;
5447
}
@@ -74,9 +67,14 @@ private void HandleChatShortcutChanged(KeyboardShortcut shortcut)
7467
Dispatcher.UIThread.Invoke(() =>
7568
{
7669
var chatWindow = ServiceLocator.Resolve<ChatWindow>();
77-
if (hWnd == chatWindow.TryGetPlatformHandle()?.Handle) element = null; // Avoid focusing to itself
78-
79-
chatWindow.ViewModel.TryFloatToTargetElementAsync(element).Detach(logger.ToExceptionHandler());
70+
if (hWnd == chatWindow.TryGetPlatformHandle()?.Handle)
71+
{
72+
chatWindow.ViewModel.IsOpened = false; // Hide chat window if it's already focused
73+
}
74+
else
75+
{
76+
chatWindow.ViewModel.TryFloatToTargetElementAsync(element).Detach(logger.ToExceptionHandler());
77+
}
8078
});
8179
}));
8280
}

src/Everywhere/ViewModels/ChatWindowViewModel.cs

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -175,17 +175,15 @@ public async Task TryFloatToTargetElementAsync(IVisualElement? targetElement)
175175

176176
try
177177
{
178-
if (_chatAttachments.Any(a => a is ChatVisualElementAttachment vea && Equals(vea.Element?.Target, targetElement)))
179-
{
180-
// Toggle the chat window visibility
181-
IsOpened = true;
182-
return;
183-
}
178+
IsOpened = true;
179+
180+
// Avoid adding duplicate attachments
181+
if (_chatAttachments.Any(a => a is ChatVisualElementAttachment vea && Equals(vea.Element?.Target, targetElement))) return;
184182

185183
TargetBoundingRect = default;
186184
if (targetElement == null)
187185
{
188-
IsOpened = false;
186+
if (_chatAttachments is [ChatVisualElementAttachment { IsFocusedElement: true }, ..]) _chatAttachments.RemoveAt(0);
189187
return;
190188
}
191189

@@ -205,8 +203,6 @@ public async Task TryFloatToTargetElementAsync(IVisualElement? targetElement)
205203
_chatAttachments.Insert(0, attachment.With(a => a.IsFocusedElement = true));
206204
}
207205
}
208-
209-
IsOpened = true;
210206
}
211207
catch (Exception ex)
212208
{

src/Everywhere/Views/Windows/ChatWindow.axaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313
IsOpened="{Binding IsOpened, Mode=OneWayToSource}"
1414
IsTitleBarVisible="False"
1515
RequestedThemeVariant="{x:Static themes:ThemeVariants.Acrylic}"
16-
SizeToContent="WidthAndHeight" SystemDecorations="BorderOnly"
16+
ShowActivated="False" SizeToContent="WidthAndHeight"
17+
SystemDecorations="BorderOnly"
1718
TargetBoundingRect="{Binding TargetBoundingRect}"
1819
TransparencyLevelHint="AcrylicBlur" WindowStartupLocation="Manual">
1920

src/Everywhere/Views/Windows/ChatWindow.axaml.cs

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
using Everywhere.Common;
1212
using Everywhere.Configuration;
1313
using Everywhere.Interop;
14-
using Everywhere.Utilities;
1514
using LiveMarkdown.Avalonia;
1615
using Microsoft.Extensions.Logging;
1716
using ShadUI;
@@ -83,6 +82,20 @@ public ChatWindow(
8382
ChatInputBox.PastingFromClipboard += HandleChatInputBoxPastingFromClipboard;
8483
}
8584

85+
/// <summary>
86+
/// Initializes the chat window.
87+
/// </summary>
88+
public void Initialize()
89+
{
90+
EnsureInitialized();
91+
ApplyStyling();
92+
StartRendering();
93+
StopRendering();
94+
_windowHelper.SetCloaked(this, true);
95+
ShowActivated = true;
96+
Topmost = true;
97+
}
98+
8699
private void HandleKeyDown(object? sender, KeyEventArgs e)
87100
{
88101
switch (e.Key)
@@ -190,26 +203,13 @@ protected override Size MeasureOverride(Size availableSize)
190203
return new Size(width, height);
191204
}
192205

193-
private readonly ReusableCancellationTokenSource _lostFocusCts = new();
194-
195-
protected override async void OnLostFocus(RoutedEventArgs e)
206+
protected override void OnLostFocus(RoutedEventArgs e)
196207
{
197-
try
198-
{
199-
base.OnLostFocus(e);
200-
201-
// leave some time for focus to move to another element within the window
202-
_lostFocusCts.Cancel();
203-
await Task.Delay(200, _lostFocusCts.Token);
208+
base.OnLostFocus(e);
204209

205-
if (!ViewModel.IsPickingFiles && !IsActive && !IsWindowPinned && !_windowHelper.AnyModelDialogOpened(this))
206-
{
207-
IsOpened = false;
208-
}
209-
}
210-
catch
210+
if (!ViewModel.IsPickingFiles && !IsActive && !IsWindowPinned && !_windowHelper.AnyModelDialogOpened(this))
211211
{
212-
// Ignore
212+
IsOpened = false;
213213
}
214214
}
215215

@@ -356,13 +356,14 @@ private void HandleViewModelPropertyChanged(object? sender, PropertyChangedEvent
356356
}
357357
}
358358

359-
WindowState = WindowState.Minimized;
360359
ShowInTaskbar = IsWindowPinned;
361360
_windowHelper.SetCloaked(this, false);
361+
StartRendering();
362362
ChatInputBox.Focus();
363363
}
364364
else
365365
{
366+
StopRendering();
366367
ShowInTaskbar = false;
367368
_windowHelper.SetCloaked(this, true);
368369
}

0 commit comments

Comments
 (0)