11// // Copyright (c) Microsoft. All rights reserved.
22// // Licensed under the MIT license. See LICENSE file in the project root for full license information.
33
4+ using Microsoft . UI ;
5+ using Microsoft . UI . Windowing ;
46using System ;
57using System . Globalization ;
68using System . Windows ;
79using System . Windows . Controls ;
810using System . Windows . Input ;
11+ using System . Windows . Interop ;
12+ using Windows . ApplicationModel . Contacts ;
913
1014namespace CalculatorDemo
1115{
@@ -18,6 +22,7 @@ public sealed partial class MainWindow : Window
1822 private Operation _lastOper ;
1923 private string _lastVal ;
2024 private string _memVal ;
25+ private AppWindow _appWindow ;
2126
2227 public MainWindow ( )
2328 {
@@ -444,5 +449,45 @@ public void Clear()
444449 _args = string . Empty ;
445450 }
446451 }
452+
453+ private void CompactView_Click ( object sender , RoutedEventArgs e )
454+ {
455+ SetCompactView ( true ) ;
456+ }
457+
458+ private void ExitCompactViewButton_Click ( object sender , RoutedEventArgs e )
459+ {
460+ SetCompactView ( false ) ;
461+ }
462+
463+ void SetCompactView ( bool useCompactView )
464+ {
465+ // Ensure we have an AppWindow for this WPF Window.
466+ if ( _appWindow == null )
467+ {
468+ _appWindow = AppWindow . GetFromWindowId (
469+ new WindowId ( ( ulong ) new WindowInteropHelper ( this ) . Handle ) ) ;
470+ }
471+
472+ if ( useCompactView )
473+ {
474+ // For compact view, hide the main panel and show the compact panel.
475+ MyPanel . Visibility = Visibility . Collapsed ;
476+ CompactPanel . Visibility = Visibility . Visible ;
477+
478+ CompactViewText . Text = DisplayBox . Text ;
479+
480+ // The AppWindow's CompactOverlay mode will make it always-on-top.
481+ _appWindow . SetPresenter ( AppWindowPresenterKind . CompactOverlay ) ;
482+ _appWindow . ResizeClient ( new Windows . Graphics . SizeInt32 ( 300 , 80 ) ) ;
483+ }
484+ else
485+ {
486+ MyPanel . Visibility = Visibility . Visible ;
487+ CompactPanel . Visibility = Visibility . Collapsed ;
488+
489+ _appWindow . SetPresenter ( AppWindowPresenterKind . Default ) ;
490+ }
491+ }
447492 }
448493}
0 commit comments