Skip to content

Commit b31f775

Browse files
authored
Close on click (#193)
1 parent 3e030e9 commit b31f775

4 files changed

Lines changed: 77 additions & 14 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
- Support some actions with pointer (e.g mouse).
66
- Add flag to hide input bar.
77
- Adaptive height config option.
8+
- Quit on outer region click.
89

910
## Changes
1011

‎src/window.rs‎

Lines changed: 55 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,12 @@ enum RenderSurface {
8282
LayerShell(wlr_layer::LayerSurface),
8383
}
8484

85+
impl RenderSurface {
86+
fn is_overlay(&self) -> bool {
87+
matches!(self, RenderSurface::LayerShell(_))
88+
}
89+
}
90+
8591
impl std::ops::Deref for RenderSurface {
8692
type Target = WlSurface;
8793

@@ -132,11 +138,18 @@ impl Window {
132138
None,
133139
);
134140

135-
if let Some((top_offset, left_offset)) = params.window_offsets {
136-
layer.set_anchor(wlr_layer::Anchor::LEFT | wlr_layer::Anchor::TOP);
137-
layer.set_margin(top_offset, 0, 0, left_offset);
138-
}
139-
layer.set_size(width, height);
141+
layer.set_anchor(
142+
wlr_layer::Anchor::TOP
143+
| wlr_layer::Anchor::BOTTOM
144+
| wlr_layer::Anchor::LEFT
145+
| wlr_layer::Anchor::RIGHT,
146+
);
147+
// If you pass 0 for either value, the compositor will assign it and inform you
148+
// of the assignment in the configure event. You must set your anchor to opposite
149+
// edges in the dimensions you omit; not doing so is a protocol error.
150+
// src: https://wayland.app/protocols/wlr-layer-shell-unstable-v1
151+
layer.set_size(0, 0);
152+
layer.set_exclusive_zone(-1);
140153
layer.set_keyboard_interactivity(wlr_layer::KeyboardInteractivity::Exclusive);
141154

142155
layer.commit();
@@ -189,6 +202,28 @@ impl Window {
189202
self.height * u32::from(self.scale)
190203
}
191204

205+
fn content_size(&self) -> (u32, u32) {
206+
let params: Params = self.config.param();
207+
(params.width, params.height)
208+
}
209+
210+
/// Returns the (x, y) pixel offset to position the content area within the
211+
/// surface, in logical (unscaled) coordinates.
212+
fn content_offset(&self) -> (f32, f32) {
213+
if !self.surface.is_overlay() {
214+
return (0.0, 0.0);
215+
}
216+
let params: Params = self.config.param();
217+
if let Some((top, left)) = params.window_offsets {
218+
(left as f32, top as f32)
219+
} else {
220+
let (cw, ch) = self.content_size();
221+
let x = (self.width as f32 - cw as f32) / 2.0;
222+
let y = (self.height as f32 - ch as f32) / 2.0;
223+
(x, y)
224+
}
225+
}
226+
192227
pub fn draw(&mut self, qh: &QueueHandle<Self>) {
193228
let width = self.width().try_into().expect("width overflow");
194229
let height = self.height().try_into().expect("height overflow");
@@ -212,6 +247,18 @@ impl Window {
212247
.0
213248
});
214249

250+
let overlay = self.surface.is_overlay();
251+
let (content_w, content_h) = if overlay {
252+
let (cw, ch) = self.content_size();
253+
let s = i32::from(self.scale);
254+
(cw as i32 * s, ch as i32 * s)
255+
} else {
256+
(width, height)
257+
};
258+
let (offset_x, offset_y) = self.content_offset();
259+
let scaled_offset_x = offset_x * self.scale as f32;
260+
let scaled_offset_y = offset_y * self.scale as f32;
261+
215262
let canvas = match self.pool.canvas(&buffer) {
216263
Some(canvas) => canvas,
217264
None => {
@@ -247,10 +294,10 @@ impl Window {
247294
};
248295

249296
let mut space_left = Space {
250-
width: width as f32,
251-
height: height as f32,
297+
width: content_w as f32,
298+
height: content_h as f32,
252299
};
253-
let mut point = Point::new(0., 0.);
300+
let mut point = Point::new(scaled_offset_x, scaled_offset_y);
254301

255302
let (mut drawables, dyn_space) =
256303
crate::draw::make_drawables(&self.config, &mut self.state, self.scale);

‎src/window/layer_shell.rs‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,9 @@ impl LayerShellHandler for Window {
2323
_serial: u32,
2424
) {
2525
let (w, h) = configure.new_size;
26-
self.width = w;
27-
self.height = h;
26+
let (cw, ch) = self.content_size();
27+
self.width = if w > 0 { w } else { cw };
28+
self.height = if h > 0 { h } else { ch };
2829
layer.set_keyboard_interactivity(KeyboardInteractivity::Exclusive);
2930

3031
if !self.configured_surface {

‎src/window/pointer.rs‎

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,24 @@ impl PointerHandler for Window {
3434
}
3535

3636
match event.kind {
37-
// TODO: implement precise clicks on items
38-
// PointerEventKind::Release {
39-
// button: BTN_LEFT, ..
40-
// } => ..,
37+
PointerEventKind::Release {
38+
button: BTN_LEFT, ..
39+
} if self.surface.is_overlay() => {
40+
let (ox, oy) = self.content_offset();
41+
let left = f64::from(ox);
42+
let top = f64::from(oy);
43+
let (cw, ch) = self.content_size();
44+
let right = cw as f64 + left;
45+
let bottom = ch as f64 + top;
46+
47+
let (px, py) = event.position;
48+
if px < left || py < top || px > right || py > bottom {
49+
self.exit = true;
50+
} else {
51+
// TODO: implement precise clicks on items
52+
continue;
53+
}
54+
}
4155
PointerEventKind::Release {
4256
button: BTN_MIDDLE, ..
4357
} if config.launch_on_middle => {

0 commit comments

Comments
 (0)