Skip to content

Commit

Permalink
Update to Avalonia@e394ca59 (Jul 27 2023) (#38)
Browse files Browse the repository at this point in the history
  • Loading branch information
jpobst committed Jul 28, 2023
1 parent 3d79cee commit 687be3b
Show file tree
Hide file tree
Showing 20 changed files with 145 additions and 76 deletions.
10 changes: 8 additions & 2 deletions src/Modern.WindowKit/Avalonia.Mac/AvaloniaNativePlatform.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@
using Modern.WindowKit.Controls.Platform;
using Modern.WindowKit.Input;
using Modern.WindowKit.Input.Platform;
using Modern.WindowKit.MicroCom;
using Modern.WindowKit.Mac.Interop;
//using Modern.WindowKit.OpenGL;
using Modern.WindowKit.Platform;
//using Modern.WindowKit.Rendering;
//using Modern.WindowKit.Rendering.Composition;
Expand Down Expand Up @@ -163,6 +161,14 @@ void DoInitialize(AvaloniaNativePlatformOptions options)


//Compositor = new Compositor(_platformGraphics, true);

AppDomain.CurrentDomain.ProcessExit += OnProcessExit;
}

private void OnProcessExit(object? sender, EventArgs e)
{
AppDomain.CurrentDomain.ProcessExit -= OnProcessExit;
_factory.Dispose();
}

//public ITrayIconImpl CreateTrayIcon()
Expand Down
3 changes: 2 additions & 1 deletion src/Modern.WindowKit/Avalonia.Win32/IBlurHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ internal enum BlurEffect
{
None,
Acrylic,
Mica
MicaLight,
MicaDark
}

internal interface IBlurHost
Expand Down
11 changes: 7 additions & 4 deletions src/Modern.WindowKit/Avalonia.Win32/WinUiCompositionShared.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ internal class WinUiCompositionShared : IDisposable
//public ICompositor5 Compositor5 { get; }
//public ICompositorDesktopInterop DesktopInterop { get; }
//public ICompositionBrush BlurBrush { get; }
//public ICompositionBrush? MicaBrush { get; }
//public object SyncRoot { get; } = new();
//public ICompositionBrush? MicaBrushLight { get; }
//public ICompositionBrush? MicaBrushDark { get; }
public object SyncRoot { get; } = new();

public static readonly Version MinWinCompositionVersion = new(10, 0, 17134);
public static readonly Version MinAcrylicVersion = new(10, 0, 15063);
Expand All @@ -21,14 +22,16 @@ internal class WinUiCompositionShared : IDisposable
// Compositor = compositor.CloneReference();
// Compositor5 = compositor.QueryInterface<ICompositor5>();
// BlurBrush = WinUiCompositionUtils.CreateAcrylicBlurBackdropBrush(compositor);
// MicaBrush = WinUiCompositionUtils.CreateMicaBackdropBrush(compositor);
// MicaBrushLight = WinUiCompositionUtils.CreateMicaBackdropBrush(compositor, 242, 0.6f);
// MicaBrushDark = WinUiCompositionUtils.CreateMicaBackdropBrush(compositor, 32, 0.8f);
// DesktopInterop = compositor.QueryInterface<ICompositorDesktopInterop>();
//}

