Remotely/Desktop.Linux/Views/ChatWindow.axaml.cs

71 lines
2.3 KiB
C#

using Avalonia;
using Avalonia.Controls;
using Avalonia.LogicalTree;
using Avalonia.Markup.Xaml;
using Avalonia.VisualTree;
using ReactiveUI;
using Remotely.Desktop.Linux.ViewModels;
using System;
using System.Linq;
using System.Threading.Tasks;
namespace Remotely.Desktop.Linux.Views
{
public class ChatWindow : Window
{
public ChatWindow()
{
this.InitializeComponent();
#if DEBUG
this.AttachDevTools();
#endif
}
private ChatWindowViewModel ViewModel => DataContext as ChatWindowViewModel;
private void ChatWindow_Closed(object sender, System.EventArgs e)
{
Environment.Exit(0);
}
private async void ChatWindow_KeyUp(object sender, Avalonia.Input.KeyEventArgs e)
{
if (e.Key == Avalonia.Input.Key.Enter)
{
await ViewModel.SendChatMessage();
}
}
private void InitializeComponent()
{
AvaloniaXamlLoader.Load(this);
this.Closed += ChatWindow_Closed;
this.FindControl<Border>("TitleBanner").PointerPressed += TitleBanner_PointerPressed;
this.FindControl<TextBox>("InputTextBox").KeyUp += ChatWindow_KeyUp;
this.FindControl<ItemsControl>("MessagesListBox").ItemContainerGenerator.Materialized += ItemContainerGenerator_Materialized;
}
private async void ItemContainerGenerator_Materialized(object sender, Avalonia.Controls.Generators.ItemContainerEventArgs e)
{
// Allows listbox height to adjust to content before scrolling the scrollviewer.
await Task.Delay(1);
// TODO: Replace with ScrollToEnd when implemented.
var scrollViewer = this.FindControl<ScrollViewer>("MessagesScrollViewer");
var listBox = this.FindControl<ItemsControl>("MessagesListBox");
scrollViewer.Offset = new Vector(0, listBox.Bounds.Height);
}
private void TitleBanner_PointerPressed(object sender, Avalonia.Input.PointerPressedEventArgs e)
{
if (e.GetCurrentPoint(this).Properties.PointerUpdateKind == Avalonia.Input.PointerUpdateKind.LeftButtonPressed)
{
this.BeginMoveDrag(e);
}
}
}
}