Skip to content

feat(terminalwidget): replace 3rdparty terminal widget with self-developed implementation - #595

Closed
Kakueeen wants to merge 19 commits into
linuxdeepin:masterfrom
Kakueeen:feat/terminalwidget-self-rework
Closed

Kakueeen wants to merge 19 commits into
linuxdeepin:masterfrom
Kakueeen:feat/terminalwidget-self-rework

Conversation

@Kakueeen

Copy link
Copy Markdown
Contributor

Summary

Rework the terminal rendering stack: add a self-developed DTerminalWidget that matches the behavior of the 3rdparty terminal widget, with a compile-time switch so the original backend stays available. The new widget is wired into the full terminal pipeline and aligned with the original behavior through a series of fixes.

Changes

  • Add self-dev data layer (util/history/filter/screen), VT parser and keyboard translator
  • Add pty/session/encoding modules and view/colorscheme/search modules, then wire the facade into the full terminal pipeline
  • Align behaviors with the 3rdparty widget: fish prompt redraw, initial focus, bottom-row rendering, cursor blink, Tab key handling, close confirmation, link interaction and input handling
  • Show shell path in tab title instead of a fixed "Terminal"
  • Install data resources (keyboard layouts, color schemes) and resolve paths at runtime: system directories first, source tree fallback for build-tree runs
  • Fix selection alignment, zoom anchoring and other rendering issues
  • Standardize comments in the new code

Testing

  • Builds cleanly with the new backend enabled
  • Rendering and interactions verified in a sandbox environment and on a real machine: links, selection, zoom/scroll anchoring, theme switching (both installed and build-tree runs)

…ile-time backend switch

- 新增 CMake 开关 BUILD_SELF_TERMWIDGET(默认 OFF,行为与现状完全一致;
  ON 时自研源码直接编入 deepin-terminal 主目标,不再构建/链接三方库)
- 新增 DTerminalDriver 驱动接口:统一抽象终端组件能力,双后端共用
- 新增 DCompatTerminalDriver:三方 QTermWidget 适配器,仅 OFF 模式编译,
  接口与信号逐条透传,保证默认路径行为不变
- 新增 DTerminalWidget:自研门面骨架(M2-M5 逐步填充实现)
- 新增 dterminalcompat:Konsole:: 命名空间兼容 shim(字体大小全局变量、光标形状枚举)
- TermWidget 组合化双模式改造:OFF 继承 QTermWidget(对外接口零变化),
  ON 持有驱动并转发,旧名委托方法为 D2 上层兼容例外
- define.h/mainwindow 双模式适配(TERM_WIDGET_NAME 焦点判定、配色列表入口)
- tests 双轨适配:ON 模式暂缓依赖三方内部类的 3 个用例(渲染模块落地后恢复)

验证:OFF 全量构建通过、UT 426/427 通过(1 例为 master 基线同样失败的
墙钟计时抖动用例,非本次引入);ON 全量构建通过(主程序 + 测试二进制)
…reen)

- dterminalutil: DCharWidth(East Asian Width 与组合字符经系统 ICU 判定;
  GB18030 字体宽度修正表为 deepin 自有贡献移植改名)+ DTerminalUtil::displayWidth
  + 字符单元基础类型(DTerminalChar/DTerminalCellColor/DTerminalPaletteEntry、
  样式与行属性位定义)
- dterminalhistory: 自研整合版历史缓冲,None/Buffer/File 三种存储
  归一为单一体系;临时文件前缀 dterminal-;以顺序读写替代 mmap 优化层
- dtextfilter: DTextFilter/DRegexFilter/DUrlFilter/DTextFilterChain
  + DTerminalTextScanner(终端图像展开为文本、热点标记、shell 提示符与
  当前命令提取,M4 接入会话管理器)+ DUrlExtractor(OSC8 超链接)
- dterminalscreen: 自研屏幕缓冲(光标/滚动/选区/历史联动/尺寸回绕;
  zsh 光标修正与滚动毛刺修复为 deepin 自有功能移植)
- 构建: ON 模式链接 ICU::uc 并编入四个新模块;tests 自研头文件
  仅 ON 模式加入目标

验证: OFF 全量构建零回归;ON 全量构建(主程序+测试二进制)通过
- dterminalparser: 自研 VT 解析器(结构级重写)。词法分析按
  字符分类+累积缓冲推导序列类型;分发表覆盖控制字符/ESC/CSI/
  私有模式/VT52/SGR(含 24 位真彩与 256 色子参数);
  xterm 扩展含鼠标协议(1000/1002/1003/1005/1006/1015)、焦点上报、
  括贴模式(2004)、DECRQM 模式查询应答、DECSCUSR 光标形状
