当前位置: 首页 > news >正文

AWTK-WIDGET-WEB-VIEW 实现笔记 (3) - MacOS

MacOS 上实现 AWTK-WIDGET-WEB-VIEW 有点麻烦,主要原因是没有一个简单的办法将一个 WebView 嵌入到一个窗口中。所以,我们只能通过创建一个独立的窗口来实现。

在这里插入图片描述

1. 创建窗口

我对 Object-C 不熟悉,也不熟悉 Cocoa 框架,在 ChatGPT 的帮助下,实现了一个简单的窗口创建函数。这个函数接受一个 SDL_Window 指针作为父窗口,然后创建一个新的窗口,将其设置为父窗口的子窗口。这样,新窗口就会在父窗口的上方显示。

在移动窗口时,子窗口也会跟着移动,但是有些延迟。

webview_os_window_t webview_os_window_create(SDL_Window* parent, int x, int y, int w, int h) {SDL_SysWMinfo info;SDL_VERSION(&info.version);SDL_GetWindowWMInfo(parent, &info);NSRect frame = NSMakeRect(x, y, w, h);NSWindow *parentWindow = (NSWindow *)info.info.cocoa.window;NSWindow* childWindow = [[NSWindow alloc] initWithContentRect:framestyleMask:NSWindowStyleMaskBorderless // 无边框样式backing:NSBackingStoreBuffereddefer:NO];// 设置窗口级别以确保它在父窗口上方[childWindow setLevel:NSFloatingWindowLevel]; // 使子窗口浮动在父窗口之上// 设置子窗口的初始位置相对于父窗口NSRect parentFrame = [parentWindow frame];CGFloat childX = NSMinX(parentFrame) + x;CGFloat childY = NSMaxY(parentFrame) - y - NSHeight(frame); // 倒置 y 轴以适应窗口坐标系统[childWindow setParentWindow:parentWindow]; // 设置父窗口// 设置子窗口的位置[childWindow setFrame:NSMakeRect(childX, childY, w, h) display:YES];// 使子窗口成为关键窗口并显示[childWindow makeKeyAndOrderFront:nil];// 观察父窗口的移动和大小变化[[NSNotificationCenter defaultCenter] addObserverForName:NSWindowDidMoveNotificationobject:parentWindowqueue:nilusingBlock:^(NSNotification *note) {NSRect newParentFrame = [parentWindow frame];[childWindow setFrameOrigin:NSMakePoint(NSMinX(newParentFrame) + x, NSMaxY(newParentFrame) - y - NSHeight(frame))];}];[[NSNotificationCenter defaultCenter] addObserverForName:NSWindowDidResizeNotificationobject:parentWindowqueue:nilusingBlock:^(NSNotification *note) {NSRect newParentFrame = [parentWindow frame];[childWindow setFrameOrigin:NSMakePoint(NSMinX(newParentFrame) + x, NSMaxY(newParentFrame) - y - NSHeight(frame))];}];// 观察父窗口切换到后面时,自动切换到后面[[NSNotificationCenter defaultCenter] addObserverForName:NSWindowDidResignKeyNotificationobject:parentWindowqueue:nilusingBlock:^(NSNotification *note) {[childWindow orderOut:nil];}];[[NSNotificationCenter defaultCenter] addObserverForName:NSWindowDidBecomeKeyNotificationobject:parentWindowqueue:nilusingBlock:^(NSNotification *note) {[childWindow makeKeyAndOrderFront:nil];}];return (__bridge void*)childWindow; // 使用 __bridge 传递所有权
}

2. 调整窗口大小

奇怪的是从 subwindow 获取父窗口的方法不起作用,所以通过传递父窗口的指针来调整子窗口的大小。

void webview_os_window_move_resize(SDL_Window* parent, webview_os_window_t subwindow, int x, int y, int w, int h) {SDL_SysWMinfo info;SDL_VERSION(&info.version);SDL_GetWindowWMInfo(parent, &info);NSWindow *childWindow = (__bridge NSWindow*)subwindow;NSWindow *parentWindow = info.info.cocoa.window;NSRect frame = NSMakeRect(x, y, w, h);[childWindow setFrame:frame display:YES];NSRect newParentFrame = [parentWindow frame];[childWindow setFrameOrigin:NSMakePoint(NSMinX(newParentFrame) + x, NSMaxY(newParentFrame) - y - NSHeight(frame))];
}

3. 销毁窗口

销毁窗口时,需要将子窗口从父窗口中移除。

void webview_os_window_destroy(webview_os_window_t subwindow) {NSWindow *childWindow = (__bridge NSWindow*)subwindow;[[NSNotificationCenter defaultCenter] removeObserver:childWindow];[childWindow close];
}
http://www.lryc.cn/news/487372.html

相关文章:

  • PgSQL即时编译JIT | 第1期 | JIT初识
  • Go小记:使用Go实现ssh客户端
  • Nginx Spring boot指定域名跨域设置
  • 深入理解Redis(七)----Redis实现分布式锁
  • Database Advantages (数据库系统的优点)
  • Qt桌面应用开发 第五天(常用控件)
  • 初识Linux · 信号处理 · 续
  • 【Linux】虚拟地址空间,页表,物理内存
  • C++ 并发专题 - 线程安全的单例模式
  • Spring Boot汽车世界:资讯与技术的交汇
  • 力扣 LeetCode 541. 反转字符串II(Day4:字符串)
  • Django5 2024全栈开发指南(一):框架简介、环境搭建与项目结构
  • Uniapp运行环境判断和解决跨端兼容性详解
  • Linux设置开机自动执行脚本 rc-local
  • 驱动开发小问题 -记录一下
  • 学习笔记018——若依框架数据权限功能的实现
  • Nginx文件下载服务器搭建
  • AWD脚本编写_1
  • HarmonyOS 如何获取设备信息(系统、版本、网络连接状态)
  • 2411rust,1.80
  • FPGA 第6讲 简单组合逻辑多路选择器
  • Android Studio开发学习(五)———LinearLayout(线性布局)
  • 大模型(LLMs)RAG 版面分析------文本分块面
  • Web3游戏先锋 Big Time Studios 重磅推出 $OL 通证,赋能 Open Loot 游戏平台
  • Linux—ln(link files)命令使用方法(How to create links on Linux)
  • 学习日记_20241110_聚类方法(K-Means)
  • 解决Oracle DECODE函数字符串截断问题的深度剖析20241113
  • 开源模型应用落地-语音转文本-whisper模型-AIGC应用探索(二)
  • PHP框架 单一入口和多入口以及优缺点
  • PhpSpreadsheet导出图片