Skip to content

Commit dfd7c67

Browse files
committed
保存
1 parent f8a243b commit dfd7c67

File tree

6 files changed

+95
-78
lines changed

6 files changed

+95
-78
lines changed

src/Demos/SerialPort/Program.cs

Lines changed: 9 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,7 @@
1-
using LVGLSharp;
1+
using LVGLSharp;
22
using LVGLSharp.Interop;
3-
#if LINUX
4-
using LVGLSharp.Runtime.Linux;
5-
#else
6-
using LVGLSharp.Runtime.Windows;
7-
#endif
8-
using SixLabors.Fonts;
93
using System.Diagnostics;
104
using System.IO.Ports;
11-
using System.Reflection.Emit;
125
using System.Runtime.CompilerServices;
136
using System.Runtime.InteropServices;
147
using System.Text;
@@ -24,9 +17,9 @@ unsafe class Program
2417
static lv_obj_t* send_btn;
2518
static lv_obj_t* clear_btn;
2619
static lv_obj_t* hex_switch;
27-
static IWindow window;
28-
static SerialPort serialPort;
29-
static List<string> serialPorts;
20+
static IWindow? window;
21+
static SerialPort? serialPort;
22+
static List<string> serialPorts = [];
3023
static List<string> bauds = ["9600", "19200", "38400", "57600", "115200"];
3124

3225
static lv_obj_t* root;
@@ -35,22 +28,12 @@ unsafe class Program
3528

3629
static void Main(string[] args)
3730
{
38-
#if LINUX
39-
window = new LinuxView(dpi: 96f);
40-
#else
41-
window = new Win32Window("LVGLSharp", 710, 470);
42-
#endif
31+
window = PlatformWindowFactory.Create();
4332
window.Init();
4433

45-
#if LINUX
46-
root = LinuxView.root;
47-
key_inputGroup = LinuxView.key_inputGroup;
48-
SendTextAreaFocusCb = LinuxView.SendTextAreaFocusCb;
49-
#else
50-
root = Win32Window.root;
51-
key_inputGroup = Win32Window.key_inputGroup;
52-
SendTextAreaFocusCb = Win32Window.SendTextAreaFocusCb;
53-
#endif
34+
root = window.Root;
35+
key_inputGroup = window.KeyInputGroup;
36+
SendTextAreaFocusCb = window.SendTextAreaFocusCallback;
5437

5538
InitUI();
5639

@@ -278,11 +261,7 @@ static void InitUI()
278261
lv_group_add_obj(key_inputGroup, send_textarea);
279262
lv_obj_set_height(send_textarea, 50);
280263

281-
#if LINUX
282-
lv_obj_t* kb = lv_keyboard_create(lv_scr_act());
283-
lv_obj_set_size(kb, 670, 200);
284-
lv_keyboard_set_textarea(kb, send_textarea);
285-
#endif
264+
window?.AttachTextInput(send_textarea);
286265

287266
// 发送按钮
288267
send_btn = lv_btn_create(send_container);
Lines changed: 24 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,26 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
2-
3-
<PropertyGroup>
4-
<OutputType>WinExe</OutputType>
5-
<TargetFramework>net10.0</TargetFramework>
6-
<ImplicitUsings>enable</ImplicitUsings>
7-
<Nullable>enable</Nullable>
8-
<PublishAot>true</PublishAot>
9-
<InvariantGlobalization>true</InvariantGlobalization>
10-
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
11-
</PropertyGroup>
12-
13-
14-
15-
<ItemGroup>
16-
<PackageReference Include="LVGLSharp.Native" Version="0.0.3" />
17-
<PackageReference Include="System.IO.Ports" Version="10.0.5" />
18-
</ItemGroup>
19-
20-
<ItemGroup Condition="'$(PublishPlatform)' == 'Windows' Or ('$(PublishPlatform)' == '' And '$(OS)' == 'Windows_NT')">
21-
<ProjectReference Include="..\..\LVGLSharp.Windows\LVGLSharp.Runtime.Windows.csproj" />
22-
</ItemGroup>
23-
24-
<ItemGroup Condition="'$(PublishPlatform)' == 'Linux'">
25-
<ProjectReference Include="..\..\LVGLSharp.Runtime.Linux\LVGLSharp.Runtime.Linux.csproj" />
26-
</ItemGroup>
27-
28-
<ItemGroup>
29-
<ProjectReference Include="..\..\LVGLSharp.Core\LVGLSharp.Core.csproj" />
30-
</ItemGroup>
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>WinExe</OutputType>
5+
<TargetFrameworks>net10.0-windows;net10.0</TargetFrameworks>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
<InvariantGlobalization>true</InvariantGlobalization>
9+
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
10+
</PropertyGroup>
11+
12+
<PropertyGroup Condition="'$(TargetFramework)' == 'net10.0'">
13+
<PublishAot>true</PublishAot>
14+
</PropertyGroup>
15+
16+
<ItemGroup>
17+
<PackageReference Include="LVGLSharp.Native" Version="0.0.3" />
18+
<PackageReference Include="System.IO.Ports" Version="10.0.5" />
19+
</ItemGroup>
20+
21+
22+
<ItemGroup>
23+
<ProjectReference Include="..\..\LVGLSharp.Core\LVGLSharp.Core.csproj" />
24+
</ItemGroup>
3125

3226
</Project>

src/Demos/SerialPort/global.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
global using static LVGLSharp.Interop.lv_align_t;
1+
global using static LVGLSharp.Interop.lv_align_t;
22
global using static LVGLSharp.Interop.lv_animimg_part_t;
33
global using static LVGLSharp.Interop.lv_arc_mode_t;
44
global using static LVGLSharp.Interop.lv_bar_mode_t;
@@ -74,4 +74,4 @@
7474
global using static LVGLSharp.Interop.lv_text_flag_t;
7575
global using static LVGLSharp.Interop.lv_thread_prio_t;
7676
global using static LVGLSharp.Interop.LVGL;
77-
global using lv_style_prop_t = byte;
77+
global using lv_style_prop_t = byte;

src/LVGLSharp.Core/IWindow.cs

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,23 @@
1-
using LVGLSharp.Interop;
2-
using System;
3-
using System.Collections.Generic;
4-
using System.Linq;
5-
using System.Text;
6-
using System.Threading.Tasks;
1+
using LVGLSharp.Interop;
72

83
namespace LVGLSharp
94
{
105
public unsafe interface IWindow
116
{
12-
public static lv_obj_t* root { get; set; }
13-
public static lv_group_t* key_inputGroup { get; set; }
14-
public static delegate* unmanaged[Cdecl]<lv_event_t*, void> SendTextAreaFocusCb { get; set; }
7+
lv_obj_t* Root { get; }
158

16-
public void Init();
17-
public void StartLoop(Action handle);
9+
lv_group_t* KeyInputGroup { get; }
10+
11+
delegate* unmanaged[Cdecl]<lv_event_t*, void> SendTextAreaFocusCallback { get; }
12+
13+
void Init();
14+
15+
void ProcessEvents();
16+
17+
void StartLoop(Action handle);
18+
19+
void Stop();
20+
21+
void AttachTextInput(lv_obj_t* textArea);
1822
}
1923
}

src/LVGLSharp.Runtime.Linux/LinuxView.cs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using LVGLSharp;
1+
using LVGLSharp;
22
using LVGLSharp.Interop;
33
using System;
44
using System.Collections.Generic;
@@ -77,6 +77,18 @@ public void Init()
7777
_defaultFontStyle = LvglHostDefaults.ApplyDefaultFontStyle(root, _fontManager.GetLvFontPtr());
7878
}
7979

