2
2
using System . IO ;
3
3
using System . Runtime . InteropServices ;
4
4
using System . Windows ;
5
- using System . Windows . Input ;
6
5
using GoAwayEdge . Common ;
7
6
using GoAwayEdge . Common . Debugging ;
7
+ using ManagedShell ;
8
+ using ManagedShell . AppBar ;
8
9
using Microsoft . Web . WebView2 . Core ;
9
10
using Wpf . Ui . Controls ;
10
11
using static GoAwayEdge . Common . AiProvider ;
@@ -16,9 +17,13 @@ namespace GoAwayEdge.UserInterface.CopilotDock
16
17
/// </summary>
17
18
public partial class CopilotDock
18
19
{
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 )
21
22
{
23
+ this . MaxHeight = SystemParameters . WorkArea . Height ;
24
+ this . MinHeight = SystemParameters . WorkArea . Height ;
25
+ Configuration . AppBarIsAttached = mode != AppBarMode . None ;
26
+
22
27
InitializeComponent ( ) ;
23
28
_ = InitializeWebViewAsync ( ) ;
24
29
}
@@ -49,186 +54,40 @@ private async Task InitializeWebViewAsync()
49
54
Logging . Log ( $ "Failed to load Provider! Provider Value '{ Configuration . Provider } ' in invalid!") ;
50
55
throw new ArgumentOutOfRangeException ( ) ;
51
56
}
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
- }
97
57
}
98
58
catch ( Exception ex )
99
59
{
100
60
Logging . Log ( $ "Failed to load WebView2 (Copilot replacement): { ex . Message } ", Logging . LogLevel . ERROR ) ;
101
61
}
102
62
}
103
63
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
-
206
64
private void CloseButton_OnClick ( object sender , RoutedEventArgs e )
207
65
{
66
+ Configuration . ShellManager . AppBarManager . SignalGracefulShutdown ( ) ;
67
+ Configuration . ShellManager . Dispose ( ) ;
208
68
this . Close ( ) ;
209
69
}
210
70
211
71
private void DockButton_OnClick ( object sender , RoutedEventArgs e )
212
72
{
213
- if ( _isDocked )
73
+ Configuration . ShellManager . AppBarManager . RegisterBar ( this , Width , Height , AppBarEdge . Right ) ;
74
+
75
+ if ( Configuration . AppBarIsAttached )
214
76
{
215
- UnregisterAppBar ( ) ;
216
- _isDocked = false ;
217
77
DockButton . Icon = new SymbolIcon ( SymbolRegular . Pin28 ) ;
218
78
RegistryConfig . SetKey ( "CopilotDockState" , "Detached" , userSetting : true ) ;
219
79
}
220
80
else
221
81
{
222
- DockWindowToRight ( ) ;
223
- _isDocked = true ;
224
82
DockButton . Icon = new SymbolIcon ( SymbolRegular . PinOff28 ) ;
225
83
RegistryConfig . SetKey ( "CopilotDockState" , "Docked" , userSetting : true ) ;
226
84
}
85
+ Configuration . AppBarIsAttached = ! Configuration . AppBarIsAttached ;
227
86
}
228
87
229
88
private void CopilotDock_OnDeactivated ( object ? sender , EventArgs e )
230
89
{
231
- if ( _isDocked ) return ;
90
+ if ( Configuration . AppBarIsAttached ) return ;
232
91
var currentProcess = Process . GetCurrentProcess ( ) ;
233
92
var currentTitle = currentProcess . MainWindowTitle ;
234
93
var currentId = currentProcess . Id ;
0 commit comments