Skip to content

Commit

Permalink
Merge pull request #187 from emoacht/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
emoacht authored Jun 16, 2021
2 parents 9b9b478 + 63d5560 commit 0112d73
Show file tree
Hide file tree
Showing 16 changed files with 746 additions and 38 deletions.
2 changes: 1 addition & 1 deletion Source/Installer/Product.wxs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Wix xmlns="http://schemas.microsoft.com/wix/2006/wi">
<Product Id="*" Name="Monitorian" Manufacturer="emoacht" Version="2.18.1"
<Product Id="*" Name="Monitorian" Manufacturer="emoacht" Version="2.19.0"
Language="1033" Codepage="1252" UpgradeCode="{81A4D148-75D3-462E-938D-8C208FB48E3C}">
<Package Id="*" InstallerVersion="500" Compressed="yes"
InstallScope="perMachine" InstallPrivileges="elevated"
Expand Down
11 changes: 9 additions & 2 deletions Source/Monitorian.Core/Models/Monitor/DeviceContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -220,11 +220,18 @@ public DeviceItem(
[DataContract]
public class HandleItem
{
[DataMember]
[DataMember(Order = 0)]
public int DisplayIndex { get; }

[DataMember]
public Rect MonitorRect { get; }
[DataMember(Order = 1, Name = nameof(MonitorRect))]
private string _monitorRectString;

[OnSerializing]
private void OnSerializing(StreamingContext context)
{
_monitorRectString = $"Location:{MonitorRect.Location}, Size:{MonitorRect.Size}";
}

public IntPtr MonitorHandle { get; }

Expand Down
1 change: 1 addition & 0 deletions Source/Monitorian.Core/Models/Monitor/IMonitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public enum AccessStatus
Succeeded,
Failed,
DdcFailed,
TransmissionFailed,
NoLongerExist
}

Expand Down
9 changes: 8 additions & 1 deletion Source/Monitorian.Core/Models/Monitor/MSMonitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,15 @@ public class DesktopItem
[DataMember(Order = 1)]
public string Description { get; }

[DataMember(Order = 2)]
public byte[] BrightnessLevels { get; }
[DataMember(Order = 2, Name = nameof(BrightnessLevels))]
private string _brightnessLevelsString;

[OnSerializing]
private void OnSerializing(StreamingContext context)
{
_brightnessLevelsString = string.Join(" ", BrightnessLevels ?? Array.Empty<byte>());
}

public DesktopItem(
string deviceInstanceId,
Expand Down
2 changes: 2 additions & 0 deletions Source/Monitorian.Core/Models/Monitor/MonitorConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -463,6 +463,7 @@ public static AccessResult SetBrightness(SafePhysicalMonitorHandle physicalMonit
private const uint ERROR_GRAPHICS_DDCCI_INVALID_MESSAGE_COMMAND = 0xC0262589;
private const uint ERROR_GRAPHICS_DDCCI_INVALID_MESSAGE_LENGTH = 0xC026258A;
private const uint ERROR_GRAPHICS_DDCCI_INVALID_MESSAGE_CHECKSUM = 0xC026258B;
private const uint ERROR_GRAPHICS_I2C_ERROR_TRANSMITTING_DATA = 0xC0262582;
private const uint ERROR_GRAPHICS_MONITOR_NO_LONGER_EXISTS = 0xC026258D;

private static AccessStatus GetStatus(int errorCode)
Expand All @@ -474,6 +475,7 @@ ERROR_GRAPHICS_DDCCI_INVALID_DATA or
ERROR_GRAPHICS_DDCCI_INVALID_MESSAGE_COMMAND or
ERROR_GRAPHICS_DDCCI_INVALID_MESSAGE_LENGTH or
ERROR_GRAPHICS_DDCCI_INVALID_MESSAGE_CHECKSUM => AccessStatus.DdcFailed,
ERROR_GRAPHICS_I2C_ERROR_TRANSMITTING_DATA => AccessStatus.TransmissionFailed,
ERROR_GRAPHICS_MONITOR_NO_LONGER_EXISTS => AccessStatus.NoLongerExist,
_ => AccessStatus.Failed
};
Expand Down
3 changes: 3 additions & 0 deletions Source/Monitorian.Core/Monitorian.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,9 @@
<Compile Include="Views\Converters\StringToVisibilityConverter.cs" />
<Compile Include="Views\Converters\VisibilityInverseConverter.cs" />
<Compile Include="Views\Converters\VisibilityToBooleanFilterConverter.cs" />
<Compile Include="Views\Touchpad\TouchpadContact.cs" />
<Compile Include="Views\Touchpad\TouchpadHelper.cs" />
<Compile Include="Views\Touchpad\TouchpadTracker.cs" />
<Compile Include="Views\MainWindow.xaml.cs">
<DependentUpon>MainWindow.xaml</DependentUpon>
</Compile>
Expand Down
4 changes: 2 additions & 2 deletions Source/Monitorian.Core/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("2.18.1.0")]
[assembly: AssemblyFileVersion("2.18.1.0")]
[assembly: AssemblyVersion("2.19.0.0")]
[assembly: AssemblyFileVersion("2.19.0.0")]
[assembly: NeutralResourcesLanguage("en-US")]

// For unit test
Expand Down
15 changes: 8 additions & 7 deletions Source/Monitorian.Core/ViewModels/MonitorViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -187,12 +187,7 @@ public void IncrementBrightness(int tickSize, bool isCycle = true)
var count = Math.Floor((Brightness - RangeLowest) / size);
int brightness = RangeLowest + (int)Math.Ceiling((count + 1) * size);

if (brightness < RangeLowest)
brightness = RangeLowest;
else if (RangeHighest < brightness)
brightness = isCycle ? RangeLowest : RangeHighest;

SetBrightness(brightness);
SetBrightness(brightness, isCycle);
}

