--- /dev/null
+package de.unixwork.ui;
+
+import java.lang.foreign.Arena;
+import java.lang.foreign.MemorySegment;
+import java.lang.invoke.MethodHandle;
+
+public abstract class AbstractWidgetBuilder {
+ UiObject obj;
+ MethodHandle widgetConstructor;
+
+ public abstract MemorySegment createArgs(Arena arena) throws Throwable;
+
+ public UiWidget create() {
+ UiWidget widget = null;
+ try (Arena arena = Arena.ofConfined()) {
+ MemorySegment args = createArgs(arena);
+ widget = new UiWidget((MemorySegment) widgetConstructor.invoke(obj.ptr, args));
+ } catch (Throwable e) {
+ throw new RuntimeException(e);
+ }
+ return widget;
+ }
+}
package de.unixwork.ui;
import java.lang.foreign.Arena;
-import java.lang.foreign.FunctionDescriptor;
import java.lang.foreign.MemorySegment;
-import java.lang.foreign.ValueLayout;
import java.lang.invoke.MethodHandle;
public class Button {
- public static void createButton(UiObject obj, ButtonArgs args) {
- ButtonFuncs ui = ButtonFuncs.getInstance();
- try (Arena arena = Arena.ofConfined()) {
- MemorySegment buttonArgs = args.createButtonArgs(obj, arena);
- MemorySegment widget = (MemorySegment) ui.button_create.invoke(obj.ptr, buttonArgs);
- } catch (Throwable e) {
- throw new RuntimeException(e);
- }
- }
- private static void createToggleButton(UiObject obj, MethodHandle func, ToggleArgs args) {
+ public static ButtonBuilder button(UiObject obj) {
ButtonFuncs ui = ButtonFuncs.getInstance();
- try (Arena arena = Arena.ofConfined()) {
- MemorySegment buttonArgs = args.createToggleArgs(obj, arena);
- MemorySegment widget = (MemorySegment) func.invoke(obj.ptr, buttonArgs);
- } catch (Throwable e) {
- throw new RuntimeException(e);
- }
+ return new ButtonBuilder(obj, ui.button_create);
}
- public static void createToggleButton(UiObject obj, ToggleArgs args) {
+ public static ToggleBuilder toggleButton(UiObject obj) {
ButtonFuncs ui = ButtonFuncs.getInstance();
- Button.createToggleButton(obj, ui.togglebutton_create, args);
+ return new ToggleBuilder(obj, ui.togglebutton_create);
}
- public static void createCheckbox(UiObject obj, ToggleArgs args) {
+ public static ToggleBuilder checkbox(UiObject obj) {
ButtonFuncs ui = ButtonFuncs.getInstance();
- Button.createToggleButton(obj, ui.checkbox_create, args);
+ return new ToggleBuilder(obj, ui.checkbox_create);
}
- public static void createSwitch(UiObject obj, ToggleArgs args) {
+ public static ToggleBuilder switchButton(UiObject obj) {
ButtonFuncs ui = ButtonFuncs.getInstance();
- Button.createToggleButton(obj, ui.switch_create, args);
+ return new ToggleBuilder(obj, ui.switch_create);
}
- public static void createRadioButton(UiObject obj, ToggleArgs args) {
+ public static ToggleBuilder radioButton(UiObject obj) {
ButtonFuncs ui = ButtonFuncs.getInstance();
- Button.createToggleButton(obj, ui.radiobutton_create, args);
+ return new ToggleBuilder(obj, ui.radiobutton_create);
}
}
import java.lang.foreign.Arena;
import java.lang.foreign.MemorySegment;
+import java.lang.invoke.MethodHandle;
-public class ButtonArgs {
-
+public class ButtonBuilder extends AbstractWidgetBuilder {
private boolean fill;
private boolean hexpand;
private boolean vexpand;
private EventHandler onClick;
private int[] states;
- public ButtonArgs() {
-
+ public ButtonBuilder(UiObject obj, MethodHandle widgetConstructor) {
+ this.obj = obj;
+ this.widgetConstructor = widgetConstructor;
}
- public ButtonArgs fill(boolean fill) {
+ public ButtonBuilder fill(boolean fill) {
this.fill = fill;
return this;
}
- public ButtonArgs hexpand(boolean hexpand) {
+ public ButtonBuilder hexpand(boolean hexpand) {
this.hexpand = hexpand;
return this;
}
- public ButtonArgs vexpand(boolean vexpand) {
+ public ButtonBuilder vexpand(boolean vexpand) {
this.vexpand = vexpand;
return this;
}
- public ButtonArgs hfill(boolean hfill) {
+ public ButtonBuilder hfill(boolean hfill) {
this.hfill = hfill;
return this;
}
- public ButtonArgs vfill(boolean vfill) {
+ public ButtonBuilder vfill(boolean vfill) {
this.vfill = vfill;
return this;
}
- public ButtonArgs overrideDefaults(boolean overrideDefaults) {
+ public ButtonBuilder overrideDefaults(boolean overrideDefaults) {
this.overrideDefaults = overrideDefaults;
return this;
}
- public ButtonArgs colspan(int colspan) {
+ public ButtonBuilder colspan(int colspan) {
this.colspan = colspan;
return this;
}
- public ButtonArgs rowspan(int rowspan) {
+ public ButtonBuilder rowspan(int rowspan) {
this.rowspan = rowspan;
return this;
}
- public ButtonArgs name(String name) {
+ public ButtonBuilder name(String name) {
this.name = name;
return this;
}
- public ButtonArgs styleClass(String styleClass) {
+ public ButtonBuilder styleClass(String styleClass) {
this.styleClass = styleClass;
return this;
}
- public ButtonArgs label(String label) {
+ public ButtonBuilder label(String label) {
this.label = label;
return this;
}
- public ButtonArgs stockId(String stockId) {
+ public ButtonBuilder stockId(String stockId) {
this.stockId = stockId;
return this;
}
- public ButtonArgs icon(String icon) {
+ public ButtonBuilder icon(String icon) {
this.icon = icon;
return this;
}
- public ButtonArgs labelType(int labelType) {
+ public ButtonBuilder labelType(int labelType) {
this.labelType = labelType;
return this;
}
- public ButtonArgs onClick(EventHandler onClick) {
+ public ButtonBuilder onClick(EventHandler onClick) {
this.onClick = onClick;
return this;
}
- public ButtonArgs states(int... states) {
+ public ButtonBuilder states(int... states) {
this.states = states;
return this;
}
- public MemorySegment createButtonArgs(UiObject parent, Arena arena) throws Throwable {
+ public MemorySegment createArgs(Arena arena) throws Throwable {
ArgFuncs ui = ArgFuncs.getInstance();
MemorySegment args = (MemorySegment)ui.button_args_new.invoke();
return args;
}
+
+ public UiWidget create() {
+ UiWidget widget = null;
+ try (Arena arena = Arena.ofConfined()) {
+ MemorySegment buttonArgs = createArgs(arena);
+ widget = new UiWidget((MemorySegment) widgetConstructor.invoke(obj.ptr, buttonArgs));
+ } catch (Throwable e) {
+ throw new RuntimeException(e);
+ }
+ return widget;
+ }
}
import java.lang.foreign.Arena;
import java.lang.foreign.MemorySegment;
+import java.lang.invoke.MethodHandle;
-public class ToggleArgs {
-
+public class ToggleBuilder extends AbstractWidgetBuilder {
private boolean fill;
private boolean hexpand;
private boolean vexpand;
private EventHandler onChange;
private int[] states;
- public ToggleArgs() {
-
+ public ToggleBuilder(UiObject obj, MethodHandle widgetConstructor) {
+ this.obj = obj;
+ this.widgetConstructor = widgetConstructor;
}
- public ToggleArgs fill(boolean fill) {
+ public ToggleBuilder fill(boolean fill) {
this.fill = fill;
return this;
}
- public ToggleArgs hexpand(boolean hexpand) {
+ public ToggleBuilder hexpand(boolean hexpand) {
this.hexpand = hexpand;
return this;
}
- public ToggleArgs vexpand(boolean vexpand) {
+ public ToggleBuilder vexpand(boolean vexpand) {
this.vexpand = vexpand;
return this;
}
- public ToggleArgs hfill(boolean hfill) {
+ public ToggleBuilder hfill(boolean hfill) {
this.hfill = hfill;
return this;
}
- public ToggleArgs vfill(boolean vfill) {
+ public ToggleBuilder vfill(boolean vfill) {
this.vfill = vfill;
return this;
}
- public ToggleArgs overrideDefaults(boolean overrideDefaults) {
+ public ToggleBuilder overrideDefaults(boolean overrideDefaults) {
this.overrideDefaults = overrideDefaults;
return this;
}
- public ToggleArgs colspan(int colspan) {
+ public ToggleBuilder colspan(int colspan) {
this.colspan = colspan;
return this;
}
- public ToggleArgs rowspan(int rowspan) {
+ public ToggleBuilder rowspan(int rowspan) {
this.rowspan = rowspan;
return this;
}
- public ToggleArgs name(String name) {
+ public ToggleBuilder name(String name) {
this.name = name;
return this;
}
- public ToggleArgs styleClass(String styleClass) {
+ public ToggleBuilder styleClass(String styleClass) {
this.styleClass = styleClass;
return this;
}
- public ToggleArgs label(String label) {
+ public ToggleBuilder label(String label) {
this.label = label;
return this;
}
- public ToggleArgs stockId(String stockId) {
+ public ToggleBuilder stockId(String stockId) {
this.stockId = stockId;
return this;
}
- public ToggleArgs icon(String icon) {
+ public ToggleBuilder icon(String icon) {
this.icon = icon;
return this;
}
- public ToggleArgs labelType(int labelType) {
+ public ToggleBuilder labelType(int labelType) {
this.labelType = labelType;
return this;
}
- public ToggleArgs varname(String varname) {
+ public ToggleBuilder varname(String varname) {
this.varname = varname;
return this;
}
- public ToggleArgs onChange(EventHandler onChange) {
+ public ToggleBuilder onChange(EventHandler onChange) {
this.onChange = onChange;
return this;
}
- public ToggleArgs states(int... states) {
+ public ToggleBuilder states(int... states) {
this.states = states;
return this;
}
- public MemorySegment createToggleArgs(UiObject parent, Arena arena) throws Throwable {
+ public MemorySegment createArgs(Arena arena) throws Throwable {
ArgFuncs ui = ArgFuncs.getInstance();
MemorySegment args = (MemorySegment)ui.toggle_args_new.invoke();
--- /dev/null
+package de.unixwork.ui;
+
+import java.lang.foreign.MemorySegment;
+
+public class UiWidget {
+ private MemorySegment widget;
+
+ public UiWidget(MemorySegment widget) {
+ this.widget = widget;
+ }
+
+ public MemorySegment getWidgetPtr() {
+ return widget;
+ }
+}
public class Main implements Application{
public void startup() {
UiObject window = UiObject.createWindow("Test Window");
- Button.createButton(window, new ButtonArgs().label("Hello World!"));
- Button.createToggleButton(window, new ToggleArgs().label("Toggle"));
- Button.createCheckbox(window, new ToggleArgs().label("Checkbox"));
- Button.createRadioButton(window, new ToggleArgs().label("Radio 1").varname("radio"));
- Button.createRadioButton(window, new ToggleArgs().label("Radio 2").varname("radio"));
+ Button.button(window).label("Click Me").create();
+ Button.checkbox(window).label("Checkbox").create();
+ Button.radioButton(window).label("Radio Button 1").varname("radiobutton").create();
+ Button.radioButton(window).label("Radio Button 2").varname("radiobutton").create();
window.show();
}