diff --git a/main.zig b/main.zig index c9492e8..183e60a 100644 --- a/main.zig +++ b/main.zig @@ -723,10 +723,18 @@ const Seat = struct { pointer: ?struct { pointer: *wl.Pointer, current_surface_offset: ?Point = null, + stage: SelectionStage = .Edit, selection: SelectionState = SelectionState.None(), } = null, + // awful naming but thats life innit + // this encodes whether the selection is actively being edited or moved as a whole + const SelectionStage = enum { + Edit, + Move, + }; + const SelectionTag = enum { none, pre, @@ -745,8 +753,8 @@ const Seat = struct { return SelectionState.none; } - fn translate(self: SelectionState, delta: Point) void { - switch (self) { + fn translate(self: *SelectionState, delta: Point) void { + switch (self.*) { .pre => |*point| { point.add(delta); }, @@ -757,7 +765,7 @@ const Seat = struct { .post => |*box| { box.translate(delta); }, - else => {return null;}, + else => {}, } } @@ -817,17 +825,40 @@ const Seat = struct { if (self.pointer.?.current_surface_offset) |offset| { const point = xy.added(offset); - switch (self.pointer.?.selection) { - .pre => { - self.pointer.?.selection = .{.updating = .{.start = point, .end = point}}; - return self.calculateRedraws(); + switch (self.pointer.?.stage) { + .Edit => { + switch (self.pointer.?.selection) { + .pre => { + self.pointer.?.selection = .{.updating = .{.start = point, .end = point}}; + return self.calculateRedraws(); + }, + .updating => |*selection| { + selection.end = point; + return self.calculateRedraws(); + }, + else => {}, + } }, - .updating => |*selection| { - selection.end = point; - return self.calculateRedraws(); + .Move => { + const delta = blk: { + switch (self.pointer.?.selection) { + .pre => |end| { + break :blk point.added(end.inverted()); + }, + .updating => |selection| { + break :blk point.added(selection.end.inverted()); + }, + else => {break :blk null;}, + } + }; + + if (delta != null) { + self.pointer.?.selection.translate(delta.?); + return self.calculateRedraws(); + } }, - else => {}, } + } }, .button => |button| { @@ -933,10 +964,24 @@ const Seat = struct { } }, + xkb.XKB_KEY_space => { + if (self.pointer) |*ptr| { + ptr.stage = .Move; + } + }, + else => {}, + } + }, + .released => { + switch (keysym) { + xkb.XKB_KEY_space => { + if (self.pointer) |*ptr| { + ptr.stage = .Edit; + } + }, else => {}, } }, - .released => {}, else => {}, } }