Skip to content

Commit

Permalink
Migrate to Mica Design
Browse files Browse the repository at this point in the history
  • Loading branch information
sabihoshi committed Sep 13, 2022
1 parent 1848b58 commit fabde6f
Show file tree
Hide file tree
Showing 14 changed files with 408 additions and 341 deletions.
15 changes: 9 additions & 6 deletions GenshinLyreMidiPlayer.WPF/App.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:s="https://github.com/canton7/Stylet"
xmlns:ui="http://schemas.modernwpf.com/2019"
xmlns:ui="http://schemas.lepo.co/wpfui/2022/xaml"
xmlns:modern="http://schemas.modernwpf.com/2019"
xmlns:wpf="clr-namespace:GenshinLyreMidiPlayer.WPF">
<Application.Resources>
<ResourceDictionary>
Expand All @@ -15,14 +16,16 @@
</s:ApplicationLoader.Bootstrapper>
</s:ApplicationLoader>

<ui:ThemeResources />
<ui:XamlControlsResources />
<modern:ThemeResources />
<modern:XamlControlsResources />
<modern:IntellisenseResources Source="/ModernWpf;component/DesignTime/DesignTimeResources.xaml" />

<ui:IntellisenseResources Source="/ModernWpf;component/DesignTime/DesignTimeResources.xaml" />
<ui:ThemesDictionary Theme="Dark" />
<ui:ControlsDictionary />

</ResourceDictionary.MergedDictionaries>

<Style TargetType="ui:SimpleStackPanel">
<Style TargetType="modern:SimpleStackPanel">
<Setter Property="Spacing" Value="10" />
<Style.Resources>
<Style TargetType="ComboBox" BasedOn="{StaticResource {x:Type ComboBox}}">
Expand All @@ -42,7 +45,7 @@
</Style.Resources>
</Style>

<Style TargetType="ui:NumberBox">
<Style TargetType="modern:NumberBox">
<Setter Property="Minimum" Value="0" />
<Setter Property="SpinButtonPlacementMode" Value="Compact" />
</Style>
Expand Down
4 changes: 4 additions & 0 deletions GenshinLyreMidiPlayer.WPF/Bootstrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
using Microsoft.EntityFrameworkCore;
using Stylet;
using StyletIoC;
using Wpf.Ui.Mvvm.Contracts;
using Wpf.Ui.Mvvm.Services;

namespace GenshinLyreMidiPlayer.WPF;

Expand Down Expand Up @@ -84,5 +86,7 @@ protected override void ConfigureIoC(IStyletIoCBuilder builder)
return player;
}).InSingletonScope();

