diff --git a/src/lua/LuaUtils.zig b/src/lua/LuaUtils.zig new file mode 100644 index 0000000..d99f30d --- /dev/null +++ b/src/lua/LuaUtils.zig @@ -0,0 +1,26 @@ +const LuaUtils = @This(); + +const std = @import("std"); +const zlua = @import("zlua"); + +pub fn coerceNumber(comptime x: type, number: zlua.Number) error{InvalidNumber}!x { + if (number < std.math.minInt(x) or number > std.math.maxInt(x) or std.math.isNan(number)) { + return error.InvalidNumber; + } + switch (@typeInfo(x)) { + .int => return @as(x, @intFromFloat(number)), + .float => return @floatCast(number), + else => @compileError("unsupported type"), + } +} + +pub fn coerceInteger(comptime x: type, number: zlua.Integer) error{InvalidInteger}!x { + if (number < std.math.minInt(x) or number > std.math.maxInt(x) or std.math.isNan(number)) { + return error.InvalidInteger; + } + switch (@typeInfo(x)) { + .int => return @intCast(number), + .float => return @as(x, @floatFromInt(number)), + else => @compileError("unsupported type"), + } +} diff --git a/src/lua/View.zig b/src/lua/View.zig index fc0bb40..ffea58e 100644 --- a/src/lua/View.zig +++ b/src/lua/View.zig @@ -2,6 +2,7 @@ const std = @import("std"); const zlua = @import("zlua"); const View = @import("../View.zig"); +const LuaUtils = @import("LuaUtils.zig"); const server = &@import("../main.zig").server; @@ -87,8 +88,8 @@ pub fn set_position(L: *zlua.Lua) i32 { // ---@param height number height for view pub fn set_size(L: *zlua.Lua) i32 { const view_id: u64 = @intCast(L.checkInteger(1)); - const width: i32 = @intFromFloat(@round(L.checkNumber(2))); - const height: i32 = @intFromFloat(@round(L.checkNumber(3))); + const width = LuaUtils.coerceNumber(i32, L.checkNumber(2)) catch L.raiseErrorStr("The width must be >= 0 and < inf", .{}); + const height = LuaUtils.coerceNumber(i32, L.checkNumber(3)) catch L.raiseErrorStr("The height must be >= 0 and < inf", .{}); const view: ?*View = if (view_id == 0) server.seat.focused_view else server.root.viewById(view_id); if(view) |v| { @@ -99,6 +100,27 @@ pub fn set_size(L: *zlua.Lua) i32 { return 1; } +pub fn get_size(L: *zlua.Lua) i32 { + const view_id: u64 = @intCast(L.checkInteger(1)); + const view: ?*View = if (view_id == 0) server.seat.focused_view else server.root.viewById(view_id); + if (view) |v| { + L.newTable(); + + _ = L.pushString("width"); + L.pushInteger(@intCast(v.xdg_toplevel.current.width)); + L.setTable(-3); + + _ = L.pushString("height"); + L.pushInteger(@intCast(v.xdg_toplevel.current.height)); + L.setTable(-3); + + return 1; + } + + L.pushNil(); + return 1; +} + // ---Remove focus from current view, and set to given id // ---@param view_id view_id Id of the view to be focused, or nil to remove focus pub fn set_focused(L: *zlua.Lua) i32 {