Skip to content

Commit 65fe2ee

Browse files
committed
Calc: Step 4: Use AppWindow's CompactOverlay mode to make a compact view of the Calc app
1 parent 4836b04 commit 65fe2ee

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed

Samples/Islands/DrawingIsland/CalculatorDemo/MainWindow.cs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
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;
46
using System;
57
using System.Globalization;
68
using System.Windows;
79
using System.Windows.Controls;
810
using System.Windows.Input;
11+
using System.Windows.Interop;
12+
using Windows.ApplicationModel.Contacts;
913

1014
namespace 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
}

Samples/Islands/DrawingIsland/CalculatorDemo/MainWindow.xaml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
<MenuItem Header="View">
4444
<MenuItem Name="StandardMenu" Click="OnMenuStandard" IsCheckable="true" IsChecked="True"
4545
Header="Standard" />
46+
<MenuItem Name="CompactView" Click="CompactView_Click" Header="Enter Compact View" />
4647
</MenuItem>
4748
<MenuItem Header="Help">
4849
<MenuItem Click="OnMenuAbout" Header="About" />
@@ -140,6 +141,13 @@
140141
<local:MyTextBox x:Name="PaperBox" BorderBrush="LightGray"/>
141142
</Border>
142143
</Grid>
144+
145+
</DockPanel>
146+
147+
<!-- This is what's shown when we enter the compat view mode-->
148+
<DockPanel Name="CompactPanel" Visibility="Collapsed">
149+
<Button Name="ExitCompactViewButton" Click="ExitCompactViewButton_Click" DockPanel.Dock="Top">Exit Compact View</Button>
150+
<TextBlock Margin="3" Name="CompactViewText" FontSize="22" />
143151
</DockPanel>
144152
</Grid>
145153
</Window>

0 commit comments

Comments
 (0)