public void Dispose()
{
//BlurBrush.Dispose();
//MicaBrush?.Dispose();
//MicaBrushLight?.Dispose();
//MicaBrushDark?.Dispose();
//DesktopInterop.Dispose();
//Compositor.Dispose();
//Compositor5.Dispose();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ protected virtual unsafe IntPtr AppWndProc(IntPtr hWnd, uint msg, IntPtr wParam,

case WindowsMessage.WM_NCCALCSIZE:
{
if (ToInt32(wParam) == 1 && !HasFullDecorations || _isClientAreaExtended)
if (ToInt32(wParam) == 1 && _windowProperties.Decorations == SystemDecorations.None || _isClientAreaExtended)
{
return IntPtr.Zero;
}
Expand Down
23 changes: 16 additions & 7 deletions src/Modern.WindowKit/Avalonia.Win32/WindowImpl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ internal partial class WindowImpl : IWindowImpl //, EglGlPlatformSurface.IEglWin
//private static POINTER_PEN_INFO[]? s_historyPenInfos;
//private static POINTER_INFO[]? s_historyInfos;
//private static MOUSEMOVEPOINT[]? s_mouseHistoryInfos;
private PlatformThemeVariant _currentThemeVariant;

public WindowImpl()
{
Expand Down Expand Up @@ -474,7 +475,12 @@ private void SetTransparencyMica(Version windowsVersion)
return;

SetUseHostBackdropBrush(false);
_blurHost?.SetBlur(BlurEffect.Mica);
_blurHost?.SetBlur(_currentThemeVariant switch
{
PlatformThemeVariant.Light => BlurEffect.MicaLight,
PlatformThemeVariant.Dark => BlurEffect.MicaDark,
_ => throw new ArgumentOutOfRangeException()
});
}

private void SetAccentState(AccentState state)
Expand Down Expand Up @@ -778,6 +784,7 @@ public void SetTopmost(bool value)

public unsafe void SetFrameThemeVariant(PlatformThemeVariant themeVariant)
{
_currentThemeVariant = themeVariant;
if (Win32Platform.WindowsVersion.Build >= 22000)
{
var pvUseBackdropBrush = themeVariant == PlatformThemeVariant.Dark ? 1 : 0;
Expand All @@ -786,6 +793,10 @@ public unsafe void SetFrameThemeVariant(PlatformThemeVariant themeVariant)
(int)DwmWindowAttribute.DWMWA_USE_IMMERSIVE_DARK_MODE,
&pvUseBackdropBrush,
sizeof(int));
if (TransparencyLevel == WindowTransparencyLevel.Mica)
{
SetTransparencyMica(Win32Platform.WindowsVersion);
}
}
}

Expand Down Expand Up @@ -1329,14 +1340,12 @@ private void UpdateWindowProperties(WindowProperties newProperties, bool forceCh

if (!_isFullScreenActive)
{
var margin = newProperties.Decorations == SystemDecorations.BorderOnly ? 1 : 0;

var margins = new MARGINS
{
cyBottomHeight = margin,
cxRightWidth = margin,
cxLeftWidth = margin,
cyTopHeight = margin
cyBottomHeight = 0,
cxRightWidth = 0,
cxLeftWidth = 0,
cyTopHeight = 0
};

DwmExtendFrameIntoClientArea(_hwnd, ref margins);
Expand Down
54 changes: 36 additions & 18 deletions src/Modern.WindowKit/Avalonia.X11/X11Clipboard.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ internal class X11Clipboard : IClipboard
private readonly X11Info _x11;
private IDataObject _storedDataObject;
private IntPtr _handle;
private TaskCompletionSource<bool> _storeAtomTcs;
private TaskCompletionSource<IntPtr[]> _requestedFormatsTcs;
private TaskCompletionSource<object> _requestedDataTcs;
private readonly IntPtr[] _textAtoms;
Expand Down Expand Up @@ -52,6 +53,12 @@ private Encoding GetStringEncoding(IntPtr atom)

private unsafe void OnEvent(ref XEvent ev)
{
if (ev.type == XEventName.SelectionClear)
{
_storeAtomTcs?.TrySetResult(true);
return;
}

if (ev.type == XEventName.SelectionRequest)
{
var sel = ev.SelectionRequestEvent;
Expand Down Expand Up @@ -82,18 +89,9 @@ IntPtr WriteTargetToProperty(IntPtr target, IntPtr window, IntPtr property)
Encoding textEnc;
if (target == _x11.Atoms.TARGETS)
{
var atoms = new HashSet<IntPtr> { _x11.Atoms.TARGETS, _x11.Atoms.MULTIPLE };
foreach (var fmt in _storedDataObject.GetDataFormats())
{
if (fmt == DataFormats.Text)
foreach (var ta in _textAtoms)
atoms.Add(ta);
else
atoms.Add(_x11.Atoms.GetAtom(fmt));
}

var atoms = ConvertDataObject(_storedDataObject);
XChangeProperty(_x11.Display, window, property,
_x11.Atoms.XA_ATOM, 32, PropertyMode.Replace, atoms.ToArray(), atoms.Count);
_x11.Atoms.XA_ATOM, 32, PropertyMode.Replace, atoms, atoms.Length);
return property;
}
else if(target == _x11.Atoms.SAVE_TARGETS && _x11.Atoms.SAVE_TARGETS != IntPtr.Zero)
Expand Down Expand Up @@ -252,21 +250,42 @@ public async Task<string> GetTextAsync()
return (string)await SendDataRequest(target);
}

private void StoreAtomsInClipboardManager(IntPtr[] atoms)

private IntPtr[] ConvertDataObject(IDataObject data)
{
if (_x11.Atoms.CLIPBOARD_MANAGER != IntPtr.Zero && _x11.Atoms.SAVE_TARGETS != IntPtr.Zero)
var atoms = new HashSet<IntPtr> { _x11.Atoms.TARGETS, _x11.Atoms.MULTIPLE };
foreach (var fmt in data.GetDataFormats())
{
if (fmt == DataFormats.Text)
foreach (var ta in _textAtoms)
atoms.Add(ta);
else
atoms.Add(_x11.Atoms.GetAtom(fmt));
}
return atoms.ToArray();
}

private Task StoreAtomsInClipboardManager(IDataObject data)
{
if (_x11.Atoms.CLIPBOARD_MANAGER != IntPtr.Zero && _x11.Atoms.SAVE_TARGETS != IntPtr.Zero)
{
var clipboardManager = XGetSelectionOwner(_x11.Display, _x11.Atoms.CLIPBOARD_MANAGER);
if (clipboardManager != IntPtr.Zero)
{
{
if (_storeAtomTcs == null || _storeAtomTcs.Task.IsCompleted)
_storeAtomTcs = new TaskCompletionSource<bool>();

var atoms = ConvertDataObject(data);
XChangeProperty(_x11.Display, _handle, _avaloniaSaveTargetsAtom, _x11.Atoms.XA_ATOM, 32,
PropertyMode.Replace,
atoms, atoms.Length);
XConvertSelection(_x11.Display, _x11.Atoms.CLIPBOARD_MANAGER, _x11.Atoms.SAVE_TARGETS,
_avaloniaSaveTargetsAtom, _handle, IntPtr.Zero);
return _storeAtomTcs.Task;
}
}
}
return Task.CompletedTask;
}

public Task SetTextAsync(string text)
{
Expand All @@ -283,9 +302,8 @@ public Task ClearAsync()
public Task SetDataObjectAsync(IDataObject data)
{
_storedDataObject = data;
XSetSelectionOwner(_x11.Display, _x11.Atoms.CLIPBOARD, _handle, IntPtr.Zero);
StoreAtomsInClipboardManager(_textAtoms);
return Task.CompletedTask;
XSetSelectionOwner(_x11.Display, _x11.Atoms.CLIPBOARD, _handle, IntPtr.Zero);
return StoreAtomsInClipboardManager(data);
}

public async Task<string[]> GetFormatsAsync()
Expand Down
16 changes: 15 additions & 1 deletion src/Modern.WindowKit/Avalonia.X11/X11Info.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ internal unsafe class X11Info
public bool HasXSync { get; set; }
public IntPtr DefaultFontSet { get; set; }

[DllImport("libc")]
private static extern void setlocale(int type, string s);

public unsafe X11Info(IntPtr display, IntPtr deferredDisplay, bool useXim)
{
Display = display;
Expand All @@ -49,6 +52,9 @@ public unsafe X11Info(IntPtr display, IntPtr deferredDisplay, bool useXim)
DefaultFontSet = XCreateFontSet(Display, "-*-*-*-*-*-*-*-*-*-*-*-*-*-*",
out var _, out var _, IntPtr.Zero);

// We have problems with text input otherwise
setlocale(0, "");

if (useXim)
{
XSetLocaleModifiers("");
Expand All @@ -59,7 +65,15 @@ public unsafe X11Info(IntPtr display, IntPtr deferredDisplay, bool useXim)

if (Xim == IntPtr.Zero)
{
XSetLocaleModifiers("@im=none");
if (XSetLocaleModifiers("@im=none") == IntPtr.Zero)
{
setlocale(0, "en_US.UTF-8");
if (XSetLocaleModifiers("@im=none") == IntPtr.Zero)
{
setlocale(0, "C.UTF-8");
XSetLocaleModifiers("@im=none");
}
}
Xim = XOpenIM(display, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero);
}

Expand Down
6 changes: 1 addition & 5 deletions src/Modern.WindowKit/Avalonia.X11/X11Platform.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,7 @@ internal class AvaloniaX11Platform : IWindowingPlatform
public IntPtr OrphanedWindow { get; private set; }
public X11Globals Globals { get; private set; }
public ManualRawEventGrouperDispatchQueue EventGrouperDispatchQueue { get; } = new();
[DllImport("libc")]
private static extern void setlocale(int type, string s);

public void Initialize(X11PlatformOptions options)
{
Options = options;
Expand All @@ -50,9 +49,6 @@ public void Initialize(X11PlatformOptions options)
// useXim = true;
//}

// We have problems with text input otherwise
setlocale(0, "");

XInitThreads();
Display = XOpenDisplay(IntPtr.Zero);
if (Display == IntPtr.Zero)
Expand Down
5 changes: 2 additions & 3 deletions src/Modern.WindowKit/Avalonia.X11/X11Structs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1109,7 +1109,7 @@ internal struct MotifWmHints {

public override string ToString ()
{
return string.Format("MotifWmHints <flags={0}, functions={1}, decorations={2}, input_mode={3}, status={4}", (MotifFlags) flags.ToInt32 (), (MotifFunctions) functions.ToInt32 (), (MotifDecorations) decorations.ToInt32 (), (MotifInputMode) input_mode.ToInt32 (), status.ToInt32 ());
return $"MotifWmHints <flags={(MotifFlags)flags.ToInt32()}, functions={(MotifFunctions)functions.ToInt32()}, decorations={(MotifDecorations)decorations.ToInt32()}, input_mode={(MotifInputMode)input_mode.ToInt32()}, status={status.ToInt32()}";
}
}

Expand Down Expand Up @@ -1707,8 +1707,7 @@ internal struct XcursorImage

public override string ToString ()
{
return string.Format ("XCursorImage (version: {0}, size: {1}, width: {2}, height: {3}, xhot: {4}, yhot: {5}, delay: {6}, pixels: {7}",
version, size, width, height, xhot, yhot, delay, pixels);
return $"XCursorImage (version: {version}, size: {size}, width: {width}, height: {height}, xhot: {xhot}, yhot: {yhot}, delay: {delay}, pixels: {pixels}";
}
} ;

Expand Down
13 changes: 10 additions & 3 deletions src/Modern.WindowKit/Avalonia.X11/X11Window.Ime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -134,13 +134,20 @@ private unsafe string TranslateEventToString(ref XEvent ev)
if (ImeBuffer == IntPtr.Zero)
ImeBuffer = Marshal.AllocHGlobal(ImeBufferSize);

var len = Xutf8LookupString(_xic, ref ev, ImeBuffer.ToPointer(), ImeBufferSize,
out _, out var istatus);
var status = (XLookupStatus)istatus;
IntPtr istatus;
int len;
if(_xic != IntPtr.Zero)
len = Xutf8LookupString(_xic, ref ev, ImeBuffer.ToPointer(),
ImeBufferSize, out _, out istatus);
else
len = XLookupString(ref ev, ImeBuffer.ToPointer(), ImeBufferSize,
out _, out istatus);

if (len == 0)
return null;

var status = (XLookupStatus)istatus;

string text;
if (status == XLookupStatus.XBufferOverflow)
return null;
Expand Down
2 changes: 1 addition & 1 deletion src/Modern.WindowKit/Avalonia.X11/XLib.cs
Original file line number Diff line number Diff line change
Expand Up @@ -468,7 +468,7 @@ public enum XLookupStatus : uint
public static extern unsafe int XLookupString(ref XEvent xevent, void* buffer, int num_bytes, out IntPtr keysym, out IntPtr status);

[DllImport (libX11)]
public static extern unsafe int Xutf8LookupString(IntPtr xic, ref XEvent xevent, void* buffer, int num_bytes, out IntPtr keysym, out UIntPtr status);
public static extern unsafe int Xutf8LookupString(IntPtr xic, ref XEvent xevent, void* buffer, int num_bytes, out IntPtr keysym, out IntPtr status);

[DllImport (libX11)]
public static extern unsafe int Xutf8LookupString(IntPtr xic, XEvent* xevent, void* buffer, int num_bytes, out IntPtr keysym, out IntPtr status);
Expand Down
7 changes: 4 additions & 3 deletions src/Modern.WindowKit/AvaloniaSynchronizationContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,14 +99,15 @@ public void Dispose()
}
}

public static RestoreContext Ensure(DispatcherPriority priority)
public static RestoreContext Ensure(DispatcherPriority priority) => Ensure(Dispatcher.UIThread, priority);
public static RestoreContext Ensure(Dispatcher dispatcher, DispatcherPriority priority)
{
if (Current is AvaloniaSynchronizationContext avaloniaContext
&& avaloniaContext.Priority == priority)
return default;
var oldContext = Current;
Dispatcher.UIThread.VerifyAccess();
SetSynchronizationContext(Dispatcher.UIThread.GetContextWithPriority(priority));
dispatcher.VerifyAccess();
SetSynchronizationContext(dispatcher.GetContextWithPriority(priority));
return new RestoreContext(oldContext);
}
}
Expand Down
Loading

0 comments on commit 687be3b

Please sign in to comment.