Skip to content

Commit bc04f43

Browse files
committed
Replaced my shitty AppBar implementation with ManagedShell
1 parent 28cfbc8 commit bc04f43

File tree

5 files changed

+62
-168
lines changed

5 files changed

+62
-168
lines changed

GoAwayEdge/Common/Configuration.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System.IO;
22
using System.Windows;
33
using GoAwayEdge.Common.Debugging;
4+
using ManagedShell;
45
using Microsoft.Win32;
56

67
namespace GoAwayEdge.Common
@@ -51,6 +52,9 @@ internal class Configuration
5152
Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles),
5253
"valnoxy",
5354
"GoAwayEdge");
55+
public static ShellManager ShellManager { get; set; }
56+
public static bool AppBarIsAttached { get; set; }
57+
5458

5559
/// <summary>
5660
/// Initialize the current environment.
@@ -69,9 +73,10 @@ public static bool InitialEnvironment()
6973
if (NoEdgeInstalled)
7074
return true;
7175

76+
ShellManager = new ShellManager();
77+
7278
FileConfiguration.EdgePath = RegistryConfig.GetKey("EdgeFilePath");
7379
FileConfiguration.NonIfeoPath = RegistryConfig.GetKey("EdgeNonIEFOFilePath");
74-
7580
try
7681
{
7782
Channel = Runtime.ArgumentParse.ParseEdgeChannel(RegistryConfig.GetKey("EdgeChannel"));

GoAwayEdge/GoAwayEdge.csproj

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,10 @@
5555
<Resource Include="Localization\ResourceDictionary.xaml" />
5656
</ItemGroup>
5757
<ItemGroup>
58+
<PackageReference Include="ManagedShell" Version="0.0.260" />
5859
<PackageReference Include="Microsoft.Toolkit.Uwp.Notifications" Version="7.1.3" />
59-
<PackageReference Include="Microsoft.Web.WebView2" Version="1.0.2592.51" />
60-
<PackageReference Include="System.Drawing.Common" Version="8.0.7" />
60+
<PackageReference Include="Microsoft.Web.WebView2" Version="1.0.2651.64" />
61+
<PackageReference Include="System.Drawing.Common" Version="8.0.8" />
6162
<PackageReference Include="TaskScheduler" Version="2.11.0" />
6263
<PackageReference Include="WPF-UI" Version="3.0.5" />
6364
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />

GoAwayEdge/UserInterface/CopilotDock/CopilotDock.xaml

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<ui:FluentWindow x:Class="GoAwayEdge.UserInterface.CopilotDock.CopilotDock"
1+
<appBar:AppBarWindow x:Class="GoAwayEdge.UserInterface.CopilotDock.CopilotDock"
22
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
33
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
44
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
@@ -8,13 +8,12 @@
88
Title="GoAwayEdge - Copilot Dock" Height="450" Width="800"
99
xmlns:ui="http://schemas.lepo.co/wpfui/2022/xaml"
1010
xmlns:wv2="clr-namespace:Microsoft.Web.WebView2.Wpf;assembly=Microsoft.Web.WebView2.Wpf"
11+
xmlns:appBar="clr-namespace:ManagedShell.AppBar;assembly=ManagedShell.AppBar"
1112
Background="{ui:ThemeResource ApplicationBackgroundBrush}"
12-
ExtendsContentIntoTitleBar="True"
13-
WindowBackdropType="Mica"
14-
WindowCornerPreference="Round"
1513
WindowStartupLocation="CenterScreen"
16-
MouseDown="Window_MouseDown"
17-
ShowInTaskbar="False"
14+
ResizeMode="CanResize"
15+
WindowStyle="None"
16+
AllowsTransparency="True"
1817
Deactivated="CopilotDock_OnDeactivated">
1918
<Grid>
2019
<Grid.RowDefinitions>
@@ -41,4 +40,4 @@
4140
<wv2:WebView2 x:Name="WebView" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"/>
4241
</Grid>
4342
</Grid>
44-
</ui:FluentWindow>
43+
</appBar:AppBarWindow>

GoAwayEdge/UserInterface/CopilotDock/CopilotDock.xaml.cs

Lines changed: 15 additions & 156 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22
using System.IO;
33
using System.Runtime.InteropServices;
44
using System.Windows;
5-
using System.Windows.Input;
65
using GoAwayEdge.Common;
76
using GoAwayEdge.Common.Debugging;
7+
using ManagedShell;
8+
using ManagedShell.AppBar;
89
using Microsoft.Web.WebView2.Core;
910
using Wpf.Ui.Controls;
1011
using static GoAwayEdge.Common.AiProvider;
@@ -16,9 +17,13 @@ namespace GoAwayEdge.UserInterface.CopilotDock
1617
/// </summary>
1718
public partial class CopilotDock
1819
{
19-
private static bool _isDocked;
20-
public CopilotDock()
20+
public CopilotDock(ShellManager shellManager, AppBarScreen screen, AppBarEdge edge, double desiredHeight, AppBarMode mode)
21+
: base(shellManager.AppBarManager, shellManager.ExplorerHelper, shellManager.FullScreenHelper, screen, edge, mode, desiredHeight)
2122
{
23+
this.MaxHeight = SystemParameters.WorkArea.Height;
24+
this.MinHeight = SystemParameters.WorkArea.Height;
25+
Configuration.AppBarIsAttached = mode != AppBarMode.None;
26+
2227
InitializeComponent();
2328
_ = InitializeWebViewAsync();
2429
}
@@ -49,186 +54,40 @@ private async Task InitializeWebViewAsync()
4954
Logging.Log($"Failed to load Provider! Provider Value '{Configuration.Provider}' in invalid!");
5055
throw new ArgumentOutOfRangeException();
5156
}
52-
53-
// Window settings
54-
var screenWidth = SystemParameters.PrimaryScreenWidth;
55-
var screenHeight = SystemParameters.PrimaryScreenHeight;
56-
57-
Visibility = Visibility.Visible;
58-
Width = screenWidth * 0.3;
59-
Height = screenHeight;
60-
MinWidth = 200;
61-
ResizeMode = ResizeMode.CanResizeWithGrip;
62-
63-
// Set window position
64-
Left = screenWidth - Width;
65-
Top = 0;
66-
67-
// Last state
68-
try
69-
{
70-
var lastState = RegistryConfig.GetKey("CopilotDockState", userSetting: true);
71-
if (string.IsNullOrEmpty(lastState))
72-
{
73-
DockWindowToRight();
74-
_isDocked = true;
75-
DockButton.Icon = new SymbolIcon(SymbolRegular.PinOff28);
76-
RegistryConfig.SetKey("CopilotDockState", "Docked", userSetting: true);
77-
}
78-
else if (lastState == "Docked")
79-
{
80-
DockWindowToRight();
81-
_isDocked = true;
82-
DockButton.Icon = new SymbolIcon(SymbolRegular.PinOff28);
83-
}
84-
else
85-
{
86-
_isDocked = false;
87-
DockButton.Icon = new SymbolIcon(SymbolRegular.Pin28);
88-
}
89-
}
90-
catch
91-
{
92-
DockWindowToRight();
93-
_isDocked = true;
94-
DockButton.Icon = new SymbolIcon(SymbolRegular.PinOff28);
95-
RegistryConfig.SetKey("CopilotDockState", "Docked", userSetting: true);
96-
}
9757
}
9858
catch (Exception ex)
9959
{
10060
Logging.Log($"Failed to load WebView2 (Copilot replacement): {ex.Message}", Logging.LogLevel.ERROR);
10161
}
10262
}
10363

