Skip to content

Commit

Permalink
messagebox
Browse files Browse the repository at this point in the history
  • Loading branch information
kikipoulet committed May 26, 2024
1 parent 8766d2a commit 77f1e1d
Show file tree
Hide file tree
Showing 10 changed files with 151 additions and 26 deletions.
7 changes: 7 additions & 0 deletions SukiUI.Demo/Features/ControlsLibrary/MiscView.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,13 @@
Command="{Binding OpenBoxCommand}"
Content="Open MessageBox" />

<Button Margin="5"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Classes="Primary"
Command="{Binding OpenBoxErrorCommand}"
Content="Open Error MessageBox" />

</StackPanel>
</controls:GroupBox>
</controls:GlassCard>
Expand Down
12 changes: 11 additions & 1 deletion SukiUI.Demo/Features/ControlsLibrary/MiscViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,15 @@
using Avalonia;
using Avalonia.Controls;
using Avalonia.Controls.ApplicationLifetimes;
using Avalonia.Media;
using Avalonia.Platform.Storage;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using Material.Icons;
using SukiUI.Content;
using SukiUI.Controls;
using SukiUI.Enums;
using SukiUI.Models;

namespace SukiUI.Demo.Features.ControlsLibrary;

Expand All @@ -28,7 +32,13 @@ private async Task ToggleBusy()
[RelayCommand]
private void OpenBox()
{
SukiHost.ShowDialog(new MessageBox(), false, true);
SukiHost.ShowMessageBox(new MessageBoxModel("Update Available", "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.", ToastType.Info, "Update Now", () =>{SukiHost.CloseDialog();} ));
}

[RelayCommand]
private void OpenBoxError()
{
SukiHost.ShowMessageBox(new MessageBoxModel("Error", "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. ", ToastType.Error));
}

[RelayCommand]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ private static Task ShowInfoToast() =>
[RelayCommand]
private static Task ShowActionToast() =>
SukiHost.ShowToast(new ToastModel("Update Available", "A new version is available for you.", ToastType.Info, TimeSpan.FromSeconds(5), null, "Update Now",
() => { SukiHost.ShowToast("Update", "Update done !");}));
() => { SukiHost.ShowToast("Update", new ProgressBar(){Value = 43, ShowProgressText = true});}));

[RelayCommand]
private static Task ShowSuccessToast() =>
Expand Down
2 changes: 1 addition & 1 deletion SukiUI/ColorTheme/Dark.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

<BoxShadows x:Key="SukiLowShadow">0 0 3 0 #353535</BoxShadows>
<BoxShadows x:Key="SukiSwitchShadow">0 1 5 0 #555555</BoxShadows>
<BoxShadows x:Key="SukiSmallPopupShadow">0 0 5 1 #101010</BoxShadows>
<BoxShadows x:Key="SukiSmallPopupShadow">0 0 4 1 #101010</BoxShadows>
<BoxShadows x:Key="SukiPopupShadow">1 1 14 1 #101010</BoxShadows>
<BoxShadows x:Key="SukiBigPopupShadow">1 1 15 0 #606060</BoxShadows>

Expand Down
2 changes: 1 addition & 1 deletion SukiUI/ColorTheme/Light.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

<BoxShadows x:Key="SukiLowShadow">0 0 3 0 #eaeaea</BoxShadows>
<BoxShadows x:Key="SukiSwitchShadow">0 1 5 0 #aaaaaa</BoxShadows>
<BoxShadows x:Key="SukiSmallPopupShadow">0 0 6 0 Gray</BoxShadows>
<BoxShadows x:Key="SukiSmallPopupShadow">0 0 2 0 DimGray</BoxShadows>
<BoxShadows x:Key="SukiPopupShadow">1 1 8 0 LightGray</BoxShadows>
<BoxShadows x:Key="SukiBigPopupShadow">1 1 15 0 #dedede</BoxShadows>

Expand Down
30 changes: 16 additions & 14 deletions SukiUI/Controls/MessageBox.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
xmlns:content="clr-namespace:SukiUI.Content"
mc:Ignorable="d" d:DesignWidth="480" Width="480"
x:Class="SukiUI.Controls.MessageBox">
<UserControl.Styles>
<Style Selector="controls|MessageBox">
<Setter Property="Template">
<ControlTemplate>
<Panel>
<Border Background="{DynamicResource SukiCardBackground}" BorderBrush="{DynamicResource SukiBorderBrush}" BorderThickness="1" CornerRadius="25" Margin="0,90,0,0">
<Border CornerRadius="25" ClipToBounds="True">
Expand All @@ -32,39 +36,37 @@
</Panel.Background>
</Panel>
<StackPanel>
<TextBlock Margin="0,60,0,0" Text="New Update Available !" FontWeight="DemiBold" FontSize="26" HorizontalAlignment="Center" Foreground="{DynamicResource SukiText}"></TextBlock>
<TextBlock Margin="30,20,30,30" Text="Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur." FontWeight="DemiBold" TextWrapping="Wrap" Foreground="{DynamicResource SukiLowText}"></TextBlock>
<StackPanel Orientation="Horizontal" Spacing="15" Margin="0,0,30,30" HorizontalAlignment="Right">
<TextBlock Margin="0,60,0,0" Text="{TemplateBinding Title}" FontWeight="DemiBold" FontSize="26" HorizontalAlignment="Center" Foreground="{DynamicResource SukiText}"></TextBlock>
<ContentPresenter Margin="30,20,30,30" Content="{TemplateBinding Content}" FontWeight="DemiBold" TextWrapping="Wrap" Foreground="{DynamicResource SukiLowText}"></ContentPresenter>
<StackPanel IsVisible="{TemplateBinding ShowActionButton}" Orientation="Horizontal" Spacing="15" Margin="0,0,30,30" HorizontalAlignment="Right">

