Skip to content

Commit

Permalink
Use wayland-csd-frame as a part of public API
Browse files Browse the repository at this point in the history
This new crate is used to provide common interface for the CSD
libraries.
  • Loading branch information
kchibisov committed Aug 11, 2023
1 parent f8ca6fa commit 6454c81
Show file tree
Hide file tree
Showing 7 changed files with 163 additions and 75 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
## Unreleased

- The double click threshold value was raised to `400ms`
- **Breaking:** `wayland-csd-frame` is now used as a part of the public interface.

## 0.6.0
- Update the `smithay-client-toolkit` to `0.17.0`
Expand Down
5 changes: 3 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@ documentation = "https://docs.rs/sctk-adwaita"
description = "Adwaita-like SCTK Frame"

[dependencies]
smithay-client-toolkit = { version = "0.17.0", default_features = false }
tiny-skia = { version = "0.11", default-features = false, features = ["std", "simd"] }
log = "0.4"
memmap2 = { version = "0.5.8", optional = true }
smithay-client-toolkit = { git = "https://github.com/smithay/client-toolkit", default_features = false }
tiny-skia = { version = "0.11", default-features = false, features = ["std", "simd"] }
wayland-csd-frame = { version = "0.2.2", default-features = false, features = ["wayland-backend_0_1"] }

# Draw title text using crossfont `--features crossfont`
crossfont = { version = "0.5.0", features = ["force_system_fontconfig"], optional = true }
Expand Down
103 changes: 72 additions & 31 deletions examples/window.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
// Based on https://github.com/Smithay/client-toolkit/blob/master/examples/themed_window.rs.

use std::sync::Arc;
use std::time::Duration;
use std::{convert::TryInto, num::NonZeroU32};

