-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainWindow.xaml.cs
More file actions
232 lines (201 loc) · 7.6 KB
/
MainWindow.xaml.cs
File metadata and controls
232 lines (201 loc) · 7.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
using System.Windows;
using Microsoft.Web.WebView2.Core;
using PiPCrunchy.Services;
using System.Reflection;
namespace PiPCrunchy;
/// <summary>
/// Interaction logic for MainWindow.xaml
/// Main browser window for multiple streaming platforms
/// </summary>
public partial class MainWindow : Window
{
// Streaming service URLs
private const string CrunchyrollUrl = "https://www.crunchyroll.com";
private const string NetflixUrl = "https://www.netflix.com";
private const string DisneyPlusUrl = "https://www.disneyplus.com";
private const string PrimeVideoUrl = "https://www.primevideo.com";
private const string YouTubeUrl = "https://www.youtube.com";
private const string TwitchUrl = "https://www.twitch.tv";
private const string FlixtorUrl = "https://flixtor.to/viplogin";
private PipWindow? _pipWindow;
private string _currentUrl = "";
public MainWindow()
{
InitializeComponent();
InitializeAsync();
}
private async void InitializeAsync()
{
try
{
// Use shared WebView2 environment to avoid conflicts
var environment = await WebView2EnvironmentManager.GetEnvironmentAsync();
await MainWebView.EnsureCoreWebView2Async(environment);
// Configure WebView2 settings for optimal performance
if (MainWebView.CoreWebView2 != null)
{
var settings = MainWebView.CoreWebView2.Settings;
settings.IsStatusBarEnabled = false;
settings.AreDefaultContextMenusEnabled = true;
settings.AreDevToolsEnabled = false; // Disable dev tools for better performance
settings.IsZoomControlEnabled = true;
settings.IsSwipeNavigationEnabled = false; // Reduce input overhead
settings.AreBrowserAcceleratorKeysEnabled = false; // Reduce key event overhead
settings.IsGeneralAutofillEnabled = false; // Reduce memory/CPU overhead
settings.IsPasswordAutosaveEnabled = false; // Reduce overhead
}
}
catch (Exception ex)
{
MessageBox.Show(
$"Failed to initialize browser: {ex.Message}",
"Initialization Error",
MessageBoxButton.OK,
MessageBoxImage.Error
);
}
}
private void NavigateToUrl(string url, string serviceName)
{
if (MainWebView.CoreWebView2 != null)
{
_currentUrl = url;
MainWebView.CoreWebView2.Navigate(url);
// Hide welcome message, show browser
WelcomeMessage.Visibility = Visibility.Collapsed;
MainWebView.Visibility = Visibility.Visible;
// Update window title
Title = $"PiPStream - {serviceName}";
}
}
private void MainWebView_NavigationStarting(object? sender, CoreWebView2NavigationStartingEventArgs e)
{
// Update URL display
UrlTextBlock.Text = e.Uri;
_currentUrl = e.Uri;
}
private void MainWebView_NavigationCompleted(object? sender, CoreWebView2NavigationCompletedEventArgs e)
{
if (MainWebView.CoreWebView2 != null)
{
// Update URL display to final URL (after redirects)
UrlTextBlock.Text = MainWebView.CoreWebView2.Source;
_currentUrl = MainWebView.CoreWebView2.Source;
}
// Don't show error for ConnectionAborted - these services often work despite the error
// Only show errors for actual connection failures
if (!e.IsSuccess && e.WebErrorStatus != CoreWebView2WebErrorStatus.ConnectionAborted)
{
string? errorMessage = e.WebErrorStatus switch
{
CoreWebView2WebErrorStatus.Timeout =>
"Connection timed out. Please check your internet connection and try again.",
CoreWebView2WebErrorStatus.HostNameNotResolved =>
"Could not connect to the service. Please check your internet connection.",
CoreWebView2WebErrorStatus.OperationCanceled =>
null, // Don't show error for user-canceled navigation
_ => $"Navigation warning: {e.WebErrorStatus}\n\nThe page may still work - please wait for it to load."
};
if (errorMessage != null)
{
MessageBox.Show(
errorMessage,
"Navigation Warning",
MessageBoxButton.OK,
MessageBoxImage.Information
);
}
}
}
// Streaming service button click handlers
private void CrunchyrollButton_Click(object sender, RoutedEventArgs e)
{
NavigateToUrl(CrunchyrollUrl, "Crunchyroll");
}
private void NetflixButton_Click(object sender, RoutedEventArgs e)
{
NavigateToUrl(NetflixUrl, "Netflix");
}
private void DisneyButton_Click(object sender, RoutedEventArgs e)
{
NavigateToUrl(DisneyPlusUrl, "Disney+");
}
private void PrimeVideoButton_Click(object sender, RoutedEventArgs e)
{
NavigateToUrl(PrimeVideoUrl, "Prime Video");
}
private void YouTubeButton_Click(object sender, RoutedEventArgs e)
{
NavigateToUrl(YouTubeUrl, "YouTube");
}
private void TwitchButton_Click(object sender, RoutedEventArgs e)
{
NavigateToUrl(TwitchUrl, "Twitch");
}
private void FlixtorButton_Click(object sender, RoutedEventArgs e)
{
NavigateToUrl(FlixtorUrl, "Flixtor");
}
private void OpenPipButton_Click(object sender, RoutedEventArgs e)
{
// Check if there's a current URL loaded
if (string.IsNullOrEmpty(_currentUrl))
{
MessageBox.Show(
"Please select a streaming service first!",
"No Content Loaded",
MessageBoxButton.OK,
MessageBoxImage.Information
);
return;
}
// Check if PiP window already exists
if (_pipWindow != null && _pipWindow.IsLoaded)
{
// Bring existing window to front
_pipWindow.Activate();
_pipWindow.Focus();
return;
}
// Create new PiP window with current URL
try
{
_pipWindow = new PipWindow(_currentUrl);
_pipWindow.Closed += (s, args) => _pipWindow = null;
_pipWindow.Show();
}
catch (Exception ex)
{
MessageBox.Show(
$"Failed to open Picture-in-Picture window: {ex.Message}",
"Error",
MessageBoxButton.OK,
MessageBoxImage.Error
);
}
}
protected override void OnClosed(EventArgs e)
{
base.OnClosed(e);
// Clean up PiP window if it exists
_pipWindow?.Close();
}
private async void CheckForUpdates_Click(object sender, RoutedEventArgs e)
{
await UpdateService.CheckForUpdatesAsync(silent: false);
}
private void About_Click(object sender, RoutedEventArgs e)
{
var version = Assembly.GetExecutingAssembly().GetName().Version;
MessageBox.Show(
$"PiPStream v{version?.Major}.{version?.Minor}.{version?.Build}\n\n" +
"Multi-Platform Picture-in-Picture Viewer\n\n" +
"Watch streaming services while gaming!\n\n" +
"Supports: Crunchyroll, Netflix, Disney+, Prime Video,\n" +
"YouTube, Twitch, and Flixtor",
"About PiPStream",
MessageBoxButton.OK,
MessageBoxImage.Information
);
}
}