using System; using Avalonia; using Avalonia.Controls; using Avalonia.Input; using Avalonia.Media; using Avalonia.Platform; namespace SimpleNote; public partial class MainWindow : Window { private ViewModel _viewModel; private PixelRect? _screen; public MainWindow() { if (Screens.Primary == null) { Screen max = Screens.All[0]; for (int i = 0; i < Screens.ScreenCount; i++) { if (Screens.All[i].Bounds.Width > max.Bounds.Width) max = Screens.All[i]; } _screen = max.WorkingArea; } else _screen = Screens.Primary?.WorkingArea; if (_screen == null) { Console.WriteLine("No screen available."); Environment.Exit(1); } this.Position = new PixelPoint(_screen.Value.Width / 2 - 25, 10); this.Width = 50; // this.MaxWidth = screen.Value.Width - 100; InitializeComponent(); _viewModel = new ViewModel(); DataContext = _viewModel; this.GetObservable(Window.WidthProperty) .Subscribe(width => { if (DataContext is ViewModel vm) { vm.WindowWidth = (double)width; } }); AddButton.Focus(); } public static double TextWidth(string text) { var textBlock = new TextBlock { Text = text, FontSize = ViewModel._fontSize, FontFamily = new FontFamily(FontFamily.DefaultFontFamilyName) }; textBlock.Measure(Size.Infinity); return textBlock.DesiredSize.Width + 30 < 60 ? 60 : (textBlock.DesiredSize.Width + 30) * 1.05; } private void TextBox_OnTextChanged(object? sender, TextChangedEventArgs e) { if (sender == null) return; ((TextBox)sender).Width = TextWidth(((TextBox)sender).Text ?? ""); double width = 55; foreach (var note in _viewModel.Notes) { width += note.Width + 49; } if ((int)width == 55) width -= 5; this.Width = width; if(!System.OperatingSystem.IsLinux()) // Screenpositionhandling on Hyprland/Wayland/X11 is different this.Position = new PixelPoint((int)((_screen.Value.Width - width) / 2), 10); } private void InputElement_OnKeyDown(object? sender, KeyEventArgs e) { if (e.Key == Key.Enter || e.Key == Key.Escape) AddButton.Focus(); } }