use smithay_client_toolkit::reexports::client::{
globals::registry_queue_init,
protocol::{wl_output, wl_pointer, wl_seat, wl_shm, wl_surface},
Connection, Proxy, QueueHandle,
};
use smithay_client_toolkit::reexports::protocols::xdg::shell::client::xdg_toplevel::ResizeEdge as XdgResizeEdge;
use smithay_client_toolkit::{
compositor::{CompositorHandler, CompositorState},
delegate_compositor, delegate_output, delegate_pointer, delegate_registry, delegate_seat,
Expand All @@ -23,7 +25,6 @@ use smithay_client_toolkit::{
},
shell::{
xdg::{
frame::{DecorationsFrame, FrameAction, FrameClick},
window::{DecorationMode, Window, WindowConfigure, WindowDecorations, WindowHandler},
XdgShell, XdgSurface,
},
Expand All @@ -35,6 +36,7 @@ use smithay_client_toolkit::{
},
subcompositor::SubcompositorState,
};
use wayland_csd_frame::{CursorIcon, DecorationsFrame, FrameAction, FrameClick, ResizeEdge};

use sctk_adwaita::{AdwaitaFrame, FrameConfig};

Expand All @@ -60,7 +62,6 @@ fn main() {
.expect("Failed to create pool");

let window_surface = compositor_state.create_surface(&qh);
let pointer_surface = compositor_state.create_surface(&qh);

let window =
xdg_shell_state.create_window(window_surface, WindowDecorations::ServerDefault, &qh);
Expand All @@ -81,7 +82,7 @@ fn main() {
registry_state,
seat_state,
output_state,
_compositor_state: compositor_state,
compositor_state,
subcompositor_state: Arc::new(subcompositor_state),
shm_state,
_xdg_shell_state: xdg_shell_state,
Expand All @@ -95,10 +96,9 @@ fn main() {
buffer: None,
window,
window_frame: None,
pointer_surface,
themed_pointer: None,
set_cursor: false,
cursor_icon: String::from("diamond_cross"),
cursor_icon: CursorIcon::Crosshair,
};

// We don't draw immediately, the configure will notify us when to first draw.
Expand All @@ -118,7 +118,7 @@ struct SimpleWindow {
registry_state: RegistryState,
seat_state: SeatState,
output_state: OutputState,
_compositor_state: CompositorState,
compositor_state: CompositorState,
subcompositor_state: Arc<SubcompositorState>,
shm_state: Shm,
_xdg_shell_state: XdgShell,
Expand All @@ -132,10 +132,9 @@ struct SimpleWindow {
buffer: Option<Buffer>,
window: Window,
window_frame: Option<AdwaitaFrame<Self>>,
pointer_surface: wl_surface::WlSurface,
themed_pointer: Option<ThemedPointer>,
set_cursor: bool,
cursor_icon: String,
cursor_icon: CursorIcon,
}

impl CompositorHandler for SimpleWindow {
Expand All @@ -149,6 +148,16 @@ impl CompositorHandler for SimpleWindow {
// Not needed for this example.
}

fn transform_changed(
&mut self,
_: &Connection,
_: &QueueHandle<Self>,
_: &wl_surface::WlSurface,
_: wl_output::Transform,
) {
// Not needed for this example.
}

fn frame(
&mut self,
conn: &Connection,
Expand Down Expand Up @@ -305,9 +314,16 @@ impl SeatHandler for SimpleWindow {
if capability == Capability::Pointer && self.themed_pointer.is_none() {
println!("Set pointer capability");
println!("Creating pointer theme");
let surface = self.compositor_state.create_surface(qh);
let themed_pointer = self
.seat_state
.get_pointer_with_theme(qh, &seat, ThemeSpec::default())
.get_pointer_with_theme(
qh,
&seat,
self.shm_state.wl_shm(),
surface,
ThemeSpec::default(),
)
.expect("Failed to create pointer");
self.themed_pointer.replace(themed_pointer);
}
Expand Down Expand Up @@ -346,8 +362,10 @@ impl PointerHandler for SimpleWindow {
self.cursor_icon = self
.window_frame
.as_mut()
.and_then(|frame| frame.click_point_moved(&event.surface, x, y))
.unwrap_or("diamond_cross")
.and_then(|frame| {
frame.click_point_moved(Duration::ZERO, &event.surface.id(), x, y)
})
.unwrap_or(CursorIcon::Crosshair)
.to_owned();

if &event.surface == self.window.wl_surface() {
Expand All @@ -362,17 +380,29 @@ impl PointerHandler for SimpleWindow {
}
println!("Pointer left");
}
Motion { .. } => {
if let Some(new_cursor) = self
.window_frame
.as_mut()
.and_then(|frame| frame.click_point_moved(&event.surface, x, y))
{
Motion { time } => {
if let Some(new_cursor) = self.window_frame.as_mut().and_then(|frame| {
frame.click_point_moved(
Duration::from_millis(time as u64),
&event.surface.id(),
x,
y,
)
}) {
self.set_cursor = true;
self.cursor_icon = new_cursor.to_owned();
}
}
Press { button, serial, .. } | Release { button, serial, .. } => {
Press {
button,
serial,
time,
}
| Release {
button,
serial,
time,
} => {
let pressed = if matches!(event.kind, Press { .. }) {
true
} else {
Expand All @@ -385,11 +415,9 @@ impl PointerHandler for SimpleWindow {
_ => continue,
};

if let Some(action) = self
.window_frame
.as_mut()
.and_then(|frame| frame.on_click(click, pressed))
{
if let Some(action) = self.window_frame.as_mut().and_then(|frame| {
frame.on_click(Duration::from_millis(time as u64), click, pressed)
}) {
self.frame_action(pointer, serial, action);
}
} else if pressed {
Expand Down Expand Up @@ -420,8 +448,23 @@ impl SimpleWindow {
FrameAction::Maximize => self.window.set_maximized(),
FrameAction::UnMaximize => self.window.unset_maximized(),
FrameAction::ShowMenu(x, y) => self.window.show_window_menu(seat, serial, (x, y)),
FrameAction::Resize(edge) => self.window.resize(seat, serial, edge),
FrameAction::Resize(edge) => {
let edge = match edge {
ResizeEdge::None => XdgResizeEdge::None,
ResizeEdge::Top => XdgResizeEdge::Top,
ResizeEdge::Bottom => XdgResizeEdge::Bottom,
ResizeEdge::Left => XdgResizeEdge::Left,
ResizeEdge::TopLeft => XdgResizeEdge::TopLeft,
ResizeEdge::BottomLeft => XdgResizeEdge::BottomLeft,
ResizeEdge::Right => XdgResizeEdge::Right,
ResizeEdge::TopRight => XdgResizeEdge::TopRight,
ResizeEdge::BottomRight => XdgResizeEdge::BottomRight,
_ => return,
};
self.window.resize(seat, serial, edge);
}
FrameAction::Move => self.window.move_(seat, serial),
_ => (),
}
}
}
Expand All @@ -435,13 +478,11 @@ impl ShmHandler for SimpleWindow {
impl SimpleWindow {
pub fn draw(&mut self, conn: &Connection, qh: &QueueHandle<Self>) {
if self.set_cursor {
let _ = self.themed_pointer.as_mut().unwrap().set_cursor(
conn,
&self.cursor_icon,
self.shm_state.wl_shm(),
&self.pointer_surface,
1,
);
let _ = self
.themed_pointer
.as_mut()
.unwrap()
.set_cursor(conn, self.cursor_icon);
self.set_cursor = false;
}

Expand Down
3 changes: 2 additions & 1 deletion src/buttons.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use log::warn;
use tiny_skia::{FillRule, PathBuilder, PixmapMut, Rect, Stroke, Transform};

use smithay_client_toolkit::shell::xdg::window::{WindowManagerCapabilities, WindowState};
use wayland_csd_frame::{WindowManagerCapabilities, WindowState};

use crate::{theme::ColorMap, Location, SkiaResult};

Expand Down Expand Up @@ -113,6 +113,7 @@ impl Buttons {
self.buttons_left.last().map(|button| button.end_x())
}

#[allow(clippy::too_many_arguments)]
pub fn draw(
&self,
start_x: f32,
Expand Down
Loading

0 comments on commit 6454c81

Please sign in to comment.