view: use a mode enum instead of floating bool

This is in preperation for fullscreen support which will add another
mode to this enum.

This commit also fixes a bug that allowed clicking floating views though
layout views in some cases.
This commit is contained in:
Isaac Freund 2020-06-27 17:33:09 +02:00
parent 3904275373
commit 89d0fb012d
No known key found for this signature in database
GPG Key ID: 86DED400DDFD7A11
7 changed files with 58 additions and 73 deletions

View File

@ -286,8 +286,7 @@ fn processMotion(self: Self, time: u32) void {
fn surfaceAt(self: Self, lx: f64, ly: f64, sx: *f64, sy: *f64) ?*c.wlr_surface {
// Find the output to check
const root = self.seat.input_manager.server.root;
const wlr_output = c.wlr_output_layout_output_at(root.wlr_output_layout, lx, ly) orelse
return null;
const wlr_output = c.wlr_output_layout_output_at(root.wlr_output_layout, lx, ly) orelse return null;
const output = util.voidCast(Output, wlr_output.*.data orelse return null);
// Get output-local coords from the layout coords
@ -304,36 +303,21 @@ fn surfaceAt(self: Self, lx: f64, ly: f64, sx: *f64, sy: *f64) ?*c.wlr_surface {
};
// Check overlay layer incl. popups
if (layerSurfaceAt(output.*, output.layers[layer_idxs[0]], ox, oy, sx, sy, false)) |surface| {
return surface;
}
if (layerSurfaceAt(output.*, output.layers[layer_idxs[0]], ox, oy, sx, sy, false)) |s| return s;
// Check top-background popups only
for (layer_idxs[1..4]) |layer_idx| {
if (layerSurfaceAt(output.*, output.layers[layer_idx], ox, oy, sx, sy, true)) |surface| {
return surface;
}
}
for (layer_idxs[1..4]) |idx|
if (layerSurfaceAt(output.*, output.layers[idx], ox, oy, sx, sy, true)) |s| return s;
// Check top layer
if (layerSurfaceAt(output.*, output.layers[layer_idxs[1]], ox, oy, sx, sy, false)) |surface| {
return surface;
}
if (layerSurfaceAt(output.*, output.layers[layer_idxs[1]], ox, oy, sx, sy, false)) |s| return s;
// Check floating views then normal views
if (viewSurfaceAt(output.*, ox, oy, sx, sy, true)) |surface| {
return surface;
}
if (viewSurfaceAt(output.*, ox, oy, sx, sy, false)) |surface| {
return surface;
}
// Check views
if (viewSurfaceAt(output.*, ox, oy, sx, sy)) |s| return s;
// Check the bottom-background layers
for (layer_idxs[2..4]) |layer_idx| {
if (layerSurfaceAt(output.*, output.layers[layer_idx], ox, oy, sx, sy, false)) |surface| {
return surface;
}
}
for (layer_idxs[2..4]) |idx|
if (layerSurfaceAt(output.*, output.layers[idx], ox, oy, sx, sy, false)) |s| return s;
return null;
}
@ -373,18 +357,10 @@ fn layerSurfaceAt(
return null;
}
/// Find the topmost visible view surface (incl. popups) at ox,oy. Will
/// check only floating views if floating is true.
fn viewSurfaceAt(output: Output, ox: f64, oy: f64, sx: *f64, sy: *f64, floating: bool) ?*c.wlr_surface {
/// Find the topmost visible view surface (incl. popups) at ox,oy.
fn viewSurfaceAt(output: Output, ox: f64, oy: f64, sx: *f64, sy: *f64) ?*c.wlr_surface {
var it = ViewStack(View).iterator(output.views.first, output.current_focused_tags);
while (it.next()) |node| {
const view = &node.view;
if (view.floating != floating) {
continue;
}
if (view.surfaceAt(ox, oy, sx, sy)) |found| {
return found;
}
}
return null;
return while (it.next()) |node| {
if (node.view.surfaceAt(ox, oy, sx, sy)) |found| break found;
} else null;
}

View File

@ -181,8 +181,7 @@ fn layoutFull(self: *Self, visible_count: u32, output_tags: u32) void {
var it = ViewStack(View).pendingIterator(self.views.first, output_tags);
while (it.next()) |node| {
const view = &node.view;
if (view.floating) continue;
view.pending_box = full_box;
if (view.mode == .layout) view.pending_box = full_box;
}
}
@ -287,11 +286,12 @@ fn layoutExternal(self: *Self, visible_count: u32, output_tags: u32) !void {
var view_it = ViewStack(View).pendingIterator(self.views.first, output_tags);
while (view_it.next()) |node| {
const view = &node.view;
if (view.floating) continue;
if (view.mode == .layout) {
view.pending_box = view_boxen.items[i];
i += 1;
}
}
}
/// Arrange all views on the output for the current layout. Modifies only
/// pending state, the changes are not appplied until a transaction is started
@ -310,8 +310,7 @@ pub fn arrangeViews(self: *Self) void {
var count: u32 = 0;
var it = ViewStack(View).pendingIterator(self.views.first, output_tags);
while (it.next()) |node| {
if (node.view.floating) continue;
count += 1;
if (node.view.mode == .layout) count += 1;
}
break :blk count;
};

View File

@ -29,16 +29,18 @@ const Output = @import("Output.zig");
const Root = @import("Root.zig");
const ViewStack = @import("view_stack.zig").ViewStack;
const XdgToplevel = @import("XdgToplevel.zig");
const XwaylandView = if (build_options.xwayland)
@import("XwaylandView.zig")
else
@import("VoidView.zig");
const XwaylandView = if (build_options.xwayland) @import("XwaylandView.zig") else @import("VoidView.zig");
const Impl = union(enum) {
xdg_toplevel: XdgToplevel,
xwayland_view: XwaylandView,
};
const Mode = enum {
layout,
float,
};
const SavedBuffer = struct {
wlr_buffer: *c.wlr_buffer,
box: Box,
@ -54,8 +56,8 @@ output: *Output,
/// This is non-null exactly when the view is mapped
wlr_surface: ?*c.wlr_surface,
/// If the view is floating or not
floating: bool,
/// The current mode of the view
mode: Mode,
/// True if the view is currently focused by at least one seat
focused: bool,
@ -92,6 +94,7 @@ pub fn init(self: *Self, output: *Output, tags: u32, surface: var) void {
self.output = output;
self.wlr_surface = null;
self.mode = .layout;
self.focused = false;
@ -196,11 +199,13 @@ pub fn setFocused(self: *Self, focused: bool) void {
}
}
/// If true is passsed, make the view float. If false, return it to the tiled
/// layout.
pub fn setFloating(self: *Self, float: bool) void {
if (float and !self.floating) {
self.floating = true;
/// Set the mode of the view to the given mode
pub fn setMode(self: *Self, mode: Mode) void {
switch (self.mode) {
.layout => switch (mode) {
.layout => {},
.float => {
self.mode = .float;
self.pending_box = Box{
.x = std.math.max(0, @divTrunc(@intCast(i32, self.output.usable_box.width) -
@intCast(i32, self.natural_width), 2)),
@ -209,8 +214,12 @@ pub fn setFloating(self: *Self, float: bool) void {
.width = self.natural_width,
.height = self.natural_height,
};
} else if (!float and self.floating) {
self.floating = false;
},
},
.float => switch (mode) {
.float => {},
.layout => self.mode = .layout,
},
}
}

View File

@ -152,7 +152,6 @@ fn handleMap(listener: ?*c.wl_listener, data: ?*c_void) callconv(.C) void {
c.wl_signal_add(&self.wlr_xdg_surface.events.new_popup, &self.listen_new_popup);
view.wlr_surface = self.wlr_xdg_surface.surface;
view.floating = false;
view.natural_width = @intCast(u32, self.wlr_xdg_surface.geometry.width);
view.natural_height = @intCast(u32, self.wlr_xdg_surface.geometry.height);
@ -172,7 +171,7 @@ fn handleMap(listener: ?*c.wl_listener, data: ?*c_void) callconv(.C) void {
for (root.server.config.float_filter.items) |filter_app_id| {
// Make views with app_ids listed in the float filter float
if (std.mem.eql(u8, std.mem.span(app_id), std.mem.span(filter_app_id))) {
view.setFloating(true);
view.setMode(.float);
break;
}
} else if ((wlr_xdg_toplevel.parent != null) or
@ -180,7 +179,7 @@ fn handleMap(listener: ?*c.wl_listener, data: ?*c_void) callconv(.C) void {
(state.min_width == state.max_width or state.min_height == state.max_height)))
{
// If the toplevel has a parent or is of fixed size make it float
view.setFloating(true);
view.setMode(.float);
}
// If the toplevel has no parent, inform it that it is tiled. This

View File

@ -140,7 +140,6 @@ fn handleMap(listener: ?*c.wl_listener, data: ?*c_void) callconv(.C) void {
c.wl_signal_add(&self.wlr_xwayland_surface.surface.*.events.commit, &self.listen_commit);
view.wlr_surface = self.wlr_xwayland_surface.surface;
view.floating = false;
view.natural_width = self.wlr_xwayland_surface.width;
view.natural_height = self.wlr_xwayland_surface.height;

View File

@ -30,7 +30,10 @@ pub fn toggleFloat(
) Error!void {
if (args.len > 1) return Error.TooManyArguments;
if (seat.focused_view) |view| {
view.setFloating(!view.floating);
switch (view.mode) {
.layout => view.setMode(.float),
.float => view.setMode(.layout),
}
view.output.root.arrange();
}
}

View File

@ -36,8 +36,8 @@ pub fn zoom(
const output = seat.focused_output;
const focused_node = @fieldParentPtr(ViewStack(View).Node, "view", current_focus);
// Don't zoom floating views
if (current_focus.floating) return;
// Only zoom views that are part of the layout
if (current_focus.mode != .layout) return;
var it = ViewStack(View).iterator(output.views.first, output.current_focused_tags);
const zoom_node = if (focused_node == it.next())