2017-08-01 39 views
5

我最近研究了gtk設計模式,並找到了in-app notifications。有關何時使用它的說明,但沒有提及gtk api。gtk應用內通知API參考

我已經搜索過它,但只找到了GNotificationGApplication.send_notification,但是這會將通知發送到桌面環境。

任何人都可以幫助找到一個教程或示例代碼做一個應用程序內通知?

+0

我想用這個自己:)所以我冒昧地報告沒有API參考鏈接在頁面底部。 – theGtknerd

+0

我今天已經寄了一封郵件,也許它不會花太長時間:) – microo8

+0

microo8 @theGtknerd檢查答案。 –

回答

5

應用程序通知「小部件」是小部件,css類和行爲的混合。

您應該在計劃使用應用程序通知的窗口中使用Gtk.Overlay,然後將Gtk.Frame與預定義的app-notification類一起使用。 Gtk.Frame應該封裝在Gtk.Revealer中以允許顯示「幻燈片」轉換。

這裏是一塊空地UI文件(APP-notification.ui)用一個例子:

<?xml version="1.0" encoding="UTF-8"?> 
<!-- Generated with glade 3.20.0 --> 
<interface> 
    <requires lib="gtk+" version="3.20"/> 
    <object class="GtkWindow" id="window1"> 
    <property name="can_focus">False</property> 
    <property name="default_width">440</property> 
    <property name="default_height">250</property> 
    <child> 
     <object class="GtkOverlay" id="overlay1"> 
     <property name="visible">True</property> 
     <property name="can_focus">False</property> 
     <child> 
      <object class="GtkBox" id="box1"> 
      <property name="visible">True</property> 
      <property name="can_focus">False</property> 
      <property name="orientation">vertical</property> 
      <child> 
       <object class="GtkLabel" id="label1"> 
       <property name="visible">True</property> 
       <property name="can_focus">False</property> 
       <property name="label" translatable="yes">APP-NOTIFICATION EXAMPLE</property> 
       </object> 
       <packing> 
       <property name="expand">True</property> 
       <property name="fill">True</property> 
       <property name="position">0</property> 
       </packing> 
      </child> 
      <child> 
       <object class="GtkButton" id="button1"> 
       <property name="label" translatable="yes">show app-notification</property> 
       <property name="visible">True</property> 
       <property name="can_focus">True</property> 
       <property name="receives_default">True</property> 
       </object> 
       <packing> 
       <property name="expand">False</property> 
       <property name="fill">True</property> 
       <property name="position">1</property> 
       </packing> 
      </child> 
      </object> 
      <packing> 
      <property name="index">-1</property> 
      </packing> 
     </child> 
     <child type="overlay"> 
      <object class="GtkOverlay" id="overlay2"> 
      <property name="visible">True</property> 
      <property name="can_focus">False</property> 
      <property name="valign">start</property> 
      <child> 
       <object class="GtkRevealer" id="revealer2"> 
       <property name="visible">True</property> 
       <property name="can_focus">False</property> 
       <property name="halign">center</property> 
       <child> 
        <object class="GtkFrame" id="frame2"> 
        <property name="visible">True</property> 
        <property name="can_focus">False</property> 
        <property name="label_xalign">0</property> 
        <property name="shadow_type">none</property> 
        <child> 
         <object class="GtkBox" id="box2"> 
         <property name="visible">True</property> 
         <property name="can_focus">False</property> 
         <property name="spacing">20</property> 
         <child> 
          <object class="GtkLabel" id="label2"> 
          <property name="visible">True</property> 
          <property name="can_focus">False</property> 
          <property name="label" translatable="yes">This is an app-notification. Click the button to dismiss</property> 
          </object> 
          <packing> 
          <property name="expand">False</property> 
          <property name="fill">True</property> 
          <property name="position">0</property> 
          </packing> 
         </child> 
         <child> 
          <object class="GtkButton" id="button2"> 
          <property name="visible">True</property> 
          <property name="can_focus">True</property> 
          <property name="receives_default">True</property> 
          <property name="relief">none</property> 
          <child> 
           <object class="GtkImage" id="image2"> 
           <property name="visible">True</property> 
           <property name="can_focus">False</property> 
           <property name="icon_name">window-close-symbolic</property> 
           </object> 
          </child> 
          <style> 
           <class name="image-button"/> 
          </style> 
          </object> 
          <packing> 
          <property name="expand">False</property> 
          <property name="fill">True</property> 
          <property name="position">1</property> 
          </packing> 
         </child> 
         </object> 
        </child> 
        <child type="label_item"> 
         <placeholder/> 
        </child> 
        <style> 
         <class name="app-notification"/> 
        </style> 
        </object> 
       </child> 
       </object> 
       <packing> 
       <property name="pass_through">True</property> 
       <property name="index">-1</property> 
       </packing> 
      </child> 
      </object> 
     </child> 
     </object> 
    </child> 
    </object> 
</interface> 

結果格萊德:

enter image description here

,這裏是使用一些Python代碼上一個glade文件,併爲通知提供一些動態行爲,以便您可以通過單擊按鈕來查看它。林間空地文件應命名爲APP-notification.ui,否則更改代碼,以反映給定的名稱:

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

def onButtonShow(self): 
    revealer.set_reveal_child(True) 

def onButtonClose(self): 
    revealer.set_reveal_child(False) 

builder = Gtk.Builder() 
builder.add_from_file("app-notification.ui") 

window = builder.get_object("window1") 

buttonShow = builder.get_object("button1") 
buttonClose = builder.get_object ("button2") 
revealer = builder.get_object("revealer2") 

buttonShow.connect ("clicked", onButtonShow) 
buttonClose.connect ("clicked", onButtonClose) 
window.connect ("destroy", Gtk.main_quit) 
window.show_all() 

Gtk.main() 
+1

出於好奇,你在哪裏找到這些信息? – theGtknerd

+0

@theGtknerd說實話,我真的不記得從哪裏,但我確實掌握了css文件來搜索一些類的名字。像累積搜索一樣,獲取A,A獲得B ...然後轉化爲C.一種好的方法是記住一些使用你想要的東西的應用程序,然後在github或gnome git上找到源代碼並嘗試找到他們已經實施了它。無論如何,你們都是對的。沒有關於如何實現它的文檔。也許我們應該在Gnome/HowDoI中添加這樣的內容 –