80+
public void AttachTextInput(lv_obj_t* textArea)
81+
{
82+
if (textArea == null)
83+
{
84+
return;
85+
}
86+
87+
lv_obj_t* keyboard = lv_keyboard_create(lv_scr_act());
88+
lv_obj_set_size(keyboard, 670, 200);
89+
lv_keyboard_set_textarea(keyboard, textArea);
90+
}
91+
8092
public void StartLoop(Action handle)
8193
{
8294
while (g_running)

src/LVGLSharp.Windows/Win32Window.cs

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using LVGLSharp;
1+
using LVGLSharp;
22
using LVGLSharp.Interop;
33
using SixLabors.Fonts;
44
using System;
@@ -43,6 +43,10 @@ public unsafe class Win32Window : IWindow
4343
public static lv_obj_t* root { get; set; }
4444
public static lv_group_t* key_inputGroup { get; set; }
4545
public static delegate* unmanaged[Cdecl]<lv_event_t*, void> SendTextAreaFocusCb { get; set; } = &HandleSendTextAreaFocusCb;
46+
public lv_obj_t* Root => root;
47+
public lv_group_t* KeyInputGroup => key_inputGroup;
48+
public delegate* unmanaged[Cdecl]<lv_event_t*, void> SendTextAreaFocusCallback => SendTextAreaFocusCb;
49+
4650

4751
static BITMAPINFO _bmi = new BITMAPINFO
4852
{
@@ -278,6 +282,13 @@ static bool PumpPendingMessages()
278282
return hadMessages;
279283
}
280284

285+
bool ProcessEventsCore()
286+
{
287+
bool hadMessages = PumpPendingMessages();
288+
lv_timer_handler();
289+
return hadMessages;
290+
}
291+
281292
static unsafe bool IsPointInsideObject(lv_obj_t* obj, int x, int y)
282293
{
283294
if (obj == null)
@@ -502,13 +513,11 @@ public void Init()
502513

503514
lv_obj_add_style(root, _defaultFontStyle, 0);
504515
}
505-
506516
public void StartLoop(Action handle)
507517
{
508518
while (g_running)
509519
{
510-
bool hadMessages = PumpPendingMessages();
511-
lv_timer_handler();
520+
bool hadMessages = ProcessEventsCore();
512521
handle?.Invoke();
513522

514523
if (!hadMessages)
@@ -520,5 +529,24 @@ public void StartLoop(Action handle)
520529
g_running = false;
521530
Marshal.FreeHGlobal(g_lvbuf);
522531
}
532+
533+
public void ProcessEvents()
534+
{
535+
ProcessEventsCore();
536+
}
537+
538+
public void Stop()
539+
{
540+
g_running = false;
541+
542+
if (g_hwnd != IntPtr.Zero)
543+
{
544+
// DestroyWindow(g_hwnd);
545+
}
546+
}
547+
548+
public void AttachTextInput(lv_obj_t* textArea)
549+
{
550+
}
523551
}
524552
}

0 commit comments

Comments
 (0)