Skip to content

Commit

Permalink
fixed all views to deal with Driver is null
Browse files Browse the repository at this point in the history
  • Loading branch information
tig committed Oct 22, 2024
1 parent 262c671 commit 98a2265
Show file tree
Hide file tree
Showing 18 changed files with 109 additions and 93 deletions.
4 changes: 2 additions & 2 deletions Terminal.Gui/View/Adornment/ShadowView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -139,9 +139,9 @@ private void DrawVerticalShadowTransparent (Rectangle viewport)
// Fill the rest of the rectangle
for (int i = Math.Max (0, screen.Y); i < screen.Y + viewport.Height; i++)
{
Driver.Move (screen.X, i);
Driver?.Move (screen.X, i);

if (Driver.Contents is { } && screen.X < Driver.Contents.GetLength (1) && i < Driver.Contents.GetLength (0))
if (Driver?.Contents is { } && screen.X < Driver.Contents.GetLength (1) && i < Driver.Contents.GetLength (0))
{
Driver.AddRune (Driver.Contents [i, screen.X].Rune);
}
Expand Down
18 changes: 9 additions & 9 deletions Terminal.Gui/Views/ComboBox.cs
Original file line number Diff line number Diff line change
Expand Up @@ -303,9 +303,9 @@ public override void OnDrawContent (Rectangle viewport)
return;
}

Driver.SetAttribute (ColorScheme.Focus);
Driver?.SetAttribute (ColorScheme.Focus);
Move (Viewport.Right - 1, 0);
Driver.AddRune (Glyphs.DownArrow);
Driver?.AddRune (Glyphs.DownArrow);
}


Expand Down Expand Up @@ -887,8 +887,8 @@ protected override bool OnMouseEvent (MouseEventArgs me)

public override void OnDrawContent (Rectangle viewport)
{
Attribute current = ColorScheme.Focus;
Driver.SetAttribute (current);
Attribute current = ColorScheme?.Focus ?? Attribute.Default;
Driver?.SetAttribute (current);
Move (0, 0);
Rectangle f = Frame;
int item = TopItem;
Expand Down Expand Up @@ -918,7 +918,7 @@ public override void OnDrawContent (Rectangle viewport)

if (newcolor != current)
{
Driver.SetAttribute (newcolor);
Driver?.SetAttribute (newcolor);
current = newcolor;
}

Expand All @@ -928,7 +928,7 @@ public override void OnDrawContent (Rectangle viewport)
{
for (var c = 0; c < f.Width; c++)
{
Driver.AddRune ((Rune)' ');
Driver?.AddRune ((Rune)' ');
}
}
else
Expand All @@ -939,16 +939,16 @@ public override void OnDrawContent (Rectangle viewport)
if (rowEventArgs.RowAttribute is { } && current != rowEventArgs.RowAttribute)
{
current = (Attribute)rowEventArgs.RowAttribute;
Driver.SetAttribute (current);
Driver?.SetAttribute (current);
}

if (AllowsMarking)
{
Driver.AddRune (
Driver?.AddRune (
Source.IsMarked (item) ? AllowsMultipleSelection ? Glyphs.CheckStateChecked : Glyphs.Selected :
AllowsMultipleSelection ? Glyphs.CheckStateUnChecked : Glyphs.UnSelected
);
Driver.AddRune ((Rune)' ');
Driver?.AddRune ((Rune)' ');
}

Source.Render (this, Driver, isSelected, item, col, row, f.Width - col, start);
Expand Down
2 changes: 1 addition & 1 deletion Terminal.Gui/Views/GraphView/GraphView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -344,5 +344,5 @@ public void Scroll (float offsetX, float offsetY)
/// Sets the color attribute of <see cref="Application.Driver"/> to the <see cref="GraphColor"/> (if defined) or
/// <see cref="ColorScheme"/> otherwise.
/// </summary>
public void SetDriverColorToGraphColor () { Driver.SetAttribute (GraphColor ?? GetNormalColor ()); }
public void SetDriverColorToGraphColor () { Driver?.SetAttribute (GraphColor ?? GetNormalColor ()); }
}
20 changes: 10 additions & 10 deletions Terminal.Gui/Views/HexView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -430,7 +430,7 @@ public override void OnDrawContent (Rectangle viewport)

Attribute currentAttribute;
Attribute current = GetFocusColor ();
Driver.SetAttribute (current);
Driver?.SetAttribute (current);
Move (0, 0);

int nBlocks = BytesPerLine / NUM_BYTES_PER_HEX_COLUMN;
Expand All @@ -452,13 +452,13 @@ public override void OnDrawContent (Rectangle viewport)

Move (0, line);
currentAttribute = new Attribute (GetNormalColor ().Foreground.GetHighlightColor (), GetNormalColor ().Background);
Driver.SetAttribute (currentAttribute);
Driver?.SetAttribute (currentAttribute);
var address = $"{_displayStart + line * nBlocks * NUM_BYTES_PER_HEX_COLUMN:x8}";
Driver.AddStr ($"{address.Substring (8 - AddressWidth)}");
Driver?.AddStr ($"{address.Substring (8 - AddressWidth)}");

if (AddressWidth > 0)
{
Driver.AddStr (" ");
Driver?.AddStr (" ");
}

SetAttribute (GetNormalColor ());
Expand All @@ -480,12 +480,12 @@ public override void OnDrawContent (Rectangle viewport)
SetAttribute (edited ? editedAttribute : GetNormalColor ());
}

