Skip to content

Commit

Permalink
Merge pull request #11 from AvaloniaUI/features/screens-implementation
Browse files Browse the repository at this point in the history
Screen API
  • Loading branch information
danwalmsley authored Sep 24, 2018
2 parents e42326d + 40481ac commit 379b37f
Show file tree
Hide file tree
Showing 12 changed files with 132 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
objects = {

/* Begin PBXBuildFile section */
37A517B32159597E00FBA241 /* Screens.mm in Sources */ = {isa = PBXBuildFile; fileRef = 37A517B22159597E00FBA241 /* Screens.mm */; };
37C09D8821580FE4006A6758 /* SystemDialogs.mm in Sources */ = {isa = PBXBuildFile; fileRef = 37C09D8721580FE4006A6758 /* SystemDialogs.mm */; };
AB00E4F72147CA920032A60A /* main.mm in Sources */ = {isa = PBXBuildFile; fileRef = AB00E4F62147CA920032A60A /* main.mm */; };
AB661C1E2148230F00291242 /* AppKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = AB661C1D2148230F00291242 /* AppKit.framework */; };
Expand All @@ -16,6 +17,7 @@

/* Begin PBXFileReference section */
379A4506214D0F6500CC143D /* headers */ = {isa = PBXFileReference; lastKnownFileType = folder; name = headers; path = ../headers; sourceTree = "<group>"; };
37A517B22159597E00FBA241 /* Screens.mm */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.objcpp; path = Screens.mm; sourceTree = "<group>"; };
37C09D8721580FE4006A6758 /* SystemDialogs.mm */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.objcpp; path = SystemDialogs.mm; sourceTree = "<group>"; };
37C09D8A21581EF2006A6758 /* window.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = window.h; sourceTree = "<group>"; };
AB00E4F62147CA920032A60A /* main.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = main.mm; sourceTree = "<group>"; };
Expand Down Expand Up @@ -55,6 +57,7 @@
AB661C1F2148286E00291242 /* window.mm */,
37C09D8A21581EF2006A6758 /* window.h */,
AB00E4F62147CA920032A60A /* main.mm */,
37A517B22159597E00FBA241 /* Screens.mm */,
37C09D8721580FE4006A6758 /* SystemDialogs.mm */,
AB7A61F02147C815003C5833 /* Products */,
AB661C1C2148230E00291242 /* Frameworks */,
Expand Down Expand Up @@ -137,6 +140,7 @@
buildActionMask = 2147483647;
files = (
AB8F7D6B21482D7F0057DBA5 /* platformthreading.mm in Sources */,
37A517B32159597E00FBA241 /* Screens.mm in Sources */,
AB00E4F72147CA920032A60A /* main.mm in Sources */,
37C09D8821580FE4006A6758 /* SystemDialogs.mm in Sources */,
AB661C202148286E00291242 /* window.mm in Sources */,
Expand Down
42 changes: 42 additions & 0 deletions src/Avalonia.Native.OSX/Screens.mm
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#include "common.h"

class Screens : public ComSingleObject<IAvnScreens, &IID_IAvnScreens>
{
public:

virtual HRESULT GetScreenCount (int* ret)
{
*ret = (int)[NSScreen screens].count;

return S_OK;
}

virtual HRESULT GetScreen (int index, AvnScreen* ret)
{
if(index < 0 || index >= [NSScreen screens].count)
{
return E_INVALIDARG;
}

auto screen = [[NSScreen screens] objectAtIndex:index];

ret->Bounds.X = [screen frame].origin.x;
ret->Bounds.Y = [screen frame].origin.y;
ret->Bounds.Height = [screen frame].size.height;
ret->Bounds.Width = [screen frame].size.width;

ret->WorkingArea.X = [screen visibleFrame].origin.x;
ret->WorkingArea.Y = [screen visibleFrame].origin.y;
ret->WorkingArea.Height = [screen visibleFrame].size.height;
ret->WorkingArea.Width = [screen visibleFrame].size.width;

ret->Primary = index == 0;

return S_OK;
}
};

extern IAvnScreens* CreateScreens()
{
return new Screens();
}
1 change: 1 addition & 0 deletions src/Avalonia.Native.OSX/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ extern IAvnPlatformThreadingInterface* CreatePlatformThreading();
extern IAvnWindow* CreateAvnWindow(IAvnWindowEvents*events);
extern IAvnPopup* CreateAvnPopup(IAvnWindowEvents*events);
extern IAvnSystemDialogs* CreateSystemDialogs();
extern IAvnScreens* CreateScreens();

extern NSPoint ToNSPoint (AvnPoint p);
extern AvnPoint ToAvnPoint (NSPoint p);
Expand Down
6 changes: 6 additions & 0 deletions src/Avalonia.Native.OSX/main.mm
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,12 @@ virtual HRESULT CreateSystemDialogs(IAvnSystemDialogs** ppv)
*ppv = ::CreateSystemDialogs();
return S_OK;
}

