Gtk3/Gnome 3 色按钮:应用“.needs-attention”css 样式

Gtk3/Gnome 3 colored button: apply ".needs-attention" css styles

前言

在 gnome 3 应用程序中,一些按钮通过有色背景而不是普通按钮的那种灰色来突出显示。这些按钮不仅在使用标准 Adwaita 主题时颜色不同,而且在其他各种主题中也有实现。以下是 Adwaita 和 Flat Plat 主题的普通按钮和彩色按钮示例。

阿德维塔

平面

现在来解决我的问题

我也希望能够在我的 Gtk3 应用程序中实现这些“重要按钮”。在研究如何做到这一点的过程中,我在主题文件中发现这些“重要按钮”有一个特殊的 CSS class,称为 needs-attention。然后我尝试将按钮的 CSS class 也设置为 needs-attention。但是那没有用。我仍然得到一个灰色的标准按钮。为了演示我所做的,我附加了一个最小脚本和 运行 程序的屏幕截图。 “重命名”按钮应该像上面的屏幕截图一样突出显示。我该怎么做?

我的代码

#!/usr/bin/env python3

import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk

class ButtonWindow(Gtk.Window):

    def __init__(self):
        Gtk.Window.__init__(self, title="Needs Attention Button")
        self.set_border_width(10)

        hbox = Gtk.Box(spacing=6)
        self.add(hbox)

        button = Gtk.Button.new_with_label("Remove")
        hbox.pack_start(button, True, True, 0)

        button = Gtk.Button.new_with_mnemonic("Rename")
        button.get_style_context().add_class("needs-attention")
        hbox.pack_start(button, True, True, 0)

win = ButtonWindow()
win.connect("delete-event", Gtk.main_quit)
win.show_all()
Gtk.main()

看来我错的只是我用错了CSSclass。正确的是 suggested-action 而不是 needs-attention。如果我在原始代码中替换该字符串,我将得到以下正确结果:

GNOME 的 Adwaita 主题为建议操作预定义了两个 classes:

  • suggested-action
  • destructive-action

您可以在 GNOME Wiki 上阅读它 HowDoI/Buttons:

While most buttons are gray, it has become more fashionable to emphasize buttons that are the suggested action, by giving them a different color. In the Adwaita theme, the color is blue. ... Recently, another style class for dangerous actions has been added as well: 'destructive-action'. The Adwaita theme uses red for buttons marked as such.

在 Adwaita 上,它们分别是这样的:

预定义的needs-attentionclass,是一个Gtk.Stack/GtkStackchild属性,渲染在Gtk.StackSwitcher调用用户注意,如 GtkStack API Reference:

中所述

The “needs-attention” child property

“needs-attention”          gboolean

Sets a flag specifying whether the child requires the user attention. This is used by the GtkStackSwitcher to change the appearance of the corresponding button when a page needs attention and it is not the current one.

可以在 gtk3-widget-factory 程序中看到生成的视觉辅助,作为需要注意的 child 上的蓝点(假设 Adwaita 主题):