Skip to content

Commit 7c9ffb0

Browse files
committed
snapping now uses an interface, so that it's not TermControl-specific
1 parent 84df819 commit 7c9ffb0

File tree

5 files changed

+64
-31
lines changed

5 files changed

+64
-31
lines changed

src/cascadia/TerminalApp/IPaneContent.idl

+12
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,18 @@ namespace TerminalApp
2222
// event CloseRequested(...);
2323

2424
event Windows.Foundation.TypedEventHandler<Object, Object> CloseRequested;
25+
};
26+
27+
28+
enum PaneSnapDirection
29+
{
30+
Width,
31+
Height
32+
};
2533

34+
interface ISnappable
35+
{
36+
Single SnapDownToGrid(PaneSnapDirection direction, Single sizeToSnap);
37+
Windows.Foundation.Size GridSize { get; };
2638
};
2739
}

src/cascadia/TerminalApp/Pane.cpp

+39-30
Original file line numberDiff line numberDiff line change
@@ -2789,12 +2789,12 @@ float Pane::CalcSnappedDimension(const bool widthOrHeight, const float dimension
27892789
// If requested size is already snapped, then both returned values equal this value.
27902790
Pane::SnapSizeResult Pane::_CalcSnappedDimension(const bool widthOrHeight, const float dimension) const
27912791
{
2792-
// TODO!: Again, bad. We're special-casing that the content just so happens to have a TermControl
2793-
const auto& termControl{ _content.GetRoot().try_as<TermControl>() };
2792+
const auto direction{ widthOrHeight ? PaneSnapDirection::Width : PaneSnapDirection::Height };
27942793

27952794
if (_IsLeaf())
27962795
{
2797-
if (!termControl)
2796+
const auto& snappable{ _content.try_as<ISnappable>() };
2797+
if (!snappable)
27982798
{
27992799
return { dimension, dimension };
28002800
}
@@ -2809,8 +2809,10 @@ Pane::SnapSizeResult Pane::_CalcSnappedDimension(const bool widthOrHeight, const
28092809
return { minDimension, minDimension };
28102810
}
28112811

2812-
auto lower = termControl.SnapDimensionToGrid(widthOrHeight, dimension);
2813-
if (widthOrHeight)
2812+
auto lower = snappable.SnapDownToGrid(widthOrHeight ? PaneSnapDirection::Width : PaneSnapDirection::Height,
2813+
dimension);
2814+
2815+
if (direction == PaneSnapDirection::Width)
28142816
{
28152817
lower += WI_IsFlagSet(_borders, Borders::Left) ? PaneBorderSize : 0;
28162818
lower += WI_IsFlagSet(_borders, Borders::Right) ? PaneBorderSize : 0;
@@ -2829,8 +2831,10 @@ Pane::SnapSizeResult Pane::_CalcSnappedDimension(const bool widthOrHeight, const
28292831
}
28302832
else
28312833
{
2832-
const auto cellSize = termControl.CharacterDimensions();
2833-
const auto higher = lower + (widthOrHeight ? cellSize.Width : cellSize.Height);
2834+
const auto cellSize = snappable.GridSize();
2835+
const auto higher = lower + (direction == PaneSnapDirection::Width ?
2836+
cellSize.Width :
2837+
cellSize.Height);
28342838
return { lower, higher };
28352839
}
28362840
}
@@ -2873,36 +2877,41 @@ Pane::SnapSizeResult Pane::_CalcSnappedDimension(const bool widthOrHeight, const
28732877
// Return Value:
28742878
// - <none>
28752879
void Pane::_AdvanceSnappedDimension(const bool widthOrHeight, LayoutSizeNode& sizeNode) const
2876-
{ // TODO!: Again, bad. We're special-casing that the content just so happens to have a TermControl
2877-
const auto& termControl{ _content.GetRoot().try_as<TermControl>() };
2878-
if (_IsLeaf() && termControl)
2880+
{
2881+
if (_IsLeaf())
28792882
{
2880-
// We're a leaf pane, so just add one more row or column (unless isMinimumSize
2881-
// is true, see below).
2882-
2883-
if (sizeNode.isMinimumSize)
2883+
const auto& snappable{ _content.try_as<ISnappable>() };
2884+
if (snappable)
28842885
{
2885-
// If the node is of its minimum size, this size might not be snapped (it might
2886-
// be, say, half a character, or fixed 10 pixels), so snap it upward. It might
2887-
// however be already snapped, so add 1 to make sure it really increases
2888-
// (not strictly necessary but to avoid surprises).
2889-
sizeNode.size = _CalcSnappedDimension(widthOrHeight, sizeNode.size + 1).higher;
2886+
// We're a leaf pane, so just add one more row or column (unless isMinimumSize
2887+
// is true, see below).
2888+
2889+
if (sizeNode.isMinimumSize)
2890+
{
2891+
// If the node is of its minimum size, this size might not be snapped (it might
2892+
// be, say, half a character, or fixed 10 pixels), so snap it upward. It might
2893+
// however be already snapped, so add 1 to make sure it really increases
2894+
// (not strictly necessary but to avoid surprises).
2895+
sizeNode.size = _CalcSnappedDimension(widthOrHeight,
2896+
sizeNode.size + 1)
2897+
.higher;
2898+
}
2899+
else
2900+
{
2901+
const auto cellSize = snappable.GridSize();
2902+
sizeNode.size += widthOrHeight ? cellSize.Width : cellSize.Height;
2903+
}
28902904
}
28912905
else
28922906
{
2893-
const auto cellSize = termControl.CharacterDimensions();
2894-
sizeNode.size += widthOrHeight ? cellSize.Width : cellSize.Height;
2907+
// If we're a leaf that didn't have a TermControl, then just increment
2908+
// by one. We have to increment by _some_ value, because this is used in
2909+
// a while() loop to find the next bigger size we can snap to. But since
2910+
// a non-terminal control doesn't really care what size it's snapped to,
2911+
// we can just say "one pixel larger is the next snap point"
2912+
sizeNode.size += 1;
28952913
}
28962914
}
2897-
else if (_IsLeaf())
2898-
{
2899-
// If we're a leaf that didn't have a TermControl, then just increment
2900-
// by one. We have to increment by _some_ value, because this is used in
2901-
// a while() loop to find the next bigger size we can snap to. But since
2902-
// a non-terminal control doesn't really care what size it's snapped to,
2903-
// we can just say "one pixel larger is the next snap point"
2904-
sizeNode.size += 1;
2905-
}
29062915
else
29072916
{
29082917
// We're a parent pane, so we have to advance dimension of our children panes. In

src/cascadia/TerminalApp/TerminalPaneContent.cpp

+9
Original file line numberDiff line numberDiff line change
@@ -270,4 +270,13 @@ namespace winrt::TerminalApp::implementation
270270
{
271271
_isDefTermSession = true;
272272
}
273+
274+
float TerminalPaneContent::SnapDownToGrid(const TerminalApp::PaneSnapDirection direction, const float sizeToSnap)
275+
{
276+
return _control.SnapDimensionToGrid(direction == PaneSnapDirection::Width, sizeToSnap);
277+
}
278+
Windows::Foundation::Size TerminalPaneContent::GridSize()
279+
{
280+
return _control.CharacterDimensions();
281+
}
273282
}

src/cascadia/TerminalApp/TerminalPaneContent.h

+3
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ namespace winrt::TerminalApp::implementation
3636
uint64_t TaskbarProgress() { return _control.TaskbarProgress(); }
3737
bool ReadOnly() { return _control.ReadOnly(); }
3838

39+
float SnapDownToGrid(const TerminalApp::PaneSnapDirection direction, const float sizeToSnap);
40+
Windows::Foundation::Size GridSize();
41+
3942
til::typed_event<TerminalApp::TerminalPaneContent, winrt::Windows::Foundation::IInspectable> RestartTerminalRequested;
4043
til::typed_event<> CloseRequested;
4144

src/cascadia/TerminalApp/TerminalPaneContent.idl

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import "IPaneContent.idl";
55

66
namespace TerminalApp
77
{
8-
[default_interface] runtimeclass TerminalPaneContent : IPaneContent
8+
[default_interface] runtimeclass TerminalPaneContent : IPaneContent, ISnappable
99
{
1010
Microsoft.Terminal.Control.TermControl GetTerminal();
1111

0 commit comments

Comments
 (0)