forked from canton7/SyncTrayzor
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add legacy mode option to fix tooltip positioning issue
Relates to: canton7#398 canton7#760
- Loading branch information
Showing
10 changed files
with
132 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
using System; | ||
using System.Windows; | ||
using System.Reflection; | ||
using Hardcodet.Wpf.TaskbarNotification; | ||
using Hardcodet.Wpf.TaskbarNotification.Interop; | ||
|
||
namespace SyncTrayzor.Utils | ||
{ | ||
public static class TrayToolTipWorkaround { | ||
private static bool hasFixed = false; | ||
private static bool legacyMode = false; | ||
|
||
private static void setTipTextMode(TaskbarIcon taskbarIcon) { | ||
if (!hasFixed || taskbarIcon == null) { | ||
return; | ||
} | ||
|
||
if (taskbarIcon.TrayToolTip == null && taskbarIcon.TrayToolTipResolved != null) { | ||
// icon data | ||
FieldInfo iconDataField = typeof(TaskbarIcon).GetField("iconData", BindingFlags.NonPublic | BindingFlags.Instance); | ||
if (iconDataField == null) { | ||
return; | ||
} | ||
|
||
NotifyIconData iconData = (NotifyIconData) iconDataField.GetValue(taskbarIcon); | ||
IconDataMembers flags = IconDataMembers.Tip; | ||
if (legacyMode) { | ||
flags |= IconDataMembers.UseLegacyToolTips; | ||
} | ||
if (iconData.ValidMembers != flags) { | ||
iconData.ValidMembers = flags; | ||
|
||
// get util type | ||
Type utilType = typeof(TaskbarIcon).GetTypeInfo().Assembly.GetType("Hardcodet.Wpf.TaskbarNotification.Util"); | ||
if (utilType == null) | ||
{ | ||
return; | ||
} | ||
|
||
// get write method | ||
MethodInfo writeIconDataMethod = utilType.GetMethod("WriteIconData", new Type[] { typeof(NotifyIconData).MakeByRefType(), typeof(NotifyCommand) }); | ||
if (writeIconDataMethod == null) | ||
{ | ||
return; | ||
} | ||
|
||
writeIconDataMethod.Invoke(null, new object[] { iconData, NotifyCommand.Modify }); | ||
} | ||
} | ||
} | ||
|
||
private static void doFix(TaskbarIcon taskbarIcon) { | ||
// register new DependencyProperty | ||
DependencyProperty newProperty = DependencyProperty.Register("NewToolTipText", typeof(string), typeof(TaskbarIcon), | ||
new FrameworkPropertyMetadata(string.Empty, (d, e) => { | ||
// call the origin callback func | ||
MethodInfo originCbMethod = typeof(TaskbarIcon).GetMethod("ToolTipTextPropertyChanged", BindingFlags.Static | BindingFlags.NonPublic); | ||
originCbMethod?.Invoke(null, new object[] { d, e }); | ||
// try to refresh legacy mode | ||
TaskbarIcon t = d as TaskbarIcon; | ||
setTipTextMode(t); | ||
}) | ||
); | ||
|
||
// back up tip text | ||
string tipTextBackup = string.Empty; | ||
if (taskbarIcon != null && !string.IsNullOrEmpty(taskbarIcon.ToolTipText)) { | ||
tipTextBackup = taskbarIcon.ToolTipText; | ||
} | ||
|
||
// set new ToolTipTextProperty, every time update ToolTipText, will run own func | ||
var propertyFieldInfo = typeof(TaskbarIcon).GetField("ToolTipTextProperty", BindingFlags.Static | BindingFlags.Public); | ||
propertyFieldInfo.SetValue(null, newProperty); | ||
|
||
// set tip text | ||
if (taskbarIcon != null) { | ||
taskbarIcon.ToolTipText = tipTextBackup; | ||
} | ||
} | ||
|
||
public static void SetLegacyMode(bool _legacyMode, TaskbarIcon taskbarIcon) | ||
{ | ||
if(_legacyMode && !hasFixed) { | ||
doFix(taskbarIcon); | ||
hasFixed = true; | ||
} | ||
legacyMode = _legacyMode; | ||
setTipTextMode(taskbarIcon); | ||
} | ||
} | ||
} |