feat(sender): 串流窗口支持点击与拖拽控制

- video_window 增加 InputHandler 回调,WM_LBUTTONDOWN/MOUSEMOVE/LBUTTONUP/CAPTURECHANGED
  映射为 0/2/1/3,移动事件按 60Hz 限流
- Paint 记录帧尺寸与实际绘制矩形,ClientToFrame 换算到画面像素并过滤黑边
- video_texture 通过方法通道转发 pointer 事件到 Dart,析构时清空回调
- bridge.dart 新增 onVideoPointer,main.dart 以 msg_type=0 触摸帧下发(压力 1000/0)
This commit is contained in:
tongtong committed 2026-11-27 15:06:46 +08:00
1 parent 03a5853da7
commit a102d10089
5 files changed
+166 -2

No files matched your search

@@ -1,5 +1,7 @@
#include "video_window.h"
#include <windowsx.h>
#include <algorithm>
#include <cmath>
@@ -12,6 +14,9 @@ constexpr UINT kMsgFrameReady = WM_APP + 0x51;
// 窗口线程内部:视频宽高比变化,需要按比例重设窗口大小。
constexpr UINT kMsgApplyAspect = WM_APP + 0x52;
// 拖动时限流下发移动事件(约 60Hz),避免高频 WM_MOUSEMOVE 塞满控制通道。
constexpr ULONGLONG kMoveIntervalMs = 16;
// 最小客户区尺寸,避免窗口被拖成一条缝。
constexpr int kMinClientWidth = 320;
constexpr int kMinClientHeight = 180;
@@ -130,12 +135,20 @@ void VideoWindow::Close() {
provider_ = nullptr;
aspect_ratio_ = 0.0;
applied_aspect_ = 0.0;
frame_width_ = 0;
frame_height_ = 0;
image_rect_ = RECT{};
dragging_ = false;
}
void VideoWindow::SetCloseHandler(CloseHandler handler) {
close_handler_ = std::move(handler);
}
void VideoWindow::SetInputHandler(InputHandler handler) {
input_handler_ = std::move(handler);
}
void VideoWindow::OnFrameAvailable() {
if (!open_.load()) {
return;
@@ -201,6 +214,45 @@ LRESULT VideoWindow::WndProc(HWND hwnd, UINT message, WPARAM wparam,
case WM_SIZE:
InvalidateRect(hwnd, nullptr, FALSE);
return 0;
case WM_LBUTTONDOWN:
// 画面内点击 = 在手机上点一下:先捕获鼠标,拖动时才能持续收到 WM_MOUSEMOVE。
SetFocus(hwnd);
SetCapture(hwnd);
dragging_ = true;
last_move_tick_ = GetTickCount64();
DispatchPointerAt(0, POINT{GET_X_LPARAM(lparam), GET_Y_LPARAM(lparam)});
return 0;
case WM_MOUSEMOVE: {
// 只有按住左键才下发移动(悬停不注入),并做 60Hz 限流。
if (!dragging_) {
return 0;
}
const ULONGLONG now = GetTickCount64();
if (now - last_move_tick_ < kMoveIntervalMs) {
return 0;
}
last_move_tick_ = now;
DispatchPointerAt(2, POINT{GET_X_LPARAM(lparam), GET_Y_LPARAM(lparam)});
return 0;
}
case WM_LBUTTONUP: {
if (!dragging_) {
return 0;
}
// 先清标志再 ReleaseCapture:否则 WM_CAPTURECHANGED 会补发一次取消。
dragging_ = false;
ReleaseCapture();
DispatchPointerAt(1, POINT{GET_X_LPARAM(lparam), GET_Y_LPARAM(lparam)});
return 0;
}
case WM_CAPTURECHANGED: {
// 窗口失焦、被别处抢走鼠标:补一个 cancel,避免手机上残留按下的手指。
if (dragging_) {
dragging_ = false;
DispatchPointerAt(3, last_point_);
}
return 0;
}
case WM_KEYDOWN:
if (wparam == VK_ESCAPE) {
// 关窗即停止投屏(由 close_handler_ 通知 Flutter)。
@@ -218,6 +270,10 @@ LRESULT VideoWindow::WndProc(HWND hwnd, UINT message, WPARAM wparam,
provider_ = nullptr;
aspect_ratio_ = 0.0;
applied_aspect_ = 0.0;
frame_width_ = 0;
frame_height_ = 0;
image_rect_ = RECT{};
dragging_ = false;
CloseHandler handler = close_handler_;
if (handler) {
handler();
@@ -264,6 +320,12 @@ void VideoWindow::Paint() {
const int offset_x = client.left + (client_width - draw_width) / 2;
const int offset_y = client.top + (client_height - draw_height) / 2;
// 记住这一帧的尺寸和实际绘制矩形,鼠标点击时据此换算成画面像素。
frame_width_ = width;
frame_height_ = height;
image_rect_ = RECT{offset_x, offset_y, offset_x + draw_width,
offset_y + draw_height};
BITMAPINFO info = {};
info.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
info.bmiHeader.biWidth = width;
@@ -277,6 +339,10 @@ void VideoWindow::Paint() {
StretchDIBits(hdc, offset_x, offset_y, draw_width, draw_height, 0, 0, width,
height, frame->data(), &info, DIB_RGB_COLORS, SRCCOPY);
} else {
// 还没有画面:清掉坐标映射,避免拿上一路的尺寸误点。
frame_width_ = 0;
frame_height_ = 0;
image_rect_ = RECT{};
// 还没画面时给个提示:窗口是在点「请求投屏」后就弹出的,
// 纯黑会让人以为卡死。
HGDIOBJ previous = SelectObject(hdc, GetStockObject(DEFAULT_GUI_FONT));
@@ -291,6 +357,38 @@ void VideoWindow::Paint() {
EndPaint(hwnd_, &paint);
}
bool VideoWindow::ClientToFrame(POINT point, int* x, int* y) const {
const int width = image_rect_.right - image_rect_.left;
const int height = image_rect_.bottom - image_rect_.top;
if (frame_width_ <= 0 || frame_height_ <= 0 || width <= 0 || height <= 0) {
return false;
}
// 黑边区域不算点击,否则会点到画面外的坐标。
if (point.x < image_rect_.left || point.x >= image_rect_.right ||
point.y < image_rect_.top || point.y >= image_rect_.bottom) {
return false;
}
const int frame_x = (point.x - image_rect_.left) * frame_width_ / width;
const int frame_y = (point.y - image_rect_.top) * frame_height_ / height;
*x = (std::min)((std::max)(frame_x, 0), frame_width_ - 1);
*y = (std::min)((std::max)(frame_y, 0), frame_height_ - 1);
return true;
}
void VideoWindow::DispatchPointerAt(int action, POINT point) {
last_point_ = point;
InputHandler handler = input_handler_;
if (!handler) {
return;
}
int x = 0;
int y = 0;
if (!ClientToFrame(point, &x, &y)) {
return;
}
handler(action, x, y);
}
void VideoWindow::ApplyAspectToWindow(double aspect) {
if (hwnd_ == nullptr || aspect <= 0.0) {
return;