Skip to content

Commit

Permalink
Refactored ContentDialog
Browse files Browse the repository at this point in the history
  • Loading branch information
rocksdanister committed Nov 6, 2023
1 parent f4675eb commit b09592d
Show file tree
Hide file tree
Showing 10 changed files with 86 additions and 75 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Library</OutputType>
<TargetFramework>net7.0</TargetFramework>
</PropertyGroup>

Expand Down
61 changes: 51 additions & 10 deletions src/Lively/Lively.UI.WinUI/Services/DialogService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
using Lively.Models;
using Lively.UI.WinUI.ViewModels;
using Lively.UI.WinUI.Views.Pages;
using Lively.UI.WinUI.Views.Pages.ControlPanel;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using System;
using System.Collections.Generic;
Expand All @@ -25,7 +27,7 @@ public DialogService()
i18n = ResourceLoader.GetForViewIndependentUse();
}

public async Task<DisplayMonitor> ShowDisplayChooseDialog()
public async Task<DisplayMonitor> ShowDisplayChooseDialogAsync()
{
var vm = App.Services.GetRequiredService<ChooseDisplayViewModel>();
var dialog = new ContentDialog()
Expand All @@ -42,17 +44,17 @@ public async Task<DisplayMonitor> ShowDisplayChooseDialog()
return vm.SelectedItem?.Screen;
}

