made some minor changes

This commit is contained in:
Ano-sys
2025-02-14 00:35:21 +01:00
parent 52112a86fd
commit 20ed5fa729
5 changed files with 253 additions and 93 deletions
+54 -39
View File
@@ -3,74 +3,89 @@ using System.ComponentModel;
using Avalonia;
using Avalonia.Controls;
using System.Reactive.Linq;
using Avalonia.Input;
using Avalonia.Interactivity;
using Avalonia.Media;
namespace SimpleNote;
public partial class MainWindow : Window
{
private MainViewModel ViewModel;
public MainWindow(string[]? args)
private ViewModel viewModel;
private PixelRect? screen;
public MainWindow()
{
if (args == null || args.Length < 1)
screen = Screens.Primary?.WorkingArea;
if (screen == null)
{
Console.WriteLine("Call with Note as Argument!");
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 MainViewModel();
DataContext = ViewModel;
viewModel = new ViewModel();
DataContext = viewModel;
this.GetObservable(Window.WidthProperty)
.Subscribe(width =>
{
if (DataContext is MainViewModel vm)
if (DataContext is ViewModel vm)
{
vm.WindowWidth = (double)width;
}
});
init_TextField(args[0]);
}
/*
public void init_TextField(string text)
{
var textBlock = new TextBlock
{
Text = text,
FontSize = ViewModel.FontSize,
FontSize = viewModel.FontSize,
FontFamily = new FontFamily(FontFamily.DefaultFontFamilyName)
};
textBlock.Measure(Size.Infinity);
ViewModel.WindowWidth = textBlock.DesiredSize.Width + 50;
viewModel.WindowWidth = textBlock.DesiredSize.Width + 50;
Note.Text = text;
}
*/
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;
}
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 (width == 55) width -= 5;
this.Width = width;
this.Position = new PixelPoint((int)((screen.Value.Width - width) / 2), 10);
}
private void InputElement_OnKeyDown(object? sender, KeyEventArgs e)
{
if (e.Key == Key.Enter)
this.Focus();
}
}
public class MainViewModel : INotifyPropertyChanged
{
private double _windowWidth = 200;
private double _fontSize = 24;
public double WindowWidth
{
get => _windowWidth;
set
{
_windowWidth = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(WindowWidth)));
}
}
public double FontSize
{
get => _fontSize;
set
{
_fontSize = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(FontSize)));
}
}
public event PropertyChangedEventHandler? PropertyChanged;
}