'onClick' 기능을 사용하여 명령에서 gnome에 알림을 만드는 방법은 무엇입니까?

'onClick' 기능을 사용하여 명령에서 gnome에 알림을 만드는 방법은 무엇입니까?

간단한 명령을 사용하여 알림을 만들 수 있습니다.

전:notify-send 'SUPER IMPORTANT!' 'This is an urgent message!' -u critical

클릭 가능하게 만들고 클릭하는 동안 스크립트를 실행할 수 있나요? 좋다

이와 같이 Nautilus 파일 관리자가 보낸 알림을 클릭하면 여기에 이미지 설명을 입력하세요

새창이 바로 열립니다. 하지만 우리의 맞춤 알림은 아무 것도 하지 않습니다. 사용자 정의 알림을 클릭하면 활동을 수행하는 방법.

답변1

흥미로운 질문입니다!

여기에 이미지 설명을 입력하세요

...아래 주제에 대한 탐구를 촉발한 것은 무엇입니까?UB 실험 저장소. 결과는 다음과 같은 옵션이 있는 알림/메시지 팝업입니다.

(선택적) 클릭 기능이 포함된 알림의 예

  • 모서리가 나타나도록 설정
  • 클릭 시 실행되는 명령 설정
  • 제목을 정하세요(굵게)
  • 메시지 텍스트 설정
  • 아이콘을 설정하다
  • 수명(초) 설정 <- 스니펫에 설정

처음 네 가지 옵션은 인수를 설정한 경우에만 적용되며, 다르게 설정하지 않는 한 모서리는 기본적으로 오른쪽 하단(기본)으로 설정됩니다.

수명 길이는 하드코딩되어 있으며, 다르게 설정하지 않는 한 기본값은 10초입니다.

노트

  • 이러한 알림은 있는 그대로 전달되지 않으므로 dbus'듣기'가 불가능합니다. 추가 개발은 Dbus 힌트에서만 창을 호출하여 Gtk 루프를 활성 상태로 유지하는 데몬과 같은 백그라운드 프로세스로 만드는 것입니다.
  • 많은 값/기본 설정을 gsettings로 이동할 수 있습니다.

설정 방법

  • 스니펫을 빈 파일에 복사하고, 다른 이름으로 저장하고 alternotify.py, 실행 가능하게 만듭니다.
  • 다음 옵션을 조합하여 실행하고 필요한 것을 선택하세요.

    • 클릭 명령:command="command_to_run"
    • 제목:title="Title to show"
    • 메시지 텍스트(본문):body="Text body of the notification message, text, text, text"
    • 아이콘(아이콘 이름에서):icon="icon-name"
    • 표시할 모서리, 1 = NE, 2 = NW, 3 = SE, 4 = SW:position=1

완전한 명령은 다음과 같습니다.

/path/to/alternotify.py title="Missing applet" body="To use this functionality, you need to run previews. Click this notification to switch it on." icon="budgie-hotcorners-symbolic" command="gedit /home/jacob/Bureaublad/Kap" position=4

여기에 이미지 설명을 입력하세요

코드

#!/usr/bin/env python3
import gi
gi.require_version("Gtk", "3.0")
from gi.repository import Gtk, Gdk, GLib
import sys
import subprocess

