Skip to content
This repository was archived by the owner on Jun 17, 2024. It is now read-only.

Commit 529861c

Browse files
committed
feat: handle input keydown event
1 parent 7e8a22f commit 529861c

File tree

1 file changed

+51
-16
lines changed

1 file changed

+51
-16
lines changed

src/ui/views/frame.c

Lines changed: 51 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
21
#include <stdio.h>
32
#include <LCUI.h>
43
#include <LCDesign.h>
4+
#include <LCUI/input.h>
55
#include <LCUI/timer.h>
66
#include <LCUI/gui/widget.h>
77
#include <LCUI/gui/widget/textedit.h>
@@ -21,7 +21,7 @@ typedef struct FrameViewRec_ {
2121
LCUI_Widget input;
2222
LCUI_Widget content;
2323
LCUI_Widget client;
24-
LCUI_Widget vscrollbar;
24+
LCUI_Widget scrollbar;
2525
LCUI_Widget hscrollbar;
2626
} FrameViewRec, *FrameView;
2727

@@ -57,15 +57,27 @@ static void BrowserView_UpdateNavbar(void *arg)
5757
static void FrameView_OnBtnBackClick(LCUI_Widget w, LCUI_WidgetEvent e,
5858
void *arg)
5959
{
60+
if (w->disabled) {
61+
return;
62+
}
6063
router_back(((FrameView)e->data)->router);
6164
}
6265

6366
static void FrameView_OnBtnForwardClick(LCUI_Widget w, LCUI_WidgetEvent e,
64-
void *arg)
67+
void *arg)
6568
{
69+
if (w->disabled) {
70+
return;
71+
}
6672
router_forward(((FrameView)e->data)->router);
6773
}
6874

75+
static void FrameView_OnBtnRefreshClick(LCUI_Widget w, LCUI_WidgetEvent e,
76+
void *arg)
77+
{
78+
router_go(((FrameView)e->data)->router, 0);
79+
}
80+
6981
static void FrameView_OnBtnHomeClick(LCUI_Widget w, LCUI_WidgetEvent e,
7082
void *arg)
7183
{
@@ -78,18 +90,45 @@ static void FrameView_OnBtnHomeClick(LCUI_Widget w, LCUI_WidgetEvent e,
7890
router_location_destroy(location);
7991
}
8092

93+
static void FrameView_OnInputKeydown(LCUI_Widget w, LCUI_WidgetEvent e,
94+
void *arg)
95+
{
96+
char *path;
97+
size_t len;
98+
wchar_t raw_path[1024];
99+
router_location_t *location;
100+
FrameView self;
101+
102+
if (e->key.code != LCUI_KEY_ENTER) {
103+
return;
104+
}
105+
self = e->data;
106+
len = TextEdit_GetTextW(self->input, 0, 1023, raw_path);
107+
raw_path[len] = 0;
108+
len = LCUI_EncodeUTF8String(NULL, raw_path, 1023);
109+
path = malloc(len * (sizeof(char) + 1));
110+
len = LCUI_EncodeUTF8String(path, raw_path, 1023);
111+
path[len] = 0;
112+
location = router_location_create(NULL, path);
113+
router_push(self->router, location);
114+
router_location_destroy(location);
115+
}
116+
81117
static void FrameView_OnRouteUpdate(void *arg, const router_route_t *to,
82118
const router_route_t *from)
83119
{
84120
const char *path;
85121
FrameView self;
122+
LCUI_WidgetEventRec e;
86123

87124
self = Widget_GetData(arg, frame_proto);
88125
if (to) {
89126
path = router_route_get_full_path(to);
90127
TextEdit_SetText(self->input, path);
91128
}
92129
LCUI_SetTimeout(0, BrowserView_UpdateNavbar, arg);
130+
LCUI_InitWidgetEvent(&e, "PageLoad");
131+
Widget_TriggerEvent(arg, &e, NULL);
93132
}
94133

95134
static void FrameView_OnInit(LCUI_Widget w)
@@ -110,11 +149,8 @@ static void FrameView_OnInit(LCUI_Widget w)
110149
self->input = LCUIWidget_New("textedit");
111150
self->content = LCUIWidget_New("router-view");
112151
self->client = LCUIWidget_New(NULL);
113-
self->vscrollbar = LCUIWidget_New("scrollbar");
114-
//self->hscrollbar = LCUIWidget_New("scrollbar");
115-
//ScrollBar_BindTarget(self->hscrollbar, self->content);
116-
ScrollBar_BindTarget(self->vscrollbar, self->content);
117-
//ScrollBar_SetDirection(self->hscrollbar, SBD_HORIZONTAL);
152+
self->scrollbar = LCUIWidget_New("scrollbar");
153+
ScrollBar_BindTarget(self->scrollbar, self->content);
118154
Widget_SetAttribute(w, "router", router_name);
119155
Widget_AddClass(w, "v-frame");
120156
Widget_AddClass(self->navbar, "c-navbar v-frame__navbar");
@@ -131,20 +167,23 @@ static void FrameView_OnInit(LCUI_Widget w)
131167
Widget_Append(self->navbar, self->btn_home);
132168
Widget_Append(self->navbar, self->input);
133169
Widget_Append(self->client, self->content);
134-
Widget_Append(self->client, self->vscrollbar);
135-
//Widget_Append(self->client, self->hscrollbar);
170+
Widget_Append(self->client, self->scrollbar);
136171
Icon_SetName(self->btn_back, "arrow-left");
137172
Icon_SetName(self->btn_forward, "arrow-right");
138173
Icon_SetName(self->btn_refresh, "refresh");
139174
Icon_SetName(self->btn_home, "home-outline");
140175
Widget_BindEvent(self->btn_back, "click", FrameView_OnBtnBackClick,
141176
self, NULL);
142-
Widget_BindEvent(self->btn_forward, "click", FrameView_OnBtnForwardClick,
143-
self, NULL);
177+
Widget_BindEvent(self->btn_forward, "click",
178+
FrameView_OnBtnForwardClick, self, NULL);
144179
Widget_BindEvent(self->btn_home, "click", FrameView_OnBtnHomeClick,
145180
self, NULL);
181+
Widget_BindEvent(self->btn_refresh, "click",
182+
FrameView_OnBtnRefreshClick, self, NULL);
183+
Widget_BindEvent(self->input, "keydown", FrameView_OnInputKeydown, self, NULL);
146184
Widget_Append(w, self->navbar);
147185
Widget_Append(w, self->client);
186+
Widget_SetId(w, router_name);
148187
FrameView_OnRouteUpdate(w, router_get_current_route(self->router),
149188
NULL);
150189
}
@@ -161,13 +200,9 @@ static void FrameView_OnDestroy(LCUI_Widget w)
161200
void FrameView_Load(LCUI_Widget w, const char *path)
162201
{
163202
router_location_t *location;
164-
165203
FrameView self;
166-
LCUI_WidgetEventRec e;
167204

168205
Widget_Update(w);
169-
LCUI_InitWidgetEvent(&e, "PageLoad");
170-
Widget_TriggerEvent(w, &e, NULL);
171206
location = router_location_create(NULL, path);
172207
self = Widget_GetData(w, frame_proto);
173208
router_push(self->router, location);

0 commit comments

Comments
 (0)