Skip to content

Commit cd384e9

Browse files
committed
fix(basetype): encapslue
changewin handler fix typo and inherits base win need be public set win postion to base base win rename mWIndow to MyWIndow re rename remove basewin close method
1 parent f7ab6bb commit cd384e9

File tree

7 files changed

+43
-48
lines changed

7 files changed

+43
-48
lines changed

Core/Editor/Base/BaseWinKeyHandler.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
namespace PCP.WhichKey.Core
1+
namespace PCP.WhichKey.Types
22
{
33
public abstract class BaseWinKeyHandler
44
{
55
public abstract void ShowWindow();
66
public virtual void OnActive() { }
7-
public abstract void CloseWindow();
87
public abstract void HandleKey(int key);
98
}
109
}

Core/Editor/Base/DepthKeyHandler.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@ public override sealed void HandleKey(int key)
1212
mKeySeq.Push(key);
1313
HandleKeyWithDepth(key);
1414
if (maxDepth > 0 && CheckDepth())
15-
mWindow.Close();
15+
window.Close();
1616
else
1717
{
1818
UpdateWindow();
19-
mWindow.UpdateHints();
19+
window.UpdateHints();
2020
}
2121
}
2222

Core/Editor/Base/WindowKeyHandler.cs

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,26 +5,22 @@ namespace PCP.WhichKey.Types
55
{
66
public abstract class WindowKeyHandler<T> : BaseWinKeyHandler where T : WkBaseWindow
77
{
8-
protected T mWindow;
8+
protected T window;
99

1010
public override void ShowWindow()
1111
{
12-
if (mWindow == null)
12+
if (window == null)
1313
{
14-
mWindow = ScriptableObject.CreateInstance<T>();
14+
window = ScriptableObject.CreateInstance<T>();
1515
}
1616

17-
mWindow.OnActive();
18-
mWindow.UpdateDelayTimer();
17+
window.OnActive();
18+
window.UpdateDelayTimer();
1919

20-
mWindow.ShowPopup();
21-
mWindow.minSize = new Vector2(0, 0);
22-
mWindow.position = new Rect(0, 0, 0, 0);
20+
window.ShowPopup();
21+
window.minSize = new Vector2(0, 0);
22+
window.position = new Rect(0, 0, 0, 0);
2323
}
2424

25-
public override void CloseWindow()
26-
{
27-
mWindow.Close();
28-
}
2925
}
3026
}

Core/Editor/Base/WkBaseWindow.cs

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,16 @@ namespace PCP.WhichKey.Types
77
public abstract class WkBaseWindow : EditorWindow
88
{
99
//FIXME
10-
protected float DefaultTimeoutLen => WhichKeyPreferences.instance.Timeout;
10+
private static WhichKeyPreferences pref => WhichKeyPreferences.instance;
1111
private bool keyReleased = true;
1212
private KeyCode prevKey;
1313
private float hideTill;
1414
private bool showHint;
1515
private bool _changeUI;
1616
private bool needClose;
17-
protected float mWidth;
18-
protected float mHeight;
17+
protected float DefaultTimeoutLen => pref.Timeout;
18+
protected float winWidth;
19+
protected float winHeight;
1920
protected float timeoutLen;
2021
public virtual void OnActive()
2122
{
@@ -129,6 +130,18 @@ private void CheckDelayTimer()
129130
Repaint();
130131
}
131132
}
133+
protected void SetWindowPosition()
134+
{
135+
if (pref.WindowFollowMouse)
136+
{
137+
Vector2 mousePos = GUIUtility.GUIToScreenPoint(Event.current.mousePosition);
138+
position = new Rect(mousePos.x - winWidth / 2, mousePos.y - winHeight / 2, winWidth, winHeight);
139+
}
140+
else
141+
{
142+
position = new Rect(pref.FixedPosition.x, pref.FixedPosition.y, winWidth, winHeight);
143+
}
144+
}
132145

133146
private void OnLostFocus() => base.Close();
134147
public new void Close() => needClose = true;

