Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[RFC] Add Window.CanMinimize/CanMaximize #18117

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion native/Avalonia.Native/src/OSX/WindowImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ BEGIN_INTERFACE_MAP()
void DoZoom();

virtual HRESULT SetCanResize(bool value) override;

virtual HRESULT SetCanMinimize(bool value) override;

virtual HRESULT SetCanMaximize(bool value) override;

virtual HRESULT SetDecorations(SystemDecorations value) override;

Expand Down Expand Up @@ -80,7 +84,7 @@ BEGIN_INTERFACE_MAP()

bool CanBecomeKeyWindow ();

bool CanZoom() override { return _isEnabled && _canResize; }
bool CanZoom() override { return _isEnabled && _canMaximize; }

protected:
virtual NSWindowStyleMask CalculateStyleMask() override;
Expand All @@ -92,6 +96,8 @@ BEGIN_INTERFACE_MAP()
NSString *_lastTitle;
bool _isEnabled;
bool _canResize;
bool _canMinimize;
bool _canMaximize;
bool _fullScreenActive;
SystemDecorations _decorations;
AvnWindowState _lastWindowState;
Expand Down
27 changes: 23 additions & 4 deletions native/Avalonia.Native/src/OSX/WindowImpl.mm
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
_extendClientHints = AvnDefaultChrome;
_fullScreenActive = false;
_canResize = true;
_canMinimize = true;
_canMaximize = true;
_decorations = SystemDecorationsFull;
_transitioningWindowState = false;
_inSetWindowState = false;
Expand Down Expand Up @@ -191,7 +193,8 @@
void WindowImpl::DoZoom() {
if (_decorations == SystemDecorationsNone ||
_decorations == SystemDecorationsBorderOnly ||
_canResize == false) {
_canResize == false ||
_canMaximize == false) {
[Window setFrame:[Window screen].visibleFrame display:true];
} else {
[Window performZoom:Window];
Expand All @@ -208,6 +211,22 @@
}
}

HRESULT WindowImpl::SetCanMinimize(bool value) {
START_COM_ARP_CALL;

_canMinimize = value;
UpdateAppearance();
return S_OK;
}

HRESULT WindowImpl::SetCanMaximize(bool value) {
START_COM_ARP_CALL;

_canMaximize = value;
UpdateAppearance();
return S_OK;
}

HRESULT WindowImpl::SetDecorations(SystemDecorations value) {
START_COM_CALL;

Expand Down Expand Up @@ -577,7 +596,7 @@
break;
}

if (!IsOwned()) {
if (_canMinimize && !IsOwned()) {
s |= NSWindowStyleMaskMiniaturizable;
}

Expand Down Expand Up @@ -605,9 +624,9 @@
[closeButton setHidden:!hasTrafficLights];
[closeButton setEnabled:_isEnabled];
[miniaturizeButton setHidden:!hasTrafficLights];
[miniaturizeButton setEnabled:_isEnabled];
[miniaturizeButton setEnabled:_isEnabled && _canMinimize];
[zoomButton setHidden:!hasTrafficLights];
[zoomButton setEnabled:CanZoom()];
[zoomButton setEnabled:CanZoom() || (([Window styleMask] & NSWindowStyleMaskFullScreen) != 0 && _isEnabled)];
}

extern IAvnWindow* CreateAvnWindow(IAvnWindowEvents*events)
Expand Down
3 changes: 3 additions & 0 deletions samples/ControlCatalog/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
ExtendClientAreaToDecorationsHint="{Binding ExtendClientAreaEnabled}"
ExtendClientAreaChromeHints="{Binding ChromeHints}"
ExtendClientAreaTitleBarHeightHint="{Binding TitleBarHeight}"
CanResize="{Binding CanResize}"
CanMinimize="{Binding CanMinimize}"
CanMaximize="{Binding CanMaximize}"
x:Name="MainWindow"
Background="Transparent"
x:Class="ControlCatalog.MainWindow" WindowState="{Binding WindowState, Mode=TwoWay}"
Expand Down
49 changes: 43 additions & 6 deletions samples/ControlCatalog/Pages/WindowCustomizationsPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,55 @@
x:DataType="viewModels:MainWindowViewModel"
x:CompileBindings="True">
<StackPanel>
<StackPanel Spacing="10" Margin="25" IsEnabled="{OnFormFactor false, Desktop=true}">
<TextBlock Classes="h2" Text="Desktop properties" Margin="4" />
<CheckBox Content="Extend Client Area to Decorations" IsChecked="{Binding ExtendClientAreaEnabled}" />
<CheckBox Content="Title Bar" IsChecked="{Binding SystemTitleBarEnabled}" />
<CheckBox Content="Prefer System Chrome" IsChecked="{Binding PreferSystemChromeEnabled}" />
<Slider Minimum="-1" Maximum="200" Value="{Binding TitleBarHeight}" />

<StackPanel
Spacing="10"
Margin="25"
IsEnabled="{OnFormFactor false, Desktop=true}">

<TextBlock Classes="h2"
Text="Desktop properties"
Margin="4" />

<CheckBox Content="Extend Client Area to Decorations"
IsChecked="{Binding ExtendClientAreaEnabled}" />

<DockPanel IsEnabled="{Binding ExtendClientAreaEnabled}">

<CheckBox Content="Title Bar"
IsChecked="{Binding SystemTitleBarEnabled}"
DockPanel.Dock="Left" />

<Slider Minimum="-1"
Maximum="200"
Value="{Binding TitleBarHeight}"
IsEnabled="{Binding SystemTitleBarEnabled}"
Margin="8,-10" />

</DockPanel>

<CheckBox Content="Prefer System Chrome"
IsChecked="{Binding PreferSystemChromeEnabled}"
IsEnabled="{Binding ExtendClientAreaEnabled}" />

