output: simplify layout code
This commit is contained in:
parent
56df9176b3
commit
05557086e6
@ -166,7 +166,7 @@ pub fn sendViewTags(self: Self) void {
|
|||||||
|
|
||||||
/// The single build in layout, which makes all views use the maximum available
|
/// The single build in layout, which makes all views use the maximum available
|
||||||
/// space.
|
/// space.
|
||||||
fn layoutFull(self: *Self, visible_count: u32, output_tags: u32) void {
|
fn layoutFull(self: *Self, visible_count: u32) void {
|
||||||
const border_width = self.root.server.config.border_width;
|
const border_width = self.root.server.config.border_width;
|
||||||
const view_padding = self.root.server.config.view_padding;
|
const view_padding = self.root.server.config.view_padding;
|
||||||
const outer_padding = self.root.server.config.outer_padding;
|
const outer_padding = self.root.server.config.outer_padding;
|
||||||
@ -179,7 +179,7 @@ fn layoutFull(self: *Self, visible_count: u32, output_tags: u32) void {
|
|||||||
.height = self.usable_box.height - (2 * xy_offset),
|
.height = self.usable_box.height - (2 * xy_offset),
|
||||||
};
|
};
|
||||||
|
|
||||||
var it = ViewStack(View).pendingIterator(self.views.first, output_tags);
|
var it = ViewStack(View).pendingIterator(self.views.first, self.pending.tags);
|
||||||
while (it.next()) |node| {
|
while (it.next()) |node| {
|
||||||
const view = &node.view;
|
const view = &node.view;
|
||||||
if (!view.pending.float and !view.pending.fullscreen) {
|
if (!view.pending.float and !view.pending.fullscreen) {
|
||||||
@ -191,31 +191,28 @@ fn layoutFull(self: *Self, visible_count: u32, output_tags: u32) void {
|
|||||||
|
|
||||||
const LayoutError = error{
|
const LayoutError = error{
|
||||||
BadExitCode,
|
BadExitCode,
|
||||||
BadWindowConfiguration,
|
WrongViewCount,
|
||||||
ConfigurationMismatch,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
/// Parse a window configuration string and write values to the box
|
/// Parse 4 integers separated by spaces into a Box
|
||||||
fn parseWindowConfig(buffer: []const u8) LayoutError!Box {
|
fn parseBox(buffer: []const u8) !Box {
|
||||||
var i: u32 = 0;
|
|
||||||
var box: Box = undefined;
|
|
||||||
var it = std.mem.split(buffer, " ");
|
var it = std.mem.split(buffer, " ");
|
||||||
while (it.next()) |token| : (i += 1) {
|
|
||||||
switch (i) {
|
const box = Box{
|
||||||
0 => box.x = std.fmt.parseInt(i32, token, 10) catch return LayoutError.BadWindowConfiguration,
|
.x = try std.fmt.parseInt(i32, it.next() orelse return error.NotEnoughArguments, 10),
|
||||||
1 => box.y = std.fmt.parseInt(i32, token, 10) catch return LayoutError.BadWindowConfiguration,
|
.y = try std.fmt.parseInt(i32, it.next() orelse return error.NotEnoughArguments, 10),
|
||||||
2 => box.width = std.fmt.parseInt(u32, token, 10) catch return LayoutError.BadWindowConfiguration,
|
.width = try std.fmt.parseInt(u32, it.next() orelse return error.NotEnoughArguments, 10),
|
||||||
3 => box.height = std.fmt.parseInt(u32, token, 10) catch return LayoutError.BadWindowConfiguration,
|
.height = try std.fmt.parseInt(u32, it.next() orelse return error.NotEnoughArguments, 10),
|
||||||
else => {},
|
};
|
||||||
}
|
|
||||||
}
|
if (it.next() != null) return error.TooManyArguments;
|
||||||
if (i != 4) return LayoutError.BadWindowConfiguration;
|
|
||||||
return box;
|
return box;
|
||||||
}
|
}
|
||||||
|
|
||||||
test "parse window configuration" {
|
test "parse window configuration" {
|
||||||
const testing = @import("std").testing;
|
const testing = @import("std").testing;
|
||||||
var box = try parseWindowConfig("5 10 100 200");
|
const box = try parseBox("5 10 100 200");
|
||||||
testing.expect(box.x == 5);
|
testing.expect(box.x == 5);
|
||||||
testing.expect(box.y == 10);
|
testing.expect(box.y == 10);
|
||||||
testing.expect(box.width == 100);
|
testing.expect(box.width == 100);
|
||||||
@ -224,7 +221,7 @@ test "parse window configuration" {
|
|||||||
|
|
||||||
/// Execute an external layout function, parse its output and apply the layout
|
/// Execute an external layout function, parse its output and apply the layout
|
||||||
/// to the output.
|
/// to the output.
|
||||||
fn layoutExternal(self: *Self, visible_count: u32, output_tags: u32) !void {
|
fn layoutExternal(self: *Self, visible_count: u32) !void {
|
||||||
const config = self.root.server.config;
|
const config = self.root.server.config;
|
||||||
const xy_offset = @intCast(i32, config.border_width + config.outer_padding + config.view_padding);
|
const xy_offset = @intCast(i32, config.border_width + config.outer_padding + config.view_padding);
|
||||||
const delta_size = (config.border_width + config.view_padding) * 2;
|
const delta_size = (config.border_width + config.view_padding) * 2;
|
||||||
@ -267,7 +264,7 @@ fn layoutExternal(self: *Self, visible_count: u32, output_tags: u32) !void {
|
|||||||
var parse_it = std.mem.split(buffer, "\n");
|
var parse_it = std.mem.split(buffer, "\n");
|
||||||
while (parse_it.next()) |token| {
|
while (parse_it.next()) |token| {
|
||||||
if (std.mem.eql(u8, token, "")) break;
|
if (std.mem.eql(u8, token, "")) break;
|
||||||
var box = try parseWindowConfig(token);
|
var box = try parseBox(token);
|
||||||
box.x += self.usable_box.x + xy_offset;
|
box.x += self.usable_box.x + xy_offset;
|
||||||
box.y += self.usable_box.y + xy_offset;
|
box.y += self.usable_box.y + xy_offset;
|
||||||
box.width -= delta_size;
|
box.width -= delta_size;
|
||||||
@ -275,11 +272,11 @@ fn layoutExternal(self: *Self, visible_count: u32, output_tags: u32) !void {
|
|||||||
try view_boxen.append(box);
|
try view_boxen.append(box);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (view_boxen.items.len != visible_count) return LayoutError.ConfigurationMismatch;
|
if (view_boxen.items.len != visible_count) return LayoutError.WrongViewCount;
|
||||||
|
|
||||||
// Apply window configuration to views
|
// Apply window configuration to views
|
||||||
var i: u32 = 0;
|
var i: u32 = 0;
|
||||||
var view_it = ViewStack(View).pendingIterator(self.views.first, output_tags);
|
var view_it = ViewStack(View).pendingIterator(self.views.first, self.pending.tags);
|
||||||
while (view_it.next()) |node| {
|
while (view_it.next()) |node| {
|
||||||
const view = &node.view;
|
const view = &node.view;
|
||||||
if (!view.pending.float and !view.pending.fullscreen) {
|
if (!view.pending.float and !view.pending.fullscreen) {
|
||||||
@ -308,17 +305,16 @@ pub fn arrangeViews(self: *Self) void {
|
|||||||
// would cause an underflow and is pointless anyway.
|
// would cause an underflow and is pointless anyway.
|
||||||
if (layout_count == 0 or self.usable_box.width == 0 or self.usable_box.height == 0) return;
|
if (layout_count == 0 or self.usable_box.width == 0 or self.usable_box.height == 0) return;
|
||||||
|
|
||||||
if (std.mem.eql(u8, self.layout, "full")) return layoutFull(self, layout_count, self.pending.tags);
|
if (std.mem.eql(u8, self.layout, "full")) return layoutFull(self, layout_count);
|
||||||
|
|
||||||
layoutExternal(self, layout_count, self.pending.tags) catch |err| {
|
self.layoutExternal(layout_count) catch |err| {
|
||||||
switch (err) {
|
switch (err) {
|
||||||
LayoutError.BadExitCode => log.err(.layout, "layout command exited with non-zero return code", .{}),
|
LayoutError.BadExitCode => log.err(.layout, "layout command exited with non-zero return code", .{}),
|
||||||
LayoutError.BadWindowConfiguration => log.err(.layout, "invalid window configuration", .{}),
|
LayoutError.WrongViewCount => log.err(.layout, "mismatch between window configuration and visible window counts", .{}),
|
||||||
LayoutError.ConfigurationMismatch => log.err(.layout, "mismatch between window configuration and visible window counts", .{}),
|
else => log.err(.layout, "failed to use external layout: {}", .{err}),
|
||||||
else => log.err(.layout, "'{}' error while trying to use external layout", .{err}),
|
|
||||||
}
|
}
|
||||||
log.err(.layout, "falling back to internal layout", .{});
|
log.err(.layout, "falling back to internal layout", .{});
|
||||||
layoutFull(self, layout_count, self.pending.tags);
|
self.layoutFull(layout_count);
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user