Flow.Launcher/Flow.Launcher.Avalonia/ViewModel/MainViewModel.cs
Hongtao Zhang 4120407ac3 Add initial Avalonia UI project for migration
Create Flow.Launcher.Avalonia project as foundation for migrating from WPF to Avalonia UI framework.

Key components:
- MainWindow with query box and results list (matching WPF layout)
- ViewModels: MainViewModel, ResultsViewModel, ResultViewModel
- Themes/Base.axaml with converted styles from WPF
- FluentAvaloniaUI for Windows 11 styling
- References existing Core/Infrastructure/Plugin projects

The project builds and runs alongside the existing WPF application.
This is Phase 1 of the incremental migration approach.
2026-01-14 23:01:35 -08:00

151 lines
3.7 KiB
C#

using System;
using System.Collections.ObjectModel;
using System.Threading.Tasks;
using System.Windows.Input;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using Flow.Launcher.Infrastructure.UserSettings;
namespace Flow.Launcher.Avalonia.ViewModel;
/// <summary>
/// Simplified MainViewModel for the Avalonia version.
/// This will eventually be unified with the WPF MainViewModel.
/// </summary>
public partial class MainViewModel : ObservableObject
{
private readonly Settings _settings;
[ObservableProperty]
private string _queryText = string.Empty;
[ObservableProperty]
private string _querySuggestionText = string.Empty;
[ObservableProperty]
private bool _isQueryRunning;
[ObservableProperty]
private bool _hasResults;
[ObservableProperty]
private ResultsViewModel _results;
public Settings Settings => _settings;
public MainViewModel(Settings settings)
{
_settings = settings;
_results = new ResultsViewModel(settings);
// Add some demo results for testing
AddDemoResults();
}
partial void OnQueryTextChanged(string value)
{
// Simulate query execution
if (!string.IsNullOrWhiteSpace(value))
{
IsQueryRunning = true;
HasResults = true;
// Simulate search
Task.Delay(100).ContinueWith(_ =>
{
IsQueryRunning = false;
}, TaskScheduler.FromCurrentSynchronizationContext());
}
else
{
HasResults = false;
IsQueryRunning = false;
}
}
private void AddDemoResults()
{
// Add demo results for UI testing
Results.AddResult(new ResultViewModel
{
Title = "Welcome to Flow Launcher (Avalonia)",
SubTitle = "This is a demo result - Avalonia migration in progress",
IconPath = "Images/app.png"
});
Results.AddResult(new ResultViewModel
{
Title = "Settings",
SubTitle = "Open Flow Launcher settings",
IconPath = "Images/app.png"
});
Results.AddResult(new ResultViewModel
{
Title = "Notepad",
SubTitle = "C:\\Windows\\System32\\notepad.exe",
IconPath = "Images/app.png"
});
Results.AddResult(new ResultViewModel
{
Title = "Calculator",
SubTitle = "Microsoft Calculator",
IconPath = "Images/app.png"
});
HasResults = true;
}
[RelayCommand]
private void Esc()
{
QueryText = string.Empty;
}
[RelayCommand]
private void OpenResult(object? parameter)
{
var selectedResult = Results.SelectedItem;
if (selectedResult != null)
{
// Execute the result action
System.Diagnostics.Debug.WriteLine($"Opening result: {selectedResult.Title}");
}
}
[RelayCommand]
private void SelectNextItem()
{
Results.SelectNextItem();
}
[RelayCommand]
private void SelectPrevItem()
{
Results.SelectPrevItem();
}
[RelayCommand]
private void AutocompleteQuery()
{
if (Results.SelectedItem != null)
{
QueryText = Results.SelectedItem.Title;
}
}
[RelayCommand]
private void ReloadPluginData()
{
// Placeholder for plugin data reload
System.Diagnostics.Debug.WriteLine("Reloading plugin data...");
}
[RelayCommand]
private void ReQuery()
{
// Placeholder for re-query
System.Diagnostics.Debug.WriteLine("Re-querying...");
}
}