public void DecrementBrightness()
Expand All @@ -212,10 +207,15 @@ public void DecrementBrightness(int tickSize, bool isCycle = true)
var count = Math.Ceiling((Brightness - RangeLowest) / size);
int brightness = RangeLowest + (int)Math.Floor((count - 1) * size);

SetBrightness(brightness, isCycle);
}

private void SetBrightness(int brightness, bool isCycle)
{
if (brightness < RangeLowest)
brightness = isCycle ? RangeHighest : RangeLowest;
else if (RangeHighest < brightness)
brightness = RangeHighest;
brightness = isCycle ? RangeLowest : RangeHighest;

SetBrightness(brightness);
}
Expand All @@ -241,6 +241,7 @@ private bool SetBrightness(int brightness)
switch (result.Status)
{
case AccessStatus.DdcFailed:
case AccessStatus.TransmissionFailed:
case AccessStatus.NoLongerExist:
_controller.OnMonitorsChangeFound();
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,13 +95,13 @@ private void OnMoved(object sender, double delta)
}
else
{
base.UpdateSourceDeferred();
base.ExecuteUpdateSource();
}
}

protected override void UpdateSourceDeferred()
protected override void ExecuteUpdateSource()
{
base.UpdateSourceDeferred();
base.ExecuteUpdateSource();

Moved?.Invoke(this, 0D);
}
Expand Down
31 changes: 19 additions & 12 deletions Source/Monitorian.Core/Views/Controls/Sliders/EnhancedSlider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,13 @@ public override void OnApplyTemplate()
CheckCanDrag();
}

public bool ChangeValue(double changeSize)
{
return UpdateValue(this.Value + changeSize);
}

public void EnsureUpdateSource() => ExecuteUpdateSource();

protected virtual bool UpdateValue(double value)
{
// Slider.SnapToTick property will not be reflected like Slider.UpdateValue method.
Expand Down Expand Up @@ -112,7 +119,7 @@ protected override void OnPreviewMouseUp(MouseButtonEventArgs e)
{
base.OnPreviewMouseUp(e);

UpdateSourceDeferred();
ExecuteUpdateSource();
}

// OnPreviewStylusDown covers the case of OnPreviewTouchDown.
Expand Down Expand Up @@ -289,7 +296,7 @@ protected override void OnManipulationCompleted(ManipulationCompletedEventArgs e
{
base.OnManipulationCompleted(e);

UpdateSourceDeferred();
ExecuteUpdateSource();
}

#endregion
Expand Down Expand Up @@ -321,32 +328,32 @@ protected override void OnMouseWheel(MouseWheelEventArgs e)
// Mouse.MouseWheelDeltaForOneLine should be casted to double in case the delta is smaller than 120.
var newValue = this.Value + (e.Delta / (double)Mouse.MouseWheelDeltaForOneLine * WheelFactor);
UpdateValue(newValue);
UpdateSourceDeferred();
ExecuteUpdateSource();
}

#endregion

#region Deferral

public bool IsUpdateSourceDeferred
public bool DefersUpdateSource
{
get { return (bool)GetValue(IsUpdateSourceDeferredProperty); }
set { SetValue(IsUpdateSourceDeferredProperty, value); }
get { return (bool)GetValue(DefersUpdateSourceProperty); }
set { SetValue(DefersUpdateSourceProperty, value); }
}
public static readonly DependencyProperty IsUpdateSourceDeferredProperty =
public static readonly DependencyProperty DefersUpdateSourceProperty =
DependencyProperty.Register(
"IsUpdateSourceDeferred",
"DefersUpdateSource",
typeof(bool),
typeof(EnhancedSlider),
new PropertyMetadata(
false,
(d, e) => ((EnhancedSlider)d).PrepareSourceDeferred((bool)e.NewValue)));
(d, e) => ((EnhancedSlider)d).PrepareUpdateSource((bool)e.NewValue)));

private BindingExpression _valuePropertyExpression;

protected virtual void PrepareSourceDeferred(bool isDeferred)
protected virtual void PrepareUpdateSource(bool defer)
{
if (isDeferred)
if (defer)
{
_valuePropertyExpression = ReplaceBinding(this, ValueProperty, BindingMode.TwoWay, UpdateSourceTrigger.Explicit);
}
Expand All @@ -373,7 +380,7 @@ static BindingExpression ReplaceBinding(DependencyObject target, DependencyPrope
}
}

