use crate::window::CursorIcon;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum CsdHitZone {
None,
Titlebar,
MinimizeButton,
MaximizeButton,
CloseButton,
ResizeBorderTop,
ResizeBorderBottom,
ResizeBorderLeft,
ResizeBorderRight,
ResizeCornerTopLeft,
ResizeCornerTopRight,
ResizeCornerBottomLeft,
ResizeCornerBottomRight,
}
impl CsdHitZone {
pub fn is_button(&self) -> bool {
matches!(
self,
CsdHitZone::MinimizeButton | CsdHitZone::MaximizeButton | CsdHitZone::CloseButton
)
}
pub fn is_titlebar(&self) -> bool {
matches!(self, CsdHitZone::Titlebar)
}
pub fn is_resize(&self) -> bool {
matches!(
self,
CsdHitZone::ResizeBorderTop
| CsdHitZone::ResizeBorderBottom
| CsdHitZone::ResizeBorderLeft
| CsdHitZone::ResizeBorderRight
| CsdHitZone::ResizeCornerTopLeft
| CsdHitZone::ResizeCornerTopRight
| CsdHitZone::ResizeCornerBottomLeft
| CsdHitZone::ResizeCornerBottomRight
)
}
pub fn to_x11_moveresize_direction(&self) -> Option<u32> {
match self {
CsdHitZone::ResizeCornerTopLeft => Some(0),
CsdHitZone::ResizeBorderTop => Some(1),
CsdHitZone::ResizeCornerTopRight => Some(2),
CsdHitZone::ResizeBorderRight => Some(3),
CsdHitZone::ResizeCornerBottomRight => Some(4),
CsdHitZone::ResizeBorderBottom => Some(5),
CsdHitZone::ResizeCornerBottomLeft => Some(6),
CsdHitZone::ResizeBorderLeft => Some(7),
CsdHitZone::Titlebar => Some(8), _ => None,
}
}
pub fn to_wayland_resize_edge(&self) -> Option<u32> {
match self {
CsdHitZone::ResizeBorderTop => Some(1), CsdHitZone::ResizeBorderBottom => Some(2), CsdHitZone::ResizeBorderLeft => Some(4), CsdHitZone::ResizeCornerTopLeft => Some(5), CsdHitZone::ResizeCornerBottomLeft => Some(6), CsdHitZone::ResizeBorderRight => Some(8), CsdHitZone::ResizeCornerTopRight => Some(9), CsdHitZone::ResizeCornerBottomRight => Some(10), _ => None,
}
}
pub fn to_win32_nchittest(&self) -> isize {
match self {
CsdHitZone::None => 1, CsdHitZone::Titlebar => 2, CsdHitZone::MinimizeButton => 8, CsdHitZone::MaximizeButton => 9, CsdHitZone::CloseButton => 20, CsdHitZone::ResizeBorderLeft => 10, CsdHitZone::ResizeBorderRight => 11, CsdHitZone::ResizeBorderTop => 12, CsdHitZone::ResizeCornerTopLeft => 13, CsdHitZone::ResizeCornerTopRight => 14, CsdHitZone::ResizeBorderBottom => 15, CsdHitZone::ResizeCornerBottomLeft => 16, CsdHitZone::ResizeCornerBottomRight => 17, }
}
pub fn cursor_icon(&self) -> CursorIcon {
match self {
CsdHitZone::None => CursorIcon::Default,
CsdHitZone::Titlebar => CursorIcon::Default,
CsdHitZone::MinimizeButton | CsdHitZone::MaximizeButton | CsdHitZone::CloseButton => {
CursorIcon::Pointer
}
CsdHitZone::ResizeBorderTop | CsdHitZone::ResizeBorderBottom => CursorIcon::ResizeN,
CsdHitZone::ResizeBorderLeft | CsdHitZone::ResizeBorderRight => CursorIcon::ResizeE,
CsdHitZone::ResizeCornerTopLeft | CsdHitZone::ResizeCornerBottomRight => {
CursorIcon::ResizeNw
}
CsdHitZone::ResizeCornerTopRight | CsdHitZone::ResizeCornerBottomLeft => {
CursorIcon::ResizeNe
}
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum ControlPlacement {
#[default]
Right, Left, }
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct CsdGeometry {
pub titlebar_height: f32,
pub button_width: f32,
pub button_height: f32,
pub resize_border_thickness: f32,
pub placement: ControlPlacement,
}
impl Default for CsdGeometry {
fn default() -> Self {
Self {
titlebar_height: 32.0,
button_width: 46.0,
button_height: 32.0,
resize_border_thickness: 6.0,
placement: ControlPlacement::Right,
}
}
}
impl CsdGeometry {
pub fn new(
titlebar_height: f32,
button_width: f32,
button_height: f32,
resize_border_thickness: f32,
placement: ControlPlacement,
) -> Self {
Self {
titlebar_height,
button_width,
button_height,
resize_border_thickness,
placement,
}
}
pub fn macos_style() -> Self {
Self {
titlebar_height: 28.0,
button_width: 24.0,
button_height: 28.0,
resize_border_thickness: 5.0,
placement: ControlPlacement::Left,
}
}
pub fn hit_test(&self, window_width: f32, window_height: f32, x: f32, y: f32) -> CsdHitZone {
if x < 0.0 || y < 0.0 || x > window_width || y > window_height {
return CsdHitZone::None;
}
let t = self.resize_border_thickness;
if x < t && y < t {
return CsdHitZone::ResizeCornerTopLeft;
}
if x > window_width - t && y < t {
return CsdHitZone::ResizeCornerTopRight;
}
if x < t && y > window_height - t {
return CsdHitZone::ResizeCornerBottomLeft;
}
if x > window_width - t && y > window_height - t {
return CsdHitZone::ResizeCornerBottomRight;
}
if y < t {
return CsdHitZone::ResizeBorderTop;
}
if y > window_height - t {
return CsdHitZone::ResizeBorderBottom;
}
if x < t {
return CsdHitZone::ResizeBorderLeft;
}
if x > window_width - t {
return CsdHitZone::ResizeBorderRight;
}
if y <= self.titlebar_height {
match self.placement {
ControlPlacement::Right => {
let bw = self.button_width;
if x >= window_width - bw {
return CsdHitZone::CloseButton;
}
if x >= window_width - 2.0 * bw {
return CsdHitZone::MaximizeButton;
}
if x >= window_width - 3.0 * bw {
return CsdHitZone::MinimizeButton;
}
CsdHitZone::Titlebar
}
ControlPlacement::Left => {
let bw = self.button_width;
if x <= bw {
return CsdHitZone::CloseButton;
}
if x <= 2.0 * bw {
return CsdHitZone::MinimizeButton;
}
if x <= 3.0 * bw {
return CsdHitZone::MaximizeButton;
}
CsdHitZone::Titlebar
}
}
} else {
CsdHitZone::None
}
}
}
#[derive(Debug, Clone)]
pub struct CsdController {
pub geometry: CsdGeometry,
pub is_dragging: bool,
pub active_zone: CsdHitZone,
pub drag_start_pos: Option<(f32, f32)>,
}
impl Default for CsdController {
fn default() -> Self {
Self {
geometry: CsdGeometry::default(),
is_dragging: false,
active_zone: CsdHitZone::None,
drag_start_pos: None,
}
}
}
impl CsdController {
pub fn new(geometry: CsdGeometry) -> Self {
Self {
geometry,
is_dragging: false,
active_zone: CsdHitZone::None,
drag_start_pos: None,
}
}
pub fn hit_test(&self, window_width: f32, window_height: f32, x: f32, y: f32) -> CsdHitZone {
self.geometry.hit_test(window_width, window_height, x, y)
}
pub fn on_pointer_down(
&mut self,
window_width: f32,
window_height: f32,
x: f32,
y: f32,
) -> CsdHitZone {
let zone = self.hit_test(window_width, window_height, x, y);
self.active_zone = zone;
if zone == CsdHitZone::Titlebar || zone.is_resize() {
self.drag_start_pos = Some((x, y));
} else {
self.drag_start_pos = None;
}
zone
}
pub fn on_pointer_up(&mut self) {
self.is_dragging = false;
self.active_zone = CsdHitZone::None;
self.drag_start_pos = None;
}
pub fn should_start_drag(&mut self, x: f32, y: f32) -> bool {
if let Some((sx, sy)) = self.drag_start_pos {
if (x - sx).abs() > 4.0 || (y - sy).abs() > 4.0 {
self.is_dragging = true;
return true;
}
}
false
}
}