Repository navigation
Expand file tree
/
Copy pathPipWindow.xaml.cs
More file actions
157 lines (133 loc) · 4.84 KB
/
Copy pathPipWindow.xaml.cs
File metadata and controls
157 lines (133 loc) · 4.84 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
using System.Windows;
using System.Windows.Input;
using Microsoft.Web.WebView2.Core;
using PiPCrunchy.Helpers;
namespace PiPCrunchy;
/// <summary>
/// Picture-in-Picture window that stays on top
/// </summary>
public partial class PipWindow : Window
{
private readonly string _initialUrl;
public PipWindow(string url)
{
InitializeComponent();
_initialUrl = url ?? "https://www.crunchyroll.com";
// Set initial position (bottom-right of screen with some padding)
SetInitialPosition();
// Initialize WebView2 asynchronously
InitializeAsync();
// Apply Win32 topmost as an additional layer
Loaded += (s, e) => WindowHelper.SetAlwaysOnTop(this);
// Subscribe to size changed event
SizeChanged += (s, e) => SaveWindowSize();
}
private void SetInitialPosition()
{
// Position in bottom-right corner with 20px padding
var workingArea = SystemParameters.WorkArea;
Left = workingArea.Right - Width - 20;
Top = workingArea.Bottom - Height - 20;
}
private async void InitializeAsync()
{
try
{
// Use shared WebView2 environment to avoid conflicts
var environment = await WebView2EnvironmentManager.GetEnvironmentAsync();
await PipWebView.EnsureCoreWebView2Async(environment);
// Configure WebView2 settings for maximum performance in PiP mode
if (PipWebView.CoreWebView2 != null)
{
var settings = PipWebView.CoreWebView2.Settings;
settings.IsStatusBarEnabled = false;
settings.AreDefaultContextMenusEnabled = false; // Reduce overhead in PiP
settings.AreDevToolsEnabled = false;
settings.IsZoomControlEnabled = false; // Reduce input overhead in PiP
settings.IsSwipeNavigationEnabled = false;
settings.AreBrowserAcceleratorKeysEnabled = false;
settings.IsGeneralAutofillEnabled = false;
settings.IsPasswordAutosaveEnabled = false;
settings.IsPinchZoomEnabled = false; // Reduce gesture processing
// Navigate to the URL
PipWebView.CoreWebView2.Navigate(_initialUrl);
}
}
catch (Exception ex)
{
MessageBox.Show(
$"Failed to initialize Picture-in-Picture browser: {ex.Message}",
"Initialization Error",
MessageBoxButton.OK,
MessageBoxImage.Error
);
Close();
}
}
private void PipWebView_NavigationCompleted(object? sender, CoreWebView2NavigationCompletedEventArgs e)
{
if (PipWebView.CoreWebView2 != null)
{
// Update title to show current page
var uri = new Uri(PipWebView.CoreWebView2.Source);
TitleTextBlock.Text = uri.Host;
}
}
private void TitleBar_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
// Allow window dragging by title bar
if (e.ChangedButton == MouseButton.Left)
{
DragMove();
}
}
private void MinimizeButton_Click(object sender, RoutedEventArgs e)
{
WindowState = WindowState.Minimized;
}
private void CloseButton_Click(object sender, RoutedEventArgs e)
{
Close();
}
protected override void OnDeactivated(EventArgs e)
{
base.OnDeactivated(e);
// Refresh topmost status when window loses focus
// This helps maintain topmost position
WindowHelper.RefreshTopmostStatus(this);
}
protected override void OnLocationChanged(EventArgs e)
{
base.OnLocationChanged(e);
// Save window position for future sessions (optional enhancement)
SaveWindowPosition();
}
private void SaveWindowPosition()
{
// Store position in application settings
Properties.Settings.Default.PipWindowLeft = Left;
Properties.Settings.Default.PipWindowTop = Top;
Properties.Settings.Default.Save();
}
private void SaveWindowSize()
{
// Store size in application settings
Properties.Settings.Default.PipWindowWidth = Width;
Properties.Settings.Default.PipWindowHeight = Height;
Properties.Settings.Default.Save();
}
private void LoadWindowSettings()
{
// Restore saved position and size if they exist
if (Properties.Settings.Default.PipWindowWidth > 0)
{
Width = Properties.Settings.Default.PipWindowWidth;
Height = Properties.Settings.Default.PipWindowHeight;
}
if (Properties.Settings.Default.PipWindowLeft > 0)
{
Left = Properties.Settings.Default.PipWindowLeft;
Top = Properties.Settings.Default.PipWindowTop;
}
}
}