55// file LICENSE or copy at https://www.gnu.org/licenses/gpl-3.0.txt)
66//
77
8- using System ;
9- using System . Threading . Tasks ;
108using Telegram . Common ;
119using Telegram . Navigation ;
1210using Telegram . Views . Host ;
13- using Windows . Foundation ;
1411using Windows . UI ;
15- using Windows . UI . Core . Preview ;
1612using Windows . UI . ViewManagement ;
1713using Windows . UI . Xaml ;
1814
1915namespace Telegram . Controls
2016{
21- /// <summary>
22- /// Raised by <see cref="WindowContent.OnWindowCloseRequested"/>. Wraps the UWP args rather
23- /// than exposing them, so a root only ever sees Handled and a deferral - the two things
24- /// every caller actually used, and the two an island host can supply by other means.
25- /// </summary>
26- public class WindowCloseRequestedEventArgs : EventArgs
27- {
28- private readonly SystemNavigationCloseRequestedPreviewEventArgs _args ;
29- private TaskCompletionSource < bool > _deferral ;
30- private bool _handled ;
31-
32- internal WindowCloseRequestedEventArgs ( SystemNavigationCloseRequestedPreviewEventArgs args )
33- {
34- _args = args ;
35- }
36-
37- /// <summary>
38- /// Raised by the app rather than by the system - the window's own close button. Handled is
39- /// then just a bool, and the deferral has to be honoured by whoever raised it.
40- /// </summary>
41- internal WindowCloseRequestedEventArgs ( )
42- {
43- }
44-
45- public bool Handled
46- {
47- get => _args ? . Handled ?? _handled ;
48- set
49- {
50- if ( _args != null )
51- {
52- _args . Handled = value ;
53- }
54- else
55- {
56- _handled = value ;
57- }
58- }
59- }
60-
61- public Deferral GetDeferral ( )
62- {
63- if ( _args != null )
64- {
65- return _args . GetDeferral ( ) ;
66- }
67-
68- // Allocated only when a handler actually asks to defer, which is the uncommon case.
69- _deferral ??= new TaskCompletionSource < bool > ( ) ;
70- return new Deferral ( CompleteDeferral ) ;
71- }
72-
73- private void CompleteDeferral ( )
74- {
75- _deferral . TrySetResult ( true ) ;
76- }
77-
78- /// <summary>
79- /// Completes once every handler that took a deferral has released it. Only meaningful for
80- /// an app-raised request: the system waits on its own.
81- /// </summary>
82- internal Task WaitAsync ( )
83- {
84- return _deferral ? . Task ?? Task . CompletedTask ;
85- }
86- }
87-
8817 /// <summary>
8918 /// Base for anything assigned to <see cref="WindowContext.Content"/> - the root of a window,
9019 /// though not a window itself. Was WindowEx, declared at the bottom of VoipWindow.xaml.cs.
@@ -137,7 +66,7 @@ protected virtual void OnPopupClosed()
13766
13867 /// <summary>
13968 /// Wired once, here, rather than in each root: what raises these is the part that differs
140- /// between a CoreWindow and an island host, and this is the only place that should know.
69+ /// between a CoreWindow and an island host, and no root should have to know.
14170 ///
14271 /// OnLoaded rather than the constructor or the raw Loaded event: roots used to subscribe
14372 /// from their constructors and never unsubscribe, keeping themselves reachable from the
@@ -153,10 +82,9 @@ protected override void OnLoaded()
15382 {
15483 window . Activated += OnWindowActivatedCore ;
15584 window . VisibilityChanged += OnWindowVisibilityChangedCore ;
85+ window . CloseRequested += OnCloseRequestedCore ;
15686 }
15787
158- SystemNavigationManagerPreview . GetForCurrentView ( ) . CloseRequested += OnCloseRequestedCore ;
159-
16088 var view = ApplicationView . GetForCurrentView ( ) ;
16189 view . Consolidated += OnConsolidatedCore ;
16290 view . VisibleBoundsChanged += OnVisibleBoundsChangedCore ;
@@ -168,10 +96,9 @@ protected override void OnUnloaded()
16896 {
16997 window . Activated -= OnWindowActivatedCore ;
17098 window . VisibilityChanged -= OnWindowVisibilityChangedCore ;
99+ window . CloseRequested -= OnCloseRequestedCore ;
171100 }
172101
173- SystemNavigationManagerPreview . GetForCurrentView ( ) . CloseRequested -= OnCloseRequestedCore ;
174-
175102 var view = ApplicationView . GetForCurrentView ( ) ;
176103 view . Consolidated -= OnConsolidatedCore ;
177104 view . VisibleBoundsChanged -= OnVisibleBoundsChangedCore ;
@@ -187,9 +114,9 @@ private void OnWindowVisibilityChangedCore(object sender, WindowVisibilityEventA
187114 OnWindowVisibilityChanged ( args . IsVisible ) ;
188115 }
189116
190- private void OnCloseRequestedCore ( object sender , SystemNavigationCloseRequestedPreviewEventArgs args )
117+ private void OnCloseRequestedCore ( object sender , WindowCloseRequestedEventArgs args )
191118 {
192- OnWindowCloseRequested ( new WindowCloseRequestedEventArgs ( args ) ) ;
119+ OnWindowCloseRequested ( args ) ;
193120 }
194121
195122 // Neither carries a payload worth forwarding: the one caller of Consolidated ignored its
@@ -218,24 +145,6 @@ protected virtual void OnWindowCloseRequested(WindowCloseRequestedEventArgs args
218145 {
219146 }
220147
221- /// <summary>
222- /// The window's own close button, routed into the same override the system's close request
223- /// uses: a root has one place to intercept a close, whatever asked for it. Returns false
224- /// when the root refused.
225- ///
226- /// Awaited, because the answer can be a question: the web app window takes a deferral and
227- /// asks the user whether to discard the bot's changes.
228- /// </summary>
229- internal async Task < bool > RequestCloseAsync ( )
230- {
231- var args = new WindowCloseRequestedEventArgs ( ) ;
232- OnWindowCloseRequested ( args ) ;
233-
234- await args . WaitAsync ( ) ;
235-
236- return ! args . Handled ;
237- }
238-
239148 protected virtual void OnWindowConsolidated ( )
240149 {
241150 }
0 commit comments