- deepin 自有功能移植:OSC52 分块处理(64MB 上限)、DECRQM 的
  $ 中间字节吸收、GB 系编码双解码路径、iconv GB18030 2005 标准
  探测、bash 长提示符退格修正、zsh resize 光标修正钩子
- DCharSequenceTable: 组合字符序列哈希表(自研实现,
  哈希耗尽时经收集器清理)
- dkeyboardtranslator: 自研键盘映射表(解析器内置整合),
  keytab 条件/修饰键/终端状态/命令/转义解析,\* 通配符展开为
  xterm 修饰参数
- 构建: ON 源清单加入两个新模块

验证: OFF 全量构建零回归;ON 全量构建(主程序+测试二进制)通过
- dterminalpty: 自研 PTY 直实现(posix_openpt + 子进程修饰器接管
  控制终端,整合历史实现中进程/pty设备/事件层三级结构);
  卸载命令检测(remove/purge 正则)、expect 密码屏蔽、乱码输出
  过滤、非 UTF-8 分块缓冲、GB 系编码写入转换均为 deepin 自有
  功能移植
- dshellsession: 自研会话(PTY+解析器串接、标题/图标 OSC 处理、
  监视计时、工作目录跟踪、前景进程检测、标题参数定时更新);
  DProcessInfo 自研实现(/proc 读取);DSessionManager 会话注册表
  (shell 提示符/命令上下文、空闲广播)
- dencodingdetector: 编码识别与转换(deepin 自有模块移植改名):
  chardet1 主识别 + uchardet/ICU 交叉验证、中文环境 GB18030 优先、
  iconv 转换与 GB18030 2005/2022 差异码点替换保护
- 构建: ON 模式链接 chardet/uchardet/ICU::i18n;
  修正 tests 自研头文件 glob 的追加时机

验证: OFF 全量构建零回归;ON 全量构建(主程序+测试二进制)通过
- dterminalview: 自研终端显示部件(整合历史实现中显示部件与
  屏幕窗口两层)。渲染管线(样式分段聚合、扩展字符解析、光标
  四种形状、线绘字符)、选区模型(词/行/列/三击)、鼠标协议、
  拖放、IME、闪烁/响铃、滚动条与输出跟踪、过滤链热点下划线
- deepin 自有功能移植:Ctrl+点击定位光标、触控手势(捏合缩放/
  点击/惯性滑动)、输出滚动许可、选区状态跟踪、字号钳制
- dcolorscheme: 自研配色解析(.colorscheme 数据文件、背景随机化、
  透明度)+ DColorSchemeRegistry(含 reloadScheme 扩展)
- dhistorysearch: 自研历史搜索(10K 行分块、正/反向与正转反
  位置换算、中文损失统计 BUG22626 修复移植)
- 资源归位: 配色与键盘布局数据迁入 src/terminalwidget/data/
  (schemes/keytabs,内容保留仅重命名路径;KDE3 .schema 弃用)
- 构建: ON 定义数据目录宏并编入三个新模块

验证: OFF 全量构建零回归;ON 全量构建(主程序+测试二进制)通过
- DTerminalWidget 由 M1 骨架转为实装门面:组装会话/解析/屏幕/
  显示/扫描器/配色/搜索,全部驱动接口落地
- 接线:会话与视图信号转发驱动接口;视图输入转会话;
  尺寸联动;屏幕切换;默认 keytab 加载;搜索流程(含 deepin
  正转反位置换算与中文统计);卸载确认回调注入上层对话框
- TermWidget 自研模式注入卸载处理器(保持同步确认语义)
- 扫描器拥有上下文存取器;修复 flowControlOn 覆盖遗漏
- docs/TERMINALWIDGET_SELF_DEV.md:变更说明(新增/删除/重命名
  清单、类与函数重命名映射、主要逻辑调整、冒烟清单)

验证: OFF 全量构建零回归;ON 全量构建通过;
ON 离屏冒烟启动 12s 无崩溃
…ogram argument

- 修复自研版真显示环境启动即段错误:视图-会话尺寸联动形成
  无限递归(fontMetricChanged → setSize → resizeRequest →
  view.setSize → propagateSize → ...)导致栈溢出;两处尺寸
  未变即返回的守卫打断环路
