Skip to content

Commit

Permalink
#40 output preferences: (preparatory refactoring)
Browse files Browse the repository at this point in the history
  • Loading branch information
chucker committed Mar 23, 2022
1 parent 5c037e5 commit cb2e54c
Show file tree
Hide file tree
Showing 20 changed files with 114 additions and 102 deletions.
2 changes: 2 additions & 0 deletions AltNetworkUtility.macOS/AltNetworkUtility.macOS.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,8 @@
<Compile Include="Repositories\NetworkInterfaceRepository\BsdDataSource.cs" />
<Compile Include="Repositories\NetworkInterfaceRepository\IORegistryDataSource.cs" />
<Compile Include="Services\MacOpenUrlService.cs" />
<Compile Include="Services\Windows\MainWindowService.cs" />
<Compile Include="Services\Windows\AboutBoxWindowService.cs" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="CliWrap" Version="3.4.0" />
Expand Down
59 changes: 59 additions & 0 deletions AltNetworkUtility.macOS/Services/Windows/AboutBoxWindowService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
using AltNetworkUtility.About;
using AltNetworkUtility.Services.Windows;

using AppKit;

using Foundation;

using Xamarin.Forms.Platform.MacOS;

#nullable enable

namespace AltNetworkUtility.macOS.Services.Windows
{
public class AboutBoxWindowService : WindowService, IAboutBoxWindowService
{
public override Window Window => Window.AboutBox;

public override NSWindow OpenWindow()
{
return _SingleInstanceWindows.GetOrAdd(Window, _ =>
{
const NSWindowStyle windowStyle = NSWindowStyle.Closable | NSWindowStyle.Titled;

var window = new NSWindow
{
BackingType = NSBackingStore.Buffered,
Level = NSWindowLevel.Normal,
StyleMask = windowStyle,
Title = $"About {AppDelegate.AppName}"
};

window.TabbingMode = NSWindowTabbingMode.Disallowed;

var page = new AboutBoxPage();

SetNSWindowFrameFromXamFormsPage(window, page);

window.ContentView = page.CreateViewController().View;

var windowController = new NSWindowController(window);
windowController.ShowWindow(null);

window.Delegate = new WindowDelegate();

ShowWindow(window);

return window;
});
}

private class WindowDelegate : NSWindowDelegate
{
public override void WillClose(NSNotification notification)
{
Xamarin.Forms.DependencyService.Get<AboutBoxWindowService>().CloseWindow();
}
}
}
}
47 changes: 47 additions & 0 deletions AltNetworkUtility.macOS/Services/Windows/MainWindowService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
using AltNetworkUtility.MainWindow;

using AppKit;

using Xamarin.Forms.Platform.MacOS;

#nullable enable

namespace AltNetworkUtility.macOS.Services.Windows
{
public class MainWindowService : WindowService
{
public override Window Window => Window.MainWindow;

public override NSWindow OpenWindow()
{
return _SingleInstanceWindows.GetOrAdd(Window, _ =>
{
const NSWindowStyle windowStyle = NSWindowStyle.Closable | NSWindowStyle.Resizable | NSWindowStyle.Titled;

var window = new NSWindow
{
BackingType = NSBackingStore.Buffered,
Level = NSWindowLevel.Normal,
StyleMask = windowStyle,
Title = AppDelegate.AppName
};

window.TabbingMode = NSWindowTabbingMode.Disallowed;

var page = new MainPage();

SetNSWindowFrameFromXamFormsPage(window, page);

window.ContentView = page.CreateViewController().View;

var windowController = new NSWindowController(window);
var appDelegate = NSApplication.SharedApplication.Delegate as AppDelegate;
windowController.ShowWindow(appDelegate);

ShowWindow(window);

return window;
});
}
}
}
93 changes: 3 additions & 90 deletions AltNetworkUtility.macOS/Services/Windows/WindowService.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System.Collections.Concurrent;

using AltNetworkUtility.About;
using AltNetworkUtility.Services.Windows;

using AppKit;
Expand All @@ -9,20 +8,17 @@

using CoreGraphics;

using Foundation;

using Serilog;

using Xamarin.Forms.Platform.MacOS;

#nullable enable

