Initial commit
This commit is contained in:
@@ -0,0 +1,137 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using CommunityToolkit.Mvvm.ComponentModel;
|
||||
using CommunityToolkit.Mvvm.Input;
|
||||
using IPClab1.Enums;
|
||||
using IPClab1.Models;
|
||||
using IPClab1.Services;
|
||||
|
||||
namespace IPClab1.ViewModels;
|
||||
|
||||
public partial class MainWindowViewModel(WindowListService windowListService, WindowInteractionService windowInteractionService) : ObservableObject
|
||||
{
|
||||
private readonly WindowListService _windowListService = windowListService;
|
||||
private readonly WindowInteractionService _windowInteractionService = windowInteractionService;
|
||||
|
||||
[ObservableProperty]
|
||||
public ObservableCollection<WindowInfo> _windows = [];
|
||||
|
||||
[ObservableProperty]
|
||||
public partial bool VisibleOnly
|
||||
{
|
||||
get;
|
||||
set;
|
||||
}
|
||||
|
||||
[RelayCommand]
|
||||
public async Task RefreshWindows()
|
||||
{
|
||||
Windows.Clear();
|
||||
foreach (WindowInfo windowInfo in await _windowListService.GetVisibleWindowsAsync(VisibleOnly))
|
||||
Windows.Add(windowInfo);
|
||||
}
|
||||
|
||||
[ObservableProperty]
|
||||
[NotifyCanExecuteChangedFor(nameof(SetNewTitleCommand))]
|
||||
[NotifyCanExecuteChangedFor(nameof(SetWindowStateCommand))]
|
||||
[NotifyCanExecuteChangedFor(nameof(CloseWindowCommand))]
|
||||
[NotifyCanExecuteChangedFor(nameof(SendClickCommand))]
|
||||
public partial WindowInfo SelectedWindow
|
||||
{
|
||||
get;
|
||||
set;
|
||||
}
|
||||
|
||||
partial void OnSelectedWindowChanged(WindowInfo value)
|
||||
{
|
||||
(Width, Height) = _windowInteractionService.GetWindowSize(value.Hwnd);
|
||||
}
|
||||
|
||||
[ObservableProperty]
|
||||
public partial int Width
|
||||
{
|
||||
get;
|
||||
set;
|
||||
}
|
||||
|
||||
[ObservableProperty]
|
||||
public partial int Height
|
||||
{
|
||||
get;
|
||||
set;
|
||||
}
|
||||
|
||||
[ObservableProperty]
|
||||
public partial int X
|
||||
{
|
||||
get;
|
||||
set;
|
||||
}
|
||||
|
||||
[ObservableProperty]
|
||||
public partial int Y
|
||||
{
|
||||
get;
|
||||
set;
|
||||
}
|
||||
|
||||
[RelayCommand(CanExecute = nameof(IsWindowSelected))]
|
||||
public void SendClick()
|
||||
{
|
||||
_windowInteractionService.SimulateClick(SelectedWindow.Hwnd, X, Y);
|
||||
}
|
||||
|
||||
[ObservableProperty]
|
||||
[NotifyCanExecuteChangedFor(nameof(SetNewTitleCommand))]
|
||||
public partial string NewTitle
|
||||
{
|
||||
get;
|
||||
set;
|
||||
}
|
||||
|
||||
private bool CanSetNewTitle()
|
||||
{
|
||||
return !string.IsNullOrEmpty(NewTitle) && SelectedWindow != null;
|
||||
}
|
||||
|
||||
[RelayCommand(CanExecute = nameof(CanSetNewTitle))]
|
||||
public void SetNewTitle()
|
||||
{
|
||||
_windowInteractionService.SetText(SelectedWindow.Hwnd, NewTitle);
|
||||
Windows[Windows.IndexOf(SelectedWindow)].WindowTitle = NewTitle;
|
||||
}
|
||||
|
||||
private bool IsWindowSelected()
|
||||
{
|
||||
return SelectedWindow != null;
|
||||
}
|
||||
|
||||
[RelayCommand(CanExecute = nameof(IsWindowSelected))]
|
||||
public void SetWindowState(WindowState windowState)
|
||||
{
|
||||
_windowInteractionService.SetWindowState(SelectedWindow.Hwnd, windowState);
|
||||
}
|
||||
|
||||
[RelayCommand(CanExecute = nameof(IsWindowSelected))]
|
||||
public void CloseWindow()
|
||||
{
|
||||
_windowInteractionService.CloseWindow(SelectedWindow.Hwnd);
|
||||
SelectedWindow = null;
|
||||
}
|
||||
|
||||
[ObservableProperty]
|
||||
public partial string InfoString
|
||||
{
|
||||
get;
|
||||
set;
|
||||
}
|
||||
|
||||
public void RefreshInfo()
|
||||
{
|
||||
if (SelectedWindow != null) InfoString = _windowInteractionService.GetWindowDetails(SelectedWindow.Hwnd);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user