<Button Classes="Flat" Content="Update Now"></Button>
<Button Name="ButtonAction" Classes="Flat" Content="{TemplateBinding ActionButtonContent}"></Button>
</StackPanel>
</StackPanel>
</Panel>
</Border>
</Border>

<Border CornerRadius="50" Margin="0,50,0,0" Height="60" Width="70" HorizontalAlignment="Center" VerticalAlignment="Top" BoxShadow="0 0 40 0 blue"></Border>
<Border CornerRadius="50" Margin="0,50,0,0" Height="60" Width="70" HorizontalAlignment="Center" VerticalAlignment="Top" BoxShadow="{DynamicResource SukiBigPopupShadow}"></Border>
<Border
Margin="0,20,0,0" Background="#fafafa" Width="100" Height="100" CornerRadius="50" VerticalAlignment="Top" HorizontalAlignment="Center">
<Border CornerRadius="50" ClipToBounds="True">
<Panel>
<Panel Opacity="1" >
<Panel.Background>
<LinearGradientBrush StartPoint="0%,0%" EndPoint="100%,100%">
<GradientStop Color="{DynamicResource SukiPrimaryColor}" Offset="0"></GradientStop>
<GradientStop Color="{DynamicResource SukiPrimaryColor50}" Offset="1"></GradientStop>

</LinearGradientBrush>
</Panel.Background>
<Panel Opacity="1" Background="{TemplateBinding Foreground}">

</Panel>

<PathIcon Width="28" Margin="0,0,0,0"
Height="28" HorizontalAlignment="Center" VerticalAlignment="Center"
Data="{x:Static content:Icons.InformationOutline}"
Data="{TemplateBinding Icon}"
Foreground="White" />

</Panel>
</Border>
</Border>
</Panel>
</ControlTemplate>
</Setter>
</Style>
</UserControl.Styles>
</UserControl>
47 changes: 47 additions & 0 deletions SukiUI/Controls/MessageBox.axaml.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using System;
using Avalonia;
using Avalonia.Controls;
using Avalonia.Controls.Primitives;
using Avalonia.Markup.Xaml;

namespace SukiUI.Controls
Expand All @@ -10,5 +12,50 @@ public MessageBox()
{
InitializeComponent();
}

protected override void OnApplyTemplate(TemplateAppliedEventArgs e)
{
base.OnApplyTemplate(e);

e.NameScope.Get<Button>("ButtonAction").Click += ((sender, args) => _onActionCallback?.Invoke());
}

public Action? _onActionCallback;

public static readonly StyledProperty<object?> IconProperty =
AvaloniaProperty.Register<MessageBox, object?>(nameof(Icon));

public object? Icon
{
get => GetValue(IconProperty);
set => SetValue(IconProperty, value);
}

public static readonly StyledProperty<string> TitleProperty =
AvaloniaProperty.Register<MessageBox, string>(nameof(Title));

public string Title
{
get => GetValue(TitleProperty);
set => SetValue(TitleProperty, value);
}

public static readonly StyledProperty<bool> ShowActionButtonProperty =
AvaloniaProperty.Register<MessageBox, bool>(nameof(ShowActionButton));

public bool ShowActionButton
{
get => GetValue(ShowActionButtonProperty);
set => SetValue(ShowActionButtonProperty, value);
}

public static readonly StyledProperty<string> ActionButtonContentProperty =
AvaloniaProperty.Register<MessageBox, string>(nameof(ActionButtonContent));

public string ActionButtonContent
{
get => GetValue(ActionButtonContentProperty);
set => SetValue(ActionButtonContentProperty, value);
}
}
}
43 changes: 43 additions & 0 deletions SukiUI/Controls/SukiHost.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@
using System.Threading.Tasks;
using Avalonia.Interactivity;
using Avalonia.LogicalTree;
using Avalonia.Media;
using Avalonia.VisualTree;
using SukiUI.Content;

namespace SukiUI.Controls;

