72 lines
1.8 KiB
C#
72 lines
1.8 KiB
C#
using System.Windows;
|
|
using System.Windows.Controls;
|
|
using System.Windows.Input;
|
|
using System.Windows.Threading;
|
|
using UtopiaCanteenSystem.ViewModels;
|
|
|
|
namespace UtopiaCanteenSystem.Views;
|
|
|
|
/// <summary>
|
|
/// Scanner view: minimal code-behind for focus management and Enter-key submission.
|
|
/// </summary>
|
|
public partial class ScannerView : UserControl
|
|
{
|
|
private bool _isUnloaded;
|
|
|
|
public ScannerView()
|
|
{
|
|
InitializeComponent();
|
|
Loaded += OnLoaded;
|
|
Unloaded += OnUnloaded;
|
|
}
|
|
|
|
private void OnLoaded(object sender, RoutedEventArgs e)
|
|
{
|
|
_isUnloaded = false;
|
|
FocusRfidInput(selectAll: true, DispatcherPriority.Input);
|
|
}
|
|
|
|
private void OnUnloaded(object sender, RoutedEventArgs e)
|
|
{
|
|
_isUnloaded = true;
|
|
}
|
|
|
|
private void FocusRfidInput(bool selectAll, DispatcherPriority priority)
|
|
{
|
|
if (_isUnloaded)
|
|
return;
|
|
|
|
Dispatcher.BeginInvoke(() =>
|
|
{
|
|
if (_isUnloaded || !IsVisible || !IsEnabled)
|
|
return;
|
|
|
|
RfidInputTextBox.Focus();
|
|
Keyboard.Focus(RfidInputTextBox);
|
|
|
|
if (selectAll)
|
|
RfidInputTextBox.SelectAll();
|
|
}, priority);
|
|
}
|
|
|
|
private void RfidInputTextBox_OnLostKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
|
|
{
|
|
// Keep the view "scan ready" by restoring focus to the RFID TextBox.
|
|
// Use a low priority so clicks (e.g., Logout) complete first.
|
|
FocusRfidInput(selectAll: false, DispatcherPriority.ApplicationIdle);
|
|
}
|
|
|
|
private void RfidInputTextBox_OnKeyDown(object sender, KeyEventArgs e)
|
|
{
|
|
if (e.Key != Key.Enter)
|
|
return;
|
|
|
|
if (DataContext is ScannerViewModel vm && vm.ScanCommand.CanExecute(null))
|
|
{
|
|
vm.ScanCommand.Execute(null);
|
|
e.Handled = true;
|
|
}
|
|
}
|
|
}
|
|
|