桌面集成#

要求与规范 涵盖了Linux桌面集成的必要方面。这个页面提供了更多可选的桌面集成特性的信息。它也提供了一个指南,便于开发者让程序的用户界面兼容多数Linux桌面和发行版。

这些信息主要是为刚接触Linux的开发者准备的。不过它也适用于那些希望把特定应用推广到更多Linux发行版的开发者。

开发适用于Linux生态的应用可能是一项挑战性的任务,但现有的标准和指南会帮助开发者更方便地适配大多数Linux环境。

检测区域设置#

应用程序的工具集(如Electron、GTK和Qt)默认提供了检测区域的功能。除此此外,你也可以使用 setlocale

门户#

门户是用于安全访问沙箱外资源的框架。沙箱内程序可以使用的特性包括:

  • 检测网络状态

  • 使用文件选择器来打开文件

  • 打开URI

  • 阻止设备关机、休眠或进入待机状态

  • 打印

  • 发送邮件

  • 显示通知

  • 截取屏幕或录屏

Toolkits like GTK and Qt provide transparent support for portals.

If you are not using one of these toolkits, it is possible to access the portals API directly. See the Portals API documentation for more information.

通知#

A number of toolkits and frameworks provide transparent support for Linux desktop notifications. This includes Electron, GTK, KDE and QML.

状态图标#

Status icons are the same concept as the system tray or the taskbar on Windows, or menu bar icons on Mac. These are supported on most Linux distributions, through abstractions such as libappindicator.

部分Linux发行版不显示状态图标;不过您仍然可以设置一个图标,以便它可以在其他发行版中显示出来。出于对兼容性的考虑,建议您只把状态图标作为应用交互逻辑的补充,而不是用于实现特定功能或显示特定信息的唯一入口(包括“最小化到托盘”这一功能)。

XEmbed style icons will function on desktops that support them with the x11 permission.

StatusNotifier#

StatusNotifier style icons will not function without extra permissions as it requires talking to a non-hardenend host service. Risks include impersonation of other software and exploitation of bugs in the host service such as image decoders.

At the very minimum to use StatusNotifier you must have the --talk-name=org.kde.StatusNotifierWatcher permission to register an item.

Depending on the exact implementation of StatusNotifier that your application is using it may need session bus ownership of org.kde.StatusNotifierItem-$PID-$ITEM_ID.

This permission is problematic in Flatpak as the $PID value is often the same in sandboxes and the item will possibly conflict with other applications. However if needed the --own-name=org.kde.* permission will allow this. This opens many new risks including the ability to impersonate any KDE service or application possibly capturing important user data.

Most implementations of StatusNotifer have dropped this requirement but known exceptions to this are Electron versions older than 23.3.0.

Current versions of Electron, Chromium, KNotifications, and libappindicator are known to work without ownership permissions.

窗口控制#

窗口控件是关闭、最大化、最小化窗口的按钮。这些在不同的Linux桌面是不同的,特别是在要显示哪些控件方面。应用程序是否试图遵循这些变化取决于它们的判断力。提供与特定桌面环境所使用的完全相同的控件不应该被视为硬性要求。

从用户体验的角度来看,确保窗口控件出现在窗口的同一侧与其他桌面是很重要的。在Linux上,这是窗口的右侧(就像Windows一样)。

On X11 applications can rely on system-provided titlebars if they don’t want to draw their own window controls. For a consistent Wayland experience applications must always provide their own. Typically toolkits handle this but raw wayland clients can use libdecor for a general solution.

窗口美化#

如果您的应用程序使用深色视觉样式以及系统提供的窗口装饰,那么应该使用 GTK_THEME_VARIANT=dark 这个X11窗口属性,以确保窗口装饰与应用程序窗口的其余部分匹配。通过运行:

xprop -f _GTK_THEME_VARIANT 8u -set _GTK_THEME_VARIANT dark

Theming#

Flatpak applications cannot directly use the system theme. This happens because flatpaks do not have the ability to use data files or libraries in /usr (where system themes are typically located). The solution to this was to package themes as Flatpaks, as relying upon the host to have the correct version for every flatpak defeats the portability benefits it provides. These themes are provided as extensions, to the Freedesktop runtime when the extension point is Gtk, and to the KDE runtime when the extension point is Qt.

The theming system requires Flatpak 0.8.4+ and applications using up to date org.gnome.Platform 3.24+, or org.freedesktop.Platform 1.6+, or org.kde.Platform 5.9+.

Installing themes#

Instructions for Gtk#

The current Gtk themes are packaged in the flathub repository which you can add (if it’s not already added) by running:

$ flatpak remote-add flathub https://flathub.org/repo/flathub.flatpakrepo

To see a list of currently packaged themes you can use the command flatpak search gtk3theme (available since Flatpak version 0.10.1). In case you use an older version of Flatpak than that, you can use the command flatpak remote-ls flathub | grep org.gtk.Gtk3theme. The difference in output between these two commands is that the first prints the application ID, the remote from which the theme comes and the theme’s description, while the second prints only the full name of the theme’s flatpak package.

You can install themes with the command flatpak install flathub org.gtk.Gtk3theme.Foo, replacing Foo with the name of the desired theme.

Instructions for Qt#

For the Qt theming to work, the flatpak packages kstyle and platformtheme must be installed. These are packed in the kdeapps repository which you can add by running:

$ flatpak remote-add kdeapps https://distribute.kde.org/kdeapps.flatpakrepo

Afterwards the two packages can be installed with the following commands:

$ flatpak install kdeapps org.kde.KStyle.Adwaita//5.9
$ flatpak install kdeapps org.kde.PlatformTheme.QGnomePlatform//5.9

Applying themes#

There is no ideal way to specify the theme Flatpak applications use. The applications will try to match the system theme currently being used, if it corresponds to any of the Flatpak themes installed, and will fall back to Adwaita (if they use Gtk2 or Gtk3) or the default Qt theme (if they use Qt) if a corresponding theme isn’t detected.

As of Flatpak 0.10.1, the Flatpak system can detect whether the system themes available correspond to any Flatpak themes available in the repositories, and, if so, will automatically install found themes at update time based upon the gtk-theme Dconf key. This key however can contain only one value, the one of the currently being used theme, which means that the Flatpak versions of matching themes that aren’t currently being used aren’t installed until those themes are enabled. If none of the corresponding system themes are currently being used, the applications will fall back to Adwaita or the default Qt theme.

On X11, Gtk3 picks up the themes via XSettings. Specifically, the GNOME XSettings daemon gsd-xsettings reads the DConf values and converts them into the XSettings values. For this to work, you need an xsettings daemon that is correctly configured. Gtk3 on Wayland picks up themes directly via Dconf. For this to work, you can either use KDE (with kde-gtk-config > 5.11.95), GNOME, which works out of the box, or manually configure the dconf keys under /org/gnome/desktop/interface/. For the DConf option to work on Wayland the application must also be configured to have DConf access.

Other notes on theming#

In regards to icon themes, since Flatpak 0.8.8 the host icons are exposed to the guest, so that there is usually no need for the presence of Flatpak icon themes.

If you use the Global Dark Theme option (removed in GNOME-Tweaks 3.28) in gnome-tweak-tool it will not work as that simply writes to settings.ini which isn’t available in the sandbox. Use dark versions of themes instead if they exist.