【免费下载链接】yasb
A highly configurable Windows status bar written in Python.
导读
本文围绕 YASB(Yet Another Status Bar,一款用 Python + PyQt6 编写的高度可配置 Windows 状态栏)中的 GitHub 通知组件展开,讲解如何在状态栏中实时展示未读 GitHub 通知数量、如何通过三种方式完成身份认证、如何按类别查看 Issue / Pull Request / 讨论 / 发布等通知,以及如何深度定制弹出菜单的样式与交互。读完本文,你将掌握该组件全部配置参数的含义与取值范围、OAuth 设备流认证的底层机制、标记已读与"全部已读"的实现原理,并能直接复制一套可运行的 YASB 配置。
一、组件概述:它能做什么
GitHub 组件(GithubWidget)的核心职责是把 GitHub 的未读通知接入 Windows 状态栏:
- 状态栏显示:以
{icon}与{data}两个占位符驱动主标签与备用标签,{data}会被替换为未读通知数; - 通知点(notification dot):在图标四角之一绘制一个可配颜色与边距的小圆点,有未读通知时点亮;
- 交互式弹窗:单击组件弹出菜单,按类别分组展示通知卡片,单击卡片即打开浏览器对应页面并同步标记为已读;
- 内置"全部已读":弹窗底部提供一键标记全部已读;
- 多种认证:支持 OAuth 设备流登录、Personal Access Token、环境变量三种方式;
- 评论计数:可选开启,通过一次批量 GraphQL 请求为 Issue、Pull Request、Discussion 拉取评论总数。
在 YASB 中,组件的完整实现位于 src/core/widgets/yasb/github.py,其配置校验模型位于 src/core/validation/widgets/yasb/github.py,数据层与认证层则分别封装在 src/core/widgets/services/github/api.py 和 src/core/widgets/services/github/auth.py。
二、快速开始:最小可用配置
在 YASB 的 config.yaml 的 widgets 段中声明该组件,type 固定为 yasb.github.GithubWidget:
github:
type: "yasb.github.GithubWidget"
options:
label: "<span>\ueba1</span>"
label_alt: "Notifications {data}" # {data} 会被替换为未读通知数
token: ghp_xxxxxxxxxxx # GitHub Personal access tokens (classic)
max_notification: 30 # 弹窗中最多显示的通知条数,上限 50
notification_dot:
enabled: True
corner: "bottom_left" # top_left / top_right / bottom_left / bottom_right
color: "red" # 支持十六进制颜色或颜色名
margin: [1, 1] # 圆点相对所在角落的 x、y 偏移
only_unread: false # 只显示未读,还是显示全部通知
show_comment_count: false # 汇总 Issue、PR、Discussion 的评论总数
reason_filters: [] # 例如 ['mention', 'assign'],按 reason 过滤
max_field_size: 54 # 标题超长时的截断字符数
update_interval: 300 # 通知刷新间隔(秒)
menu:
blur: True # 弹窗启用模糊背景
round_corners: True # 圆角(Windows 10 不支持)
round_corners_type: "normal" # normal / small(Windows 10 不支持)
border_color: "System" # 边框颜色(Windows 10 不支持)
alignment: "right"
direction: "down"
show_categories: false
categories_order: ["PullRequest", "Issue", "CheckSuite", "Release", "Discussion"]
上述 YAML 直接来自官方文档 docs/widgets/(Widget)-Github.md-Github.md) 的配置示例,可直接复制到你的配置文件中。若
label使用<span>...</span>包裹图标,通知点圆点会自动绘制在该图标上;未加<span>的普通文本部分则作为普通标签渲染。
组件注册的默认交互回调为:左键单击打开菜单 / OAuth 登录,右键单击切换主标签与备用标签(对应 toggle_menu 与 toggle_label 两个回调,见 src/core/widgets/yasb/github.py)。
三、全部选项详解
以下表格完整继承了官方文档中的选项定义,并补充了源码 src/core/validation/widgets/yasb/github.py 中确认的默认值与取值范围:
| 选项 | 类型 | 默认值 | 说明 |
|---|---|---|---|
label | string | '{icon}' | 主标签格式串,支持 {icon} 等占位符 |
label_alt | string | '{data} Notifications' | 备用标签格式串,适合展示更多通知细节,{data} 为未读通知数 |
tooltip | boolean | true | 悬停时是否显示工具提示 |
update_interval | integer | 600 | 通知刷新间隔(秒),校验范围 60 ~ 3600 |
token | string | "" | 个人访问令牌;留空走 OAuth,设为 "env" 则从 YASB_GITHUB_TOKEN 环境变量读取 |
max_notification | integer | 30 | 弹窗中最多显示的通知条数,上限 50 |
notification_dot | dict | {'enabled': true, 'corner': 'bottom_left', 'color': 'red', 'margin': [1, 1]} | 通知点(角标圆点)设置 |
hide_empty | boolean | false | 无未读通知时隐藏整个组件 |
only_unread | boolean | false | 仅显示未读通知 |
show_comment_count | boolean | false | 是否请求并展示聚合的评论计数 |
reason_filters | list | [] | 可选的通知 reason 白名单,如 ['mention', 'assign'];空列表返回全部 |
max_field_size | integer | 100 | 标题截断前的最大字符数 |
menu | dict | {'blur': true, 'round_corners': true, 'round_corners_type': 'normal', 'border_color': 'System', 'alignment': 'right', 'direction': 'down', 'offset_top': 6, 'offset_left': 0, 'show_categories': true, 'categories_order': []} | 弹窗菜单设置 |
icons | dict | 见下文默认图标表 | 菜单中各通知类型对应的图标 |
3.1 label 与 label_alt
label:主显示格式。官方推荐用<span>\ueba1</span>之类的方式嵌入图标字体字符,{icon}占位符可动态插入图标信息;label_alt:备用格式,点击右键切换显示。{data}会被替换为未读通知数量(在 src/core/widgets/yasb/github.py 的_update_label中通过part.format(data=notification_count)完成替换)。
另外,当有未读通知时,图标标签会被追加 new-notification CSS 类,无未读时移除,方便你用样式表做高亮(如改变颜色)。
3.2 notification_dot:图标角标圆点
notification_dot 是字典,包含四个键:
enabled:是否启用通知点;corner:圆点出现的位置,取值top_left、top_right、bottom_left、bottom_right(源码中以Corner枚举定义,见 src/core/validation/widgets/yasb/github.py);color:圆点颜色,支持十六进制(如#ef4747)或颜色名(如red);margin:[x, y]形式的边距数组,控制圆点与角落的偏移。
圆点并非 CSS 实现,而是由 NotificationLabel 在 paintEvent 中用 QPainter 按角标位置计算坐标后绘制(圆半径固定为 6 像素,见 src/core/widgets/yasb/github.py)。只有被 <span> 标签包裹的图标标签才会成为 NotificationLabel,从而获得圆点能力。
3.3 hide_empty / only_unread
hide_empty=True时,组件在未读数为 0 时自动隐藏(setVisible(False)),有新通知时重新显示;only_unread决定数据源:为true时仅拉取未读线程(对应 GitHub API 参数all=false),为false时拉取全部线程,由弹窗内new类区分未读项。
3.4 show_comment_count:GraphQL 批量评论计数
开启后,组件会一次性发起一个批量 GraphQL 请求,为 Issue、Pull Request 与 Discussion 拉取评论总数:
- Issue:查询
state comments { totalCount }; - Pull Request:查询
state mergedAt isDraft comments { totalCount } reviewThreads { totalCount },且 PR 的评论总数包含 review 线程(reviewThreads); - Discussion:查询
isAnswered comments { totalCount }。
具体实现在 src/core/widgets/services/github/api.py 的 _enrich_notifications 中,所有查询合并为单个 query { ... } POST 到 https://api.github.com/graphql。开启后,每条通知卡片右侧会多出 comment-icon + comment-count 两个元素。
3.5 reason_filters:按通知原因过滤
GitHub 通知线程自带 reason 字段,reason_filters 可限定只显示指定原因。支持的取值与官方文档一致,包括:assign、author、comment、ci_activity、invitation、manual、mention、review_requested、security_alert、state_change、subscribed、team_mention。留空 [] 则显示所有原因。源码在 _get_all_notifications 中用小写集合做 O(1) 匹配过滤(src/core/widgets/services/github/api.py)。
3.6 icons:菜单图标映射
icons 字典为不同类型通知指定图标(默认值为字体图标 Unicode,来自校验模型 src/core/validation/widgets/yasb/github.py):
| 键 | 用途 | 默认值 |
|---|---|---|
issue | 打开的 Issue | \uf41b |
issue_closed | 已关闭的 Issue | \uf41d |
pull_request | 打开的 PR | \uea64 |
pull_request_closed | 关闭(未合并)的 PR | \uebda |
pull_request_merged | 已合并的 PR | \uf17f |
pull_request_draft | 草稿 PR | \uebdb |
release | 发布通知 | \uea84 |
discussion | 打开的讨论 | \uf442 |
discussion_answered | 已有采纳答案的讨论 | \uf4c0 |
checksuite | Check Suite 通知 | \uf418 |
default | 未显式处理类型的兜底图标 | \uea84 |
github_logo | GitHub 标志(空状态展示用) | \uea84 |
comment | 开启评论计数时前缀评论数的小图标 | \uf41f |
图标的选择逻辑集中在 src/core/widgets/yasb/github.py 的 _resolve_icon_and_states:根据通知类型与 GraphQL 富化出的 issue_state、pull_request_state、pull_request_is_merged、pull_request_is_draft、discussion_is_answered 等字段决定具体图标与附加状态类(如 closed、merged、draft、answered)。
3.7 menu:弹窗菜单
menu 字典控制弹出菜单的观感与排布:
blur:菜单背景模糊;round_corners/round_corners_type/border_color:圆角与边框设置,这三个选项在 Windows 10 上不受支持(官方文档明确标注);alignment:菜单水平对齐,可选left、right;direction:展开方向,可选up、down;offset_top/offset_left:菜单相对屏幕顶/左的偏移;show_categories:按 GitHub 通知类型分组展示。开启后每个分组渲染在一个.section容器内,并带.section-header标题;categories_order:分组顺序,值不区分大小写且须匹配 GitHub 通知类型(如PullRequest、Issue)。未列出的类别自动排到后面。可用类别包括PullRequest、Issue、CheckSuite、Release、Discussion。
当 show_categories 开启时,每个分组内第一张与最后一张通知卡片会额外获得 .first 与 .last 类;关闭分组时,这两个类改加在扁平列表的首尾项上,用于精细调整间距或边框圆角。类别标题的中文/英文可读名称由 _format_category_title 生成(如 Issue → Issues、PullRequest → Pull Requests),见 src/core/widgets/yasb/github.py。
四、认证方式与 Token 管理
组件支持三种认证方式(官方文档明确列出):
4.1 OAuth 设备流登录(推荐)
token 留空即可。点击状态栏组件时,_toggle_menu 发现没有 token,会改为启动 GitHubAuthDialog(src/core/widgets/services/github/auth_dialog.py),流程如下:
- 后台线程请求 GitHub 设备码(Device Code);
- 弹窗展示 10 位
user_code并自动打开浏览器到 GitHub 设备授权页; - 后台线程按
interval轮询授权结果,处理authorization_pending、slow_down、expired_token、access_denied等状态(src/core/widgets/services/github/auth.py); - 授权成功后 token 自动保存并注入
GitHubDataManager,立即触发一次数据拉取。
Token 会被持久化到 %LOCALAPPDATA%\YASB\github_token(源码中由 app_data_path("github_token") 计算路径,见 src/core/widgets/services/github/auth.py)。要退出登录,只需删除该文件并重启 YASB。
4.2 Personal Access Token
在 GitHub 设置页生成一个带 notifications 权限的 Personal Access Token(classic),直接粘贴到 token 选项。注意在 GitHub 的 API 调用中,该 token 通过 Authorization: token <token> 头携带(见 src/core/widgets/services/github/api.py),因此需要具备通知读取权限。
4.3 环境变量
设置 token: env,然后在系统环境中定义 YASB_GITHUB_TOKEN。GithubWidget.__init__ 会在初始化时执行 os.getenv("YASB_GITHUB_TOKEN", "") 读取(src/core/widgets/yasb/github.py)。
三种方式的优先级在源码中体现为:
env标记 → 配置直填 token → 磁盘中已保存的 OAuth token(get_saved_token()),后两者作为前两者的兜底。
五、弹窗交互:查看、跳转与标记已读
点击组件打开弹窗后,每个通知卡片都绑定了鼠标按压事件(_bind_container_mouse_press),交互行为如下:
- 点击通知卡片:先调用
GitHubDataManager.mark_as_read()将该线程标记为已读(底层是对https://api.github.com/notifications/threads/{id}发起PATCH请求,见 src/core/widgets/services/github/api.py),然后关闭菜单并用系统浏览器打开对应 URL,同时移除卡片的new类; - 底部"Mark all as read":对
https://api.github.com/notifications发起PUT请求,携带当前时间last_read_at,一键全部已读(src/core/widgets/services/github/api.py); - 底部左侧计数:显示
Unread notifications (N); - 空状态:无通知时弹窗显示放大的 GitHub 图标与 "No unread notifications" 文案。
通知 URL 由 API 层根据类型生成:PR 会转换为 github.com/.../pull/{n} 形式,Release 指向仓库 releases 页,CheckSuite 指向仓库 actions 页(src/core/widgets/services/github/api.py)。
六、弹窗样式定制
组件与弹窗暴露了完整的 CSS 类体系,可在 YASB 样式表中覆盖。官方文档列出的全部类如下:
.github-widget {}
.github-widget .widget-container {}
.github-widget .widget-container .label {}
/* Popup menu */
.github-menu {}
.github-menu .header {}
.github-menu .footer {}
.github-menu .footer .label {}
.github-menu .contents {} /* 可滚动区域,包含所有通知分组 */
.github-menu .contents .section {} /* 通知容器:关闭分类时为整体,开启分类时为单个类别分组 */
.github-menu .contents .section-header {} /* 类别标题(如 "Issues"、"Pull Requests"),仅 show_categories 开启时可见 */
.github-menu .contents .item {} /* 单个通知卡片 */
.github-menu .contents .item.first {} /* 分组/列表中的第一项 */
.github-menu .contents .item.last {} /* 分组/列表中的最后一项 */
.github-menu .contents .item.new {} /* 未读通知卡片的样式 */
.github-menu .contents .item .title {} /* 卡片标题文本 */
.github-menu .contents .item .description {} /* 卡片描述文本 */
.github-menu .contents .item .icon {} /* 图标默认样式 */
.github-menu .contents .item .icon.issue {} /* Issue 图标 */
.github-menu .contents .item .icon.issue.closed {}
.github-menu .contents .item .icon.issue.open {}
.github-menu .contents .item .icon.pullrequest {} /* PR 图标 */
.github-menu .contents .item .icon.pullrequest.open {}
.github-menu .contents .item .icon.pullrequest.closed {}
.github-menu .contents .item .icon.pullrequest.merged {}
.github-menu .contents .item .icon.pullrequest.draft {}
.github-menu .contents .item .icon.release {} /* Release 图标 */
.github-menu .contents .item .icon.discussion {} /* Discussion 图标 */
.github-menu .contents .item .icon.discussion.answered {}
.github-menu .contents .item .icon.checksuite {} /* Check Suite 图标 */
.github-menu .contents .item .comment-count {} /* 评论数字文本 */
.github-menu .contents .item .comment-icon {} /* 评论图标 */
以下是官方文档提供的弹窗示例样式,可直接粘贴到样式表中使用:
.github-menu {
background-color: rgba(17, 17, 27, 0.2);
max-height: 500px;
min-height: 500px;
min-width: 420px;
}
.github-menu .header {
border-bottom: 1px solid rgba(255, 255, 255, 0.1);
font-size: 15px;
font-weight: 400;
font-family: 'Segoe UI';
padding: 8px;
color: white;
background-color: rgba(17, 17, 27, 0.75);
}
.github-menu .footer {
border-top: 1px solid rgba(255, 255, 255, 0.1);
padding: 4px 8px 6px 8px;
background-color: rgba(17, 17, 27, 0.75);
}
.github-menu .footer .label{
font-size: 12px;
font-family: 'Segoe UI';
color: #9399b2;
}
.github-menu .contents {
background-color: rgba(17, 17, 27, 0.2);
}
.github-menu .contents .item {
min-height: 40px;
padding: 6px 0;
border-bottom: 1px solid rgba(255, 255, 255, 0.075);
}
.github-menu .contents .item:hover {
background-color: rgba(255, 255, 255, 0.05);
border-bottom: 1px solid rgba(255, 255, 255, 0);
}
.github-menu .contents .item .title,
.github-menu .contents .item .description {
color: #9399b2;
font-family: Segoe UI;
}
.github-menu .contents .item .title {
font-size: 14px;
font-weight: 600;
}
.github-menu .contents .item .description {
font-size: 12px;
font-weight: 500
}
.github-menu .contents .item.new .title,
.github-menu .contents .item.new .description {
color: #ffffff
}
.github-menu .contents .item .icon {
font-size: 16px;
padding-left: 8px;
padding-right: 4px;
color: #9399b2;
}
.github-menu .contents .item.new .icon {
color: #3fb950;
}
此外,仓库内置的主题也包含一组默认 GitHub 样式(.github-menu 高度 700px、分组卡片 .section 使用圆角深色卡片、.item.first/.item.last 处理圆角衔接等),定义在 src/core/setup/widgets_styles.py 的 GITHUB_WIDGET_STYLE 中,可作为自定义的起点参考。
七、源码级原理:数据是怎么流动的
7.1 GitHubDataManager:定时拉取与共享数据
所有 GitHub API 调用集中在一个类级别的单例 GitHubDataManager(src/core/widgets/services/github/api.py)中:
- 组件初始化时调用
GitHubDataManager.initialize(...),把token、only_unread、max_notification、update_interval、reason_filters、show_comment_count传入,并启动一个QTimer(update_interval * 1000毫秒)周期触发; - 每次拉取在后台线程中进行(避免阻塞 UI),并通过回调机制把新数据推给已注册的组件(
register_callback+ 加锁复制); - 分页请求通过 GitHub 响应的
Link头解析next页(_parse_link_header),per_page取min(max_notification, 50),因为 GitHub API 单页上限 50,最终截断到max_notification条; fetch_all_notifications是暴露给其他模块(如快速启动面板)的按需拉取入口,允许异常上抛以便调用方展示错误信息。
7.2 GraphQL 富化:状态与评论数从哪来
REST 通知接口只返回线程元数据,不含 Issue/PR 的开启/关闭/合并/草稿状态与评论数。因此 _enrich_notifications 会解析每个 subject 的 API URL 提取 owner / repo / number(_parse_subject_metadata),然后按类型拼装 GraphQL fragment,所有通知合并成一次请求提交到 https://api.github.com/graphql,把结果回写到通知字典(issue_state、pull_request_state、pull_request_is_merged、pull_request_is_draft、discussion_is_answered、comment_count)。这正是图标状态、new 高亮与评论计数得以正确展示的数据基础。
7.3 与快速启动(Quick Launch)的联动
值得留意的是,同一套数据层还被 Quick Launch 的 github_notifications Provider 复用(src/core/widgets/services/quick_launch/providers/github_notifications.py):以 gh 为前缀,可按标题/仓库/类型/原因过滤通知,把未读与已读分组展示,并支持复制 URL、单条已读、全部已读、刷新等上下文菜单动作——如果你在 YASB 中启用了快速启动面板,GitHub 通知便多了一条更轻量的查看路径。
八、仓库内置预设:一键启用的参考配置
仓库为快速搭建提供了一组预设(GITHUB_WIDGET,见 src/core/setup/widgets_config.py),可直接参考其参数组合:
github:
type: "yasb.github.GithubWidget"
options:
label: "<span>\ueba1</span>"
label_alt: "Notifications {data}"
max_notification: 50
notification_dot:
enabled: True
corner: "top_right"
color: "#ef4747"
margin: [0, 3]
only_unread: False
show_comment_count: True
max_field_size: 54
update_interval: 300
icons:
comment: "\ue90a"
menu:
blur: True
round_corners: True
round_corners_type: "normal"
border_color: "System"
alignment: "center"
direction: "down"
show_categories: True
categories_order: ["Issue", "PullRequest", "CheckSuite", "Release", "Discussion"]
该预设展示了几个值得借鉴的取舍:max_notification 提到上限 50、show_comment_count 开启、通知点放在右上角使用品牌红 #ef4747、分类开启且把 Issue 排在首位。你可以在其基础上按第三节的选项说明微调。
九、常见问题速查
- 点击组件弹不出菜单? 未配置任何 token 时点击会先弹出 OAuth 授权对话框而非通知菜单,这是设计行为;授权成功后才能正常查看通知。
update_interval报错? 校验范围固定为 60~3600 秒,超出会被 pydantic 校验拒绝(src/core/validation/widgets/yasb/github.py)。- Windows 10 上菜单圆角不生效?
round_corners、round_corners_type、border_color三个选项在 Windows 10 上不受支持,属预期限制。 - 如何彻底退出登录? 删除
%LOCALAPPDATA%\YASB\github_token文件并重启 YASB。 - 评论计数不显示? 需要同时满足:
show_comment_count: true、通知类型为 Issue / PullRequest / Discussion、且 GraphQL 请求成功(网络异常时仅记录日志并跳过富化,不会导致组件崩溃)。
【免费下载链接】yasb
A highly configurable Windows status bar written in Python.
创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考




