Skip to content

Commit

Permalink
rewritten post process to deal with screens individually
Browse files Browse the repository at this point in the history
  • Loading branch information
wiegell committed Nov 15, 2022
1 parent c3feb5f commit add2fb4
Show file tree
Hide file tree
Showing 25 changed files with 876 additions and 132 deletions.
70 changes: 39 additions & 31 deletions AstroWall/ApplicationLayer/Helpers/General.Macos.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Security.Policy;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
using AppKit;
using AstroWall.ApplicationLayer.Helpers;
using Foundation;

namespace AstroWall
Expand Down Expand Up @@ -47,45 +51,49 @@ public static string getPrefsPath()
}

/// <summary>
/// The return object is a boxed bool
/// The return object is a boxed bool, if the setting of wallpaper was successfull
/// </summary>
/// <param name="path"></param>
/// <param name="onAllScreens"></param>
/// <returns></returns>
public static async Task<object> SetWallpaper(String path, bool onAllScreens = false)
public static async Task<object> SetWallpaper(Dictionary<Screen, string> urlToPostProcessedByNSScreen)
{
bool ret = false;
object boxedBool = (object)ret;

foreach (KeyValuePair<Screen, string> urlNSScreenKV in urlToPostProcessedByNSScreen)
{
string url = urlNSScreenKV.Value;
Screen screen = urlNSScreenKV.Key;
boxedBool = await General.SetWallpaper(screen, url);
}

return boxedBool;

}
public static async Task<object> SetWallpaper(Screen screen, string url)
{
return await RunOnUIThread<object>(async
() =>
{
Console.WriteLine("setting wallpaper: " + path);
NSWorkspace workspace = NSWorkspace.SharedWorkspace;
NSScreen[] screens = NSScreen.Screens;
NSScreen mainScreen = NSScreen.MainScreen;
Console.WriteLine("screen count: " + screens.Length);

bool ret = false;
object boxedBool = (object)ret;
if (!onAllScreens)
{
boxedBool = workspace.SetDesktopImageUrl(NSUrl.FromFilename(path), mainScreen, new NSDictionary(), new NSError());
}
else
foreach (var screen in screens)
{
try
{
boxedBool = workspace.SetDesktopImageUrl(NSUrl.FromFilename(path), screen, new NSDictionary(), new NSError());
}
catch (Exception)
{
Console.WriteLine("desk not set, returning false");
boxedBool = false;
}
}
return boxedBool;
});
() =>
{
bool ret = false;
object boxedBool = (object)ret;
Console.Write($"Setting wallpaper {url} to screen {screen.Id}: ");
try
{
boxedBool = NSWorkspace.SharedWorkspace.SetDesktopImageUrl(NSUrl.FromFilename(url), screen.toNSScreen(), new NSDictionary(), new NSError());
Console.WriteLine("Success");
}
catch (Exception ex)
{
boxedBool = false;
Console.WriteLine("Fail - " + ex.GetType());
}
return boxedBool;
});
}


public static string getCurrentWallpaperPath()
{
NSWorkspace workspace = NSWorkspace.SharedWorkspace;
Expand Down
63 changes: 63 additions & 0 deletions AstroWall/ApplicationLayer/Helpers/Screen.Model.Macos.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
using System;
using AppKit;
using Newtonsoft.Json;
using System.Linq;
using System.Collections.Generic;

namespace AstroWall.ApplicationLayer.Helpers
{
[JsonObject]
public struct Screen
{
public static Dictionary<string, Screen> FromCurrentConnected()
{
return NSScreen.Screens.Select(nsscreen => new Screen(nsscreen)).ToDictionary(screen => screen.Id);
}
public static Screen Main()
{
return new Screen(NSScreen.MainScreen);
}

private Screen(NSScreen nSscreen)
{
Id = nSscreen.LocalizedName;
xRes = (int)nSscreen.Frame.Size.Width;
yRes = (int)nSscreen.Frame.Size.Height;
isMainScreen = NSScreen.MainScreen == nSscreen;
}

/// <summary>
/// Can possibly return null, if the screen width this.Id is
/// no longer connected
/// </summary>
/// <returns></returns>
public NSScreen toNSScreen()
{
var that = this;
var filteredArray = NSScreen.Screens.Where(nsscreen => nsscreen.LocalizedName == that.Id).ToArray();
if (filteredArray.Length != 1) return null;
else return filteredArray[0];
}

[JsonProperty]
public string Id;
[JsonProperty]
public int xRes;
[JsonProperty]
public int yRes;
[JsonProperty]
public bool isMainScreen;

public bool isHorizontal()
{
return xRes > yRes;
}
public bool isVertical()
{
return yRes > xRes;
}
}


}

5 changes: 3 additions & 2 deletions AstroWall/ApplicationLayer/View/AppDelegate.Menu.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Drawing;
using AppKit;
using AstroWall.BusinessLayer.Preferences;
using Foundation;

namespace AstroWall.ApplicationLayer
Expand All @@ -20,14 +21,14 @@ public void createStatusBar(string title)
MenuTitle.Title = title;
}

