Skip to content

Commit f7cb226

Browse files
committed
重构视图类,移除 IView 接口实现;优化视图生命周期管理逻辑
1 parent 5b73bbc commit f7cb226

File tree

7 files changed

+363
-472
lines changed

7 files changed

+363
-472
lines changed

src/LVGLSharp.Core/ViewLifetimeBase.cs

Lines changed: 66 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,75 @@
1+
using LVGLSharp.Interop;
2+
13
namespace LVGLSharp;
24

3-
internal abstract class ViewLifetimeBase
5+
public abstract unsafe class ViewLifetimeBase : IView
46
{
57
private bool _isOpen;
68
private bool _isDisposed;
79

10+
public void Open()
11+
{
12+
if (!TryBeginOpen())
13+
{
14+
return;
15+
}
16+
17+
try
18+
{
19+
OnOpenCore();
20+
}
21+
catch
22+
{
23+
MarkOpenFailed();
24+
throw;
25+
}
26+
}
27+
28+
public void RunLoop(Action iteration)
29+
{
30+
try
31+
{
32+
RunLoopCore(iteration);
33+
}
34+
finally
35+
{
36+
Close();
37+
}
38+
}
39+
40+
public void Close()
41+
{
42+
if (CanSkipClose())
43+
{
44+
TryBeginClose();
45+
return;
46+
}
47+
48+
if (!TryBeginClose())
49+
{
50+
return;
51+
}
52+
53+
OnCloseCore();
54+
}
55+
56+
public void Dispose()
57+
{
58+
Close();
59+
}
60+
61+
public abstract lv_obj_t* Root { get; }
62+
public abstract lv_group_t* KeyInputGroup { get; }
63+
public abstract delegate* unmanaged[Cdecl]<lv_event_t*, void> SendTextAreaFocusCallback { get; }
64+
public abstract void HandleEvents();
65+
public abstract void RegisterTextInput(lv_obj_t* textArea);
66+
67+
protected abstract void OnOpenCore();
68+
protected abstract void RunLoopCore(Action iteration);
69+
protected abstract void OnCloseCore();
70+
71+
protected virtual bool CanSkipClose() => false;
72+
873
protected void ThrowIfDisposed()
974
{
1075
if (_isDisposed)

src/LVGLSharp.Runtime.Linux/FrameBufferView.cs

Lines changed: 41 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
namespace LVGLSharp.Runtime.Linux
1313
{
14-
public unsafe class FrameBufferView : ViewLifetimeBase, IView
14+
public unsafe class FrameBufferView : ViewLifetimeBase
1515
{
1616
static lv_display_t* g_display;
1717
static lv_indev_t* g_indev;
@@ -23,9 +23,9 @@ public unsafe class FrameBufferView : ViewLifetimeBase, IView
2323
public static lv_group_t* KeyInputGroupObject { get; set; } = null;
2424
public static delegate* unmanaged[Cdecl]<lv_event_t*, void> SendTextAreaFocusCallbackCore { get; set; } = null;
2525

26-
public lv_obj_t* Root => RootObject;
27-
public lv_group_t* KeyInputGroup => KeyInputGroupObject;
28-
public delegate* unmanaged[Cdecl]<lv_event_t*, void> SendTextAreaFocusCallback => SendTextAreaFocusCallbackCore;
26+
public override lv_obj_t* Root => RootObject;
27+
public override lv_group_t* KeyInputGroup => KeyInputGroupObject;
28+
public override delegate* unmanaged[Cdecl]<lv_event_t*, void> SendTextAreaFocusCallback => SendTextAreaFocusCallbackCore;
2929

3030
private lv_font_t* _fallbackFont;
3131
private lv_style_t* _defaultFontStyle;
@@ -48,58 +48,45 @@ public FrameBufferView(string fbdev = "/dev/fb0", string indev = "/dev/input/eve
4848
_dpi = dpi;
4949
}
5050

51-
public void Open()
51+
protected override void OnOpenCore()
5252
{
53-
if (!TryBeginOpen())
54-
{
55-
return;
56-
}
57-
5853
if (g_running)
5954
{
6055
return;
6156
}
6257

63-
try
64-
{
65-
LvglNativeLibraryResolver.EnsureRegistered();
66-
g_running = true;
67-
startTick = Environment.TickCount;
68-
lv_init();
69-
lv_tick_set_cb(&my_tick);
70-
71-
g_display = lv_linux_fbdev_create();
72-
fixed (byte* ptr = Encoding.ASCII.GetBytes($"{_fbdev}\0"))
73-
lv_linux_fbdev_set_file(g_display, ptr);
74-
75-
fixed (byte* ptr = Encoding.ASCII.GetBytes($"{_indev}\0"))
76-
g_indev = lv_evdev_create(lv_indev_type_t.LV_INDEV_TYPE_POINTER, ptr);
77-
78-
RootObject = lv_scr_act();
79-
80-
_fallbackFont = lv_obj_get_style_text_font(RootObject, LV_PART_MAIN);
81-
82-
var systemFontPath = LinuxSystemFontResolver.TryResolveFontPath();
83-
if (!string.IsNullOrWhiteSpace(systemFontPath))
84-
{
85-
_fontManager = new SixLaborsFontManager(
86-
systemFontPath,
87-
12,
88-
_dpi,
89-
_fallbackFont,
90-
LvglHostDefaults.CreateDefaultFontFallbackGlyphs());
91-
92-
_defaultFontStyle = LvglHostDefaults.ApplyDefaultFontStyle(RootObject, _fontManager.GetLvFontPtr());
93-
}
94-
}
95-
catch
58+
LvglNativeLibraryResolver.EnsureRegistered();
59+
g_running = true;
60+
startTick = Environment.TickCount;
61+
lv_init();
62+
lv_tick_set_cb(&my_tick);
63+
64+
g_display = lv_linux_fbdev_create();
65+
fixed (byte* ptr = Encoding.ASCII.GetBytes($"{_fbdev}\0"))
66+
lv_linux_fbdev_set_file(g_display, ptr);
67+
68+
fixed (byte* ptr = Encoding.ASCII.GetBytes($"{_indev}\0"))
69+
g_indev = lv_evdev_create(lv_indev_type_t.LV_INDEV_TYPE_POINTER, ptr);
70+
71+
RootObject = lv_scr_act();
72+
73+
_fallbackFont = lv_obj_get_style_text_font(RootObject, LV_PART_MAIN);
74+
75+
var systemFontPath = LinuxSystemFontResolver.TryResolveFontPath();
76+
if (!string.IsNullOrWhiteSpace(systemFontPath))
9677
{
97-
MarkOpenFailed();
98-
throw;
78+
_fontManager = new SixLaborsFontManager(
79+
systemFontPath,
80+
12,
81+
_dpi,
82+
_fallbackFont,
83+
LvglHostDefaults.CreateDefaultFontFallbackGlyphs());
84+
85+
_defaultFontStyle = LvglHostDefaults.ApplyDefaultFontStyle(RootObject, _fontManager.GetLvFontPtr());
9986
}
10087
}
10188

102-
public void RegisterTextInput(lv_obj_t* textArea)
89+
public override void RegisterTextInput(lv_obj_t* textArea)
10390
{
10491
if (textArea == null)
10592
{
@@ -111,41 +98,23 @@ public void RegisterTextInput(lv_obj_t* textArea)
11198
lv_keyboard_set_textarea(keyboard, textArea);
11299
}
113100

114-
public void RunLoop(Action iteration)
101+
protected override void RunLoopCore(Action iteration)
115102
{
116-
try
103+
while (g_running)
117104
{
118-
while (g_running)
119-
{
120-
HandleEvents();
121-
iteration?.Invoke();
122-
Thread.Sleep(5);
123-
}
124-
}
125-
finally
126-
{
127-
Close();
105+
HandleEvents();
106+
iteration?.Invoke();
107+
Thread.Sleep(5);
128108
}
129109
}
130110

131-
public void HandleEvents()
111+
public override void HandleEvents()
132112
{
133113
lv_timer_handler();
134114
}
135115

136-
public void Close()
116+
protected override void OnCloseCore()
137117
{
138-
if (!g_running && g_display == null && g_indev == null && RootObject == null)
139-
{
140-
TryBeginClose();
141-
return;
142-
}
143-
144-
if (!TryBeginClose())
145-
{
146-
return;
147-
}
148-
149118
g_running = false;
150119

151120
if (g_indev != null)
@@ -167,9 +136,6 @@ public void Close()
167136
SendTextAreaFocusCallbackCore = null;
168137
}
169138

170-
public void Dispose()
171-
{
172-
Close();
173-
}
139+
protected override bool CanSkipClose() => !g_running && g_display == null && g_indev == null && RootObject == null;
174140
}
175141
}

src/LVGLSharp.Runtime.Linux/LinuxView.cs

Lines changed: 11 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
namespace LVGLSharp.Runtime.Linux;
66

7-
public unsafe class LinuxView : ViewLifetimeBase, IView
7+
public unsafe class LinuxView : ViewLifetimeBase
88
{
99
private readonly IView _inner;
1010
private readonly LinuxHostEnvironment _environment;
@@ -30,9 +30,9 @@ public LinuxView(string title = "LVGLSharp Linux", int width = 800, int height =
3030
s_activeView = this;
3131
}
3232

33-
public lv_obj_t* Root => _inner.Root;
34-
public lv_group_t* KeyInputGroup => _inner.KeyInputGroup;
35-
public delegate* unmanaged[Cdecl]<lv_event_t*, void> SendTextAreaFocusCallback => _inner.SendTextAreaFocusCallback;
33+
public override lv_obj_t* Root => _inner.Root;
34+
public override lv_group_t* KeyInputGroup => _inner.KeyInputGroup;
35+
public override delegate* unmanaged[Cdecl]<lv_event_t*, void> SendTextAreaFocusCallback => _inner.SendTextAreaFocusCallback;
3636
public static (int X, int Y) CurrentMousePosition => s_activeView?._inner switch
3737
{
3838
X11View => X11View.CurrentMousePosition,
@@ -49,51 +49,30 @@ public LinuxView(string title = "LVGLSharp Linux", int width = 800, int height =
4949
_ => 0U,
5050
};
5151

52-
public void Open()
52+
protected override void OnOpenCore()
5353
{
54-
if (!TryBeginOpen())
55-
{
56-
return;
57-
}
58-
59-
try
60-
{
61-
_inner.Open();
62-
}
63-
catch
64-
{
65-
MarkOpenFailed();
66-
throw;
67-
}
54+
_inner.Open();
6855
}
6956

70-
public void HandleEvents()
57+
public override void HandleEvents()
7158
{
7259
_inner.HandleEvents();
7360
}
7461

75-
public void RunLoop(Action iteration)
62+
protected override void RunLoopCore(Action iteration)
7663
{
7764
_inner.RunLoop(iteration);
7865
}
7966

80-
public void Close()
67+
protected override void OnCloseCore()
8168
{
82-
if (!TryBeginClose())
83-
{
84-
return;
85-
}
86-
8769
_inner.Close();
8870
}
8971

90-
public void RegisterTextInput(lv_obj_t* textArea)
72+
public override void RegisterTextInput(lv_obj_t* textArea)
9173
{
9274
_inner.RegisterTextInput(textArea);
9375
}
9476

95-
public void Dispose()
96-
{
97-
Close();
98-
}
77+
protected override bool CanSkipClose() => false;
9978
}

0 commit comments

Comments
 (0)