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
+1 -1
View File
@@ -16,7 +16,7 @@ public partial class App : Application
{ {
if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop) if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
{ {
desktop.MainWindow = new MainWindow(desktop.Args); desktop.MainWindow = new MainWindow();
} }
base.OnFrameworkInitializationCompleted(); base.OnFrameworkInitializationCompleted();
} }
+59 -30
View File
@@ -5,58 +5,87 @@
xmlns:vm="clr-namespace:SimpleNote" xmlns:vm="clr-namespace:SimpleNote"
mc:Ignorable="d" d:DesignWidth="40" d:DesignHeight="200" mc:Ignorable="d" d:DesignWidth="40" d:DesignHeight="200"
x:Class="SimpleNote.MainWindow" x:Class="SimpleNote.MainWindow"
x:DataType="vm:MainViewModel" x:DataType="vm:ViewModel"
WindowStartupLocation="CenterScreen" WindowStartupLocation="Manual"
TransparencyLevelHint="AcrylicBlur" TransparencyLevelHint="Transparent"
TransparencyBackgroundFallback="DimGray"
Background="Transparent" Background="Transparent"
Height="50" Height="50"
Width="{Binding WindowWidth}"
SystemDecorations="None" SystemDecorations="None"
ExtendClientAreaToDecorationsHint="False" ExtendClientAreaToDecorationsHint="True"
CanResize="False" CanResize="False"
Topmost="True" Topmost="True"
Title="SimpleNote"> Title="SimpleNote">
<Window.Styles>
<Panel> <Style Selector="TextBox">
<Style Selector="^:focus">
<Setter Property="BorderBrush" Value="Transparent"/>
<Style Selector="^ /template/ Border#PART_BorderElement">
<Setter Property="Background" Value="Transparent"/>
<Setter Property="BorderBrush" Value="Transparent"/>
<Setter Property="BorderThickness" Value="1" />
</Style>
</Style>
<Style Selector="^:pointerover /template/ Border#PART_BorderElement">
<Setter Property="BorderBrush" Value="Transparent"/>
<Setter Property="Background" Value="Transparent" />
</Style>
</Style>
</Window.Styles>
<!--<Border CornerRadius="15" BorderBrush="White" BorderThickness="2" Background="#000000aa">-->
<StackPanel Orientation="Horizontal">
<ExperimentalAcrylicBorder IsHitTestVisible="False"> <ExperimentalAcrylicBorder IsHitTestVisible="False">
<ExperimentalAcrylicBorder.Material> <ExperimentalAcrylicBorder.Material>
<ExperimentalAcrylicMaterial <ExperimentalAcrylicMaterial
BackgroundSource="Digger" BackgroundSource="Digger"
TintColor="Black" TintColor="Green"
TintOpacity="0.6" TintOpacity="0.5"
MaterialOpacity="0.65" /> MaterialOpacity="0.65" />
</ExperimentalAcrylicBorder.Material> </ExperimentalAcrylicBorder.Material>
</ExperimentalAcrylicBorder> </ExperimentalAcrylicBorder>
<TextBox HorizontalAlignment="Center" <TextBox x:Name="FocusBox" IsVisible="False"/>
VerticalAlignment="Top" <Button Margin="5,0" Command="{Binding AddButtonPressedCommand}" FontSize="{Binding FontSize}" Width="40" HorizontalContentAlignment="Center">
Height="5" +
IsHitTestVisible="False" </Button>
Margin="0, -3, 0, 0" <ItemsControl ItemsSource="{Binding Notes}">
Foreground="Black" <ItemsControl.ItemsPanel>
Text="SimpleNote - Note" <ItemsPanelTemplate>
Background="Transparent" <StackPanel Orientation="Horizontal"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate x:DataType="vm:Note">
<Border CornerRadius="15" Margin="5,5,0,5" BorderBrush="#fff" BorderThickness="2" Background="Transparent">
<StackPanel Margin="5" Background="Transparent" Orientation="Horizontal">
<TextBox Height="20"
CaretBrush="Transparent"
BorderThickness="0" BorderThickness="0"
FontSize="12" Width="{Binding Width}"
IsTabStop="False" TextChanged="TextBox_OnTextChanged"
IsReadOnly="True" Margin="0, 0, 0, 0"
>
</TextBox>
<TextBox x:Name="Note"
Height="20"
BorderThickness="0"
Width="{Binding WindowWidth}"
Margin="0, 12, 0, 0"
Padding="5,0,5,0" Padding="5,0,5,0"
TextAlignment="Center" TextAlignment="Center"
HorizontalAlignment="Center" HorizontalAlignment="Center"
VerticalAlignment="Center" VerticalAlignment="Center"
Foreground="White" Foreground="White"
Background="Transparent" Background="Transparent"
IsHitTestVisible="False" IsHitTestVisible="True"
FontSize="{Binding FontSize}" FontSize="{Binding FontSize}"
IsTabStop="False" IsTabStop="False"
IsReadOnly="True" IsReadOnly="False"
Text="{Binding Content}"
VerticalContentAlignment="Center"
KeyDown="InputElement_OnKeyDown"
> >
</TextBox> </TextBox>
</Panel> <Button Command="{Binding RemoveButtonClicked}" FontSize="{Binding FontSize}" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" Width="25" Padding="1,-3,0,0" Margin="5,0,0,0">
x
</Button>
</StackPanel>
</Border>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</StackPanel>
<!--</Border>-->
</Window> </Window>
+46 -31
View File
@@ -3,74 +3,89 @@ using System.ComponentModel;
using Avalonia; using Avalonia;
using Avalonia.Controls; using Avalonia.Controls;
using System.Reactive.Linq; using System.Reactive.Linq;
using Avalonia.Input;
using Avalonia.Interactivity;
using Avalonia.Media; using Avalonia.Media;
namespace SimpleNote; namespace SimpleNote;
public partial class MainWindow : Window public partial class MainWindow : Window
{ {
private MainViewModel ViewModel; private ViewModel viewModel;
public MainWindow(string[]? args) 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); Environment.Exit(1);
} }
this.Position = new PixelPoint(screen.Value.Width / 2 - 25, 10);
this.Width = 50;
// this.MaxWidth = screen.Value.Width - 100;
InitializeComponent(); InitializeComponent();
ViewModel = new MainViewModel(); viewModel = new ViewModel();
DataContext = ViewModel; DataContext = viewModel;
this.GetObservable(Window.WidthProperty) this.GetObservable(Window.WidthProperty)
.Subscribe(width => .Subscribe(width =>
{ {
if (DataContext is MainViewModel vm) if (DataContext is ViewModel vm)
{ {
vm.WindowWidth = (double)width; vm.WindowWidth = (double)width;
} }
}); });
init_TextField(args[0]);
} }
/*
public void init_TextField(string text) public void init_TextField(string text)
{ {
var textBlock = new TextBlock var textBlock = new TextBlock
{ {
Text = text, Text = text,
FontSize = ViewModel.FontSize, FontSize = viewModel.FontSize,
FontFamily = new FontFamily(FontFamily.DefaultFontFamilyName) FontFamily = new FontFamily(FontFamily.DefaultFontFamilyName)
}; };
textBlock.Measure(Size.Infinity); textBlock.Measure(Size.Infinity);
ViewModel.WindowWidth = textBlock.DesiredSize.Width + 50; viewModel.WindowWidth = textBlock.DesiredSize.Width + 50;
Note.Text = text; 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;
} }
public class MainViewModel : INotifyPropertyChanged private void TextBox_OnTextChanged(object? sender, TextChangedEventArgs e)
{ {
private double _windowWidth = 200; if (sender == null) return;
private double _fontSize = 24; ((TextBox)sender).Width = TextWidth(((TextBox)sender).Text);
double width = 55;
public double WindowWidth foreach (var note in viewModel.Notes)
{ {
get => _windowWidth; width += note.Width + 49;
set
{
_windowWidth = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(WindowWidth)));
}
} }
public double FontSize if (width == 55) width -= 5;
{
get => _fontSize; this.Width = width;
set this.Position = new PixelPoint((int)((screen.Value.Width - width) / 2), 10);
{
_fontSize = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(FontSize)));
}
} }
public event PropertyChangedEventHandler? PropertyChanged; private void InputElement_OnKeyDown(object? sender, KeyEventArgs e)
{
if (e.Key == Key.Enter)
this.Focus();
}
} }
+6 -5
View File
@@ -9,12 +9,13 @@
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="Avalonia" Version="11.1.0" /> <PackageReference Include="Avalonia" Version="11.2.3" />
<PackageReference Include="Avalonia.Desktop" Version="11.1.0" /> <PackageReference Include="Avalonia.Desktop" Version="11.2.3" />
<PackageReference Include="Avalonia.Themes.Fluent" Version="11.1.0" /> <PackageReference Include="Avalonia.Themes.Fluent" Version="11.2.3" />
<PackageReference Include="Avalonia.Fonts.Inter" Version="11.1.0" /> <PackageReference Include="Avalonia.Fonts.Inter" Version="11.2.3" />
<!--Condition below is needed to remove Avalonia.Diagnostics package from build output in Release configuration.--> <!--Condition below is needed to remove Avalonia.Diagnostics package from build output in Release configuration.-->
<PackageReference Condition="'$(Configuration)' == 'Debug'" Include="Avalonia.Diagnostics" Version="11.1.0" /> <PackageReference Condition="'$(Configuration)' == 'Debug'" Include="Avalonia.Diagnostics" Version="11.2.3" />
<PackageReference Include="CommunityToolkit.Mvvm" Version="8.4.0" />
<PackageReference Include="System.Reactive.Linq" Version="6.0.1" /> <PackageReference Include="System.Reactive.Linq" Version="6.0.1" />
</ItemGroup> </ItemGroup>
</Project> </Project>
+115
View File
@@ -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<Note> Notes { get; set; } = new ObservableCollection<Note>();
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<object?> _execute;
private readonly Func<object?, bool>? _canExecute;
public event EventHandler? CanExecuteChanged;
public RelayCommand(Action<object?> execute, Func<object?, bool>? 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);
}