Driver.AddStr (offset >= n && !edited ? " " : $"{value:x2}");
Driver?.AddStr (offset >= n && !edited ? " " : $"{value:x2}");
SetAttribute (GetNormalColor ());
Driver.AddRune (_spaceCharRune);
Driver?.AddRune (_spaceCharRune);
}

Driver.AddStr (block + 1 == nBlocks ? " " : $"{_columnSeparatorRune} ");
Driver?.AddStr (block + 1 == nBlocks ? " " : $"{_columnSeparatorRune} ");
}

for (var byteIndex = 0; byteIndex < nBlocks * NUM_BYTES_PER_HEX_COLUMN; byteIndex++)
Expand Down Expand Up @@ -538,12 +538,12 @@ public override void OnDrawContent (Rectangle viewport)
SetAttribute (edited ? editedAttribute : GetNormalColor ());
}

Driver.AddRune (c);
Driver?.AddRune (c);

for (var i = 1; i < utf8BytesConsumed; i++)
{
byteIndex++;
Driver.AddRune (_periodCharRune);
Driver?.AddRune (_periodCharRune);
}
}
}
Expand All @@ -553,7 +553,7 @@ void SetAttribute (Attribute attribute)
if (currentAttribute != attribute)
{
currentAttribute = attribute;
Driver.SetAttribute (attribute);
Driver?.SetAttribute (attribute);
}
}
}
Expand Down
10 changes: 5 additions & 5 deletions Terminal.Gui/Views/Line.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,29 +70,29 @@ public override void OnDrawContent (Rectangle viewport)

if (SuperViewRendersLineCanvas)
{
lc = SuperView.LineCanvas;
lc = SuperView?.LineCanvas;
}

if (SuperView is Adornment adornment)
{
lc = adornment.Parent.LineCanvas;
lc = adornment.Parent?.LineCanvas;
}

Point pos = ViewportToScreen (viewport).Location;
int length = Orientation == Orientation.Horizontal ? Frame.Width : Frame.Height;

if (SuperViewRendersLineCanvas && Orientation == Orientation.Horizontal)
if (SuperView is {} && SuperViewRendersLineCanvas && Orientation == Orientation.Horizontal)
{
pos.Offset (-SuperView.Border.Thickness.Left, 0);
length += SuperView.Border.Thickness.Horizontal;
}

