Skip to content

Commit

Permalink
Some quality of life improvements.
Browse files Browse the repository at this point in the history
  • Loading branch information
soukoku committed Apr 10, 2023
1 parent 88458c0 commit b89c186
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 3 deletions.
17 changes: 17 additions & 0 deletions src/NTwain/Data/TWAINH_EXTRAS.cs
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,16 @@ public struct STS
// return new STS { RC = rc };
//}

/// <summary>
/// Quick check if the RC is success.
/// </summary>
public bool IsSuccess => RC == TWRC.SUCCESS;

/// <summary>
/// Quick access to condition code.
/// </summary>
public TWCC ConditionCode => STATUS.ConditionCode;

public override string ToString()
{
return $"{RC} - {STATUS.ConditionCode}";
Expand Down Expand Up @@ -647,10 +657,16 @@ public override string ToString()
}
partial struct TW_IDENTITY_LEGACY
{
/// <summary>
/// A simplified check on whether this has valid data from DSM.
/// </summary>
public bool HasValue => Id == 0 && ProtocolMajor == 0 && ProtocolMinor == 0;

public override string ToString()
{
return $"{Manufacturer} - {ProductName} v{Version.MajorNum}.{Version.MinorNum} (TWAIN {ProtocolMajor}.{ProtocolMinor})";
}

public static implicit operator TW_IDENTITY(TW_IDENTITY_LEGACY value) => new()
{
Id = value.Id,
Expand Down Expand Up @@ -820,6 +836,7 @@ partial struct TW_DEVICEEVENT
// provide casted versions over raw value

public TWDE Event { get { return (TWDE)_Event; } }

public TWFL FlashUsed2 { get { return (TWFL)_FlashUsed2; } }
}

Expand Down
14 changes: 14 additions & 0 deletions src/NTwain/TwainAppSession.Caps.cs
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,20 @@ public STS SetCap(ref TW_CAPABILITY value)
return WrapInSTS(rc);
}

/// <summary>
/// A simpler cap value setter for common one-value scenarios
/// that's easier to use. Not for other container type sets.
/// </summary>
/// <typeparam name="TValue"></typeparam>
/// <param name="cap"></param>
/// <param name="value"></param>
/// <returns></returns>
public STS SetCap<TValue>(CAP cap, TValue value) where TValue : struct
{
var twcap = ValueWriter.CreateOneValueCap(cap, this, value);
return SetCap(ref twcap);
}

/// <summary>
/// Sets a CAP's constraint values.
/// An easy way to create a value is to use the
Expand Down
7 changes: 6 additions & 1 deletion src/NTwain/TwainAppSession.PropEvents.cs
Original file line number Diff line number Diff line change
Expand Up @@ -134,10 +134,15 @@ public byte[]? CustomDsData
public event TwainEventDelegate<TW_IDENTITY_LEGACY>? DefaultSourceChanged;

/// <summary>
/// Fires when <see cref="CurrentSource"/> changes.
/// Fires when <see cref="CurrentSource"/> changes (opened and closed).
/// </summary>
public event TwainEventDelegate<TW_IDENTITY_LEGACY>? CurrentSourceChanged;

/// <summary>
/// Fires when source has moved from enabled to disabled state.
/// </summary>
public event TwainEventDelegate<TW_IDENTITY_LEGACY>? SourceDisabled;

/// <summary>
/// Fires when the source has some device event happening.
/// </summary>
Expand Down
1 change: 1 addition & 0 deletions src/NTwain/TwainAppSession.Sources.cs
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ public STS DisableSource()
if (rc == TWRC.SUCCESS)
{
State = STATE.S4;
SourceDisabled?.Invoke(this, _currentDS);
}
return WrapInSTS(rc);
}
Expand Down
5 changes: 3 additions & 2 deletions src/NTwain/TwainAppSession.cs
Original file line number Diff line number Diff line change
Expand Up @@ -228,12 +228,13 @@ public STS CloseDSM()
}

/// <summary>
/// Wraps return code with additional status if not successful.
/// Wraps a return code with additional status if not successful.
/// Use this right after an API call to get its condition code.
/// </summary>
/// <param name="rc"></param>
/// <param name="dsmOnly">true to get status for dsm operation error, false to get status for ds operation error,</param>
/// <returns></returns>
protected STS WrapInSTS(TWRC rc, bool dsmOnly = false)
public STS WrapInSTS(TWRC rc, bool dsmOnly = false)
{
if (rc != TWRC.FAILURE) return new STS { RC = rc };
var sts = new STS { RC = rc, STATUS = GetLastStatus(dsmOnly) };
Expand Down

0 comments on commit b89c186

Please sign in to comment.