Expand Down Expand Up @@ -144,6 +146,47 @@ public static void ShowDialog(Window window, object? content, bool showCardBehin
/// <param name="allowBackgroundClose">Allows the dialog to be closed by clicking outside of it.</param>
public static void ShowDialog(object? content, bool showCardBehind = true, bool allowBackgroundClose = false) =>
ShowDialog(_mainWindow, content, showCardBehind, allowBackgroundClose);

public static void ShowMessageBox(MessageBoxModel model, bool allowbackgroundclose = true)
{


SukiHost.ShowDialog(new MessageBox(){
_onActionCallback = model.ActionButton,
Title = model.Title, Content = model.Content, ShowActionButton = model.ActionButtonContent != null,
ActionButtonContent = model.ActionButtonContent,
Icon = model.Type switch
{
ToastType.Info => Icons.InformationOutline,
ToastType.Success => Icons.Check,
ToastType.Warning => Icons.AlertOutline,
ToastType.Error => Icons.AlertOutline,
_ => Icons.InformationOutline
}
, Foreground = model.Type switch
{
ToastType.Info => SukiHost.GetGradient(Color.FromRgb(47,84,235)),
ToastType.Success => SukiHost.GetGradient(Color.FromRgb(82,196,26)),
ToastType.Warning => SukiHost.GetGradient(Color.FromRgb(240,140,22)),
ToastType.Error => SukiHost.GetGradient(Color.FromRgb(245,34,45)),
_ => SukiHost.GetGradient(Color.FromRgb(89,126,255))
}}, false, allowbackgroundclose);

}

private static LinearGradientBrush GetGradient(Color c1)
{
return new LinearGradientBrush()
{
StartPoint = new RelativePoint(0, 0, RelativeUnit.Relative),
EndPoint = new RelativePoint(1, 1, RelativeUnit.Relative),
GradientStops =
{
new GradientStop(){Color = c1, Offset = 0},
new GradientStop(){Color = Color.FromArgb(140, c1.R,c1.G,c1.B), Offset = 1}
}
};
}

/// <summary>
/// Attempts to close a dialog if one is shown in a specific window.
Expand Down
15 changes: 7 additions & 8 deletions SukiUI/Controls/SukiToast.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,11 @@

<DockPanel Margin="0,-7" LastChildFill="True">
<StackPanel DockPanel.Dock="Top" Orientation="Horizontal">
<TextBlock Margin="12,0,0,0"
<ContentPresenter Margin="12,0,0,0"
FontSize="15"
FontWeight="DemiBold"
Foreground="{DynamicResource SukiText}"
Text="{TemplateBinding Title}" />
Content="{TemplateBinding Title}" />
</StackPanel>
<Button Name="ButtonAction" FontSize="14" Padding="14,6" DockPanel.Dock="Bottom" IsVisible="{TemplateBinding ShowActionButton}" HorizontalAlignment="Right" Margin="0,14,0,0" Content="{TemplateBinding ActionButtonContent}">
</Button>
Expand All @@ -68,13 +68,12 @@
</Border>
</Border>
<Border BoxShadow="{DynamicResource SukiSmallPopupShadow}"
Margin="20,0,0,0" Background="{DynamicResource SukiCardBackground}" Width="40" Height="40" CornerRadius="40" VerticalAlignment="Center" HorizontalAlignment="Left">
<Border CornerRadius="40" ClipToBounds="True">
Margin="22,0,0,0" Background="{DynamicResource SukiCardBackground}" Width="35" Height="35" CornerRadius="35" VerticalAlignment="Center" HorizontalAlignment="Left">
<Border CornerRadius="35" ClipToBounds="True">
<Panel>
<Panel IsVisible="{DynamicResource IsDark}" Opacity="1" Background="{TemplateBinding Foreground}"></Panel>
<Panel IsVisible="{DynamicResource IsLight}" Opacity="1" Background="{TemplateBinding Foreground}"></Panel>
<PathIcon Width="16" Margin="0,0,0,0"
Height="16" HorizontalAlignment="Center" VerticalAlignment="Center"
<Panel Opacity="1" Background="{TemplateBinding Foreground}"></Panel>
<PathIcon Width="15" Margin="0,0,0,0"
Height="15" HorizontalAlignment="Center" VerticalAlignment="Center"
Data="{TemplateBinding Icon}"
Foreground="White" />

Expand Down
17 changes: 17 additions & 0 deletions SukiUI/Models/MessageBoxModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System;
using SukiUI.Enums;

namespace SukiUI.Models
{
public readonly record struct MessageBoxModel(string Title, object Content, ToastType Type = ToastType.Info, string? ActionButtonContent = null,Action? ActionButton= null)
{
public string Title { get; } = Title;
public object Content { get; } = Content;
public ToastType Type { get; } = Type;



public string? ActionButtonContent { get; } = ActionButtonContent;
public Action? OnActionButtonClicked { get; } = ActionButton;
}
}

0 comments on commit 77f1e1d

Please sign in to comment.