Free, open-source file converter. Convert 263 formats offline by drag & drop — images, PDF, Word, Excel, PowerPoint, HWP/HWPX, video, audio, e-books and archives. No upload, no Office needed.
C# audio-converteravaloniabatch-conversionconvertercsharpdocument-converterdocx-to-pdfdotnetdrag-and-dropfile-conversionfile-converterheic-converterhwphwpximage-converterlinuxmacospdf-convertervideo-converterwindows
// Main (settings) window: left navigation + current page.
using Avalonia;
using Avalonia.Controls;
using Avalonia.Controls.Primitives;
using Avalonia.Media;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using Filee.App.Services;
using Filee.App.ViewModels.Pages;
using Filee.Core.Localization;
using Filee.Core.Settings;
using Microsoft.Extensions.DependencyInjection;
namespace Filee.App.ViewModels;
/// <summary>Navigation entry. <see cref="Title"/> follows the UI language.</summary>
public sealed partial class NavItem(string key, string titleKey, string iconKey, Type pageType) : ObservableObject
{
public string Key { get; } = key;
public string TitleKey { get; } = titleKey;
public Geometry? Icon { get; } = Application.Current?.FindResource(iconKey) as Geometry;
public Type PageType { get; } = pageType;
[ObservableProperty]
[NotifyPropertyChangedFor(nameof(Tip))]
private string _title = "";
/// <summary>The navigation is collapsed to icons: the title is hidden and shown as a tooltip instead.</summary>
[ObservableProperty]
[NotifyPropertyChangedFor(nameof(Tip), nameof(ShowTitle))]
private bool _collapsed;
public bool ShowTitle => !Collapsed;
/// <summary>The tooltip: the title while collapsed (no text is shown then), else none.</summary>
public string? Tip => Collapsed ? Title : null;
public void Refresh(ILocalizer loc) => Title = loc[TitleKey];
}
/// <summary>
/// One per main window (<see cref="WindowService.ShowMain"/>); disposed with it, which drops its subscriptions on the
/// long-lived services and the current page.
/// </summary>
public sealed partial class MainWindowViewModel : ObservableObject, IDisposable
{
private readonly IServiceProvider _services;
private readonly ILocalizer _loc;
private readonly UserDataStore _store;
private bool _disposed;
/// <summary>Width of the navigation with its titles, and collapsed to icons.</summary>
private const double NavExpandedWidth = 230;
private const double NavCollapsedWidth = 64;
/// <summary>The repository, for the GitHub button above the version.</summary>
public string RepositoryUrl => UpdateService.RepositoryUrl;
public MainWindowViewModel(IServiceProvider services, ILocalizer loc, UpdateService updates, UserDataStore store)
{
_services = services;
_loc = loc;
_store = store;
_isNavCollapsed = store.Settings.NavCollapsed;
Updates = updates;
updates.PropertyChanged += OnUpdatesChanged;
RefreshVersionTexts();
Items =
[
new("home", "nav.home", "Icon.Home", typeof(HomePageViewModel)),
new("general", "nav.general", "Icon.Settings", typeof(GeneralPageViewModel)),
new("triggers", "nav.triggers", "Icon.Keyboard", typeof(TriggersPageViewModel)),
new("toolbar", "nav.toolbar", "Icon.Donut", typeof(ToolbarPageViewModel)),
new("presets", "nav.presets", "Icon.Tune", typeof(PresetsPageViewModel)),
new("watch", "nav.watch", "Icon.Folder", typeof(WatchFoldersPageViewModel)),
new("theme", "nav.theme", "Icon.Palette", typeof(ThemePageViewModel)),
new("engines", "nav.engines", "Icon.Engine", typeof(EnginesPageViewModel)),
new("about", "nav.about", "Icon.Info", typeof(AboutPageViewModel)),
];
foreach (var item in Items)
{
item.Refresh(loc);
item.Collapsed = _isNavCollapsed;
}
loc.LanguageChanged += OnLanguageChanged;
_selectedItem = Items[0];
_currentPage = CreatePage(Items[0]);
}
public IReadOnlyList<NavItem> Items { get; }
/// <summary>The navigation shows only icons (with tooltips), as the user left it (<see cref="AppSettings.NavCollapsed"/>).</summary>
[ObservableProperty]
[NotifyPropertyChangedFor(nameof(NavWidth), nameof(NavPadding), nameof(NavToggleTip))]
private bool _isNavCollapsed;
public double NavWidth => IsNavCollapsed ? NavCollapsedWidth : NavExpandedWidth;
/// <summary>Room around the navigation's content; less when collapsed, so an icon fits the rail.</summary>
public Thickness NavPadding => new(IsNavCollapsed ? 10 : 14, 18);
/// <summary>Tooltip and screen-reader name of the button that collapses or expands the navigation.</summary>
public string NavToggleTip => _loc[IsNavCollapsed ? "nav.expand" : "nav.collapse"];
[RelayCommand]
private void ToggleNav() => IsNavCollapsed = !IsNavCollapsed;
partial void OnIsNavCollapsedChanged(bool value)
{
foreach (var item in Items)
item.Collapsed = value;
_store.SaveNavCollapsed(value);
}
/// <summary>Update state for the bottom of the navigation (version, "update available" button).</summary>
public UpdateService Updates { get; }
[ObservableProperty] private NavItem _selectedItem;
[ObservableProperty]
[NotifyPropertyChangedFor(nameof(PageScroll))]
private ObservableObject _currentPage;
/// <summary>
/// Pages scroll as a whole, except pages that fit the window height and scroll their own parts
/// (<see cref="IFitsWindowHeight"/>, e.g. the presets list and editor).
/// </summary>
public ScrollBarVisibility PageScroll => CurrentPage is IFitsWindowHeight ? ScrollBarVisibility.Disabled : ScrollBarVisibility.Auto;
[ObservableProperty] private string _versionText = "";
[ObservableProperty] private string _updateText = "";
[ObservableProperty] private string _updateTip = "";
/// <summary>Downloads and runs the new installer (or opens the download page in a portable copy).</summary>
[RelayCommand]
private Task DownloadUpdate() => AppHost.Get<WindowService>().StartUpdateAsync(Updates);
private void OnUpdatesChanged(object? sender, System.ComponentModel.PropertyChangedEventArgs e)
{
if (UpdateTexts.Affects(e.PropertyName))
RefreshVersionTexts();
}
private void OnLanguageChanged(object? sender, EventArgs e)
{
foreach (var item in Items)
item.Refresh(_loc);
OnPropertyChanged(nameof(NavToggleTip));
RefreshVersionTexts();
CurrentPage = CreatePage(SelectedItem); // rebuild so code-generated labels update too
}
private void RefreshVersionTexts()
{
VersionText = _loc.Format("about.version", UpdateService.CurrentVersion);
UpdateText = UpdateTexts.Short(Updates, _loc);
var changes = UpdateTexts.Changes(Updates, _loc);
UpdateTip = UpdateTexts.Status(Updates, _loc)
?? $"{_loc[$"update.level.{Updates.Level}"]}{Environment.NewLine}{(changes.Length > 0 ? changes + Environment.NewLine : "")}{_loc[UpdateTexts.HowKey]}";
}
public void Navigate(string key)
{
var item = Items.FirstOrDefault(i => i.Key == key);
if (item is not null)
SelectedItem = item;
}
// Pages are recreated on every visit so they always reflect the latest settings. A navigation list that loses its
// selection keeps the open page (Choices.Restore).
partial void OnSelectedItemChanged(NavItem? oldValue, NavItem newValue)
{
if (!Choices.Restore(oldValue, newValue, v => SelectedItem = v))
CurrentPage = CreatePage(newValue);
}
// Old pages may hold event subscriptions on long-lived services.
partial void OnCurrentPageChanged(ObservableObject? oldValue, ObservableObject newValue) => (oldValue as IDisposable)?.Dispose();
private ObservableObject CreatePage(NavItem item) =>
(ObservableObject)ActivatorUtilities.CreateInstance(_services, item.PageType);
public void Dispose()
{
if (_disposed)
return;
_disposed = true;
Updates.PropertyChanged -= OnUpdatesChanged;
_loc.LanguageChanged -= OnLanguageChanged;
(CurrentPage as IDisposable)?.Dispose();
}
}