Skip to content

Commit 1a88b64

Browse files
committed
Adjust Arguments tooltip according to function position
1 parent b0e6c8c commit 1a88b64

File tree

4 files changed

+37
-8
lines changed

4 files changed

+37
-8
lines changed

Source/ExcelDna.IntelliSense/IntelliSenseDisplay.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,7 @@ void FormulaEditTextChange(string formulaPrefix, Rect editWindowBounds, IntPtr e
293293
FunctionInfo functionInfo;
294294
if (_functionInfoMap.TryGetValue(functionName, out functionInfo))
295295
{
296+
var lineBeforeFunctionName = FormulaParser.GetLineBeforeFunctionName(formulaPrefix, functionName);
296297
// We have a function name and we want to show info
297298
if (_argumentsToolTip != null)
298299
{
@@ -308,8 +309,8 @@ void FormulaEditTextChange(string formulaPrefix, Rect editWindowBounds, IntPtr e
308309
// }
309310
//}
310311
int topOffset = GetTopOffset(excelToolTipWindow);
311-
var infoText = GetFunctionIntelliSense(functionInfo, currentArgIndex);
312-
_argumentsToolTip.ShowToolTip(infoText, (int)editWindowBounds.Left, (int)editWindowBounds.Bottom + 5, topOffset);
312+
FormattedText infoText = GetFunctionIntelliSense(functionInfo, currentArgIndex);
313+
_argumentsToolTip.ShowToolTip(infoText, lineBeforeFunctionName, (int)editWindowBounds.Left, (int)editWindowBounds.Bottom + 5, topOffset);
313314
}
314315
else
315316
{
@@ -384,6 +385,7 @@ void FunctionListSelectedItemChange(string selectedItemText, Rect selectedItemBo
384385
{
385386
_descriptionToolTip.ShowToolTip(
386387
text: new FormattedText { GetFunctionDescriptionOrNull(functionInfo) },
388+
linePrefix: null,
387389
left: (int)listBounds.Right + DescriptionLeftMargin,
388390
top: (int)selectedItemBounds.Bottom - 18,
389391
topOffset: 0,

Source/ExcelDna.IntelliSense/ToolTipForm.cs

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ namespace ExcelDna.IntelliSense
1414
class ToolTipForm : Form
1515
{
1616
FormattedText _text;
17+
int _linePrefixWidth;
1718
System.ComponentModel.IContainer components;
1819
Win32Window _owner;
1920
// Help Link
@@ -110,10 +111,12 @@ void ShowToolTip()
110111
}
111112
}
112113

113-
public void ShowToolTip(FormattedText text, int left, int top, int topOffset, int? listLeft = null)
114+
public void ShowToolTip(FormattedText text, string linePrefix, int left, int top, int topOffset, int? listLeft = null)
114115
{
115116
Debug.Print($"@@@ ShowToolTip - Old TopOffset: {_topOffset}, New TopOffset: {topOffset}");
116117
_text = text;
118+
_linePrefixWidth = MeasureFormulaStringWidth(linePrefix);
119+
left += _linePrefixWidth;
117120
if (left != _showLeft || top != _showTop || topOffset != _topOffset || listLeft != _listLeft)
118121
{
119122
// Update the start position and the current position
@@ -126,21 +129,22 @@ public void ShowToolTip(FormattedText text, int left, int top, int topOffset, in
126129
}
127130
if (!Visible)
128131
{
129-
Debug.Print($"ShowToolTip - Showing ToolTipForm: {_text.ToString()}");
132+
Debug.Print($"ShowToolTip - Showing ToolTipForm: {linePrefix} => {_text.ToString()}");
130133
// Make sure we're in the right position before we're first shown
131134
SetBounds(_currentLeft, _currentTop + _topOffset, 0, 0);
132135
ShowToolTip();
133136
}
134137
else
135138
{
136-
Debug.Print($"ShowToolTip - Invalidating ToolTipForm: {_text.ToString()}");
139+
Debug.Print($"ShowToolTip - Invalidating ToolTipForm: {linePrefix} => {_text.ToString()}");
137140
Invalidate();
138141
}
139142
}
140143

141144
public void MoveToolTip(int left, int top, int topOffset, int? listLeft = null)
142145
{
143146
Debug.Print($"@@@ MoveToolTip - Old TopOffset: {_topOffset}, New TopOffset: {topOffset}");
147+
left += _linePrefixWidth;
144148
// We might consider checking the new position against earlier mouse movements
145149
_currentLeft = left;
146150
_currentTop = top;
@@ -175,6 +179,17 @@ public IntPtr OwnerHandle
175179
}
176180
}
177181

182+
// TODO: Move or clean up or something...
183+
Font StandardFont = new Font("Calibri", 11);
184+
int MeasureFormulaStringWidth(string formulaString)
185+
{
186+
if (string.IsNullOrEmpty(formulaString))
187+
return 0;
188+
189+
var size = TextRenderer.MeasureText(formulaString, StandardFont);
190+
return size.Width;
191+
}
192+
178193
#region Mouse Handling
179194

180195
void MouseButtonDown(Point screenLocation)
@@ -329,6 +344,7 @@ protected override void OnPaint(PaintEventArgs e)
329344
}
330345
var width = lineWidths.Max() + widthPadding;
331346
var height = totalHeight + heightPadding;
347+
332348
UpdateLocation(width, height);
333349
DrawRoundedRectangle(e.Graphics, new RectangleF(0,0, Width - 1, Height - 1), 2, 2);
334350
}

Source/ExcelDna.IntelliSense/UIMonitor/FormulaEditWatcher.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public StateChangeEventArgs(StateChangeType? stateChangeType = null)
4545
// We don't really care whether it is the formula bar or in-cell,
4646
// we just need to get the right window handle
4747
public Rect EditWindowBounds { get; set; }
48-
public Point CaretPosition { get; set; }
48+
4949
public IntPtr FormulaEditWindow
5050
{
5151
get
@@ -406,7 +406,7 @@ void UpdateEditState(bool moveOnly = false)
406406
IntPtr hwnd = (IntPtr)(int)focusedEdit.GetCurrentPropertyValue(AutomationElement.NativeWindowHandleProperty);
407407

408408
var pt = Win32Helper.GetClientCursorPos(hwnd);
409-
CaretPosition = new Point(pt.X, pt.Y);
409+
410410
if (!IsEditingFormula)
411411
InstallLocationMonitor(GetTopLevelWindow(focusedEdit));
412412
IsEditingFormula = true;
@@ -417,7 +417,7 @@ void UpdateEditState(bool moveOnly = false)
417417
CurrentPrefix = newPrefix;
418418
prefixChanged = true;
419419
}
420-
Logger.WindowWatcher.Verbose($"FormulaEdit UpdateEditState Formula editing: CurrentPrefix {CurrentPrefix}, EditWindowBounds: {EditWindowBounds}, CaretPosition {CaretPosition}");
420+
Logger.WindowWatcher.Verbose($"FormulaEdit UpdateEditState Formula editing: CurrentPrefix {CurrentPrefix}, EditWindowBounds: {EditWindowBounds}");
421421
}
422422

423423
// TODO: Smarter notification...?

Source/ExcelDna.IntelliSense/UIMonitor/FormulaParser.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,5 +77,16 @@ internal static bool TryGetFormulaInfo(string formulaPrefix, out string function
7777
currentArgIndex = -1;
7878
return false;
7979
}
80+
81+
internal static string GetLineBeforeFunctionName(string formulaPrefix, string functionName)
82+
{
83+
// TODO: This should be cleaned up - just added something for testing...
84+
int lastFuncStart = formulaPrefix.LastIndexOf(functionName);
85+
if (lastFuncStart < 0)
86+
return "";
87+
88+
int lastLineStart = formulaPrefix.LastIndexOf('\n', lastFuncStart) + 1;
89+
return formulaPrefix.Substring(lastLineStart, lastFuncStart - lastLineStart);
90+
}
8091
}
8192
}

0 commit comments

Comments
 (0)