diff --git a/desktop/go.mod b/desktop/go.mod index a57b8355c9..c30f8fc200 100644 --- a/desktop/go.mod +++ b/desktop/go.mod @@ -19,4 +19,4 @@ require ( github.com/godbus/dbus/v5 v5.2.2 // indirect ) -replace github.com/vivaui/viva => github.com/Lithe-IDEA/viva v0.6.3-0.20261114092958-609dd4733b3d +replace github.com/vivaui/viva => github.com/Lithe-IDEA/viva v0.6.3-0.20261114132607-4829f2bdde18 diff --git a/desktop/go.sum b/desktop/go.sum index 8766ceb8d5..84c50b7e3a 100644 --- a/desktop/go.sum +++ b/desktop/go.sum @@ -1,5 +1,5 @@ -github.com/Lithe-IDEA/viva v0.6.3-0.20261114092958-609dd4733b3d h1:P/0ttJIa0g4ZeujeNDMonZh1g7xt8IJ9ap37QcTGgmA= -github.com/Lithe-IDEA/viva v0.6.3-0.20261114092958-609dd4733b3d/go.mod h1:e9In6+J77G7B9ow4enZ2FSOFh7U3q4cQ5iJr3Qn/Olk= +github.com/Lithe-IDEA/viva v0.6.3-0.20261114132607-4829f2bdde18 h1:2SRDSXAk+1fHMJ6B8lTlcPrPoz0zGm0rJXXowlOgDFE= +github.com/Lithe-IDEA/viva v0.6.3-0.20261114132607-4829f2bdde18/go.mod h1:e9In6+J77G7B9ow4enZ2FSOFh7U3q4cQ5iJr3Qn/Olk= github.com/creack/pty v1.1.24 h1:bJrF4RRfyJnbTJqzRLHzcGaZK1NeM5kTC9jGgovnR1s= github.com/creack/pty v1.1.24/go.mod h1:08sCNb52WyoAwi2QDyzUCTgcvVFhUzewun7wtTfvcwE= github.com/danieljoos/wincred v1.2.3 h1:v7dZC2x32Ut3nEfRH+vhoZGvN72+dQ/snVXo/vMFLdQ= diff --git a/desktop/internal/workbench/browser_preview.go b/desktop/internal/workbench/browser_preview.go index b62b463f28..7822bcb8f3 100644 --- a/desktop/internal/workbench/browser_preview.go +++ b/desktop/internal/workbench/browser_preview.go @@ -1,17 +1,19 @@ package workbench import ( - "net/url" + "context" "path/filepath" "strings" "github.com/1lck/Lithe-IDEA/desktop/internal/i18n" + "github.com/vivaui/viva/app" ) // HTML and SVG files are text the editor edits; where the system has a web engine, what they look // like is also shown beside the text (html_preview.go, svg_preview.go). "Open in Browser" shows -// the file in the system browser, as IntelliJ IDEA's does. The file on disk is opened, so unsaved -// changes are saved first. +// the file in the default web browser, as IntelliJ IDEA's does: the browser http links open in, +// whatever application the file's type is associated with, which is often an editor. The file on +// disk is opened, so unsaved changes are saved first. // browserExtensions are the files a browser shows. var browserExtensions = map[string]bool{".html": true, ".htm": true, ".xhtml": true, ".svg": true} @@ -21,22 +23,25 @@ func browserPreviewable(d *document) bool { return d != nil && !d.virtual && d.path != "" && browserExtensions[strings.ToLower(filepath.Ext(d.path))] } -// openInBrowser saves d when it has changes and opens the file in the system browser. +// openInBrowser saves d when it has changes and opens the file in the default web browser. The +// system is asked off the window thread: on macOS the handoff waits for open to report. func (a *App) openInBrowser(p *project, d *document) { if !browserPreviewable(d) { return } open := func() { - address := (&url.URL{Scheme: "file", Path: filepath.ToSlash(d.path)}).String() - if !strings.HasPrefix(filepath.ToSlash(d.path), "/") { - // A Windows path such as C:/x.html is file:///C:/x.html. - address = "file:///" + filepath.ToSlash(d.path) - } - if err := openURL(address); err != nil { - a.notifyError(i18n.SharedFormat(a.language(), "desktop.editorMenu.openInBrowserFailed", map[string]string{"reason": err.Error()})) - return - } - a.performed("editor.openInBrowser", surfaceMenu) + path := d.path + a.background(nil, 0, func(context.Context) func(*App) { + err := app.OpenPathInBrowser(path) + return func(a *App) { + if err != nil { + a.emit(operationFailedLevel, operationFailedEvent, operationFailedFields("editor_open_in_browser", documentLogError(err))) + a.notifyError(i18n.SharedFormat(a.language(), "desktop.editorMenu.openInBrowserFailed", map[string]string{"reason": err.Error()})) + return + } + a.performed("editor.openInBrowser", surfaceMenu) + } + }) } if d.dirty() || d.saving { a.saveBeforeClose(p, d, func(ok bool) { diff --git a/desktop/internal/workbench/browser_preview_test.go b/desktop/internal/workbench/browser_preview_test.go new file mode 100644 index 0000000000..c6f14a2ac0 --- /dev/null +++ b/desktop/internal/workbench/browser_preview_test.go @@ -0,0 +1,55 @@ +package workbench + +import ( + "os" + "strings" + "testing" + + "github.com/1lck/Lithe-IDEA/desktop/internal/core" + "github.com/1lck/Lithe-IDEA/desktop/internal/i18n" +) + +// noticeTexts are the texts of the notification history, newest first, for failure messages. +func noticeTexts(a *App) []string { + texts := make([]string, 0, len(a.notifications.list)) + for _, entry := range a.notifications.list { + texts = append(texts, entry.text) + } + return texts +} + +// TestOpenInBrowserGivesTheFileToTheDefaultBrowser: Open in Browser on the bar of an HTML file's +// page saves the edit the file has, then hands the file on disk to the default web browser, +// through Viva's checks. It is neither the file's own association, which is often an editor, nor +// a file:// address, which Viva refuses since a web address is http, https or mailto. +func TestOpenInBrowserGivesTheFileToTheDefaultBrowser(t *testing.T) { + heldHTMLWaits(t) + h := newHarness(t, &testKernel{Client: core.NewClient()}) + fakePreviews(h) + d := openHTML(t, h, testHTML) + button := i18n.Shared(h.app.language(), "desktop.editorMenu.openInBrowser") + + h.app.post(func(a *App) { a.insertForTest(d, "") }) + h.settle() + h.click(center(h.hitLabeled(button))) + h.await("the file to be opened or refused", func() bool { + _, opened := h.driver.LastOpenedInBrowser() + return opened || len(h.app.notifications.list) > 0 + }) + if path, ok := h.driver.LastOpenedInBrowser(); !ok || path != d.path { + t.Fatalf("Open in Browser gave the browser %q (%v), want the file %q; notices %q", path, ok, d.path, noticeTexts(h.app)) + } + if path, ok := h.driver.LastOpenedPath(); ok { + t.Fatalf("Open in Browser also opened %q with its file association", path) + } + if address, ok := h.driver.LastOpenedURL(); ok { + t.Fatalf("Open in Browser also opened the address %q", address) + } + if len(h.app.notifications.list) != 0 { + t.Fatalf("Open in Browser told %q", noticeTexts(h.app)) + } + saved, err := os.ReadFile(d.path) + if err != nil || !strings.Contains(string(saved), "") { + t.Fatalf("the edit was not saved before the file opened: %q (%v)", saved, err) + } +} diff --git a/desktop/internal/workbench/logevents_test.go b/desktop/internal/workbench/logevents_test.go index 85edafe18f..fa317dcfe0 100644 --- a/desktop/internal/workbench/logevents_test.go +++ b/desktop/internal/workbench/logevents_test.go @@ -42,6 +42,7 @@ func TestLogEventsMatchCatalog(t *testing.T) { {operationFailedLevel, operationFailedEvent, operationFailedFields("git_commit", failure)}, {operationFailedLevel, operationFailedEvent, operationFailedFields("settings_save", failure)}, {operationFailedLevel, operationFailedEvent, operationFailedFields("workspace_reveal_recent", failure)}, + {operationFailedLevel, operationFailedEvent, operationFailedFields("editor_open_in_browser", failure)}, {operationFailedLevel, operationFailedEvent, operationFailedFields("workspace_open", failure)}, {operationFailedLevel, operationFailedEvent, operationFailedFields("workspace_recent_reduce", failure)}, {operationFailedLevel, operationFailedEvent, operationFailedFields("workspace_recent_save", failure)}, diff --git a/shared/platform-feature-matrix/features/embedded-viewers.json b/shared/platform-feature-matrix/features/embedded-viewers.json index 501f938f0f..665845a5bc 100644 --- a/shared/platform-feature-matrix/features/embedded-viewers.json +++ b/shared/platform-feature-matrix/features/embedded-viewers.json @@ -24,7 +24,9 @@ "desktop/internal/core/html_preview.go", "desktop/internal/workbench/html_preview_test.go", "desktop/internal/webpreview/site_test.go", - "desktop/internal/core/html_preview_test.go" + "desktop/internal/core/html_preview_test.go", + "desktop/internal/workbench/browser_preview.go", + "desktop/internal/workbench/browser_preview_test.go" ] }, "windows": { @@ -48,11 +50,13 @@ "desktop/internal/core/html_preview.go", "desktop/internal/workbench/html_preview_test.go", "desktop/internal/webpreview/site_test.go", - "desktop/internal/core/html_preview_test.go" + "desktop/internal/core/html_preview_test.go", + "desktop/internal/workbench/browser_preview.go", + "desktop/internal/workbench/browser_preview_test.go" ] }, "owner": "Editor", - "verification": "分别打开 HTML、SVG、图片、PDF 和二进制文件,验证缩放、错误和源文件返回。 两端将 .html 默认打开程序改成文本编辑器,再点击 HTML 浏览器按钮,确认仍打开默认网页浏览器中的磁盘文件;覆盖中文、空格、# 文件名和无默认浏览器失败提示。 在 Windows 的 Web Viewer 标签中浏览几页后,用 Navigate 菜单或 Ctrl+Alt+←/→ 确认先在网页历史中后退/前进,到达网页历史起点后再次后退回到之前的代码位置。 在 macOS 的 HTML 预览中验证内嵌浏览器:后退/前进/刷新(Shift 强制刷新)、地址栏输入本地路径和网址、−/+/百分比和 ⌘+/⌘−/⌘0 页面缩放(跳转页面后保持)、内容溢出时常驻纵横滚动条、锚点与相对链接在预览内跳转、module 脚本与 fetch 可读取工作区文件、alert/confirm/prompt 与文件选择弹窗、开发者工具可开关;本地隐藏文件和密钥文件不能被页面读取。Windows 的 iframe 预览尚无对应能力。 在 macOS 的 HTML 预览中继续验证:页面有焦点时按 ⌘F 打开查找条,输入文字后页面内选中并高亮匹配项,⌘G / ⇧⌘G 与 Return / ⇧Return 前进后退并回绕,没有匹配时显示“No matches”,Esc 关闭后键盘回到页面;用默认键位,页面有焦点时按 ⌘F / ⌘G / ⇧⌘G 打开并驱动的是预览的查找条而不是编辑器的查找(应用键位监听器对页面的缩放、查找、打印按键让路);分屏时焦点在编辑器里按 ⌘F / ⌘G / ⌘P 仍由编辑器处理。右键点击链接、图片和页面空白处:可以复制链接地址和检查元素,没有“在新窗口打开”,页面空白处的菜单有 Print… 与 Export as PDF…;⌘P 打开作为窗口 sheet 的打印面板,导出 PDF 得到整页的单页 PDF,取消保存面板不写文件。摄像头和麦克风:在 Info.plist 带 NSCameraUsageDescription 与 NSMicrophoneUsageDescription 的构建里,页面调用 getUserMedia 弹出写明页面名称和设备的确认框(默认按钮是 Don’t Allow),页面每次加载后的第一次调用都会询问(同一页面里再次调用是否再询问取决于 WebKit 是否复用已批准的授权,记录实测结果);允许后地址栏右侧出现红色摄像头 / 麦克风指示,点击可暂停(变灰)或停止,页面里的轨道随之静音或结束;没有这两项说明的构建里 navigator.mediaDevices 不存在,页面拿不到设备也不会弹出系统权限框。Lithe 自己的页面(Markdown 预览、Monaco 编辑器和 diff、发行说明)调用 getUserMedia 一律被拒绝且不弹窗,源码扫描测试保证每个 WKWebView 都有人回答摄像头 / 麦克风请求、授权只出现在 WebMediaCapture.swift。地理位置和剪贴板读取使用 WebKit 自带的确认,未单独验证。 自动回归:./scripts/macos/test/test-macos.sh --filter HTMLPreview(真实 WKWebView 覆盖查找、打印、导出 PDF、右键菜单、WebAssembly、经真实应用键位监听器的页面快捷键放行,以及 WebKit 模拟摄像头 / 麦克风下的授权、静音与停止),界面尚未在运行的应用里手动验收。 Viva 新桌面端:分别打开 PNG、JPEG、GIF、WebP、BMP、TIFF、HEIC 图片,确认 34 点工具栏显示文件名、“宽 × 高 · 大小”和 −/百分比/+ 缩放,打开时按留 24 点边距适配且不放大小图,缩放范围为适配比例与 25% 中较小者到 400%、每步 25%,点击百分比回到 100%,Ctrl/⌘ 加滚轮围绕指针缩放,16 点棋盘格背景随深浅主题变化;解码失败显示“无法显示”与“用外部应用打开”,超过 64 MiB 显示过大提示。打开 PDF 确认用系统 web 引擎内置的 PDF 查看器显示并可翻页、缩放,页内链接交给系统浏览器;没有 web 引擎时提示用外部应用打开。打开 SVG 确认左侧源码、右侧图片,标签栏末尾三态切换源码 / 两者 / 仅图片;编辑后约 150 毫秒更新图片且保持缩放与滚动位置,改成无法绘制的内容时保留上一张图片,从未绘制成功时显示失败;超过 4 MiB 不绘制。Windows 需实机确认 WebView2 能否解码 TIFF/HEIC(不能时应落到失败提示)以及自定义协议下的 PDF 查看器。打开工作区里的 HTML 文件,确认左侧源码、右侧页面,标签栏末尾三态切换源码 / 两者 / 仅预览;页面按工作区为根加载相对路径的样式、脚本、图片和 module 脚本,fetch 能读工作区文件,fetch('/.env') 与私钥文件得到 403;编辑后约 150 毫秒页面重新加载并回到原来的滚动位置,未保存的修改也显示;工具栏的后退、前进、重新加载可用,“在浏览器中打开”先保存再用系统浏览器打开磁盘文件;页面里点到站外的 https 链接和 mailto 用系统应用打开、页面停在原处,脚本自动跳到站外的导航被取消,window.open 到站外每秒最多打开一次;页面里的 Cookie 和 localStorage 关闭预览后不保留,也不和其他 HTML 文件共享;超过 8 MiB 显示过大提示。Windows 需实机确认:WebView2 101 以下打不开时只有 HTML 文件改为源码、其他预览不受影响;以 / 开头的绝对路径目前找不到文件(Viva 限制)。自动回归:scripts/desktop/desktop-go.sh test ./internal/workbench ./internal/webpreview ./internal/core -run 'Viewer|SVG|HTML|Site|ImageFit|ImageCanvas|Checkerboard|Serves|Navigation|ReadFile' -count=1。", + "verification": "分别打开 HTML、SVG、图片、PDF 和二进制文件,验证缩放、错误和源文件返回。 两端将 .html 默认打开程序改成文本编辑器,再点击 HTML 浏览器按钮,确认仍打开默认网页浏览器中的磁盘文件;覆盖中文、空格、# 文件名和无默认浏览器失败提示。 在 Windows 的 Web Viewer 标签中浏览几页后,用 Navigate 菜单或 Ctrl+Alt+←/→ 确认先在网页历史中后退/前进,到达网页历史起点后再次后退回到之前的代码位置。 在 macOS 的 HTML 预览中验证内嵌浏览器:后退/前进/刷新(Shift 强制刷新)、地址栏输入本地路径和网址、−/+/百分比和 ⌘+/⌘−/⌘0 页面缩放(跳转页面后保持)、内容溢出时常驻纵横滚动条、锚点与相对链接在预览内跳转、module 脚本与 fetch 可读取工作区文件、alert/confirm/prompt 与文件选择弹窗、开发者工具可开关;本地隐藏文件和密钥文件不能被页面读取。Windows 的 iframe 预览尚无对应能力。 在 macOS 的 HTML 预览中继续验证:页面有焦点时按 ⌘F 打开查找条,输入文字后页面内选中并高亮匹配项,⌘G / ⇧⌘G 与 Return / ⇧Return 前进后退并回绕,没有匹配时显示“No matches”,Esc 关闭后键盘回到页面;用默认键位,页面有焦点时按 ⌘F / ⌘G / ⇧⌘G 打开并驱动的是预览的查找条而不是编辑器的查找(应用键位监听器对页面的缩放、查找、打印按键让路);分屏时焦点在编辑器里按 ⌘F / ⌘G / ⌘P 仍由编辑器处理。右键点击链接、图片和页面空白处:可以复制链接地址和检查元素,没有“在新窗口打开”,页面空白处的菜单有 Print… 与 Export as PDF…;⌘P 打开作为窗口 sheet 的打印面板,导出 PDF 得到整页的单页 PDF,取消保存面板不写文件。摄像头和麦克风:在 Info.plist 带 NSCameraUsageDescription 与 NSMicrophoneUsageDescription 的构建里,页面调用 getUserMedia 弹出写明页面名称和设备的确认框(默认按钮是 Don’t Allow),页面每次加载后的第一次调用都会询问(同一页面里再次调用是否再询问取决于 WebKit 是否复用已批准的授权,记录实测结果);允许后地址栏右侧出现红色摄像头 / 麦克风指示,点击可暂停(变灰)或停止,页面里的轨道随之静音或结束;没有这两项说明的构建里 navigator.mediaDevices 不存在,页面拿不到设备也不会弹出系统权限框。Lithe 自己的页面(Markdown 预览、Monaco 编辑器和 diff、发行说明)调用 getUserMedia 一律被拒绝且不弹窗,源码扫描测试保证每个 WKWebView 都有人回答摄像头 / 麦克风请求、授权只出现在 WebMediaCapture.swift。地理位置和剪贴板读取使用 WebKit 自带的确认,未单独验证。 自动回归:./scripts/macos/test/test-macos.sh --filter HTMLPreview(真实 WKWebView 覆盖查找、打印、导出 PDF、右键菜单、WebAssembly、经真实应用键位监听器的页面快捷键放行,以及 WebKit 模拟摄像头 / 麦克风下的授权、静音与停止),界面尚未在运行的应用里手动验收。 Viva 新桌面端:分别打开 PNG、JPEG、GIF、WebP、BMP、TIFF、HEIC 图片,确认 34 点工具栏显示文件名、“宽 × 高 · 大小”和 −/百分比/+ 缩放,打开时按留 24 点边距适配且不放大小图,缩放范围为适配比例与 25% 中较小者到 400%、每步 25%,点击百分比回到 100%,Ctrl/⌘ 加滚轮围绕指针缩放,16 点棋盘格背景随深浅主题变化;解码失败显示“无法显示”与“用外部应用打开”,超过 64 MiB 显示过大提示。打开 PDF 确认用系统 web 引擎内置的 PDF 查看器显示并可翻页、缩放,页内链接交给系统浏览器;没有 web 引擎时提示用外部应用打开。打开 SVG 确认左侧源码、右侧图片,标签栏末尾三态切换源码 / 两者 / 仅图片;编辑后约 150 毫秒更新图片且保持缩放与滚动位置,改成无法绘制的内容时保留上一张图片,从未绘制成功时显示失败;超过 4 MiB 不绘制。Windows 需实机确认 WebView2 能否解码 TIFF/HEIC(不能时应落到失败提示)以及自定义协议下的 PDF 查看器。打开工作区里的 HTML 文件,确认左侧源码、右侧页面,标签栏末尾三态切换源码 / 两者 / 仅预览;页面按工作区为根加载相对路径的样式、脚本、图片和 module 脚本,fetch 能读工作区文件,fetch('/.env') 与私钥文件得到 403;编辑后约 150 毫秒页面重新加载并回到原来的滚动位置,未保存的修改也显示;工具栏的后退、前进、重新加载可用,“在浏览器中打开”先保存,再把磁盘文件交给默认网页浏览器(打开 http 链接的应用),不跟随 .html 的文件关联:把 .html 的默认打开程序改成文本编辑器后仍在浏览器里打开,没有默认浏览器时提示失败原因(Windows 需实机确认按 http 协议找到默认浏览器);页面里点到站外的 https 链接和 mailto 用系统应用打开、页面停在原处,脚本自动跳到站外的导航被取消,window.open 到站外每秒最多打开一次;页面里的 Cookie 和 localStorage 关闭预览后不保留,也不和其他 HTML 文件共享;超过 8 MiB 显示过大提示。Windows 需实机确认:WebView2 101 以下打不开时只有 HTML 文件改为源码、其他预览不受影响;以 / 开头的绝对路径目前找不到文件(Viva 限制)。自动回归:scripts/desktop/desktop-go.sh test ./internal/workbench ./internal/webpreview ./internal/core -run 'Viewer|SVG|HTML|Site|ImageFit|ImageCanvas|Checkerboard|Serves|Navigation|ReadFile|OpenInBrowser' -count=1。", "legacy": { "revision": "5a54c41aa37a4e6e6b68918a4b05765b9dd07a36", "macos": {