Skip to content

Commit ce8dad9

Browse files
authored
Merge pull request #2580 from cwensley/curtis/mac-fix-gridview-tooltips-with-background-color
Mac: Fix Tree/GridView header tooltips when setting the background color
2 parents 7cafd8a + 6f4e4e7 commit ce8dad9

File tree

2 files changed

+32
-59
lines changed

2 files changed

+32
-59
lines changed

src/Eto.Mac/Forms/Controls/GridHandler.cs

+31-58
Original file line numberDiff line numberDiff line change
@@ -71,57 +71,23 @@ public override void MouseDown(NSEvent theEvent)
7171
}
7272
}
7373

74-
class EtoTableHeaderViewWithBackground : EtoTableHeaderView
74+
class EtoBackgroundView : NSView
7575
{
76+
[Export("backgroundColor")]
7677
public NSColor BackgroundColor { get; set; }
78+
79+
public EtoBackgroundView()
80+
{
81+
}
82+
83+
public EtoBackgroundView(NativeHandle handle) : base(handle)
84+
{
85+
}
7786

7887
public override void DrawRect(CGRect dirtyRect)
7988
{
80-
if (BackgroundColor == null)
81-
{
82-
base.DrawRect(dirtyRect);
83-
return;
84-
}
85-
86-
// gotta draw header cells manually to get a custom background color without tinting...
87-
var bounds = Bounds;
8889
BackgroundColor.SetFill();
89-
NSBezierPath.FillRect(bounds);
90-
91-
NSBezierPath path;
92-
nfloat? position = null;
93-
var dividerSize = ConvertSizeToBacking(new CGSize(1, 1));
94-
var spacing = TableView.IntercellSpacing.Width;
95-
var columns = TableView.TableColumns();
96-
for (int i = 0; i < columns.Length; i++)
97-
{
98-
var cellFrame = GetHeaderRect(i);
99-
var col = columns[i];
100-
var cell = col.HeaderCell;
101-
if (col.Hidden || cell == null)
102-
continue;
103-
cell.DrawWithFrame(cellFrame, this);
104-
if (position == null)
105-
{
106-
// draw separator up to first column
107-
NSColor.Separator.Set();
108-
path = new NSBezierPath();
109-
path.MoveTo(new CGPoint(bounds.X, bounds.Bottom));
110-
path.LineTo(new CGPoint(cellFrame.X, bounds.Bottom));
111-
path.LineWidth = dividerSize.Height;
112-
path.Stroke();
113-
path.Dispose();
114-
}
115-
position = cellFrame.Right;
116-
}
117-
// draw separator from last column
118-
NSColor.Separator.Set();
119-
path = new NSBezierPath();
120-
path.MoveTo(new CGPoint(position ?? bounds.X, bounds.Bottom));
121-
path.LineTo(new CGPoint(bounds.Right, bounds.Bottom));
122-
path.LineWidth = dividerSize.Height;
123-
path.Stroke();
124-
path.Dispose();
90+
NSBezierPath.FillRect(Bounds);
12591
}
12692
}
12793

@@ -530,10 +496,7 @@ public bool ShowHeader
530496
{
531497
if (value && Control.HeaderView == null)
532498
{
533-
if (HasBackgroundColor && !UseNSBoxBackgroundColor)
534-
headerView = new EtoTableHeaderViewWithBackground { Handler = this, BackgroundColor = BackgroundColor.ToNSUI(), Menu = ContextMenu.ToNS() };
535-
else
536-
headerView = new EtoTableHeaderView { Handler = this, Menu = ContextMenu.ToNS() };
499+
headerView = new EtoTableHeaderView { Handler = this, Menu = ContextMenu.ToNS() };
537500
Control.HeaderView = headerView;
538501
}
539502
else if (!value && Control.HeaderView != null)
@@ -975,17 +938,27 @@ protected override void SetBackgroundColor(Color? color)
975938
Control.BackgroundColor = bg;
976939
if (!UseNSBoxBackgroundColor)
977940
{
978-
var currentHeader = Control.HeaderView;
979-
if (currentHeader is EtoTableHeaderViewWithBackground backgroundHeaderView)
980-
{
981-
backgroundHeaderView.BackgroundColor = bg;
982-
backgroundHeaderView.SetNeedsDisplay();
983-
}
984-
else if (currentHeader != null)
941+
var clip = ScrollView.Subviews.OfType<NSClipView>().FirstOrDefault(r => r.DocumentView == headerView);
942+
var banner = clip?.Subviews.FirstOrDefault(r => r.Class.Name == "NSBannerView");
943+
var effectView = banner?.Subviews.OfType<NSVisualEffectView>().FirstOrDefault();
944+
if (effectView == null || banner == null)
945+
return;
946+
947+
// inject a view above the effectView with our desired background color
948+
var backgroundView = banner.Subviews.OfType<EtoBackgroundView>().FirstOrDefault();
949+
if (backgroundView == null)
985950
{
986-
headerView = new EtoTableHeaderViewWithBackground { Handler = this, BackgroundColor = bg, Menu = ContextMenu.ToNS() };
987-
Control.HeaderView = headerView;
951+
backgroundView = new EtoBackgroundView
952+
{
953+
Frame = banner.Bounds,
954+
AutoresizingMask = NSViewResizingMask.WidthSizable | NSViewResizingMask.HeightSizable,
955+
TranslatesAutoresizingMaskIntoConstraints = true
956+
};
957+
banner.AddSubview(backgroundView, NSWindowOrderingMode.Above, effectView);
988958
}
959+
960+
backgroundView.BackgroundColor = bg;
961+
backgroundView.SetNeedsDisplay();
989962
}
990963
}
991964

0 commit comments

Comments
 (0)