- 修复 shell 启动后立即退出(fish 等 shell 把重复的程序名参数
  当作脚本执行返回 127):按历史约定剥离参数列表首项(argv[0]
  程序名),与三方 Pty::start 的 arguments.mid(1) 语义一致
- 会话启动失败时发出 shellWarningMessage 并补 homePath 回退

验证: OFF 真显示窗口存活(timeout 124);ON 真显示窗口存活
(timeout 124);两模式全量构建通过
…pat contract

- dterminalcompat: 显式声明兼容面契约(全仓库审计:上层仅引用
  字体全局与光标枚举两类符号)+ 枚举取值编译期断言
- DTerminalView 补完:
  * 滚动条经网格布局定位(左/右/隐藏),内容区改用 contentsRect
    计算行列数(此前滚动条从未布局、不可见)
  * setOpacity 真实生效(背景 alpha 混合,含背景图混合路径)
  * calcGeometry 内容区计算修正(此前误用未初始化的内容高宽)
  * 线绘字符过程化绘制:34 个盒线码点段解析(单线/粗线/双线/
    虚线),未覆盖码点回退字体
  * 手势补完:pan 纵向滑动滚动、swipe 惯性缓动(指数衰减)
- 扫尾:清理 extendSelection 未使用变量

验证: ON 真显示窗口存活(timeout 124);ON/OFF 全量构建通过
…th audit trail

- 说明 dterminaldriver.cpp 仅承载构造锚点与 moc 元对象代码,
  44 个纯虚方法在两个具体实现类中全覆盖(机器审计 44/44):
  DCompatTerminalDriver(三方转发)与 DTerminalWidget(自研组装)
…ttom-row rendering

