Skip to content

Commit

Permalink
Add accessibility
Browse files Browse the repository at this point in the history
  • Loading branch information
rmcrackan committed May 7, 2024
1 parent d19fe22 commit 27b2fe7
Show file tree
Hide file tree
Showing 10 changed files with 219 additions and 100 deletions.
49 changes: 49 additions & 0 deletions Source/LibationWinForms/AccessibleDataGridViewButtonCell.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
using System.Windows.Forms;

namespace LibationWinForms
{
public class AccessibleDataGridViewButtonCell : DataGridViewButtonCell
{
protected string AccessibilityName
{
get => MyAccessibilityObject.AccessibilityName;
set => MyAccessibilityObject.AccessibilityName = value;
}

/// <summary>
/// Get or set description for accessibility. eg: screen readers. Also sets the ToolTipText
/// </summary>
protected string AccessibilityDescription
{
get => MyAccessibilityObject.AccessibilityDescription;
set
{
MyAccessibilityObject.AccessibilityDescription = value;
MyAccessibilityObject.Owner.ToolTipText = value;
}
}

protected ButtonCellAccessibilityObject MyAccessibilityObject { get; set; }
protected override AccessibleObject CreateAccessibilityInstance() => MyAccessibilityObject;

public AccessibleDataGridViewButtonCell(string accessibilityName) : base()
{
MyAccessibilityObject = new(this, name: accessibilityName, description: "");
}

protected class ButtonCellAccessibilityObject : DataGridViewButtonCellAccessibleObject
{
public string AccessibilityName { get; set; }
public string AccessibilityDescription { get; set; }

public override string Name => AccessibilityName;
public override string Description => AccessibilityDescription;

public ButtonCellAccessibilityObject(DataGridViewCell owner, string name, string description) : base(owner)
{
AccessibilityName = name;
AccessibilityDescription = description;
}
}
}
}
49 changes: 49 additions & 0 deletions Source/LibationWinForms/AccessibleDataGridViewTextBoxCell.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
using System.Windows.Forms;

namespace LibationWinForms
{
internal class AccessibleDataGridViewTextBoxCell : DataGridViewTextBoxCell
{
protected virtual string AccessibilityName
{
get => MyAccessibilityObject.AccessibilityName;
set => MyAccessibilityObject.AccessibilityName = value;
}

/// <summary>
/// Get or set description for accessibility. eg: screen readers. Also sets the ToolTipText
/// </summary>
protected string AccessibilityDescription
{
get => MyAccessibilityObject.AccessibilityDescription;
set
{
MyAccessibilityObject.AccessibilityDescription = value;
MyAccessibilityObject.Owner.ToolTipText = value;
}
}

protected ButtonCellAccessibilityObject MyAccessibilityObject { get; set; }
protected override AccessibleObject CreateAccessibilityInstance() => MyAccessibilityObject;

public AccessibleDataGridViewTextBoxCell(string accessibilityName) : base()
{
MyAccessibilityObject = new(this, name: accessibilityName, description: "");
}

protected class ButtonCellAccessibilityObject : DataGridViewTextBoxCellAccessibleObject
{
public string AccessibilityName { get; set; }
public string AccessibilityDescription { get; set; }

public override string Name => AccessibilityName;
public override string Description => AccessibilityDescription;

public ButtonCellAccessibilityObject(DataGridViewCell owner, string name, string description) : base(owner)
{
AccessibilityName = name;
AccessibilityDescription = description;
}
}
}
}
14 changes: 7 additions & 7 deletions Source/LibationWinForms/Dialogs/AccountsDialog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,13 @@ private void populateGridValues()

private void AddAccountToGrid(Account account)
{
int row = dataGridView1.Rows.Add(
"X",
"Export",
account.LibraryScan,
account.AccountId,
account.Locale.Name,
account.AccountName);
var row = dataGridView1.Rows.Add(
"X",
"Export",
account.LibraryScan,
account.AccountId,
account.Locale.Name,
account.AccountName);

dataGridView1[COL_Export, row].ToolTipText = "Export account authorization to audible-cli";
}
Expand Down
38 changes: 27 additions & 11 deletions Source/LibationWinForms/Dialogs/EditQuickFilters.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,23 +24,39 @@ public partial class EditQuickFilters : Form
private const string COL_MoveUp = nameof(MoveUp);
private const string COL_MoveDown = nameof(MoveDown);

