Skip to content

Commit f7261d7

Browse files
committed
Let's use the existing Rect and Color types in the GUI API.
Any type that doesn't depend on indirect data can probably be used here.
1 parent b2d86b7 commit f7261d7

File tree

4 files changed

+68
-23
lines changed

4 files changed

+68
-23
lines changed

Kernel/GUITypes.h

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,9 @@
11
#pragma once
22

3-
// GUI system call API types.
3+
#include <Widgets/Color.h>
4+
#include <Widgets/Rect.h>
45

5-
struct GUI_Rect {
6-
int x;
7-
int y;
8-
int width;
9-
int height;
10-
};
6+
// GUI system call API types.
117

128
struct GUI_WindowFlags { enum {
139
Visible = 1 << 0,
@@ -16,9 +12,9 @@ struct GUI_WindowFlags { enum {
1612
typedef unsigned GUI_Color;
1713

1814
struct GUI_CreateWindowParameters {
19-
GUI_Rect rect;
20-
GUI_Color background_color;
21-
unsigned flags;
15+
Rect rect;
16+
Color background_color;
17+
unsigned flags { 0 };
2218
char title[128];
2319
};
2420

@@ -29,9 +25,9 @@ enum class GUI_WidgetType : unsigned {
2925

3026
struct GUI_CreateWidgetParameters {
3127
GUI_WidgetType type;
32-
GUI_Rect rect;
33-
GUI_Color background_color;
34-
bool opaque;
35-
unsigned flags;
28+
Rect rect;
29+
Color background_color;
30+
bool opaque { true };
31+
unsigned flags { 0 };
3632
char text[256];
3733
};

Kernel/ProcessGUI.cpp

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,8 @@ int Process::gui$create_window(const GUI_CreateWindowParameters* user_params)
3737
return -EFAULT;
3838

3939
auto params = *user_params;
40-
Rect rect { params.rect.x, params.rect.y, params.rect.width, params.rect.height };
4140

42-
if (rect.is_empty())
41+
if (params.rect.is_empty())
4342
return -EINVAL;
4443

4544
ProcessPagingScope scope(EventLoop::main().server_process());
@@ -52,14 +51,14 @@ int Process::gui$create_window(const GUI_CreateWindowParameters* user_params)
5251
m_windows.append(window->makeWeakPtr());
5352

5453
window->setTitle(params.title);
55-
window->setRect(rect);
54+
window->setRect(params.rect);
5655

5756
auto* main_widget = new Widget;
5857
window->setMainWidget(main_widget);
59-
main_widget->setWindowRelativeRect({ 0, 0, rect.width(), rect.height() });
58+
main_widget->setWindowRelativeRect({ 0, 0, params.rect.width(), params.rect.height() });
6059
main_widget->setBackgroundColor(params.background_color);
6160
main_widget->setFillWithBackgroundColor(true);
62-
dbgprintf("%s<%u> gui$create_window: %d with rect {%d,%d %dx%d}\n", name().characters(), pid(), window_id, rect.x(), rect.y(), rect.width(), rect.height());
61+
dbgprintf("%s<%u> gui$create_window: %d with rect {%d,%d %dx%d}\n", name().characters(), pid(), window_id, params.rect.x(), params.rect.y(), params.rect.width(), params.rect.height());
6362

6463
return window_id;
6564
}
@@ -92,9 +91,8 @@ int Process::gui$create_widget(int window_id, const GUI_CreateWidgetParameters*
9291
auto& window = *m_windows[window_id];
9392

9493
auto params = *user_params;
95-
Rect rect { params.rect.x, params.rect.y, params.rect.width, params.rect.height };
9694

97-
if (rect.is_empty())
95+
if (params.rect.is_empty())
9896
return -EINVAL;
9997

10098
Widget* widget = nullptr;
@@ -112,10 +110,10 @@ int Process::gui$create_widget(int window_id, const GUI_CreateWidgetParameters*
112110
int widget_id = m_widgets.size();
113111
m_widgets.append(widget->makeWeakPtr());
114112

115-
widget->setWindowRelativeRect(rect);
113+
widget->setWindowRelativeRect(params.rect);
116114
widget->setBackgroundColor(params.background_color);
117115
widget->setFillWithBackgroundColor(params.opaque);
118-
dbgprintf("%s<%u> gui$create_widget: %d with rect {%d,%d %dx%d}\n", name().characters(), pid(), widget_id, rect.x(), rect.y(), rect.width(), rect.height());
116+
dbgprintf("%s<%u> gui$create_widget: %d with rect {%d,%d %dx%d}\n", name().characters(), pid(), widget_id, params.rect.x(), params.rect.y(), params.rect.width(), params.rect.height());
119117

120118
return window_id;
121119
}

Userland/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,4 @@ mkdir
2121
touch
2222
sync
2323
more
24+
guitest

Userland/guitest.cpp

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#include <stdio.h>
2+
#include <unistd.h>
3+
#include <errno.h>
4+
#include <string.h>
5+
#include <stdlib.h>
6+
#include <fcntl.h>
7+
#include <assert.h>
8+
#include <Kernel/GUITypes.h>
9+
#include <Kernel/Syscall.h>
10+
11+
int main(int argc, char** argv)
12+
{
13+
GUI_CreateWindowParameters wparams;
14+
wparams.rect = { 200, 200, 300, 200 };
15+
wparams.background_color = 0xffc0c0;
16+
strcpy(wparams.title, "GUI test app");
17+
int window_id = syscall(SC_gui_create_window, &wparams);
18+
if (window_id < 0) {
19+
perror("gui_create_window");
20+
return 1;
21+
}
22+
23+
GUI_CreateWidgetParameters label_params;
24+
label_params.type = GUI_WidgetType::Label;
25+
label_params.rect = { 20, 20, 260, 20 };
26+
label_params.background_color = 0xffffff;
27+
label_params.opaque = true;
28+
strcpy(label_params.text, "Hello World!");
29+
int label_id = syscall(SC_gui_create_widget, window_id, &label_params);
30+
if (label_id < 0) {
31+
perror("gui_create_widget");
32+
return 1;
33+
}
34+
35+
GUI_CreateWidgetParameters button_params;
36+
button_params.type = GUI_WidgetType::Button;
37+
button_params.rect = { 60, 60, 120, 20 };
38+
button_params.background_color = 0xffffff;
39+
button_params.opaque = true;
40+
strcpy(button_params.text, "I'm a button!");
41+
int button_id = syscall(SC_gui_create_widget, window_id, &button_params);
42+
if (button_id < 0) {
43+
perror("gui_create_widget");
44+
return 1;
45+
}
46+
47+
for (;;) {
48+
}
49+
return 0;
50+
}

0 commit comments

Comments
 (0)