fish prompt missing after first Enter:
- m_utf8 defaulted to false and was never wired to the session codec, so the
  non-UTF-8 line buffering engaged once Enter set m_isCommandExec; fish's
  redraw chunks contain no newline and use \e[m (not \e[00m) as SGR reset,
  so the ⏎ abandon-line hack and prompt were held in m_pendingBytes forever
  and never reached the parser. sendToProcess now derives m_utf8 from the
  session codec (default UTF-8; line buffering only for GB encodings).

No initial focus in the view:
- add a focus proxy chain TermWidget -> DTerminalWidget -> view, mirroring
  3rdparty QTermWidget::setFocusProxy(terminalDisplay), so the terminal
  accepts keyboard input when first shown without a click.

Bottom row shows only the cursor:
- paintEvent passed endLine as the QRect height ((x,y,w,h) semantics) when
  calling drawContent, so full repaints never covered the last row (and the
  last column); only the 9x20 cursor-blink region ever touched it. Convert
  to proper row/column counts.

View/model alignment (same batch):
- stage getImage at screen stride and copy row-by-row into the view image
  (3rdparty ScreenWindow/TerminalDisplay two-step), removing the silent
  truncation clamp that left stale rows during resize transitions
- mirror konsole calcGeometry row/column formulas (h+1, no -1 bias)
- keep earlier accumulated fixes: cursorRight clamped to grid width, smart
  scroll race (receivedData -> syncOutputTracking before outputReceived),
  session resize forwarding, random scheme HSV offset, wcwidth additions

Cleanup:
- remove the temporary [PIPE]/VDBG debug dumps and now-unused includes
…on with 3rdparty

Cursor blink / focus interaction (konsole TerminalDisplay semantics):
- stop the cursor/text blink timers and reset the hidden phase on focus
  loss, resume them on focus regain, and flip the phase back to visible
  when blinking is disabled (setBlinkingCursor keeps the deepin focus-sync
  fix: no blinking while unfocused after the setting changes)

Tab key:
- override focusNextPrevChild() to return false so Tab/Shift+Tab reach the
  terminal instead of Qt's focus chain (titlebar buttons were stealing the
  key; fish completion broke)
- accept ShortcutOverride for Tab/Backtab/Delete/Home/End/Backspace/
  arrows/Escape like konsole's handleShortcutOverrideEvent, with the host
  veto signal for single-modifier combos

Theme switch:
- re-apply the application palette to the scrollbar on PaletteChange/
  ApplicationPaletteChange so the right strip follows light/dark switches
  (konsole TerminalDisplay::event)

Close confirmation:
- hasLiveProcess now reports foreground-process-group activity instead of
  shell liveness, so closing an idle fish shell no longer asks for
  confirmation (3rdparty hasRunningProcess semantics)
…erminal"

shellTitle() only read the NameRole title, which fish never sets (it
reports the working directory via the OSC 0 user title on every prompt).
Prefer the user title and fall back to NameRole, mirroring 3rdparty
QTermWidget::title() semantics.
…rdparty

- Draw a solid underline only for the hovered link at baseline+underlinePos
  with per-line segments; switch to pointing hand cursor only when hovering
  a link with Ctrl held (paint-time evaluation, as 3rdparty)
- Activate the link hotspot on click; whether to open is gated by the
  shared TermWidget::onUrlActivated Ctrl check
- Rescan filter hotspots on scroll/resize/setScrollBarPosition
  (3rdparty scrolled->updateFilters parity)
- Scroll to end on keypress when pressing-scroll mode is
  MoveEndScreenWindow, so Enter jumps back to bottom after scrolling away
- Clear the current selection when pasting (3rdparty emitSelection parity)
Standardize comments across the terminalwidget module.
Consolidate duplicated implementations and simplify the terminal backend to the self-developed widget only.
自研终端组件的键盘布局与配色资源此前不随程序安装,且路径宏写死
源码目录,打包部署后程序无法读取。本次按 3rdparty qtermwidget 的
布局补齐资源安装与运行期路径解析:

- 资源安装到 /usr/share/deepin-terminal/{kb-layouts,color-schemes},
  其中 keytab 与配色分别按 *.keytab 与 *.colorscheme 过滤,与自研
  组件当前能力对齐
- 数据目录宏由源码路径改为安装路径(KB_LAYOUT_DIR / COLORSCHEMES_DIR)
- 新增 RUNDIR_KEYBOARD_LAYOUT_FIRST 开关,Debug 构建下把 keytab 拷贝
  到构建目录二进制旁,运行期优先读取,找不到时回退安装路径,保证
  本机不安装也能直接运行调试
- 默认安装前缀归一化提前到数据目录宏之前,避免宏记录到 /usr/local
- 配色目录改为按安装路径扫描,运行期行为与 3rdparty 一致
…tree

配色目录改为安装路径后,Debug 构建下未安装时运行程序读不到任何配色,
主题列表为空导致主题切换失效。参照 keytab 的处理,为配色补齐同样的
Debug 运行期回退:

- Debug 构建时同样把配色资源拷贝到构建目录二进制旁 color-schemes/,
  运行期优先读取该目录,找不到时回退安装路径,本机不安装也能直接
  运行并正常切换主题
- listSchemePaths 增加二进制旁目录作为配色来源之一,与 keytab 的
  解析顺序保持一致
键盘布局与配色资源的运行期解析顺序由"工程拷贝优先、安装路径兜底"
调整为"系统目录优先、工程源码兜底":

- 系统安装目录(/usr/share/deepin-terminal/ 下)存在且包含匹配文件时
  优先使用,使调试行为与安装后行为一致
- 系统目录缺失或为空(无 *.keytab/*.colorscheme)时回退工程源码数据
  目录,本机不安装也能正常加载键盘布局与切换主题
- Debug 构建定义 SOURCE_DATA_DIR 宏指向源码数据目录,构建目录不再
  拷贝数据文件;Release 行为不变,仅读取系统目录
- 字体步进与格宽对齐:setVTFont 补 3rdparty 的 full hinting 与
  styleName 设置,drawCharacters 在字形步进与格宽不一致时逐码点
  按占格宽度绘制,解决选中文本跳位、选区边界空隙错乱与 CJK 半字
  裁剪
- 选区坐标:按下时冻结基准行,拖动期间视图滚动/输出跟随下高亮
  与文本保持同栅,选区变化后重取图
- 缩放锚定对齐 3rdparty:重排前捕获贴底意图(只看跟踪标志,去掉
  距底一页容差),输出通知按跟踪标志定位并补偿历史丢弃行;
  scrollTo 先按最新总量同步滚动条范围再设值,修复多步缩放丢贴底
- 焦点判定类名修正为 DTerminalView;自定义主题按文件路径重读;
  Ctrl+左键定位光标;选中文本保留硬换行;窗口标题 %D 目录参数
@sourcery-ai

sourcery-ai Bot commented Nov 19, 2026

Copy link
Copy Markdown

Sorry @Kakueeen, your pull request is larger than the review limit of 150,000 diff characters

@deepin-ci-robot

Copy link
Copy Markdown

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: Kakueeen

The full list of commands accepted by this bot can be found here.

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants