Skip to content

Commit 5b73bbc

Browse files
committed
重构视图类,添加 ViewLifetimeBase 基类以管理视图的生命周期;优化 Open 和 Close 方法的逻辑
1 parent abcb047 commit 5b73bbc

File tree

7 files changed

+295
-183
lines changed

7 files changed

+295
-183
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
namespace LVGLSharp;
2+
3+
internal abstract class ViewLifetimeBase
4+
{
5+
private bool _isOpen;
6+
private bool _isDisposed;
7+
8+
protected void ThrowIfDisposed()
9+
{
10+
if (_isDisposed)
11+
{
12+
throw new ObjectDisposedException(GetType().Name);
13+
}
14+
}
15+
16+
protected bool TryBeginOpen()
17+
{
18+
ThrowIfDisposed();
19+
if (_isOpen)
20+
{
21+
return false;
22+
}
23+
24+
_isOpen = true;
25+
return true;
26+
}
27+
28+
protected bool TryBeginClose()
29+
{
30+
if (_isDisposed)
31+
{
32+
return false;
33+
}
34+
35+
_isDisposed = true;
36+
_isOpen = false;
37+
return true;
38+
}
39+
40+
protected void MarkOpenFailed()
41+
{
42+
_isOpen = false;
43+
}
44+
45+
protected bool IsDisposed => _isDisposed;
46+
protected bool IsOpen => _isOpen;
47+
}

src/LVGLSharp.Runtime.Linux/FrameBufferView.cs

Lines changed: 58 additions & 32 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 : IView
14+
public unsafe class FrameBufferView : ViewLifetimeBase, IView
1515
{
1616
static lv_display_t* g_display;
1717
static lv_indev_t* g_indev;
@@ -50,39 +50,52 @@ public FrameBufferView(string fbdev = "/dev/fb0", string indev = "/dev/input/eve
5050

5151
public void Open()
5252
{
53-
if (g_running)
53+
if (!TryBeginOpen())
5454
{
5555
return;
5656
}
5757

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);
58+
if (g_running)
59+
{
60+
return;
61+
}
7462

75-
var systemFontPath = LinuxSystemFontResolver.TryResolveFontPath();
76-
if (!string.IsNullOrWhiteSpace(systemFontPath))
63+
try
7764
{
78-
_fontManager = new SixLaborsFontManager(
79-
systemFontPath,
80-
12,
81-
_dpi,
82-
_fallbackFont,
83-
LvglHostDefaults.CreateDefaultFontFallbackGlyphs());
84-
85-
_defaultFontStyle = LvglHostDefaults.ApplyDefaultFontStyle(RootObject, _fontManager.GetLvFontPtr());
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
96+
{
97+
MarkOpenFailed();
98+
throw;
8699
}
87100
}
88101

@@ -100,11 +113,18 @@ public void RegisterTextInput(lv_obj_t* textArea)
100113

101114
public void RunLoop(Action iteration)
102115
{
103-
while (g_running)
116+
try
117+
{
118+
while (g_running)
119+
{
120+
HandleEvents();
121+
iteration?.Invoke();
122+
Thread.Sleep(5);
123+
}
124+
}
125+
finally
104126
{
105-
HandleEvents();
106-
iteration?.Invoke();
107-
Thread.Sleep(5);
127+
Close();
108128
}
109129
}
110130

@@ -116,6 +136,12 @@ public void HandleEvents()
116136
public void Close()
117137
{
118138
if (!g_running && g_display == null && g_indev == null && RootObject == null)
139+
{
140+
TryBeginClose();
141+
return;
142+
}
143+
144+
if (!TryBeginClose())
119145
{
120146
return;
121147
}

src/LVGLSharp.Runtime.Linux/LinuxView.cs

Lines changed: 21 additions & 3 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 : IView
7+
public unsafe class LinuxView : ViewLifetimeBase, IView
88
{
99
private readonly IView _inner;
1010
private readonly LinuxHostEnvironment _environment;
@@ -51,7 +51,20 @@ public LinuxView(string title = "LVGLSharp Linux", int width = 800, int height =
5151

5252
public void Open()
5353
{
54-
_inner.Open();
54+
if (!TryBeginOpen())
55+
{
56+
return;
57+
}
58+
59+
try
60+
{
61+
_inner.Open();
62+
}
63+
catch
64+
{
65+
MarkOpenFailed();
66+
throw;
67+
}
5568
}
5669

5770
public void HandleEvents()
@@ -66,6 +79,11 @@ public void RunLoop(Action iteration)
6679

6780
public void Close()
6881
{
82+
if (!TryBeginClose())
83+
{
84+
return;
85+
}
86+
6987
_inner.Close();
7088
}
7189

@@ -76,6 +94,6 @@ public void RegisterTextInput(lv_obj_t* textArea)
7694

7795
public void Dispose()
7896
{
79-
_inner.Dispose();
97+
Close();
8098
}
8199
}

src/LVGLSharp.Runtime.Linux/SdlView.cs

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
namespace LVGLSharp.Runtime.Linux;
1010

11-
public unsafe sealed partial class SdlView : IView
11+
public unsafe sealed partial class SdlView : ViewLifetimeBase, IView
1212
{
1313
private static SdlView? s_activeView;
1414

@@ -41,8 +41,6 @@ public unsafe sealed partial class SdlView : IView
4141
private lv_group_t* _keyInputGroup;
4242
private GCHandle _selfHandle;
4343
private lv_obj_t* _focusedTextArea;
44-
private bool _disposed;
45-
4644
public SdlView(string title = "LVGLSharp SDL", int width = 800, int height = 600, float dpi = 96f, bool borderless = false)
4745
{
4846
_title = title;
@@ -67,12 +65,7 @@ public SdlView(string title = "LVGLSharp SDL", int width = 800, int height = 600
6765

6866
public void Open()
6967
{
70-
if (_disposed)
71-
{
72-
throw new ObjectDisposedException(nameof(SdlView));
73-
}
74-
75-
if (_initialized)
68+
if (!TryBeginOpen())
7669
{
7770
return;
7871
}
@@ -116,6 +109,7 @@ public void Open()
116109
}
117110
catch
118111
{
112+
MarkOpenFailed();
119113
Close();
120114
throw;
121115
}
@@ -152,11 +146,6 @@ public void RunLoop(Action iteration)
152146

153147
public void Close()
154148
{
155-
if (_disposed)
156-
{
157-
return;
158-
}
159-
160149
if (s_activeView == this)
161150
{
162151
s_activeView = null;
@@ -172,7 +161,12 @@ public void Close()
172161
_root == null &&
173162
_keyInputGroup == null)
174163
{
175-
_disposed = true;
164+
TryBeginClose();
165+
return;
166+
}
167+
168+
if (!TryBeginClose())
169+
{
176170
return;
177171
}
178172

@@ -231,7 +225,6 @@ public void Close()
231225
SdlNative.SDL_StopTextInput();
232226

233227
_initialized = false;
234-
_disposed = true;
235228
}
236229

237230
public void RegisterTextInput(lv_obj_t* textArea)

0 commit comments

Comments
 (0)