Skip to content

Commit

Permalink
C Bindings: Add Slider & Fix: Crash with undefined ctrl shortcuts
Browse files Browse the repository at this point in the history
  • Loading branch information
malisipi committed May 1, 2023
1 parent 8f8f2dc commit 61fd5d5
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 6 deletions.
10 changes: 8 additions & 2 deletions c_bindings/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,15 @@ all: lib crun
lib:
v -cc tcc -d no_backtrace mui.v -shared -o mui.so

lib_prod:
v -cc gcc -prod -d power_save -skip-unused -shared mui.v -cflags "-s" -o mui.so

lib_mini:
v -cc gcc -prod -d no_emoji -d power_save -skip-unused -shared mui.v -cflags "-Os -s -fno-ident -fno-asynchronous-unwind-tables" -o mui.so

crun:
clang test.c mui.so -o test.out
clang test.c mui.so -Wl,-rpath . -o test.out
$(MAKE) run

run:
LD_LIBRARY_PATH=. ./test.out
./test.out
1 change: 0 additions & 1 deletion c_bindings/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ gcc test.c mui.dll -o test.exe

## Unsupported Widgets:

* Slider (Verical & Horizontal)
* Group
* Rect
* Progress
Expand Down
1 change: 1 addition & 0 deletions c_bindings/mui.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ void mui_link(mui_window* window, char* config, void (*onclk)(mui_event_details,
void mui_checkbox(mui_window* window, char* config, void (*onchg)(mui_event_details, mui_window*, void**));
void mui_switch(mui_window* window, char* config, void (*onchg)(mui_event_details, mui_window*, void**));
void mui_scrollbar(mui_window* window, char* config, void (*onclk)(mui_event_details, mui_window*, void**), void (*onchg)(mui_event_details, mui_window*, void**), void (*onunclk)(mui_event_details, mui_window*, void**), mui_object*);
void mui_slider(mui_window* window, char* config, void (*onclk)(mui_event_details, mui_window*, void**), void (*onchg)(mui_event_details, mui_window*, void**), void (*onunclk)(mui_event_details, mui_window*, void**));

mui_window* mui_create(char* config, void**);
void mui_run(mui_window* window);
Expand Down
11 changes: 9 additions & 2 deletions c_bindings/mui.v
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ fn mui_set_object_property_char(mut window &mui.Window, object &map[string]mui.W
v_value := value.vstring()
match v_property {
"path" {
object["image"] = mui.WindowData{img: window.gg.create_image(v_value) or { gg.Image{} }}
object["image"] = mui.WindowData{img: window.gg.create_image(v_value.clone()) or { gg.Image{} }}
} else {
object[property.vstring()] = mui.WindowData{str: v_value}
object[property.vstring()] = mui.WindowData{str: v_value.clone()}
}
}
}
Expand Down Expand Up @@ -120,6 +120,8 @@ fn mui_widget(_type string, mut window &mui.Window, pconf &char, onclk mui.OnEve
window.password(wconf)
} "scrollbar" {
window.scrollbar(wconf)
} "slider" {
window.slider(wconf)
} "image" {
window.image(wconf)
} "link" {
Expand Down Expand Up @@ -186,6 +188,11 @@ fn mui_scrollbar(mut window &mui.Window, pconf &char, onclk mui.OnEvent, onchg m
mui_widget("scrollbar", mut window, pconf, mui.empty_fn, onchg, mui.empty_fn, connected_object)
}

[export: "mui_slider"]
fn mui_slider(mut window &mui.Window, pconf &char, onclk mui.OnEvent, onchg mui.OnEvent, onunclk mui.OnEvent,){
mui_widget("slider", mut window, pconf, mui.empty_fn, onchg, mui.empty_fn, &mui.null_object)
}

[export: "mui_get_object_by_id"]
fn mui_get_object_by_id(mut window &mui.Window, id &char) voidptr {
unsafe {
Expand Down
12 changes: 12 additions & 0 deletions c_bindings/test.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,17 @@ void checkbox_event_handler(mui_event_details details, mui_window* window, void*
}
}

void slider_event_handler(mui_event_details details, mui_window* window, void** app_data){
struct mui_parsed_event_details parsed_details = mui_parse_event_details(details);
int scale;
char size[4];
sscanf(parsed_details.value, "%d", &scale);
mui_object* image = mui_get_object_by_id(window, "image1");
sprintf(size, "%d", scale*10);
mui_set_object_property_char(window, image, "h_raw", size);
mui_set_object_property_char(window, image, "w_raw", size);
}

int main(int argc, char** argv){
struct AppData app_data;
app_data.count = 0;
Expand All @@ -52,6 +63,7 @@ int main(int argc, char** argv){
mui_link(window, "{\"id\":\"link1\", \"x\":\"10%x +80\", \"y\":\"250\", \"width\":\"120\", \"link\": \"https://example.com\", \"text\":\"example.com\"}", *mui_empty_fn);
mui_checkbox(window, "{\"id\":\"checkbox1\", \"x\":\"10%x +80\", \"y\":\"280\", \"width\":\"20\", \"text\":\"Do not check it!\"}", *checkbox_event_handler);
mui_switch(window, "{\"id\":\"switch1\", \"x\":\"10%x +80\", \"y\":\"310\", \"width\":\"50\", \"text\":\"Do not check it!\"}", *checkbox_event_handler);
mui_slider(window, "{\"id\":\"slider1\", \"x\":\"10%x\", \"y\":\"350\", \"width\":\"100\", \"height\":\"20\"}", *mui_empty_fn, *slider_event_handler, *mui_empty_fn);
mui_object* textarea1 = mui_get_object_by_id (window, "textarea1");
mui_scrollbar(window, "{\"id\":\"scrollbar1\", \"x\":\"# 10%x\", \"y\":\"130\", \"width\":\"20\", \"height\":\"100\", \"vertical\":\"true\"}", *mui_empty_fn, *mui_empty_fn, *mui_empty_fn, textarea1);
mui_run(window);
Expand Down
2 changes: 1 addition & 1 deletion events.v
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@ fn char_fn(chr u32, mut app &Window){
}
if app.gg.key_modifiers==.ctrl {
chr_keybinding:="ctrl|"+utf32_to_str(chr).to_lower()
if app.keybindings[chr_keybinding].num!=120 {
if app.keybindings[chr_keybinding].num > 120 {
app.keybindings[chr_keybinding].fun(EventDetails{event:"keypress",trigger:"keyboard",value:chr_keybinding}, mut app, mut app.app_data)
}
} else {
Expand Down

0 comments on commit 61fd5d5

Please sign in to comment.