Skip to content

Commit

Permalink
proof of concept done
Browse files Browse the repository at this point in the history
  • Loading branch information
itisrazza committed Mar 14, 2021
1 parent ef9839f commit 04db930
Show file tree
Hide file tree
Showing 15 changed files with 213 additions and 62 deletions.
28 changes: 0 additions & 28 deletions SuperSize/DisplayUtils.cs

This file was deleted.

2 changes: 1 addition & 1 deletion SuperSize/Forms/ConfigForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ private void RenderDisplayConfiguration(Bitmap bmp)

// get displays
var screens = Screen.GetAllScreens();
var screenBounds = DisplayUtils.GetAllScreenBounds();
var screenBounds = Utilities.GetAllScreenBounds();

// calculate scaling factors
var scale = Math.Min(
Expand Down
38 changes: 30 additions & 8 deletions SuperSize/Forms/NotifyIconForm.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

51 changes: 44 additions & 7 deletions SuperSize/Forms/NotifyIconForm.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using SuperSize.Service;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
Expand All @@ -15,6 +16,26 @@ public partial class NotifyIconForm : Form
public NotifyIconForm()
{
InitializeComponent();
PopulateWindowList();
}

private void PopulateWindowList()
{
// generate a window list
windowMenu.Items.Clear();
windowMenu.Items.AddRange(
OS.Utilities.GetOpenWindows()
.Select((window) =>
{
var item = new ToolStripMenuItem
{
Text = window.Title
};
item.Click += (_, _) => Sizer.SizeWindow(window.Handle);

return item;
})
.ToArray());
}

private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
Expand All @@ -24,12 +45,28 @@ private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)

private void contextMenuStrip_Opening(object sender, CancelEventArgs e)
{
// generate a window list
maxWindowMenu.DropDownItems.Clear();
maxWindowMenu.DropDownItems.AddRange(
OS.Utilities.GetOpenWindows()
.Select((window) => new ToolStripDropDownButton { Text = window.Title })
.ToArray());
}

private void notifyIcon_MouseClick(object sender, MouseEventArgs e)
{
switch (e.Button)
{
case MouseButtons.Left:
break;
case MouseButtons.None:
break;
case MouseButtons.Right:
//PopulateWindowList();
break;
case MouseButtons.Middle:
break;
case MouseButtons.XButton1:
break;
case MouseButtons.XButton2:
break;
default:
break;
}
}
}
}
2 changes: 1 addition & 1 deletion SuperSize/Forms/NotifyIconForm.resx
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
<metadata name="s1.GenerateMember" type="System.Boolean, System.Private.CoreLib, Version=5.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e">
<value>False</value>
</metadata>
<assembly alias="System.Drawing.Common" name="System.Drawing.Common, Version=5.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51" />
<assembly alias="System.Drawing.Common" name="System.Drawing.Common, Version=5.0.0.1, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51" />
<data name="notifyIcon.Icon" type="System.Drawing.Icon, System.Drawing.Common" mimetype="application/x-microsoft.net.object.bytearray.base64">
<value>
AAABAAkAAAAAAAEAIADpHQAAlgAAAICAAAABACAAKAgBAH8eAABgYAAAAQAgAKiUAACnJgEASEgAAAEA
Expand Down
10 changes: 10 additions & 0 deletions SuperSize/Model/KeyboardShortcut.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,15 @@ public enum ModifierKeys : uint
Alt = 1,
Shift = 4
}

public static bool operator ==(KeyboardShortcut left, KeyboardShortcut right)
{
return left.Equals(right);
}

public static bool operator !=(KeyboardShortcut left, KeyboardShortcut right)
{
return !(left == right);
}
}
}
3 changes: 3 additions & 0 deletions SuperSize/Model/SizingLogic.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ namespace SuperSize.Model
{
public abstract class SizingLogic
{

public Rectangle Calculate() => Calculate(Screen.GetAllScreens());

public abstract Rectangle Calculate(Screen[] screens);
}
}
37 changes: 36 additions & 1 deletion SuperSize/OS/NativeImports.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@

