通知气泡中的消息溢出

Raz*_*zor 7 notification python command-line gtk notify-osd

我正在学习如何使用 gtk 通知,似乎通过通知气泡显示的任何输出都有最大溢出大小,大概 10 行左右。所以,如果我想显示的信息不止于此,它就会被压制。有没有办法强制显示整个消息而不会被压制?

顺便说一下,我正在使用 notifyOSD。

Jac*_*ijm 10

我不久前在(现在)已删除的 Q/A 上发布了这个。也许它对你有用。


一个允许(非常)长消息的补丁

下面的“补丁”将允许您在桌面上收到通知:

在(非常)长的通知的情况下,而不是这样:

在此处输入图片说明

你会看到这个:

在此处输入图片说明

消息的持续时间自动设置为文本的长度。

它能做什么

通过notify-osd( notify-send)发送的通知仅限于 appr。120 个字符。
该解决方案使用dbus-monitor. 如果消息超过 120 个字符,它将接管消息并使用“自己的”消息窗口来显示通知,如上所示。

脚本

  1. 设置存在两个部分;de "listen-" 脚本,它拦截通知:

    #!/bin/bash
    
    currdir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
    
    dbus-monitor "interface='org.freedesktop.Notifications'" |\
     grep --line-buffered "string" |\
     grep --line-buffered -e method -e ":" -e '""' -e urgency -e notify -v |\
     grep --line-buffered '.*(?=string)|(?<=string).*' -oPi |\
     grep --line-buffered -v '^\s*$' |\
     xargs -I '{}' $currdir/message {}
    
    Run Code Online (Sandbox Code Playgroud)

    将脚本复制到一个空文件中并将其另存为 catch_notifs.sh

  2. 创建替换通知的脚本:

    #!/usr/bin/env python3
    import subprocess
    import os
    import gi
    gi.require_version('Gtk', '3.0')
    from gi.repository import GObject, Gtk, Gdk, Pango
    from threading import Thread
    import time
    import sys
    
    text = sys.argv[1]
    length = len(text)
    showtime = length/20
    
    def get_screen():
        scr = [s.split("x") for s in subprocess.check_output([
            "xrandr"]).decode("utf-8").split() if "+0+0" in s][0]
        return int(scr[0]) -450
    
    class Splash(Gtk.Window):
    
        def __init__(self):
            Gtk.Window.__init__(self, title="splashtitle")
            maingrid = Gtk.Grid()
            self.add(maingrid)
            maingrid.set_border_width(20)
            label = Gtk.Label(text)
            label.set_line_wrap(True)
            label.set_max_width_chars(45)
            label.modify_font(Pango.FontDescription('Ubuntu 11'))
            maingrid.attach(label, 0, 0, 1, 1)
            self.stop = Thread(target=self.close_window)
            self.stop.start()
    
        def close_window(self):
            time.sleep(showtime)
            Gtk.main_quit()
    
    def splashwindow():
        window = Splash()
        window.set_decorated(False)
        window.set_resizable(False)
        window.override_background_color(Gtk.StateType.NORMAL, Gdk.RGBA(0,0,0,1))
        window.modify_fg(Gtk.StateFlags.NORMAL, Gdk.color_parse("white"))
        # window.set_opacity(0.8)
        window.move(get_screen(), 80)
        window.set_keep_above(True)
        window.show_all()
        window.set_default_size(200, 500)
        GObject.threads_init()
        Gtk.main()
    
    if len(text) > 120:
        subprocess.Popen(["pkill", "notify-osd"])
        splashwindow()
    
    Run Code Online (Sandbox Code Playgroud)

    将上面的脚本复制到一个空文件中,将其另存为(完全正确!)message(无扩展名)并使其可执行

  3. 将两个脚本存储在同一个目录中
  4. 通过命令(从终端窗口)测试运行脚本:

    /bin/bash /path/to/catch_notifs.sh
    
    Run Code Online (Sandbox Code Playgroud)

    (保持运行)

    您可以通过运行(在另一个终端中)来测试设置:

    notify-send '<long_text>'
    
    Run Code Online (Sandbox Code Playgroud)
  5. 如果一切正常,请将其添加到启动应用程序:Dash > 启动应用程序 > 添加。添加命令:

    /bin/bash /path/to/catch_notifs.sh
    
    Run Code Online (Sandbox Code Playgroud)

它应该工作:)