virtual HRESULT CreateScreens (IAvnScreens** ppv)
{
*ppv = ::CreateScreens ();
return S_OK;
}
};

extern "C" IAvaloniaNativeFactory* CreateAvaloniaNative()
Expand Down
1 change: 0 additions & 1 deletion src/Avalonia.Native/AvaloniaNativePlatform.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@ void DoInitialize(Action<AvaloniaNativeOptions> configure)
.Bind<IMouseDevice>().ToConstant(MouseDevice)
.Bind<IPlatformSettings>().ToConstant(this)
.Bind<IWindowingPlatform>().ToConstant(this)
.Bind<ISystemDialogImpl>().ToSingleton<SystemDialogImpl>()
.Bind<IClipboard>().ToSingleton<ClipboardImpl>()
.Bind<IRenderLoop>().ToConstant(new RenderLoop())
.Bind<IRenderTimer>().ToConstant(new DefaultRenderTimer(60))
Expand Down
5 changes: 5 additions & 0 deletions src/Avalonia.Native/Helpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,10 @@ public static Size ToAvaloniaSize (this AvnSize size)
{
return new Size(size.Width, size.Height);
}

public static Rect ToAvaloniaRect (this AvnRect rect)
{
return new Rect(rect.X, rect.Y, rect.Width, rect.Height);
}
}
}
8 changes: 7 additions & 1 deletion src/Avalonia.Native/PopupImpl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,13 @@ public class PopupImpl : WindowBaseImpl, IPopupImpl
public PopupImpl(IAvaloniaNativeFactory factory)
{
using (var e = new PopupEvents(this))
Init(_native = factory.CreatePopup(e));
Init(_native = factory.CreatePopup(e), factory.CreateScreens());
}

public override void Dispose()
{
_native.Dispose();
base.Dispose();
}

class PopupEvents : WindowBaseEvents, IAvnWindowEvents
Expand Down
42 changes: 42 additions & 0 deletions src/Avalonia.Native/ScreenImpl.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using System;
using Avalonia.Native.Interop;
using Avalonia.Platform;

namespace Avalonia.Native
{
class ScreenImpl : IScreenImpl, IDisposable
{
private IAvnScreens _native;

public ScreenImpl(IAvnScreens native)
{
_native = native;
}

public int ScreenCount => _native.GetScreenCount();

public Screen[] AllScreens
{
get
{
var count = ScreenCount;
var result = new Screen[count];

for(int i = 0; i < count; i++)
{
var screen = _native.GetScreen(i);

result[i] = new Screen(screen.Bounds.ToAvaloniaRect(), screen.WorkingArea.ToAvaloniaRect(), screen.Primary);
}

return result;
}
}

public void Dispose ()
{
_native.Dispose();
_native = null;
}
}
}
22 changes: 1 addition & 21 deletions src/Avalonia.Native/Stubs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,7 @@
using Avalonia.Platform;