public async Task<ApplicationModel> ShowApplicationPickerDialog()
public async Task<ApplicationModel> ShowApplicationPickerDialogAsync()
{
var vm = App.Services.GetRequiredService<FindMoreAppsViewModel>();
var result = await ShowDialog(new Views.Pages.Settings.FindMoreAppsView() { DataContext = vm },
var result = await ShowDialogAsync(new Views.Pages.Settings.FindMoreAppsView() { DataContext = vm },
i18n.GetString("TitleChooseApplication/Text"),
i18n.GetString("TextAdd"),
i18n.GetString("Cancel/Content"));
return result == DialogResult.primary ? vm.SelectedItem : null;
}

public async Task ShowDialog(string message, string title, string primaryBtnText)
public async Task ShowDialogAsync(string message, string title, string primaryBtnText)
{
await new ContentDialog()
{
Expand All @@ -64,7 +66,7 @@ public async Task ShowDialog(string message, string title, string primaryBtnText
}.ShowAsyncQueue();
}

public async Task<DialogResult> ShowDialog(object content,
public async Task<DialogResult> ShowDialogAsync(object content,
string title,
string primaryBtnText,
string secondaryBtnText,
Expand All @@ -89,7 +91,7 @@ public async Task ShowDialog(string message, string title, string primaryBtnText
};
}

public async Task<string> ShowTextInputDialog(string title)
public async Task<string> ShowTextInputDialogAsync(string title)
{
var tb = new TextBox();
var dialog = new ContentDialog
Expand All @@ -103,7 +105,7 @@ public async Task<string> ShowTextInputDialog(string title)
return tb.Text;
}

public async Task ShowThemeDialog()
public async Task ShowThemeDialogAsync()
{
await new ContentDialog()
{
Expand All @@ -115,7 +117,7 @@ public async Task ShowThemeDialog()
}.ShowAsyncQueue();
}

public async Task<LibraryModel> ShowDepthWallpaperDialog(string imagePath)
public async Task<LibraryModel> ShowDepthWallpaperDialogAsync(string imagePath)
{
var vm = App.Services.GetRequiredService<DepthEstimateWallpaperViewModel>();
vm.SelectedImage = imagePath;
Expand Down Expand Up @@ -150,7 +152,7 @@ public async Task<LibraryModel> ShowDepthWallpaperDialog(string imagePath)
return vm.NewWallpaper;
}

public async Task<WallpaperCreateType?> ShowWallpaperCreateDialog(string filePath)
public async Task<WallpaperCreateType?> ShowWallpaperCreateDialogAsync(string filePath)
{
if (filePath is null)
return await InnerShowWallpaperCreateDialog(null);
Expand All @@ -163,7 +165,7 @@ public async Task<LibraryModel> ShowDepthWallpaperDialog(string imagePath)
return await InnerShowWallpaperCreateDialog(filter);
}

public async Task<WallpaperCreateType?> ShowWallpaperCreateDialog()
public async Task<WallpaperCreateType?> ShowWallpaperCreateDialogAsync()
{
return await InnerShowWallpaperCreateDialog(null);
}
Expand All @@ -190,5 +192,44 @@ public async Task<LibraryModel> ShowDepthWallpaperDialog(string imagePath)
};
return await dlg.ShowAsyncQueue() != ContentDialogResult.Secondary ? vm.SelectedItem.CreateType : null;
}

public async Task ShowAboutDialogAsync()
{
var vm = App.Services.GetRequiredService<AboutViewModel>();
var dlg = new ContentDialog()
{
Title = i18n.GetString("About/Label"),
Content = new AboutView(vm),
PrimaryButtonText = i18n.GetString("TextOK"),
DefaultButton = ContentDialogButton.Primary,
XamlRoot = App.Services.GetRequiredService<MainWindow>().Content.XamlRoot,
};
await dlg.ShowAsyncQueue();
vm.OnWindowClosing(this, new RoutedEventArgs());
}

public async Task ShowControlPanelDialogAsync()
{
await new ContentDialog()
{
Title = i18n.GetString("DescriptionScreenLayout"),
Content = new ControlPanelView(),
PrimaryButtonText = i18n.GetString("TextOK"),
DefaultButton = ContentDialogButton.Primary,
XamlRoot = App.Services.GetRequiredService<MainWindow>().Content.XamlRoot,
}.ShowAsyncQueue();
}

public async Task ShowHelpDialogAsync()
{
await new ContentDialog()
{
Title = i18n.GetString("Help/Label"),
Content = new HelpView(),
PrimaryButtonText = i18n.GetString("TextOK"),
DefaultButton = ContentDialogButton.Primary,
XamlRoot = App.Services.GetRequiredService<MainWindow>().Content.XamlRoot,
}.ShowAsyncQueue();
}
}
}
21 changes: 12 additions & 9 deletions src/Lively/Lively.UI.WinUI/Services/IDialogService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,22 @@ namespace Lively.UI.WinUI.Services
{
public interface IDialogService
{
Task<DisplayMonitor> ShowDisplayChooseDialog();
Task<ApplicationModel> ShowApplicationPickerDialog();
Task ShowDialog(string message, string title, string primaryBtnText);
Task<DialogResult> ShowDialog(object content,
Task ShowHelpDialogAsync();
Task ShowControlPanelDialogAsync();
Task ShowAboutDialogAsync();
Task<DisplayMonitor> ShowDisplayChooseDialogAsync();
Task<ApplicationModel> ShowApplicationPickerDialogAsync();
Task ShowDialogAsync(string message, string title, string primaryBtnText);
Task<DialogResult> ShowDialogAsync(object content,
string title,
string primaryBtnText,
string secondaryBtnText,
bool isDefaultPrimary = true);
Task<string> ShowTextInputDialog(string title);
Task ShowThemeDialog();
Task<LibraryModel> ShowDepthWallpaperDialog(string imagePath);
Task<WallpaperCreateType?> ShowWallpaperCreateDialog(string filePath);
Task<WallpaperCreateType?> ShowWallpaperCreateDialog();
Task<string> ShowTextInputDialogAsync(string title);
Task ShowThemeDialogAsync();
Task<LibraryModel> ShowDepthWallpaperDialogAsync(string imagePath);
Task<WallpaperCreateType?> ShowWallpaperCreateDialogAsync(string filePath);
Task<WallpaperCreateType?> ShowWallpaperCreateDialogAsync();
public enum DialogResult
{
none,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ private async Task Authenticate(string provider)

if (exception != null)
{
await dialogService.ShowDialog(exception.ToString(), i18n.GetString("TextError"), i18n.GetString("TextClose"));
await dialogService.ShowDialogAsync(exception.ToString(), i18n.GetString("TextError"), i18n.GetString("TextClose"));
IsProcessing = false;
}
else
Expand Down Expand Up @@ -131,7 +131,7 @@ private async Task RestoreSubscribedWallpapers()
{
vm.Wallpapers.Add(new GalleryModel(item, false) { IsSelected = true });
}
var result = await dialogService.ShowDialog(
var result = await dialogService.ShowDialogAsync(
new Views.Pages.Gallery.RestoreWallpaperView(vm),
i18n.GetString("TitleWelcomeback/Text"),
i18n.GetString("TextDownloadNow/Content"),
Expand Down
2 changes: 1 addition & 1 deletion src/Lively/Lively.UI.WinUI/ViewModels/LibraryViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ public LibraryModel SelectedItem
return;
var monitor = displayManager.DisplayMonitors.Count == 1 || userSettings.Settings.WallpaperArrangement != WallpaperArrangement.per ?
displayManager.DisplayMonitors.FirstOrDefault(x => x.IsPrimary) : await dialogService.ShowDisplayChooseDialog();
displayManager.DisplayMonitors.FirstOrDefault(x => x.IsPrimary) : await dialogService.ShowDisplayChooseDialogAsync();
if (monitor is not null)
await desktopCore.SetWallpaper(wp, monitor);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public ManageAccountViewModel(GalleryClient galleryClient, IDialogService dialog
private async Task DeleteAccount()
{
//IsProcessing = true;
var choice = await dialogService.ShowDialog(i18n.GetString("GalleryAccountDeleteConfirm/Text"),
var choice = await dialogService.ShowDialogAsync(i18n.GetString("GalleryAccountDeleteConfirm/Text"),
i18n.GetString("PleaseWait/Text"),
i18n.GetString("GalleryAccountDelete/Content"),
i18n.GetString("Cancel/Content"),
Expand All @@ -72,7 +72,7 @@ private async Task DeleteAccount()
var response = await galleryClient.DeleteAccountAsync();
if (response != null) //fail
{
await dialogService.ShowDialog(i18n.GetString("GalleryAccountDeleteFail/Text"),
await dialogService.ShowDialogAsync(i18n.GetString("GalleryAccountDeleteFail/Text"),
i18n.GetString("TextError"),
i18n.GetString("TextOK"));
}
Expand Down
6 changes: 3 additions & 3 deletions src/Lively/Lively.UI.WinUI/ViewModels/SettingsViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ public bool MoveExistingWallpaperNewDir
_openWallpaperDirectory ??= new RelayCommand(async () => await DesktopBridgeUtil.OpenFolder(userSettings.Settings.WallpaperDir));

private RelayCommand _themeBackgroundCommand;
public RelayCommand ThemeBackgroundCommand => _themeBackgroundCommand ??= new RelayCommand(async () => await dialogService.ShowThemeDialog());
public RelayCommand ThemeBackgroundCommand => _themeBackgroundCommand ??= new RelayCommand(async () => await dialogService.ShowThemeDialogAsync());

#endregion general

Expand Down Expand Up @@ -376,7 +376,7 @@ public ApplicationRulesModel SelectedAppRuleItem

private async Task AppRuleAddProgram()
{
var result = await dialogService.ShowApplicationPickerDialog();
var result = await dialogService.ShowApplicationPickerDialogAsync();
if (result != null)
{
try
Expand Down Expand Up @@ -831,7 +831,7 @@ private async void ExtractLog()
}
catch (Exception ex)
{
await dialogService.ShowDialog(ex.Message, "Error", "OK");
await dialogService.ShowDialogAsync(ex.Message, "Error", "OK");
}
}
}
Expand Down
49 changes: 9 additions & 40 deletions src/Lively/Lively.UI.WinUI/Views/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ private void AppUpdater_UpdateChecked(object sender, AppUpdaterEventArgs e)
btn.Click += (_, _) =>
{
infoBar.IsOpen = false;
ShowAboutDialog();
_ = dialogService.ShowAboutDialogAsync();
};
infoBar.ActionButton = btn;
infoBar.Title = i18n.GetString("TextUpdateAvailable");
Expand Down Expand Up @@ -325,7 +325,7 @@ private async void AddWallpaperButton_Click(object sender, RoutedEventArgs e)

public async Task CreateWallpaper(string filePath)
{
var creationType = await dialogService.ShowWallpaperCreateDialog(filePath);
var creationType = await dialogService.ShowWallpaperCreateDialogAsync(filePath);
if (creationType is null)
return;

Expand All @@ -343,7 +343,7 @@ public async Task CreateWallpaper(string filePath)
filePath ??= await FilePickerUtil.PickSingleFile(WallpaperType.picture);
if (filePath is not null)
{
var result = await dialogService.ShowDepthWallpaperDialog(filePath);
var result = await dialogService.ShowDepthWallpaperDialogAsync(filePath);
if (result is not null)
await desktopCore.SetWallpaper(result, userSettings.Settings.SelectedDisplay);
}
Expand Down Expand Up @@ -415,52 +415,21 @@ public async Task AddWallpapers(List<string> files)
}
}

private void ControlPanelButton_Click(object sender, RoutedEventArgs e) => ShowControlPanelDialog();

private void ShowControlPanelDialog()
{
_ = new ContentDialog()
{
Title = i18n.GetString("DescriptionScreenLayout"),
Content = new ControlPanelView(),
PrimaryButtonText = i18n.GetString("TextOK"),
DefaultButton = ContentDialogButton.Primary,
XamlRoot = this.Content.XamlRoot,
}.ShowAsyncQueue();
}
private void ControlPanelButton_Click(object sender, RoutedEventArgs e) => _ = dialogService.ShowControlPanelDialogAsync();

private void AppBarCoffeeBtn_Click(object sender, RoutedEventArgs e) =>
LinkUtil.OpenBrowser("https://rocksdanister.github.io/lively/coffee/");

private void AppBarThemeButton_Click(object sender, RoutedEventArgs e) => dialogService.ShowThemeDialog();
private void AppBarThemeButton_Click(object sender, RoutedEventArgs e) => dialogService.ShowThemeDialogAsync();

private void AppBarHelpButton_Click(object sender, RoutedEventArgs e)
{
_ = new ContentDialog()
{
Title = i18n.GetString("Help/Label"),
Content = new HelpView(),
PrimaryButtonText = i18n.GetString("TextOK"),
DefaultButton = ContentDialogButton.Primary,
XamlRoot = this.Content.XamlRoot,
}.ShowAsyncQueue();
_ = dialogService.ShowHelpDialogAsync();
}

private void AppBarAboutButton_Click(object sender, RoutedEventArgs e)
{
ShowAboutDialog();
}

private void ShowAboutDialog()
{
_ = new ContentDialog()
{
Title = i18n.GetString("About/Label"),
Content = new AboutView(),
PrimaryButtonText = i18n.GetString("TextOK"),
DefaultButton = ContentDialogButton.Primary,
XamlRoot = this.Content.XamlRoot,
}.ShowAsyncQueue();
_ = dialogService.ShowAboutDialogAsync();
}

private void SliderAudio_ValueChanged(object sender, RangeBaseValueChangedEventArgs e)
Expand Down Expand Up @@ -563,7 +532,7 @@ private async void Window_Closed(object sender, WindowEventArgs args)
args.Handled = true;

//Option 1: Show user prompt with choice to cancel.
var result = await dialogService.ShowDialog(i18n.GetString("TextConfirmCancel/Text"),
var result = await dialogService.ShowDialogAsync(i18n.GetString("TextConfirmCancel/Text"),
i18n.GetString("TitleDownloadProgress/Text"),
i18n.GetString("TextYes"),
i18n.GetString("TextWait/Text"),
Expand Down Expand Up @@ -627,7 +596,7 @@ private async Task StdInListener()
}
else if (args[1].Equals("SHOWCUSTOMISEPANEL", StringComparison.OrdinalIgnoreCase))
{
ShowControlPanelDialog();
_ = dialogService.ShowControlPanelDialogAsync();
}
}
});
Expand Down
9 changes: 3 additions & 6 deletions src/Lively/Lively.UI.WinUI/Views/Pages/AboutView.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,22 +28,19 @@ namespace Lively.UI.WinUI.Views.Pages
/// </summary>
public sealed partial class AboutView : Page
{
public AboutView()
public AboutView(AboutViewModel vm)
{
this.InitializeComponent();
var vm = App.Services.GetRequiredService<AboutViewModel>();
this.DataContext = vm;
markDownPatreon.Loaded += vm.OnPatreonLoaded;
//Unreliable, issue: https://github.com/microsoft/microsoft-ui-xaml/issues/1900
//this.Unloaded += vm.OnWindowClosing;
}

#region socials

private void GithubButton_Click(object sender, RoutedEventArgs e) => LinkUtil.OpenBrowser("https://github.com/rocksdanister");
private void TwitterButton_Click(object sender, RoutedEventArgs e) => LinkUtil.OpenBrowser("https://twitter.com/rocksdanister");
private void RedditButton_Click(object sender, RoutedEventArgs e) => LinkUtil.OpenBrowser("https://reddit.com/u/rocksdanister");
private void YoutubeButton_Click(object sender, RoutedEventArgs e) => LinkUtil.OpenBrowser("https://www.youtube.com/channel/UClep84ofxC41H8-R9UfNPSQ");
private void EmailButton_Click(object sender, RoutedEventArgs e) => LinkUtil.OpenBrowser("mailto:awoo.git@gmail.com");

#endregion //socials
}
}
4 changes: 2 additions & 2 deletions src/Lively/Lively.UI.WinUI/Views/Pages/LibraryView.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ private async void Page_Drop(object sender, DragEventArgs e)

try
{
var creationType = await dialogService.ShowWallpaperCreateDialog(item);
var creationType = await dialogService.ShowWallpaperCreateDialogAsync(item);
if (creationType is null)
return;

Expand All @@ -285,7 +285,7 @@ private async void Page_Drop(object sender, DragEventArgs e)
break;
case WallpaperCreateType.depthmap:
{
var result = await dialogService.ShowDepthWallpaperDialog(item);
var result = await dialogService.ShowDepthWallpaperDialogAsync(item);
if (result is not null)
await desktopCore.SetWallpaper(result, userSettings.Settings.SelectedDisplay);
}
Expand Down

0 comments on commit b09592d

Please sign in to comment.