From 478adfab195535a5b8366c072bd6a45d8e91e506 Mon Sep 17 00:00:00 2001 From: Lorenz Cuno Klopfenstein Date: Tue, 29 Oct 2013 12:20:39 +0100 Subject: [PATCH] Issue #37: added width/height only command line parameters. --- OnTopReplica/AspectRatioForm.cs | 18 ++++++++++++++++-- OnTopReplica/StartupOptions/Factory.cs | 12 +++++++++++- OnTopReplica/StartupOptions/Options.cs | 24 +++++++++++++++++++----- 3 files changed, 46 insertions(+), 8 deletions(-) diff --git a/OnTopReplica/AspectRatioForm.cs b/OnTopReplica/AspectRatioForm.cs index 87f99f7..6c31476 100644 --- a/OnTopReplica/AspectRatioForm.cs +++ b/OnTopReplica/AspectRatioForm.cs @@ -150,7 +150,7 @@ namespace OnTopReplica { //Ensure that the ClientSize of the form is always respected //(not ensured by the WM_SIZING message alone because of rounding errors and the chrome space) if (KeepAspectRatio) { - var newHeight = (int)Math.Round(((ClientSize.Width - ExtraPadding.Horizontal) / AspectRatio) + ExtraPadding.Vertical); + var newHeight = ComputeHeightFromWidth(ClientSize.Width); ClientSize = new Size(ClientSize.Width, newHeight); } } @@ -203,7 +203,7 @@ namespace OnTopReplica { #endregion - #region ClientSize/Size conversion helpers + #region Conversion helpers /// /// Converts a client size measurement to a window size measurement. @@ -227,6 +227,20 @@ namespace OnTopReplica { return new Size(size.Width - difference.Width, size.Height - difference.Height); } + /// + /// Computes height from width value, according to aspect ratio of window. + /// + public int ComputeHeightFromWidth(int width) { + return (int)Math.Round(((width - ExtraPadding.Horizontal) / AspectRatio) + ExtraPadding.Vertical); + } + + /// + /// Computes width from height value, according to aspect ratio of window. + /// + public int ComputeWidthFromHeight(int height) { + return (int)Math.Round(((height - ExtraPadding.Vertical) * AspectRatio) + ExtraPadding.Horizontal); + } + #endregion /// diff --git a/OnTopReplica/StartupOptions/Factory.cs b/OnTopReplica/StartupOptions/Factory.cs index f62ad6c..79e4985 100644 --- a/OnTopReplica/StartupOptions/Factory.cs +++ b/OnTopReplica/StartupOptions/Factory.cs @@ -83,9 +83,19 @@ namespace OnTopReplica.StartupOptions { .Add("v|visible", "If set, only clones windows that are visible.", s => { options.MustBeVisible = true; }) - .Add("size=", "Target {WIDTH,HEIGHT} of the cloned thumbnail.", s => { + .Add("size=", "Target {WIDTH,HEIGHT} of the cloned thumbnail, or", s => { options.StartSize = s; }) + .Add("width=", "Target WIDTH of cloned thumbnail, or", i => { + if (options.StartSize.HasValue || options.StartHeight.HasValue) + return; + options.StartWidth = i; + }) + .Add("height=", "Target HEIGHT of cloned thumbnail.", i => { + if (options.StartSize.HasValue || options.StartWidth.HasValue) + return; + options.StartHeight = i; + }) .Add("position=", "Target {X,Y} of the OnTopReplica window.", s => { options.StartLocation = new Point(s.Width, s.Height); options.StartPositionLock = null; diff --git a/OnTopReplica/StartupOptions/Options.cs b/OnTopReplica/StartupOptions/Options.cs index 39ef5bf..cabb1cf 100644 --- a/OnTopReplica/StartupOptions/Options.cs +++ b/OnTopReplica/StartupOptions/Options.cs @@ -29,6 +29,10 @@ namespace OnTopReplica.StartupOptions { public Size? StartSize { get; set; } + public int? StartWidth { get; set; } + + public int? StartHeight { get; set; } + #endregion #region Window cloning @@ -124,6 +128,21 @@ namespace OnTopReplica.StartupOptions { form.PositionLock = StartPositionLock.Value; } + //Clone any found handle (this applies thumbnail and aspect ratio) + if (handle != null) { + form.SetThumbnail(handle, Region); + } + + //Adaptive size handling + if (!StartSize.HasValue && (StartWidth.HasValue || StartHeight.HasValue)) { + if (StartWidth.HasValue) { + StartSize = new Size(StartWidth.Value, form.ComputeHeightFromWidth(StartWidth.Value)); + } + else { + StartSize = new Size(form.ComputeWidthFromHeight(StartHeight.Value), StartHeight.Value); + } + } + //Size and location start values if (StartLocation.HasValue && StartSize.HasValue) { form.StartPosition = System.Windows.Forms.FormStartPosition.Manual; @@ -139,11 +158,6 @@ namespace OnTopReplica.StartupOptions { form.ClientSize = StartSize.Value; } - //Clone any found handle - if (handle != null) { - form.SetThumbnail(handle, Region); - } - //Other features if (EnableClickForwarding) { form.ClickForwardingEnabled = true;