protected virtual void UpdateSourceDeferred()
protected virtual void ExecuteUpdateSource()
{
_valuePropertyExpression?.UpdateSource();
}
Expand Down
10 changes: 2 additions & 8 deletions Source/Monitorian.Core/Views/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -610,8 +610,7 @@

<Button Grid.Column="1"
Width="80" Height="{DynamicResource ButtonHeight}" Margin="3,0,0,0"
Style="{StaticResource BrightnessButtonStyle}"
IsManipulationEnabled="True">
Style="{StaticResource BrightnessButtonStyle}">
<Grid>
<TextBlock Margin="2" HorizontalAlignment="Left" VerticalAlignment="Bottom"
Text="{Binding Brightness, Mode=OneWay, StringFormat={}{0}%}"
Expand All @@ -628,11 +627,6 @@
<i:CallMethodAction TargetObject="{Binding}"
MethodName="IncrementBrightness"/>
</i:EventTrigger>
<i:EventTrigger EventName="ManipulationCompleted">
<behaviors:ManipulationCallMethodAction TargetObject="{Binding}"
IncreaseMethodName="IncrementBrightness"
DecreaseMethodName="DecrementBrightness"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</Button>

Expand All @@ -641,7 +635,7 @@
Style="{StaticResource SliderHorizontal}"
Minimum="0" Maximum="100"
Value="{Binding Brightness, Mode=TwoWay, Delay=50}"
IsUpdateSourceDeferred="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Window}}, Path=DataContext.Settings.DefersUpdate, Mode=OneWay}"
DefersUpdateSource="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Window}}, Path=DataContext.Settings.DefersUpdate, Mode=OneWay}"
IsShadowVisible="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Window}}, Path=DataContext.Settings.ShowsAdjusted}"
ValueShadow="{Binding BrightnessSystemAdjusted, Mode=OneWay}"
IsUnison="{Binding IsUnison, Mode=TwoWay}"
Expand Down
15 changes: 15 additions & 0 deletions Source/Monitorian.Core/Views/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,16 @@
using Monitorian.Core.Helper;
using Monitorian.Core.Models;
using Monitorian.Core.ViewModels;
using Monitorian.Core.Views.Controls;
using Monitorian.Core.Views.Touchpad;
using ScreenFrame.Movers;

namespace Monitorian.Core.Views
{
public partial class MainWindow : Window
{
private readonly StickWindowMover _mover;
private readonly TouchpadTracker _tracker;
public MainWindowViewModel ViewModel => (MainWindowViewModel)this.DataContext;

public MainWindow(AppControllerCore controller)
Expand All @@ -33,6 +36,18 @@ public MainWindow(AppControllerCore controller)
this.DataContext = new MainWindowViewModel(controller);

_mover = new StickWindowMover(this, controller.NotifyIconContainer.NotifyIcon);

_tracker = new TouchpadTracker(this);
_tracker.ManipulationDelta += (_, delta) =>
{
var slider = FocusManager.GetFocusedElement(this) as EnhancedSlider;
slider?.ChangeValue(delta);
};
_tracker.ManipulationCompleted += (_, _) =>
{
var slider = FocusManager.GetFocusedElement(this) as EnhancedSlider;
slider?.EnsureUpdateSource();
};
}

protected override void OnSourceInitialized(EventArgs e)
Expand Down
58 changes: 58 additions & 0 deletions Source/Monitorian.Core/Views/Touchpad/TouchpadContact.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace Monitorian.Core.Views.Touchpad
{
internal struct TouchpadContact : IEquatable<TouchpadContact>
{
public int ContactId { get; }
public int X { get; }
public int Y { get; }

public Point Point => new(X, Y);

public TouchpadContact(int contactId, int x, int y) =>
(this.ContactId, this.X, this.Y) = (contactId, x, y);

public override bool Equals(object obj) => (obj is TouchpadContact other) && Equals(other);

public bool Equals(TouchpadContact other) =>
(this.ContactId == other.ContactId) && (this.X == other.X) && (this.Y == other.Y);

public static bool operator ==(TouchpadContact a, TouchpadContact b) => a.Equals(b);
public static bool operator !=(TouchpadContact a, TouchpadContact b) => !a.Equals(b);

public override int GetHashCode() => (this.ContactId, this.X, this.Y).GetHashCode();

public override string ToString() => $"Contact ID:{ContactId} Point:{X},{Y}";
}

internal class TouchpadContactCreator
{
public int? ContactId { get; set; }
public int? X { get; set; }
public int? Y { get; set; }

public bool TryCreate(out TouchpadContact contact)
{
if (ContactId.HasValue && X.HasValue && Y.HasValue)
{
contact = new TouchpadContact(ContactId.Value, X.Value, Y.Value);
return true;
}
contact = default;
return false;
}

public void Clear()
{
ContactId = null;
X = null;
Y = null;
}
}
}
Loading

0 comments on commit 0112d73

Please sign in to comment.