if (SuperViewRendersLineCanvas && Orientation == Orientation.Vertical)
if (SuperView is { } && SuperViewRendersLineCanvas && Orientation == Orientation.Vertical)
{
pos.Offset (0, -SuperView.Border.Thickness.Top);
length += SuperView.Border.Thickness.Vertical;
}
lc.AddLine (
lc?.AddLine (
pos,
length,
Orientation,
Expand Down
4 changes: 2 additions & 2 deletions Terminal.Gui/Views/LineView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public override void OnDrawContent (Rectangle viewport)
base.OnDrawContent (viewport);

Move (0, 0);
Driver.SetAttribute (GetNormalColor ());
Driver?.SetAttribute (GetNormalColor ());

int hLineWidth = Math.Max (1, Glyphs.HLine.GetColumns ());

Expand Down Expand Up @@ -87,7 +87,7 @@ public override void OnDrawContent (Rectangle viewport)
rune = EndingAnchor ?? LineRune;
}

Driver.AddRune (rune);
Driver?.AddRune (rune);
}
}
}
14 changes: 7 additions & 7 deletions Terminal.Gui/Views/ListView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -752,8 +752,8 @@ public override void OnDrawContent (Rectangle viewport)
{
base.OnDrawContent (viewport);

Attribute current = ColorScheme.Focus;
Driver.SetAttribute (current);
Attribute current = ColorScheme?.Focus ?? Attribute.Default;
Driver?.SetAttribute (current);
Move (0, 0);
Rectangle f = Viewport;
int item = Viewport.Y;
Expand All @@ -770,7 +770,7 @@ public override void OnDrawContent (Rectangle viewport)

if (newcolor != current)
{
Driver.SetAttribute (newcolor);
Driver?.SetAttribute (newcolor);
current = newcolor;
}

Expand All @@ -780,7 +780,7 @@ public override void OnDrawContent (Rectangle viewport)
{
for (var c = 0; c < f.Width; c++)
{
Driver.AddRune ((Rune)' ');
Driver?.AddRune ((Rune)' ');
}
}
else
Expand All @@ -791,16 +791,16 @@ public override void OnDrawContent (Rectangle viewport)
if (rowEventArgs.RowAttribute is { } && current != rowEventArgs.RowAttribute)
{
current = (Attribute)rowEventArgs.RowAttribute;
Driver.SetAttribute (current);
Driver?.SetAttribute (current);
}

if (_allowsMarking)
{
Driver.AddRune (
Driver?.AddRune (
_source.IsMarked (item) ? AllowsMultipleSelection ? Glyphs.CheckStateChecked : Glyphs.Selected :
AllowsMultipleSelection ? Glyphs.CheckStateUnChecked : Glyphs.UnSelected
);
Driver.AddRune ((Rune)' ');
Driver?.AddRune ((Rune)' ');
}

Source.Render (this, Driver, isSelected, item, col, row, f.Width - col, start);
Expand Down
2 changes: 1 addition & 1 deletion Terminal.Gui/Views/Menu/MenuBar.cs
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ internal Menu? OpenCurrentMenu
/// <inheritdoc/>
public override void OnDrawContent (Rectangle viewport)
{
Driver.SetAttribute (GetNormalColor ());
Driver?.SetAttribute (GetNormalColor ());

Clear ();

Expand Down
12 changes: 6 additions & 6 deletions Terminal.Gui/Views/ProgressBar.cs
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ public override string Text
///<inheritdoc/>
public override void OnDrawContent (Rectangle viewport)
{
Driver.SetAttribute (GetHotNormalColor ());
Driver?.SetAttribute (GetHotNormalColor ());

Move (0, 0);

Expand All @@ -151,11 +151,11 @@ public override void OnDrawContent (Rectangle viewport)
{
if (Array.IndexOf (_activityPos, i) != -1)
{
Driver.AddRune (SegmentCharacter);
Driver?.AddRune (SegmentCharacter);
}
else
{
Driver.AddRune ((Rune)' ');
Driver?.AddRune ((Rune)' ');
}
}
}
Expand All @@ -166,12 +166,12 @@ public override void OnDrawContent (Rectangle viewport)

for (i = 0; (i < mid) & (i < Viewport.Width); i++)
{
Driver.AddRune (SegmentCharacter);
Driver?.AddRune (SegmentCharacter);
}

for (; i < Viewport.Width; i++)
{
Driver.AddRune ((Rune)' ');
Driver?.AddRune ((Rune)' ');
}
}

Expand All @@ -185,7 +185,7 @@ public override void OnDrawContent (Rectangle viewport)
attr = new Attribute (ColorScheme.HotNormal.Background, ColorScheme.HotNormal.Foreground);
}

tf?.Draw (
tf.Draw (
ViewportToScreen (Viewport),
attr,
ColorScheme.Normal,
Expand Down
8 changes: 4 additions & 4 deletions Terminal.Gui/Views/RadioGroup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,7 @@ public override void OnDrawContent (Rectangle viewport)
{
base.OnDrawContent (viewport);

Driver.SetAttribute (GetNormalColor ());
Driver?.SetAttribute (GetNormalColor ());

for (var i = 0; i < _radioLabels.Count; i++)
{
Expand All @@ -381,8 +381,8 @@ public override void OnDrawContent (Rectangle viewport)
}

string rl = _radioLabels [i];
Driver.SetAttribute (GetNormalColor ());
Driver.AddStr ($"{(i == _selected ? Glyphs.Selected : Glyphs.UnSelected)} ");
Driver?.SetAttribute (GetNormalColor ());
Driver?.AddStr ($"{(i == _selected ? Glyphs.Selected : Glyphs.UnSelected)} ");
TextFormatter.FindHotKey (rl, HotKeySpecifier, out int hotPos, out Key hotKey);

if (hotPos != -1 && hotKey != Key.Empty)
Expand Down Expand Up @@ -430,7 +430,7 @@ public override void OnDrawContent (Rectangle viewport)
}

Application.Driver?.AddRune (rune);
Driver.SetAttribute (GetNormalColor ());
Driver?.SetAttribute (GetNormalColor ());
}
}
else
Expand Down
14 changes: 9 additions & 5 deletions Terminal.Gui/Views/TabView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -292,15 +292,19 @@ public void EnsureSelectedTabIsVisible ()
/// <inheritdoc/>
public override void OnDrawContent (Rectangle viewport)
{
Driver.SetAttribute (GetNormalColor ());
Driver?.SetAttribute (GetNormalColor ());

if (Tabs.Any ())
{
Rectangle savedClip = SetClip ();
_tabsBar.OnDrawContent (viewport);
_contentView.SetNeedsDisplay ();
_contentView.Draw ();
Driver.Clip = savedClip;

if (Driver is { })
{
Driver.Clip = savedClip;
}
}
}

Expand Down Expand Up @@ -648,7 +652,7 @@ public override void OnDrawContent (Rectangle viewport)
RenderTabLine ();

RenderUnderline ();
Driver.SetAttribute (HasFocus ? GetFocusColor () : GetNormalColor ());
Driver?.SetAttribute (HasFocus ? GetFocusColor () : GetNormalColor ());
}

public override void OnDrawContentComplete (Rectangle viewport)
Expand Down Expand Up @@ -1271,7 +1275,7 @@ private void RenderTabLine ()

tab.OnDrawAdornments ();

Attribute prevAttr = Driver.GetAttribute ();
Attribute prevAttr = Driver?.GetAttribute () ?? Attribute.Default;

// if tab is the selected one and focus is inside this control
if (toRender.IsSelected && _host.HasFocus)
Expand All @@ -1296,7 +1300,7 @@ private void RenderTabLine ()

tab.OnRenderLineCanvas ();

Driver.SetAttribute (GetNormalColor ());
Driver?.SetAttribute (GetNormalColor ());
}
}

Expand Down
Loading

0 comments on commit 98a2265

Please sign in to comment.