internal class DisableButtonCell : DataGridViewButtonCell
{
protected override void Paint(Graphics graphics, Rectangle clipBounds, Rectangle cellBounds, int rowIndex, DataGridViewElementStates elementState, object value, object formattedValue, string errorText, DataGridViewCellStyle cellStyle, DataGridViewAdvancedBorderStyle advancedBorderStyle, DataGridViewPaintParts paintParts)
internal class DisableButtonCell : AccessibleDataGridViewButtonCell
{
private int LastRowIndex => DataGridView.Rows[^1].IsNewRow ? DataGridView.Rows[^1].Index - 1 : DataGridView.Rows[^1].Index;

public DisableButtonCell() : base("Edit Filter button") { }

protected override void Paint(Graphics graphics, Rectangle clipBounds, Rectangle cellBounds, int rowIndex, DataGridViewElementStates elementState, object value, object formattedValue, string errorText, DataGridViewCellStyle cellStyle, DataGridViewAdvancedBorderStyle advancedBorderStyle, DataGridViewPaintParts paintParts)
{
if ((OwningColumn.Name == COL_MoveUp && rowIndex == 0)
|| (OwningColumn.Name == COL_MoveDown && rowIndex == LastRowIndex)
|| OwningRow.IsNewRow)
{
var isMoveUp = OwningColumn.Name == COL_MoveUp;
var isMoveDown = OwningColumn.Name == COL_MoveDown;
var isDelete = OwningColumn.Name == COL_Delete;
var isNewRow = OwningRow.IsNewRow;

if (isNewRow
|| (isMoveUp && rowIndex == 0)
|| (isMoveDown && rowIndex == LastRowIndex))
{
base.Paint(graphics, clipBounds, cellBounds, rowIndex, elementState, null, null, null, cellStyle, advancedBorderStyle, paintParts ^ (DataGridViewPaintParts.ContentBackground | DataGridViewPaintParts.ContentForeground | DataGridViewPaintParts.SelectionBackground));

ButtonRenderer.DrawButton(graphics, cellBounds, value as string, cellStyle.Font, false, System.Windows.Forms.VisualStyles.PushButtonState.Disabled);
}
}
else
base.Paint(graphics, clipBounds, cellBounds, rowIndex, elementState, value, formattedValue, errorText, cellStyle, advancedBorderStyle, paintParts);
{
base.Paint(graphics, clipBounds, cellBounds, rowIndex, elementState, value, formattedValue, errorText, cellStyle, advancedBorderStyle, paintParts);

if (isMoveUp)
AccessibilityDescription = "Move up";
else if (isMoveDown)
AccessibilityDescription = "Move down";
else if (isDelete)
AccessibilityDescription = "Delete";
}
}

int LastRowIndex => DataGridView.Rows[^1].IsNewRow ? DataGridView.Rows[^1].Index - 1 : DataGridView.Rows[^1].Index;
}

public EditQuickFilters()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
using System.Drawing;
using System.Windows.Forms;

namespace LibationWinForms.GridView
{
public class DataGridViewImageButtonCell : DataGridViewButtonCell
{
protected void DrawButtonImage(Graphics graphics, Image image, Rectangle cellBounds)
public class DataGridViewImageButtonCell : AccessibleDataGridViewButtonCell
{
public DataGridViewImageButtonCell(string accessibilityName) : base(accessibilityName) { }

protected void DrawButtonImage(Graphics graphics, Image image, Rectangle cellBounds)
{
var scaleFactor = OwningColumn is IDataGridScaleColumn scCol ? scCol.ScaleFactor : 1f;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,33 @@ public EditTagsDataGridViewImageButtonColumn()
}

internal class EditTagsDataGridViewImageButtonCell : DataGridViewImageButtonCell
{
private static Image ButtonImage { get; } = Properties.Resources.edit_25x25;
{
public EditTagsDataGridViewImageButtonCell() : base("Edit Tags button") { }

private static Image ButtonImage { get; } = Properties.Resources.edit_25x25;

protected override void Paint(Graphics graphics, Rectangle clipBounds, Rectangle cellBounds, int rowIndex, DataGridViewElementStates elementState, object value, object formattedValue, string errorText, DataGridViewCellStyle cellStyle, DataGridViewAdvancedBorderStyle advancedBorderStyle, DataGridViewPaintParts paintParts)
{
if (rowIndex >= 0 && DataGridView.GetBoundItem<IGridEntry>(rowIndex) is ISeriesEntry)
base.Paint(graphics, clipBounds, cellBounds, rowIndex, elementState, null, null, null, cellStyle, advancedBorderStyle, DataGridViewPaintParts.Background | DataGridViewPaintParts.Border);
else if (value is string tagStr && tagStr.Length == 0)
// series
if (rowIndex >= 0 && DataGridView.GetBoundItem<IGridEntry>(rowIndex) is ISeriesEntry)
{
base.Paint(graphics, clipBounds, cellBounds, rowIndex, elementState, null, null, null, cellStyle, advancedBorderStyle, DataGridViewPaintParts.Background | DataGridViewPaintParts.Border);
}
// tag: empty
else if (value is string tagStr && tagStr.Length == 0)
{
base.Paint(graphics, clipBounds, cellBounds, rowIndex, elementState, null, null, null, cellStyle, advancedBorderStyle, paintParts);

DrawButtonImage(graphics, ButtonImage, cellBounds);
}
AccessibilityDescription = "Click to edit tags";
}
// tag: not empty
else
base.Paint(graphics, clipBounds, cellBounds, rowIndex, elementState, value, formattedValue, errorText, cellStyle, advancedBorderStyle, paintParts);
}
{
base.Paint(graphics, clipBounds, cellBounds, rowIndex, elementState, value, formattedValue, errorText, cellStyle, advancedBorderStyle, paintParts);

AccessibilityDescription = (string)value;
}
}
}
}
20 changes: 12 additions & 8 deletions Source/LibationWinForms/GridView/LastDownloadedGridViewColumn.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using LibationUiBase.GridView;
using System;
using System;
using System.Drawing;
using System.Windows.Forms;
using LibationUiBase.GridView;

namespace LibationWinForms.GridView
{
Expand All @@ -21,15 +21,19 @@ public override DataGridViewCell CellTemplate
}
}

internal class LastDownloadedGridViewCell : DataGridViewTextBoxCell
{
internal class LastDownloadedGridViewCell : AccessibleDataGridViewTextBoxCell
{
private LastDownloadStatus LastDownload => (LastDownloadStatus)Value;
protected override void Paint(Graphics graphics, Rectangle clipBounds, Rectangle cellBounds, int rowIndex, DataGridViewElementStates cellState, object value, object formattedValue, string errorText, DataGridViewCellStyle cellStyle, DataGridViewAdvancedBorderStyle advancedBorderStyle, DataGridViewPaintParts paintParts)

public LastDownloadedGridViewCell() : base("Last Downloaded") { }

protected override void Paint(Graphics graphics, Rectangle clipBounds, Rectangle cellBounds, int rowIndex, DataGridViewElementStates cellState, object value, object formattedValue, string errorText, DataGridViewCellStyle cellStyle, DataGridViewAdvancedBorderStyle advancedBorderStyle, DataGridViewPaintParts paintParts)
{
if (value is LastDownloadStatus lastDl)
ToolTipText = lastDl.ToolTipText;
base.Paint(graphics, clipBounds, cellBounds, rowIndex, cellState, value, formattedValue, errorText, cellStyle, advancedBorderStyle, paintParts);
}

if (value is LastDownloadStatus lastDl)
AccessibilityDescription = lastDl.ToolTipText;
}

protected override void OnDoubleClick(DataGridViewCellEventArgs e)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,7 @@ public LiberateDataGridViewImageButtonColumn()

internal class LiberateDataGridViewImageButtonCell : DataGridViewImageButtonCell
{
#region Accessibility
private string accessibilityName => "Liberate Image Button";
private string accessibilityDescription = "undefined";
protected override AccessibleObject CreateAccessibilityInstance() => new MyAccessibilityObject(accessibilityName, accessibilityDescription);
protected class MyAccessibilityObject : DataGridViewCellAccessibleObject
{
public override string Name => _name;
public override string Description => _description;

private string _name { get; }
private string _description { get; }

public MyAccessibilityObject(string name, string description) : base()
{
_name = name;
_description = description;
}
}
#endregion
public LiberateDataGridViewImageButtonCell() : base("Liberate button") { }

private static readonly Brush DISABLED_GRAY = new SolidBrush(Color.FromArgb(0x60, Color.LightGray));
private static readonly Color HiddenForeColor = Color.LightGray;
Expand All @@ -51,8 +33,7 @@ protected override void Paint(Graphics graphics, Rectangle clipBounds, Rectangle
base.Paint(graphics, clipBounds, cellBounds, rowIndex, elementState, null, null, null, cellStyle, advancedBorderStyle, paintParts);

DrawButtonImage(graphics, (Image)status.ButtonImage, cellBounds);
accessibilityDescription = status.ToolTip;
ToolTipText = status.ToolTip;
AccessibilityDescription = status.ToolTip;

if (status.IsUnavailable || status.Opacity < 1)
graphics.FillRectangle(DISABLED_GRAY, cellBounds);
Expand Down
25 changes: 14 additions & 11 deletions Source/LibationWinForms/GridView/MyRatingGridViewColumn.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
using DataLayer;
using System;
using System;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using DataLayer;

namespace LibationWinForms.GridView
{
Expand All @@ -24,14 +24,17 @@ public override DataGridViewCell CellTemplate
}
}

internal class MyRatingGridViewCell : DataGridViewTextBoxCell
{
internal class MyRatingGridViewCell : AccessibleDataGridViewTextBoxCell
{
private static Rating DefaultRating => new Rating(0, 0, 0);
public override object DefaultNewRowValue => DefaultRating;
public override Type EditType => typeof(MyRatingCellEditor);
public override Type ValueType => typeof(Rating);

public MyRatingGridViewCell() { ToolTipText = ReadOnly ? "" : "Click to change ratings"; }
public MyRatingGridViewCell() : base("My Rating")
{
AccessibilityDescription = ReadOnly ? "" : "Click to change ratings";
}

public override void InitializeEditingControl(int rowIndex, object initialFormattedValue, DataGridViewCellStyle dataGridViewCellStyle)
{
Expand All @@ -46,14 +49,14 @@ protected override void Paint(Graphics graphics, Rectangle clipBounds, Rectangle
{
if (value is Rating rating)
{
ToolTipText = ReadOnly ? "" : "Click to change ratings";

var starString = rating.ToStarString();
base.Paint(graphics, clipBounds, cellBounds, rowIndex, cellState, starString, starString, errorText, cellStyle, advancedBorderStyle, paintParts);
}
else
base.Paint(graphics, clipBounds, cellBounds, rowIndex, cellState, string.Empty, string.Empty, errorText, cellStyle, advancedBorderStyle, paintParts);
}

AccessibilityDescription = ReadOnly ? "" : "Click to change ratings";
}
else
base.Paint(graphics, clipBounds, cellBounds, rowIndex, cellState, string.Empty, string.Empty, errorText, cellStyle, advancedBorderStyle, paintParts);
}

protected override object GetFormattedValue(object value, int rowIndex, ref DataGridViewCellStyle cellStyle, TypeConverter valueTypeConverter, TypeConverter formattedValueTypeConverter, DataGridViewDataErrorContexts context)
=> value is Rating rating ? rating.ToStarString() : value?.ToString();
Expand Down
Loading

0 comments on commit 27b2fe7

Please sign in to comment.