Core/Editor/Commands/ChangeWinHandlerCmd.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
using PCP.WhichKey.Core;
22
namespace PCP.WhichKey.Types
33
{
4-
public abstract class ChangeWinHandlerCmd
4+
public abstract class ChangeWinHandlerCmd:WKCommand
55
{
66
public abstract BaseWinKeyHandler Handler { get; }
77
public virtual bool isEnd => true;
8-
public void Excute()
8+
public void Execute()
99
{
1010
OnActive();
1111
WhichKeyManager.instance.ChangeWinKeyHandler(Handler);

Core/Editor/Main/TreeHandler.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public void ProcessKey(int Key)
4949
if (kn == null)
5050
{
5151
WkLogger.LogInfo($"KeySeq {mKeyLabel} not found");
52-
mWindow.Close();
52+
window.Close();
5353
}
5454
else if (kn.Type == 0)
5555
{
@@ -62,7 +62,7 @@ public void ProcessKey(int Key)
6262
if (cmd == null)
6363
{
6464
WkLogger.LogError($"KeySeq {mKeyLabel} has no command");
65-
mWindow.Close();
65+
window.Close();
6666
return;
6767
}
6868
try
@@ -73,14 +73,14 @@ public void ProcessKey(int Key)
7373
{
7474
WkLogger.LogError($"KeySeq {mKeyLabel} execute failed\n{e}");
7575
}
76-
if (cmd.isEnd) mWindow.Close();
76+
if (cmd.isEnd) window.Close();
7777
}
7878
}
7979

8080
protected override void UpdateWindow()
8181
{
82-
mWindow.Hints = GetLayerHints();
83-
mWindow.Title = GetLayerName();
82+
window.Hints = GetLayerHints();
83+
window.Title = GetLayerName();
8484
}
8585

8686
public LayerHint[] GetLayerHints()
@@ -177,7 +177,7 @@ public void ChangeHandler(IWkHandler handler, int depth)
177177
mCurrentHandler = handler;
178178
if (depth > 0)
179179
maxDepth = mKeySeq.Count + depth;
180-
mWindow.OverreideSets(handler.Timeout, handler.ColWidth);
180+
window.OverreideSets(handler.Timeout, handler.ColWidth);
181181
// if (handler is IWkWinModifier modifer)
182182
// {
183183
// if (mWindow == null)

Core/Editor/Windows/MainHintsWindow.cs

Lines changed: 8 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@ internal class MainHintsWindow : WkBaseWindow
1616
//Maybe a structure
1717
//OPT
1818
internal static float lineHeight;
19-
private static bool followMouse;
20-
private static Vector2 fixedPosition;
2119
private static int maxHintLines;
2220
private static float maxColWidth;
2321
private static VisualTreeAsset hintLabel;
@@ -51,8 +49,6 @@ public static void Init()
5149

5250
var pref = WhichKeyPreferences.instance;
5351
var uil = UILoader.instance;
54-
followMouse = pref.WindowFollowMouse;
55-
fixedPosition = pref.FixedPosition;
5652
maxHintLines = pref.MaxHintLines;
5753
maxColWidth = pref.ColWidth;
5854
hintLabel = uil.HintLabel;
@@ -102,23 +98,23 @@ protected override void ShowHints()
10298
labelFrame.Clear();
10399
if (Hints.Length == 0)
104100
{
105-
mHeight = lineHeight + 2 * mainFrame.resolvedStyle.paddingTop;
106-
mWidth = mColWidth + mainFrame.resolvedStyle.paddingLeft * 2;
107-
maxSize = new Vector2(mWidth, mHeight);
101+
winHeight = lineHeight + 2 * mainFrame.resolvedStyle.paddingTop;
102+
winWidth = mColWidth + mainFrame.resolvedStyle.paddingLeft * 2;
103+
maxSize = new Vector2(winWidth, winHeight);
108104
}
109105
else
110106
{
111-
mHeight = lineHeight * (maxHintLines + 1) + 2 * mainFrame.resolvedStyle.paddingTop;
107+
winHeight = lineHeight * (maxHintLines + 1) + 2 * mainFrame.resolvedStyle.paddingTop;
112108
var cols = Mathf.CeilToInt((float)Hints.Length / maxHintLines);
113-
mWidth = cols * mColWidth + mainFrame.resolvedStyle.paddingLeft * 2;
114-
maxSize = new Vector2(mWidth, mHeight);
109+
winWidth = cols * mColWidth + mainFrame.resolvedStyle.paddingLeft * 2;
110+
maxSize = new Vector2(winWidth, winHeight);
115111

116112
for (int j = 0; j < cols; j++)
117113
{
118114
var col = new VisualElement();
119115
col.style.flexDirection = FlexDirection.Column;
120116
col.style.width = mColWidth;
121-
col.style.height = mHeight;
117+
col.style.height = winHeight;
122118
for (int i = 0; i < maxHintLines; i++)
123119
{
124120
int ind = i + j * maxHintLines;
@@ -136,16 +132,7 @@ protected override void ShowHints()
136132
labelFrame.Add(col);
137133
}
138134
}
139-
140-
if (followMouse)
141-
{
142-
Vector2 mousePos = GUIUtility.GUIToScreenPoint(Event.current.mousePosition);
143-
position = new Rect(mousePos.x - mWidth / 2, mousePos.y - mHeight / 2, mWidth, mHeight);
144-
}
145-
else
146-
{
147-
position = new Rect(fixedPosition.x, fixedPosition.y, mWidth, mHeight);
148-
}
135+
SetWindowPosition();
149136
}
150137
}
151138
}

0 commit comments

Comments
 (0)