public void updateMenuCheckMarks(BusinessLayer.Preferences prefs)
public void updateMenuCheckMarks(Preferences prefs)
{
General.RunOnUIThread(() =>
{
this.MenuOutletAutoInstallUpdates.State = prefs.AutoInstallUpdates ? NSCellStateValue.On : NSCellStateValue.Off;
this.MenuOutletCheckUpdatesOnStartup.State = prefs.CheckUpdatesOnStartup ? NSCellStateValue.On : NSCellStateValue.Off;
this.MenuOutletRunAtLogin.State = prefs.RunAtStartup ? NSCellStateValue.On : NSCellStateValue.Off;
this.MenuOutletDailyCheckNewest.State = prefs.DailyCheck == BusinessLayer.DailyCheckEnum.Newest ? NSCellStateValue.On : NSCellStateValue.Off;
this.MenuOutletDailyCheckNewest.State = prefs.DailyCheck == DailyCheckEnum.Newest ? NSCellStateValue.On : NSCellStateValue.Off;
});
}

Expand Down
48 changes: 35 additions & 13 deletions AstroWall/ApplicationLayer/View/AppDelegate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Threading.Tasks;
using AppKit;
using AstroWall.BusinessLayer;
using AstroWall.BusinessLayer.Preferences;
using Foundation;
using GameController;

Expand Down Expand Up @@ -36,7 +37,7 @@ public async override void DidFinishLaunching(NSNotification notification)
await appHandler.Init();
}

public void waitForUserToChosePrefs(Func<BusinessLayer.Preferences, Task> callback)
public void waitForUserToChosePrefs(Func<Preferences, Task> callback)
{
// Launch prefs always on top window
var storyboard = NSStoryboard.FromName("Main", null);
Expand Down Expand Up @@ -64,18 +65,26 @@ public void launchUpdatePrompt(UpdateLibrary.Release rel, Action<UpdatePromptRes
NSApplication.SharedApplication.ActivateIgnoringOtherApps(true);
}

public void launchPostProcessPrompt(Preferences oldPrefs, Action<Preferences> callbackWithNewPrefs)
public void launchPostProcessPrompt(Preferences oldPrefs, Action<AddText> callbackWithNewPostProcess)
{
// Launch prefs always on top window
var storyboard = NSStoryboard.FromName("Main", null);
postProcessPromptWindowController = storyboard.InstantiateControllerWithIdentifier("postprocesswindowcontroller2") as NSWindowController;
var window = postProcessPromptWindowController.Window;
var splitViewController = ((NSSplitViewController)postProcessPromptWindowController.ContentViewController);
//view.SetRelease(rel);
//view.RegChoiceCallback(callback);
postProcessPromptWindowController.ShowWindow(postProcessPromptWindowController);
window.OrderFront(null);
NSApplication.SharedApplication.ActivateIgnoringOtherApps(true);
// Don't spawn new window if already opened
if (!checkIfWindowIsAlreadyOpened(postProcessPromptWindowController))
{

// Launch prefs always on top window
var storyboard = NSStoryboard.FromName("Main", null);
postProcessPromptWindowController = storyboard.InstantiateControllerWithIdentifier("postprocesswindowcontroller2") as NSWindowController;
var window = postProcessPromptWindowController.Window;
var splitViewController = ((NSSplitViewController)postProcessPromptWindowController.ContentViewController);
var contentView = (PostProcessTextSettings)splitViewController.SplitViewItems[1].ViewController.View;
//view.SetRelease(rel);
//view.RegChoiceCallback(callback);
contentView.setData(oldPrefs.AddTextPostProcess);
contentView.regChangePrefsCallback(callbackWithNewPostProcess);
postProcessPromptWindowController.ShowWindow(postProcessPromptWindowController);
window.OrderFront(null);
NSApplication.SharedApplication.ActivateIgnoringOtherApps(true);
}
}

public override void WillTerminate(NSNotification notification)
Expand All @@ -85,7 +94,20 @@ public override void WillTerminate(NSNotification notification)
}
#endregion


private bool checkIfWindowIsAlreadyOpened(NSWindowController windowController)
{
if (windowController == null) return false;
if (!windowController.Window.IsVisible) return false;
if (windowController.Window.IsVisible)
{
windowController.Window.OrderFront(null);
NSApplication.SharedApplication.ActivateIgnoringOtherApps(true);

return true;
}
else throw new Exception("should never be reached");
}




Expand Down
9 changes: 5 additions & 4 deletions AstroWall/ApplicationLayer/View/FreshInstallViewController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@
using Foundation;
using AppKit;
using System.Threading.Tasks;
using AstroWall.BusinessLayer.Preferences;

namespace AstroWall
{
public partial class FreshInstallViewController : NSView
{
private Func<BusinessLayer.Preferences, Task> callback;
private Func<Preferences, Task> callback;

public FreshInstallViewController(IntPtr handle) : base(handle)
{
Expand All @@ -24,14 +25,14 @@ partial void saveAction(Foundation.NSObject sender)
callback(prefs);
}

public void regSaveCallback(Func<BusinessLayer.Preferences, Task> callbackArg)
public void regSaveCallback(Func<Preferences, Task> callbackArg)
{
this.callback = callbackArg;
}

private BusinessLayer.Preferences createPrefs()
private Preferences createPrefs()
{
var prefs = new BusinessLayer.Preferences();
var prefs = new Preferences();
prefs.AutoInstallUpdates = (this.OutletAutoinstall.State == NSCellStateValue.On);
prefs.
CheckUpdatesOnStartup = (this.OutletCheckUpdatesAtStartup.State == NSCellStateValue.On);
Expand Down

This file was deleted.

Loading

0 comments on commit add2fb4

Please sign in to comment.