namespace AltNetworkUtility.macOS.Services.Windows
{
public enum Window
{
AboutBox,
MainWindow,
AboutBox
Preferences
}

public abstract class WindowService : IWindowService
Expand Down Expand Up @@ -73,7 +69,7 @@ protected void SetNSWindowFrameFromXamFormsPage(NSWindow window, Xamarin.Forms.P
{
if (page.WidthRequest == 0 || page.HeightRequest == 0)
Log.Warning("Width or height weren't set");

window.SetFrame(new CGRect(200, 200, page.WidthRequest, page.HeightRequest), true, true);
}

Expand All @@ -99,87 +95,4 @@ private static void HideWindow(NSWindow window)
window.IsVisible = false;
}
}

public class MainWindowService : WindowService
{
public override Window Window => Window.MainWindow;

public override NSWindow OpenWindow()
{
return _SingleInstanceWindows.GetOrAdd(Window, _ =>
{
const NSWindowStyle windowStyle = NSWindowStyle.Closable | NSWindowStyle.Resizable | NSWindowStyle.Titled;

var window = new NSWindow
{
BackingType = NSBackingStore.Buffered,
Level = NSWindowLevel.Normal,
StyleMask = windowStyle,
Title = AppDelegate.AppName
};

window.TabbingMode = NSWindowTabbingMode.Disallowed;

var page = new MainPage();

SetNSWindowFrameFromXamFormsPage(window, page);

window.ContentView = page.CreateViewController().View;

var windowController = new NSWindowController(window);
var appDelegate = NSApplication.SharedApplication.Delegate as AppDelegate;
windowController.ShowWindow(appDelegate);

ShowWindow(window);

return window;
});
}
}

public class AboutBoxWindowService : WindowService, IAboutBoxWindowService
{
public override Window Window => Window.AboutBox;

public override NSWindow OpenWindow()
{
return _SingleInstanceWindows.GetOrAdd(Window, _ =>
{
const NSWindowStyle windowStyle = NSWindowStyle.Closable | NSWindowStyle.Titled;

var window = new NSWindow
{
BackingType = NSBackingStore.Buffered,
Level = NSWindowLevel.Normal,
StyleMask = windowStyle,
Title = $"About {AppDelegate.AppName}"
};

window.TabbingMode = NSWindowTabbingMode.Disallowed;

var page = new AboutBoxPage();

SetNSWindowFrameFromXamFormsPage(window, page);

window.ContentView = page.CreateViewController().View;

var windowController = new NSWindowController(window);
windowController.ShowWindow(null);

window.Delegate = new WindowDelegate();

ShowWindow(window);

return window;
});
}

private class WindowDelegate : NSWindowDelegate
{
public override void WillClose(NSNotification notification)
{
Xamarin.Forms.DependencyService.Get<AboutBoxWindowService>().CloseWindow();
}
}
}
}
9 changes: 0 additions & 9 deletions AltNetworkUtility/AltNetworkUtility.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,4 @@
<PackageReference Include="Xamarin.Forms" Version="5.0.0.2291" />
<PackageReference Include="System.Text.Json" Version="6.0.1" />
</ItemGroup>
<ItemGroup>
<None Remove="System.Text.Json" />
<None Remove="About\" />
<None Remove="Services\Windows\" />
</ItemGroup>
<ItemGroup>
<Folder Include="About\" />
<Folder Include="Services\Windows\" />
</ItemGroup>
</Project>
2 changes: 1 addition & 1 deletion AltNetworkUtility/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public App()
{
InitializeComponent();

MainPage = new MainPage();
MainPage = new MainWindow.MainPage();

#if Hacky_StyleSheet_HotReload
new Timer(_ =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
xmlns:netstat="clr-namespace:AltNetworkUtility.Tabs.Netstat"
xmlns:ping="clr-namespace:AltNetworkUtility.Tabs.Ping"
xmlns:lookup="clr-namespace:AltNetworkUtility.Tabs.Lookup"
x:Class="AltNetworkUtility.MainPage"
x:Class="AltNetworkUtility.MainWindow.MainPage"
WidthRequest="640" HeightRequest="480"
PagesChanged="TabbedPage_PagesChanged"
CurrentPageChanged="TabbedPage_CurrentPageChanged">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

using Xamarin.Forms;

namespace AltNetworkUtility
namespace AltNetworkUtility.MainWindow
{
public partial class MainPage : TabbedPage
{
Expand Down
File renamed without changes.
File renamed without changes.

0 comments on commit cb2e54c

Please sign in to comment.