code: simplify direction parsing

This commit is contained in:
Isaac Freund 2020-07-15 13:02:58 +02:00
parent 1389b4a850
commit 8afe7c2c87
No known key found for this signature in database
GPG Key ID: 86DED400DDFD7A11
4 changed files with 13 additions and 22 deletions

View File

@ -20,17 +20,8 @@ const std = @import("std");
const Seat = @import("Seat.zig"); const Seat = @import("Seat.zig");
pub const Direction = enum { pub const Direction = enum {
Next, next,
Prev, previous,
pub fn parse(str: []const u8) error{InvalidDirection}!Direction {
return if (std.mem.eql(u8, str, "next"))
Direction.Next
else if (std.mem.eql(u8, str, "previous"))
Direction.Prev
else
error.InvalidDirection;
}
}; };
// TODO: this could be replaced with a comptime hashmap // TODO: this could be replaced with a comptime hashmap

View File

@ -33,7 +33,7 @@ pub fn focusOutput(
if (args.len < 2) return Error.NotEnoughArguments; if (args.len < 2) return Error.NotEnoughArguments;
if (args.len > 2) return Error.TooManyArguments; if (args.len > 2) return Error.TooManyArguments;
const direction = try Direction.parse(args[1]); const direction = std.meta.stringToEnum(Direction, args[1]) orelse return Error.InvalidDirection;
const root = &seat.input_manager.server.root; const root = &seat.input_manager.server.root;
// If the noop output is focused, there are no other outputs to switch to // If the noop output is focused, there are no other outputs to switch to
@ -45,8 +45,8 @@ pub fn focusOutput(
// Focus the next/prev output in the list if there is one, else wrap // Focus the next/prev output in the list if there is one, else wrap
const focused_node = @fieldParentPtr(std.TailQueue(Output).Node, "data", seat.focused_output); const focused_node = @fieldParentPtr(std.TailQueue(Output).Node, "data", seat.focused_output);
seat.focusOutput(switch (direction) { seat.focusOutput(switch (direction) {
.Next => if (focused_node.next) |node| &node.data else &root.outputs.first.?.data, .next => if (focused_node.next) |node| &node.data else &root.outputs.first.?.data,
.Prev => if (focused_node.prev) |node| &node.data else &root.outputs.last.?.data, .previous => if (focused_node.prev) |node| &node.data else &root.outputs.last.?.data,
}); });
seat.focus(null); seat.focus(null);

View File

@ -34,7 +34,7 @@ pub fn focusView(
if (args.len < 2) return Error.NotEnoughArguments; if (args.len < 2) return Error.NotEnoughArguments;
if (args.len > 2) return Error.TooManyArguments; if (args.len > 2) return Error.TooManyArguments;
const direction = try Direction.parse(args[1]); const direction = std.meta.stringToEnum(Direction, args[1]) orelse return Error.InvalidDirection;
const output = seat.focused_output; const output = seat.focused_output;
if (seat.focused_view) |current_focus| { if (seat.focused_view) |current_focus| {
@ -44,8 +44,8 @@ pub fn focusView(
// If there is a currently focused view, focus the next visible view in the stack. // If there is a currently focused view, focus the next visible view in the stack.
const focused_node = @fieldParentPtr(ViewStack(View).Node, "view", current_focus); const focused_node = @fieldParentPtr(ViewStack(View).Node, "view", current_focus);
var it = switch (direction) { var it = switch (direction) {
.Next => ViewStack(View).iterator(focused_node, output.current.tags), .next => ViewStack(View).iterator(focused_node, output.current.tags),
.Prev => ViewStack(View).reverseIterator(focused_node, output.current.tags), .previous => ViewStack(View).reverseIterator(focused_node, output.current.tags),
}; };
// Skip past the focused node // Skip past the focused node
@ -60,8 +60,8 @@ pub fn focusView(
// There is either no currently focused view or the last visible view in the // There is either no currently focused view or the last visible view in the
// stack is focused and we need to wrap. // stack is focused and we need to wrap.
var it = switch (direction) { var it = switch (direction) {
.Next => ViewStack(View).iterator(output.views.first, output.current.tags), .next => ViewStack(View).iterator(output.views.first, output.current.tags),
.Prev => ViewStack(View).reverseIterator(output.views.last, output.current.tags), .previous => ViewStack(View).reverseIterator(output.views.last, output.current.tags),
}; };
seat.focus(if (it.next()) |node| &node.view else null); seat.focus(if (it.next()) |node| &node.view else null);

View File

@ -33,7 +33,7 @@ pub fn sendToOutput(
if (args.len < 2) return Error.NotEnoughArguments; if (args.len < 2) return Error.NotEnoughArguments;
if (args.len > 2) return Error.TooManyArguments; if (args.len > 2) return Error.TooManyArguments;
const direction = try Direction.parse(args[1]); const direction = std.meta.stringToEnum(Direction, args[1]) orelse return Error.InvalidDirection;
const root = &seat.input_manager.server.root; const root = &seat.input_manager.server.root;
if (seat.focused_view) |view| { if (seat.focused_view) |view| {
@ -46,8 +46,8 @@ pub fn sendToOutput(
// Send to the next/prev output in the list if there is one, else wrap // Send to the next/prev output in the list if there is one, else wrap
const current_node = @fieldParentPtr(std.TailQueue(Output).Node, "data", view.output); const current_node = @fieldParentPtr(std.TailQueue(Output).Node, "data", view.output);
const destination_output = switch (direction) { const destination_output = switch (direction) {
.Next => if (current_node.next) |node| &node.data else &root.outputs.first.?.data, .next => if (current_node.next) |node| &node.data else &root.outputs.first.?.data,
.Prev => if (current_node.prev) |node| &node.data else &root.outputs.last.?.data, .previous => if (current_node.prev) |node| &node.data else &root.outputs.last.?.data,
}; };
// Move the view to the target output // Move the view to the target output