river/river/DragIcon.zig

80 lines
2.6 KiB
Zig
Raw Normal View History

2020-09-09 16:13:47 -07:00
// This file is part of river, a dynamic tiling wayland compositor.
//
// Copyright 2020-2021 The River Developers
2020-09-09 16:13:47 -07:00
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, version 3.
2020-09-09 16:13:47 -07:00
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
const DragIcon = @This();
2020-09-09 16:13:47 -07:00
const std = @import("std");
const wlr = @import("wlroots");
const wl = @import("wayland").server.wl;
2020-09-09 16:13:47 -07:00
const server = &@import("main.zig").server;
2020-09-09 16:13:47 -07:00
const util = @import("util.zig");
const Cursor = @import("Cursor.zig");
2023-02-16 07:54:53 -08:00
const SceneNodeData = @import("SceneNodeData.zig");
2020-09-09 16:13:47 -07:00
wlr_drag_icon: *wlr.Drag.Icon,
2023-12-01 05:57:18 -08:00
scene_drag_icon: *wlr.SceneTree,
2022-12-30 13:03:10 -08:00
2020-12-31 06:35:35 -08:00
destroy: wl.Listener(*wlr.Drag.Icon) = wl.Listener(*wlr.Drag.Icon).init(handleDestroy),
pub fn create(wlr_drag_icon: *wlr.Drag.Icon, cursor: *Cursor) error{OutOfMemory}!void {
2023-12-01 05:57:18 -08:00
const scene_drag_icon = try server.root.drag_icons.createSceneDragIcon(wlr_drag_icon);
errdefer scene_drag_icon.node.destroy();
2023-02-16 07:54:53 -08:00
const drag_icon = try util.gpa.create(DragIcon);
errdefer util.gpa.destroy(drag_icon);
drag_icon.* = .{
.wlr_drag_icon = wlr_drag_icon,
2023-12-01 05:57:18 -08:00
.scene_drag_icon = scene_drag_icon,
2023-02-16 07:54:53 -08:00
};
2023-12-01 05:57:18 -08:00
scene_drag_icon.node.data = @intFromPtr(drag_icon);
2023-02-16 07:54:53 -08:00
drag_icon.updatePosition(cursor);
wlr_drag_icon.events.destroy.add(&drag_icon.destroy);
2020-09-09 16:13:47 -07:00
}
pub fn updatePosition(drag_icon: *DragIcon, cursor: *Cursor) void {
switch (drag_icon.wlr_drag_icon.drag.grab_type) {
.keyboard => unreachable,
.keyboard_pointer => {
2023-12-01 05:57:18 -08:00
drag_icon.scene_drag_icon.node.setPosition(
@intFromFloat(cursor.wlr_cursor.x),
@intFromFloat(cursor.wlr_cursor.y),
);
},
.keyboard_touch => {
const touch_id = drag_icon.wlr_drag_icon.drag.touch_id;
if (cursor.touch_points.get(touch_id)) |point| {
2023-12-01 05:57:18 -08:00
drag_icon.scene_drag_icon.node.setPosition(
@intFromFloat(point.lx),
@intFromFloat(point.ly),
);
}
},
}
}
fn handleDestroy(listener: *wl.Listener(*wlr.Drag.Icon), _: *wlr.Drag.Icon) void {
const drag_icon = @fieldParentPtr(DragIcon, "destroy", listener);
drag_icon.destroy.link.remove();
2022-12-30 13:03:10 -08:00
util.gpa.destroy(drag_icon);
2020-09-09 16:13:47 -07:00
}