// SPDX-License-Identifier: LGPL-2.1-only
// Copyright (C) 2026 Liberux Labs, S. L. <info@liberux.net>

use smithay_client_toolkit::seat::pointer::PointerEvent;
use smithay_client_toolkit::reexports::client::
{
	protocol::wl_pointer::WlPointer,
	Connection, QueueHandle,
};

use crate::app::App;
use crate::event_loop::{ AppData, SurfaceFocus };

impl<A: App> AppData<A>
{
	pub( super ) fn on_pointer_release(
		&mut self,
		_conn:    &Connection,
		_qh:      &QueueHandle<Self>,
		_pointer: &WlPointer,
		event:    &PointerEvent,
	)
	{
		let focus = self.focus_for_surface( &event.surface )
			.unwrap_or( SurfaceFocus::Main );
		let pos = self.surface( focus ).to_physical( event.position.0, event.position.1 );
		self.pointer_pos = pos;
		self.app.on_pointer_move( pos.x, pos.y );
		self.app.on_pointer_button( pos.x, pos.y, false );
		let global_drag = self.has_active_long_press_drag();
		let swipe       = self.swipe_config( focus );
		let events_out =
		{
			let ss = self.surface_mut( focus );
			ss.needs_redraw = true;
			ss.gesture.on_release( pos, &ss.frame.widget_rects, &swipe, global_drag )
		};
		self.apply_release_events( focus, events_out );
		// Cancel any held-button repeat — the press is
		// over, so the timer no longer has anything to
		// fire against.
		self.stop_button_repeat();
		// Slider drag (if any) just ended — cursor reverts
		// from `Grabbing` to whatever the hovered widget
		// asks for.
		self.dispatch_cursor_shape( focus );
	}
}
