Skip to content

Commit

Permalink
Mac: Add ability to auto attach/detach a control
Browse files Browse the repository at this point in the history
  • Loading branch information
cwensley committed Jul 14, 2023
1 parent 2b141d2 commit feaa4e6
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
26 changes: 26 additions & 0 deletions src/Eto.Mac/Forms/MacView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ static partial class MacView
public static readonly object AcceptsFirstMouse_Key = new object();
public static readonly object TextInputCancelled_Key = new object();
public static readonly object TextInputImplemented_Key = new object();
public static readonly object AutoAttachNative_Key = new object();
public static readonly IntPtr selMouseDown = Selector.GetHandle("mouseDown:");
public static readonly IntPtr selMouseUp = Selector.GetHandle("mouseUp:");
public static readonly IntPtr selMouseDragged = Selector.GetHandle("mouseDragged:");
Expand Down Expand Up @@ -1575,6 +1576,31 @@ public virtual void UpdateLayout()
{
ContainerControl?.Window?.LayoutIfNeeded();
}

public bool AutoAttachNative
{
get => Widget.Properties.Get<bool>(MacView.AutoAttachNative_Key);
set
{
if (Widget.Properties.TrySet(MacView.AutoAttachNative_Key, value) && value)
{
// ensure method is added to the container control's class
AddMethod(MacView.selViewDidMoveToWindow, MacView.TriggerViewDidMoveToWindow_Delegate, "v@:@", ContainerControl);

Check failure on line 1588 in src/Eto.Mac/Forms/MacView.cs

View workflow job for this annotation

GitHub Actions / build-windows

'MacView' does not contain a definition for 'selViewDidMoveToWindow'

Check failure on line 1588 in src/Eto.Mac/Forms/MacView.cs

View workflow job for this annotation

GitHub Actions / build-windows

'MacView' does not contain a definition for 'TriggerViewDidMoveToWindow_Delegate'

Check failure on line 1588 in src/Eto.Mac/Forms/MacView.cs

View workflow job for this annotation

GitHub Actions / build-mac

'MacView' does not contain a definition for 'selViewDidMoveToWindow'

Check failure on line 1588 in src/Eto.Mac/Forms/MacView.cs

View workflow job for this annotation

GitHub Actions / build-mac

'MacView' does not contain a definition for 'TriggerViewDidMoveToWindow_Delegate'

Check failure on line 1588 in src/Eto.Mac/Forms/MacView.cs

View workflow job for this annotation

GitHub Actions / build-mac

'MacView' does not contain a definition for 'selViewDidMoveToWindow'

Check failure on line 1588 in src/Eto.Mac/Forms/MacView.cs

View workflow job for this annotation

GitHub Actions / build-mac

'MacView' does not contain a definition for 'TriggerViewDidMoveToWindow_Delegate'

Check failure on line 1588 in src/Eto.Mac/Forms/MacView.cs

View workflow job for this annotation

GitHub Actions / build-mac

'MacView' does not contain a definition for 'selViewDidMoveToWindow'

Check failure on line 1588 in src/Eto.Mac/Forms/MacView.cs

View workflow job for this annotation

GitHub Actions / build-mac

'MacView' does not contain a definition for 'TriggerViewDidMoveToWindow_Delegate'
}
}
}

public virtual void OnViewDidMoveToWindow()
{
if (!AutoAttachNative)
return;

// ensure load/unload get called appropriately.
if (ContainerControl.Window == null)
Widget.DetachNative();
else
Widget.AttachNative();
}
}
}

16 changes: 16 additions & 0 deletions src/Eto.Mac/MacHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,22 @@ public static NSView ToNative(this Control control, bool attach = false)
}
return control.GetContainerView();
}

/// <summary>
/// Sets a value indicating that the control should auto attach/detach when added to a native window.
/// </summary>
/// <remarks>
/// This is an alternative to using AttachNative/DetachNative manually, and will automatically be called when the view
/// is added/removed to/from a native Window. This ensures that both Load and UnLoad are triggered on the Eto control(s).
/// </remarks>
/// <param name="control">Control to auto attach</param>
/// <param name="autoAttach"><c>true</c> to auto attach, <c>false</c> otherwise.</param>
public static void SetAutoAttach(this Control control, bool autoAttach)
{
var handler = control.GetMacViewHandler();
if (handler != null)
handler.AutoAttachNative = true;

Check failure on line 57 in src/Eto.Mac/MacHelpers.cs

View workflow job for this annotation

GitHub Actions / build-windows

'IMacViewHandler' does not contain a definition for 'AutoAttachNative' and no accessible extension method 'AutoAttachNative' accepting a first argument of type 'IMacViewHandler' could be found (are you missing a using directive or an assembly reference?)

Check failure on line 57 in src/Eto.Mac/MacHelpers.cs

View workflow job for this annotation

GitHub Actions / build-mac

'IMacViewHandler' does not contain a definition for 'AutoAttachNative' and no accessible extension method 'AutoAttachNative' accepting a first argument of type 'IMacViewHandler' could be found (are you missing a using directive or an assembly reference?)

Check failure on line 57 in src/Eto.Mac/MacHelpers.cs

View workflow job for this annotation

GitHub Actions / build-mac

'IMacViewHandler' does not contain a definition for 'AutoAttachNative' and no accessible extension method 'AutoAttachNative' accepting a first argument of type 'IMacViewHandler' could be found (are you missing a using directive or an assembly reference?)

Check failure on line 57 in src/Eto.Mac/MacHelpers.cs

View workflow job for this annotation

GitHub Actions / build-mac

'IMacViewHandler' does not contain a definition for 'AutoAttachNative' and no accessible extension method 'AutoAttachNative' accepting a first argument of type 'IMacViewHandler' could be found (are you missing a using directive or an assembly reference?)

Check failure on line 57 in src/Eto.Mac/MacHelpers.cs

View workflow job for this annotation

GitHub Actions / build-mac

'IMacViewHandler' does not contain a definition for 'AutoAttachNative' and no accessible extension method 'AutoAttachNative' accepting a first argument of type 'IMacViewHandler' could be found (are you missing a using directive or an assembly reference?)
}

/// <summary>
/// Wraps the specified <paramref name="view"/> to an Eto control that can be used directly in Eto.Forms code.
Expand Down

0 comments on commit feaa4e6

Please sign in to comment.