104-
// Testing
105-
private void Window_MouseDown(object sender, MouseButtonEventArgs e)
106-
{
107-
if (e.ChangedButton == MouseButton.Left)
108-
this.DragMove();
109-
}
110-
111-
#region Docking
112-
113-
private void DockWindowToRight()
114-
{
115-
var screenWidth = SystemParameters.PrimaryScreenWidth;
116-
var screenHeight = SystemParameters.PrimaryScreenHeight;
117-
118-
Visibility = Visibility.Visible;
119-
Width = screenWidth * 0.3;
120-
Height = screenHeight;
121-
MinWidth = 200;
122-
ResizeMode = ResizeMode.CanResizeWithGrip;
123-
124-
// Set window position
125-
Left = screenWidth - Width;
126-
Top = 0;
127-
128-
// Register App Bar
129-
RegisterAppBar();
130-
}
131-
132-
[StructLayout(LayoutKind.Sequential)]
133-
private struct APPBARDATA
134-
{
135-
public uint cbSize;
136-
public IntPtr hWnd;
137-
public uint uCallbackMessage;
138-
public uint uEdge;
139-
public RECT rc;
140-
public int lParam;
141-
}
142-
143-
[StructLayout(LayoutKind.Sequential)]
144-
private struct RECT
145-
{
146-
public int left, top, right, bottom;
147-
}
148-
149-
private const int ABM_NEW = 0x00000000;
150-
private const int ABM_REMOVE = 0x00000001;
151-
private const int ABM_SETPOS = 0x00000003;
152-
private const int ABE_RIGHT = 2;
153-
154-
[DllImport("shell32.dll", CallingConvention = CallingConvention.StdCall)]
155-
private static extern uint SHAppBarMessage(uint dwMessage, ref APPBARDATA pData);
156-
157-
private void RegisterAppBar()
158-
{
159-
APPBARDATA appBarData = new APPBARDATA();
160-
appBarData.cbSize = (uint)Marshal.SizeOf(appBarData);
161-
appBarData.hWnd = new System.Windows.Interop.WindowInteropHelper(this).Handle;
162-
appBarData.uEdge = ABE_RIGHT;
163-
appBarData.rc.left = (int)(SystemParameters.PrimaryScreenWidth - this.Width);
164-
appBarData.rc.right = (int)SystemParameters.PrimaryScreenWidth;
165-
appBarData.rc.top = 0;
166-
appBarData.rc.bottom = (int)SystemParameters.PrimaryScreenHeight;
167-
168-
SHAppBarMessage(ABM_NEW, ref appBarData);
169-
SHAppBarMessage(ABM_SETPOS, ref appBarData);
170-
}
171-
172-
protected override void OnRenderSizeChanged(SizeChangedInfo sizeInfo)
173-
{
174-
base.OnRenderSizeChanged(sizeInfo);
175-
AdjustWindowPosition();
176-
}
177-
178-
private void AdjustWindowPosition()
179-
{
180-
this.Left = SystemParameters.PrimaryScreenWidth - this.Width;
181-
}
182-
183-
protected override void OnMouseLeftButtonUp(System.Windows.Input.MouseButtonEventArgs e)
184-
{
185-
base.OnMouseLeftButtonUp(e);
186-
if (_isDocked) RegisterAppBar();
187-
}
188-
189-
private void UnregisterAppBar()
190-
{
191-
APPBARDATA appBarData = new APPBARDATA();
192-
appBarData.cbSize = (uint)Marshal.SizeOf(appBarData);
193-
appBarData.hWnd = new System.Windows.Interop.WindowInteropHelper(this).Handle;
194-
195-
SHAppBarMessage(ABM_REMOVE, ref appBarData);
196-
}
197-
198-
protected override void OnClosed(EventArgs e)
199-
{
200-
base.OnClosed(e);
201-
UnregisterAppBar();
202-
}
203-
204-
#endregion
205-
20664
private void CloseButton_OnClick(object sender, RoutedEventArgs e)
20765
{
66+
Configuration.ShellManager.AppBarManager.SignalGracefulShutdown();
67+
Configuration.ShellManager.Dispose();
20868
this.Close();
20969
}
21070

