Skip to content

Commit

Permalink
Merge pull request #46 from JeremyAnsel/input_box
Browse files Browse the repository at this point in the history
Add InputBox dialog
  • Loading branch information
NotYoojun authored Feb 25, 2024
2 parents fe050d0 + 65d1de6 commit f345809
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 2 deletions.
73 changes: 73 additions & 0 deletions source/iNKORE.UI.WPF.Modern.Controls/Controls/InputBox/InputBox.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
using System.Threading.Tasks;
using System.Windows;
using static iNKORE.UI.WPF.Modern.Controls.LocalizedDialogCommands;

namespace iNKORE.UI.WPF.Modern.Controls
{
public static class InputBox
{
public static async Task<string> ShowAsync(object title, object prompt, string defaultResponse = "")
{
ContentDialog dialog = BuildDialog(title, prompt, defaultResponse);
ContentDialogResult result = await dialog.ShowAsync();

if (result != ContentDialogResult.Primary)
{
return string.Empty;
}

InputBoxContent content = (InputBoxContent)dialog.Content;
string response = content.responseTextControl.Text;
return response;
}

public static async Task<string> ShowAsync(Window owner, object title, object prompt, string defaultResponse = "")
{
ContentDialog dialog = BuildDialog(title, prompt, defaultResponse);
ContentDialogResult result = await dialog.ShowAsync(owner);

if (result != ContentDialogResult.Primary)
{
return string.Empty;
}

InputBoxContent content = (InputBoxContent)dialog.Content;
string response = content.responseTextControl.Text;
return response;
}

public static async Task<string> ShowAsync(ContentDialogPlacement placement, object title, object prompt, string defaultResponse = "")
{
ContentDialog dialog = BuildDialog(title, prompt, defaultResponse);
ContentDialogResult result = await dialog.ShowAsync(placement);

if (result != ContentDialogResult.Primary)
{
return string.Empty;
}

InputBoxContent content = (InputBoxContent)dialog.Content;
string response = content.responseTextControl.Text;
return response;
}

private static ContentDialog BuildDialog(object title, object prompt, string defaultResponse)
{
ContentDialog dialog = new()
{
PrimaryButtonText = GetString(DialogBoxCommand.IDOK),
CloseButtonText = GetString(DialogBoxCommand.IDCANCEL),
DefaultButton = ContentDialogButton.Primary
};

var content = new InputBoxContent();
dialog.Content = content;

dialog.Title = title;
content.promptTextControl.Content = prompt;
content.responseTextControl.Text = defaultResponse;

return dialog;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<UserControl x:Class="iNKORE.UI.WPF.Modern.Controls.InputBoxContent"
x:ClassModifier="internal"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:iNKORE.UI.WPF.Modern.Controls" xmlns:ui="http://schemas.inkore.net/lib/ui/wpf/modern"
mc:Ignorable="d"
d:DesignHeight="200"
d:DesignWidth="400">
<ui:SimpleStackPanel Spacing="16"
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch">
<ui:ContentPresenterEx x:Name="promptTextControl" TextWrapping="Wrap"/>
<TextBox x:Name="responseTextControl"
Text="" />
</ui:SimpleStackPanel>
</UserControl>
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;

namespace iNKORE.UI.WPF.Modern.Controls
{
internal partial class InputBoxContent : UserControl
{
public InputBoxContent()
{
InitializeComponent();

Loaded += InputBoxContent_Loaded;
}

private void InputBoxContent_Loaded(object sender, RoutedEventArgs e)
{
responseTextControl.Focus();
Keyboard.Focus(responseTextControl);
}
}
}
5 changes: 3 additions & 2 deletions source/samples/WpfApp1/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,13 @@ private void AppBarButton_Click(object sender, RoutedEventArgs e)
//naview.SelectedItem = sgsac;
}

private void ToggleButton_Click(object sender, RoutedEventArgs e)
private async void ToggleButton_Click(object sender, RoutedEventArgs e)
{
//AppBarToggleButton1.IsChecked = false;
AppBarToggleButton1.IsEnabled = false;
//AppBarToggleButton1.IsEnabled = false;
//MicaHelper.RemoveTitleBar(this);
MicaHelper.Apply(this, BackdropType.Mica, false);
await InputBox.ShowAsync("promptssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssok", "promptssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssok", "test");
}

private void Button_MessageBox_Click(object sender, RoutedEventArgs e)
Expand Down

0 comments on commit f345809

Please sign in to comment.