dotnet / maui

.NET MAUI is the .NET Multi-platform App UI, a framework for building native device applications spanning mobile, tablet, and desktop.
https://dot.net/maui
MIT License
22.04k stars 1.73k forks source link

[Windows] The title bar is shown doubly if I set `Window.AppWindow.TitleBar.ExtendsContentIntoTitleBar` as false. #22894

Open Takym opened 3 months ago

Takym commented 3 months ago

Description

In the Windows platform, I observed the window tile bar was shown doubly when I set Window.AppWindow.TitleBar.ExtendsContentIntoTitleBar as false. I tried to set this property to false because I wanted to use the native title bar to show a title bar icon with a .ico file. In the current version, it is necessary to switch to the native title bar showing an icon.

Steps to Reproduce

Set Window.AppWindow.TitleBar.ExtendsContentIntoTitleBar as false like the below code:

builder.ConfigureLifecycleEvents(
    builder => builder.AddWindows(
        builder => builder.OnWindowCreated(window => {
            window.AppWindow.TitleBar.ExtendsContentIntoTitleBar = false;
        })
    )
);

Screenshot for Actual Behavior image

Screenshot for Expected Behavior image

Link to public reproduction project repository

https://github.com/Takym/DoublyTitleBarsInMaui/blob/master/DoublyTitleBarsInMaui/MauiProgram.cs#L31

Version with bug

8.0.21 SR4.1

Is this a regression from previous behavior?

Not sure, did not test other versions

Last version that worked well

Unknown/Other

Affected platforms

Windows

Affected platform versions

Microsoft Windows [Version 10.0.19045.4412]

Did you find any workaround?

I could hide the second title bar by overlapping it with the native container (FrameworkElement object). This code is to set the container margin to (0, -32, 0, 0) and the Z index to zero.

if (window.Content is FrameworkElement elem) {
    elem.Margin = new(0, -32, 0, 0);
    Canvas.SetZIndex(elem, 0);
}

I added this code below of window.AppWindow.TitleBar.ExtendsContentIntoTitleBar = false; line.

builder.ConfigureLifecycleEvents(
    builder => builder.AddWindows(
        builder => builder.OnWindowCreated(window => {
            window.AppWindow.TitleBar.ExtendsContentIntoTitleBar = false;

+           if (window.Content is FrameworkElement elem) {
+               elem.Margin = new(0, -32, 0, 0);
+               Canvas.SetZIndex(elem, 0);
+           }
        })
    )
);

Relevant log output

No response

github-actions[bot] commented 3 months ago

Hi I'm an AI powered bot that finds similar issues based off the issue title.

Please view the issues below to see if they solve your problem, and if the issue describes your problem please consider closing this one and thumbs upping the other issue to help us prioritize it. Thank you!

Open similar issues:

Closed similar issues:

Note: You can give me feedback by thumbs upping or thumbs downing this comment.

Zhanglirong-Winnie commented 3 months ago

Verified this issue with Visual Studio 17.11.0 Preview 1.1 (8.0.40 & 8.0.21). Can repro on Windows platform with sample project.

Takym commented 3 months ago

Memo

Takym commented 3 months ago

I am not sure adding a new API is a good solution to fix a bug but I suggest adding a new lifecycle event WindowsLifecycle.OnWindowCreating raised after var winuiWindow = new MauiWinUIWindow(); and before var mauiContext = applicationContext!.MakeWindowScope(winuiWindow, out var windowScope);. So, you can modify the Window.AppWindow.TitleBar.ExtendsContentIntoTitleBar property in this new event.

Alternatively, I suggest adding a new constructor public MauiWinUIWindow(bool extendsContentIntoTitleBar) and a new service interface like the one below:

public interface IDefaultExtendsContentIntoTitleBarValueProvider
{
    public bool ExtendsContentIntoTitleBar { get; }
}

// Utility Extension Methods for a Builder:
// - UseSystemTitleBar()
// - UseCustomTitleBar()

Yet another alternatively, I suggest making NavigationRootManager.SetTitleBarVisibility(bool) or WindowRootView.UpdateAppTitleBar(int, bool) public. So, you can refresh a tile bar state at any time like the below:

void ShowCustomTitleBar(Microsoft.UI.Xaml.Window window, NavigationRootManager navRootMgr)
{
    window.AppWindow.TitleBar.ExtendsContentIntoTitleBar = true;
    navRootMgr.SetTitleBarVisibility(true);
}

void HideCustomTitleBar(Microsoft.UI.Xaml.Window window, NavigationRootManager navRootMgr)
{
    window.AppWindow.TitleBar.ExtendsContentIntoTitleBar = false;
    navRootMgr.SetTitleBarVisibility(false);
}
void ShowCustomTitleBar(Microsoft.UI.Xaml.Window window, WindowRootView wndRootView)
{
    window.AppWindow.TitleBar.ExtendsContentIntoTitleBar = true;
    wndRootView.UpdateAppTitleBar(32, true);
}

void HideCustomTitleBar(Microsoft.UI.Xaml.Window window, WindowRootView wndRootView)
{
    window.AppWindow.TitleBar.ExtendsContentIntoTitleBar = false;
    wndRootView.UpdateAppTitleBar(0, false);
}
Takym commented 3 weeks ago

https://github.com/dotnet/maui/issues/22784#issuecomment-2344085236

I have tried #24266 (comment) however the title bar is still shown doubly. I tested with this commit on the reproduction project: Takym/DoublyTitleBarsInMaui@9145222.

(I am sorry for the late reply.)

P.S. Current OS Version: Microsoft Windows [Version 10.0.19045.4894]