21171
private void DockButton_OnClick(object sender, RoutedEventArgs e)
21272
{
213-
if (_isDocked)
73+
Configuration.ShellManager.AppBarManager.RegisterBar(this, Width, Height, AppBarEdge.Right);
74+
75+
if (Configuration.AppBarIsAttached)
21476
{
215-
UnregisterAppBar();
216-
_isDocked = false;
21777
DockButton.Icon = new SymbolIcon(SymbolRegular.Pin28);
21878
RegistryConfig.SetKey("CopilotDockState", "Detached", userSetting: true);
21979
}
22080
else
22181
{
222-
DockWindowToRight();
223-
_isDocked = true;
22482
DockButton.Icon = new SymbolIcon(SymbolRegular.PinOff28);
22583
RegistryConfig.SetKey("CopilotDockState", "Docked", userSetting: true);
22684
}
85+
Configuration.AppBarIsAttached = !Configuration.AppBarIsAttached;
22786
}
22887

22988
private void CopilotDock_OnDeactivated(object? sender, EventArgs e)
23089
{
231-
if (_isDocked) return;
90+
if (Configuration.AppBarIsAttached) return;
23291
var currentProcess = Process.GetCurrentProcess();
23392
var currentTitle = currentProcess.MainWindowTitle;
23493
var currentId = currentProcess.Id;

GoAwayEdge/UserInterface/CopilotDock/InterfaceManager.cs

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
using System.Diagnostics;
22
using System.Runtime.InteropServices;
33
using System.Text;
4+
using GoAwayEdge.Common;
45
using GoAwayEdge.Common.Debugging;
6+
using ManagedShell.AppBar;
57

68
namespace GoAwayEdge.UserInterface.CopilotDock
79
{
@@ -12,16 +14,44 @@ public static void ShowDock()
1214
using var mutex = new Mutex(true, "GoAwayEdge_CopilotDock", out var createdNew);
1315
if (createdNew)
1416
{
17+
var mode = AppBarMode.Normal;
18+
19+
// Last state
20+
try
21+
{
22+
var lastState = RegistryConfig.GetKey("CopilotDockState", userSetting: true);
23+
if (string.IsNullOrEmpty(lastState))
24+
{
25+
// Never set; leave Mode as Normal
26+
RegistryConfig.SetKey("CopilotDockState", "Docked", userSetting: true);
27+
}
28+
else if (lastState != "Docked")
29+
{
30+
// Undocked or unknown state; set to None
31+
mode = AppBarMode.None;
32+
}
33+
}
34+
catch
35+
{
36+
// Error reading last state; set to Docked
37+
RegistryConfig.SetKey("CopilotDockState", "Docked", userSetting: true);
38+
}
39+
1540
var closed = false;
16-
var copilotDock = new CopilotDock();
41+
var copilotDock = new CopilotDock(
42+
Configuration.ShellManager,
43+
AppBarScreen.FromPrimaryScreen(),
44+
AppBarEdge.Right,
45+
500, // temporary size
46+
mode);
1747
copilotDock.Closed += (sender, args) => closed = true;
1848
copilotDock.ShowDialog();
1949

2050
while (!closed)
2151
{
2252
Thread.Sleep(1000);
2353
}
24-
Logging.Log("Closed CopilotDock", Logging.LogLevel.INFO);
54+
Logging.Log("Closed CopilotDock");
2555
}
2656
else
2757
{

0 commit comments

Comments
 (0)