Files
Mimante/Mimante/Core/EventHandlers/MainWindow.EventHandlers.Browser.cs
Alberto Balbo 6036896f7d Refactoring e nuove funzionalità per AutoBidder v4.0
* Aggiornamento alla versione 4.0.0
* Refactoring architetturale: introdotte partial classes e UserControls modulari per migliorare manutenibilità e leggibilità.
* Aggiunti nuovi UserControls: `AuctionMonitorControl`, `BrowserControl`, `SettingsControl`, `StatisticsControl`.
* Introdotto supporto per WebView2 per il browser integrato.
* Migliorata gestione delle aste: aggiunta/rimozione tramite URL o ID, configurazione predefinita.
* Nuove funzionalità di esportazione: supporto CSV, JSON, XML con opzioni configurabili.
* Logging avanzato: codifica colore per severità e auto-scroll.
* Tema scuro moderno e miglioramenti UI/UX: sidebar di navigazione, griglie virtualizzate, icone emoji.
* Persistenza dati: salvataggio automatico di aste e impostazioni in file JSON.
* Documentazione aggiornata: `README.md`, `CHANGELOG.md` e nuovi file di supporto.
* Miglioramenti alla sicurezza: cookie di sessione salvati in modo sicuro con DPAPI.
* Preparazione per future estensioni: placeholder per funzionalità avanzate e struttura modulare.
2025-11-17 16:01:22 +01:00

177 lines
5.9 KiB
C#

using System;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using Microsoft.Web.WebView2.Core;
namespace AutoBidder
{
/// <summary>
/// Browser event handlers and navigation
/// </summary>
public partial class MainWindow
{
private void BrowserBackButton_Click(object sender, RoutedEventArgs e)
{
try
{
if (EmbeddedWebView?.CoreWebView2 != null && EmbeddedWebView.CoreWebView2.CanGoBack)
EmbeddedWebView.CoreWebView2.GoBack();
}
catch { }
}
private void BrowserForwardButton_Click(object sender, RoutedEventArgs e)
{
try
{
if (EmbeddedWebView?.CoreWebView2 != null && EmbeddedWebView.CoreWebView2.CanGoForward)
EmbeddedWebView.CoreWebView2.GoForward();
}
catch { }
}
private void BrowserRefreshButton_Click(object sender, RoutedEventArgs e)
{
try
{
EmbeddedWebView?.Reload();
}
catch { }
}
private void BrowserHomeButton_Click(object sender, RoutedEventArgs e)
{
try
{
EmbeddedWebView?.CoreWebView2?.Navigate("https://it.bidoo.com/");
BrowserAddress.Text = "https://it.bidoo.com/";
}
catch { }
}
private void BrowserGoButton_Click(object sender, RoutedEventArgs e)
{
try
{
var url = BrowserAddress.Text?.Trim();
if (string.IsNullOrEmpty(url)) return;
if (!url.StartsWith("http", StringComparison.OrdinalIgnoreCase))
url = "https://" + url;
EmbeddedWebView?.CoreWebView2?.Navigate(url);
}
catch { }
}
private void BrowserAddAuctionButton_Click(object sender, RoutedEventArgs e)
{
try
{
var url = BrowserAddress.Text?.Trim() ?? EmbeddedWebView?.Source?.ToString();
if (!string.IsNullOrEmpty(url))
_ = AddAuctionFromUrl(url);
}
catch { }
}
private void EmbeddedWebView_NavigationStarting(object sender, CoreWebView2NavigationStartingEventArgs e)
{
try
{
BrowserAddress.Text = e.Uri ?? string.Empty;
var btn = this.FindName("BrowserAddAuctionButton") as Button;
if (btn != null)
btn.IsEnabled = IsValidAuctionUrl(e.Uri ?? string.Empty);
}
catch { }
}
private void EmbeddedWebView_NavigationCompleted(object sender, CoreWebView2NavigationCompletedEventArgs e)
{
try
{
var uri = EmbeddedWebView?.Source?.ToString() ?? BrowserAddress.Text;
BrowserAddress.Text = uri;
var btn = this.FindName("BrowserAddAuctionButton") as Button;
if (btn != null)
btn.IsEnabled = IsValidAuctionUrl(uri);
}
catch { }
}
private void EmbeddedWebView_PreviewMouseRightButtonUp(object sender, MouseButtonEventArgs e)
{
try
{
e.Handled = true;
}
catch { }
}
private void CoreWebView2_ContextMenuRequested(object? sender, CoreWebView2ContextMenuRequestedEventArgs e)
{
try
{
// Prevent default native menu
e.Handled = true;
var target = e.ContextMenuTarget;
string? link = null;
try
{
link = target?.LinkUri;
if (string.IsNullOrEmpty(link)) link = target?.PageUri;
}
catch { }
// Show WPF ContextMenu on UI thread
Dispatcher.BeginInvoke(new Action(() =>
{
try
{
var menu = new ContextMenu();
var canAdd = !string.IsNullOrEmpty(link) || IsValidAuctionUrl(EmbeddedWebView?.Source?.ToString() ?? BrowserAddress.Text);
var addItem = new MenuItem { Header = "Aggiungi Asta", IsEnabled = canAdd };
addItem.Click += async (s, args) =>
{
try
{
string? urlToAdd = link;
if (string.IsNullOrEmpty(urlToAdd))
urlToAdd = EmbeddedWebView?.Source?.ToString() ?? BrowserAddress.Text;
if (!string.IsNullOrEmpty(urlToAdd))
{
await AddAuctionFromUrl(urlToAdd);
}
}
catch (Exception ex)
{
Log($"[ERRORE] Aggiungi Asta dal menu: {ex.Message}");
}
};
menu.Items.Add(addItem);
var copyLink = new MenuItem { Header = "Copia link", IsEnabled = !string.IsNullOrEmpty(link) };
copyLink.Click += (s, args) =>
{
try { if (!string.IsNullOrEmpty(link)) Clipboard.SetText(link); }
catch { }
};
menu.Items.Add(copyLink);
menu.Placement = System.Windows.Controls.Primitives.PlacementMode.MousePoint;
menu.IsOpen = true;
}
catch { }
}));
}
catch { }
}
}
}