Skip to content

Commit

Permalink
Improves Android support, disables CodeEditor in android builds.
Browse files Browse the repository at this point in the history
  • Loading branch information
Felix "xq" Queißner committed Oct 26, 2022
1 parent 549f1c2 commit 73b1547
Show file tree
Hide file tree
Showing 11 changed files with 469 additions and 397 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ zig-out/
hackzig/
.vscode/
*.ll
*.idsig
17 changes: 11 additions & 6 deletions Sdk.zig
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ pub const Application = struct {
app.resolution = resolution;
}

fn prepareExe(app: *Application, exe: *std.build.LibExeObjStep, app_pkg: std.build.Pkg) void {
fn prepareExe(app: *Application, exe: *std.build.LibExeObjStep, app_pkg: std.build.Pkg, platform_id: std.meta.Tag(Platform)) void {
exe.main_pkg_path = sdkPath("/src");

exe.addPackage(app_pkg);
Expand All @@ -240,7 +240,7 @@ pub const Application = struct {
exe.addIncludePath(sdkPath("/src/scintilla"));

if (app.enable_code_editor) {
if (exe.target.getCpuArch() != .wasm32) {
if (platform_id != .android and platform_id != .web) {
const scintilla_header = app.sdk.builder.addTranslateC(.{ .path = sdkPath("/src/scintilla/code_editor.h") });
scintilla_header.setTarget(exe.target);
scintilla_header.use_stage1 = exe.use_stage1;
Expand All @@ -265,16 +265,20 @@ pub const Application = struct {
.dependencies = app.packages.items,
});

const options = app.sdk.builder.addOptions();
options.addOption(bool, "enable_code_editor", app.enable_code_editor and (platform != .android and platform != .web));

switch (platform) {
.desktop => |target| {
const exe = app.sdk.builder.addExecutable(app.name, sdkPath("/src/main/desktop.zig"));
exe.setBuildMode(app.build_mode);
exe.setTarget(target);
exe.addPackage(options.getPackage("build_options"));

exe.addPackage(app.sdk.sdl_sdk.getNativePackage("sdl2"));
app.sdk.sdl_sdk.link(exe, .dynamic);

app.prepareExe(exe, app_pkg);
app.prepareExe(exe, app_pkg, platform);

// For desktop versions, we link lib-nfd
const libnfd = NFD.makeLib(app.sdk.builder, .ReleaseSafe, target);
Expand All @@ -294,8 +298,9 @@ pub const Application = struct {
.os_tag = .freestanding,
.abi = .musl,
});
exe.addPackage(options.getPackage("build_options"));

app.prepareExe(exe, app_pkg);
app.prepareExe(exe, app_pkg, platform);

return app.createCompilation(.{
.web = exe,
Expand Down Expand Up @@ -327,8 +332,8 @@ pub const Application = struct {
);

for (android_app.libraries) |lib| {
app.prepareExe(lib, app_pkg);

app.prepareExe(lib, app_pkg, platform);
lib.addPackage(options.getPackage("build_options"));
lib.addPackage(android_app.getAndroidPackage("android"));
}

Expand Down
19 changes: 10 additions & 9 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -91,18 +91,19 @@ pub fn build(b: *std.build.Builder) !void {
}

// Build wasm application
{
const wasm_build = app.compileFor(.web);
wasm_build.install();
// TODO: Reinclude when https://github.com/llvm/llvm-project/issues/58557 is fixed.
// {
// const wasm_build = app.compileFor(.web);
// wasm_build.install();

const serve = wasm_build.run();
// const serve = wasm_build.run();

const build_step = b.step("build-wasm", "Builds the wasm app and installs it.");
build_step.dependOn(wasm_build.install_step.?);
// const build_step = b.step("build-wasm", "Builds the wasm app and installs it.");
// build_step.dependOn(wasm_build.install_step.?);

const run_step = b.step("run-wasm", "Serves the wasm app");
run_step.dependOn(&serve.step);
}
// const run_step = b.step("run-wasm", "Serves the wasm app");
// run_step.dependOn(&serve.step);
// }

if (enable_android) {
const android_build = app.compileFor(.android);
Expand Down
16 changes: 9 additions & 7 deletions examples/demo-application.zig
Original file line number Diff line number Diff line change
Expand Up @@ -187,15 +187,17 @@ pub fn update(app: *Application) !bool {
}
}

const editor = try ui.codeEditor(.{ .x = 270, .y = 130, .width = 250, .height = 116 }, "", .{});
{
const events = editor.getNotifications();
if (@hasDecl(zero_graphics, "CodeEditor")) {
const editor = try ui.codeEditor(.{ .x = 270, .y = 130, .width = 250, .height = 116 }, "", .{});
{
const events = editor.getNotifications();

if (events.contains(.text_changed)) {
const string = try editor.getText(app.allocator);
defer app.allocator.free(string);
if (events.contains(.text_changed)) {
const string = try editor.getText(app.allocator);
defer app.allocator.free(string);

// TODO: Handle text changed here
// TODO: Handle text changed here
}
}
}

Expand Down
131 changes: 74 additions & 57 deletions src/UserInterface.zig
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,8 @@ pub const Theme = struct {
};
};

const has_code_editor = @hasDecl(types, "CodeEditor");

const WidgetID = enum(u32) { _ };

const Widget = struct {
Expand All @@ -143,7 +145,8 @@ const Widget = struct {
panel: Panel,
button: Button,
text_box: TextBox,
code_editor: CodeEditor,

code_editor: if (has_code_editor) CodeEditor else void,
label: Label,
check_box: CheckBox,
radio_button: RadioButton,
Expand All @@ -161,7 +164,8 @@ const Widget = struct {
ctrl.editor.deinit();
},
.code_editor => |*ctrl| {
ctrl.editor.destroy();
if (has_code_editor)
ctrl.editor.destroy();
},
.label => |*ctrl| {
ctrl.text.deinit();
Expand All @@ -181,7 +185,7 @@ const Widget = struct {
.panel => |ctrl| ctrl.config.hit_test_visible,
.button => |ctrl| ctrl.config.hit_test_visible,
.text_box => |ctrl| ctrl.config.hit_test_visible,
.code_editor => |ctrl| ctrl.config.hit_test_visible,
.code_editor => |ctrl| if (has_code_editor) ctrl.config.hit_test_visible else unreachable,
.label => |ctrl| ctrl.config.hit_test_visible,
.check_box => |ctrl| ctrl.config.hit_test_visible,
.radio_button => |ctrl| ctrl.config.hit_test_visible,
Expand Down Expand Up @@ -246,7 +250,8 @@ const Widget = struct {
control.events.insert(.enter);
},
.code_editor => |*control| {
control.editor.setFocus(true);
if (has_code_editor)
control.editor.setFocus(true);
},
else => {},
}
Expand All @@ -260,7 +265,8 @@ const Widget = struct {
control.events.insert(.leave);
},
.code_editor => |*control| {
control.editor.setFocus(false);
if (has_code_editor)
control.editor.setFocus(false);
},
else => {},
}
Expand Down Expand Up @@ -918,50 +924,52 @@ pub const Builder = struct {
return null;
}

pub fn codeEditor(self: Self, rectangle: Rectangle, initial_code: []const u8, config: anytype) Error!*types.CodeEditor {
const info = try self.initOrUpdateWidget(.code_editor, rectangle, config);
const code_editor: *Widget.CodeEditor = info.control;
pub usingnamespace if (has_code_editor) struct {
pub fn codeEditor(self: Self, rectangle: Rectangle, initial_code: []const u8, config: anytype) Error!*types.CodeEditor {
const info = try self.initOrUpdateWidget(.code_editor, rectangle, config);
const code_editor: *Widget.CodeEditor = info.control;

// reset all events at the end of this
defer code_editor.events = std.enums.EnumSet(Widget.CodeEditor.Event){}; // clear
// reset all events at the end of this
defer code_editor.events = std.enums.EnumSet(Widget.CodeEditor.Event){}; // clear

const display_hash = StringHash.compute(initial_code);
const display_hash = StringHash.compute(initial_code);

if (info.needs_init) {
info.control.* = .{
.editor = undefined,
.content_hash = display_hash,
};
try code_editor.editor.create(self.ui.renderer.?);
try code_editor.editor.setText(initial_code);
if (info.needs_init) {
info.control.* = .{
.editor = undefined,
.content_hash = display_hash,
};
try code_editor.editor.create(self.ui.renderer.?);
try code_editor.editor.setText(initial_code);

// Clear all notifications created through init actions
_ = code_editor.editor.getNotifications();
} else {
// Clear all notifications created through init actions
_ = code_editor.editor.getNotifications();
} else {

// // clear text box to default when ESC is pressed or the input string changes
// if ((code_editor.content_hash != display_hash) or code_editor.events.contains(.cancelled)) {
// logger.info("updating code editor content to {s}", .{initial_code});
// try code_editor.editor.setText(initial_code);
// code_editor.content_hash = display_hash;
// // clear text box to default when ESC is pressed or the input string changes
// if ((code_editor.content_hash != display_hash) or code_editor.events.contains(.cancelled)) {
// logger.info("updating code editor content to {s}", .{initial_code});
// try code_editor.editor.setText(initial_code);
// code_editor.content_hash = display_hash;

// // Clear all notifications created through changing the text
// _ = code_editor.editor.getNotifications();
// }
}
updateWidgetConfig(&code_editor.config, config);
// // Clear all notifications created through changing the text
// _ = code_editor.editor.getNotifications();
// }
}
updateWidgetConfig(&code_editor.config, config);

code_editor.editor.setPosition(rectangle);
code_editor.editor.setPosition(rectangle);

if (code_editor.tick_increment == 0) {
code_editor.editor.tick();
code_editor.tick_increment = 6;
} else {
code_editor.tick_increment -= 1;
}
if (code_editor.tick_increment == 0) {
code_editor.editor.tick();
code_editor.tick_increment = 6;
} else {
code_editor.tick_increment -= 1;
}

return &code_editor.editor;
}
return &code_editor.editor;
}
} else struct {};
};

pub fn processInput(self: *UserInterface) InputProcessor {
Expand Down Expand Up @@ -1011,10 +1019,12 @@ pub const InputProcessor = struct {
if (self.ui.hovered_widget) |widget| {
switch (widget.control) {
.code_editor => |*control| {
control.editor.mouseMove(
self.ui.pointer_position.x,
self.ui.pointer_position.y,
);
if (has_code_editor) {
control.editor.mouseMove(
self.ui.pointer_position.x,
self.ui.pointer_position.y,
);
}
},
else => {},
}
Expand All @@ -1029,11 +1039,13 @@ pub const InputProcessor = struct {
if (clicked_widget) |widget| {
switch (widget.control) {
.code_editor => |*control| {
control.editor.mouseDown(
@intToFloat(f32, types.milliTimestamp()) / 1000.0,
self.ui.pointer_position.x,
self.ui.pointer_position.y,
);
if (has_code_editor) {
control.editor.mouseDown(
@intToFloat(f32, types.milliTimestamp()) / 1000.0,
self.ui.pointer_position.x,
self.ui.pointer_position.y,
);
}
},
else => {},
}
Expand Down Expand Up @@ -1061,11 +1073,13 @@ pub const InputProcessor = struct {

switch (widget.control) {
.code_editor => |*control| {
control.editor.mouseUp(
@intToFloat(f32, types.milliTimestamp()) / 1000.0,
self.ui.pointer_position.x,
self.ui.pointer_position.y,
);
if (has_code_editor) {
control.editor.mouseUp(
@intToFloat(f32, types.milliTimestamp()) / 1000.0,
self.ui.pointer_position.x,
self.ui.pointer_position.y,
);
}
},
else => {},
}
Expand All @@ -1091,6 +1105,7 @@ pub const InputProcessor = struct {
const active_widget = self.ui.focused_widget orelse return;
switch (active_widget.control) {
.code_editor => |*control| {
if (!has_code_editor) return;
switch (button) {
.ctrl_left, .ctrl_right => control.ctrl_pressed = true,
.shift_left, .shift_right => control.shift_pressed = true,
Expand Down Expand Up @@ -1142,7 +1157,7 @@ pub const InputProcessor = struct {
pub fn buttonUp(self: Self, button: types.Input.Scancode) !void {
const active_widget = self.ui.focused_widget orelse return;
switch (active_widget.control) {
.code_editor => |*control| switch (button) {
.code_editor => |*control| if (has_code_editor) switch (button) {
.ctrl_left, .ctrl_right => control.ctrl_pressed = false,
.shift_left, .shift_right => control.shift_pressed = false,
.alt_left, .alt_right => control.alt_pressed = false,
Expand Down Expand Up @@ -1200,7 +1215,8 @@ pub const InputProcessor = struct {
},

.code_editor => |*control| {
control.editor.enterString(string);
if (has_code_editor)
control.editor.enterString(string);
},

else => return, // just eat the event by default
Expand Down Expand Up @@ -1427,7 +1443,8 @@ pub fn render(self: UserInterface) !void {
},

.code_editor => |*control| {
control.editor.render();
if (has_code_editor)
control.editor.render();
},

.label => |control| {
Expand Down
Loading

0 comments on commit 73b1547

Please sign in to comment.