builder.Bind<IThemeService>().To<ThemeService>().InSingletonScope();
}
}
5 changes: 3 additions & 2 deletions GenshinLyreMidiPlayer.WPF/GenshinLyreMidiPlayer.WPF.csproj
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>net6.0-windows10.0.22621.0</TargetFramework>
<UseWPF>true</UseWPF>
<StartupObject>GenshinLyreMidiPlayer.WPF.App</StartupObject>
<ApplicationManifest>app.manifest</ApplicationManifest>
<Version>3.2.1</Version>
<Version>4.0.0</Version>
<ApplicationIcon>item_windsong_lyre.ico</ApplicationIcon>
<Nullable>enable</Nullable>
<RepositoryUrl>https://github.com/sabihoshi/GenshinLyreMidiPlayer</RepositoryUrl>
Expand All @@ -30,6 +30,7 @@
<PackageReference Include="ModernWpfUI.MahApps" Version="0.9.5" />
<PackageReference Include="PropertyChanged.Fody" Version="4.0.1" />
<PackageReference Include="Stylet" Version="1.3.6" />
<PackageReference Include="WPF-UI" Version="2.0.2" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ public class ErrorContentDialog : ContentDialog
{
public ErrorContentDialog(Exception e, IReadOnlyCollection<Enum>? options = null, string? closeText = null)
{
Title = e.GetType();
Content = e;
Title = e.GetType().Name;
Content = e.Message;

PrimaryButtonText = options?.ElementAtOrDefault(0)?.Humanize();
SecondaryButtonText = options?.ElementAtOrDefault(1)?.Humanize();
Expand Down
12 changes: 7 additions & 5 deletions GenshinLyreMidiPlayer.WPF/ViewModels/LyrePlayerViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -220,8 +220,9 @@ public void CloseFile()
MidiTracks.Clear();
MoveSlider(TimeSpan.Zero);

Playback = null;
Playlist.OpenedFile = null;
Playback = null;
Playlist.OpenedFile = null;
SettingsPage.Transpose = null;
}

public async void Next()
Expand Down Expand Up @@ -356,8 +357,8 @@ public void UpdateButtons()
private int ApplyNoteSettings(Keyboard.Instrument instrument, int noteId)
{
noteId -= Playlist.OpenedFile?.History.Key ?? SettingsPage.KeyOffset;
return Settings.TransposeNotes
? LyrePlayer.TransposeNote(instrument, ref noteId, SettingsPage.Transpose.Key)
return Settings.TransposeNotes && SettingsPage.Transpose is not null
? LyrePlayer.TransposeNote(instrument, ref noteId, SettingsPage.Transpose.Value.Key)
: noteId;
}

Expand Down Expand Up @@ -399,7 +400,8 @@ await Application.Current.Dispatcher.Invoke(async () =>
var exceptionDialog = new ErrorContentDialog(
new MissingNotesException(
"Some notes cannot be played by this instrument, and may play incorrectly. " +
"This can be solved by snapping to the nearest semitone."),
"This can be solved by snapping to the nearest semitone. " +
"You can choose to shift the notes up or down, or ignore the missing notes."),
options, "Ignore");
var result = await exceptionDialog.ShowAsync();
Expand Down
100 changes: 50 additions & 50 deletions GenshinLyreMidiPlayer.WPF/ViewModels/MainWindowViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,29 +1,39 @@
using System.Collections.Generic;
using System.Linq;
using System.Windows.Controls;
using GenshinLyreMidiPlayer.Data;
using GenshinLyreMidiPlayer.WPF.Views;
using JetBrains.Annotations;
using ModernWpf.Controls;
using ModernWpf;
using Stylet;
using StyletIoC;
using Wpf.Ui.Appearance;
using Wpf.Ui.Common;
using Wpf.Ui.Controls;
using Wpf.Ui.Controls.Interfaces;
using Wpf.Ui.Mvvm.Contracts;
using AutoSuggestBox = Wpf.Ui.Controls.AutoSuggestBox;

namespace GenshinLyreMidiPlayer.WPF.ViewModels;

[UsedImplicitly]
public class MainWindowViewModel : Conductor<IScreen>.StackNavigation
public class MainWindowViewModel : Conductor<IScreen>
{
public static NavigationStore Navigation = null!;
private readonly IContainer _ioc;
private readonly Stack<NavigationViewItem> _history = new();
private NavigationView _navView = null!;
private readonly IThemeService _theme;

public MainWindowViewModel(IContainer ioc)
public MainWindowViewModel(IContainer ioc, IThemeService theme)
{
_ioc = ioc;
Title = $"Genshin Lyre MIDI Player {SettingsPageViewModel.ProgramVersion}";

_ioc = ioc;
_theme = theme;

PlaylistView = new(ioc, this);
SettingsView = new(ioc, this);
PlayerView = new(ioc, this);
PianoSheetView = new(this);

ActiveItem = PlayerView = new(ioc, this);
}

public bool ShowUpdate => SettingsView.NeedsUpdate && ActiveItem != SettingsView;
Expand All @@ -36,67 +46,57 @@ public MainWindowViewModel(IContainer ioc)

public SettingsPageViewModel SettingsView { get; }

public string Title { get; set; } = "Genshin Lyre MIDI Player";
public string Title { get; set; }

protected override async void OnViewLoaded()
public void Navigate(INavigation sender, RoutedNavigationEventArgs args)
{
// Work around because events do not conform to the signatures Stylet supports
_navView = ((MainWindowView) View).NavView;

_navView.AutoSuggestBox.TextChanged += AutoSuggestBoxOnTextChanged;
if ((args.CurrentPage as NavigationItem)?.Tag is IScreen viewModel)
ActivateItem(viewModel);

_navView.SelectionChanged += Navigate;
_navView.BackRequested += NavigateBack;
NotifyOfPropertyChange(() => ShowUpdate);
}

var menuItems = _navView.MenuItems.Cast<NavigationViewItemBase>();
_navView.SelectedItem = menuItems.FirstOrDefault(item => item is NavigationViewItem);
public void NavigateToSettings() => ActivateItem(SettingsView);

if (!SettingsView.TryGetLocation()) _ = SettingsView.LocationMissing();

if (SettingsView.AutoCheckUpdates)
{
_ = SettingsView.CheckForUpdate()
.ContinueWith(_ => NotifyOfPropertyChange(() => ShowUpdate));
}
public void ToggleTheme()
{
ThemeManager.Current.ApplicationTheme
= _theme.GetTheme() is ThemeType.Dark
? ApplicationTheme.Light
: ApplicationTheme.Dark;

await using var db = _ioc.Get<LyreContext>();
await PlaylistView.AddFiles(db.History);
SettingsView.OnThemeChanged();
}

private void AutoSuggestBoxOnTextChanged(AutoSuggestBox sender, AutoSuggestBoxTextChangedEventArgs e)
public void SearchSong(AutoSuggestBox sender, TextChangedEventArgs e)
{
PlaylistView.OnFilterTextChanged(sender, e);
if (ActiveItem != PlaylistView)
{
var playlist = (NavigationViewItem) _navView.MenuItems
.Cast<NavigationViewItemBase>()
.First(nav => nav.Tag == PlaylistView);
ActivateItem(PlaylistView);

_navView.SelectedItem = playlist;
var playlist = Navigation.Items
.OfType<NavigationItem>()
.First(nav => nav.Tag == PlaylistView);
var index = Navigation.Items.IndexOf(playlist);
Navigation.SelectedPageIndex = index;
}

PlaylistView.FilterText = sender.Text;
}

private void Navigate(NavigationView sender, NavigationViewSelectionChangedEventArgs args)
protected override async void OnViewLoaded()
{
if (args.IsSettingsSelected)
Activate(SettingsView);
else if ((args.SelectedItem as NavigationViewItem)?.Tag is IScreen viewModel)
Activate(viewModel);

sender.IsBackEnabled = _history.Count > 1;
NotifyOfPropertyChange(() => ShowUpdate);
Navigation = ((MainWindowView) View).RootNavigation;
_theme.SetTheme(_theme.GetSystemTheme());

void Activate(IScreen viewModel)
if (!SettingsView.TryGetLocation()) _ = SettingsView.LocationMissing();
if (SettingsView.AutoCheckUpdates)
{
ActivateItem(viewModel);
_history.Push((NavigationViewItem) sender.SelectedItem);
_ = SettingsView.CheckForUpdate()
.ContinueWith(_ => { NotifyOfPropertyChange(() => ShowUpdate); });
}
}

private void NavigateBack(NavigationView sender, NavigationViewBackRequestedEventArgs args)
{
_history.Pop();
sender.SelectedItem = _history.Pop();
sender.IsBackEnabled = _history.Count > 1;
await using var db = _ioc.Get<LyreContext>();
await PlaylistView.AddFiles(db.History);
}
}
6 changes: 3 additions & 3 deletions GenshinLyreMidiPlayer.WPF/ViewModels/PianoSheetViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,9 @@ public void Update()
foreach (var note in notes)
{
var id = note.NoteNumber - SettingsPage.KeyOffset;
var transpose = SettingsPage.Transpose.Key;
if (Settings.TransposeNotes)
LyrePlayer.TransposeNote(instrument, ref id, transpose);
var transpose = SettingsPage.Transpose?.Key;
if (Settings.TransposeNotes && transpose is not null)
LyrePlayer.TransposeNote(instrument, ref id, transpose.Value);

if (!LyrePlayer.TryGetKey(layout, instrument, id, out var key)) continue;

Expand Down
13 changes: 8 additions & 5 deletions GenshinLyreMidiPlayer.WPF/ViewModels/PlaylistViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
using Melanchall.DryWetMidi.Core;
using Microsoft.Win32;
using ModernWpf;
using ModernWpf.Controls;
using Stylet;
using StyletIoC;
using MidiFile = GenshinLyreMidiPlayer.Data.Midi.MidiFile;
Expand Down Expand Up @@ -45,6 +44,8 @@ public PlaylistViewModel(IContainer ioc, MainWindowViewModel main)

public bool Shuffle { get; set; }

public IEnumerable<string> TrackTitles => Tracks.Select(t => t.Title);

public LoopMode Loop { get; set; } = LoopMode.All;

public MidiFile? OpenedFile { get; set; }
Expand Down Expand Up @@ -125,6 +126,10 @@ public async Task ClearPlaylist()
await db.SaveChangesAsync();

Tracks.Clear();
FilteredTracks.Clear();
History.Clear();


OpenedFile = null;
SelectedFile = null;
}
Expand Down Expand Up @@ -171,17 +176,15 @@ public void OnFileChanged(object sender, EventArgs e)
_events.Publish(SelectedFile);
}

public void OnFilterTextChanged(AutoSuggestBox sender, AutoSuggestBoxTextChangedEventArgs e)
=> FilterText = sender.Text;

public void OnOpenedFileChanged()
{
if (OpenedFile is null) return;

var transpose = SettingsPageViewModel.TransposeNames
.FirstOrDefault(e => e.Key == OpenedFile.History.Transpose);

_main.SettingsView.Transpose = transpose;
if (OpenedFile.History.Transpose is not null)
_main.SettingsView.Transpose = transpose;
_main.SettingsView.KeyOffset = OpenedFile.History.Key;
}

Expand Down
Loading

0 comments on commit fabde6f

Please sign in to comment.