<CheckBox Content="Can Resize"
IsChecked="{Binding CanResize}" />

<CheckBox Content="Can Minimize"
IsChecked="{Binding CanMinimize}" />

<CheckBox Content="Can Maximize"
IsChecked="{Binding CanMaximize}"
IsEnabled="{Binding CanResize}" />

</StackPanel>

<StackPanel Spacing="10" Margin="25" IsEnabled="{OnFormFactor false, Mobile=true}">
<TextBlock Classes="h2" Text="Mobile properties" Margin="4" />
<CheckBox Content="Is System Bar Visible" IsChecked="{Binding IsSystemBarVisible}" />
<CheckBox Content="Display Edge To Edge" IsChecked="{Binding DisplayEdgeToEdge}" />
<TextBlock Text="{Binding SafeAreaPadding, StringFormat='Safe Area Padding: {0}'}" />
</StackPanel>

</StackPanel>
</UserControl>
64 changes: 50 additions & 14 deletions samples/ControlCatalog/ViewModels/MainWindowViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using Avalonia.Controls;
using Avalonia.Controls.ApplicationLifetimes;
using Avalonia.Controls.Notifications;
using Avalonia.Dialogs;
using Avalonia.Platform;
using System;
Expand All @@ -22,6 +21,9 @@ class MainWindowViewModel : ViewModelBase
private bool _isSystemBarVisible;
private bool _displayEdgeToEdge;
private Thickness _safeAreaPadding;
private bool _canResize;
private bool _canMinimize;
private bool _canMaximize;

public MainWindowViewModel()
{
Expand Down Expand Up @@ -49,7 +51,7 @@ public MainWindowViewModel()
WindowState.FullScreen,
};

this.PropertyChanged += (s, e) =>
PropertyChanged += (s, e) =>
{
if (e.PropertyName is nameof(SystemTitleBarEnabled) or nameof(PreferSystemChromeEnabled))
{
Expand All @@ -67,70 +69,104 @@ public MainWindowViewModel()
}
};

SystemTitleBarEnabled = true;
SystemTitleBarEnabled = true;
TitleBarHeight = -1;
CanResize = true;
CanMinimize = true;
CanMaximize = true;
}

public ExtendClientAreaChromeHints ChromeHints
{
get { return _chromeHints; }
set { this.RaiseAndSetIfChanged(ref _chromeHints, value); }
set { RaiseAndSetIfChanged(ref _chromeHints, value); }
}

public bool ExtendClientAreaEnabled
{
get { return _extendClientAreaEnabled; }
set { this.RaiseAndSetIfChanged(ref _extendClientAreaEnabled, value); }
set
{
if (RaiseAndSetIfChanged(ref _extendClientAreaEnabled, value) && !value)
{
SystemTitleBarEnabled = true;
}
}
}

public bool SystemTitleBarEnabled
{
get { return _systemTitleBarEnabled; }
set { this.RaiseAndSetIfChanged(ref _systemTitleBarEnabled, value); }
set
{
if (RaiseAndSetIfChanged(ref _systemTitleBarEnabled, value) && !value)
{
TitleBarHeight = -1;
}
}
}

public bool PreferSystemChromeEnabled
{
get { return _preferSystemChromeEnabled; }
set { this.RaiseAndSetIfChanged(ref _preferSystemChromeEnabled, value); }
set { RaiseAndSetIfChanged(ref _preferSystemChromeEnabled, value); }
}

public double TitleBarHeight
{
get { return _titleBarHeight; }
set { this.RaiseAndSetIfChanged(ref _titleBarHeight, value); }
set { RaiseAndSetIfChanged(ref _titleBarHeight, value); }
}

public WindowState WindowState
{
get { return _windowState; }
set { this.RaiseAndSetIfChanged(ref _windowState, value); }
set { RaiseAndSetIfChanged(ref _windowState, value); }
}

public WindowState[] WindowStates
{
get { return _windowStates; }
set { this.RaiseAndSetIfChanged(ref _windowStates, value); }
set { RaiseAndSetIfChanged(ref _windowStates, value); }
}

public bool IsSystemBarVisible
{
get { return _isSystemBarVisible; }
set { this.RaiseAndSetIfChanged(ref _isSystemBarVisible, value); }
set { RaiseAndSetIfChanged(ref _isSystemBarVisible, value); }
}

public bool DisplayEdgeToEdge
{
get { return _displayEdgeToEdge; }
set { this.RaiseAndSetIfChanged(ref _displayEdgeToEdge, value); }
set { RaiseAndSetIfChanged(ref _displayEdgeToEdge, value); }
}

public Thickness SafeAreaPadding
{
get { return _safeAreaPadding; }
set { this.RaiseAndSetIfChanged(ref _safeAreaPadding, value); }
set { RaiseAndSetIfChanged(ref _safeAreaPadding, value); }
}

public bool CanResize
{
get { return _canResize; }
set { RaiseAndSetIfChanged(ref _canResize, value); }
}

public bool CanMinimize
{
get { return _canMinimize; }
set { RaiseAndSetIfChanged(ref _canMinimize, value); }
}

public bool CanMaximize
{
get { return _canMaximize; }
set { RaiseAndSetIfChanged(ref _canMaximize, value); }
}


public MiniCommand AboutCommand { get; }

public MiniCommand ExitCommand { get; }
Expand All @@ -144,7 +180,7 @@ public Thickness SafeAreaPadding
public DateTime? ValidatedDateExample
{
get => _validatedDateExample;
set => this.RaiseAndSetIfChanged(ref _validatedDateExample, value);
set => RaiseAndSetIfChanged(ref _validatedDateExample, value);
}
}
}
Loading