namespace Avalonia.Native
{
class SystemDialogImpl : ISystemDialogImpl
{
public Task<string[]> ShowFileDialogAsync(FileDialog dialog, IWindowImpl parent)
{
return Task.FromResult((string[])null);
}

public Task<string> ShowFolderDialogAsync(OpenFolderDialog dialog, IWindowImpl parent)
{
return Task.FromResult<string>(null);
}
}

{
class ClipboardImpl : IClipboard
{
public Task ClearAsync()
Expand All @@ -38,11 +25,4 @@ public Task SetTextAsync(string text)
return Task.CompletedTask;
}
}

class ScreenImpl : IScreenImpl
{
public int ScreenCount => 1;

public Screen[] AllScreens => new[] { new Screen(new Rect(0, 0, 1600, 900), new Rect(0, 0, 1600, 900), true) };
}
}
2 changes: 1 addition & 1 deletion src/Avalonia.Native/WindowImpl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class WindowImpl : WindowBaseImpl, IWindowImpl
public WindowImpl(IAvaloniaNativeFactory factory)
{
using (var e = new WindowEvents(this))
Init(_native = factory.CreateWindow(e));
Init(_native = factory.CreateWindow(e), factory.CreateScreens());
}

class WindowEvents : WindowBaseEvents, IAvnWindowEvents
Expand Down
10 changes: 7 additions & 3 deletions src/Avalonia.Native/WindowImplBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,11 @@ public WindowBaseImpl()
_mouse = AvaloniaLocator.Current.GetService<IMouseDevice>();
}

protected void Init(IAvnWindowBase window)
protected void Init(IAvnWindowBase window, IAvnScreens screens)
{
_native = window;

Screen = new ScreenImpl(screens);
_savedLogicalSize = ClientSize;
_savedScaling = Scaling;
}
Expand Down Expand Up @@ -183,11 +185,13 @@ public IRenderer CreateRenderer(IRenderRoot root)
return new ImmediateRenderer(root);
}

public void Dispose()
public virtual void Dispose()
{
_native.Close();
_native.Dispose();
_native = null;

(Screen as ScreenImpl)?.Dispose();
}


Expand Down Expand Up @@ -257,7 +261,7 @@ public void SetTopmost(bool value)
public IPlatformHandle Handle => new PlatformHandle(IntPtr.Zero, "NOT SUPPORTED");


public IScreenImpl Screen => new ScreenImpl();
public IScreenImpl Screen { get; private set; }

Action<double> ITopLevelImpl.ScalingChanged { get; set; }

Expand Down
16 changes: 16 additions & 0 deletions src/headers/avalonia-native.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ struct IAvnMacOptions;
struct IAvnPlatformThreadingInterface;
struct IAvnSystemDialogEvents;
struct IAvnSystemDialogs;
struct IAvnScreens;

struct AvnSize
{
Expand All @@ -30,6 +31,13 @@ struct AvnPoint
double X, Y;
};

struct AvnScreen
{
AvnRect Bounds;
AvnRect WorkingArea;
bool Primary;
};

enum AvnPixelFormat
{
kAvnRgb565,
Expand Down Expand Up @@ -82,6 +90,7 @@ AVNCOM(IAvaloniaNativeFactory, 01) : virtual IUnknown
virtual HRESULT CreatePopup (IAvnWindowEvents* cb, IAvnPopup** ppv) = 0;
virtual HRESULT CreatePlatformThreadingInterface(IAvnPlatformThreadingInterface** ppv) = 0;
virtual HRESULT CreateSystemDialogs (IAvnSystemDialogs** ppv) = 0;
virtual HRESULT CreateScreens (IAvnScreens** ppv) = 0;
};

AVNCOM(IAvnWindowBase, 02) : virtual IUnknown
Expand Down Expand Up @@ -196,4 +205,11 @@ AVNCOM(IAvnSystemDialogs, 0d) : virtual IUnknown
const char* filters) = 0;
};

AVNCOM(IAvnScreens, 0e) : virtual IUnknown
{
virtual HRESULT GetScreenCount (int* ret) = 0;
virtual HRESULT GetScreen (int index, AvnScreen* ret) = 0;

};

extern "C" IAvaloniaNativeFactory* CreateAvaloniaNative();

0 comments on commit 379b37f

Please sign in to comment.