namespace SuperSize.OS
{
internal class NativeImports
/// <summary>
/// Definitions for Win32 API calls.
/// </summary>
internal static class NativeImports
{
[DllImport("user32.dll")]
public static extern bool RegisterHotKey(IntPtr hWnd, int id, uint fsModifiers, uint vk);
Expand All @@ -31,5 +34,37 @@ internal class NativeImports

[DllImport("USER32.DLL")]
public static extern IntPtr GetShellWindow();

[DllImport("user32.dll", SetLastError = true)]
public static extern bool BringWindowToTop(IntPtr hWnd);

[DllImport("user32.dll")]
public static extern bool ShowWindowAsync(IntPtr hWnd, int nCmdShow);

[DllImport("user32.dll")]
public static extern bool SetForegroundWindow(IntPtr hWnd);

/// <summary>
/// Changes the size, position, and Z order of a child, pop-up, or top-level window. These windows are ordered according to their appearance on the screen. The topmost window receives the highest rank and is the first window in the Z order.
/// </summary>
/// <param name="hWnd">A handle to the window.</param>
/// <param name="hWndInsertAfter">A handle to the window to precede the positioned window in the Z order. This parameter must be a window handle or one of <see cref="HWnd"/>.</param>
/// <param name="x">The new position of the left side of the window, in client coordinates.</param>
/// <param name="y">The new position of the top of the window, in client coordinates.</param>
/// <param name="cx">The new width of the window, in pixels.</param>
/// <param name="cy">The new height of the window, in pixels.</param>
/// <param name="uFlags">The window sizing and positioning flags. This parameter can be a combination of the following values.</param>
/// <returns>If the function succeeds, the return value is nonzero. If the function fails, the return value is zero. To get extended error information, call GetLastError.</returns>
/// <!-- https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-setwindowpos -->
[DllImport("user32.dll")]
public static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int x, int y, int cx, int cy, uint uFlags);

public static class HWnd
{
public static IntPtr Bottom { get; } = new IntPtr(1);
public static IntPtr NoTopMost { get; } = new IntPtr(-2);
public static IntPtr Top { get; } = new IntPtr(0);
public static IntPtr TopMost { get; } = new IntPtr(-1);
}
}
}
21 changes: 21 additions & 0 deletions SuperSize/OS/Utilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Threading.Tasks;
using System.Diagnostics;
using System.Windows.Forms;
using System.Drawing;

namespace SuperSize.OS
{
Expand All @@ -25,12 +26,32 @@ public static class Utilities
var strBuilder = new StringBuilder(strLength);
NativeImports.GetWindowText(hWnd, strBuilder, strLength + 1);

// ignore it if the title is empty
if (strBuilder.ToString() == string.Empty) return true;

// add to dict
list.Add((hWnd, strBuilder.ToString()));
return true;
}, 0);

return list;
}
public static Rectangle GetAllScreenBounds()
{
var top = 0;
var right = 0;
var bottom = 0;
var left = 0;
foreach (var screen in Screen.AllScreens)
{
var (x, y, width, height) = screen.Bounds;
if (x < left) left = x;
if (y < top) top = y;
if (x + width > right) right = x + width;
if (y + height > bottom) bottom = y + height;
}

return new(left, top, right - left, bottom - top);
}
}
}
2 changes: 1 addition & 1 deletion SuperSize/Properties/Settings.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion SuperSize/Properties/Settings.settings
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<Value Profile="(Default)">0</Value>
</Setting>
<Setting Name="BuiltinScript" Type="System.String" Scope="User">
<Value Profile="(Default)" />
<Value Profile="(Default)">Fill out completely</Value>
</Setting>
<Setting Name="ScreenBorderColor" Type="System.Drawing.Color" Scope="User">
<Value Profile="(Default)">45, 125, 154</Value>
Expand Down
19 changes: 19 additions & 0 deletions SuperSize/Scripts/FillOutCompletely.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using SuperSize.Model;
using SuperSize.OS;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SuperSize.Scripts
{
public class FillOutCompletely : SizingLogic
{
public override Rectangle Calculate(Screen[] screens)
{
return Utilities.GetAllScreenBounds();
}
}
}
3 changes: 2 additions & 1 deletion SuperSize/Service/PythonSizingLogic.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using IronPython.Hosting;
using Microsoft.Scripting.Hosting;
using SuperSize.Model;
using SuperSize.OS;
using System;
using System.Collections.Generic;
using System.Drawing;
Expand Down Expand Up @@ -76,7 +77,7 @@ private void PythonSetup()

// screen detail
{ "get_screens", new Func<Screen[]>(() => Screen.GetAllScreens()) },
{ "get_all_screen_bounds", new Func<Rectangle>(() => DisplayUtils.GetAllScreenBounds()) },
{ "get_all_screen_bounds", new Func<Rectangle>(() => Utilities.GetAllScreenBounds()) },

// .NET object constructors
{ "rectange", new Func<int, int, int, int, Rectangle>((x, y, w, h) => new Rectangle(x, y, w, h)) },
Expand Down
Loading

0 comments on commit 04db930

Please sign in to comment.