From 20ed5fa729e2a89ee50aff2a3fac03208bc8e09c Mon Sep 17 00:00:00 2001 From: Ano-sys Date: Fri, 14 Feb 2025 00:35:21 +0100 Subject: [PATCH] made some minor changes --- App.axaml.cs | 2 +- MainWindow.axaml | 125 +++++++++++++++++++++++++++----------------- MainWindow.axaml.cs | 93 ++++++++++++++++++-------------- SimpleNote.csproj | 11 ++-- ViewModel.cs | 115 ++++++++++++++++++++++++++++++++++++++++ 5 files changed, 253 insertions(+), 93 deletions(-) create mode 100644 ViewModel.cs diff --git a/App.axaml.cs b/App.axaml.cs index 18f6d8a..b4d467f 100644 --- a/App.axaml.cs +++ b/App.axaml.cs @@ -16,7 +16,7 @@ public partial class App : Application { if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop) { - desktop.MainWindow = new MainWindow(desktop.Args); + desktop.MainWindow = new MainWindow(); } base.OnFrameworkInitializationCompleted(); } diff --git a/MainWindow.axaml b/MainWindow.axaml index dee9b8d..c493ccc 100644 --- a/MainWindow.axaml +++ b/MainWindow.axaml @@ -5,58 +5,87 @@ xmlns:vm="clr-namespace:SimpleNote" mc:Ignorable="d" d:DesignWidth="40" d:DesignHeight="200" x:Class="SimpleNote.MainWindow" - x:DataType="vm:MainViewModel" - WindowStartupLocation="CenterScreen" - TransparencyLevelHint="AcrylicBlur" + x:DataType="vm:ViewModel" + WindowStartupLocation="Manual" + TransparencyLevelHint="Transparent" + TransparencyBackgroundFallback="DimGray" Background="Transparent" Height="50" - Width="{Binding WindowWidth}" SystemDecorations="None" - ExtendClientAreaToDecorationsHint="False" + ExtendClientAreaToDecorationsHint="True" CanResize="False" Topmost="True" Title="SimpleNote"> - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/MainWindow.axaml.cs b/MainWindow.axaml.cs index fb3b1fe..c1c479d 100644 --- a/MainWindow.axaml.cs +++ b/MainWindow.axaml.cs @@ -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; -} \ No newline at end of file diff --git a/SimpleNote.csproj b/SimpleNote.csproj index 0c9baa2..7fb1c58 100644 --- a/SimpleNote.csproj +++ b/SimpleNote.csproj @@ -9,12 +9,13 @@ - - - - + + + + - + + diff --git a/ViewModel.cs b/ViewModel.cs new file mode 100644 index 0000000..3dd31a9 --- /dev/null +++ b/ViewModel.cs @@ -0,0 +1,115 @@ +using System; +using System.Collections.ObjectModel; +using System.ComponentModel; +using System.Windows.Input; +using Avalonia.Interactivity; +using CommunityToolkit.Mvvm.ComponentModel; +using CommunityToolkit.Mvvm.Input; + +namespace SimpleNote; + +public class ViewModel : ObservableObject +{ + private double _windowWidth = 200; + public static double _fontSize = 20; + private double _noteWidth; + + public ObservableCollection Notes { get; set; } = new ObservableCollection(); + public ICommand AddButtonPressedCommand { get; } + + public ViewModel() + { + AddButtonPressedCommand = new RelayCommand(AddButtonPressedCommandEvent); + } + + public double WindowWidth + { + get => _windowWidth; + set => SetProperty(ref _windowWidth, value); + } + + public double NoteWidth + { + get => _noteWidth; + set => SetProperty(ref _noteWidth, value); + } + + public double FontSize + { + get => _fontSize; + set => SetProperty(ref _fontSize, value); + } + + private void AddButtonPressedCommandEvent(object? sender) + { + // TODO: generate new Note + AddNote(); + } + + //[RelayCommand] + private void AddNote() + { + // Füge eine neue Note zur Collection hinzu + Note note = new Note + { + Width = MainWindow.TextWidth("New Note"), Content = "New Note", FontSize = FontSize, + }; + note.RemoveButtonClicked = new RelayCommand(o => RemoveNote(note)); + Notes.Add(note); + + } + + private void RemoveNote(Note note) + { + if (Notes.Contains(note)) + { + Notes.Remove(note); + } + } + + public event PropertyChangedEventHandler? PropertyChanged; +} + +public class Note : ObservableObject +{ + public double Width { get; set; } + public string _content; + public double FontSize { get; set; } + public RelayCommand RemoveButtonClicked { get; set; } + + public string Content + { + get => _content; + set + { + _content = value; + Width = MainWindow.TextWidth(_content); + PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(Content))); + } + } + + public event PropertyChangedEventHandler? PropertyChanged; +} + +public class RelayCommand : ICommand +{ + private readonly Action _execute; + private readonly Func? _canExecute; + + public event EventHandler? CanExecuteChanged; + + public RelayCommand(Action execute, Func? canExecute = null) + { + _execute = execute ?? throw new ArgumentNullException(nameof(execute)); + _canExecute = canExecute; + } + + public bool CanExecute(object? parameter) => + _canExecute?.Invoke(parameter) ?? true; + + public void Execute(object? parameter) => + _execute(parameter); + + public void RaiseCanExecuteChanged() => + CanExecuteChanged?.Invoke(this, EventArgs.Empty); +} \ No newline at end of file