class NotifyWindow(Gtk.Window):

    def __init__(self):
        Gtk.Window.__init__(self)
        self.set_decorated(False)
        distance = 80 # gsettings
        winwidth = 300 # gsettings
        winheight = 80 # gsettings
        self.set_default_size(winwidth, winheight)
        self.maingrid = Gtk.Grid()
        self.add(self.maingrid)  
        self.set_space()
        self.winpos = 4 # gsettings? default = SE
        self.get_args()
        self.currage = 0
        self.targetage = 10 # gsettings,life seconds
        GLib.timeout_add_seconds(1, self.limit_windowlife) 
        self.maingrid.show_all()
        self.position_popup(self.winpos, winwidth, winheight, distance)
        self.show_all()
        Gtk.main()

    def get_winpos(self, arg):
        self.winpos = int(arg)

    def limit_windowlife(self):
        if self.currage >= self.targetage:
            Gtk.main_quit()
        self.currage = self.currage + 1;
        return True        

    def position_popup(self, winpos, winwidth, winheight, distance):
        monitordata = self.get_primarymonitor()
        winsize = self.get_size()
        winwidth, winheight = winsize.width, winsize.height
        monitor_xpos = monitordata[2]
        monitor_ypos = monitordata[3]
        monitor_width = monitordata[0]
        monitor_height = monitordata[1]

        if winpos == 1:
            wintargetx = monitor_xpos + distance
            wintargety = monitor_ypos + distance
        elif winpos == 2:
            wintargetx = monitor_width + monitor_xpos - winwidth - distance
            wintargety = monitor_ypos + distance
        elif winpos == 3:
            wintargetx = monitor_xpos + distance
            wintargety = monitor_ypos + monitor_height - (
                distance + winheight
            )
        elif winpos == 4:
            wintargetx = monitor_width + monitor_xpos - winwidth - distance
            wintargety = monitor_ypos + monitor_height - (
                distance + winheight
            )
        self.move(wintargetx, wintargety)

    def get_primarymonitor(self):
        # see what is the resolution on the primary monitor
        prim = Gdk.Display.get_default().get_primary_monitor()
        geo = prim.get_geometry()
        [width, height, screen_xpos, screen_ypos] = [
            geo.width, geo.height, geo.x, geo.y
        ]
        height = geo.height
        return width, height, screen_xpos, screen_ypos

    def show_title(self, title):
        title_label = Gtk.Label(label=title)
        self.maingrid.attach(title_label, 3, 1, 1, 1)
        title_label.set_xalign(0)
        # set title bold
        self.noti_css = ".title {font-weight: bold; padding-bottom: 5px;}"
        self.provider = Gtk.CssProvider.new()
        self.provider.load_from_data(self.noti_css.encode())
        self.set_textstyle(title_label, "title")

    def set_body(self, body):
        body_label = Gtk.Label(
            label=body
        )
        self.maingrid.attach(body_label, 3, 2, 1, 1)
        body_label.set_xalign(0)
        body_label.set_size_request(250, -1)
        body_label.set_line_wrap(True)

    def set_icon(self, icon):
        self.maingrid.attach(Gtk.Label(label="\t"), 2, 0, 1, 1)
        if not "/" in icon:
            newicon = Gtk.Image.new_from_icon_name(
                icon, Gtk.IconSize.DIALOG
            )
            self.maingrid.attach(newicon, 1, 1, 1, 2)
            self.maingrid.show_all()

    def get_args(self):
        args = sys.argv[1:]
        funcs = [
            self.show_title, self.set_body, self.set_icon,
            self.connect_action, self.get_winpos,
        ]
        argnames = ["title", "body", "icon", "command", "position"]
        for arg in args:
            argdata = arg.split("=")
            argname = argdata[0]
            arg = argdata[1]
            try:
                i = argnames.index(argname)
                funcs[i](arg)
            except ValueError:
                print("invalid argument:", arg)             

    def connect_action(self, arg):
        self.connect("button_press_event", self.run_command, arg)
        pass

    def set_textstyle(self, widget, style):
        widget_cont = widget.get_style_context()
        widget_cont.add_class(style)
        Gtk.StyleContext.add_provider(
            widget_cont,
            self.provider,
            Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION,
        )               

    def run_command(self, event, key, command):
        if key.get_button()[1] == 1:
            subprocess.Popen(["/bin/bash", "-c", command])

    def set_space(self):
        for cell in [[0, 0], [100, 0], [0, 100], [100, 100]]:
            self.maingrid.attach(
                Gtk.Label(label="\t"), cell[0], cell[1], 1, 1
            )

NotifyWindow()

관련 정보