tstephansen / MacCatalystWindowHelper

A macOS library that can be used to make MacCatalyst windows transparent.
1 stars 0 forks source link

How can I modify the Swift project and have the changes reflect in MAUI #1

Open danieltharris opened 4 months ago

danieltharris commented 4 months ago

This looks like exactly what I need for an upcoming project, and we've been struggling to get transparent windows in MAUI for Mac Catalyst.

The sample runs up fine with a red transparent window, however when we've tried to modify the Xcode project (for example to change the background colour to blue), built it, and then copied over the build output to the sample MAUI project it still shows with a red background.

Feels like we're missing something, using Swift libraries with MAUI is not something we've needed to do before now, so it's likely something simple.

tstephansen commented 4 months ago

I'm sorry. I'm not really following. Are you talking about the windows's background color or the title bar? If you're talking about the background of the window you can just set the color to what you want it to be in the xaml file. For example, in the MauiApp1 project change the SecondPage.xaml file to below and you will get a transparent window with a red background.

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="MauiApp1.SecondPage"
             Background="Transparent"
             Title="SecondPage">
    <Grid>
        <Rectangle Fill="#7DFF0000" HorizontalOptions="Fill" VerticalOptions="Fill"/>
    </Grid>
</ContentPage>

The result would look like this. Screenshot 2024-04-26 at 13 41 25@2x

danieltharris commented 4 months ago

I didn't realise we could make the window transparent in MAUI to be honest - Is that something recent (as your original repo using the dylib from Xcode seemed to be trying to achieve what is in your reply above?)

What I'm actually trying to do is use the native blur effects for my Mac app - So I came across your repo here and saw you're using the dylib to make windows transparent, which looks like something your approach can help me achieve. This is where I got it to so far:

image

The issue I was having was that I pulled down this repo and wanted to build the Xcode project with some changes to it, but when I copied over my version the MAUI build was failing due to missing symbols for ARM and x64 - Turned out to be my build config in Xcode so I'm passed that issue now.

The issue I'm having, which I'm not sure if you also had is:

The MAUI app is trying to load the dylib file from:

/usr/local/lib/mylibraryname.dylib

Currently I have to manually copy it to this location - if it's not there the MAUI app crashes immediately upon running.

tstephansen commented 4 months ago

I had this issue with another native library we are using (basically a C lib that was originally compiled for windows) that we recompiled for mac. I ended up adding it as a NativeReference (like the transparent window dylib) and using the internal DllImport. Try adding it as a NativeReference in your csproj file and using the DllImport below. I'm don't know if this will fix your issue or not but hopefully it helps.

<ItemGroup>
    <NativeReference Include="Libs\mylibraryname.dylib" Kind="Dynamic" />
</ItemGroup>
[DllImport("__Internal")]
private static extern void doSomething();

Also, for that one we recompiled, we still used the calling convention and entry point on the DllImport attribute.

[DllImport("__Internal", CallingConvention = CallingConvention.Cdecl, EntryPoint = "freeData")]
private static extern void freeData(ref IntPtr buf);
danieltharris commented 4 months ago

Thanks for your help, I'm going to run the steps a few more times as I need to do this into a permanent project anyway but I seem to have got it working.

Before I saw your reply I created a new Xcode project from scratch - it initially had the same issue, but I changed the build setting for Installation Directory to the below:

image

This version seems to load fine without needing to be within the /usr/local/lib folder once I made that change - This doesn't 100% make sense to me because the executable at runtime doesn't actually have a /Lib folder, but it did seem to get it working....so I'll just see how it goes as it's enabled me to get the native blur effects applying to the window, something I have struggled to get working for some time in MAUI previously.

When inspecting the built dylib using otool -L libmylibraryname.dylib it also shows as having taken effect (previously this showed the /usr/lib path at the top:

image

This is how my csproj, DllImport look, which I think matches your original repo:

<ItemGroup>
    <NativeReference Include="Libs\libWindowUtilsMAUI.dylib" Kind="Dynamic" />
</ItemGroup>
[DllImport("__Internal")]
private static extern void makeWindowsTransparent();

[DllImport("__Internal")]
private static extern void showAlert();